@@ -2,55 +2,69 @@ package state
22
33import "sync"
44
5+ type account struct {
6+ stateObject * StateObject
7+ nstart uint64
8+ nonces []bool
9+ }
10+
511type ManagedState struct {
612 * StateDB
713
814 mu sync.RWMutex
915
10- accounts map [string ]* StateObject
16+ accounts map [string ]* account
1117}
1218
1319func ManageState (statedb * StateDB ) * ManagedState {
1420 return & ManagedState {
1521 StateDB : statedb ,
16- accounts : make (map [string ]* StateObject ),
22+ accounts : make (map [string ]* account ),
1723 }
1824}
1925
20- func (ms * ManagedState ) IncrementNonce (addr []byte ) {
21- ms .mu .Lock ()
22- defer ms .mu .Unlock ()
23-
24- ms .getAccount (addr ).nonce ++
25- }
26-
27- func (ms * ManagedState ) DecrementNonce (addr []byte ) {
28- // Decrementing a nonce does not mean we are interested in the account
29- // incrementing only happens if you control the account, therefor
30- // incrementing behaves differently from decrementing
26+ func (ms * ManagedState ) RemoveNonce (addr []byte , n uint64 ) {
3127 if ms .hasAccount (addr ) {
3228 ms .mu .Lock ()
3329 defer ms .mu .Unlock ()
3430
35- ms .getAccount (addr ).nonce --
31+ account := ms .getAccount (addr )
32+ if n - account .nstart < uint64 (len (account .nonces )) {
33+ reslice := make ([]bool , n - account .nstart )
34+ copy (reslice , account .nonces [:n - account .nstart ])
35+ account .nonces = reslice
36+ }
3637 }
3738}
3839
39- func (ms * ManagedState ) GetNonce (addr []byte ) uint64 {
40+ func (ms * ManagedState ) NewNonce (addr []byte ) uint64 {
4041 ms .mu .RLock ()
4142 defer ms .mu .RUnlock ()
42- return ms .getAccount (addr ).nonce
43+
44+ account := ms .getAccount (addr )
45+ for i , nonce := range account .nonces {
46+ if ! nonce {
47+ return account .nstart + uint64 (i )
48+ }
49+ }
50+ account .nonces = append (account .nonces , false )
51+ return uint64 (len (account .nonces )) + account .nstart
4352}
4453
4554func (ms * ManagedState ) hasAccount (addr []byte ) bool {
4655 _ , ok := ms .accounts [string (addr )]
4756 return ok
4857}
4958
50- func (ms * ManagedState ) getAccount (addr []byte ) * StateObject {
59+ func (ms * ManagedState ) getAccount (addr []byte ) * account {
5160 if _ , ok := ms .accounts [string (addr )]; ! ok {
52- ms .accounts [string (addr )] = ms .GetOrNewStateObject (addr )
61+ so := ms .GetOrNewStateObject (addr )
62+ ms .accounts [string (addr )] = newAccount (so )
5363 }
5464
5565 return ms .accounts [string (addr )]
5666}
67+
68+ func newAccount (so * StateObject ) * account {
69+ return & account {so , so .nonce - 1 , nil }
70+ }
0 commit comments