Try   HackMD

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={X1,X2,,XN}Z0N be the set of variables that count the number of each operation.

Each operation

i has a constant price in gas
PiZ0
, and each block has a gas limit
GZ0
.

Gas Inequality: The protocol dictates that given a block, all operations from included transactions are summed up, and the inequality

P1X1+P2X2++PNXNG

must hold.

For all

i, let
ti:Z0Z0
map each variable to the time required to "execute" it (or the time that needs to be allocated for it). We assume
ti
to be monotonically increasing, bijective and
ti(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.

t1(X1)+t2(X2)++tN(XN)T

Goal: To assign a price

Pi to each operation
i
using
T
,
ti
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.

ti(Xi)=αiXi for all
i
. Then time inequality becomes

α1X1+α2X2++αNXNT.

We consider the scenarios that for a given

j,
Xi=0
for all
ij
. In each scenario, time inequality is reduced to
αjXjT
, which gives us

max(Xj)=Tαj

Moreover, gas inequality is reduced to

PjXjG. This gives us
Pjmax(Xj)=G
which we substitute the previous result to obtain

Pj=αjGT

Theorem: When gas inequality holds and

Pi=αiGT for all
i
, time inequality also holds.

Proof: We substitute

Pi's in gas inequality

α1GTX1+α2GTX2++αNGTXNG

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

α0= 5e-4s and 1 compute
α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
P0=α0G/T333
and
P1=α1G/T0.0667
.

We proceed by introducing some on-chain measurements: a transaction has

X¯0= 200 txdata and
X¯1=
50,000 compute on average. Then we can estimate the average number of transactions
n
per block. Time inequality should be satisfied:

α0nX¯0+α1nX¯1T

This gives us

max(n)=Tα0X¯0+α1X¯1142

transactions per block on average. The respective throughputs are calculated as

ϕ=max(n)X¯/T, giving us
ϕ0
1904 txdata/s and
ϕ1
476910 compute/s. That means, between blocks
max(n)α0X¯0
14.3s (95%) of round length is consumed bytxdata and
max(n)α1X¯1
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

ti is positive, monotonically increasing and bijective for all
i
, then the price for each operation
i
can be derived from the limiting case where
ti
's slope is maximum. The idea is to replace
ti
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,ti1(T)]
, i.e. from zero to the maximum value
Xi
can take according to time inequality:

Xi=argmax0Xiti1(T)dtidXi

We then assume linear functions

α¯iXi where

α¯i=dtidXi|Xi

Since

ti is monotonically increasing, we can write

()ti(Xi)α¯iXii.

Then similar to the previous section, we can define the price for each

i as

Pi=m¯iGT

Theorem: When gas inequality holds and

Pi=α¯iGT for all
i
, time inequality also holds.

Proof: We substitute

Pi's in gas inequality

α¯1GTX1+α¯2GTX2++α¯NGTXNG

which simplifies to

α¯1X1+α¯2X2++α¯NXNT.

() allows us to write

t1(X1)+t2(X2)++tN(XN)α¯1X1+α¯2X2++α¯NXN

Since RHS is less than or equal to

T, 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.

TBD