From 9702badd83399d62dca4df0cfd65587340def300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Wed, 31 May 2017 21:29:50 +0300 Subject: core: don't uselessly recheck transactions on dump --- core/tx_pool.go | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) (limited to 'core/tx_pool.go') diff --git a/core/tx_pool.go b/core/tx_pool.go index 1f5b46d4b..5e399d95f 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -339,17 +339,6 @@ func (pool *TxPool) Pending() (map[common.Address]types.Transactions, error) { pool.mu.Lock() defer pool.mu.Unlock() - state, err := pool.currentState() - if err != nil { - return nil, err - } - - // check queue first - pool.promoteExecutables(state) - - // invalidate any txs - pool.demoteUnexecutables(state) - pending := make(map[common.Address]types.Transactions) for addr, list := range pool.pending { pending[addr] = list.Flatten() @@ -551,12 +540,12 @@ func (pool *TxPool) Add(tx *types.Transaction) error { if err != nil { return err } - state, err := pool.currentState() - if err != nil { - return err - } // If we added a new transaction, run promotion checks and return if !replace { + state, err := pool.currentState() + if err != nil { + return err + } pool.promoteExecutables(state) } return nil @@ -579,11 +568,11 @@ func (pool *TxPool) AddBatch(txs []*types.Transaction) error { } // Only reprocess the internal state if something was actually added if added > 0 { - state, err := pool.currentState() - if err != nil { - return err - } if !replaced { + state, err := pool.currentState() + if err != nil { + return err + } pool.promoteExecutables(state) } } -- cgit v1.2.3 From cba33029a844a6631935f0ad57afd9793eabde6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Wed, 31 May 2017 21:49:20 +0300 Subject: core: only reorg changed account, not all --- core/tx_pool.go | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) (limited to 'core/tx_pool.go') diff --git a/core/tx_pool.go b/core/tx_pool.go index 5e399d95f..04ffa8a98 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -251,7 +251,7 @@ func (pool *TxPool) resetState() { } // Check the queue and move transactions over to the pending if possible // or remove those that have become invalid - pool.promoteExecutables(currentState) + pool.promoteExecutables(currentState, nil) } // Stop terminates the transaction pool. @@ -546,7 +546,8 @@ func (pool *TxPool) Add(tx *types.Transaction) error { if err != nil { return err } - pool.promoteExecutables(state) + from, _ := types.Sender(pool.signer, tx) // already validated + pool.promoteExecutables(state, []common.Address{from}) } return nil } @@ -557,24 +558,26 @@ func (pool *TxPool) AddBatch(txs []*types.Transaction) error { defer pool.mu.Unlock() // Add the batch of transaction, tracking the accepted ones - replaced, added := true, 0 + dirty := make(map[common.Address]struct{}) for _, tx := range txs { if replace, err := pool.add(tx); err == nil { - added++ if !replace { - replaced = false + from, _ := types.Sender(pool.signer, tx) // already validated + dirty[from] = struct{}{} } } } // Only reprocess the internal state if something was actually added - if added > 0 { - if !replaced { - state, err := pool.currentState() - if err != nil { - return err - } - pool.promoteExecutables(state) + if len(dirty) > 0 { + state, err := pool.currentState() + if err != nil { + return err + } + addrs := make([]common.Address, 0, len(dirty)) + for addr, _ := range dirty { + addrs = append(addrs, addr) } + pool.promoteExecutables(state, addrs) } return nil } @@ -651,12 +654,23 @@ func (pool *TxPool) removeTx(hash common.Hash) { // promoteExecutables moves transactions that have become processable from the // future queue to the set of pending transactions. During this process, all // invalidated transactions (low nonce, low balance) are deleted. -func (pool *TxPool) promoteExecutables(state *state.StateDB) { +func (pool *TxPool) promoteExecutables(state *state.StateDB, accounts []common.Address) { gaslimit := pool.gasLimit() + // Gather all the accounts potentially needing updates + if accounts == nil { + accounts = make([]common.Address, 0, len(pool.queue)) + for addr, _ := range pool.queue { + accounts = append(accounts, addr) + } + } // Iterate over all accounts and promote any executable transactions queued := uint64(0) - for addr, list := range pool.queue { + for _, addr := range accounts { + list := pool.queue[addr] + if list == nil { + continue // Just in case someone calls with a non existing account + } // Drop all transactions that are deemed too old (low nonce) for _, tx := range list.Forward(state.GetNonce(addr)) { hash := tx.Hash() -- cgit v1.2.3