diff options
-rw-r--r-- | core/chain_manager.go | 2 | ||||
-rw-r--r-- | core/error.go | 2 | ||||
-rw-r--r-- | core/state_transition.go | 3 | ||||
-rw-r--r-- | miner/worker.go | 1 | ||||
-rw-r--r-- | state/managed_state.go | 20 | ||||
-rw-r--r-- | state/managed_state_test.go | 35 |
6 files changed, 55 insertions, 8 deletions
diff --git a/core/chain_manager.go b/core/chain_manager.go index 7fed7b8b1..101d0568d 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -449,6 +449,8 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error { */ self.setTransState(state.New(block.Root(), self.stateDb)) + self.setTxState(state.New(block.Root(), self.stateDb)) + queue[i] = ChainEvent{block} queueEvent.canonicalCount++ } else { diff --git a/core/error.go b/core/error.go index 04e40646c..69e320eb0 100644 --- a/core/error.go +++ b/core/error.go @@ -78,7 +78,7 @@ func (err *NonceErr) Error() string { } func NonceError(is, exp uint64) *NonceErr { - return &NonceErr{Message: fmt.Sprintf("Nonce err. Is %d, expected %d", is, exp), Is: is, Exp: exp} + return &NonceErr{Message: fmt.Sprintf("Transaction w/ invalid nonce (%d / %d)", is, exp), Is: is, Exp: exp} } func IsNonceErr(err error) bool { diff --git a/core/state_transition.go b/core/state_transition.go index ee99ec7aa..f49aed874 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -148,6 +148,9 @@ func (self *StateTransition) preCheck() (err error) { // Pre-pay gas / Buy gas of the coinbase account if err = self.BuyGas(); err != nil { + if state.IsGasLimitErr(err) { + return err + } return InvalidTxError(err) } diff --git a/miner/worker.go b/miner/worker.go index 9bcea1539..a39a0b43b 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -212,6 +212,7 @@ gasLimit: fallthrough case core.IsInvalidTxErr(err): // Remove invalid transactions + self.chain.TxState().RemoveNonce(tx.From(), tx.Nonce()) remove = append(remove, tx) case state.IsGasLimitErr(err): // Break on gas limit diff --git a/state/managed_state.go b/state/managed_state.go index 1914e977a..aff0206b2 100644 --- a/state/managed_state.go +++ b/state/managed_state.go @@ -23,13 +23,19 @@ func ManageState(statedb *StateDB) *ManagedState { } } +func (ms *ManagedState) SetState(statedb *StateDB) { + ms.mu.Lock() + defer ms.mu.Unlock() + ms.StateDB = statedb +} + func (ms *ManagedState) RemoveNonce(addr []byte, n uint64) { if ms.hasAccount(addr) { ms.mu.Lock() defer ms.mu.Unlock() account := ms.getAccount(addr) - if n-account.nstart < uint64(len(account.nonces)) { + if n-account.nstart <= uint64(len(account.nonces)) { reslice := make([]bool, n-account.nstart) copy(reslice, account.nonces[:n-account.nstart]) account.nonces = reslice @@ -47,7 +53,7 @@ func (ms *ManagedState) NewNonce(addr []byte) uint64 { return account.nstart + uint64(i) } } - account.nonces = append(account.nonces, false) + account.nonces = append(account.nonces, true) return uint64(len(account.nonces)) + account.nstart } @@ -57,9 +63,17 @@ func (ms *ManagedState) hasAccount(addr []byte) bool { } func (ms *ManagedState) getAccount(addr []byte) *account { - if _, ok := ms.accounts[string(addr)]; !ok { + if account, ok := ms.accounts[string(addr)]; !ok { so := ms.GetOrNewStateObject(addr) ms.accounts[string(addr)] = newAccount(so) + } else { + // Always make sure the state account nonce isn't actually higher + // than the tracked one. + so := ms.StateDB.GetStateObject(addr) + if so != nil && uint64(len(account.nonces))+account.nstart < so.nonce { + ms.accounts[string(addr)] = newAccount(so) + } + } return ms.accounts[string(addr)] diff --git a/state/managed_state_test.go b/state/managed_state_test.go index ae374f728..da52ab270 100644 --- a/state/managed_state_test.go +++ b/state/managed_state_test.go @@ -1,12 +1,18 @@ package state -import "testing" +import ( + "testing" -var addr = []byte("test") + "github.com/ethereum/go-ethereum/ethutil" +) + +var addr = ethutil.Address([]byte("test")) func create() (*ManagedState, *account) { - ms := ManageState(nil) - ms.accounts[string(addr)] = newAccount(&StateObject{nonce: 100}) + ms := ManageState(&StateDB{stateObjects: make(map[string]*StateObject)}) + so := &StateObject{address: addr, nonce: 100} + ms.StateDB.stateObjects[string(addr)] = so + ms.accounts[string(addr)] = newAccount(so) return ms, ms.accounts[string(addr)] } @@ -16,6 +22,11 @@ func TestNewNonce(t *testing.T) { nonce := ms.NewNonce(addr) if nonce != 100 { + t.Error("expected nonce 100. got", nonce) + } + + nonce = ms.NewNonce(addr) + if nonce != 101 { t.Error("expected nonce 101. got", nonce) } } @@ -52,3 +63,19 @@ func TestReuse(t *testing.T) { t.Error("expected nonce to be 105. got", nonce) } } + +func TestRemoteNonceChange(t *testing.T) { + ms, account := create() + nn := make([]bool, 10) + for i, _ := range nn { + nn[i] = true + } + account.nonces = append(account.nonces, nn...) + nonce := ms.NewNonce(addr) + + ms.StateDB.stateObjects[string(addr)].nonce = 200 + nonce = ms.NewNonce(addr) + if nonce != 200 { + t.Error("expected nonce after remote update to be", 201, "got", nonce) + } +} |