From 0a7f10933bf982cc1da23c9b739e5c3c42b22c65 Mon Sep 17 00:00:00 2001 From: Nguyen Ba Tam Date: Mon, 5 Nov 2018 16:31:10 +0700 Subject: [PATCH] fix error duplicate nonce when fetcher insert multi blocks - concurrent inserts --- contracts/utils.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/contracts/utils.go b/contracts/utils.go index 0d9a6f9a9e..548794e5d0 100644 --- a/contracts/utils.go +++ b/contracts/utils.go @@ -27,6 +27,7 @@ import ( "math/big" "math/rand" "strconv" + "sync" "time" "github.com/ethereum/go-ethereum/accounts" @@ -56,8 +57,11 @@ type rewardLog struct { Reward *big.Int `json:"reward"` } +var TxSignMu sync.RWMutex // Send tx sign for block number to smart contract blockSigner. func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager, block *types.Block, chainDb ethdb.Database) error { + TxSignMu.Lock() + defer TxSignMu.Unlock() if chainConfig.Posv != nil { // Find active account. account := accounts.Account{} @@ -80,7 +84,7 @@ func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, m // Add tx signed to local tx pool. err = pool.AddLocal(txSigned) if err != nil { - log.Error("Fail to add tx sign to local pool.", "error", err) + log.Error("Fail to add tx sign to local pool.", "error", err, "number", block.NumberU64(), "hash", block.Hash().Hex(), "from", account.Address, "nonce", nonce) } // Create secret tx. @@ -108,7 +112,7 @@ func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, m // Add tx signed to local tx pool. err = pool.AddLocal(txSigned) if err != nil { - log.Error("Fail to add tx secret to local pool.", "error", err) + log.Error("Fail to add tx secret to local pool.", "error", err, "number", block.NumberU64(), "hash", block.Hash().Hex(), "from", account.Address, "nonce", nonce) } // Put randomize key into chainDb. @@ -135,7 +139,7 @@ func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, m // Add tx to pool. err = pool.AddLocal(txSigned) if err != nil { - log.Error("Fail to add tx opening to local pool.", "error", err) + log.Error("Fail to add tx opening to local pool.", "error", err, "number", block.NumberU64(), "hash", block.Hash().Hex(), "from", account.Address, "nonce", nonce) } // Clear randomize key in state db.