feat: shutdown node at upgrade height

This commit is contained in:
Shawn 2025-06-04 17:14:58 -07:00
parent 6ebd351534
commit f29e6ddb65
2 changed files with 32 additions and 0 deletions

View file

@ -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)

View file

@ -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