There is no commentSelect some text and then click Comment, or simply add a comment to this page from below to start a discussion.
How to price operations in smart contracts
Time-based pricing
Blocks are proposed (or mined) sequantially. The protocol enforces a round length in which all the operations must be completed before the next block can be proposed. This includes the time required to execute the transactions, as well the time required to propagate the block. In that sense, we impose no distinction between a computational operation such as addition or multiplication, and the propagation of a payload bit to all validators: both require some allocation of the total round length .
Let be the set of variables that count the number of each operation.
Each operation has a constant price in gas , and each block has a gas limit .
Gas Inequality: The protocol dictates that given a block, all operations from included transactions are summed up, and the inequality
must hold.
For all , let map each variable to the time required to "execute" it (or the time that needs to be allocated for it). We assume to be monotonically increasing, bijective and for all .
Time inequality: Sum of the times consumed by each operation should be less than or equal to the round length, i.e.
Goal: To assign a price to each operation using , and . The prices need to be assigned in a way to ensure that given gas inequality holds, time inequality also holds. This way, each block can be created, propagated and finalized on time.
Linear relationship between operation count and execution time
We assume that, time is consumed linearly with each new operation, i.e. for all . Then time inequality becomes
We consider the scenarios that for a given , for all . In each scenario, time inequality is reduced to , which gives us
Moreover, gas inequality is reduced to . This gives us which we substitute the previous result to obtain
Theorem: When gas inequality holds and for all , time inequality also holds.
Proof: We substitute 's in gas inequality
which simplifies to time inequality. Thus, if gas inequality holds, time inequality also holds.
Example: Let us have a set of only two operations, txdata, corresponding to a byte of transaction payload, and compute, corresponding to a basic instruction such as MUL. As such, txdata relates to bandwidth and compute relates to processing power. Each operation consumes a certain portion of the round length, either with propagation delay or local computation.
Let us assume that 1 txdata consumes 5e-4s and 1 compute 1e-7s. Let the round length be 15s and block gas limit be 10,000,000 gas. Then the gas prices should be set to and .
We proceed by introducing some on-chain measurements: a transaction has 200 txdata and 50,000 compute on average. Then we can estimate the average number of transactions per block. Time inequality should be satisfied:
This gives us
transactions per block on average. The respective throughputs are calculated as , giving us 1904 txdata/s and 476910 compute/s. That means, between blocks 14.3s (95%) of round length is consumed bytxdata and 0.7s (5%) by compute.
Nonlinear relationship between operation count and execution time
We generalize the idea from the previous section to the nonlinear case: if is positive, monotonically increasing and bijective for all , then the price for each operation can be derived from the limiting case where 's slope is maximum. The idea is to replace with a linear function with the maximum slope, and use that function to make a conservative estimate of the price.
To this end, we solve an optimization problem for each in the range , i.e. from zero to the maximum value can take according to time inequality:
We then assume linear functions where
Since is monotonically increasing, we can write
Then similar to the previous section, we can define the price for each as
Theorem: When gas inequality holds and for all , time inequality also holds.
Proof: We substitute 's in gas inequality
which simplifies to
allows us to write
Since RHS is less than or equal to , LHS must be too. Thus, if gas inequality holds, time inequality also holds.
When execution time of an operation depends on other variables
In certain cases, the execution time of an operation may depend on other variables, for example size of the state trie.