Transaction costs for the datalog ledger.
Consensus decides ORDER. It does not decide how much work a transaction is, and it must not: the state machine is a pure function with no database, so it cannot know what a transaction will actually do. This namespace prices the work, at the one place that can — where the agreed prefix meets the database.
The obvious design is to charge for the transaction as written: count its forms, or its bytes, and reject anything too big. That is unsound here, and measurably so:
[[:db/retractEntity 42]]
is twenty-three characters and produced FOUR datoms in the probe that motivated this namespace, on an entity with four attributes. On an entity with ten thousand attributes it is still twenty-three characters. The work is a function of the DATABASE, not of the text, and the gap between them is unbounded — which is exactly the shape of an attack: bounded fee, unbounded work.
The same holds for upserts that resolve to a retraction plus an addition, for
cardinality-many replacement, and for :db/fn transaction functions, which
can expand to anything at all.
d/with applies a transaction to a database VALUE and returns the datoms it
would produce, without committing anything. So the real cost is available
before the decision to pay it:
price -> run `d/with`, count the datoms it really produces
afford? -> compare against what is left in the budget
apply -> commit only if it fits
It is deterministic. Every honest validator holds the identical committed
prefix — that is what consensus just established — so every validator calls
d/with on the identical database value with the identical transaction, gets
the identical datoms, computes the identical cost, and makes the identical
accept/reject decision. Cost enforcement therefore adds no new agreement
problem; it rides entirely on the agreement urd already provides.
This is the same seam argument that puts signature verification at the network edge and konserve's async API at the store: the pure core stays pure, and the thing that needs the world happens where the world is.
Its :base fee, and nothing else. It is CONSUMED — recorded as seen, with no
datoms written — rather than left to be retried forever. A transaction that
could be retried for free after being refused for cost is a free retry loop,
which is the thing a budget exists to prevent.
Pricing also enforces the protocol limit on logical transaction expansion.
Transaction costs for the datalog ledger.
Consensus decides ORDER. It does not decide how much work a transaction is,
and it must not: the state machine is a pure function with no database, so it
cannot know what a transaction will actually do. This namespace prices the
work, at the one place that can — where the agreed prefix meets the database.
## Why cost cannot be estimated from the transaction
The obvious design is to charge for the transaction as written: count its
forms, or its bytes, and reject anything too big. That is unsound here, and
measurably so:
[[:db/retractEntity 42]]
is twenty-three characters and produced FOUR datoms in the probe that
motivated this namespace, on an entity with four attributes. On an entity
with ten thousand attributes it is still twenty-three characters. The work is
a function of the DATABASE, not of the text, and the gap between them is
unbounded — which is exactly the shape of an attack: bounded fee, unbounded
work.
The same holds for upserts that resolve to a retraction plus an addition, for
cardinality-many replacement, and for `:db/fn` transaction functions, which
can expand to anything at all.
## What we do instead: measure, then decide
`d/with` applies a transaction to a database VALUE and returns the datoms it
would produce, without committing anything. So the real cost is available
before the decision to pay it:
price -> run `d/with`, count the datoms it really produces
afford? -> compare against what is left in the budget
apply -> commit only if it fits
## Why this is safe across validators
It is deterministic. Every honest validator holds the identical committed
prefix — that is what consensus just established — so every validator calls
`d/with` on the identical database value with the identical transaction, gets
the identical datoms, computes the identical cost, and makes the identical
accept/reject decision. Cost enforcement therefore adds no new agreement
problem; it rides entirely on the agreement urd already provides.
This is the same seam argument that puts signature verification at the network
edge and konserve's async API at the store: the pure core stays pure, and the
thing that needs the world happens where the world is.
## What a rejected transaction costs
Its `:base` fee, and nothing else. It is CONSUMED — recorded as seen, with no
datoms written — rather than left to be retried forever. A transaction that
could be retried for free after being refused for cost is a free retry loop,
which is the thing a budget exists to prevent.
Pricing also enforces the protocol limit on logical transaction expansion.(afford? remaining priced)Does priced fit in what is left of the budget?
nil means unmetered, and is the default everywhere: a deployment that has
not thought about costs gets the behaviour it had before this namespace
existed, rather than a limit it did not choose.
Does `priced` fit in what is left of the budget? `nil` means unmetered, and is the default everywhere: a deployment that has not thought about costs gets the behaviour it had before this namespace existed, rather than a limit it did not choose.
(datom-cost schedule datom)What one produced datom costs, under schedule.
What one produced datom costs, under `schedule`.
What each unit of work costs.
The numbers are a starting point and are meant to be configured, not believed. What matters structurally is the SHAPE:
:base is charged for every transaction, accepted or rejected. Without it
a stream of rejected transactions is free, and free is a denial of service.:byte prices the value, so a datom carrying a megabyte string is not the
same as one carrying an integer.It was 50 against an addition's 100, on the reasoning that an addition grows the indices while a retraction is bounded by what is already there. That reasoning is about the datoms PRODUCED and misses the work of finding them.
Measured against a 200-attribute entity, [:db/retractEntity e] bought
roughly twice as much d/with time per unit of fee as an ordinary write —
it is three tokens long, names no attribute, and makes the validator walk
the entity. Pricing retraction equal to addition moved that advantage from
2.13x to 1.07x, i.e. to parity. It was the whole of the gap.
The remaining asymmetry is deliberate and lives elsewhere: retraction should eventually REFUND the storage a datom was occupying, which prices net growth rather than gross writes. That is a deposit, not a discount, and a discount on the compute term is not a substitute for it.
A deployment that wants a different schedule supplies one. It must be the SAME schedule on every validator, for the same reason the schedule can be applied at all — see the namespace docstring.
What each unit of work costs. The numbers are a starting point and are meant to be configured, not believed. What matters structurally is the SHAPE: - `:base` is charged for every transaction, accepted or rejected. Without it a stream of rejected transactions is free, and free is a denial of service. - a retraction costs the SAME as an addition. See below — this used to be half, and the discount was an attack. - `:byte` prices the value, so a datom carrying a megabyte string is not the same as one carrying an integer. ## Why retraction is no longer discounted It was 50 against an addition's 100, on the reasoning that an addition grows the indices while a retraction is bounded by what is already there. That reasoning is about the datoms PRODUCED and misses the work of finding them. Measured against a 200-attribute entity, `[:db/retractEntity e]` bought roughly twice as much `d/with` time per unit of fee as an ordinary write — it is three tokens long, names no attribute, and makes the validator walk the entity. Pricing retraction equal to addition moved that advantage from 2.13x to 1.07x, i.e. to parity. It was the whole of the gap. The remaining asymmetry is deliberate and lives elsewhere: retraction should eventually REFUND the storage a datom was occupying, which prices net growth rather than gross writes. That is a deposit, not a discount, and a discount on the compute term is not a substitute for it. A deployment that wants a different schedule supplies one. It must be the SAME schedule on every validator, for the same reason the schedule can be applied at all — see the namespace docstring.
(meter db tx-datas)(meter schedule budget db tx-datas)Price and apply tx-datas in order against db, within budget.
Returns
{:db-after the database value after everything that was applied
:applied [{:index i :cost n :datoms k}]
:rejected [{:index i :cost n :reason :over-budget|:error :error msg}]
:spent total charged, including the base fee of rejected ones
:remaining what is left of the budget, or nil when unmetered}
Sequential and order-preserving, because each transaction is priced against the database the PREVIOUS one produced. That is not an implementation detail: the cost of a transaction depends on the state it lands in, so pricing them in parallel, or against the starting state, would give a different answer to a validator that applied them in the agreed order. Cost has to be measured along the same path everyone else walks.
A rejection does NOT stop the run. Later transactions are independent, and one node stopping early where another did not is precisely the divergence this whole library exists to prevent. Everything is charged its base fee, so a rejected transaction is still paid for.
:spent never exceeds budget and :remaining never goes below zero. The
first version of this charged the base fee for rejections unconditionally,
which let a budget of 1300 record 3101 spent and -1801 remaining across three
transactions. A budget that does not bound spending is not a budget, and the
overrun was silent — it showed up only because the numbers were printed.
Price and apply `tx-datas` in order against `db`, within `budget`.
Returns
{:db-after the database value after everything that was applied
:applied [{:index i :cost n :datoms k}]
:rejected [{:index i :cost n :reason :over-budget|:error :error msg}]
:spent total charged, including the base fee of rejected ones
:remaining what is left of the budget, or nil when unmetered}
Sequential and order-preserving, because each transaction is priced against
the database the PREVIOUS one produced. That is not an implementation detail:
the cost of a transaction depends on the state it lands in, so pricing them
in parallel, or against the starting state, would give a different answer to
a validator that applied them in the agreed order. Cost has to be measured
along the same path everyone else walks.
A rejection does NOT stop the run. Later transactions are independent, and
one node stopping early where another did not is precisely the divergence
this whole library exists to prevent. Everything is charged its base fee, so
a rejected transaction is still paid for.
`:spent` never exceeds `budget` and `:remaining` never goes below zero. The
first version of this charged the base fee for rejections unconditionally,
which let a budget of 1300 record 3101 spent and -1801 remaining across three
transactions. A budget that does not bound spending is not a budget, and the
overrun was silent — it showed up only because the numbers were printed.Maximum logical expansion units in one speculative pricing pass.
Maximum logical expansion units in one speculative pricing pass.
(prepare db forms per-pass remaining)Prepare against a single block executor's remaining units. Every attempted pass consumes its actual units, including work preceding an exception.
Prepare against a single block executor's remaining units. Every attempted pass consumes its actual units, including work preceding an exception.
(price db tx-data)(price schedule db tx-data)(price schedule db tx-data remaining)What applying tx-data to the database value db would cost.
Returns {:cost n :datoms k :report r}, or {:cost base :error msg} when the
transaction cannot be applied at all. Nothing is committed either way.
A transaction that THROWS is priced at the base fee and refused. It has to be handled here rather than allowed to propagate: the input arrived over the network, malformed transactions are an expected input rather than a bug, and an exception escaping into the apply loop would take the node down on a message anyone can send. It also has to be deterministic, and it is — every validator runs the same transaction against the same database and gets the same exception.
What applying `tx-data` to the database value `db` would cost.
Returns `{:cost n :datoms k :report r}`, or `{:cost base :error msg}` when the
transaction cannot be applied at all. Nothing is committed either way.
A transaction that THROWS is priced at the base fee and refused. It has to be
handled here rather than allowed to propagate: the input arrived over the
network, malformed transactions are an expected input rather than a bug, and
an exception escaping into the apply loop would take the node down on a
message anyone can send. It also has to be deterministic, and it is — every
validator runs the same transaction against the same database and gets the
same exception.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 |