Add loop in new block creation

pull/99/head
Rongjian Lan 6 years ago
parent bd859a800b
commit 5b1324305e
  1. 2
      node/node.go
  2. 15
      node/node_handler.go
  3. 12
      node/worker/worker.go
  4. 2
      proto/node/node.go

@ -103,7 +103,7 @@ func (node *Node) addPendingTransactionsAccount(newTxs types.Transactions) {
node.pendingTxMutexAccount.Lock()
node.pendingTransactionsAccount = append(node.pendingTransactionsAccount, newTxs...)
node.pendingTxMutexAccount.Unlock()
node.log.Debug("Got more transactions (account model)", "num", len(newTxs), "totalPending", len(node.pendingTransactions), "node", node)
node.log.Debug("Got more transactions (account model)", "num", len(newTxs), "totalPending", len(node.pendingTransactionsAccount), "node", node)
}
// Take out a subset of valid transactions from the pending transaction list

@ -390,19 +390,28 @@ func (node *Node) WaitForConsensusReadyAccount(readySignal chan struct{}) {
}
if !retry {
for {
if len(node.pendingTransactionsAccount) >= 1000 {
// Normal tx block consensus
selectedTxs, _ := node.getTransactionsForNewBlockAccount(MaxNumberOfTransactionsPerBlock)
if node.Worker.CommitTransactions(selectedTxs, pki.GetAddressFromPublicKey(node.SelfPeer.PubKey)) {
newBlock = node.Worker.Commit()
err := node.Worker.CommitTransactions(selectedTxs, pki.GetAddressFromPublicKey(node.SelfPeer.PubKey))
if err == nil {
block, err := node.Worker.Commit()
if err != nil {
node.log.Debug("Failed commiting new block", "Error", err)
} else {
newBlock = block
break
}
} else {
node.log.Debug("Failed to create new block")
node.log.Debug("Failed to create new block", "Error", err)
}
}
// If not enough transactions to run Consensus,
// periodically check whether we have enough transactions to package into block.
time.Sleep(1 * time.Second)
}
}
// Send the new block to Consensus so it can be confirmed.
if newBlock != nil {

@ -49,7 +49,7 @@ func (w *Worker) commitTransaction(tx *types.Transaction, coinbase common.Addres
return receipt.Logs, nil
}
func (w *Worker) CommitTransactions(txs []*types.Transaction, coinbase common.Address) bool {
func (w *Worker) CommitTransactions(txs []*types.Transaction, coinbase common.Address) error {
snap := w.current.state.Snapshot()
if w.current.gasPool == nil {
@ -59,11 +59,11 @@ func (w *Worker) CommitTransactions(txs []*types.Transaction, coinbase common.Ad
_, err := w.commitTransaction(tx, coinbase)
if err != nil {
w.current.state.RevertToSnapshot(snap)
return false
return err
}
}
return true
return nil
}
func (w *Worker) UpdateCurrent() error {
@ -98,13 +98,13 @@ func (w *Worker) GetCurrentState() *state.StateDB {
return w.current.state
}
func (w *Worker) Commit() *types.Block {
func (w *Worker) Commit() (*types.Block, error) {
s := w.current.state.Copy()
block, err := w.engine.Finalize(w.chain, w.current.header, s, w.current.txs, w.current.receipts)
if err != nil {
return nil
return nil, err
}
return block
return block, nil
}
func New(config *params.ChainConfig, chain *core.BlockChain, engine consensus.Engine) *Worker {

@ -4,8 +4,8 @@ import (
"bytes"
"encoding/gob"
"fmt"
"github.com/ethereum/go-ethereum/rlp"
"github.com/harmony-one/harmony/core/types"
"go-ethereum/rlp"
"log"
"github.com/harmony-one/harmony/blockchain"

Loading…
Cancel
Save