pub struct Cost(/* private fields */);
Expand description
CPU cost of a Simplicity expression.
The cost is measured in milli weight units and can be converted into weight units using the appropriate method.
Roughly speaking, the operational semantics of a combinator on the Bit Machine determine its cost.
First, every combinator has a fixed overhead cost. Frame allocations, copy and write operations cost proportional to the number of allocated or written bits. Frame moves / drops or cursor moves are one-step operations that are covered by the overhead.
The cost of a program is compared to its budget. A program is valid if it does not exceed its budget.
The budget is the size of the witness stack of the transaction input that includes the program. Users pay for their Simplicity programs in terms of fees which are based on transaction size, like normal Tapscript.
Programs that are CPU-heavy need to be padded so that the witness stack provides a large-enough budget.
Implementations§
Source§impl Cost
impl Cost
Sourcepub const CONSENSUS_MAX: Self
pub const CONSENSUS_MAX: Self
Maximum cost allowed by consensus.
This is equal to the maximum budget that any program can have inside a Taproot transaction: 4 million weight units plus 50 free weight units for validation.
This assumes a block that consists of a single transaction which in turn consists of nothing but its witness stack.
Transactions include other data besides the witness stack. Also, transaction may have multiple inputs and blocks usually include multiple transactions. This means that the maximum budget is an unreachable upper bound.
Sourcepub const fn of_type(bit_width: usize) -> Self
pub const fn of_type(bit_width: usize) -> Self
Return the cost of a type with the given bit width.
Sourcepub const fn from_milliweight(milliweight: u32) -> Self
pub const fn from_milliweight(milliweight: u32) -> Self
Convert the given milli weight units into cost.
Sourcepub fn is_consensus_valid(self) -> bool
pub fn is_consensus_valid(self) -> bool
Return whether the cost is allowed by consensus.
This means the cost is within the maximum budget that any program inside a Taproot transaction can have.