From f29e6ddb652baa67c924d418bd5ff045df3f278c Mon Sep 17 00:00:00 2001 From: Shawn <44221603+shaspitz@users.noreply.github.com> Date: Wed, 4 Jun 2025 17:14:58 -0700 Subject: [PATCH] feat: shutdown node at upgrade height --- cmd/geth/main.go | 3 +++ cmd/utils/cmd.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index a94a49ea7d..08e7c1d5b8 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -355,6 +355,9 @@ func startNode(ctx *cli.Context, stack *node.Node, backend ethapi.Backend, isCon // Start up the node itself utils.StartNode(ctx, stack, isConsole) + upgradeBlockHeight := uint64(10) + utils.ShutdownAtUpgradeBlockHeight(ctx, stack, upgradeBlockHeight) + // Unlock any account specifically requested unlockAccounts(ctx, stack) diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 4b57164665..46e4b53256 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -123,6 +123,35 @@ func StartNode(ctx *cli.Context, stack *node.Node, isConsole bool) { }() } +func ShutdownAtUpgradeBlockHeight(ctx *cli.Context, n *node.Node, upgradeBlockHeight uint64) { + sub := n.EventMux().Subscribe(core.ChainHeadEvent{}) + go func() { + defer sub.Unsubscribe() + for { + select { + case <-ctx.Done(): + log.Info("ShutdownAtUpgradeBlockHeight: context cancelled, exiting goroutine") + return + case ev, ok := <-sub.Chan(): + if !ok { + log.Error("ShutdownAtUpgradeBlockHeight: subscription closed, exiting goroutine") + return + } + ch, ok := ev.Data.(core.ChainHeadEvent) + if !ok { + log.Error("ShutdownAtUpgradeBlockHeight: failed to convert ChainHeadEvent, exiting goroutine") + continue + } + if ch.Block.Number().Uint64() >= upgradeBlockHeight { + log.Info("Target upgrade block height reached, initiating shutdown", "block", ch.Block.Number().Uint64()) + n.Close() + return + } + } + } + }() +} + func monitorFreeDiskSpace(sigc chan os.Signal, path string, freeDiskSpaceCritical uint64) { if path == "" { return