improve loop and transaction sender in test

This commit is contained in:
Lucia 2025-11-08 13:44:02 +13:00
parent 7f9b06e7aa
commit 17d845ea82

View file

@ -54,7 +54,8 @@ var (
// Its goal is to get around setting up a valid statedb for the balance and nonce // Its goal is to get around setting up a valid statedb for the balance and nonce
// checks. // checks.
type testTxPool struct { type testTxPool struct {
pool map[common.Hash]*types.Transaction // Hash map of collected transactions pool map[common.Hash]*types.Transaction // Hash map of collected transactions
signer types.Signer // Fallback signer that understands modern tx types
txFeed event.Feed // Notification feed to allow waiting for inclusion txFeed event.Feed // Notification feed to allow waiting for inclusion
lock sync.RWMutex // Protects the transaction pool lock sync.RWMutex // Protects the transaction pool
@ -63,7 +64,8 @@ type testTxPool struct {
// newTestTxPool creates a mock transaction pool. // newTestTxPool creates a mock transaction pool.
func newTestTxPool() *testTxPool { func newTestTxPool() *testTxPool {
return &testTxPool{ return &testTxPool{
pool: make(map[common.Hash]*types.Transaction), pool: make(map[common.Hash]*types.Transaction),
signer: types.LatestSigner(params.MergedTestChainConfig),
} }
} }
@ -134,7 +136,10 @@ func (p *testTxPool) Pending(filter txpool.PendingFilter) map[common.Address][]*
batches := make(map[common.Address][]*types.Transaction) batches := make(map[common.Address][]*types.Transaction)
for _, tx := range p.pool { for _, tx := range p.pool {
from, _ := types.Sender(types.HomesteadSigner{}, tx) from, err := p.senderOf(tx)
if err != nil {
panic(err)
}
batches[from] = append(batches[from], tx) batches[from] = append(batches[from], tx)
} }
for _, batch := range batches { for _, batch := range batches {
@ -157,6 +162,22 @@ func (p *testTxPool) Pending(filter txpool.PendingFilter) map[common.Address][]*
return pending return pending
} }
// senderOf attempts to recover the sender of any transaction type the tests
// might place into the pool. It prefers using the tx's explicit chain ID when
// available and falls back to a permissive signer that enables all modern
// transaction types.
func (p *testTxPool) senderOf(tx *types.Transaction) (common.Address, error) {
if chainID := tx.ChainId(); chainID != nil && chainID.Sign() > 0 {
if from, err := types.Sender(types.LatestSignerForChainID(chainID), tx); err == nil {
return from, nil
}
}
if from, err := types.Sender(p.signer, tx); err == nil {
return from, nil
}
return types.Sender(types.HomesteadSigner{}, tx)
}
// SubscribeTransactions should return an event subscription of NewTxsEvent and // SubscribeTransactions should return an event subscription of NewTxsEvent and
// send events to the given channel. // send events to the given channel.
func (p *testTxPool) SubscribeTransactions(ch chan<- core.NewTxsEvent, reorgs bool) event.Subscription { func (p *testTxPool) SubscribeTransactions(ch chan<- core.NewTxsEvent, reorgs bool) event.Subscription {
@ -292,7 +313,7 @@ func benchmarkBroadcastChoice(b *testing.B, npeers int) {
choice := newBroadcastChoice(self, [16]byte{1}) choice := newBroadcastChoice(self, [16]byte{1})
b.ResetTimer() b.ResetTimer()
for i := range b.N { for i := 0; i < b.N; i++ {
set := choice.choosePeers(peers, txsenders[i]) set := choice.choosePeers(peers, txsenders[i])
if len(set) == 0 { if len(set) == 0 {
b.Fatal("empty result") b.Fatal("empty result")