core, eth, miner: only retain 1 tx/nonce, remove bad ones

pull/2742/head
Péter Szilágyi 8 years ago
parent 49227f65ff
commit 795b70423e
  1. 282
      core/tx_pool.go
  2. 133
      core/tx_pool_test.go
  3. 2
      eth/api_backend.go
  4. 60
      internal/ethapi/api.go
  5. 2
      internal/ethapi/backend.go
  6. 10
      miner/worker.go

@ -51,6 +51,11 @@ const (
type stateFn func() (*state.StateDB, error) type stateFn func() (*state.StateDB, error)
// TxList is a "list" of transactions belonging to an account, sorted by account
// nonce. To allow gaps and avoid constant copying, the list is represented as a
// hash map.
type TxList map[uint64]*types.Transaction
// TxPool contains all currently known transactions. Transactions // TxPool contains all currently known transactions. Transactions
// enter the pool when they are received from the network or submitted // enter the pool when they are received from the network or submitted
// locally. They exit the pool when they are included in the blockchain. // locally. They exit the pool when they are included in the blockchain.
@ -68,8 +73,10 @@ type TxPool struct {
events event.Subscription events event.Subscription
localTx *txSet localTx *txSet
mu sync.RWMutex mu sync.RWMutex
pending map[common.Hash]*types.Transaction // processable transactions
queue map[common.Address]map[common.Hash]*types.Transaction pending map[common.Address]TxList // All currently processable transactions
queue map[common.Address]TxList // Queued but non-processable transactions
all map[common.Hash]*types.Transaction // All transactions to allow lookups
wg sync.WaitGroup // for shutdown sync wg sync.WaitGroup // for shutdown sync
@ -79,8 +86,9 @@ type TxPool struct {
func NewTxPool(config *ChainConfig, eventMux *event.TypeMux, currentStateFn stateFn, gasLimitFn func() *big.Int) *TxPool { func NewTxPool(config *ChainConfig, eventMux *event.TypeMux, currentStateFn stateFn, gasLimitFn func() *big.Int) *TxPool {
pool := &TxPool{ pool := &TxPool{
config: config, config: config,
pending: make(map[common.Hash]*types.Transaction), pending: make(map[common.Address]TxList),
queue: make(map[common.Address]map[common.Hash]*types.Transaction), queue: make(map[common.Address]TxList),
all: make(map[common.Hash]*types.Transaction),
eventMux: eventMux, eventMux: eventMux,
currentState: currentStateFn, currentState: currentStateFn,
gasLimit: gasLimitFn, gasLimit: gasLimitFn,
@ -143,12 +151,12 @@ func (pool *TxPool) resetState() {
// Loop over the pending transactions and base the nonce of the new // Loop over the pending transactions and base the nonce of the new
// pending transaction set. // pending transaction set.
for _, tx := range pool.pending { for addr, txs := range pool.pending {
if addr, err := tx.From(); err == nil { // Set the nonce. Transaction nonce can never be lower
// Set the nonce. Transaction nonce can never be lower // than the state nonce; validatePool took care of that.
// than the state nonce; validatePool took care of that. for nonce, _ := range txs {
if pool.pendingState.GetNonce(addr) <= tx.Nonce() { if pool.pendingState.GetNonce(addr) <= nonce {
pool.pendingState.SetNonce(addr, tx.Nonce()+1) pool.pendingState.SetNonce(addr, nonce+1)
} }
} }
} }
@ -174,7 +182,9 @@ func (pool *TxPool) Stats() (pending int, queued int) {
pool.mu.RLock() pool.mu.RLock()
defer pool.mu.RUnlock() defer pool.mu.RUnlock()
pending = len(pool.pending) for _, txs := range pool.pending {
pending += len(txs)
}
for _, txs := range pool.queue { for _, txs := range pool.queue {
queued += len(txs) queued += len(txs)
} }
@ -183,30 +193,27 @@ func (pool *TxPool) Stats() (pending int, queued int) {
// Content retrieves the data content of the transaction pool, returning all the // Content retrieves the data content of the transaction pool, returning all the
// pending as well as queued transactions, grouped by account and nonce. // pending as well as queued transactions, grouped by account and nonce.
func (pool *TxPool) Content() (map[common.Address]map[uint64][]*types.Transaction, map[common.Address]map[uint64][]*types.Transaction) { func (pool *TxPool) Content() (map[common.Address]TxList, map[common.Address]TxList) {
pool.mu.RLock() pool.mu.RLock()
defer pool.mu.RUnlock() defer pool.mu.RUnlock()
// Retrieve all the pending transactions and sort by account and by nonce // Retrieve all the pending transactions and sort by account and by nonce
pending := make(map[common.Address]map[uint64][]*types.Transaction) pending := make(map[common.Address]TxList)
for _, tx := range pool.pending { for addr, txs := range pool.pending {
account, _ := tx.From() copy := make(TxList)
for nonce, tx := range txs {
owned, ok := pending[account] copy[nonce] = tx
if !ok {
owned = make(map[uint64][]*types.Transaction)
pending[account] = owned
} }
owned[tx.Nonce()] = append(owned[tx.Nonce()], tx) pending[addr] = copy
} }
// Retrieve all the queued transactions and sort by account and by nonce // Retrieve all the queued transactions and sort by account and by nonce
queued := make(map[common.Address]map[uint64][]*types.Transaction) queued := make(map[common.Address]TxList)
for account, txs := range pool.queue { for addr, txs := range pool.queue {
owned := make(map[uint64][]*types.Transaction) copy := make(TxList)
for _, tx := range txs { for nonce, tx := range txs {
owned[tx.Nonce()] = append(owned[tx.Nonce()], tx) copy[nonce] = tx
} }
queued[account] = owned queued[addr] = copy
} }
return pending, queued return pending, queued
} }
@ -280,7 +287,7 @@ func (pool *TxPool) validateTx(tx *types.Transaction) error {
func (self *TxPool) add(tx *types.Transaction) error { func (self *TxPool) add(tx *types.Transaction) error {
hash := tx.Hash() hash := tx.Hash()
if self.pending[hash] != nil { if self.all[hash] != nil {
return fmt.Errorf("Known transaction (%x)", hash[:4]) return fmt.Errorf("Known transaction (%x)", hash[:4])
} }
err := self.validateTx(tx) err := self.validateTx(tx)
@ -306,33 +313,63 @@ func (self *TxPool) add(tx *types.Transaction) error {
return nil return nil
} }
// queueTx will queue an unknown transaction // queueTx will queue an unknown transaction.
func (self *TxPool) queueTx(hash common.Hash, tx *types.Transaction) { func (self *TxPool) queueTx(hash common.Hash, tx *types.Transaction) {
from, _ := tx.From() // already validated addr, _ := tx.From() // already validated
if self.queue[from] == nil { if self.queue[addr] == nil {
self.queue[from] = make(map[common.Hash]*types.Transaction) self.queue[addr] = make(TxList)
} }
self.queue[from][hash] = tx // If the nonce is already used, discard the lower priced transaction
nonce := tx.Nonce()
if old, ok := self.queue[addr][nonce]; ok {
if old.GasPrice().Cmp(tx.GasPrice()) >= 0 {
return // Old was better, discard this
}
delete(self.all, old.Hash()) // New is better, drop and overwrite old one
}
self.queue[addr][nonce] = tx
self.all[hash] = tx
} }
// addTx will add a transaction to the pending (processable queue) list of transactions // addTx will moves a transaction from the non-executable queue to the pending
func (pool *TxPool) addTx(hash common.Hash, addr common.Address, tx *types.Transaction) { // (processable) list of transactions.
// init delayed since tx pool could have been started before any state sync func (pool *TxPool) addTx(addr common.Address, tx *types.Transaction) {
// Init delayed since tx pool could have been started before any state sync
if pool.pendingState == nil { if pool.pendingState == nil {
pool.resetState() pool.resetState()
} }
// If the nonce is already used, discard the lower priced transaction
hash, nonce := tx.Hash(), tx.Nonce()
if _, ok := pool.pending[hash]; !ok { if old, ok := pool.pending[addr][nonce]; ok {
pool.pending[hash] = tx oldHash := old.Hash()
// Increment the nonce on the pending state. This can only happen if switch {
// the nonce is +1 to the previous one. case oldHash == hash: // Nothing changed, noop
pool.pendingState.SetNonce(addr, tx.Nonce()+1) return
// Notify the subscribers. This event is posted in a goroutine case old.GasPrice().Cmp(tx.GasPrice()) >= 0: // Old was better, discard this
// because it's possible that somewhere during the post "Remove transaction" delete(pool.all, hash)
// gets called which will then wait for the global tx pool lock and deadlock. return
go pool.eventMux.Post(TxPreEvent{tx}) default: // New is better, discard old
delete(pool.all, oldHash)
}
}
// The transaction is being kept, insert it into the tx pool
if _, ok := pool.pending[addr]; !ok {
pool.pending[addr] = make(TxList)
} }
pool.pending[addr][nonce] = tx
pool.all[hash] = tx
// Increment the nonce on the pending state. This can only happen if
// the nonce is +1 to the previous one.
pool.pendingState.SetNonce(addr, nonce+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.
go pool.eventMux.Post(TxPreEvent{tx})
} }
// Add queues a single transaction in the pool if it is valid. // Add queues a single transaction in the pool if it is valid.
@ -371,58 +408,39 @@ func (tp *TxPool) GetTransaction(hash common.Hash) *types.Transaction {
tp.mu.RLock() tp.mu.RLock()
defer tp.mu.RUnlock() defer tp.mu.RUnlock()
// check the txs first return tp.all[hash]
if tx, ok := tp.pending[hash]; ok {
return tx
}
// check queue
for _, txs := range tp.queue {
if tx, ok := txs[hash]; ok {
return tx
}
}
return nil
} }
// GetTransactions returns all currently processable transactions. // GetTransactions returns all currently processable transactions.
// The returned slice may be modified by the caller. // The returned slice may be modified by the caller.
func (self *TxPool) GetTransactions() (txs types.Transactions) { func (self *TxPool) GetTransactions() types.Transactions {
self.mu.Lock() self.mu.Lock()
defer self.mu.Unlock() defer self.mu.Unlock()
// check queue first // check queue first
self.checkQueue() self.checkQueue()
// invalidate any txs // invalidate any txs
self.validatePool() self.validatePool()
txs = make(types.Transactions, len(self.pending)) count := 0
i := 0 for _, txs := range self.pending {
for _, tx := range self.pending { count += len(txs)
txs[i] = tx
i++
} }
return txs pending := make(types.Transactions, 0, count)
} for _, txs := range self.pending {
// GetQueuedTransactions returns all non-processable transactions.
func (self *TxPool) GetQueuedTransactions() types.Transactions {
self.mu.RLock()
defer self.mu.RUnlock()
var ret types.Transactions
for _, txs := range self.queue {
for _, tx := range txs { for _, tx := range txs {
ret = append(ret, tx) pending = append(pending, tx)
} }
} }
sort.Sort(types.TxByNonce(ret)) return pending
return ret
} }
// RemoveTransactions removes all given transactions from the pool. // RemoveTransactions removes all given transactions from the pool.
func (self *TxPool) RemoveTransactions(txs types.Transactions) { func (self *TxPool) RemoveTransactions(txs types.Transactions) {
self.mu.Lock() self.mu.Lock()
defer self.mu.Unlock() defer self.mu.Unlock()
for _, tx := range txs { for _, tx := range txs {
self.removeTx(tx.Hash()) self.removeTx(tx.Hash())
} }
@ -432,29 +450,35 @@ func (self *TxPool) RemoveTransactions(txs types.Transactions) {
func (pool *TxPool) RemoveTx(hash common.Hash) { func (pool *TxPool) RemoveTx(hash common.Hash) {
pool.mu.Lock() pool.mu.Lock()
defer pool.mu.Unlock() defer pool.mu.Unlock()
pool.removeTx(hash) pool.removeTx(hash)
} }
func (pool *TxPool) removeTx(hash common.Hash) { func (pool *TxPool) removeTx(hash common.Hash) {
// delete from pending pool // Fetch the transaction we wish to delete
delete(pool.pending, hash) tx, ok := pool.all[hash]
// delete from queue if !ok {
for address, txs := range pool.queue { return
if _, ok := txs[hash]; ok { }
if len(txs) == 1 { addr, _ := tx.From()
// if only one tx, remove entire address entry.
delete(pool.queue, address) // Remove it from all internal lists
} else { delete(pool.all, hash)
delete(txs, hash)
} delete(pool.pending[addr], tx.Nonce())
break if len(pool.pending[addr]) == 0 {
} delete(pool.pending, addr)
}
delete(pool.queue[addr], tx.Nonce())
if len(pool.queue[addr]) == 0 {
delete(pool.queue, addr)
} }
} }
// checkQueue moves transactions that have become processable to main pool. // checkQueue moves transactions that have become processable from the pool's
// queue to the set of pending transactions.
func (pool *TxPool) checkQueue() { func (pool *TxPool) checkQueue() {
// init delayed since tx pool could have been started before any state sync // Init delayed since tx pool could have been started before any state sync
if pool.pendingState == nil { if pool.pendingState == nil {
pool.resetState() pool.resetState()
} }
@ -473,17 +497,19 @@ func (pool *TxPool) checkQueue() {
trueNonce = currentState.GetNonce(address) // nonce known by the last state trueNonce = currentState.GetNonce(address) // nonce known by the last state
) )
promote = promote[:0] promote = promote[:0]
for hash, tx := range txs { for nonce, tx := range txs {
// Drop processed or out of fund transactions // Drop processed or out of fund transactions
if tx.Nonce() < trueNonce || balance.Cmp(tx.Cost()) < 0 { if nonce < trueNonce || balance.Cmp(tx.Cost()) < 0 {
if glog.V(logger.Core) { if glog.V(logger.Core) {
glog.Infof("removed tx (%v) from pool queue: low tx nonce or out of funds\n", tx) glog.Infof("removed tx (%v) from pool queue: low tx nonce or out of funds\n", tx)
} }
delete(txs, hash) delete(txs, nonce)
delete(pool.all, tx.Hash())
continue continue
} }
// Collect the remaining transactions for the next pass. // Collect the remaining transactions for the next pass.
promote = append(promote, txQueueEntry{hash, address, tx}) promote = append(promote, txQueueEntry{address, tx})
} }
// Find the next consecutive nonce range starting at the current account nonce, // Find the next consecutive nonce range starting at the current account nonce,
// pushing the guessed nonce forward if we add consecutive transactions. // pushing the guessed nonce forward if we add consecutive transactions.
@ -493,17 +519,18 @@ func (pool *TxPool) checkQueue() {
if entry.Nonce() > guessedNonce { if entry.Nonce() > guessedNonce {
if len(promote)-i > maxQueued { if len(promote)-i > maxQueued {
if glog.V(logger.Debug) { if glog.V(logger.Debug) {
glog.Infof("Queued tx limit exceeded for %s. Tx %s removed\n", common.PP(address[:]), common.PP(entry.hash[:])) glog.Infof("Queued tx limit exceeded for %s. Tx %s removed\n", common.PP(address[:]), common.PP(entry.Hash().Bytes()))
} }
for _, drop := range promote[i+maxQueued:] { for _, drop := range promote[i+maxQueued:] {
delete(txs, drop.hash) delete(txs, drop.Nonce())
delete(pool.all, drop.Hash())
} }
} }
break break
} }
// Otherwise promote the transaction and move the guess nonce if needed // Otherwise promote the transaction and move the guess nonce if needed
pool.addTx(entry.hash, address, entry.Transaction) pool.addTx(address, entry.Transaction)
delete(txs, entry.hash) delete(txs, entry.Nonce())
if entry.Nonce() == guessedNonce { if entry.Nonce() == guessedNonce {
guessedNonce++ guessedNonce++
@ -532,40 +559,48 @@ func (pool *TxPool) validatePool() {
// Clean up the pending pool, accumulating invalid nonces // Clean up the pending pool, accumulating invalid nonces
gaps := make(map[common.Address]uint64) gaps := make(map[common.Address]uint64)
for hash, tx := range pool.pending { for addr, txs := range pool.pending {
sender, _ := tx.From() // err already checked for nonce, tx := range txs {
// Perform light nonce and balance validation
// Perform light nonce and balance validation balance := balanceCache[addr]
balance := balanceCache[sender] if balance == nil {
if balance == nil { balance = state.GetBalance(addr)
balance = state.GetBalance(sender) balanceCache[addr] = balance
balanceCache[sender] = balance
}
if past := state.GetNonce(sender) > tx.Nonce(); past || balance.Cmp(tx.Cost()) < 0 {
// Remove an already past it invalidated transaction
if glog.V(logger.Core) {
glog.Infof("removed tx (%v) from pool: low tx nonce or out of funds\n", tx)
} }
delete(pool.pending, hash) if past := state.GetNonce(addr) > nonce; past || balance.Cmp(tx.Cost()) < 0 {
// Remove an already past it invalidated transaction
if glog.V(logger.Core) {
glog.Infof("removed tx (%v) from pool: low tx nonce or out of funds\n", tx)
}
delete(pool.pending[addr], nonce)
if len(pool.pending[addr]) == 0 {
delete(pool.pending, addr)
}
delete(pool.all, tx.Hash())
// Track the smallest invalid nonce to postpone subsequent transactions // Track the smallest invalid nonce to postpone subsequent transactions
if !past { if !past {
if prev, ok := gaps[sender]; !ok || tx.Nonce() < prev { if prev, ok := gaps[addr]; !ok || nonce < prev {
gaps[sender] = tx.Nonce() gaps[addr] = nonce
}
} }
} }
} }
} }
// Move all transactions after a gap back to the future queue // Move all transactions after a gap back to the future queue
if len(gaps) > 0 { if len(gaps) > 0 {
for hash, tx := range pool.pending { for addr, txs := range pool.pending {
sender, _ := tx.From() for nonce, tx := range txs {
if gap, ok := gaps[sender]; ok && tx.Nonce() >= gap { if gap, ok := gaps[addr]; ok && nonce >= gap {
if glog.V(logger.Core) { if glog.V(logger.Core) {
glog.Infof("postponed tx (%v) due to introduced gap\n", tx) glog.Infof("postponed tx (%v) due to introduced gap\n", tx)
}
delete(pool.pending[addr], nonce)
if len(pool.pending[addr]) == 0 {
delete(pool.pending, addr)
}
pool.queueTx(tx.Hash(), tx)
} }
pool.queueTx(hash, tx)
delete(pool.pending, hash)
} }
} }
} }
@ -574,7 +609,6 @@ func (pool *TxPool) validatePool() {
type txQueue []txQueueEntry type txQueue []txQueueEntry
type txQueueEntry struct { type txQueueEntry struct {
hash common.Hash
addr common.Address addr common.Address
*types.Transaction *types.Transaction
} }

@ -103,7 +103,7 @@ func TestTransactionQueue(t *testing.T) {
currentState.SetNonce(from, 2) currentState.SetNonce(from, 2)
pool.queueTx(tx.Hash(), tx) pool.queueTx(tx.Hash(), tx)
pool.checkQueue() pool.checkQueue()
if _, ok := pool.pending[tx.Hash()]; ok { if _, ok := pool.pending[from][tx.Nonce()]; ok {
t.Error("expected transaction to be in tx pool") t.Error("expected transaction to be in tx pool")
} }
@ -139,7 +139,7 @@ func TestRemoveTx(t *testing.T) {
currentState, _ := pool.currentState() currentState, _ := pool.currentState()
currentState.AddBalance(from, big.NewInt(1)) currentState.AddBalance(from, big.NewInt(1))
pool.queueTx(tx.Hash(), tx) pool.queueTx(tx.Hash(), tx)
pool.addTx(tx.Hash(), from, tx) pool.addTx(from, tx)
if len(pool.queue) != 1 { if len(pool.queue) != 1 {
t.Error("expected queue to be 1, got", len(pool.queue)) t.Error("expected queue to be 1, got", len(pool.queue))
} }
@ -210,18 +210,38 @@ func TestTransactionDoubleNonce(t *testing.T) {
} }
resetState() resetState()
tx := transaction(0, big.NewInt(100000), key) tx1, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(100000), big.NewInt(1), nil).SignECDSA(key)
tx2 := transaction(0, big.NewInt(1000000), key) tx2, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(2), nil).SignECDSA(key)
if err := pool.add(tx); err != nil { tx3, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(1), nil).SignECDSA(key)
// Add the first two transaction, ensure higher priced stays only
if err := pool.add(tx1); err != nil {
t.Error("didn't expect error", err) t.Error("didn't expect error", err)
} }
if err := pool.add(tx2); err != nil { if err := pool.add(tx2); err != nil {
t.Error("didn't expect error", err) t.Error("didn't expect error", err)
} }
pool.checkQueue() pool.checkQueue()
if len(pool.pending) != 2 { if len(pool.pending[addr]) != 1 {
t.Error("expected 2 pending txs. Got", len(pool.pending)) t.Error("expected 1 pending transactions, got", len(pool.pending))
}
if tx := pool.pending[addr][0]; tx.Hash() != tx2.Hash() {
t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash())
}
// Add the thid transaction and ensure it's not saved (smaller price)
if err := pool.add(tx3); err != nil {
t.Error("didn't expect error", err)
}
pool.checkQueue()
if len(pool.pending[addr]) != 1 {
t.Error("expected 1 pending transactions, got", len(pool.pending))
}
if tx := pool.pending[addr][0]; tx.Hash() != tx2.Hash() {
t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash())
}
// Ensure the total transaction count is correct
if len(pool.all) != 1 {
t.Error("expected 1 total transactions, got", len(pool.all))
} }
} }
@ -234,12 +254,15 @@ func TestMissingNonce(t *testing.T) {
if err := pool.add(tx); err != nil { if err := pool.add(tx); err != nil {
t.Error("didn't expect error", err) t.Error("didn't expect error", err)
} }
if len(pool.pending) != 0 { if len(pool.pending[addr]) != 0 {
t.Error("expected 0 pending transactions, got", len(pool.pending)) t.Error("expected 0 pending transactions, got", len(pool.pending[addr]))
} }
if len(pool.queue[addr]) != 1 { if len(pool.queue[addr]) != 1 {
t.Error("expected 1 queued transaction, got", len(pool.queue[addr])) t.Error("expected 1 queued transaction, got", len(pool.queue[addr]))
} }
if len(pool.all) != 1 {
t.Error("expected 1 total transactions, got", len(pool.all))
}
} }
func TestNonceRecovery(t *testing.T) { func TestNonceRecovery(t *testing.T) {
@ -270,8 +293,11 @@ func TestRemovedTxEvent(t *testing.T) {
currentState.AddBalance(from, big.NewInt(1000000000000)) currentState.AddBalance(from, big.NewInt(1000000000000))
pool.eventMux.Post(RemovedTransactionEvent{types.Transactions{tx}}) pool.eventMux.Post(RemovedTransactionEvent{types.Transactions{tx}})
pool.eventMux.Post(ChainHeadEvent{nil}) pool.eventMux.Post(ChainHeadEvent{nil})
if len(pool.pending) != 1 { if len(pool.pending[from]) != 1 {
t.Error("expected 1 pending tx, got", len(pool.pending)) t.Error("expected 1 pending tx, got", len(pool.pending[from]))
}
if len(pool.all) != 1 {
t.Error("expected 1 total transactions, got", len(pool.all))
} }
} }
@ -292,41 +318,50 @@ func TestTransactionDropping(t *testing.T) {
tx10 = transaction(10, big.NewInt(100), key) tx10 = transaction(10, big.NewInt(100), key)
tx11 = transaction(11, big.NewInt(200), key) tx11 = transaction(11, big.NewInt(200), key)
) )
pool.addTx(tx0.Hash(), account, tx0) pool.addTx(account, tx0)
pool.addTx(tx1.Hash(), account, tx1) pool.addTx(account, tx1)
pool.queueTx(tx10.Hash(), tx10) pool.queueTx(tx10.Hash(), tx10)
pool.queueTx(tx11.Hash(), tx11) pool.queueTx(tx11.Hash(), tx11)
// Check that pre and post validations leave the pool as is // Check that pre and post validations leave the pool as is
if len(pool.pending) != 2 { if len(pool.pending[account]) != 2 {
t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending), 2) t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending[account]), 2)
} }
if len(pool.queue[account]) != 2 { if len(pool.queue[account]) != 2 {
t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue), 2) t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue[account]), 2)
}
if len(pool.all) != 4 {
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 4)
} }
pool.resetState() pool.resetState()
if len(pool.pending) != 2 { if len(pool.pending[account]) != 2 {
t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending), 2) t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending[account]), 2)
} }
if len(pool.queue[account]) != 2 { if len(pool.queue[account]) != 2 {
t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue), 2) t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue[account]), 2)
}
if len(pool.all) != 4 {
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 4)
} }
// Reduce the balance of the account, and check that invalidated transactions are dropped // Reduce the balance of the account, and check that invalidated transactions are dropped
state.AddBalance(account, big.NewInt(-750)) state.AddBalance(account, big.NewInt(-750))
pool.resetState() pool.resetState()
if _, ok := pool.pending[tx0.Hash()]; !ok { if _, ok := pool.pending[account][tx0.Nonce()]; !ok {
t.Errorf("funded pending transaction missing: %v", tx0) t.Errorf("funded pending transaction missing: %v", tx0)
} }
if _, ok := pool.pending[tx1.Hash()]; ok { if _, ok := pool.pending[account][tx1.Nonce()]; ok {
t.Errorf("out-of-fund pending transaction present: %v", tx1) t.Errorf("out-of-fund pending transaction present: %v", tx1)
} }
if _, ok := pool.queue[account][tx10.Hash()]; !ok { if _, ok := pool.queue[account][tx10.Nonce()]; !ok {
t.Errorf("funded queued transaction missing: %v", tx10) t.Errorf("funded queued transaction missing: %v", tx10)
} }
if _, ok := pool.queue[account][tx11.Hash()]; ok { if _, ok := pool.queue[account][tx11.Nonce()]; ok {
t.Errorf("out-of-fund queued transaction present: %v", tx11) t.Errorf("out-of-fund queued transaction present: %v", tx11)
} }
if len(pool.all) != 2 {
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 2)
}
} }
// Tests that if a transaction is dropped from the current pending pool (e.g. out // Tests that if a transaction is dropped from the current pending pool (e.g. out
@ -349,50 +384,59 @@ func TestTransactionPostponing(t *testing.T) {
} else { } else {
tx = transaction(uint64(i), big.NewInt(500), key) tx = transaction(uint64(i), big.NewInt(500), key)
} }
pool.addTx(tx.Hash(), account, tx) pool.addTx(account, tx)
txns = append(txns, tx) txns = append(txns, tx)
} }
// Check that pre and post validations leave the pool as is // Check that pre and post validations leave the pool as is
if len(pool.pending) != len(txns) { if len(pool.pending[account]) != len(txns) {
t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending), len(txns)) t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending[account]), len(txns))
} }
if len(pool.queue[account]) != 0 { if len(pool.queue[account]) != 0 {
t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue), 0) t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue[account]), 0)
}
if len(pool.all) != len(txns) {
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns))
} }
pool.resetState() pool.resetState()
if len(pool.pending) != len(txns) { if len(pool.pending[account]) != len(txns) {
t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending), len(txns)) t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending[account]), len(txns))
} }
if len(pool.queue[account]) != 0 { if len(pool.queue[account]) != 0 {
t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue), 0) t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue[account]), 0)
}
if len(pool.all) != len(txns) {
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns))
} }
// Reduce the balance of the account, and check that transactions are reorganised // Reduce the balance of the account, and check that transactions are reorganised
state.AddBalance(account, big.NewInt(-750)) state.AddBalance(account, big.NewInt(-750))
pool.resetState() pool.resetState()
if _, ok := pool.pending[txns[0].Hash()]; !ok { if _, ok := pool.pending[account][txns[0].Nonce()]; !ok {
t.Errorf("tx %d: valid and funded transaction missing from pending pool: %v", 0, txns[0]) t.Errorf("tx %d: valid and funded transaction missing from pending pool: %v", 0, txns[0])
} }
if _, ok := pool.queue[account][txns[0].Hash()]; ok { if _, ok := pool.queue[account][txns[0].Nonce()]; ok {
t.Errorf("tx %d: valid and funded transaction present in future queue: %v", 0, txns[0]) t.Errorf("tx %d: valid and funded transaction present in future queue: %v", 0, txns[0])
} }
for i, tx := range txns[1:] { for i, tx := range txns[1:] {
if i%2 == 1 { if i%2 == 1 {
if _, ok := pool.pending[tx.Hash()]; ok { if _, ok := pool.pending[account][tx.Nonce()]; ok {
t.Errorf("tx %d: valid but future transaction present in pending pool: %v", i+1, tx) t.Errorf("tx %d: valid but future transaction present in pending pool: %v", i+1, tx)
} }
if _, ok := pool.queue[account][tx.Hash()]; !ok { if _, ok := pool.queue[account][tx.Nonce()]; !ok {
t.Errorf("tx %d: valid but future transaction missing from future queue: %v", i+1, tx) t.Errorf("tx %d: valid but future transaction missing from future queue: %v", i+1, tx)
} }
} else { } else {
if _, ok := pool.pending[tx.Hash()]; ok { if _, ok := pool.pending[account][tx.Nonce()]; ok {
t.Errorf("tx %d: out-of-fund transaction present in pending pool: %v", i+1, tx) t.Errorf("tx %d: out-of-fund transaction present in pending pool: %v", i+1, tx)
} }
if _, ok := pool.queue[account][tx.Hash()]; ok { if _, ok := pool.queue[account][tx.Nonce()]; ok {
t.Errorf("tx %d: out-of-fund transaction present in future queue: %v", i+1, tx) t.Errorf("tx %d: out-of-fund transaction present in future queue: %v", i+1, tx)
} }
} }
} }
if len(pool.all) != len(txns)/2 {
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns)/2)
}
} }
// Tests that if the transaction count belonging to a single account goes above // Tests that if the transaction count belonging to a single account goes above
@ -423,6 +467,9 @@ func TestTransactionQueueLimiting(t *testing.T) {
} }
} }
} }
if len(pool.all) != maxQueued {
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), maxQueued)
}
} }
// Tests that even if the transaction count belonging to a single account goes // Tests that even if the transaction count belonging to a single account goes
@ -441,13 +488,16 @@ func TestTransactionPendingLimiting(t *testing.T) {
if err := pool.Add(transaction(i, big.NewInt(100000), key)); err != nil { if err := pool.Add(transaction(i, big.NewInt(100000), key)); err != nil {
t.Fatalf("tx %d: failed to add transaction: %v", i, err) t.Fatalf("tx %d: failed to add transaction: %v", i, err)
} }
if len(pool.pending) != int(i)+1 { if len(pool.pending[account]) != int(i)+1 {
t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, len(pool.pending), i+1) t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, len(pool.pending[account]), i+1)
} }
if len(pool.queue[account]) != 0 { if len(pool.queue[account]) != 0 {
t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, len(pool.queue[account]), 0) t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, len(pool.queue[account]), 0)
} }
} }
if len(pool.all) != maxQueued+5 {
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), maxQueued+5)
}
} }
// Tests that the transaction limits are enforced the same way irrelevant whether // Tests that the transaction limits are enforced the same way irrelevant whether
@ -486,6 +536,9 @@ func testTransactionLimitingEquivalency(t *testing.T, origin uint64) {
if len(pool1.queue[account1]) != len(pool2.queue[account2]) { if len(pool1.queue[account1]) != len(pool2.queue[account2]) {
t.Errorf("queued transaction count mismatch: one-by-one algo: %d, batch algo: %d", len(pool1.queue[account1]), len(pool2.queue[account2])) t.Errorf("queued transaction count mismatch: one-by-one algo: %d, batch algo: %d", len(pool1.queue[account1]), len(pool2.queue[account2]))
} }
if len(pool1.all) != len(pool2.all) {
t.Errorf("total transaction count mismatch: one-by-one algo %d, batch algo %d", len(pool1.all), len(pool2.all))
}
} }
// Benchmarks the speed of validating the contents of the pending queue of the // Benchmarks the speed of validating the contents of the pending queue of the
@ -503,7 +556,7 @@ func benchmarkValidatePool(b *testing.B, size int) {
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
tx := transaction(uint64(i), big.NewInt(100000), key) tx := transaction(uint64(i), big.NewInt(100000), key)
pool.addTx(tx.Hash(), account, tx) pool.addTx(account, tx)
} }
// Benchmark the speed of pool validation // Benchmark the speed of pool validation
b.ResetTimer() b.ResetTimer()

@ -149,7 +149,7 @@ func (b *EthApiBackend) Stats() (pending int, queued int) {
return b.eth.txPool.Stats() return b.eth.txPool.Stats()
} }
func (b *EthApiBackend) TxPoolContent() (map[common.Address]map[uint64][]*types.Transaction, map[common.Address]map[uint64][]*types.Transaction) { func (b *EthApiBackend) TxPoolContent() (map[common.Address]core.TxList, map[common.Address]core.TxList) {
b.eth.txMu.Lock() b.eth.txMu.Lock()
defer b.eth.txMu.Unlock() defer b.eth.txMu.Unlock()

@ -100,32 +100,26 @@ func NewPublicTxPoolAPI(b Backend) *PublicTxPoolAPI {
} }
// Content returns the transactions contained within the transaction pool. // Content returns the transactions contained within the transaction pool.
func (s *PublicTxPoolAPI) Content() map[string]map[string]map[string][]*RPCTransaction { func (s *PublicTxPoolAPI) Content() map[string]map[string]map[string]*RPCTransaction {
content := map[string]map[string]map[string][]*RPCTransaction{ content := map[string]map[string]map[string]*RPCTransaction{
"pending": make(map[string]map[string][]*RPCTransaction), "pending": make(map[string]map[string]*RPCTransaction),
"queued": make(map[string]map[string][]*RPCTransaction), "queued": make(map[string]map[string]*RPCTransaction),
} }
pending, queue := s.b.TxPoolContent() pending, queue := s.b.TxPoolContent()
// Flatten the pending transactions // Flatten the pending transactions
for account, batches := range pending { for account, txs := range pending {
dump := make(map[string][]*RPCTransaction) dump := make(map[string]*RPCTransaction)
for nonce, txs := range batches { for nonce, tx := range txs {
nonce := fmt.Sprintf("%d", nonce) dump[fmt.Sprintf("%d", nonce)] = newRPCPendingTransaction(tx)
for _, tx := range txs {
dump[nonce] = append(dump[nonce], newRPCPendingTransaction(tx))
}
} }
content["pending"][account.Hex()] = dump content["pending"][account.Hex()] = dump
} }
// Flatten the queued transactions // Flatten the queued transactions
for account, batches := range queue { for account, txs := range queue {
dump := make(map[string][]*RPCTransaction) dump := make(map[string]*RPCTransaction)
for nonce, txs := range batches { for nonce, tx := range txs {
nonce := fmt.Sprintf("%d", nonce) dump[fmt.Sprintf("%d", nonce)] = newRPCPendingTransaction(tx)
for _, tx := range txs {
dump[nonce] = append(dump[nonce], newRPCPendingTransaction(tx))
}
} }
content["queued"][account.Hex()] = dump content["queued"][account.Hex()] = dump
} }
@ -143,10 +137,10 @@ func (s *PublicTxPoolAPI) Status() map[string]*rpc.HexNumber {
// Inspect retrieves the content of the transaction pool and flattens it into an // Inspect retrieves the content of the transaction pool and flattens it into an
// easily inspectable list. // easily inspectable list.
func (s *PublicTxPoolAPI) Inspect() map[string]map[string]map[string][]string { func (s *PublicTxPoolAPI) Inspect() map[string]map[string]map[string]string {
content := map[string]map[string]map[string][]string{ content := map[string]map[string]map[string]string{
"pending": make(map[string]map[string][]string), "pending": make(map[string]map[string]string),
"queued": make(map[string]map[string][]string), "queued": make(map[string]map[string]string),
} }
pending, queue := s.b.TxPoolContent() pending, queue := s.b.TxPoolContent()
@ -158,24 +152,18 @@ func (s *PublicTxPoolAPI) Inspect() map[string]map[string]map[string][]string {
return fmt.Sprintf("contract creation: %v wei + %v × %v gas", tx.Value(), tx.Gas(), tx.GasPrice()) return fmt.Sprintf("contract creation: %v wei + %v × %v gas", tx.Value(), tx.Gas(), tx.GasPrice())
} }
// Flatten the pending transactions // Flatten the pending transactions
for account, batches := range pending { for account, txs := range pending {
dump := make(map[string][]string) dump := make(map[string]string)
for nonce, txs := range batches { for nonce, tx := range txs {
nonce := fmt.Sprintf("%d", nonce) dump[fmt.Sprintf("%d", nonce)] = format(tx)
for _, tx := range txs {
dump[nonce] = append(dump[nonce], format(tx))
}
} }
content["pending"][account.Hex()] = dump content["pending"][account.Hex()] = dump
} }
// Flatten the queued transactions // Flatten the queued transactions
for account, batches := range queue { for account, txs := range queue {
dump := make(map[string][]string) dump := make(map[string]string)
for nonce, txs := range batches { for nonce, tx := range txs {
nonce := fmt.Sprintf("%d", nonce) dump[fmt.Sprintf("%d", nonce)] = format(tx)
for _, tx := range txs {
dump[nonce] = append(dump[nonce], format(tx))
}
} }
content["queued"][account.Hex()] = dump content["queued"][account.Hex()] = dump
} }

@ -58,7 +58,7 @@ type Backend interface {
GetPoolTransaction(txHash common.Hash) *types.Transaction GetPoolTransaction(txHash common.Hash) *types.Transaction
GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
Stats() (pending int, queued int) Stats() (pending int, queued int)
TxPoolContent() (map[common.Address]map[uint64][]*types.Transaction, map[common.Address]map[uint64][]*types.Transaction) TxPoolContent() (map[common.Address]core.TxList, map[common.Address]core.TxList)
} }
type State interface { type State interface {

@ -68,12 +68,12 @@ type Work struct {
ancestors *set.Set // ancestor set (used for checking uncle parent validity) ancestors *set.Set // ancestor set (used for checking uncle parent validity)
family *set.Set // family set (used for checking uncle invalidity) family *set.Set // family set (used for checking uncle invalidity)
uncles *set.Set // uncle set uncles *set.Set // uncle set
remove *set.Set // tx which will be removed
tcount int // tx count in cycle tcount int // tx count in cycle
ignoredTransactors *set.Set ignoredTransactors *set.Set
lowGasTransactors *set.Set lowGasTransactors *set.Set
ownedAccounts *set.Set ownedAccounts *set.Set
lowGasTxs types.Transactions lowGasTxs types.Transactions
failedTxs types.Transactions
localMinedBlocks *uint64RingBuffer // the most recent block numbers that were mined locally (used to check block inclusion) localMinedBlocks *uint64RingBuffer // the most recent block numbers that were mined locally (used to check block inclusion)
Block *types.Block // the new block Block *types.Block // the new block
@ -383,7 +383,6 @@ func (self *worker) makeCurrent(parent *types.Block, header *types.Header) error
accounts := self.eth.AccountManager().Accounts() accounts := self.eth.AccountManager().Accounts()
// Keep track of transactions which return errors so they can be removed // Keep track of transactions which return errors so they can be removed
work.remove = set.New()
work.tcount = 0 work.tcount = 0
work.ignoredTransactors = set.New() work.ignoredTransactors = set.New()
work.lowGasTransactors = set.New() work.lowGasTransactors = set.New()
@ -533,7 +532,9 @@ func (self *worker) commitNewWork() {
*/ */
work.commitTransactions(self.mux, transactions, self.gasPrice, self.chain) work.commitTransactions(self.mux, transactions, self.gasPrice, self.chain)
self.eth.TxPool().RemoveTransactions(work.lowGasTxs) self.eth.TxPool().RemoveTransactions(work.lowGasTxs)
self.eth.TxPool().RemoveTransactions(work.failedTxs)
// compute uncles for the new block. // compute uncles for the new block.
var ( var (
@ -639,11 +640,10 @@ func (env *Work) commitTransactions(mux *event.TypeMux, transactions types.Trans
// ignore the transactor so no nonce errors will be thrown for this account // ignore the transactor so no nonce errors will be thrown for this account
// next time the worker is run, they'll be picked up again. // next time the worker is run, they'll be picked up again.
env.ignoredTransactors.Add(from) env.ignoredTransactors.Add(from)
glog.V(logger.Detail).Infof("Gas limit reached for (%x) in this block. Continue to try smaller txs\n", from[:4]) glog.V(logger.Detail).Infof("Gas limit reached for (%x) in this block. Continue to try smaller txs\n", from[:4])
case err != nil:
env.remove.Add(tx.Hash())
case err != nil:
env.failedTxs = append(env.failedTxs, tx)
if glog.V(logger.Detail) { if glog.V(logger.Detail) {
glog.Infof("TX (%x) failed, will be removed: %v\n", tx.Hash().Bytes()[:4], err) glog.Infof("TX (%x) failed, will be removed: %v\n", tx.Hash().Bytes()[:4], err)
} }

Loading…
Cancel
Save