Skip to content

Commit f1fcda4

Browse files
committed
Implemented managed state
* Reimplemented nonce management for known accounts.
1 parent cda88ce commit f1fcda4

4 files changed

Lines changed: 90 additions & 24 deletions

File tree

‎core/block_processor.go‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,10 @@ func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, stated
7979
statedb.EmptyLogs()
8080

8181
cb := statedb.GetStateObject(coinbase.Address())
82-
/*
83-
st := NewStateTransition(NewEnv(statedb, self.bc, tx, block), tx, cb)
84-
_, err := st.TransitionState()
85-
*/
8682
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, block), tx, cb)
8783
if err != nil && (IsNonceErr(err) || state.IsGasLimitErr(err) || IsInvalidTxErr(err)) {
84+
// If the account is managed, remove the invalid nonce.
85+
self.bc.TxState().RemoveNonce(tx.From(), tx.Nonce())
8886
return nil, nil, err
8987
}
9088

‎state/managed_state.go‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,69 @@ package state
22

33
import "sync"
44

5+
type account struct {
6+
stateObject *StateObject
7+
nstart uint64
8+
nonces []bool
9+
}
10+
511
type ManagedState struct {
612
*StateDB
713

814
mu sync.RWMutex
915

10-
accounts map[string]*StateObject
16+
accounts map[string]*account
1117
}
1218

1319
func 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

4554
func (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+
}

‎state/managed_state_test.go‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package state
2+
3+
import "testing"
4+
5+
var addr = []byte("test")
6+
7+
func create() (*ManagedState, *account) {
8+
ms := ManageState(nil)
9+
ms.accounts[string(addr)] = newAccount(&StateObject{nonce: 100})
10+
11+
return ms, ms.accounts[string(addr)]
12+
}
13+
14+
func TestNewNonce(t *testing.T) {
15+
ms, _ := create()
16+
17+
nonce := ms.NewNonce(addr)
18+
if nonce != 100 {
19+
t.Error("expected nonce 101. got", nonce)
20+
}
21+
}
22+
23+
func TestRemove(t *testing.T) {
24+
ms, account := create()
25+
26+
nn := make([]bool, 10)
27+
for i, _ := range nn {
28+
nn[i] = true
29+
}
30+
account.nonces = append(account.nonces, nn...)
31+
32+
i := uint64(5)
33+
ms.RemoveNonce(addr, account.nstart+i)
34+
if len(account.nonces) != 5 {
35+
t.Error("expected", i, "'th index to be false")
36+
}
37+
}
38+
39+
func TestReuse(t *testing.T) {
40+
ms, account := create()
41+
42+
nn := make([]bool, 10)
43+
for i, _ := range nn {
44+
nn[i] = true
45+
}
46+
account.nonces = append(account.nonces, nn...)
47+
48+
i := uint64(5)
49+
ms.RemoveNonce(addr, account.nstart+i)
50+
nonce := ms.NewNonce(addr)
51+
if nonce != 105 {
52+
t.Error("expected nonce to be 105. got", nonce)
53+
}
54+
}

‎xeth/xeth.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeSt
353353
}
354354

355355
state := self.chainManager.TxState()
356-
nonce := state.GetNonce(from)
356+
nonce := state.NewNonce(from) //state.GetNonce(from)
357357
tx.SetNonce(nonce)
358358

359359
if err := self.sign(tx, from, false); err != nil {
@@ -362,7 +362,7 @@ func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeSt
362362
if err := self.eth.TxPool().Add(tx); err != nil {
363363
return "", err
364364
}
365-
state.IncrementNonce(from)
365+
//state.IncrementNonce(from)
366366

367367
if contractCreation {
368368
addr := core.AddressFromMessage(tx)

0 commit comments

Comments
 (0)