core: address comments

This commit is contained in:
rjl493456442 2019-07-17 16:35:45 +08:00
parent b49f3bf2c5
commit 0baea4d7be
2 changed files with 7 additions and 7 deletions

View file

@ -63,16 +63,16 @@ func (txn *txNoncer) set(addr common.Address, nonce uint64) {
txn.nonces[addr] = nonce txn.nonces[addr] = nonce
} }
// compareAndSet inserts or updates a new virtual nonce into the virtual state // setIfLower updates a new virtual nonce into the virtual state database if the
// database if the compare callback is true. // the new one is lower.
func (txn *txNoncer) compareAndSet(addr common.Address, nonce uint64, compare func(uint64, uint64) bool) { func (txn *txNoncer) setIfLower(addr common.Address, nonce uint64) {
txn.lock.Lock() txn.lock.Lock()
defer txn.lock.Unlock() defer txn.lock.Unlock()
if _, ok := txn.nonces[addr]; !ok { if _, ok := txn.nonces[addr]; !ok {
txn.nonces[addr] = txn.fallback.GetNonce(addr) txn.nonces[addr] = txn.fallback.GetNonce(addr)
} }
if compare == nil || !compare(txn.nonces[addr], nonce) { if txn.nonces[addr] <= nonce {
return return
} }
txn.nonces[addr] = nonce txn.nonces[addr] = nonce

View file

@ -854,7 +854,7 @@ func (pool *TxPool) removeTx(hash common.Hash, outofbound bool) {
pool.enqueueTx(tx.Hash(), tx) pool.enqueueTx(tx.Hash(), tx)
} }
// Update the account nonce if needed // 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 // Reduce the pending counter
pendingCounter.Dec(int64(1 + len(invalids))) pendingCounter.Dec(int64(1 + len(invalids)))
return return
@ -1230,7 +1230,7 @@ func (pool *TxPool) truncatePending() {
pool.all.Remove(hash) pool.all.Remove(hash)
// Update the account nonce to the dropped transaction // 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) log.Trace("Removed fairness-exceeding pending transaction", "hash", hash)
} }
pool.priced.Removed(len(caps)) pool.priced.Removed(len(caps))
@ -1257,7 +1257,7 @@ func (pool *TxPool) truncatePending() {
pool.all.Remove(hash) pool.all.Remove(hash)
// Update the account nonce to the dropped transaction // 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) log.Trace("Removed fairness-exceeding pending transaction", "hash", hash)
} }
pool.priced.Removed(len(caps)) pool.priced.Removed(len(caps))