From a4200bccb12dfb4c80442b90effd5e1487857713 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Tue, 27 Nov 2018 18:12:23 -0800 Subject: [PATCH] Fully integrate with tx gen by account model --- client/txgen/main.go | 14 +++++++++++++ client/txgen/txgen/account_txs_generator.go | 1 - consensus/consensus_leader.go | 1 + harmony/main.go | 2 -- node/node.go | 1 + node/node_handler.go | 23 ++++++++++++++------- 6 files changed, 32 insertions(+), 10 deletions(-) diff --git a/client/txgen/main.go b/client/txgen/main.go index e83a1d9b1..1e89f56bd 100644 --- a/client/txgen/main.go +++ b/client/txgen/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "github.com/ethereum/go-ethereum/rlp" "github.com/harmony-one/harmony/client/txgen/txgen" "github.com/harmony-one/harmony/core/types" "os" @@ -103,6 +104,19 @@ func main() { utxoPoolMutex.Lock() node.UpdateUtxoAndState(block) utxoPoolMutex.Unlock() + + accountBlock := new(types.Block) + err := rlp.DecodeBytes(block.AccountBlock, accountBlock) + fmt.Println("RECEIVED NEW BLOCK ", len(accountBlock.Transactions())) + if err != nil { + log.Error("Failed decoding the block with RLP") + } else { + err = node.Worker.CommitTransactions(accountBlock.Transactions(), accountBlock.Coinbase()) + node.Worker.UpdateCurrent() + if err != nil { + log.Debug("Failed to add new block to worker", "Error", err) + } + } } else { continue } diff --git a/client/txgen/txgen/account_txs_generator.go b/client/txgen/txgen/account_txs_generator.go index 95b179dc9..4cab3ed2d 100644 --- a/client/txgen/txgen/account_txs_generator.go +++ b/client/txgen/txgen/account_txs_generator.go @@ -21,7 +21,6 @@ func GenerateSimulatedTransactionsAccount(shardID int, dataNodes []*node.Node, s txs := make([]*types.Transaction, 1000) for i := 0; i < 100; i++ { baseNonce := node.Worker.GetCurrentState().GetNonce(crypto.PubkeyToAddress(node.TestBankKeys[i].PublicKey)) - node.Worker.UpdateCurrent() for j := 0; j < 10; j++ { randomUserKey, _ := crypto.GenerateKey() randomUserAddress := crypto.PubkeyToAddress(randomUserKey.PublicKey) diff --git a/consensus/consensus_leader.go b/consensus/consensus_leader.go index 1a11d6070..9064afb14 100644 --- a/consensus/consensus_leader.go +++ b/consensus/consensus_leader.go @@ -61,6 +61,7 @@ func (consensus *Consensus) WaitForNewBlockAccount(blockChannel chan *types.Bloc // time.Sleep(500 * time.Millisecond) data, err := rlp.EncodeToBytes(newBlock) if err == nil { + consensus.Log.Debug("Sample tx", "tx", newBlock.Transactions()[0]) consensus.startConsensus(&blockchain.Block{Hash: newBlock.Hash(), AccountBlock: data}) } else { consensus.Log.Error("Failed encoding the block with RLP") diff --git a/harmony/main.go b/harmony/main.go index f17d4d982..99ca260d5 100644 --- a/harmony/main.go +++ b/harmony/main.go @@ -84,7 +84,6 @@ func main() { txs := make([]*types.Transaction, 100) worker := worker.New(params.TestChainConfig, chain, consensus.NewFaker()) fmt.Println(worker.GetCurrentState().GetBalance(testBankAddress)) - fmt.Println(worker.Commit().Root()) for i, _ := range txs { randomUserKey, _ := crypto.GenerateKey() @@ -96,5 +95,4 @@ func main() { worker.CommitTransactions(txs, crypto.PubkeyToAddress(testBankKey.PublicKey)) fmt.Println(worker.GetCurrentState().GetBalance(testBankAddress)) - fmt.Println(worker.Commit().Root()) } diff --git a/node/node.go b/node/node.go index 7e8606d7f..224eb53c8 100644 --- a/node/node.go +++ b/node/node.go @@ -134,6 +134,7 @@ func (node *Node) getTransactionsForNewBlockAccount(maxNumTxs int) (types.Transa node.log.Debug("Invalid transactions discarded", "number", len(invalid)) node.pendingTransactionsAccount = unselected + node.log.Debug("Remaining pending transactions", "number", len(node.pendingTransactionsAccount)) node.pendingTxMutexAccount.Unlock() return selected, crossShardTxs //TODO: replace cross-shard proofs for account model } diff --git a/node/node_handler.go b/node/node_handler.go index e736b630b..e5a447b8b 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -394,7 +394,11 @@ func (node *Node) WaitForConsensusReadyAccount(readySignal chan struct{}) { if len(node.pendingTransactionsAccount) >= 1000 { // Normal tx block consensus selectedTxs, _ := node.getTransactionsForNewBlockAccount(MaxNumberOfTransactionsPerBlock) - err := node.Worker.CommitTransactions(selectedTxs, pki.GetAddressFromPublicKey(node.SelfPeer.PubKey)) + err := node.Worker.UpdateCurrent() + if err != nil { + node.log.Debug("Failed updating worker's state", "Error", err) + } + err = node.Worker.CommitTransactions(selectedTxs, pki.GetAddressFromPublicKey(node.SelfPeer.PubKey)) if err == nil { block, err := node.Worker.Commit() if err != nil { @@ -465,7 +469,7 @@ func (node *Node) VerifyNewBlock(newBlock *blockchain.Block) bool { func (node *Node) VerifyNewBlockAccount(newBlock *types.Block) bool { err := node.Chain.ValidateNewBlock(newBlock, pki.GetAddressFromPublicKey(node.SelfPeer.PubKey)) if err != nil { - node.log.Debug("Failed verifying new block", "Error", err) + node.log.Debug("Failed verifying new block", "Error", err, "tx", newBlock.Transactions()[0]) return false } return true @@ -506,8 +510,7 @@ func (node *Node) PostConsensusProcessing(newBlock *blockchain.Block) { if err != nil { node.log.Error("Failed decoding the block with RLP") } - node.AddNewBlockAccount(accountBlock) - // For utxo model only + node.AddNewBlock(newBlock) node.UpdateUtxoAndState(newBlock) @@ -515,11 +518,9 @@ func (node *Node) PostConsensusProcessing(newBlock *blockchain.Block) { // AddNewBlockAccount is usedd to add new block into the blockchain. func (node *Node) AddNewBlockAccount(newBlock *types.Block) { - fmt.Println("Adding BLOCK") - fmt.Println(len(newBlock.Transactions())) num, err := node.Chain.InsertChain([]*types.Block{newBlock}) if err != nil { - fmt.Println("Error adding to chain", "numBlocks", num, "Error", err) + node.log.Debug("Error adding to chain", "numBlocks", num, "Error", err) } } @@ -532,6 +533,14 @@ func (node *Node) AddNewBlock(newBlock *blockchain.Block) { node.log.Info("Writing new block into disk.") newBlock.Write(node.db, strconv.Itoa(len(node.blockchain.Blocks))) } + + // Account model + accountBlock := new(types.Block) + err := rlp.DecodeBytes(newBlock.AccountBlock, accountBlock) + if err != nil { + node.log.Error("Failed decoding the block with RLP") + } + node.AddNewBlockAccount(accountBlock) } // UpdateUtxoAndState updates Utxo and state.