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

[EVM] Add get random source to Cadence Arch #5742

Merged
merged 20 commits into from
Apr 25, 2024

Conversation

sideninja
Copy link
Contributor

Closes: #5507

This PR implements a new function on the Cadence Arch precompile contract, that exposes access to Flow RandomBeaconHistory contract. This allows for retrieving a random source in the EVM which can be used as a seed for any PRG implementations.

The new method could be called like so:

(bool ok, bytes memory data) = cadenceArch.staticcall(abi.encodeWithSignature("getRandomSource(uint64)", height))

It also allows for implementing commit-reveal schemes which are required for a lot of applications and was discussed on Discord: https://discord.com/channels/613813861610684416/1230199673629835375
We must keep in mind that the new function for obtaining random source relies on Cadence block heights not EVM block heights, which are not co-related but this is fine since we can use the get latest flow height function also available on Cadence arch.

@sisyphusSmiling even made a proof of concept that abstracts away this implementation detail https://gist.github.com/sisyphusSmiling/3e58dc3827c69afd038abe65ef8dda92

@sideninja sideninja self-assigned this Apr 19, 2024
Comment on lines +26 to +29
func RandomBeaconAddress(chainID flow.ChainID) flow.Address {
return systemcontracts.SystemContractsForChain(chainID).RandomBeaconHistory.Address
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have to be here, given we pass the chainID to the underlying layer. I know we do this for storage account but I think that's a legacy from the time EVM storage address was not part of system contracts.

Comment on lines +48 to +49
func(u uint64) (uint64, error) {
return rand, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might add a line here to check if the input that is received is what is expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea yeah

Comment on lines +54 to +55
uint64 output = abi.decode(data, (uint64));
return output;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually use the verify methods to verify the output matches what is expected, for example see the verifyArchCallToFlowBlockHeight , we check the output produced matches the expected.

Copy link
Contributor Author

@sideninja sideninja Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that in Go test, but it makes sense to adpot the pattern you made

Copy link
Contributor

@ramtinms ramtinms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work 👏 , just added some minor comments

Copy link
Contributor

@janezpodhostnik janezpodhostnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice!

@sideninja sideninja added this pull request to the merge queue Apr 25, 2024
Merged via the queue into master with commit 9a6781f Apr 25, 2024
55 checks passed
@sideninja sideninja deleted the gregor/evm/revertible-random branch April 25, 2024 11:22
@sideninja sideninja restored the gregor/evm/revertible-random branch May 16, 2024 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Flow EVM] provide revertible randomness in EVM
4 participants