ScorexFoundation / sigmastate-interpreter

ErgoScript compiler and ErgoTree Interpreter implementation for Ergo blockchain
MIT License
62 stars 40 forks source link

[v6.0] Accumulate ErgoTree rewriting cost when handling Deserialize operations #846

Open aslesarenko opened 1 year ago

aslesarenko commented 1 year ago

Current implementation

In order to execute DeserializeContext operation, the ErgoTree is traversed once. During the traverse the DeserializeContext node is replaced by the result of deserialization. It is done here

The deserializeSubstitutionCost is calculated and accumulated to the new context1 as initCost, however the context1 is erroneously not used in applyDeserializeContextJITC.

What should be done

The context1 should be used in applyDeserializeContextJITC instead of context.

SethDusek commented 10 months ago

I would like to attempt this.

SethDusek commented 10 months ago

Hi, some questions regarding this. I've accumulated the deserialization cost, but this presents some issues:

aslesarenko commented 9 months ago

@SethDusek, yes, this change can only be activated (or rather its execution should be conditional on the currently activated protocol version) Here is an example how such change has been done for v5.0 https://github.com/ScorexFoundation/sigmastate-interpreter/blob/aae4118fed18f6587413d9a6330e449b05d8d5ad/data/shared/src/main/scala/sigma/ast/trees.scala#L38 You can add a new version constant here https://github.com/ScorexFoundation/sigmastate-interpreter/blob/11c98a3382646095ee943b333a0b5bfa65fa291d/core/shared/src/main/scala/sigma/VersionContext.scala#L37-L36

Call it like this val ScriptVersionForV6: Byte = 3

SethDusek commented 9 months ago

Hi, another question regarding this. Should this only apply if both v6.0 is activated and the ErgoTree version is >= 3, or should it apply to any version of ergotree including 0, 1, and 2 provided this is activated on the network? Asking because if it applies if activation then the test cases will need deserialization costs for all previous versions (Some of them have different ergotree byte lengths apparently)

aslesarenko commented 9 months ago

Hi, @SethDusek. It shouldn't depend on the test cases, but rather on usability and backwards compatibility.

1) Regardless of the activated version, dApps can create new ergo tree with v0-v2, and with v3 activated, they should continue to do so. 2) Cost estimation is internal mechanism, and we better keep it simple when possible, which means we can do the same calculation of costs for all versions of ErgoTree.

So, I think we should choose "it apply to any version of ergotree including 0, 1, and 2 provided this is activated on the network".