From 1776bf1313fe82553bf6758b737976ea56192ad4 Mon Sep 17 00:00:00 2001 From: Hao Duan Date: Wed, 3 Jun 2020 11:55:14 +0800 Subject: [PATCH] core: filter out txs with invalid signatures as soon as possible (#21170) Once we detect an invalid transaction during recovering signatures, we should directly exclude this transaction to avoid validating the signatures hereafter. This should optimize the validations times of transactions with invalid signatures to only one time. --- core/tx_pool.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/core/tx_pool.go b/core/tx_pool.go index e9782bc992..64eac3e403 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -929,16 +929,22 @@ func (pool *TxPool) addTxs(txs []*types.Transaction, local, sync bool) []error { knownTxMeter.Mark(1) continue } + // Exclude transactions with invalid signatures as soon as + // possible and cache senders in transactions before + // obtaining lock + _, err := types.Sender(pool.signer, tx) + if err != nil { + errs[i] = ErrInvalidSender + invalidTxMeter.Mark(1) + continue + } // Accumulate all unknown transactions for deeper processing news = append(news, tx) } if len(news) == 0 { return errs } - // Cache senders in transactions before obtaining lock (pool.signer is immutable) - for _, tx := range news { - types.Sender(pool.signer, tx) - } + // Process all the new transaction and merge any errors into the original slice pool.mu.Lock() newErrs, dirtyAddrs := pool.addTxsLocked(news, local)