From 289a80a7eb0c184c70b1a41b51ec2088d0816542 Mon Sep 17 00:00:00 2001 From: rob thijssen Date: Wed, 9 Sep 2026 17:57:02 +0300 Subject: [PATCH] fix: a single-runtime chain lost the one fact worth having MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `discover_runtimes` seeded its map with two plain inserts — the runtime at block 1 and the runtime at the tip. On a chain that has only ever run one, both probes report the same `spec_version` and the second insert overwrote the first, so the recorded height was the tip rather than block 1. Mainnet therefore read "in force from #13,501", which is where this observer started watching and not where the runtime started. Heisenberg hid it: its two ends are different runtimes, so nothing was overwritten and all six boundaries came out right. Both call sites and the recursion now go through one `note` helper that keeps the lowest height seen. That is the invariant the whole search rests on — bisection meets a runtime inside its reign before it finds where the reign began, so a probe can only improve the bound and none may worsen it — and it was previously written out twice, correctly in the recursion and not at the seed. `lower_first_seen` takes the minimum, so the wrong row corrects itself on the next discovery pass rather than needing a migration. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01Jp6a8EDar9ueEhAxzep4V5 --- crates/blackbeard-api/src/ingest.rs | 30 +++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/crates/blackbeard-api/src/ingest.rs b/crates/blackbeard-api/src/ingest.rs index d5ca56d..ab45e2d 100644 --- a/crates/blackbeard-api/src/ingest.rs +++ b/crates/blackbeard-api/src/ingest.rs @@ -978,9 +978,15 @@ async fn discover_runtimes(chain: Arc, store: Store) { return; }; + // `note`, never `insert`: on a chain that has only ever run one runtime + // both probes report the same `spec_version`, and a plain insert would let + // the tip overwrite the block-1 answer — throwing away the one fact worth + // having, that this runtime has been in force since the beginning. Mainnet + // read "in force from #13,501", which is where this observer started + // watching and not where the runtime started. let mut found = std::collections::BTreeMap::new(); - found.insert(low.0, (low.1.clone(), 1u64)); - found.insert(high.0, (high.1.clone(), tip)); + note(&mut found, low.0, &low.1, 1); + note(&mut found, high.0, &high.1, tip); if low.0 != high.0 { tracing::info!(chain = %id, from = low.0, to = high.0, tip, "searching for runtime upgrade boundaries"); @@ -1026,6 +1032,23 @@ async fn discover_runtimes(chain: Arc, store: Store) { tracing::info!(chain = %id, runtimes = count, "runtime history complete"); } +/// Record a runtime at a height, keeping the lowest height seen for it. +/// +/// The lowest is the answer: bisection meets a runtime somewhere inside its +/// reign before it finds where that reign began, so every probe can only +/// improve the bound and none may worsen it. +fn note( + found: &mut std::collections::BTreeMap, + spec: u32, + name: &str, + height: u64, +) { + found + .entry(spec) + .and_modify(|e| e.1 = e.1.min(height)) + .or_insert_with(|| (name.to_owned(), height)); +} + /// `(spec_version, spec_name)` at one height. async fn version_at(chain: &Arc, height: u64) -> Option<(u32, String)> { let hash = chain.rpc.block_hash(height).await.ok()??; @@ -1056,8 +1079,7 @@ fn bisect<'a>( let Some((spec, name)) = version_at(chain, mid).await else { return; }; - let entry = found.entry(spec).or_insert((name, mid)); - entry.1 = entry.1.min(mid); + note(found, spec, &name, mid); bisect(chain, low_height, low_spec, mid, spec, found).await; bisect(chain, mid, spec, high_height, high_spec, found).await; })