@@ -23,13 +23,19 @@ func ManageState(statedb *StateDB) *ManagedState {
2323 }
2424}
2525
26+ func (ms * ManagedState ) SetState (statedb * StateDB ) {
27+ ms .mu .Lock ()
28+ defer ms .mu .Unlock ()
29+ ms .StateDB = statedb
30+ }
31+
2632func (ms * ManagedState ) RemoveNonce (addr []byte , n uint64 ) {
2733 if ms .hasAccount (addr ) {
2834 ms .mu .Lock ()
2935 defer ms .mu .Unlock ()
3036
3137 account := ms .getAccount (addr )
32- if n - account .nstart < uint64 (len (account .nonces )) {
38+ if n - account .nstart <= uint64 (len (account .nonces )) {
3339 reslice := make ([]bool , n - account .nstart )
3440 copy (reslice , account .nonces [:n - account .nstart ])
3541 account .nonces = reslice
@@ -47,7 +53,7 @@ func (ms *ManagedState) NewNonce(addr []byte) uint64 {
4753 return account .nstart + uint64 (i )
4854 }
4955 }
50- account .nonces = append (account .nonces , false )
56+ account .nonces = append (account .nonces , true )
5157 return uint64 (len (account .nonces )) + account .nstart
5258}
5359
@@ -57,9 +63,17 @@ func (ms *ManagedState) hasAccount(addr []byte) bool {
5763}
5864
5965func (ms * ManagedState ) getAccount (addr []byte ) * account {
60- if _ , ok := ms .accounts [string (addr )]; ! ok {
66+ if account , ok := ms .accounts [string (addr )]; ! ok {
6167 so := ms .GetOrNewStateObject (addr )
6268 ms .accounts [string (addr )] = newAccount (so )
69+ } else {
70+ // Always make sure the state account nonce isn't actually higher
71+ // than the tracked one.
72+ so := ms .StateDB .GetStateObject (addr )
73+ if so != nil && uint64 (len (account .nonces ))+ account .nstart < so .nonce {
74+ ms .accounts [string (addr )] = newAccount (so )
75+ }
76+
6377 }
6478
6579 return ms .accounts [string (addr )]
0 commit comments