From 0baea4d7be71cfa98abac26361caff3e3b8a9984 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Wed, 17 Jul 2019 16:35:45 +0800 Subject: [PATCH] core: address comments --- core/tx_noncer.go | 8 ++++---- core/tx_pool.go | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/tx_noncer.go b/core/tx_noncer.go index 3a9c48ea14..aa87c643ae 100644 --- a/core/tx_noncer.go +++ b/core/tx_noncer.go @@ -63,16 +63,16 @@ func (txn *txNoncer) set(addr common.Address, nonce uint64) { txn.nonces[addr] = nonce } -// compareAndSet inserts or updates a new virtual nonce into the virtual state -// database if the compare callback is true. -func (txn *txNoncer) compareAndSet(addr common.Address, nonce uint64, compare func(uint64, uint64) bool) { +// setIfLower updates a new virtual nonce into the virtual state database if the +// the new one is lower. +func (txn *txNoncer) setIfLower(addr common.Address, nonce uint64) { txn.lock.Lock() defer txn.lock.Unlock() if _, ok := txn.nonces[addr]; !ok { txn.nonces[addr] = txn.fallback.GetNonce(addr) } - if compare == nil || !compare(txn.nonces[addr], nonce) { + if txn.nonces[addr] <= nonce { return } txn.nonces[addr] = nonce diff --git a/core/tx_pool.go b/core/tx_pool.go index 6bcb97b0c6..c41d3fbd4a 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -854,7 +854,7 @@ func (pool *TxPool) removeTx(hash common.Hash, outofbound bool) { pool.enqueueTx(tx.Hash(), tx) } // Update the account nonce if needed - pool.pendingNonces.compareAndSet(addr, tx.Nonce(), func(old uint64, new uint64) bool { return old > new }) + pool.pendingNonces.setIfLower(addr, tx.Nonce()) // Reduce the pending counter pendingCounter.Dec(int64(1 + len(invalids))) return @@ -1230,7 +1230,7 @@ func (pool *TxPool) truncatePending() { pool.all.Remove(hash) // Update the account nonce to the dropped transaction - pool.pendingNonces.compareAndSet(offenders[i], tx.Nonce(), func(old uint64, new uint64) bool { return old > new }) + pool.pendingNonces.setIfLower(offenders[i], tx.Nonce()) log.Trace("Removed fairness-exceeding pending transaction", "hash", hash) } pool.priced.Removed(len(caps)) @@ -1257,7 +1257,7 @@ func (pool *TxPool) truncatePending() { pool.all.Remove(hash) // Update the account nonce to the dropped transaction - pool.pendingNonces.compareAndSet(addr, tx.Nonce(), func(old uint64, new uint64) bool { return old > new }) + pool.pendingNonces.setIfLower(addr, tx.Nonce()) log.Trace("Removed fairness-exceeding pending transaction", "hash", hash) } pool.priced.Removed(len(caps))