From bf07063a2e24d12e4265d3e05fbcefca8b68d67c Mon Sep 17 00:00:00 2001 From: Dennis Won Date: Mon, 12 Aug 2019 18:14:02 -0700 Subject: [PATCH] fix recentTxsCounts map data clean up bug --- core/types/transaction.go | 16 +++++++--------- node/node.go | 22 +++++++++++++--------- node/worker/worker.go | 2 +- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/core/types/transaction.go b/core/types/transaction.go index cb186a469..6657b5342 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -295,15 +295,6 @@ func (tx *Transaction) RawSignatureValues() (*big.Int, *big.Int, *big.Int) { // Transactions is a Transaction slice type for basic sorting. type Transactions []*Transaction -// TODO: put these temp custom txs data structures into other places to keep Transaction type code clean. - -// RecentTxsStats is a recent transactions stats map tracking stats like BlockTxsCounts. -type RecentTxsStats map[uint64]BlockTxsCounts - -// BlockTxsCounts is a transactions counts map of -// the number of transactions made by each account in a block on this node. -type BlockTxsCounts map[common.Address]uint64 - // Len returns the length of s. func (s Transactions) Len() int { return len(s) } @@ -494,3 +485,10 @@ func (m Message) Data() []byte { func (m Message) CheckNonce() bool { return m.checkNonce } + +// RecentTxsStats is a recent transactions stats map tracking stats like BlockTxsCounts. +type RecentTxsStats map[uint64]BlockTxsCounts + +// BlockTxsCounts is a transactions counts map of +// the number of transactions made by each account in a block on this node. +type BlockTxsCounts map[common.Address]uint64 diff --git a/node/node.go b/node/node.go index f38edc3c7..62d665b63 100644 --- a/node/node.go +++ b/node/node.go @@ -90,10 +90,8 @@ type Node struct { BlockChannel chan *types.Block // The channel to send newly proposed blocks ConfirmedBlockChannel chan *types.Block // The channel to send confirmed blocks BeaconBlockChannel chan *types.Block // The channel to send beacon blocks for non-beaconchain nodes - pendingTransactions types.Transactions // All the transactions received but not yet processed for Consensus - pendingTxMutex sync.Mutex - recentTxsStats types.RecentTxsStats - DRand *drand.DRand // The instance for distributed randomness protocol + + DRand *drand.DRand // The instance for distributed randomness protocol // Shard databases shardChains shardchain.Collection @@ -112,7 +110,12 @@ type Node struct { // BeaconNeighbors store only neighbor nodes in the beacon chain shard BeaconNeighbors sync.Map // All the neighbor nodes, key is the sha256 of Peer IP/Port, value is the p2p.Peer - TxPool *core.TxPool + TxPool *core.TxPool // TODO migrate to TxPool from pendingTransactions list below + + pendingTransactions types.Transactions // All the transactions received but not yet processed for Consensus + pendingTxMutex sync.Mutex + recentTxsStats types.RecentTxsStats + Worker *worker.Worker BeaconWorker *worker.Worker // worker for beacon chain @@ -256,13 +259,14 @@ func (node *Node) AddPendingTransaction(newTx *types.Transaction) { func (node *Node) getTransactionsForNewBlock(coinbase common.Address) types.Transactions { node.pendingTxMutex.Lock() - // update recentTxsStats and initiailize for the new block - // the next block number to be added in consensus protocol, which is always one more than current chain header block newBlockNum := node.Blockchain().CurrentBlock().NumberU64() + 1 + + // remove old (currently > 1 hr) blockNum keys from recentTxsStats and initiailize for the new block for blockNum := range node.recentTxsStats { - blockNumHourAgo := (time.Hour / time.Second) / node.BlockPeriod - if blockNum < node.Consensus.ChainReader.CurrentHeader().Number.Uint64()-uint64(blockNumHourAgo) { + blockNumHourAgo := uint64(time.Hour / node.BlockPeriod) + + if blockNumHourAgo < newBlockNum-blockNum { delete(node.recentTxsStats, blockNum) } } diff --git a/node/worker/worker.go b/node/worker/worker.go index 76daaf9d6..78468993f 100644 --- a/node/worker/worker.go +++ b/node/worker/worker.go @@ -91,7 +91,7 @@ func (w *Worker) throttleTxs(selected types.Transactions, recentTxsStats types.R utils.GetLogInstance().Info("Throttling tx with max txs per account in a single block limit", "tx Id", tx.Hash().Hex(), "MaxNumRecentTxsPerAccountLimit", txsThrottleConfig.MaxNumRecentTxsPerAccountLimit) - return sender, shardingconfig.TxUnselect + return sender, shardingconfig.TxInvalid } return sender, shardingconfig.TxSelect