From 6cd04086f6105ae796e56c05b1ae41d7be98d6fe Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Tue, 21 Aug 2018 16:44:01 -0700 Subject: [PATCH] refactor db init --- benchmark.go | 13 ++++++++----- client/btctxgen/main.go | 4 ++-- client/txgen/main.go | 4 ++-- node/node.go | 17 +++++------------ node/node_handler.go | 4 ++-- node/node_test.go | 4 ++-- 6 files changed, 21 insertions(+), 25 deletions(-) diff --git a/benchmark.go b/benchmark.go index 21875d8ef..e30278e6a 100644 --- a/benchmark.go +++ b/benchmark.go @@ -86,6 +86,13 @@ func main() { ) log.Root().SetHandler(h) + // Initialize leveldb if dbSupported. + var ldb *db.LDBDatabase = nil + + if *dbSupported == 1 { + ldb, _ = InitLDBDatabase(*ip, *port) + } + // Consensus object. consensus := consensus.NewConsensus(*ip, *port, shardID, peers, leader) @@ -97,11 +104,7 @@ func main() { // Set logger to attack model. attack.GetInstance().SetLogger(consensus.Log) // Current node. - currentNode := node.New(consensus, true) - // Initialize leveldb if dbSupported. - if *dbSupported == 1 { - currentNode.DB, _ = InitLDBDatabase(*ip, *port) - } + currentNode := node.New(consensus, ldb) // Create client peer. clientPeer := distributionConfig.GetClientPeer() // If there is a client configured in the node list. diff --git a/client/btctxgen/main.go b/client/btctxgen/main.go index eaa51ae35..716848028 100644 --- a/client/btctxgen/main.go +++ b/client/btctxgen/main.go @@ -200,13 +200,13 @@ func main() { // Nodes containing utxopools to mirror the shards' data in the network nodes := []*node.Node{} for _, shardID := range shardIDs { - nodes = append(nodes, node.New(&consensus.Consensus{ShardID: shardID}, false)) + nodes = append(nodes, node.New(&consensus.Consensus{ShardID: shardID}, nil)) } // Client/txgenerator server node setup clientPort := configr.GetClientPort() consensusObj := consensus.NewConsensus("0", clientPort, "0", nil, p2p.Peer{}) - clientNode := node.New(consensusObj, false) + clientNode := node.New(consensusObj, nil) initClient(clientNode, clientPort, &leaders, &nodes) diff --git a/client/txgen/main.go b/client/txgen/main.go index f7fdc8c80..719142c9c 100644 --- a/client/txgen/main.go +++ b/client/txgen/main.go @@ -276,7 +276,7 @@ func main() { // Nodes containing utxopools to mirror the shards' data in the network nodes := []*node.Node{} for _, shardId := range shardIds { - node := node.New(&consensus.Consensus{ShardID: shardId}, false) + node := node.New(&consensus.Consensus{ShardID: shardId}, nil) // Assign many fake addresses so we have enough address to play with at first node.AddTestingAddresses(setting.numOfAddress) nodes = append(nodes, node) @@ -285,7 +285,7 @@ func main() { // Client/txgenerator server node setup clientPort := configr.GetClientPort() consensusObj := consensus.NewConsensus("0", clientPort, "0", nil, p2p.Peer{}) - clientNode := node.New(consensusObj, false) + clientNode := node.New(consensusObj, nil) if clientPort != "" { clientNode.Client = client.NewClient(&leaders) diff --git a/node/node.go b/node/node.go index 133fdfad9..acc19f587 100644 --- a/node/node.go +++ b/node/node.go @@ -22,7 +22,7 @@ type Node struct { 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 blockchain *blockchain.Blockchain // The blockchain for the shard where this node belongs - DB *db.LDBDatabase // LevelDB to store blockchain. + db *db.LDBDatabase // LevelDB to store 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. CrossTxsToReturn []*blockchain.CrossShardTxAndProof // The cross shard txs and proof that needs to be sent back to the user client. @@ -97,7 +97,7 @@ func (node *Node) countNumTransactionsInBlockchain() int { } // Create a new Node -func New(consensus *consensus.Consensus, dbSupported bool) *Node { +func New(consensus *consensus.Consensus, db *db.LDBDatabase) *Node { node := Node{} // Consensus and associated channel to communicate blocks @@ -119,15 +119,8 @@ func New(consensus *consensus.Consensus, dbSupported bool) *Node { // Logger node.log = node.Consensus.Log - // // Initialize leveldb - // if dbSupported { - // // TODO(minhdoan): Refactor this. - // var err = os.Remove("/tmp/harmony_db.dat") - // if err != nil { - // fmt.Println(err.Error()) - // os.Exit(1) - // } - // node.db, _ = db.NewLDBDatabase("/tmp/harmony_db.dat", 0, 0) - // } + // Initialize level db. + node.db = db + return &node } diff --git a/node/node_handler.go b/node/node_handler.go index 8427a5193..3ffffee82 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -280,9 +280,9 @@ func (node *Node) AddNewBlock(newBlock *blockchain.Block) { // Add it to blockchain node.blockchain.Blocks = append(node.blockchain.Blocks, newBlock) // Store it into leveldb. - if node.DB != nil { + if node.db != nil { node.log.Info("Writing new block into disk.") - newBlock.Write(node.DB, strconv.Itoa(len(node.blockchain.Blocks))) + newBlock.Write(node.db, strconv.Itoa(len(node.blockchain.Blocks))) } // Update UTXO pool node.UtxoPool.Update(newBlock.Transactions) diff --git a/node/node_test.go b/node/node_test.go index b45fbab72..ad18b0f0e 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -12,7 +12,7 @@ func TestNewNewNode(test *testing.T) { validator := p2p.Peer{Ip: "3", Port: "5"} consensus := consensus.NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader) - node := New(consensus, false) + node := New(consensus, nil) if node.Consensus == nil { 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"} consensus := consensus.NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader) - node := New(consensus, false) + node := New(consensus, nil) node.AddTestingAddresses(1000) if node.countNumTransactionsInBlockchain() != 1001 { test.Error("Count of transactions in the blockchain is incorrect")