A ledger: urd's committed prefix, applied to a datahike database.
This is the consumer the effect protocol was designed against, and it is worth saying what it does NOT need. There is no reconciliation layer, no undo log, no shadow copy of the committed sequence. Three effects map onto three datahike calls:
commit -> transact! extend the prefix
revert -> force-branch! move the branch pointer back
resync -> force-branch! state the prefix wholesale, after a restart
node/revert's docstring claims reverting is cheap on a copy-on-write
substrate because the blocks are immutable and still in the store, so a
rollback is a pointer move. datahike is that substrate: force-branch!
writes a new head record naming an older db value and every index node it
refers to is still there. There is no per-datom undo, and nothing is
rewritten in place.
This is the half of the design that Bitcoin pays for and Bullshark does not. btcd's two-phase reorg machinery exists because its UTXO set is a destructively mutated map; at the header layer, where every quantity is a fold over the ancestor path, its undo log is empty. Here the whole database is at the header layer.
A ledger is handed the same vertex more than once in normal operation: Raft
and SCP both announce their committed prefix on restart, a resync restates
it, and a catching-up node may re-learn a height it already had. So each
vertex carries its id in :tx-meta and a vertex already applied is skipped.
That also makes the mapping RECOVERABLE. :tx-meta becomes attributes on the
tx entity, so [?tx :urd/vertex ?id] is an ordinary query, and
branch-history walks the commit graph -- so after a process restart the
ledger rebuilds vertex -> commit-id from the database rather than from
memory it no longer has. A ledger that could only revert within one process
lifetime would be a ledger that cannot revert.
force-branch! warns that existing connections see stale state and must be
released and reconnected. A caller cannot be expected to know that a revert
invalidated the connection it is holding, so the ledger owns it: db hands
out a value, never the connection, and the revert path releases and
reconnects before returning.
A ledger: urd's committed prefix, applied to a datahike database.
This is the consumer the effect protocol was designed against, and it is
worth saying what it does NOT need. There is no reconciliation layer, no
undo log, no shadow copy of the committed sequence. Three effects map onto
three datahike calls:
commit -> transact! extend the prefix
revert -> force-branch! move the branch pointer back
resync -> force-branch! state the prefix wholesale, after a restart
## Why the revert is cheap
`node/revert`'s docstring claims reverting is cheap on a copy-on-write
substrate because the blocks are immutable and still in the store, so a
rollback is a pointer move. datahike is that substrate: `force-branch!`
writes a new head record naming an older db value and every index node it
refers to is still there. There is no per-datom undo, and nothing is
rewritten in place.
This is the half of the design that Bitcoin pays for and Bullshark does not.
btcd's two-phase reorg machinery exists because its UTXO set is a
destructively mutated map; at the header layer, where every quantity is a
fold over the ancestor path, its undo log is empty. Here the whole database
is at the header layer.
## Idempotence, and why it is not optional
A ledger is handed the same vertex more than once in normal operation: Raft
and SCP both announce their committed prefix on restart, a `resync` restates
it, and a catching-up node may re-learn a height it already had. So each
vertex carries its id in `:tx-meta` and a vertex already applied is skipped.
That also makes the mapping RECOVERABLE. `:tx-meta` becomes attributes on the
tx entity, so `[?tx :urd/vertex ?id]` is an ordinary query, and
`branch-history` walks the commit graph -- so after a process restart the
ledger rebuilds vertex -> commit-id from the database rather than from
memory it no longer has. A ledger that could only revert within one process
lifetime would be a ledger that cannot revert.
## The connection is owned here
`force-branch!` warns that existing connections see stale state and must be
released and reconnected. A caller cannot be expected to know that a revert
invalidated the connection it is holding, so the ledger owns it: `db` hands
out a value, never the connection, and the revert path releases and
reconnects before returning.(app-hash ledger)This ledger's digest of the state it has applied, for urd.consensus.apphash.
d/commit-id, and that was the design note's assumptionThe plan said datahike supplies a digest for free with :crypto-hash? true,
because the commit id then becomes a merkle hash over the stored roots.
Measured, on two genuinely independent replicas -- distinct stores, released
between runs, identical content:
d/commit-id DIFFER
hash over [e a v] datoms MATCH
hash over [e a v tx] datoms MATCH
So :crypto-hash? makes the commit id tamper-evident WITHIN one database's
history, which is what datahike.audit/verify-chain walks. It does not make
it comparable ACROSS nodes: something per-database is in the commit that is
not the content. Two correct replicas would have accused each other of
divergence on every single value.
(Worth knowing how nearly this went the other way: sharing the store :id
across the two configs DID make the ids match, and it took reading the datoms
to see why -- both configs had resolved to the same database, so the second
replica's datoms contained the first's. A green result for the wrong reason,
and the check that caught it was reading the data rather than the digest.)
Hashing the datoms is O(size of the database), per proposal. That is fine for
a ledger of thousands of datoms and wrong for one of millions, so this is
honest rather than finished: the scalable version is a merkle root over the
index, which is exactly what :crypto-hash? computes internally and does not
expose separately from the commit id. Getting at it is a datahike-side
question, not a urd one, and the convention above this function does not
change when the answer arrives -- urd.consensus.apphash compares opaque
values and never learns which of these produced them.
This ledger's digest of the state it has applied, for `urd.consensus.apphash`.
## NOT `d/commit-id`, and that was the design note's assumption
The plan said datahike supplies a digest for free with `:crypto-hash? true`,
because the commit id then becomes a merkle hash over the stored roots.
Measured, on two genuinely independent replicas -- distinct stores, released
between runs, identical content:
d/commit-id DIFFER
hash over [e a v] datoms MATCH
hash over [e a v tx] datoms MATCH
So `:crypto-hash?` makes the commit id tamper-evident WITHIN one database's
history, which is what `datahike.audit/verify-chain` walks. It does not make
it comparable ACROSS nodes: something per-database is in the commit that is
not the content. Two correct replicas would have accused each other of
divergence on every single value.
(Worth knowing how nearly this went the other way: sharing the store `:id`
across the two configs DID make the ids match, and it took reading the datoms
to see why -- both configs had resolved to the same database, so the second
replica's datoms contained the first's. A green result for the wrong reason,
and the check that caught it was reading the data rather than the digest.)
## The cost of the digest that does work
Hashing the datoms is O(size of the database), per proposal. That is fine for
a ledger of thousands of datoms and wrong for one of millions, so this is
honest rather than finished: the scalable version is a merkle root over the
index, which is exactly what `:crypto-hash?` computes internally and does not
expose separately from the commit id. Getting at it is a datahike-side
question, not a urd one, and the convention above this function does not
change when the answer arrives -- `urd.consensus.apphash` compares opaque
values and never learns which of these produced them.(db {:keys [conn]})The current database VALUE. Never the connection: a revert replaces it.
The current database VALUE. Never the connection: a revert replaces it.
(handlers ledger)The three callbacks, shaped for runtime/start-runtime!.
The three callbacks, shaped for `runtime/start-runtime!`.
(marks {:keys [marks]})vertex-id -> commit-id, for assertions.
vertex-id -> commit-id, for assertions.
(on-finalize {:keys [conn marks tx-data-fn vertex-id-fn on-divergence]
:as ledger}
vertices)commit: extend the prefix.
Skips a vertex already applied. See the namespace docstring -- being handed the same vertex twice is ordinary, not exceptional.
`commit`: extend the prefix. Skips a vertex already applied. See the namespace docstring -- being handed the same vertex twice is ordinary, not exceptional.
(on-resync {:keys [conn marks branch] :as ledger} vertices)resync: the node is stating what its committed prefix now IS.
Not a revocation and not an extension, which is exactly why it is its own effect. The prefix is rebuilt from the base commit rather than diffed against what we hold: a restarting node does not know what we hold, and that is the whole reason it is telling us.
`resync`: the node is stating what its committed prefix now IS. Not a revocation and not an extension, which is exactly why it is its own effect. The prefix is rebuilt from the base commit rather than diffed against what we hold: a restarting node does not know what we hold, and that is the whole reason it is telling us.
(on-revert {:keys [conn marks branch vertex-id-fn base] :as ledger}
to-vertex
dropped)revert: move the branch pointer back to to-vertex, dropping dropped.
dropped is not merely informational here. The runtime has already checked
it against the tail it holds, and this checks it against what was actually
applied: a revert naming vertices this ledger never saw means the two
disagree about history, and continuing would move the branch somewhere
neither of them intended.
A to-vertex that was never applied is the genesis case -- Nakamoto's reorg
to the root -- and lands on the commit this ledger opened at.
`revert`: move the branch pointer back to `to-vertex`, dropping `dropped`. `dropped` is not merely informational here. The runtime has already checked it against the tail it holds, and this checks it against what was actually applied: a revert naming vertices this ledger never saw means the two disagree about history, and continuing would move the branch somewhere neither of them intended. A `to-vertex` that was never applied is the genesis case -- Nakamoto's reorg to the root -- and lands on the commit this ledger opened at.
(open cfg
{:keys [tx-data-fn vertex-id-fn branch on-divergence]
:or {tx-data-fn :tx-data vertex-id-fn :id branch :db}})Open a ledger on an existing datahike config.
on-revert calls force-branch!, which discards every transaction after the
target -- including transactions an application wrote itself. There is no way
to keep them: a reorg means consensus decided a different history, and a
transaction that was interleaved with the abandoned one has no defined place
in the new one.
So sharing a branch between this ledger and application writes is not
supported, and the failure is silent: the application's data disappears at
the next reorg, on the nodes that reorged, which is also when nobody is
looking at the application. An application with its own writes should use its
own BRANCH; :branch selects which one this ledger advances.
Options:
:tx-data-fn (fn [vertex]) -> tx-data. Default :tx-data.
:vertex-id-fn (fn [vertex]) -> id. Default :id.
:branch the branch to advance. Default :db.
:on-divergence (fn [{:keys [claimed ours value]}]) -- called when a DECIDED
value carries an app-hash that disagrees with ours. Defaults
to throwing; see on-finalize.
The database must already exist; creating one is the application's decision, not consensus's.
Open a ledger on an existing datahike config.
## The ledger OWNS its branch, exclusively
`on-revert` calls `force-branch!`, which discards every transaction after the
target -- including transactions an application wrote itself. There is no way
to keep them: a reorg means consensus decided a different history, and a
transaction that was interleaved with the abandoned one has no defined place
in the new one.
So sharing a branch between this ledger and application writes is not
supported, and the failure is silent: the application's data disappears at
the next reorg, on the nodes that reorged, which is also when nobody is
looking at the application. An application with its own writes should use its
own BRANCH; `:branch` selects which one this ledger advances.
Options:
:tx-data-fn (fn [vertex]) -> tx-data. Default `:tx-data`.
:vertex-id-fn (fn [vertex]) -> id. Default `:id`.
:branch the branch to advance. Default `:db`.
:on-divergence (fn [{:keys [claimed ours value]}]) -- called when a DECIDED
value carries an app-hash that disagrees with ours. Defaults
to throwing; see `on-finalize`.
The database must already exist; creating one is the application's decision,
not consensus's.Where a vertex's consensus id is recorded. Namespaced so a ledger sharing a database with an application cannot collide with it.
Where a vertex's consensus id is recorded. Namespaced so a ledger sharing a database with an application cannot collide with it.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |