diff --git a/core/state/managed_state.go b/core/state/managed_state.go index f8e2f2b876..6a1514508e 100644 --- a/core/state/managed_state.go +++ b/core/state/managed_state.go @@ -82,6 +82,23 @@ func (ms *ManagedState) NewNonce(addr common.Address) uint64 { return uint64(len(account.nonces)-1) + account.nstart } +// GetNextNonce returns the canonical nonce for the managed or unmanaged account and increments it by one +func (ms *ManagedState) GetNextNonce(addr common.Address) uint64 { + ms.mu.Lock() + defer ms.mu.Unlock() + + var nonce uint64 + + if ms.hasAccount(addr) { + account := ms.getAccount(addr) + nonce = uint64(len(account.nonces)) + account.nstart + } else { + nonce = ms.StateDB.GetNonce(addr) + } + ms.StateDB.SetNonce(addr, nonce+1) + return nonce +} + // GetNonce returns the canonical nonce for the managed or unmanaged account func (ms *ManagedState) GetNonce(addr common.Address) uint64 { ms.mu.RLock() diff --git a/core/state/state_object.go b/core/state/state_object.go index 769c63d42d..84a37e9833 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -229,8 +229,10 @@ func (self *StateObject) SetCode(code []byte) { } func (self *StateObject) SetNonce(nonce uint64) { - self.nonce = nonce - self.dirty = true + if nonce > self.nonce { + self.nonce = nonce + self.dirty = true + } } func (self *StateObject) Nonce() uint64 { diff --git a/eth/api_backend.go b/eth/api_backend.go index efcdb33619..bab6f4e7ae 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -136,10 +136,7 @@ func (b *EthApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transactio } func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { - b.eth.txMu.Lock() - defer b.eth.txMu.Unlock() - - return b.eth.txPool.State().GetNonce(addr), nil + return b.eth.txPool.State().GetNextNonce(addr), nil } func (b *EthApiBackend) Stats() (pending int, queued int) {