algorand / go-algorand

Algorand's official implementation in Go.
https://developer.algorand.org/
Other
1.35k stars 473 forks source link

Handle int/byte const blocks differently #2029

Closed barnjamin closed 3 years ago

barnjamin commented 3 years ago

Is your feature request related to a problem? Please describe.

As part of a project I'm working on I need a delegate sig to validate that the account receiving an ASA was created using a template contract. To do this I'm having the populated contract and template variables passed into the delegate signature signature call, removing the variable bytes and comparing the hash with the known empty template hash.

Removing the bytes is not as straight forward as it could be though not impossible.

Describe the solution you'd like.

I'd like the ability to define TMPL_* variables directly in the intc/bytec blocks. Currently defining intc/bytec blocks in teal results in the assembler assuming this step was already done and not looking for other constants. Also, if the constants appear more than once they're de-duplicated which complicates removing just the bytes I'm interested in.

Additional context.

I wrote up a quick summary of what I'm trying to do here and the pyteal/teal/helper scripts are there as well. This may be a terrible idea to implement or put in production so I'd appreciate any criticism you might have about it.

jasonpaulos commented 3 years ago

Hi @barnjamin, I assume this is a follow up from algorand/pyteal#48? If that's the case, I think there are two things worth mentioning:

  1. TEAL v3 introduced new ops pushint and pushbytes which are alternatives to using intc/bytec blocks. These allow you to push constants to the stack without the assembler deduplicating them. Is this what you're asking for?
  2. We have a program called tealcut which was designed to help overwrite constants, like template variables, in compiled TEAL programs. It's pretty basic right now, but would something like this be useful to you?
barnjamin commented 3 years ago

Hi @jasonpaulos it is, I should have linked it.

  1. After you pointed this out it does look like it would be useful. I didn't realize they don't get added to the const blocks when just looking at the op description. Thanks! Should I PR a doc tweak to mention that or would that be too verbose for the op docs?

  2. It looks like that program does something like the preprocessing I do before compiling the delegate sig program with the hash of the template program. I'm not likely to use that specific program but the fact that you have something doing this already is encouraging that I'm not doing something completely stupid :)

jasonpaulos commented 3 years ago

Should I PR a doc tweak to mention that or would that be too verbose for the op docs?

I think that would be a good improvement. The opcode docs are generated from the code here: https://github.com/algorand/go-algorand/blob/master/data/transactions/logic/doc.go

...the fact that you have something doing this already is encouraging that I'm not doing something completely stupid :)

Glad to hear that! It's complicated to deal with template contracts like this, but they definitely have legitimate use cases.

barnjamin commented 3 years ago

https://github.com/algorand/go-algorand/pull/2030 submitted here