core: avoid modification of accountSet cache in tx_pool (#21159)

* core: avoid modification of accountSet cache in tx_pool

when runReorg, we may copy the dirtyAccounts' accountSet cache to promoteAddrs
in which accounts will be promoted, however, if we have reset request at the
same time, we may reuse promoteAddrs and modify the cache content which is
against the original intention of accountSet cache. So, we need to make a new
slice here to avoid modify accountSet cache.

* core: fix flatten condition + comment

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
Hao Duan 2020-08-04 17:51:53 +08:00 committed by GitHub
parent e24e05dd01
commit b35e4fce99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1032,7 +1032,10 @@ func (pool *TxPool) runReorg(done chan struct{}, reset *txpoolResetRequest, dirt
defer close(done) defer close(done)
var promoteAddrs []common.Address var promoteAddrs []common.Address
if dirtyAccounts != nil { if dirtyAccounts != nil && reset == nil {
// Only dirty accounts need to be promoted, unless we're resetting.
// For resets, all addresses in the tx queue will be promoted and
// the flatten operation can be avoided.
promoteAddrs = dirtyAccounts.flatten() promoteAddrs = dirtyAccounts.flatten()
} }
pool.mu.Lock() pool.mu.Lock()
@ -1048,7 +1051,7 @@ func (pool *TxPool) runReorg(done chan struct{}, reset *txpoolResetRequest, dirt
} }
} }
// Reset needs promote for all addresses // Reset needs promote for all addresses
promoteAddrs = promoteAddrs[:0] promoteAddrs = make([]common.Address, 0, len(pool.queue))
for addr := range pool.queue { for addr := range pool.queue {
promoteAddrs = append(promoteAddrs, addr) promoteAddrs = append(promoteAddrs, addr)
} }