Files
node/chains/catchup_test.go
zeekay ef80498766 fix(consensus): wire the engine's catch-up transport — un-strand behind followers
node built the consensus engine with netCfg.Catchup unset, so the engine's
requestCatchup() hit a nil interface: a follower that fell behind DURING
consensus (it drives blocks by gossip, not Put/PushQuery) could never fetch the
missing ancestors and looped on an unfinalizable orphan forever. Observed live —
after the mainnet RLP recovery, luxd-0/2 stranded at 1082780 while peers reached
1082793. (Bootstrap had GetAncestors wired, so startup sync worked; only the
live-engine transport was unplugged — the same class of gap as the GossipOp
vote-transport bug.)

Fix is the decomplected bridge avalanche's design implies: the engine DECIDES
when to catch up (chain.Catchup); the blockHandler owns the WIRE — it already has
requestContext (send GetAncestors) and handleContext->Put->engine (deliver the
fetched ancestors). networkCatchup routes one to the other through a one-method
ancestorRequester interface (testable, narrow, no consensus/network cross-import),
late-bound at chainInfo construction since the handler wraps the engine. Behind
followers now self-heal without a restart.

RED test: a nil wire leaves calls=0 (the bug); the wired bridge routes
RequestAncestors -> requestContext once, for the missing block and the advertising
peer; + a compile-time guard that *blockHandler stays a valid ancestorRequester.
2026-06-26 11:35:49 -07:00

57 lines
2.2 KiB
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package chains
import (
"context"
"testing"
"github.com/luxfi/ids"
"github.com/stretchr/testify/require"
)
// *blockHandler must satisfy ancestorRequester, or the catch-up wire silently
// loses its transport. Catch it at compile time, not in production.
var _ ancestorRequester = (*blockHandler)(nil)
// catchupSpy records the requestContext calls networkCatchup bridges to.
type catchupSpy struct {
calls int
lastFrom ids.NodeID
lastBlock ids.ID
}
func (s *catchupSpy) requestContext(_ context.Context, from ids.NodeID, blockID ids.ID) {
s.calls++
s.lastFrom = from
s.lastBlock = blockID
}
// TestNetworkCatchup_BridgesEngineSignalToWire is the regression for the
// stranded-follower bug: node never set netCfg.Catchup, so the engine's
// requestCatchup hit a nil interface and a follower that fell behind during
// consensus never fetched the missing ancestors — it looped on an unfinalizable
// orphan forever (observed live: mainnet luxd-0/2 stuck at 1082780 while peers
// reached 1082793). The wire must (a) be nil-safe before its handler is
// late-bound and (b) route the engine's RequestAncestors to the handler's
// GetAncestors transport (requestContext), once, for the right block and peer.
func TestNetworkCatchup_BridgesEngineSignalToWire(t *testing.T) {
missing := ids.GenerateTestID()
peer := ids.GenerateTestNodeID()
// (a) Before late-binding (handler nil): a harmless no-op, never a panic.
c := &networkCatchup{}
require.NoError(t, c.RequestAncestors(ids.Empty, ids.Empty, missing, peer))
// (b) Once wired: the engine's catch-up signal reaches the GetAncestors wire
// exactly once — for the missing block, addressed to the peer that advertised
// its child. RED before the fix: handler is never set, calls stays 0.
spy := &catchupSpy{}
c.handler = spy
require.NoError(t, c.RequestAncestors(ids.Empty, ids.Empty, missing, peer))
require.Equal(t, 1, spy.calls, "RequestAncestors must route to requestContext — a nil wire IS the stranded-follower bug")
require.Equal(t, missing, spy.lastBlock)
require.Equal(t, peer, spy.lastFrom)
}