# How to price operations in smart contracts ## Time-based pricing Blocks are proposed (or mined) sequantially. The protocol enforces a round length $T$ 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 $T$. Let $X=\{X_1, X_2,\dots,X_N\}\in \mathbb{Z}_{\geq 0}^N$ be the set of variables that count the number of each operation. Each operation $i$ has a constant price in gas $P_i\in \mathbb{Z}_{\geq 0}$, and each block has a gas limit $G\in \mathbb{Z}_{\geq 0}$. **Gas Inequality:** The protocol dictates that given a block, all operations from included transactions are summed up, and the inequality $$ P_1X_1 + P_2X_2 + \cdots + P_NX_N \leq G $$ must hold. For all $i$, let $t_i: \mathbb{Z}_{\geq 0}\to \mathbb{Z}_{\geq 0}$ map each variable to the time required to "execute" it (or the time that needs to be allocated for it). We assume $t_i$ to be monotonically increasing, bijective and $t_i(0)=0$ for all $i$. **Time inequality:** Sum of the times consumed by each operation should be less than or equal to the round length, i.e. $$ t_1(X_1)+t_2(X_2)+\cdots+t_N(X_N)\leq T $$ **Goal:** To assign a price $P_i$ to each operation $i$ using $T$, $t_i$ and $G$. 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. $t_i(X_i) = \alpha_i X_i$ for all $i$. Then time inequality becomes $$ \alpha_1 X_1+\alpha_2 X_2+\cdots+\alpha_N X_N\leq T. $$ We consider the scenarios that for a given $j$, $X_i=0$ for all $i\neq j$. In each scenario, time inequality is reduced to $\alpha_jX_j \leq T$, which gives us $$ \max(X_j) = \frac{T}{\alpha_j} $$ Moreover, gas inequality is reduced to $P_jX_j\leq G$. This gives us $P_j\max(X_j) = G$ which we substitute the previous result to obtain $$ \boxed{ P_j = \alpha_j \frac{G}{T} } $$ **Theorem:** When gas inequality holds and $P_i = \alpha_i\frac{ G}{T}$ for all $i$, time inequality also holds. *Proof:* We substitute $P_i$'s in gas inequality $$ \alpha_1\frac{G}{T}X_1 + \alpha_2\frac{G}{T}X_2 +\cdots + \alpha_N\frac{G}{T}X_N \leq G $$ which simplifies to time inequality. Thus, if gas inequality holds, time inequality also holds.$\blacksquare$ **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 $\alpha_0=$ 5e-4s and 1 `compute` $\alpha_1=$ 1e-7s. Let the round length be $T=$ 15s and block gas limit be $G=$ 10,000,000 gas. Then the gas prices should be set to $P_0=\alpha_0 G/T\approx 333$ and $P_1=\alpha_1 G/T\approx 0.0667$. We proceed by introducing some on-chain measurements: a transaction has $\bar{X}_0=$ 200 `txdata` and $\bar{X}_1=$ 50,000 `compute` on average. Then we can estimate the average number of transactions $n$ per block. Time inequality should be satisfied: $$ \alpha_0 n \bar{X}_0 + \alpha_1 n \bar{X}_1 \leq T $$ This gives us $$ \max(n) = \frac{T}{\alpha_0\bar{X}_0 + \alpha_1\bar{X}_1} \approx 142 $$ transactions per block on average. The respective throughputs are calculated as $\phi_\ast=\max(n)\bar{X}_\ast/T$, giving us $\phi_0\approx$ 1904 `txdata`/s and $\phi_1\approx$ 476910 `compute`/s. That means, between blocks $\max(n)\alpha_0\bar{X}_0\approx$ 14.3s (95%) of round length is consumed by`txdata` and $\max(n)\alpha_1\bar{X}_1\approx$ 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 $t_i$ is positive, monotonically increasing and bijective for all $i$, then the price for each operation $i$ can be derived from the limiting case where $t_i$'s slope is maximum. The idea is to replace $t_i$ 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 $i$ in the range $[0, t_i^{-1}(T)]$, i.e. from zero to the maximum value $X_i$ can take according to time inequality: $$ X^\ast_i = \mathop{\text{argmax}}_{0\leq X_i \leq t_i^{-1}(T)} \frac{dt_i}{dX_i} $$ We then assume linear functions $\bar{\alpha}_i X_i$ where $$ \bar{\alpha}_i = \frac{dt_i}{dX_i}\bigg|_{X^\ast_i} $$ Since $t_i$ is monotonically increasing, we can write $$ t_i(X_i) \leq \bar{\alpha}_i X_i \quad \forall i. \tag{$\ast$} $$ Then similar to the previous section, we can define the price for each $i$ as $$ \boxed{ P_i = \bar{m}_i \frac{G}{T} } $$ **Theorem:** When gas inequality holds and $P_i = \bar{\alpha}_i\frac{ G}{T}$ for all $i$, time inequality also holds. *Proof:* We substitute $P_i$'s in gas inequality $$ \bar{\alpha}_1\frac{G}{T}X_1 + \bar{\alpha}_2\frac{G}{T}X_2 +\cdots + \bar{\alpha}_N\frac{G}{T}X_N \leq G $$ which simplifies to $$ \bar{\alpha}_1 X_1 + \bar{\alpha}_2 X_2 +\cdots + \bar{\alpha}_N X_N \leq T. $$ $(\ast)$ allows us to write $$ t_1(X_1)+t_2(X_2)+\cdots+t_N(X_N) \leq \bar{\alpha}_1 X_1 + \bar{\alpha}_2 X_2 +\cdots + \bar{\alpha}_N X_N $$ Since RHS is less than or equal to $T$, LHS must be too. Thus, if gas inequality holds, time inequality also holds.$\blacksquare$ ### 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](https://eips.ethereum.org/EIPS/eip-1884). TBD