Liking cljdoc? Tell your friends :D

skuld.ledger.storage

Who pays for state that persists, and how much.

An invariant, not an escrow

The obvious design is a deposit: lock tokens when data is written, refund them when it is deleted. It is what I prototyped, and it has a problem -- a refund is a payment, so a transaction that frees a lot of data has a NEGATIVE fee, and a transaction that pays its sender is a minting attack unless refunds are bounded by a per-owner deposit ledger.

NEAR does not have that problem because it does not have refunds. Storage is a POST-CONDITION on the account:

balance  >=  bytes-used x rate

There is no deposit account, no refund receipt, no release action. Freeing bytes lowers bytes-used, which lowers the requirement, which makes tokens spendable again -- automatically, exactly, in the same transaction. The whole mechanism is one integer per account and one comparison.

Two meters, three orders of magnitude apart

Gas is burnt and prices the WORK of writing or deleting; deleting genuinely costs gas. Storage prices RESIDENCY and is never spent. Measured on NEAR at 100KB the ratio is about 3000:1 -- the burn to delete is ~0.01% of the stake it releases, which is what makes cleanup rational rather than something you pay twice for. skuld.ledger.cost is the first meter; this is the second.

Attribution: namespace first, row owner second

The question every design has to answer is which account a datom's bytes count against, and NEAR gets it wrong in a way that is instructive. Its trie is account-partitioned, so bytes are charged to the account whose subtree the data lives in -- the CONTRACT, not the writer. A fungible-token contract therefore pays the storage stake for every user's balance row, and one user can grow it until the contract can no longer be written to at all. NEP-145 exists to work around this at the application layer: contracts make each user pre-pay, track it manually, and refuse to serve the unregistered.

Datoms carry provenance, so we can do at the protocol layer what NEAR needs a standard for:

a datom whose ATTRIBUTE is in a key's namespace counts against that key, wherever the datom sits. Annotating someone else's row with your own attribute is charged to you, which is the correct answer and the one NEAR cannot express.

a datom on a SHARED-SCHEMA attribute counts against the row's owner, since the attribute names no one and the row does.

Neither costs a lookup we do not already perform: the namespace is in the attribute, and the row owner is what skuld.ledger.ownership reads anyway.

The counter will drift, so measure it

NEAR needed FOUR protocol upgrades to fix byte-counting bugs, plus a hard-coded per-account correction table shipped for mainnet. An incrementally-maintained counter diverges from the recomputable truth; the only question is whether a test finds out or a chain halt does. recompute and audit exist for that, and are meant to be run in tests and by operators, not merely to exist.

Who pays for state that persists, and how much.

## An invariant, not an escrow

The obvious design is a deposit: lock tokens when data is written, refund
them when it is deleted. It is what I prototyped, and it has a problem --
a refund is a payment, so a transaction that frees a lot of data has a
NEGATIVE fee, and a transaction that pays its sender is a minting attack
unless refunds are bounded by a per-owner deposit ledger.

NEAR does not have that problem because it does not have refunds. Storage
is a POST-CONDITION on the account:

    balance  >=  bytes-used x rate

There is no deposit account, no refund receipt, no release action. Freeing
bytes lowers `bytes-used`, which lowers the requirement, which makes tokens
spendable again -- automatically, exactly, in the same transaction. The
whole mechanism is one integer per account and one comparison.

## Two meters, three orders of magnitude apart

Gas is burnt and prices the WORK of writing or deleting; deleting genuinely
costs gas. Storage prices RESIDENCY and is never spent. Measured on NEAR at
100KB the ratio is about 3000:1 -- the burn to delete is ~0.01% of the stake
it releases, which is what makes cleanup rational rather than something you
pay twice for. `skuld.ledger.cost` is the first meter; this is the second.

## Attribution: namespace first, row owner second

The question every design has to answer is which account a datom's bytes
count against, and NEAR gets it wrong in a way that is instructive. Its trie
is account-partitioned, so bytes are charged to the account whose subtree
the data lives in -- the CONTRACT, not the writer. A fungible-token contract
therefore pays the storage stake for every user's balance row, and one user
can grow it until the contract can no longer be written to at all. NEP-145
exists to work around this at the application layer: contracts make each
user pre-pay, track it manually, and refuse to serve the unregistered.

Datoms carry provenance, so we can do at the protocol layer what NEAR needs
a standard for:

  a datom whose ATTRIBUTE is in a key's namespace counts against that key,
  wherever the datom sits. Annotating someone else's row with your own
  attribute is charged to you, which is the correct answer and the one
  NEAR cannot express.

  a datom on a SHARED-SCHEMA attribute counts against the row's owner, since
  the attribute names no one and the row does.

Neither costs a lookup we do not already perform: the namespace is in the
attribute, and the row owner is what `skuld.ledger.ownership` reads anyway.

## The counter will drift, so measure it

NEAR needed FOUR protocol upgrades to fix byte-counting bugs, plus a
hard-coded per-account correction table shipped for mainnet. An
incrementally-maintained counter diverges from the recomputable truth; the
only question is whether a test finds out or a chain halt does. `recompute`
and `audit` exist for that, and are meant to be run in tests and by
operators, not merely to exist.
raw docstring

auditclj

(audit db recorded)

Accounts whose recorded usage disagrees with the recomputed truth.

Empty is the only acceptable answer. Returns the disagreements rather than a boolean so a drift can be attributed to an account rather than merely detected.

Accounts whose recorded usage disagrees with the recomputed truth.

Empty is the only acceptable answer. Returns the disagreements rather than a
boolean so a drift can be attributed to an account rather than merely
detected.
raw docstring

charged-toclj

(charged-to db assigned fallback datom)

Which account a datom's bytes count against.

Namespace first, row owner second, and fallback -- the transaction's sender -- last. See the namespace docstring on why the first two are the half NEAR could not express.

The fallback is not a nicety. Returning nil for an unowned row meant those bytes were charged to NOBODY and skipped entirely, which is unbounded free state growth: exactly the failure this namespace exists to prevent. NEAR has no equivalent gap because every byte lives in some account's subtree by construction; here a row without an owner is representable, so somebody has to pay for it and the writer is the only defensible answer.

Which account a datom's bytes count against.

Namespace first, row owner second, and `fallback` -- the transaction's
sender -- last. See the namespace docstring on why the first two are the
half NEAR could not express.

The fallback is not a nicety. Returning nil for an unowned row meant those
bytes were charged to NOBODY and skipped entirely, which is unbounded free
state growth: exactly the failure this namespace exists to prevent. NEAR has
no equivalent gap because every byte lives in some account's subtree by
construction; here a row without an owner is representable, so somebody has
to pay for it and the writer is the only defensible answer.
raw docstring

checkclj

(check {:keys [balances bonded usage rate] :or {bonded {} rate default-rate}})

Accounts left under-funded by a transaction, as {account shortfall}.

A POST-condition, evaluated after every effect of the transaction, never per-datom: a transaction must be free to grow and shrink internally, and only its net result has to be affordable. NEAR checks per-receipt for the same reason, and rolls the whole receipt back on failure.

Empty means the transaction is affordable and may be applied.

Accounts left under-funded by a transaction, as `{account shortfall}`.

A POST-condition, evaluated after every effect of the transaction, never
per-datom: a transaction must be free to grow and shrink internally, and
only its net result has to be affordable. NEAR checks per-receipt for the
same reason, and rolls the whole receipt back on failure.

Empty means the transaction is affordable and may be applied.
raw docstring

covered?clj

(covered? balance bytes)
(covered? balance bonded bytes rate)

Does balance cover bytes of residency?

Bonded stake counts toward the requirement, following NEAR: staking your tokens must not cost you your data, or the two incentives fight.

Does `balance` cover `bytes` of residency?

Bonded stake counts toward the requirement, following NEAR: staking your
tokens must not cost you your data, or the two incentives fight.
raw docstring

datom-bytesclj

(datom-bytes datom)

default-rateclj

Tokens required per byte held.

A placeholder to be configured, not believed. What matters structurally is that it is orders of magnitude ABOVE the per-byte gas charge: residency is permanent and the work of writing is not.

Tokens required per byte held.

A placeholder to be configured, not believed. What matters structurally is
that it is orders of magnitude ABOVE the per-byte gas charge: residency is
permanent and the work of writing is not.
raw docstring

per-datom-overheadclj

Bytes charged for a datom's own index entries, beyond its value.

A flat approximation, as NEAR's num_extra_bytes_record (40) is. The true cost depends on index structure and shared prefixes; a slightly generous constant is the right kind of wrong here, because under-charging is what lets state grow for free.

Bytes charged for a datom's own index entries, beyond its value.

A flat approximation, as NEAR's `num_extra_bytes_record` (40) is. The true
cost depends on index structure and shared prefixes; a slightly generous
constant is the right kind of wrong here, because under-charging is what
lets state grow for free.
raw docstring

recomputeclj

(recompute db)
(recompute db fallback)

Byte usage for every account, derived from the database itself.

The ground truth the incrementally-maintained counter is checked against. NEAR ships the equivalent (compute_storage_usage) plus a delta tool, and needed both.

Byte usage for every account, derived from the database itself.

The ground truth the incrementally-maintained counter is checked against.
NEAR ships the equivalent (`compute_storage_usage`) plus a delta tool, and
needed both.
raw docstring

requiredclj

(required bytes)
(required bytes rate)

Tokens account must hold to cover bytes.

Tokens `account` must hold to cover `bytes`.
raw docstring

usage-deltaclj

(usage-delta report)
(usage-delta report fallback)

Byte change per account produced by report, as {account delta}.

Additions add, retractions subtract, and BOTH use the same expression -- that symmetry is what makes release exact, with no rounding drift between the two directions. Attribution resolves against :db-before plus the transaction's own owner assignments, so a row created and a row retracted are both charged to somebody.

Byte change per account produced by `report`, as `{account delta}`.

Additions add, retractions subtract, and BOTH use the same expression --
that symmetry is what makes release exact, with no rounding drift between
the two directions. Attribution resolves against `:db-before` plus the
transaction's own owner assignments, so a row created and a row retracted
are both charged to somebody.
raw docstring

value-bytesclj

(value-bytes v)

A deterministic size for a datom's value.

Deterministic is the requirement, not accurate: every validator must agree. pr-str is identical everywhere for the value types datahike stores.

A deterministic size for a datom's value.

Deterministic is the requirement, not accurate: every validator must agree.
`pr-str` is identical everywhere for the value types datahike stores.
raw 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