Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The new opcode to calculate the ContractId or PredicateId inside of the script/contract #490

Open
xgreenx opened this issue May 26, 2023 · 5 comments
Labels
question Further information is requested

Comments

@xgreenx
Copy link
Contributor

xgreenx commented May 26, 2023

Some users have use cases where they need to calculate the predicate id on the fly inside of the script. Adding a new opcode would be nice because it would be much cheaper to do inside on the VM side than on the Sway side.

We can add an opcode that calculates the contract's root, and then on the Sway side, the user can concatenate it with the ChainId and generate a predicate id. Or we can add an opcode that returns the predicate id directly.

Another question, do we want to do the same for ContractId? Maybe it is not too actual because usually, you also need state root(storage slots).

@xgreenx xgreenx added the question Further information is requested label May 26, 2023
@SilentCicero
Copy link
Member

Can you describe what the use case would be for calculating the predicate ID on the fly?

How would we handle cases for things like configuration time constants?

@xgreenx
Copy link
Contributor Author

xgreenx commented Jun 5, 2023

Let's imagine you have a script/contract that needs to create a personal predicate (where the owner or any other data is hardcoded into the predicate's bytecode).

The script/contract should have/load the predicate's bytecode into memory, replace the dummy values with the actual owner's data, and calculate a new predicate id.

Because the predicate's bytecode is a part of the script/contract, it has to be compiled before with some "default"/"dummy" values from configurable constants. The developer of the script/contract should handle the actualization of the data to personalize the predicate.

@dmihal
Copy link
Contributor

dmihal commented Jun 6, 2023

Calculating contract IDs & predicate IDs is definitely a useful feature.

IMO the most difficult part is filling in the configurable constants before calculating the root, I guess that's something that should probably be a feature added to Sway?

Regarding this opcode, I guess it would primarily be used to take a byte array and calculate the binary merkle tree root?

Would it be possible to do this as a precompile instead of an opcode?

@SilentCicero
Copy link
Member

Why can't this be done at the language level? I understand it may be complex, but still feels like something that can be done through library and not an opcode.

@xgreenx
Copy link
Contributor Author

xgreenx commented Jun 20, 2023

Why can't this be done at the language level? I understand it may be complex, but still feels like something that can be done through library and not an opcode.

It can be done on the language level, but it would be much cheaper in terms of gas for the end-user. We already provide the CROO opcode, but it fetches the root from the storage.

The code root calculation requires a binary Merkle tree, and implement it on the Sway will be too expensive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants