Who pays for state that persists, and how much.
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.
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.
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.
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.(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.
(check {:keys [balances usage rate] :or {rate default-rate}})Return collateral shortfalls. Only the post-settlement liquid balance counts.
Return collateral shortfalls. Only the post-settlement liquid balance counts.
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.
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.
(recompute db)Recompute current application and guard storage from a finalized database.
Recompute current application and guard storage from a finalized database.
(required bytes)(required bytes rate)Tokens account must hold to cover bytes.
Tokens `account` must hold to cover `bytes`.
(usage-delta {:keys [db-before db-after tx-data]})Price added and removed facts under the payer in their respective snapshots. For changes to an existing row owner, reattribute retained shared facts too.
Price added and removed facts under the payer in their respective snapshots. For changes to an existing row owner, reattribute retained shared facts too.
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 |