A borg host is an ordinary overlay host that loads the borg module: one
file, host/bundle/borg-module.js, which mounts the topic manager
tm_borg_<name>_<suffix> for each hall it carries and the lookup service
ls_borg for all of them (spec.md sections 10 and 11). The
module imports nothing but @bsv/sdk (the host's own copy) and Node's
built-ins; bcommon is inlined.
$ (cd host && npm ci) # once
$ make module # writes host/bundle/borg-module.jsCopy the file into a directory beside the host's node_modules (for
example <host>/modules/borg/borg-module.js) and name it by absolute path:
OVERLAY_TOPICS=tm_borg_example_hall_vkqzmtrwxp
OVERLAY_MODULES=/srv/overlay/modules/borg/borg-module.js
BORG_HALLS=example_hall_vkqzmtrwxp
BORG_STATE_DIR=/var/lib/borg
BORG_HEADERS_URL=https://headers.example.comOr run the image ghcr.io/lightwebinc/borg-host (built by
make docker-build-host REFERENCE_HOST_IMAGE=<reference overlay host image>): the reference host with the module at
/app/modules/borg/borg-module.js, its state directory at /var/lib/borg
(mount a volume there), and an entrypoint that requires BORG_HALLS and
derives OVERLAY_TOPICS from it when unset.
deploy/quickstart/compose.yaml runs two of them. The reference host
requires OVERLAY_ADMIN_TOKEN (the bearer token of its /admin routes);
when it is unset the entrypoint sets a random one for that run and logs
so, which leaves /admin unusable until you set your own.
The host must run with no broadcaster, and must not enable an unordered
synchronization of unspent outputs for tm_borg_ topics (spec sections
11.1 and 11.6). The reference host has neither unless configured.
| Variable | Default | Meaning |
|---|---|---|
BORG_HALLS |
required | the hall identifiers this host carries, a comma list. Every tm_borg_ topic in OVERLAY_TOPICS must be one of them, or the module refuses to start: a borg topic left to the host's default manager would admit anything |
BORG_STATE_DIR |
required | an absolute directory kept for the host's life (below) |
BORG_HEADERS_URL |
OVERLAY_CHAIN_TRACKER_URL when it is an http or https URL |
the header source admission checks proofs against, in the reference host's native shape (/v1/root/<height>, /v1/tip). Required when the host's own source is another kind |
BORG_RETENTION_DAYS |
31 | how long a host keeps what it no longer answers (below). At least 31 |
BORG_MAX_BEEF |
262144 | the host's BEEF bound, in bytes. At least 262144 |
BORG_MAX_SWEEP_INPUTS |
1024 | the most inputs of a sweep the host accepts. At least 1024 |
BORG_SPEND_TABLE |
65536 | the spends of outpoints no carrier used that the host keeps (below). At least 65536 |
BORG_LISTEN |
none | host:port of the terms route (below) |
BORG_PRICES |
none | the classes this host prices, for example history=5,history-after=5. Only the priceable classes; a price of 0 charges nothing. A class and its -after form are priced together or not at all |
BORG_PAYEE_KEY |
none | the payee's private key, 64 lowercase hex characters: the terms route's BRC-104 identity and the key payments are derived from |
BORG_SESSIONS |
10000 | the most BRC-104 sessions the terms route keeps. Past it the least recently used is forgotten |
BORG_SESSION_TTL |
600 | seconds a BRC-104 session is kept once idle |
BORG_HANDSHAKES_PER_SEC |
4 | BRC-104 handshakes a second the terms route answers; decimals allowed |
BORG_HANDSHAKE_BURST |
8 | the most handshakes the terms route answers at once |
BORG_HANDSHAKES_PER_ADDR_PER_SEC |
1 | handshakes a second one remote address is answered; decimals allowed |
BORG_HANDSHAKE_ADDR_BURST |
4 | the most handshakes one remote address is answered at once |
BORG_RESPONSES_PER_SEC |
5 | signed responses a second one BRC-104 session is given; decimals allowed |
BORG_RESPONSE_BURST |
20 | the most signed responses one session is given at once |
BORG_ARCADE_URL |
none | arcade, which the host broadcasts each payment through before it answers (POST /tx, GET /tx/<txid>) |
BORG_ASSET_URL |
none | the node's asset service: the spend view of a payment's inputs and the proof of a held or watched payment. Without arcade and a spend view (this, or BORG_CHAIN) every payment is held until it mines |
BORG_CHAIN |
none | woc:main or woc:test: WhatsOnChain as that spend view and proof source, for a host with no node; refused beside BORG_ASSET_URL |
BORG_WOC_KEY |
none | a WhatsOnChain API key for BORG_CHAIN |
BORG_ACCEPT_THRESHOLD_SATS |
25000000 | the largest payment answered on the network's acceptance; above it a payment is held until it mines |
BORG_ACCEPT_PAYER_LIMIT |
the threshold | satoshis one payer may have answered fast and not yet mined; past it, held |
BORG_ACCEPT_TOTAL_LIMIT |
ten thresholds | the same across every payer |
BORG_ACCEPT_WINDOW |
1h |
how long a fast payment counts against the limits unless it mines first (ms, s, m, h) |
BORG_ACCEPT_WATCH |
0s |
how long the host keeps watching for a conflict after arcade's acceptance before it answers |
The host spells its header source BORG_HEADERS_URL; the borg command
spells its own BORG_HEADER_URL (the header_url key,
configuration.md).
The generic terms route, budget, acceptance and state directory variables
(BORG_SESSIONS through BORG_ACCEPT_WATCH) behave as
the host module describes for <APP>_.
A price needs BORG_LISTEN and BORG_PAYEE_KEY. history and
history-after sell one walk, so a host that prices one and not the other
(history=5 alone) is refused: the free form would answer every page but
the first for nothing. Any mistake stops the host before its port opens.
The engine checks a token's own proof before the topic manager runs, but
not its parent's, and the reference host hands a module no chain tracker.
So the module reads headers itself, from the same kind of source the host
reads, and reuses a confirmed root for ten minutes. A source that does not
answer refuses a token or sweep unmined; the refusal is raised, so the
engine records nothing and the same object is decided again when it is
offered again.
Every refusal is raised (spec section 11.1), so the engine stores nothing for it and a later submission of the same txid is decided again: a copy offered first in a worse BEEF cannot suppress the genuine object, and a token refused while the host's headers lagged is admitted once they catch up. Admission never reads another topic or anything the topic holds.
ls_borg keeps its index in memory and rebuilds it on start from the
engine's storage and the state directory (below), whatever order storage
returns rows in. Tokens the next token spent are not among the unspent rows
the host hands a module; the module reads them back from the rows it keeps,
and failing that walks back from each held token through the input that
spends its predecessor, reading each as the engine stores it (the token
alone with its proof). An admission that arrives while the index is being
rebuilt waits for the rebuild, and is then decided as any other.
At most 8 tokens are kept at one seq of a chain: those that are not
pending first, then the lowest txids. The set is a function of what the
host holds, never of arrival order, and a token on a predecessor no host
has cannot take the place of one on the chain. A pending token turned
away from a full seq stays in the engine's storage; once the predecessor
it names is decided the module reads it back and it takes its place.
A carrier is retracted, superseded or held, and a token pending,
consistent, roll-mismatch, policy-mismatch, members-mismatch,
commitment-reuse or rejoin-unrotated (spec section 11.2). A status never refuses anything; it
decides what is answered:
- a retracted or superseded carrier is not answered, except that
rollandmemberanswer every grant the roll holds, andpolicy,roleandboundevery policy record the policy set holds, whatever its status, so that a reader computing a root sees each and checks it; roll,member,member-of,policy,roleandbounduse the fold of a chain's highest token that is notpending, the first mined among several, so a token that arrived before its predecessor never hides the last complete state;org,groupand their-seqclasses answer every token held at theseqasked, whatever its status, and a reader decides.
No answer carries a token's status: a reader folds the chain itself
(spec section 13). The host reports what it found in its metrics and its
log: borg_token_status_total{status} counts each token's status once it is
decided, borg_grants and borg_policies are the carriers of each kind the
host holds, borg_tokens_pending is the tokens still waiting for a
predecessor, and every status other than consistent is logged with its
hall, organization, chain, seq and txid.
A sweep's spend of an outpoint some carrier spent joins that outpoint's row
and is kept for the host's life. A spend of any other outpoint waits in a
table of at most BORG_SPEND_TABLE outpoints, oldest dropped first: a
carrier that arrives later on a waiting outpoint is retracted at once and
the spend joins its row. A sweep whose every spend has left the table is
dropped and deleted from storage, so a chain of mined sweeps of a
stranger's own outputs costs the host the table and nothing more. The
table is rebuilt on start from the sweeps storage holds. A held sweep whose
tombstone an admitted transaction then spends is no longer among the
unspent rows storage hands a restart, so the state directory names it and
the restart reads it back: a spend that waits in the table still retracts
a carrier that arrives after a restart.
One mined sweep may be published in several halls. It is indexed in each hall that admitted it, and dropping it from one topic leaves it answering in the others.
Kept for the host's life: tokens, outpoint rows and the sweeps they record.
A grant is kept while the roll the host answers holds it; one that left
the roll, or was swept, is kept BORG_RETENTION_DAYS after the later of
the two, and one never admitted to a roll is kept until it is swept. The
wraps of a group's current epoch are kept; those of an earlier epoch for
BORG_RETENTION_DAYS after the host first saw the token that ended it.
Up to 8 superseded wraps and 64 superseded grants are kept on one outpoint
as evidence, and the rest are dropped at once. Retention runs hourly.
What the index drops is deleted from the engine's storage at once: after
the admission that dropped it, and after each retention run. A storage
with no deleteOutput keeps what was dropped; the module says so in the
log once and keeps no list of it.
borg-rows.jsonlholds what the host keeps for its life beside the engine's storage: every carrier any hall admitted with the funding output it spent, every drop, every spend of such an outpoint by an admitted sweep, when the host first saw each token, and each held sweep whose tombstone was spent. It keeps a funding output from answering a second carrier and keeps retention's clocks, so it is kept for the host's life, backed up with the database, and never edited. Each line is flushed to disk before the admission it records completes.payments.jsonlandunsettleable.jsonlare the generic payee ledger and watch files of the host module, which also covers settling, the backup script and restoring.
borg-rows.jsonl is append-only like them, so a copy taken while the host
runs is safe. Restore it only when it is lost or damaged, never to roll the
host back: rows written after the copy are gone.
The module holds a carrier line in memory for each carrier an outpoint's
row keeps: its winner and the superseded ones within the kind's evidence
bound (64 grants or policy records, 8 wraps). A dropped carrier keeps no
line, so a row's memory does not grow with equivocation: an outpoint
carries at most 65 lines however many carriers are offered on it.
borg_carrier_lines counts the lines held and
borg_carrier_lines_forgotten those the rows stopped keeping. At about
1 KB a line, a host's carrier memory is bounded by its outpoints rather
than by its admissions.
The journal is bounded with it, but only partly, and the difference is
worth knowing. A carrier already past its row's bound when it arrives is
refused before anything is written, so it costs no line at all. A
carrier that was within the bound and is pushed out later by a lower
commitment keeps the line it already has: the journal is append-only and
is not compacted, so a restart replays that line, rebuilds the row and
applies the bound again (borg_carrier_lines_forgotten rises again from
zero). An organization that offers its carriers lowest commitment first
therefore costs the journal nothing past the bound, and one that offers
them highest first costs a line each. Bounding that case needs journal
compaction, which this version does not do.
Tokens cost a line each as well. Give Node the heap for the outpoints the
host expects (--max-old-space-size), and watch the gauges.
A state directory written by an earlier build loads unchanged: the lines it
holds are the lines this build writes, and a payments.jsonl line without
its outpoints has them read from its transaction. The reverse does not
hold once a spent sweep has been named: an earlier build refuses a journal
with a line it does not know ("k":"w") and does not start. To run an
earlier build again, remove those lines from a copy of borg-rows.jsonl
first.
With BORG_LISTEN set the module serves the terms route, its session,
handshake and response budgets, replay refusal and payment acceptance
exactly as the host module describes, with <app> = borg.
What is borg's own:
- the terms document is spec section 10.3, at
GET /ls_borg/terms, and the priceable classes arehistoryandhistory-after; POST /lookupanswers the same BRC-24 questions as the host's/lookup, amemberanswer's context included;- a payer's next payment may spend the change of its last, which is another coin;
- a held payment is answered 202
ERR_PAYMENT_HELD, and a response over the session budget makesborg historywait asRetry-Aftersays (1 to 30 seconds) and ask once more; borg payee settlereads the ledger'sdecisionandreason.
The same module exists in Go, module/ in this repository, written to
bcommon's engine-neutral contract (github.com/lightwebinc/bcommon/module)
for a Go overlay host that carries the adapter: tm_borg_<name>_<suffix>
for each hall BORG_HALLS names and ls_borg for all of them, with the
same admissions and refusals (the rules are borgrec.Admit, and every
refusal is raised), the same folds and statuses, outpoint rows, spend
table, evidence bounds, retention, restore and question classes (a
member, role or bound answer carries its inclusion path as context),
the same terms route (bcommon's Go termsroute: the same paths, status
codes, budgets, metric names and ledger lines as the route above) and the
same metrics. It reads the same variables and refuses the same mistakes,
and has one of its own:
| Variable | Default | Meaning |
|---|---|---|
BORG_JOURNAL_SYNC_MS |
100 | the rows journal's group commit window: lines are written to borg-rows.jsonl at once and fsynced together this often, off the admission's path. 0 fsyncs each line before the admission completes, as the module above does. At most 60000 |
The state directory is the same: borg-rows.jsonl holds the same lines,
byte for byte, so a host can move between the two modules with its
journal, and payments.jsonl has the same layout, which borg payee settle reads as before. With a window, a process that dies loses no line
(each is written before the admission completes) and a power loss loses at
most the last window's, as a store that syncs its write-ahead log on a
timer does.
On the Go host the topic manager reads the submission's bytes exactly as
they arrived (module.WithSubmission), as the module above reads the
submitted BEEF: the beef rule counts what the wire declares. ls_borg
answers the host's /lookup with outputs it hydrates from the host's
storage, each with its context, and the terms route hydrates its answers
the same way. A dropped carrier's output is deleted through DeleteOutput
on the storage a restore is handed; the host keeps its record that the
transaction was applied. The host logs borg module mounted with the
topics, the header source, the terms route, the priced classes, the
budgets, the payment acceptance, the journal's window, whether a chain
source is configured for replay, and the build.
Ordered replay. The Go module's lookup service is also a replayer
(bcommon module.Replayer): a Go host that missed part of a hall is given
it by replay from a sibling host (spec section 11.6), never by an
unordered synchronization of unspent outputs. For each organization the
host knows in the hall (it holds a token or a carrier of it), a walk asks
the sibling's ls_borg free classes, in this order: the organization's
groups (groups); the tokens of the organization's chain and of each
group's chain at each seq the host lacks (org, org-seq, group,
group-seq), each submitted in the BEEF of the token and its parent,
parents first, and held to section 11.1 before it is submitted, a chain's
first token with its chain tree read with its proof from the chain source
(BORG_ASSET_URL, or WhatsOnChain with BORG_CHAIN); then the roll's
grants (roll), the policy set's records (policy), the held wraps of
each group's current epoch and of an earlier epoch whose ending token the
host first saw within the retention floor less a day (wraps), and the
sweeps of each held carrier's funding outpoint (sweep). Everything is
admitted through the host's ordinary path, so every rule of section 11.1
holds. What no free class leads to is not replayed: an organization the
host knows nothing of, a grant that left the roll, a retired policy
record, a superseded or retracted wrap, a sweep of an outpoint no held
carrier spends, and a chain's first token once the chain source no longer
serves its chain tree with a proof. The organization's fill repairs
those.
The differences from the module above, each deliberate: the journal's sync window; a refused question's words (both refuse the same questions; the texts differ in detail); a restore that runs a second time in one process clears the policy index too; and replay, which the module above does not do.
Its tests drive it with the transaction, refusal and query vectors and this module's own cases, ported from the module above case for case, through a stand-in for an engine's admission path and a restart, the terms route over HTTP with go-sdk's BRC-104 client paying its 402, and replay between two hosts.
| Metric | What |
|---|---|
borg_admitted_total{kind}, borg_admitted_repeats_total{kind} |
transactions admitted (grant, policy, wrap, org-token, group-token, sweep), each once; a txid the engine showed the manager again |
borg_refused_total{reason} |
refusals, by the reason labels of spec sections 3.1 and 11.1 |
borg_token_status_total{status} |
tokens by the status the fold gave them |
borg_retractions_total |
spends of a carrier's funding outpoint |
borg_dropped_total{why} |
carriers dropped as evidence or by retention, and sweeps dropped from the table |
borg_lookups_total{class} |
questions answered |
borg_halls, borg_tokens, borg_tokens_pending, borg_grants, borg_policies, borg_wraps, borg_sweeps, borg_outpoint_rows, borg_spend_table |
what the index holds |
borg_carrier_lines |
carrier lines held in memory: each outpoint's winner and the superseded carriers within its evidence bound |
borg_carrier_lines_forgotten |
carrier lines the rows stopped keeping, because the carrier was dropped |
borg_sessions, borg_sessions_evicted_total{why}, borg_handshakes_total{result} |
the terms route |
borg_payments_total{decision,reason} |
paid questions by what the host did: requested (no-payment, a 402 with the price); fast (at-or-below-threshold, mined); hold (above-threshold, payer-limit, total-limit, payer-flagged, no-broadcast-leg, broadcast-unconfirmed, no-network-verdict, double-spend-attempted, spend-view-unknown, already-charged, payer-asked-hold, price-unknown, price-stale, no-txid, watch-limit); refuse (pays-nothing, malformed, wrong-script, spv-failed, not-final, outputs-exceed-inputs, network-refused, double-spent, replayed, conflict) |
borg_payment_events_total{kind} |
fast payments the watch resolved: confirmed, double-spent, refused, unmined (all but the first flag the payer) |
borg_payments_unmined |
fast payments being watched |
borg_requests_total{result} |
authenticated requests refused before any signature: replayed, or limited by the session's budget |
borg_journal_lines_total, borg_journal_wait_seconds_total |
the Go module: journal lines written, and the time admissions spent writing them |
borg_journal_syncs_total{result}, borg_journal_sync_seconds_total |
the Go module: the journal's timed fsyncs (ok, error), and their time |
make e2e REFERENCE_HOST=/path/to/reference-host runs the bundle on a
local reference host (its compiled dist/index.js and its node_modules)
with a throwaway MySQL in Docker and a local chain: it creates an
organization, grants three members, puts them in a group, opens the epoch
key as each from the host's answers, removes one (who cannot open the next
epoch), rotates, revokes by the roll and by a sweep, replays a suppression
attempt, pays a priced question's 402, restarts the host and asks
everything again of the restored index.