From 9513da48e21bcbee6690d70dbc723fa02a98b3f3 Mon Sep 17 00:00:00 2001 From: anshalshukla Date: Wed, 4 Sep 2024 15:01:04 +0530 Subject: [PATCH] fix: add back ethApi and mining login in handler --- cmd/geth/config.go | 5 +++-- cmd/geth/main.go | 27 ++++++++++++++++++++++++--- eth/api_backend.go | 9 +++++++++ eth/handler.go | 2 ++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/cmd/geth/config.go b/cmd/geth/config.go index 726df9d2bc..2cf2551911 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -40,6 +40,7 @@ import ( "github.com/ethereum/go-ethereum/eth/catalyst" "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/eth/ethconfig" + "github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/internal/flags" "github.com/ethereum/go-ethereum/internal/version" "github.com/ethereum/go-ethereum/log" @@ -189,7 +190,7 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) { } // makeFullNode loads geth configuration and creates the Ethereum backend. -func makeFullNode(ctx *cli.Context) *node.Node { +func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) { stack, cfg := makeConfigNode(ctx) if ctx.IsSet(utils.OverrideCancun.Name) { v := ctx.Int64(utils.OverrideCancun.Name) @@ -258,7 +259,7 @@ func makeFullNode(ctx *cli.Context) *node.Node { utils.Fatalf("failed to register catalyst service: %v", err) } } - return stack + return stack, backend } // dumpConfig is the dumpconfig command. diff --git a/cmd/geth/main.go b/cmd/geth/main.go index bde47590d3..e5fe247364 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -33,9 +33,11 @@ import ( "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/console/prompt" + "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/internal/debug" + "github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/internal/flags" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" @@ -376,10 +378,10 @@ func geth(ctx *cli.Context) error { } prepare(ctx) - stack := makeFullNode(ctx) + stack, backend := makeFullNode(ctx) defer stack.Close() - startNode(ctx, stack, false) + startNode(ctx, stack, backend, false) stack.Wait() return nil @@ -388,7 +390,7 @@ func geth(ctx *cli.Context) error { // startNode boots up the system node and all registered protocols, after which // it unlocks any requested accounts, and starts the RPC/IPC interfaces and the // miner. -func startNode(ctx *cli.Context, stack *node.Node, isConsole bool) { +func startNode(ctx *cli.Context, stack *node.Node, backend ethapi.Backend, isConsole bool) { debug.Memsize.Add("node", stack) // Start up the node itself @@ -465,6 +467,25 @@ func startNode(ctx *cli.Context, stack *node.Node, isConsole bool) { } }() } + + // Start auxiliary services if enabled + if ctx.Bool(utils.MiningEnabledFlag.Name) { + // Mining only makes sense if a full Ethereum node is running + if ctx.String(utils.SyncModeFlag.Name) == "light" { + utils.Fatalf("Light clients do not support mining") + } + + ethBackend, ok := backend.(*eth.EthAPIBackend) + if !ok { + utils.Fatalf("Ethereum service not running") + } + // Set the gas price to the limits from the CLI and start mining + gasprice := flags.GlobalBig(ctx, utils.MinerGasPriceFlag.Name) + ethBackend.TxPool().SetGasTip(gasprice) + if err := ethBackend.StartMining(); err != nil { + utils.Fatalf("Failed to start mining: %v", err) + } + } } // unlockAccounts unlocks any account specifically requested. diff --git a/eth/api_backend.go b/eth/api_backend.go index a73a433d5e..da3cb1a3d8 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -38,6 +38,7 @@ import ( "github.com/ethereum/go-ethereum/eth/tracers" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rpc" ) @@ -467,6 +468,14 @@ func (b *EthAPIBackend) CurrentHeader() *types.Header { return b.eth.blockchain.CurrentHeader() } +func (b *EthAPIBackend) Miner() *miner.Miner { + return b.eth.Miner() +} + +func (b *EthAPIBackend) StartMining() error { + return b.eth.StartMining() +} + func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (*state.StateDB, tracers.StateReleaseFunc, error) { return b.eth.stateAtBlock(ctx, block, reexec, base, readOnly, preferDisk) } diff --git a/eth/handler.go b/eth/handler.go index cb719e519b..12d826bdb6 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -97,6 +97,7 @@ type handlerConfig struct { EventMux *event.TypeMux // Legacy event mux, deprecate for `feed` checker ethereum.ChainValidator RequiredBlocks map[uint64]common.Hash // Hard coded map of required block hashes for sync challenges + EthAPI *ethapi.BlockChainAPI // EthAPI to interact enableBlockTracking bool // Whether to log information collected while tracking block lifecycle } @@ -155,6 +156,7 @@ func newHandler(config *handlerConfig) (*handler, error) { txpool: config.TxPool, chain: config.Chain, peers: newPeerSet(), + ethAPI: config.EthAPI, requiredBlocks: config.RequiredBlocks, enableBlockTracking: config.enableBlockTracking, quitSync: make(chan struct{}),