core/txpool: guard nil head subscription

This commit is contained in:
apetro2 2026-03-19 00:24:39 +08:00
parent 469846166a
commit 7f64995ee3

View file

@ -17,7 +17,6 @@
package txpool package txpool
import ( import (
"errors"
"testing" "testing"
"time" "time"
@ -33,41 +32,30 @@ type nilHeadSubChain struct{}
func (nilHeadSubChain) Config() *params.ChainConfig { return params.TestChainConfig } func (nilHeadSubChain) Config() *params.ChainConfig { return params.TestChainConfig }
func (nilHeadSubChain) CurrentBlock() *types.Header { return &types.Header{} } func (nilHeadSubChain) CurrentBlock() *types.Header { return &types.Header{Root: types.EmptyRootHash} }
func (nilHeadSubChain) SubscribeChainHeadEvent(chan<- core.ChainHeadEvent) event.Subscription { func (nilHeadSubChain) SubscribeChainHeadEvent(chan<- core.ChainHeadEvent) event.Subscription {
return nil return nil
} }
func (nilHeadSubChain) StateAt(common.Hash) (*state.StateDB, error) { func (nilHeadSubChain) StateAt(common.Hash) (*state.StateDB, error) {
return nil, errors.New("not implemented") return state.New(types.EmptyRootHash, state.NewDatabaseForTesting())
} }
func TestTxPoolLoopNilHeadSubscription(t *testing.T) { func TestTxPoolCloseNilHeadSubscription(t *testing.T) {
t.Parallel() t.Parallel()
pool := &TxPool{ // TxPool.BlockChain exists to allow mocked chains in tests. A mock that
chain: nilHeadSubChain{}, // opts out of head notifications may return a nil subscription.
quit: make(chan chan error), pool, err := New(0, nilHeadSubChain{}, nil)
term: make(chan struct{}), if err != nil {
sync: make(chan chan error), t.Fatalf("failed to create txpool: %v", err)
} }
go pool.loop(nil)
errc := make(chan error, 1) if err := pool.Close(); err != nil {
select { t.Fatalf("unexpected close error: %v", err)
case pool.quit <- errc:
case <-time.After(time.Second):
t.Fatal("timed out waiting for txpool loop to accept quit signal")
}
select {
case err := <-errc:
if err != nil {
t.Fatalf("unexpected close error: %v", err)
}
case <-time.After(time.Second):
t.Fatal("timed out waiting for txpool loop to stop")
} }
select { select {
case <-pool.term: case <-pool.term:
case <-time.After(time.Second): case <-time.After(time.Second):