Skip to content

Commit a026648

Browse files
committed
Make remote dominant
1 parent b663e8b commit a026648

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

‎state/managed_state.go‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
2632
func (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

5965
func (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)]

‎state/managed_state_test.go‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package state
22

3-
import "testing"
3+
import (
4+
"testing"
45

5-
var addr = []byte("test")
6+
"github.com/ethereum/go-ethereum/ethutil"
7+
)
8+
9+
var addr = ethutil.Address([]byte("test"))
610

711
func create() (*ManagedState, *account) {
8-
ms := ManageState(nil)
9-
ms.accounts[string(addr)] = newAccount(&StateObject{nonce: 100})
12+
ms := ManageState(&StateDB{stateObjects: make(map[string]*StateObject)})
13+
so := &StateObject{address: addr, nonce: 100}
14+
ms.StateDB.stateObjects[string(addr)] = so
15+
ms.accounts[string(addr)] = newAccount(so)
1016

1117
return ms, ms.accounts[string(addr)]
1218
}
@@ -16,6 +22,11 @@ func TestNewNonce(t *testing.T) {
1622

1723
nonce := ms.NewNonce(addr)
1824
if nonce != 100 {
25+
t.Error("expected nonce 100. got", nonce)
26+
}
27+
28+
nonce = ms.NewNonce(addr)
29+
if nonce != 101 {
1930
t.Error("expected nonce 101. got", nonce)
2031
}
2132
}
@@ -52,3 +63,19 @@ func TestReuse(t *testing.T) {
5263
t.Error("expected nonce to be 105. got", nonce)
5364
}
5465
}
66+
67+
func TestRemoteNonceChange(t *testing.T) {
68+
ms, account := create()
69+
nn := make([]bool, 10)
70+
for i, _ := range nn {
71+
nn[i] = true
72+
}
73+
account.nonces = append(account.nonces, nn...)
74+
nonce := ms.NewNonce(addr)
75+
76+
ms.StateDB.stateObjects[string(addr)].nonce = 200
77+
nonce = ms.NewNonce(addr)
78+
if nonce != 200 {
79+
t.Error("expected nonce after remote update to be", 201, "got", nonce)
80+
}
81+
}

0 commit comments

Comments
 (0)