diff options
author | obscuren <geffobscura@gmail.com> | 2015-06-10 00:14:46 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-06-10 03:01:02 +0800 |
commit | bc6031e7bb453ec7e1f229b39e11967a8b32175a (patch) | |
tree | b5699e36abcd7bbe32e65e95eb335d677b837740 | |
parent | 93f48528441b99f2c0a89326eb60a3e1ae6f92bc (diff) | |
download | dexon-bc6031e7bb453ec7e1f229b39e11967a8b32175a.tar dexon-bc6031e7bb453ec7e1f229b39e11967a8b32175a.tar.gz dexon-bc6031e7bb453ec7e1f229b39e11967a8b32175a.tar.bz2 dexon-bc6031e7bb453ec7e1f229b39e11967a8b32175a.tar.lz dexon-bc6031e7bb453ec7e1f229b39e11967a8b32175a.tar.xz dexon-bc6031e7bb453ec7e1f229b39e11967a8b32175a.tar.zst dexon-bc6031e7bb453ec7e1f229b39e11967a8b32175a.zip |
core, xeth: moved nonce management burden from xeth to txpool
-rw-r--r-- | core/state/managed_state.go | 2 | ||||
-rw-r--r-- | core/transaction_pool.go | 25 | ||||
-rw-r--r-- | core/transaction_pool_test.go | 38 | ||||
-rw-r--r-- | xeth/xeth.go | 2 |
4 files changed, 44 insertions, 23 deletions
diff --git a/core/state/managed_state.go b/core/state/managed_state.go index 5114f7a7a..aa6650d9b 100644 --- a/core/state/managed_state.go +++ b/core/state/managed_state.go @@ -23,7 +23,7 @@ type ManagedState struct { // ManagedState returns a new managed state with the statedb as it's backing layer func ManageState(statedb *StateDB) *ManagedState { return &ManagedState{ - StateDB: statedb, + StateDB: statedb.Copy(), accounts: make(map[string]*account), } } diff --git a/core/transaction_pool.go b/core/transaction_pool.go index 918e7b957..a2f970195 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -39,7 +39,7 @@ type stateFn func() *state.StateDB type TxPool struct { quit chan bool // Quiting channel currentState stateFn // The state function which will allow us to do some pre checkes - state *state.ManagedState + pendingState *state.ManagedState gasLimit func() *big.Int // The current gas limit function callback eventMux *event.TypeMux events event.Subscription @@ -57,7 +57,7 @@ func NewTxPool(eventMux *event.TypeMux, currentStateFn stateFn, gasLimitFn func( eventMux: eventMux, currentState: currentStateFn, gasLimit: gasLimitFn, - state: state.ManageState(currentStateFn()), + pendingState: state.ManageState(currentStateFn()), } } @@ -76,7 +76,7 @@ func (pool *TxPool) Start() { } func (pool *TxPool) resetState() { - pool.state = state.ManageState(pool.currentState()) + pool.pendingState = state.ManageState(pool.currentState()) // validate the pool of pending transactions, this will remove // any transactions that have been included in the block or @@ -90,7 +90,7 @@ func (pool *TxPool) resetState() { if addr, err := tx.From(); err == nil { // Set the nonce. Transaction nonce can never be lower // than the state nonce; validatePool took care of that. - pool.state.SetNonce(addr, tx.Nonce()) + pool.pendingState.SetNonce(addr, tx.Nonce()) } } @@ -110,7 +110,7 @@ func (pool *TxPool) State() *state.ManagedState { pool.mu.RLock() defer pool.mu.RUnlock() - return pool.state + return pool.pendingState } // validateTx checks whether a transaction is valid according @@ -302,7 +302,9 @@ func (pool *TxPool) addTx(hash common.Hash, addr common.Address, tx *types.Trans if _, ok := pool.pending[hash]; !ok { pool.pending[hash] = tx - pool.state.SetNonce(addr, tx.AccountNonce) + // Increment the nonce on the pending state. This can only happen if + // the nonce is +1 to the previous one. + pool.pendingState.SetNonce(addr, tx.AccountNonce+1) // Notify the subscribers. This event is posted in a goroutine // because it's possible that somewhere during the post "Remove transaction" // gets called which will then wait for the global tx pool lock and deadlock. @@ -312,14 +314,17 @@ func (pool *TxPool) addTx(hash common.Hash, addr common.Address, tx *types.Trans // checkQueue moves transactions that have become processable to main pool. func (pool *TxPool) checkQueue() { - state := pool.state + state := pool.pendingState var addq txQueue for address, txs := range pool.queue { - curnonce := state.GetNonce(address) + // guessed nonce is the nonce currently kept by the tx pool (pending state) + guessedNonce := state.GetNonce(address) + // true nonce is the nonce known by the last state + trueNonce := pool.currentState().GetNonce(address) addq := addq[:0] for hash, tx := range txs { - if tx.AccountNonce < curnonce { + if tx.AccountNonce < trueNonce { // Drop queued transactions whose nonce is lower than // the account nonce because they have been processed. delete(txs, hash) @@ -332,7 +337,7 @@ func (pool *TxPool) checkQueue() { // current account nonce. sort.Sort(addq) for _, e := range addq { - if e.AccountNonce > curnonce { + if e.AccountNonce > guessedNonce { break } delete(txs, e.hash) diff --git a/core/transaction_pool_test.go b/core/transaction_pool_test.go index b8bf78f00..b763c196d 100644 --- a/core/transaction_pool_test.go +++ b/core/transaction_pool_test.go @@ -37,21 +37,21 @@ func TestInvalidTransactions(t *testing.T) { } from, _ := tx.From() - pool.state.AddBalance(from, big.NewInt(1)) + pool.currentState().AddBalance(from, big.NewInt(1)) err = pool.Add(tx) if err != ErrInsufficientFunds { t.Error("expected", ErrInsufficientFunds) } balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(tx.Gas(), tx.GasPrice())) - pool.state.AddBalance(from, balance) + pool.currentState().AddBalance(from, balance) err = pool.Add(tx) if err != ErrIntrinsicGas { t.Error("expected", ErrIntrinsicGas, "got", err) } - pool.state.SetNonce(from, 1) - pool.state.AddBalance(from, big.NewInt(0xffffffffffffff)) + pool.currentState().SetNonce(from, 1) + pool.currentState().AddBalance(from, big.NewInt(0xffffffffffffff)) tx.GasLimit = big.NewInt(100000) tx.Price = big.NewInt(1) tx.SignECDSA(key) @@ -67,7 +67,7 @@ func TestTransactionQueue(t *testing.T) { tx := transaction() tx.SignECDSA(key) from, _ := tx.From() - pool.state.AddBalance(from, big.NewInt(1)) + pool.currentState().AddBalance(from, big.NewInt(1)) pool.queueTx(tx.Hash(), tx) pool.checkQueue() @@ -79,7 +79,7 @@ func TestTransactionQueue(t *testing.T) { tx.SetNonce(1) tx.SignECDSA(key) from, _ = tx.From() - pool.state.SetNonce(from, 2) + pool.currentState().SetNonce(from, 2) pool.queueTx(tx.Hash(), tx) pool.checkQueue() if _, ok := pool.pending[tx.Hash()]; ok { @@ -117,7 +117,7 @@ func TestRemoveTx(t *testing.T) { tx := transaction() tx.SignECDSA(key) from, _ := tx.From() - pool.state.AddBalance(from, big.NewInt(1)) + pool.currentState().AddBalance(from, big.NewInt(1)) pool.queueTx(tx.Hash(), tx) pool.addTx(tx.Hash(), from, tx) if len(pool.queue) != 1 { @@ -146,7 +146,7 @@ func TestNegativeValue(t *testing.T) { tx.Value().Set(big.NewInt(-1)) tx.SignECDSA(key) from, _ := tx.From() - pool.state.AddBalance(from, big.NewInt(1)) + pool.currentState().AddBalance(from, big.NewInt(1)) err := pool.Add(tx) if err != ErrNegativeValue { t.Error("expected", ErrNegativeValue, "got", err) @@ -156,7 +156,15 @@ func TestNegativeValue(t *testing.T) { func TestTransactionChainFork(t *testing.T) { pool, key := setupTxPool() addr := crypto.PubkeyToAddress(key.PublicKey) - pool.currentState().AddBalance(addr, big.NewInt(100000000000000)) + resetState := func() { + db, _ := ethdb.NewMemDatabase() + statedb := state.New(common.Hash{}, db) + pool.currentState = func() *state.StateDB { return statedb } + pool.currentState().AddBalance(addr, big.NewInt(100000000000000)) + pool.resetState() + } + resetState() + tx := transaction() tx.GasLimit = big.NewInt(100000) tx.SignECDSA(key) @@ -168,7 +176,7 @@ func TestTransactionChainFork(t *testing.T) { pool.RemoveTransactions([]*types.Transaction{tx}) // reset the pool's internal state - pool.resetState() + resetState() err = pool.add(tx) if err != nil { t.Error("didn't expect error", err) @@ -178,7 +186,15 @@ func TestTransactionChainFork(t *testing.T) { func TestTransactionDoubleNonce(t *testing.T) { pool, key := setupTxPool() addr := crypto.PubkeyToAddress(key.PublicKey) - pool.currentState().AddBalance(addr, big.NewInt(100000000000000)) + resetState := func() { + db, _ := ethdb.NewMemDatabase() + statedb := state.New(common.Hash{}, db) + pool.currentState = func() *state.StateDB { return statedb } + pool.currentState().AddBalance(addr, big.NewInt(100000000000000)) + pool.resetState() + } + resetState() + tx := transaction() tx.GasLimit = big.NewInt(100000) tx.SignECDSA(key) diff --git a/xeth/xeth.go b/xeth/xeth.go index 9b49b412c..d2f992084 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -957,7 +957,7 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS if err := self.backend.TxPool().Add(tx); err != nil { return "", err } - state.SetNonce(from, nonce+1) + //state.SetNonce(from, nonce+1) if contractCreation { addr := core.AddressFromMessage(tx) |