refactor db init

pull/61/head
Minh Doan 6 years ago
parent 9e645f186e
commit 6cd04086f6
  1. 13
      benchmark.go
  2. 4
      client/btctxgen/main.go
  3. 4
      client/txgen/main.go
  4. 17
      node/node.go
  5. 4
      node/node_handler.go
  6. 4
      node/node_test.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.

@ -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)

@ -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)

@ -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
}

@ -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)

@ -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")

Loading…
Cancel
Save