algorand / pyteal

Algorand Smart Contracts in Python
https://pyteal.readthedocs.io
MIT License
287 stars 131 forks source link

Provide Op Cost method given Expression #445

Open barnjamin opened 2 years ago

barnjamin commented 2 years ago

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:

ahangsu commented 2 years ago

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.