Implement prevrandao block_randomness in pallet-evm#246
Implement prevrandao block_randomness in pallet-evm#246snowmead wants to merge 3 commits intomoonbeam-polkadot-stable2409from
block_randomness in pallet-evm#246Conversation
get random in `block_randomness` for prevrandao
|
|
||
| #[pallet::no_default] | ||
| type RandomnessProvider: frame_support::traits::Randomness< | ||
| sp_core::H256, |
There was a problem hiding this comment.
The runtime implementing this pallet may not what to enabled randomness. (For example, the template runtime which currently has a really dangerous example)
| sp_core::H256, | |
| Option<sp_core::H256>, |
We can also put this type behind a feature, or just allow the random result to be None.
There was a problem hiding this comment.
I want to avoid changing this type to Option because I wouldn't be able to pass the randomness pallet directly as an impl since it implements <Hash, ...>. I would have to wrap it.
primitives/evm/src/lib.rs
Outdated
| /// `AccountCodes` key size. 16 (hash) + 20 (key) | ||
| pub const ACCOUNT_CODES_KEY_SIZE: u64 = 36; | ||
| /// System block number. | ||
| pub const SYSTEM_BLOCK_NUMBER_PROOF_SIZE: u64 = 32; |
| pub struct RandomnessProvider; | ||
| impl | ||
| frame_support::traits::Randomness< | ||
| <Runtime as frame_system::Config>::Hash, | ||
| BlockNumberFor<Runtime>, | ||
| > for RandomnessProvider | ||
| { | ||
| fn random( | ||
| subject: &[u8], | ||
| ) -> ( | ||
| <Runtime as frame_system::Config>::Hash, | ||
| BlockNumberFor<Runtime>, | ||
| ) { | ||
| let output = <Runtime as frame_system::Config>::Hashing::hash(subject); | ||
| let block_number = frame_system::Pallet::<Runtime>::block_number(); | ||
| (output, block_number) | ||
| } | ||
| } |
There was a problem hiding this comment.
This example is dangerous and we should change it. Suggestion in a comment above.
There was a problem hiding this comment.
I could return a zero hash instead.
There was a problem hiding this comment.
Ignore my comment, this should be fine. The spec explicit says that this should not be used as a true randomness source.
There was a problem hiding this comment.
Sorry for coming back to this, could we append pallet_timestamp::Pallet::<Runtime>::now() to the subject.
And also add a note saying This is just an example, this should not be used as a true randomness source
|
It is probably fine to add it to frontier, but we should not need it on moonbeam since there is no need. Similarly to arbitrum, they always return a constant value https://docs.arbitrum.io/build-decentralized-apps/arbitrum-vs-ethereum/solidity-support#differences-from-solidity-on-ethereum |
I think we should add it for the purposes of giving smart contract developers the ability to access randomness easily without going through the regular request/response scheme we have for BABE and local VRF. We just need to ensure that they know this source of randomness is not to be trusted for critical operations. They could use it for games and other things that do not require much safety guarantee. |
Get random in
block_randomnessfromRanomnessProviderimplementation passed in as a config to pallet-evm .This randomness is not necessarily secure since it is generated and returned on the fly.
It is still recommended to use the BABE and local VRF precompiles to request randomness which is returned at a later time.