Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

txpool: streamline initialisation #13202

Merged
merged 14 commits into from
Jan 7, 2025
36 changes: 19 additions & 17 deletions cmd/txpool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/common/datadir"
"github.com/erigontech/erigon-lib/common/paths"
"github.com/erigontech/erigon-lib/direct"
"github.com/erigontech/erigon-lib/gointerfaces"
"github.com/erigontech/erigon-lib/gointerfaces/grpcutil"
Expand All @@ -38,16 +39,12 @@ import (
"github.com/erigontech/erigon-lib/kv/remotedbserver"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon/cmd/rpcdaemon/rpcdaemontest"
"github.com/erigontech/erigon/consensus/misc"
"github.com/erigontech/erigon/ethdb/privateapi"
"github.com/erigontech/erigon/txnprovider/txpool"
"github.com/erigontech/erigon/txnprovider/txpool/txpoolcfg"
"github.com/erigontech/erigon/txnprovider/txpool/txpoolutil"

"github.com/erigontech/erigon-lib/common/paths"
"github.com/erigontech/erigon/cmd/utils"
"github.com/erigontech/erigon/ethdb/privateapi"
"github.com/erigontech/erigon/turbo/debug"
"github.com/erigontech/erigon/turbo/logging"
"github.com/erigontech/erigon/txnprovider/txpool"
"github.com/erigontech/erigon/txnprovider/txpool/txpoolcfg"
)

var (
Expand Down Expand Up @@ -183,26 +180,31 @@ func doTxpool(ctx context.Context, logger log.Logger) error {
cfg.TracedSenders[i] = string(sender[:])
}

newTxs := make(chan txpool.Announcements, 1024)
defer close(newTxs)
txPoolDB, txPool, fetch, send, txpoolGrpcServer, err := txpoolutil.AllComponents(ctx, cfg,
kvcache.New(cacheConfig), newTxs, coreDB, sentryClients, kvClient, misc.Eip1559FeeCalculator, logger)
notifyMiner := func() {}
txPool, txpoolGrpcServer, err := txpool.Assemble(
ctx,
cfg,
coreDB,
kvcache.New(cacheConfig),
sentryClients,
kvClient,
notifyMiner,
logger,
)
if err != nil {
return err
}
defer txPoolDB.Close()
fetch.ConnectCore()
fetch.ConnectSentries()

miningGrpcServer := privateapi.NewMiningServer(ctx, &rpcdaemontest.IsMiningMock{}, nil, logger)

grpcServer, err := txpool.StartGrpc(txpoolGrpcServer, miningGrpcServer, txpoolApiAddr, nil, logger)
if err != nil {
return err
}

notifyMiner := func() {}
txpool.MainLoop(ctx, txPool, newTxs, send, txpoolGrpcServer.NewSlotsStreams, notifyMiner)
err = txPool.Run(ctx)
if err != nil {
return err
}

grpcServer.GracefulStop()
return nil
Expand Down
105 changes: 48 additions & 57 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ import (
"github.com/erigontech/erigon/consensus/clique"
"github.com/erigontech/erigon/consensus/ethash"
"github.com/erigontech/erigon/consensus/merge"
"github.com/erigontech/erigon/consensus/misc"
"github.com/erigontech/erigon/core"
"github.com/erigontech/erigon/core/rawdb"
"github.com/erigontech/erigon/core/rawdb/blockio"
Expand Down Expand Up @@ -134,7 +133,6 @@ import (
"github.com/erigontech/erigon/txnprovider/shutter"
"github.com/erigontech/erigon/txnprovider/txpool"
"github.com/erigontech/erigon/txnprovider/txpool/txpoolcfg"
"github.com/erigontech/erigon/txnprovider/txpool/txpoolutil"
)

// Config contains the configuration options of the ETH protocol.
Expand Down Expand Up @@ -193,16 +191,12 @@ type Ethereum struct {
waitForStageLoopStop chan struct{}
waitForMiningStop chan struct{}

txPoolDB kv.RwDB
txPool *txpool.TxPool
newTxs chan txpool.Announcements
txPoolFetch *txpool.Fetch
txPoolSend *txpool.Send
txPoolGrpcServer txpoolproto.TxpoolServer
shutterPool *shutter.Pool
notifyMiningAboutNewTxs chan struct{}
forkValidator *engine_helpers.ForkValidator
downloader *downloader.Downloader
txPool *txpool.TxPool
txPoolGrpcServer txpoolproto.TxpoolServer
shutterPool *shutter.Pool
blockBuilderNotifyNewTxns chan struct{}
forkValidator *engine_helpers.ForkValidator
downloader *downloader.Downloader

blockSnapshots *freezeblocks.RoSnapshots
blockReader services.FullBlockReader
Expand Down Expand Up @@ -276,14 +270,17 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger

// kv_remote architecture does blocks on stream.Send - means current architecture require unlimited amount of txs to provide good throughput
backend := &Ethereum{
sentryCtx: ctx,
sentryCancel: ctxCancel,
config: config,
networkID: config.NetworkID,
etherbase: config.Miner.Etherbase,
waitForStageLoopStop: make(chan struct{}),
waitForMiningStop: make(chan struct{}),
logger: logger,
sentryCtx: ctx,
sentryCancel: ctxCancel,
config: config,
networkID: config.NetworkID,
etherbase: config.Miner.Etherbase,
waitForStageLoopStop: make(chan struct{}),
waitForMiningStop: make(chan struct{}),
blockBuilderNotifyNewTxns: make(chan struct{}, 1),
miningSealingQuit: make(chan struct{}),
minedBlocks: make(chan *types.Block, 1),
logger: logger,
stopNode: func() error {
return stack.Close()
},
Expand Down Expand Up @@ -645,34 +642,43 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger
return nil, err
}

var txnProvider txnprovider.TxnProvider
var miningRPC txpoolproto.MiningServer
stateDiffClient := direct.NewStateDiffClientDirect(kvRPC)
var txnProvider txnprovider.TxnProvider
if config.TxPool.Disable {
backend.txPoolGrpcServer = &txpool.GrpcDisabled{}
} else {
backend.newTxs = make(chan txpool.Announcements, 1024)
backend.txPoolDB, backend.txPool, backend.txPoolFetch, backend.txPoolSend, backend.txPoolGrpcServer, err = txpoolutil.AllComponents(
ctx, config.TxPool, kvcache.NewDummy(), backend.newTxs, backend.chainDB, backend.sentriesClient.Sentries(), stateDiffClient, misc.Eip1559FeeCalculator, logger,
sentries := backend.sentriesClient.Sentries()
blockBuilderNotifyNewTxns := func() {
select {
case backend.blockBuilderNotifyNewTxns <- struct{}{}:
default:
}
}
backend.txPool, backend.txPoolGrpcServer, err = txpool.Assemble(
ctx,
config.TxPool,
backend.chainDB,
kvcache.NewDummy(),
sentries,
stateDiffClient,
blockBuilderNotifyNewTxns,
logger,
)
if err != nil {
return nil, err
}

txnProvider = backend.txPool
}
if config.Shutter.Enabled {
if config.TxPool.Disable {
panic("can't enable shutter pool when devp2p txpool is disabled")
}
backend.shutterPool = shutter.NewPool(logger, config.Shutter, txnProvider)

backend.shutterPool = shutter.NewPool(logger, config.Shutter, backend.txPool)
txnProvider = backend.shutterPool
}

backend.notifyMiningAboutNewTxs = make(chan struct{}, 1)
backend.miningSealingQuit = make(chan struct{})
backend.pendingBlocks = make(chan *types.Block, 1)
backend.minedBlocks = make(chan *types.Block, 1)

miner := stagedsync.NewMiningState(&config.Miner)
backend.pendingBlocks = miner.PendingResultCh

Expand Down Expand Up @@ -791,7 +797,7 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger

blockRetire := freezeblocks.NewBlockRetire(1, dirs, blockReader, blockWriter, backend.chainDB, heimdallStore, bridgeStore, backend.chainConfig, config, backend.notifications.Events, segmentsBuildLimiter, logger)

miningRPC = privateapi.NewMiningServer(ctx, backend, ethashApi, logger)
var miningRPC txpoolproto.MiningServer = privateapi.NewMiningServer(ctx, backend, ethashApi, logger)

var creds credentials.TransportCredentials
if stack.Config().PrivateApiAddr != "" {
Expand Down Expand Up @@ -821,26 +827,6 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger
if currentBlock == nil {
currentBlock = genesis
}
// We start the transaction pool on startup, for a couple of reasons:
// 1) Hive tests requires us to do so and starting it from eth_sendRawTransaction is not viable as we have not enough data
// to initialize it properly.
// 2) we cannot propose for block 1 regardless.

if !config.TxPool.Disable {
backend.txPoolFetch.ConnectCore()
backend.txPoolFetch.ConnectSentries()
var newTxsBroadcaster *txpool.NewSlotsStreams
if casted, ok := backend.txPoolGrpcServer.(*txpool.GrpcServer); ok {
newTxsBroadcaster = casted.NewSlotsStreams
}
go txpool.MainLoop(backend.sentryCtx, backend.txPool, backend.newTxs, backend.txPoolSend, newTxsBroadcaster,
func() {
select {
case backend.notifyMiningAboutNewTxs <- struct{}{}:
default:
}
})
}

go func() {
defer debug.LogPanic()
Expand Down Expand Up @@ -1279,8 +1265,8 @@ func (s *Ethereum) StartMining(ctx context.Context, db kv.RwDB, stateDiffClient
// block info in the state channel
hasWork = true

case <-s.notifyMiningAboutNewTxs:
//log.Warn("[dbg] notifyMiningAboutNewTxs")
case <-s.blockBuilderNotifyNewTxns:
//log.Warn("[dbg] blockBuilderNotifyNewTxns")

// Skip mining based on new txn notif for bor consensus
hasWork = s.chainConfig.Bor == nil
Expand Down Expand Up @@ -1598,6 +1584,14 @@ func (s *Ethereum) Start() error {
}
}

if s.txPool != nil {
// We start the transaction pool on startup, for a couple of reasons:
// 1) Hive tests requires us to do so and starting it from eth_sendRawTransaction is not viable as we have not enough data
// to initialize it properly.
// 2) we cannot propose for block 1 regardless.
s.bgComponentsEg.Go(func() error { return s.txPool.Run(s.sentryCtx) })
}

if s.shutterPool != nil {
s.bgComponentsEg.Go(func() error { return s.shutterPool.Run(s.sentryCtx) })
}
Expand Down Expand Up @@ -1639,9 +1633,6 @@ func (s *Ethereum) Stop() error {
for _, sentryServer := range s.sentryServers {
sentryServer.Close()
}
if s.txPoolDB != nil {
s.txPoolDB.Close()
}
s.chainDB.Close()

if s.silkwormRPCDaemonService != nil {
Expand Down
Loading
Loading