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; })