Skip to content

Commit bc6031e

Browse files
committed
core, xeth: moved nonce management burden from xeth to txpool
1 parent 93f4852 commit bc6031e

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

core/state/managed_state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type ManagedState struct {
2323
// ManagedState returns a new managed state with the statedb as it's backing layer
2424
func ManageState(statedb *StateDB) *ManagedState {
2525
return &ManagedState{
26-
StateDB: statedb,
26+
StateDB: statedb.Copy(),
2727
accounts: make(map[string]*account),
2828
}
2929
}

core/transaction_pool.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type stateFn func() *state.StateDB
3939
type TxPool struct {
4040
quit chan bool // Quiting channel
4141
currentState stateFn // The state function which will allow us to do some pre checkes
42-
state *state.ManagedState
42+
pendingState *state.ManagedState
4343
gasLimit func() *big.Int // The current gas limit function callback
4444
eventMux *event.TypeMux
4545
events event.Subscription
@@ -57,7 +57,7 @@ func NewTxPool(eventMux *event.TypeMux, currentStateFn stateFn, gasLimitFn func(
5757
eventMux: eventMux,
5858
currentState: currentStateFn,
5959
gasLimit: gasLimitFn,
60-
state: state.ManageState(currentStateFn()),
60+
pendingState: state.ManageState(currentStateFn()),
6161
}
6262
}
6363

@@ -76,7 +76,7 @@ func (pool *TxPool) Start() {
7676
}
7777

7878
func (pool *TxPool) resetState() {
79-
pool.state = state.ManageState(pool.currentState())
79+
pool.pendingState = state.ManageState(pool.currentState())
8080

8181
// validate the pool of pending transactions, this will remove
8282
// any transactions that have been included in the block or
@@ -90,7 +90,7 @@ func (pool *TxPool) resetState() {
9090
if addr, err := tx.From(); err == nil {
9191
// Set the nonce. Transaction nonce can never be lower
9292
// than the state nonce; validatePool took care of that.
93-
pool.state.SetNonce(addr, tx.Nonce())
93+
pool.pendingState.SetNonce(addr, tx.Nonce())
9494
}
9595
}
9696

@@ -110,7 +110,7 @@ func (pool *TxPool) State() *state.ManagedState {
110110
pool.mu.RLock()
111111
defer pool.mu.RUnlock()
112112

113-
return pool.state
113+
return pool.pendingState
114114
}
115115

116116
// 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
302302
if _, ok := pool.pending[hash]; !ok {
303303
pool.pending[hash] = tx
304304

305-
pool.state.SetNonce(addr, tx.AccountNonce)
305+
// Increment the nonce on the pending state. This can only happen if
306+
// the nonce is +1 to the previous one.
307+
pool.pendingState.SetNonce(addr, tx.AccountNonce+1)
306308
// Notify the subscribers. This event is posted in a goroutine
307309
// because it's possible that somewhere during the post "Remove transaction"
308310
// 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
312314

313315
// checkQueue moves transactions that have become processable to main pool.
314316
func (pool *TxPool) checkQueue() {
315-
state := pool.state
317+
state := pool.pendingState
316318

317319
var addq txQueue
318320
for address, txs := range pool.queue {
319-
curnonce := state.GetNonce(address)
321+
// guessed nonce is the nonce currently kept by the tx pool (pending state)
322+
guessedNonce := state.GetNonce(address)
323+
// true nonce is the nonce known by the last state
324+
trueNonce := pool.currentState().GetNonce(address)
320325
addq := addq[:0]
321326
for hash, tx := range txs {
322-
if tx.AccountNonce < curnonce {
327+
if tx.AccountNonce < trueNonce {
323328
// Drop queued transactions whose nonce is lower than
324329
// the account nonce because they have been processed.
325330
delete(txs, hash)
@@ -332,7 +337,7 @@ func (pool *TxPool) checkQueue() {
332337
// current account nonce.
333338
sort.Sort(addq)
334339
for _, e := range addq {
335-
if e.AccountNonce > curnonce {
340+
if e.AccountNonce > guessedNonce {
336341
break
337342
}
338343
delete(txs, e.hash)

core/transaction_pool_test.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@ func TestInvalidTransactions(t *testing.T) {
3737
}
3838

3939
from, _ := tx.From()
40-
pool.state.AddBalance(from, big.NewInt(1))
40+
pool.currentState().AddBalance(from, big.NewInt(1))
4141
err = pool.Add(tx)
4242
if err != ErrInsufficientFunds {
4343
t.Error("expected", ErrInsufficientFunds)
4444
}
4545

4646
balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(tx.Gas(), tx.GasPrice()))
47-
pool.state.AddBalance(from, balance)
47+
pool.currentState().AddBalance(from, balance)
4848
err = pool.Add(tx)
4949
if err != ErrIntrinsicGas {
5050
t.Error("expected", ErrIntrinsicGas, "got", err)
5151
}
5252

53-
pool.state.SetNonce(from, 1)
54-
pool.state.AddBalance(from, big.NewInt(0xffffffffffffff))
53+
pool.currentState().SetNonce(from, 1)
54+
pool.currentState().AddBalance(from, big.NewInt(0xffffffffffffff))
5555
tx.GasLimit = big.NewInt(100000)
5656
tx.Price = big.NewInt(1)
5757
tx.SignECDSA(key)
@@ -67,7 +67,7 @@ func TestTransactionQueue(t *testing.T) {
6767
tx := transaction()
6868
tx.SignECDSA(key)
6969
from, _ := tx.From()
70-
pool.state.AddBalance(from, big.NewInt(1))
70+
pool.currentState().AddBalance(from, big.NewInt(1))
7171
pool.queueTx(tx.Hash(), tx)
7272

7373
pool.checkQueue()
@@ -79,7 +79,7 @@ func TestTransactionQueue(t *testing.T) {
7979
tx.SetNonce(1)
8080
tx.SignECDSA(key)
8181
from, _ = tx.From()
82-
pool.state.SetNonce(from, 2)
82+
pool.currentState().SetNonce(from, 2)
8383
pool.queueTx(tx.Hash(), tx)
8484
pool.checkQueue()
8585
if _, ok := pool.pending[tx.Hash()]; ok {
@@ -117,7 +117,7 @@ func TestRemoveTx(t *testing.T) {
117117
tx := transaction()
118118
tx.SignECDSA(key)
119119
from, _ := tx.From()
120-
pool.state.AddBalance(from, big.NewInt(1))
120+
pool.currentState().AddBalance(from, big.NewInt(1))
121121
pool.queueTx(tx.Hash(), tx)
122122
pool.addTx(tx.Hash(), from, tx)
123123
if len(pool.queue) != 1 {
@@ -146,7 +146,7 @@ func TestNegativeValue(t *testing.T) {
146146
tx.Value().Set(big.NewInt(-1))
147147
tx.SignECDSA(key)
148148
from, _ := tx.From()
149-
pool.state.AddBalance(from, big.NewInt(1))
149+
pool.currentState().AddBalance(from, big.NewInt(1))
150150
err := pool.Add(tx)
151151
if err != ErrNegativeValue {
152152
t.Error("expected", ErrNegativeValue, "got", err)
@@ -156,7 +156,15 @@ func TestNegativeValue(t *testing.T) {
156156
func TestTransactionChainFork(t *testing.T) {
157157
pool, key := setupTxPool()
158158
addr := crypto.PubkeyToAddress(key.PublicKey)
159-
pool.currentState().AddBalance(addr, big.NewInt(100000000000000))
159+
resetState := func() {
160+
db, _ := ethdb.NewMemDatabase()
161+
statedb := state.New(common.Hash{}, db)
162+
pool.currentState = func() *state.StateDB { return statedb }
163+
pool.currentState().AddBalance(addr, big.NewInt(100000000000000))
164+
pool.resetState()
165+
}
166+
resetState()
167+
160168
tx := transaction()
161169
tx.GasLimit = big.NewInt(100000)
162170
tx.SignECDSA(key)
@@ -168,7 +176,7 @@ func TestTransactionChainFork(t *testing.T) {
168176
pool.RemoveTransactions([]*types.Transaction{tx})
169177

170178
// reset the pool's internal state
171-
pool.resetState()
179+
resetState()
172180
err = pool.add(tx)
173181
if err != nil {
174182
t.Error("didn't expect error", err)
@@ -178,7 +186,15 @@ func TestTransactionChainFork(t *testing.T) {
178186
func TestTransactionDoubleNonce(t *testing.T) {
179187
pool, key := setupTxPool()
180188
addr := crypto.PubkeyToAddress(key.PublicKey)
181-
pool.currentState().AddBalance(addr, big.NewInt(100000000000000))
189+
resetState := func() {
190+
db, _ := ethdb.NewMemDatabase()
191+
statedb := state.New(common.Hash{}, db)
192+
pool.currentState = func() *state.StateDB { return statedb }
193+
pool.currentState().AddBalance(addr, big.NewInt(100000000000000))
194+
pool.resetState()
195+
}
196+
resetState()
197+
182198
tx := transaction()
183199
tx.GasLimit = big.NewInt(100000)
184200
tx.SignECDSA(key)

xeth/xeth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS
957957
if err := self.backend.TxPool().Add(tx); err != nil {
958958
return "", err
959959
}
960-
state.SetNonce(from, nonce+1)
960+
//state.SetNonce(from, nonce+1)
961961

962962
if contractCreation {
963963
addr := core.AddressFromMessage(tx)

0 commit comments

Comments
 (0)