Scope block reorg detection to the current chain
Blocks are now stamped with the Bitcoin network name ("main", "test",
"signet") at ingest time. The reconcile loop's Pass 2 skips any stored
block whose chain differs from the node's current chain, preventing
testnet blocks from being falsely orphaned after switching back to
mainnet. Legacy rows with an empty chain field fall through unchanged.
The UI shows a blue "test" / "signet" badge next to blocks from a
non-current network so operators can distinguish cross-chain history
from genuine reorg-orphaned blocks.
This commit is contained in:
@@ -23,6 +23,7 @@ type BlockRecord struct {
|
||||
Source string `json:"source"` // "logmon" for now; "zmq" later
|
||||
ShareDiff float64 `json:"share_diff,omitempty"`
|
||||
OrphanedAt time.Time `json:"orphaned_at,omitempty"`
|
||||
Chain string `json:"chain,omitempty"` // "main", "test", "signet"
|
||||
}
|
||||
|
||||
// IngestAttemptEvents counts "Possible/Submitting block solve" log
|
||||
@@ -67,11 +68,20 @@ func (a *Aggregator) IngestBlockEvents(ctx context.Context, events <-chan logmon
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
// Stamp the chain name at the moment the block is seen so
|
||||
// the reconcile loop can skip it if the node switches networks.
|
||||
a.mu.RLock()
|
||||
currentChain := ""
|
||||
if a.snap.Chain != nil {
|
||||
currentChain = a.snap.Chain.Chain
|
||||
}
|
||||
a.mu.RUnlock()
|
||||
rec := BlockRecord{
|
||||
Height: ev.Height,
|
||||
FoundAt: ev.SeenAt,
|
||||
Source: "logmon",
|
||||
ShareDiff: ev.ShareDiff,
|
||||
Chain: currentChain,
|
||||
}
|
||||
// Best-effort enrich with hash + coinbase reward via bitcoind.
|
||||
// We look up the hash from height, then fetch the full block
|
||||
@@ -101,6 +111,7 @@ func (a *Aggregator) IngestBlockEvents(ctx context.Context, events <-chan logmon
|
||||
FoundAt: rec.FoundAt,
|
||||
Source: rec.Source,
|
||||
ShareDiff: rec.ShareDiff,
|
||||
Chain: rec.Chain,
|
||||
})
|
||||
if err != nil {
|
||||
a.Log.Warn("block persist failed", "height", rec.Height, "err", err)
|
||||
@@ -199,6 +210,18 @@ func (a *Aggregator) reconcileOnce(ctx context.Context) {
|
||||
// matches our record. Don't bother with rows that are already
|
||||
// orphaned — we won't un-orphan, since the network already
|
||||
// chose another chain.
|
||||
//
|
||||
// Skip blocks whose recorded chain doesn't match the current
|
||||
// bitcoind network — they were mined on a different node config
|
||||
// (e.g. testnet) and cannot be checked against mainnet's tip.
|
||||
// A mismatch here is a false positive, not a real reorg.
|
||||
a.mu.RLock()
|
||||
currentChain := ""
|
||||
if a.snap.Chain != nil {
|
||||
currentChain = a.snap.Chain.Chain
|
||||
}
|
||||
a.mu.RUnlock()
|
||||
|
||||
recent, err := a.Store.Recent(64)
|
||||
if err != nil {
|
||||
a.Log.Warn("reconcile: load recent failed", "err", err)
|
||||
@@ -213,6 +236,13 @@ func (a *Aggregator) reconcileOnce(ctx context.Context) {
|
||||
if b.FoundAt.Before(since) {
|
||||
continue
|
||||
}
|
||||
// Both chain values must be known before we can compare them.
|
||||
// Legacy rows (Chain=="") fall through so existing installs keep
|
||||
// their reorg detection — there was no network switch before this
|
||||
// feature landed.
|
||||
if b.Chain != "" && currentChain != "" && b.Chain != currentChain {
|
||||
continue
|
||||
}
|
||||
lookupCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
canonical, herr := a.RPC.GetBlockHash(lookupCtx, b.Height)
|
||||
cancel()
|
||||
@@ -291,6 +321,7 @@ func (a *Aggregator) loadPersistedBlocks() {
|
||||
Source: r.Source,
|
||||
ShareDiff: r.ShareDiff,
|
||||
OrphanedAt: r.OrphanedAt,
|
||||
Chain: r.Chain,
|
||||
})
|
||||
}
|
||||
a.mu.Lock()
|
||||
|
||||
Reference in New Issue
Block a user