core/txpool: get tx by sender and nonce

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2024-10-03 20:37:21 +08:00
parent 3cbadcdf3d
commit acdf44bc14
4 changed files with 52 additions and 0 deletions

View file

@ -1208,6 +1208,26 @@ func (p *BlobPool) Get(hash common.Hash) *types.Transaction {
return item
}
// GetBySenderAndNonce returns a transaction of a sender and its corresponding nonce.
func (p *BlobPool) GetBySenderAndNonce(sender common.Address, nonce uint64) *types.Transaction {
p.lock.RLock()
defer p.lock.RUnlock()
txs, ok := p.index[sender]
if !ok {
return nil
}
next := p.state.GetNonce(sender)
offset := int(nonce - next)
if offset < 0 || offset >= len(txs) {
return nil
}
return p.Get(txs[offset].hash)
}
// Add inserts a set of blob transactions into the pool if they pass validation (both
// consensus validity and pool restrictions).
func (p *BlobPool) Add(txs []*types.Transaction, local bool, sync bool) []error {

View file

@ -1072,6 +1072,25 @@ func (pool *LegacyPool) Get(hash common.Hash) *types.Transaction {
return tx
}
// GetBySenderAndNonce returns a transaction of a sender and it's corresponding nonce.
func (pool *LegacyPool) GetBySenderAndNonce(sender common.Address, nonce uint64) *types.Transaction {
// Check if the transaction is in the pending pool
if txList := pool.pending[sender]; txList != nil {
if tx := txList.txs.items[nonce]; tx != nil {
return tx
}
}
// Check if the transaction is in the queue pool
if txList := pool.queue[sender]; txList != nil {
if tx := txList.txs.items[nonce]; tx != nil {
return tx
}
}
return nil
}
// get returns a transaction if it is contained in the pool and nil otherwise.
func (pool *LegacyPool) get(hash common.Hash) *types.Transaction {
return pool.all.Get(hash)

View file

@ -123,6 +123,9 @@ type SubPool interface {
// Get returns a transaction if it is contained in the pool, or nil otherwise.
Get(hash common.Hash) *types.Transaction
// GetBySenderAndNonce returns a transaction of a sender and it's corresponding nonce.
GetBySenderAndNonce(sender common.Address, nonce uint64) *types.Transaction
// Add enqueues a batch of transactions into the pool if they are valid. Due
// to the large transaction churn, add may postpone fully integrating the tx
// to a later point to batch multiple ones together.

View file

@ -305,6 +305,16 @@ func (p *TxPool) Get(hash common.Hash) *types.Transaction {
return nil
}
// GetBySenderAndNonce returns a transaction of a sender and it's corresponding nonce.
func (p *TxPool) GetBySenderAndNonce(sender common.Address, nonce uint64) *types.Transaction {
for _, subpool := range p.subpools {
if tx := subpool.GetBySenderAndNonce(sender, nonce); tx != nil {
return tx
}
}
return nil
}
// Add enqueues a batch of transactions into the pool if they are valid. Due
// to the large transaction churn, add may postpone fully integrating the tx
// to a later point to batch multiple ones together.