Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhang2023 committed Mar 21, 2024
2 parents 4dcca39 + a52d6d7 commit 35c4a99
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
7 changes: 6 additions & 1 deletion core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type StateDB struct {
hasher crypto.KeccakState
snaps *snapshot.Tree // Nil if snapshot is not available
snap snapshot.Snapshot // Nil if snapshot is not available
snapLock sync.Mutex // make the snap account multi-thread safe

// originalRoot is the pre-state root, before any changes were made.
// It will be updated when the Commit is called.
Expand Down Expand Up @@ -572,7 +573,11 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject {
var data *types.StateAccount
if s.snap != nil {
start := time.Now()
acc, err := s.snap.Account(crypto.HashData(s.hasher, addr.Bytes()))
// hasher need to be locked for multi-thread safe.
s.snapLock.Lock()
addrHash := crypto.HashData(s.hasher, addr.Bytes())
s.snapLock.Unlock()
acc, err := s.snap.Account(addrHash)
if metrics.EnabledExpensive {
s.SnapshotAccountReads += time.Since(start)
}
Expand Down
2 changes: 0 additions & 2 deletions core/txpool/legacypool/legacypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,6 @@ func (pool *LegacyPool) SetGasTip(tip *big.Int) {
// Nonce returns the next nonce of an account, with all transactions executable
// by the pool already applied on top.
func (pool *LegacyPool) Nonce(addr common.Address) uint64 {
pool.mu.RLock()
defer pool.mu.RUnlock()
defer func(t0 time.Time) {
nonceDurationTimer.Update(time.Since(t0))
}(time.Now())
Expand Down
27 changes: 13 additions & 14 deletions core/txpool/legacypool/noncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
type noncer struct {
fallback *state.StateDB
nonces map[common.Address]uint64
lock sync.Mutex
lock sync.RWMutex
}

// newNoncer creates a new virtual state database to track the pool nonces.
Expand All @@ -45,15 +45,19 @@ func newNoncer(statedb *state.StateDB) *noncer {
func (txn *noncer) get(addr common.Address) uint64 {
// We use mutex for get operation is the underlying
// state will mutate db even for read access.
txn.lock.Lock()
defer txn.lock.Unlock()
txn.lock.RLock()
nonce, ok := txn.nonces[addr]
txn.lock.RUnlock()

if _, ok := txn.nonces[addr]; !ok {
if !ok {
// GetNonce is so heavy that we don't want to hold the lock while calling it.
if nonce := txn.fallback.GetNonce(addr); nonce != 0 {
txn.lock.Lock()
txn.nonces[addr] = nonce
txn.lock.Unlock()
}
}
return txn.nonces[addr]
return nonce
}

// set inserts a new virtual nonce into the virtual state database to be returned
Expand All @@ -68,18 +72,13 @@ func (txn *noncer) set(addr common.Address, nonce uint64) {
// setIfLower updates a new virtual nonce into the virtual state database if the
// new one is lower.
func (txn *noncer) setIfLower(addr common.Address, nonce uint64) {
txn.lock.Lock()
defer txn.lock.Unlock()

if _, ok := txn.nonces[addr]; !ok {
if nonce := txn.fallback.GetNonce(addr); nonce != 0 {
txn.nonces[addr] = nonce
}
}
if txn.nonces[addr] <= nonce {
currNonce := txn.get(addr)
if currNonce <= nonce {
return
}
txn.lock.Lock()
txn.nonces[addr] = nonce
txn.lock.Unlock()
}

// setAll sets the nonces for all accounts to the given map.
Expand Down

0 comments on commit 35c4a99

Please sign in to comment.