pyteal
pyteal copied to clipboard
Provide Op Cost method given Expression
Problem
I want to know how much a given expression should cost for computing how many additional opup requests to make.
Adding another element to the Op tuple for cost provides a way to get the cost per op and we can use something like
from pyteal import *
options = CompileOptions(mode=Mode.Application, version=6)
def op_cost(tb: TealBlock):
cost = 0
for x in TealBlock.Iterate(tb):
for op in x.ops:
cost += op.op.cost
return cost
tb, _ = Seq(
Assert(Int(1)),
Pop(Int(1)+Int(1)),
).__teal__(options)
print(op_cost(tb)) # 6
Issues with this approach:
-
Variable opcode cost by pragma version
-
Variable opcode costs by argument (Secp256k1=1700, Secp256r1=2500)
-
Things like loops can't be used directly, rather the author should compute the cost of the loop body and multiply it by the number of times it is called at runtime
Just to add a related issue: some opcodes like jsonref has cost linear to the length of stack argument, so it cannot be handled trivially.