Liking cljdoc? Tell your friends :D

skuld.ledger.account

Balances, nonces, and actually charging for a transaction.

Everything before this namespace computed a price and charged nobody.

The charge is tx-data, not a side effect

settle returns the transaction to APPLY: the sender's own tx-data, plus the ownership stamps, plus the datoms that debit the sender, advance the nonce, and record the fee. One transaction.

Splitting them would be a divergence bug waiting for a crash: a node that applied the effects and died before the debit would carry a different balance than its peers forever, and the app-hash comparison would find it one block later with no way to say which half was right. Making the fee part of the same transaction means there is no window.

It also means a REFUSED transaction still produces a transaction: the fee and the nonce bump, without the sender's effects. A refusal that wrote nothing would leave the nonce unspent, and a transaction that can be retried for free after being refused is a free retry loop.

Nonces are sequential, not merely increasing

n+1 exactly. Monotonic-but-gappy is enough to stop a replay and is simpler to implement, but sequential buys two things worth the strictness: a mempool can order an account's pending transactions without guessing, and an account cannot burn its own nonce space by jumping to a large value.

The nonce lives on the account entity, so it advances in the same transaction as the fee -- there is no separate replay table to keep consistent with the balance.

An account that cannot pay is refused and charged nothing

There is nothing to take. This deliberately leaves cheap spam from empty accounts to the layer that can actually stop it: the mempool drops a transaction whose sender cannot cover the base fee, before it reaches consensus. Doing it here would mean pretending to charge an account with a zero balance, which is bookkeeping theatre.

The cost an attacker still imposes is one signature verification at the network edge. That is the cheapest check we have, and it is why urd.consensus.auth refuses an unknown author before any crypto runs.

Balances, nonces, and actually charging for a transaction.

Everything before this namespace computed a price and charged nobody.

## The charge is tx-data, not a side effect

`settle` returns the transaction to APPLY: the sender's own tx-data, plus
the ownership stamps, plus the datoms that debit the sender, advance the
nonce, and record the fee. One transaction.

Splitting them would be a divergence bug waiting for a crash: a node that
applied the effects and died before the debit would carry a different
balance than its peers forever, and the app-hash comparison would find it
one block later with no way to say which half was right. Making the fee part
of the same transaction means there is no window.

It also means a REFUSED transaction still produces a transaction: the fee
and the nonce bump, without the sender's effects. A refusal that wrote
nothing would leave the nonce unspent, and a transaction that can be retried
for free after being refused is a free retry loop.

## Nonces are sequential, not merely increasing

`n+1` exactly. Monotonic-but-gappy is enough to stop a replay and is simpler
to implement, but sequential buys two things worth the strictness: a
mempool can order an account's pending transactions without guessing, and an
account cannot burn its own nonce space by jumping to a large value.

The nonce lives on the account entity, so it advances in the same
transaction as the fee -- there is no separate replay table to keep
consistent with the balance.

## An account that cannot pay is refused and charged nothing

There is nothing to take. This deliberately leaves cheap spam from empty
accounts to the layer that can actually stop it: the mempool drops a
transaction whose sender cannot cover the base fee, before it reaches
consensus. Doing it here would mean pretending to charge an account with a
zero balance, which is bookkeeping theatre.

The cost an attacker still imposes is one signature verification at the
network edge. That is the cheapest check we have, and it is why
`urd.consensus.auth` refuses an unknown author before any crypto runs.
raw docstring

accountclj

(account db hex)

{:eid :balance :nonce} for hex, or nil when the key has no account.

No account means no balance means cannot transact. Genesis credits the initial set; everyone else is credited by someone who already has funds.

`{:eid :balance :nonce}` for `hex`, or nil when the key has no account.

No account means no balance means cannot transact. Genesis credits the
initial set; everyone else is credited by someone who already has funds.
sourceraw docstring

charge-tx-dataclj

(charge-tx-data _db {:keys [from-eid from-balance from-nonce fee]})

Debit from, advance from's nonce, and record the fee on the transaction.

Computed from the database value rather than expressed as an increment, because datahike has no atomic add and every validator must produce the same datoms from the same db. meter applies transactions in order against the previous one's result, so two charges to one account in a block see each other.

Why nothing is credited here

The obvious implementation credits the fee recipient in the same transaction. It is wrong, and not for an accounting reason.

A transaction that writes the validator's balance writes SHARED state. Every transaction would then touch a row every other transaction touches, so no transaction is ever single-writer: transactions could not be scheduled in parallel by disjoint write sets, and the consensus fast path -- where a transaction touching only its sender's own rows needs witnessing rather than ordering -- would be unreachable. The accounting would have destroyed the concurrency, not the workload.

So the debit lands on the sender (their own row) and the fee is RECORDED on the transaction entity. Distribution is a separate, periodic operation over the recorded fees -- which is what Sui does at epoch end and what Cosmos does with its fee pool. Neither credits a validator per transaction.

Debit `from`, advance `from`'s nonce, and record the fee on the transaction.

Computed from the database value rather than expressed as an increment,
because datahike has no atomic add and every validator must produce the same
datoms from the same db. `meter` applies transactions in order against the
previous one's result, so two charges to one account in a block see each
other.

## Why nothing is credited here

The obvious implementation credits the fee recipient in the same
transaction. It is wrong, and not for an accounting reason.

A transaction that writes the validator's balance writes SHARED state. Every
transaction would then touch a row every other transaction touches, so no
transaction is ever single-writer: transactions could not be scheduled in
parallel by disjoint write sets, and the consensus fast path -- where a
transaction touching only its sender's own rows needs witnessing rather than
ordering -- would be unreachable. The accounting would have destroyed the
concurrency, not the workload.

So the debit lands on the sender (their own row) and the fee is RECORDED on
the transaction entity. Distribution is a separate, periodic operation over
the recorded fees -- which is what Sui does at epoch end and what Cosmos
does with its fee pool. Neither credits a validator per transaction.
sourceraw docstring

collected-feesclj

(collected-fees db since-tx)

Total recorded by transactions since since-tx (exclusive), and the transactions counted.

Distribution reads this rather than a running counter, because a counter is a row every transaction would have to write -- the contention this design exists to avoid.

Why this walks datoms instead of querying

A fee datom only counts if it sits on ITS OWN transaction entity, i.e. the datom's entity equals the datom's tx. Datalog cannot express that constraint conveniently, and the naive query -- match any :urd/fee and sum it -- counted a fee planted on any row at all. A sender who owned a row, or who chose an unallocated id above the transaction watermark, minted money at distribution: measured, 999,999,999 credited for a fee of 1,375.

skuld.ledger.ownership/protocol-attributes is the primary defence and now refuses :urd/fee anywhere but a transaction entity. This is the second one, and it is the cheaper of the two to be sure of.

Total recorded by transactions since `since-tx` (exclusive), and the
transactions counted.

Distribution reads this rather than a running counter, because a counter is
a row every transaction would have to write -- the contention this design
exists to avoid.

## Why this walks datoms instead of querying

A fee datom only counts if it sits on ITS OWN transaction entity, i.e. the
datom's entity equals the datom's tx. Datalog cannot express that constraint
conveniently, and the naive query -- match any `:urd/fee` and sum it --
counted a fee planted on any row at all. A sender who owned a row, or who
chose an unallocated id above the transaction watermark, minted money at
distribution: measured, 999,999,999 credited for a fee of 1,375.

`skuld.ledger.ownership/protocol-attributes` is the primary defence and now
refuses `:urd/fee` anywhere but a transaction entity. This is the second
one, and it is the cheaper of the two to be sure of.
sourceraw docstring

credit-tx-dataclj

(credit-tx-data db hex amount)
(credit-tx-data db hex amount genesis)

Give hex amount. Upserts on the unique key, so it also creates the account -- which is how genesis funds the initial holders.

The 3-arity registers the account's genesis record at the same time. That pairing is deliberate: an account with a balance but no genesis can be funded and can never transact, because nothing can authorise a key for it.

Give `hex` `amount`. Upserts on the unique key, so it also creates the
account -- which is how genesis funds the initial holders.

The 3-arity registers the account's genesis record at the same time. That
pairing is deliberate: an account with a balance but no genesis can be
funded and can never transact, because nothing can authorise a key for it.
sourceraw docstring

distribute-tx-dataclj

(distribute-tx-data db total validators)

Split total among validators in proportion to their power.

validators is {hex power}. Returns tx-data crediting each.

Integer division, and the remainder is deliberately left UNDISTRIBUTED rather than given to anyone: handing the dust to the first validator by iteration order would make the reward depend on map ordering, which differs between nodes and is exactly the kind of divergence an app-hash comparison would find a block later with no way to attribute it.

Split `total` among `validators` in proportion to their power.

`validators` is `{hex power}`. Returns tx-data crediting each.

Integer division, and the remainder is deliberately left UNDISTRIBUTED
rather than given to anyone: handing the dust to the first validator by
iteration order would make the reward depend on map ordering, which differs
between nodes and is exactly the kind of divergence an app-hash comparison
would find a block later with no way to attribute it.
sourceraw docstring

fee-attrclj

Where a transaction records what it paid.

On the TRANSACTION entity, which is unique per transaction, so recording a fee contends with nothing.

Where a transaction records what it paid.

On the TRANSACTION entity, which is unique per transaction, so recording a
fee contends with nothing.
sourceraw docstring

key-attrclj

A key's hex. :db.unique/identity, so the account entity is the key entity -- the same one skuld.ledger.ownership points :urd/owner at.

One entity per key, carrying its balance, its nonce, and the rows it owns. Two entities would let a balance and an ownership record disagree about who a key is.

A key's hex. `:db.unique/identity`, so the account entity is the key
entity -- the same one `skuld.ledger.ownership` points `:urd/owner` at.

One entity per key, carrying its balance, its nonce, and the rows it owns.
Two entities would let a balance and an ownership record disagree about who
a key is.
sourceraw docstring

refusal-settlementclj

(refusal-settlement db {:keys [sender nonce]} schedule admission)

Preserve already-consumed guard fuel when a later admission stage refuses.

Preserve already-consumed guard fuel when a later admission stage refuses.
sourceraw docstring

schemaclj

Installed by genesis. Kontor runs :schema-flexibility :write, so none of this can be implicit.

Installed by genesis. Kontor runs `:schema-flexibility :write`, so none of
this can be implicit.
sourceraw docstring

settleclj

(settle db
        {:keys [sender nonce tx-data fuel] :or {fuel 0}}
        {:keys [schedule preparation-remaining]})

Price, authorise and charge a transaction. Returns what to apply.

{:ok?     was the sender's tx-data accepted
 :reason  why not
 :fee     charged either way
 :apply   the tx-data to transact -- effects + stamps + charge, or
          just the charge when refused
 :violations ...}

envelope is {:sender <hex> :nonce n :tx-data [...]}. The SIGNATURE is not checked here -- skuld.boundary/admit does that at the edge, before this is reached, for the same reason urd.consensus.auth verifies before the state machine sees anything.

Order: nonce, then funds, then price, then scope. Each is cheaper than the next and each can refuse on its own.

The accounting datoms are NOT themselves priced -- charging for the charge is circular, since the fee would change the datoms that carry it. They are protocol overhead, covered by the base fee. The consequence is that the applied transaction contains a few more datoms than were paid for, which is a constant per transaction and not a lever anyone can pull.

Price, authorise and charge a transaction. Returns what to apply.

    {:ok?     was the sender's tx-data accepted
     :reason  why not
     :fee     charged either way
     :apply   the tx-data to transact -- effects + stamps + charge, or
              just the charge when refused
     :violations ...}

`envelope` is `{:sender <hex> :nonce n :tx-data [...]}`. The SIGNATURE is
not checked here -- `skuld.boundary/admit` does that at the edge, before this
is reached, for the same reason `urd.consensus.auth` verifies before the
state machine sees anything.

Order: nonce, then funds, then price, then scope. Each is cheaper than the
next and each can refuse on its own.

The accounting datoms are NOT themselves priced -- charging for the charge
is circular, since the fee would change the datoms that carry it. They are
protocol overhead, covered by the base fee. The consequence is that the
applied transaction contains a few more datoms than were paid for, which is
a constant per transaction and not a lever anyone can pull.
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close