core, xeth: moved nonce management burden from xeth to txpool
This commit is contained in:
parent
93f4852844
commit
bc6031e7bb
@ -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),
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user