Skip to content

Speak the @bsv/sdk 4.1.1 overlay wire for lookup and broadcast - #21

Merged
rohenaz merged 8 commits into
mainfrom
overlay-ts-wire
Nov 16, 2026
Merged

rohenaz merged 8 commits into
mainfrom
overlay-ts-wire

Conversation

@rohenaz

@rohenaz rohenaz commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Before: Swift overlay lookup and broadcast (BRC-22/24) differed from @bsv/sdk 4.1.1 in STEAK keys, aggregated Atomic BEEF, SHIP/SLAP admin token checks, headers and off-chain values, so TS overlay services and Swift clients could not fully talk. One failing or bogus host also made a whole broadcast fail.

After: lookup and broadcast match TS 4.1.1 on the wire, host discovery and acknowledgment follow TS rules, and hostile hosts can't exhaust memory or crowd out honest hosts.

How: codecs and resolver/broadcaster logic follow the TS source (LookupResolver, SHIPBroadcaster, LookupHostQueue, createPublicNetworkFetch), with TS vectors in the tests. Details in COMP-053, COMP-058 and COMP-061.

Behaviour changes

  • STEAK uses camelCase keys; aggregated answers are split into per-output Atomic BEEF matching toBinaryAtomic (BUMP order can still differ when a child precedes its parents; both are valid). X-Topics and off-chain values follow TS; any 2xx passes and anything not octet-stream is read as JSON.
  • SHIP/SLAP admin tokens are verified as TS does, including the linked locking key.
  • Broadcast uses SHIPBroadcaster's acknowledgment defaults; a host that fails, sends an empty STEAK or acknowledges a topic it wasn't sent acknowledges nothing; hosts named in acknowledgmentByHost keep their places. broadcastDetailed returns an OverlayBroadcastReport per host.
  • Lookup: each SLAP tracker gets an equal share of host places, taken in turn (TS LookupHostQueue); caller hosts keep their places; outputs a host can't decode are skipped, not fatal. resolveDetailed returns LookupResolution with host failures, tracker failures, rejected outputs and skipped host count.
  • Discovered private and loopback hosts are dropped by default (OverlayDiscoveredHostPolicy, .anyHTTPSOrigin turns it off). Hosts are parsed like WHATWG URL (checked against Node 22 on 21,147 URIs). A domain that resolves to a private address is not checked (documented in COMP-061).
  • Aggregated Atomic BEEF building stops at the answer limit inside the loop (TS LookupResolver.ts:927-938), so a 1 MB answer can no longer allocate gigabytes.
  • New public: OutputListItem.transaction(beefLimits:), LookupResolution, OverlayHostFailure, LookupOutputRejection, OverlayDiscoveredHostPolicy, broadcastDetailed, OverlayBroadcastReport; new case OverlayPolicyError.invalidSteak; OverlayHTTPConfiguration.maximumContextByteCount deprecated.
  • Failures are logged with full detail via OverlayLog and NetworkLog (hosts logged as scheme/host/port only); errors have plain-word descriptions.

Testing

swift test in swift:6.1 (Linux) after merging main: 968 tests passed, XCTest 155 passed, 0 failures; only live and opt-in tests skipped. Reviewed against TS 4.1.1 in three rounds with probes (memory, URI parsing, malformed JSON).

Live SHIP/SLAP hosts run @bsv/sdk 4.1.1 and Overlay Express. Align the
lookup, broadcast and advertisement rules with them.

Lookup:
- Aggregated octet-stream answers give each output Atomic BEEF for its
  own transaction, with its txid and context. Before, every output got
  the shared BEEF, so readers saw one transaction for all outpoints.
- JSON answers accept beef as a byte array, plus optional context and
  txid, and ignore unknown keys.
- Any 2xx status is success; non-octet-stream bodies parse as JSON.
- Output lists win over freeform answers. All hosts failing still
  throws noSuccessfulResponses.
- Per-output context may be up to 1 MiB.

Broadcast:
- Read camelCase STEAK keys as well as Go's PascalCase.
- Send X-Topics comma-separated.
- Send off-chain values with x-includes-off-chain-values and a
  length-prefixed body instead of dropping them.
- Send each host only the topics it advertised.
- Accept host origins with one trailing slash.

Advertisements:
- Require the locking key to be the identity's BRC-42 SHIP/SLAP child,
  and verify the signature under it (new unlinkedLockingKey error).
- Apply TS URI, field-size, identity-key and DER rules.
- Discovery requires one-satoshi outputs and normalizes advertised
  origins.
- Topic and service names follow the TS grammar.

Rulings COMP-053, 058 and 061 record the changes.
Broadcast:
- Follow @bsv/sdk SHIPBroadcaster acknowledgment rules. By default some
  host must acknowledge every topic and other hosts may fail, so one bogus
  advertisement no longer fails every broadcast.
- Report each host's acknowledged topics and failures through
  broadcastDetailed. An empty STEAK is a failed host.
- Hosts past the host limit are left out instead of throwing.
- Acknowledgment keys must be https origins.

Lookup:
- Leave out an output whose transaction cannot be read, as TS does, and
  report it, with failed hosts, through resolveDetailed.
- Make OutputListItem.transaction(beefLimits:) public.
- Normalize caller-supplied https hosts so they dedupe with discovered
  ones.
- Skip discovered hosts on loopback, private or reserved addresses by
  default, matching createPublicNetworkFetch.

Advertisement URIs:
- Parse IPv4 and IPv6 hosts as WHATWG URL does: reject invalid
  addresses and write numeric aliases in canonical form.

Wire:
- Only a missing ancestor falls back to the shared BEEF.
- Atomic BEEF keeps Merkle paths in TS order and follows a record's own
  path only.
- Read JSON output lists straight from the bytes, so byte arrays do not
  balloon through JSONSerialization.
- Bound freeform result text.
- Read an empty instruction object as no instructions.
- Deprecate OverlayHTTPConfiguration.maximumContextByteCount in favor of
  OverlayLimits.maximumLookupContextByteCount.

Tests add the TypeScript 2-BUMP Atomic BEEF vector, WHATWG host and
public-address tables from Bun, and the new resolver and broadcaster
behavior.
Keep the @bsv/sdk 4.1.1 overlay status and content-type rules alongside
main's NetworkLog logging and rejection messages. Add plain-word
descriptions for the overlay value, advertisement and policy errors,
and log hosts and outputs the resolver and broadcaster leave out.
SLAP discovery threw limitExceeded once trackers listed more hosts than
the limit, so a flood of one-satoshi advertisements could stop every
lookup for a service. Keep caller hosts and fill the rest of the limit
in tracker order, as LookupHostQueue does in @bsv/sdk 4.1.1, and log
how many hosts were left out by the resolver and the broadcaster.
Each distinct transaction in an aggregated answer gets its own Atomic
BEEF, and for a chain of unproven transactions those grow with the
chain, so a 1 MB answer could build hundreds of megabytes before the
answer limit was checked. Count generated and extracted bytes as they
are built and stop at the answer limit, as extractAtomicOutputs does in
@bsv/sdk 4.1.1, and hash each record once instead of once per output.
Log the BEEF error before it is mapped to an overlay error.
Lookup discovery now gives each SLAP tracker an equal share of the host
limit and takes hosts from the trackers in turn, as @bsv/sdk 4.1.1 does,
so one tracker or one flood of advertisements cannot fill every place.
The broadcaster keeps places for hosts named in acknowledgment
requirements, and a named host that discovery did not return fails its
requirement. LookupResolution and OverlayBroadcastReport count skipped
hosts, and failed trackers are logged and reported.

Refuse discovered domains with empty labels or only digits and dots,
log hosts as scheme, host and port only, log why an advertisement or
BEEF could not be used, treat an empty STEAK instruction object as a
failed host, and report an oversized JSON answer as a resource limit.
Document that discovered domains are not resolved before use.
@rohenaz
rohenaz merged commit fab8998 into main Nov 16, 2026
4 of 8 checks passed
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.

1 participant