Merge branch 'master' of github.com:simple-rules/harmony-benchmark

pull/61/head
Rongjian Lan 6 years ago
commit 1c95f52e81
  1. 21
      benchmark.go
  2. 65
      blockchain/merkle_tree.go
  3. 4
      client/btctxgen/main.go
  4. 4
      client/txgen/main.go
  5. 8
      deploy.sh
  6. 8
      node/node.go
  7. 6
      node/node_handler.go
  8. 4
      node/node_test.go
  9. 0
      waitnode/wait_node.go
  10. 0
      waitnode/wait_node_test.go

@ -10,6 +10,7 @@ import (
"github.com/simple-rules/harmony-benchmark/attack" "github.com/simple-rules/harmony-benchmark/attack"
"github.com/simple-rules/harmony-benchmark/consensus" "github.com/simple-rules/harmony-benchmark/consensus"
"github.com/simple-rules/harmony-benchmark/db"
"github.com/simple-rules/harmony-benchmark/log" "github.com/simple-rules/harmony-benchmark/log"
"github.com/simple-rules/harmony-benchmark/node" "github.com/simple-rules/harmony-benchmark/node"
"github.com/simple-rules/harmony-benchmark/utils" "github.com/simple-rules/harmony-benchmark/utils"
@ -38,12 +39,23 @@ func startProfiler(shardID string, logFolder string) {
} }
} }
func InitLDBDatabase(ip string, port string) (*db.LDBDatabase, error) {
// TODO(minhdoan): Refactor this.
dbFileName := "/tmp/harmony_" + ip + port + ".dat"
var err = os.RemoveAll(dbFileName)
if err != nil {
fmt.Println(err.Error())
}
return db.NewLDBDatabase(dbFileName, 0, 0)
}
func main() { func main() {
ip := flag.String("ip", "127.0.0.1", "IP of the node") ip := flag.String("ip", "127.0.0.1", "IP of the node")
port := flag.String("port", "9000", "port of the node.") port := flag.String("port", "9000", "port of the node.")
configFile := flag.String("config_file", "config.txt", "file containing all ip addresses") configFile := flag.String("config_file", "config.txt", "file containing all ip addresses")
logFolder := flag.String("log_folder", "latest", "the folder collecting the logs of this execution") logFolder := flag.String("log_folder", "latest", "the folder collecting the logs of this execution")
attackedMode := flag.Int("attacked_mode", 0, "0 means not attacked, 1 means attacked, 2 means being open to be selected as attacked") attackedMode := flag.Int("attacked_mode", 0, "0 means not attacked, 1 means attacked, 2 means being open to be selected as attacked")
dbSupported := flag.Int("db_supported", 0, "0 means not db_supported, 1 means db_supported")
flag.Parse() flag.Parse()
// Set up randomization seed. // Set up randomization seed.
@ -74,6 +86,13 @@ func main() {
) )
log.Root().SetHandler(h) log.Root().SetHandler(h)
// Initialize leveldb if dbSupported.
var ldb *db.LDBDatabase = nil
if *dbSupported == 1 {
ldb, _ = InitLDBDatabase(*ip, *port)
}
// Consensus object. // Consensus object.
consensus := consensus.NewConsensus(*ip, *port, shardID, peers, leader) consensus := consensus.NewConsensus(*ip, *port, shardID, peers, leader)
@ -85,7 +104,7 @@ func main() {
// Set logger to attack model. // Set logger to attack model.
attack.GetInstance().SetLogger(consensus.Log) attack.GetInstance().SetLogger(consensus.Log)
// Current node. // Current node.
currentNode := node.New(consensus) currentNode := node.New(consensus, ldb)
// Create client peer. // Create client peer.
clientPeer := distributionConfig.GetClientPeer() clientPeer := distributionConfig.GetClientPeer()
// If there is a client configured in the node list. // If there is a client configured in the node list.

@ -0,0 +1,65 @@
package blockchain
import (
"crypto/sha256"
)
// MerkleTree represent a Merkle tree
type MerkleTree struct {
RootNode *MerkleNode
}
// MerkleNode represent a Merkle tree node
type MerkleNode struct {
Left *MerkleNode
Right *MerkleNode
Data []byte
}
// NewMerkleTree creates a new Merkle tree from a sequence of data
func NewMerkleTree(data [][]byte) *MerkleTree {
var nodes []MerkleNode
if len(data)%2 != 0 {
data = append(data, data[len(data)-1])
}
for _, datum := range data {
node := NewMerkleNode(nil, nil, datum)
nodes = append(nodes, *node)
}
for i := 0; i < len(data)/2; i++ {
var newLevel []MerkleNode
for j := 0; j < len(nodes); j += 2 {
node := NewMerkleNode(&nodes[j], &nodes[j+1], nil)
newLevel = append(newLevel, *node)
}
nodes = newLevel
}
mTree := MerkleTree{&nodes[0]}
return &mTree
}
// NewMerkleNode creates a new Merkle tree node
func NewMerkleNode(left, right *MerkleNode, data []byte) *MerkleNode {
mNode := MerkleNode{}
if left == nil && right == nil {
hash := sha256.Sum256(data)
mNode.Data = hash[:]
} else {
prevHashes := append(left.Data, right.Data...)
hash := sha256.Sum256(prevHashes)
mNode.Data = hash[:]
}
mNode.Left = left
mNode.Right = right
return &mNode
}

@ -200,13 +200,13 @@ func main() {
// Nodes containing utxopools to mirror the shards' data in the network // Nodes containing utxopools to mirror the shards' data in the network
nodes := []*node.Node{} nodes := []*node.Node{}
for _, shardID := range shardIDs { for _, shardID := range shardIDs {
nodes = append(nodes, node.New(&consensus.Consensus{ShardID: shardID})) nodes = append(nodes, node.New(&consensus.Consensus{ShardID: shardID}, nil))
} }
// Client/txgenerator server node setup // Client/txgenerator server node setup
clientPort := configr.GetClientPort() clientPort := configr.GetClientPort()
consensusObj := consensus.NewConsensus("0", clientPort, "0", nil, p2p.Peer{}) consensusObj := consensus.NewConsensus("0", clientPort, "0", nil, p2p.Peer{})
clientNode := node.New(consensusObj) clientNode := node.New(consensusObj, nil)
initClient(clientNode, clientPort, &leaders, &nodes) initClient(clientNode, clientPort, &leaders, &nodes)

@ -276,7 +276,7 @@ func main() {
// Nodes containing utxopools to mirror the shards' data in the network // Nodes containing utxopools to mirror the shards' data in the network
nodes := []*node.Node{} nodes := []*node.Node{}
for _, shardId := range shardIds { for _, shardId := range shardIds {
node := node.New(&consensus.Consensus{ShardID: shardId}) node := node.New(&consensus.Consensus{ShardID: shardId}, nil)
// Assign many fake addresses so we have enough address to play with at first // Assign many fake addresses so we have enough address to play with at first
node.AddTestingAddresses(setting.numOfAddress) node.AddTestingAddresses(setting.numOfAddress)
nodes = append(nodes, node) nodes = append(nodes, node)
@ -285,7 +285,7 @@ func main() {
// Client/txgenerator server node setup // Client/txgenerator server node setup
clientPort := configr.GetClientPort() clientPort := configr.GetClientPort()
consensusObj := consensus.NewConsensus("0", clientPort, "0", nil, p2p.Peer{}) consensusObj := consensus.NewConsensus("0", clientPort, "0", nil, p2p.Peer{})
clientNode := node.New(consensusObj) clientNode := node.New(consensusObj, nil)
if clientPort != "" { if clientPort != "" {
clientNode.Client = client.NewClient(&leaders) clientNode.Client = client.NewClient(&leaders)

@ -25,6 +25,8 @@ if [ -z "$config" ]; then
usage usage
fi fi
db_supported=$2
# Kill nodes if any # Kill nodes if any
./kill_node.sh ./kill_node.sh
@ -48,7 +50,11 @@ while IFS='' read -r line || [[ -n "$line" ]]; do
IFS=' ' read ip port mode shardId <<< $line IFS=' ' read ip port mode shardId <<< $line
#echo $ip $port $mode #echo $ip $port $mode
if [ "$mode" != "client" ]; then if [ "$mode" != "client" ]; then
./bin/benchmark -ip $ip -port $port -config_file $config -log_folder $log_folder& if [ -z "$db_supported" ]; then
./bin/benchmark -ip $ip -port $port -config_file $config -log_folder $log_folder&
else
./bin/benchmark -ip $ip -port $port -config_file $config -log_folder $log_folder -db_supported 1&
fi
fi fi
done < $config done < $config

@ -9,6 +9,7 @@ import (
"github.com/simple-rules/harmony-benchmark/client" "github.com/simple-rules/harmony-benchmark/client"
"github.com/simple-rules/harmony-benchmark/consensus" "github.com/simple-rules/harmony-benchmark/consensus"
"github.com/simple-rules/harmony-benchmark/crypto/pki" "github.com/simple-rules/harmony-benchmark/crypto/pki"
"github.com/simple-rules/harmony-benchmark/db"
"github.com/simple-rules/harmony-benchmark/log" "github.com/simple-rules/harmony-benchmark/log"
"github.com/simple-rules/harmony-benchmark/p2p" "github.com/simple-rules/harmony-benchmark/p2p"
) )
@ -21,6 +22,7 @@ type Node struct {
pendingTransactions []*blockchain.Transaction // All the transactions received but not yet processed for Consensus pendingTransactions []*blockchain.Transaction // All the transactions received but not yet processed for Consensus
transactionInConsensus []*blockchain.Transaction // The transactions selected into the new block and under Consensus process transactionInConsensus []*blockchain.Transaction // The transactions selected into the new block and under Consensus process
blockchain *blockchain.Blockchain // The blockchain for the shard where this node belongs blockchain *blockchain.Blockchain // The blockchain for the shard where this node belongs
db *db.LDBDatabase // LevelDB to store blockchain.
UtxoPool *blockchain.UTXOPool // The corresponding UTXO pool of the current blockchain UtxoPool *blockchain.UTXOPool // The corresponding UTXO pool of the current blockchain
CrossTxsInConsensus []*blockchain.CrossShardTxAndProof // The cross shard txs that is under consensus, the proof is not filled yet. CrossTxsInConsensus []*blockchain.CrossShardTxAndProof // The cross shard txs that is under consensus, the proof is not filled yet.
CrossTxsToReturn []*blockchain.CrossShardTxAndProof // The cross shard txs and proof that needs to be sent back to the user client. CrossTxsToReturn []*blockchain.CrossShardTxAndProof // The cross shard txs and proof that needs to be sent back to the user client.
@ -95,7 +97,7 @@ func (node *Node) countNumTransactionsInBlockchain() int {
} }
// Create a new Node // Create a new Node
func New(consensus *consensus.Consensus) *Node { func New(consensus *consensus.Consensus, db *db.LDBDatabase) *Node {
node := Node{} node := Node{}
// Consensus and associated channel to communicate blocks // Consensus and associated channel to communicate blocks
@ -116,5 +118,9 @@ func New(consensus *consensus.Consensus) *Node {
// Logger // Logger
node.log = node.Consensus.Log node.log = node.Consensus.Log
// Initialize level db.
node.db = db
return &node return &node
} }

@ -5,6 +5,7 @@ import (
"encoding/gob" "encoding/gob"
"net" "net"
"os" "os"
"strconv"
"time" "time"
"github.com/simple-rules/harmony-benchmark/blockchain" "github.com/simple-rules/harmony-benchmark/blockchain"
@ -278,6 +279,11 @@ func (node *Node) PostConsensusProcessing(newBlock *blockchain.Block) {
func (node *Node) AddNewBlock(newBlock *blockchain.Block) { func (node *Node) AddNewBlock(newBlock *blockchain.Block) {
// Add it to blockchain // Add it to blockchain
node.blockchain.Blocks = append(node.blockchain.Blocks, newBlock) node.blockchain.Blocks = append(node.blockchain.Blocks, newBlock)
// Store it into leveldb.
if node.db != nil {
node.log.Info("Writing new block into disk.")
newBlock.Write(node.db, strconv.Itoa(len(node.blockchain.Blocks)))
}
// Update UTXO pool // Update UTXO pool
node.UtxoPool.Update(newBlock.Transactions) node.UtxoPool.Update(newBlock.Transactions)
// Clear transaction-in-Consensus list // Clear transaction-in-Consensus list

@ -12,7 +12,7 @@ func TestNewNewNode(test *testing.T) {
validator := p2p.Peer{Ip: "3", Port: "5"} validator := p2p.Peer{Ip: "3", Port: "5"}
consensus := consensus.NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader) consensus := consensus.NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader)
node := New(consensus) node := New(consensus, nil)
if node.Consensus == nil { if node.Consensus == nil {
test.Error("Consensus is not initialized for the node") test.Error("Consensus is not initialized for the node")
} }
@ -39,7 +39,7 @@ func TestCountNumTransactionsInBlockchain(test *testing.T) {
validator := p2p.Peer{Ip: "3", Port: "5"} validator := p2p.Peer{Ip: "3", Port: "5"}
consensus := consensus.NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader) consensus := consensus.NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader)
node := New(consensus) node := New(consensus, nil)
node.AddTestingAddresses(1000) node.AddTestingAddresses(1000)
if node.countNumTransactionsInBlockchain() != 1001 { if node.countNumTransactionsInBlockchain() != 1001 {
test.Error("Count of transactions in the blockchain is incorrect") test.Error("Count of transactions in the blockchain is incorrect")

Loading…
Cancel
Save