Skip to content

[codex] Implement BRC-136 BASM overlay sync#170

Open
ty-everett wants to merge 9 commits into
bsv-blockchain:mainfrom
ty-everett:codex/brc136-basm-sync
Open

[codex] Implement BRC-136 BASM overlay sync#170
ty-everett wants to merge 9 commits into
bsv-blockchain:mainfrom
ty-everett:codex/brc136-basm-sync

Conversation

@ty-everett

@ty-everett ty-everett commented May 29, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Implements BRC-136 BASM primitives in @bsv/overlay: computeBasmRoot, computeTac, anchor/raw-tx wire types, and a lightweight BASM peer remote.
  • Persists direct proof metadata, raw transaction bytes, per-topic admitted tx block metadata, and topic block anchors through a new Knex migration.
  • Adds Engine APIs for anchor reads, admitted lists, compound BUMP responses, raw tx responses, BASM catch-up, BEEF compaction after direct proof ingestion, and opt-in unproven transaction eviction.
  • Adds Overlay Express BRC-136 routes plus admin startBASMSync and evictUnproven routes.
  • Extends Overlay Monitor with BASM tip/TAC/staleness/unproven-state probes while keeping lookup probes intact.
  • Adds BRC-136 conformance vectors and optional raw-tx extension types in @bsv/gasp without changing existing GASP behavior.

Review Notes

  • Normative BASM/TAC hashing follows the published BRC-136 wording: public hash fields stay display-order hex, hashing reverses to Bitcoin internal byte order, empty BASM root is 32 zero bytes, single-leaf root is the txid, and odd leaves duplicate.
  • BASM reconciliation verifies compound BUMP proofs before fetching raw transactions, then submits raw txs with the verified proof attached instead of asking peers for BEEF.
  • Unproven eviction is opt-in. The default threshold is 144 blocks, and eviction only targets topic-applied transactions without direct proof metadata.

BRC-136 Author Context

Requested @sirdeggen for review. Deggen authored BRC-136 with Siggi, so also tagging @siggioskarsson for visibility.

Validation

  • corepack pnpm --filter @bsv/overlay run build
  • corepack pnpm --filter @bsv/overlay-express run build
  • corepack pnpm --filter @bsv/gasp run build
  • corepack pnpm --filter @bsv/overlay exec jest --runInBand
  • corepack pnpm --filter @bsv/overlay-express exec jest --runInBand
  • corepack pnpm conformance

Lint Note

Package-wide lint:ci currently reports pre-existing unrelated issues in OverlayGASPStorage.ts and OverlayExpress.test.ts; this PR does not include those cleanup changes.

@ty-everett ty-everett requested a review from sirdeggen as a code owner May 29, 2026 19:39
@ty-everett

Copy link
Copy Markdown
Contributor Author

cc @sirdeggen for BRC-136 review context. GitHub rejected my formal reviewer request from the fork account, so tagging here explicitly. cc @siggioskarsson as co-author context.

@ty-everett

Copy link
Copy Markdown
Contributor Author

Correction: GitHub now shows @sirdeggen in the review requests. Keeping this explicit cc here as well for BRC-136 review context. cc @siggioskarsson.

@sirdeggen sirdeggen self-assigned this May 29, 2026
sirdeggen and others added 8 commits June 8, 2026 10:30
Per BRC-136, the cumulative TAC must advance on every block so a peer can authoritatively confirm a block contained no transactions for a topic. Previously the chain only recomputed touched heights, which could reset across empty blocks and leave gaps from out-of-order proofs.

Engine: rebuild contiguous anchor slices over [fromHeight, toHeight], filling missing heights with empty anchors (zero basmRoot, zero admittedCount) chained off the prior TAC. Add advanceTopicAnchorChains() to extend every topic's chain forward to the current tip, halting cleanly when a block hash cannot be resolved and capping work per pass at DEFAULT_BASM_RANGE_LIMIT.

OverlayExpress: extend anchor chains to the tip on startup and poll periodically (basmBlockPollIntervalMs, default 10m; 0 disables) so chains follow new blocks. Add configureBASMBlockPollInterval().

Add BASMChain.test.ts covering TAC continuity across empty blocks.

Signed-off-by: Deggen <d.kellenschwiler@bsvassociation.org>
Document the BRC-136 admitted-set vs lookup-set separation (admin/janitor
removal cannot rewrite anchors/TAC) and add the approved design spec for
reorg handling that reuses the chaintracks subscribeReorgs mechanism.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Primary reorg trigger is go-chaintracks /v2/reorg/stream (ReorgEvent:
orphanedHashes, commonAncestor, newTip, depth), consumed via an SSE client.
In-process subscribeReorgs becomes a secondary adapter; poll sweep covers
event-less trackers and SSE reconnect catch-up (no Last-Event-ID replay).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add Engine.handleReorg (demote orphaned-block admissions to unproven, rebuild
affected topic anchor chains over canonical hashes) and revalidateRecentAnchors
(root-revalidation sweep for the fallback + SSE reconnect catch-up). Add
forceResolve to anchor rebuild so stale block hashes are re-resolved, plus the
storage methods findProvenAppliedTransactionsByBlockHash / ...InRange /
demoteAppliedTransactionToUnproven (interface + Knex). Tested in BASMChain.test.ts.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add ReorgStream (parseReorgEvent + extractSseFrames + ReorgSseAdapter) to
consume go-chaintracks/Arcade /v2/reorg/stream, mapping each ReorgEvent to
Engine.handleReorg. Wire into the BASM lifecycle: configureReorgStream sets the
URL + scan depth; the block poll also runs revalidateRecentAnchors as a fallback
and the SSE adapter runs it as reconnect catch-up. Pure helpers unit-tested.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add a Reorg handling section to BRC-136-BASM.md: go-chaintracks/Arcade
/v2/reorg/stream as the reorg authority + ReorgEvent shape, the
demote-and-rebuild flow, the revalidation sweep fallback + reconnect catch-up,
interaction with admin/janitor removal, config, and operator verification steps.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Hoist H101_NEW, the post-reorg header resolver, and seedTwoBlockChain to module
scope so the reorg-handling and revalidation-sweep suites share one copy,
clearing SonarCloud new-code duplication.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
docs-site validate walks docs/** and requires schema frontmatter; the spec
lacked it. Dates quoted so YAML keeps them as strings.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@sonarqubecloud

sonarqubecloud Bot commented Jun 8, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
4.2% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants