-
Notifications
You must be signed in to change notification settings - Fork 55
Implement prevrandao block_randomness in pallet-evm
#246
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
base: moonbeam-polkadot-stable2409
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ use sp_consensus_aura::sr25519::AuthorityId as AuraId; | |
| use sp_consensus_grandpa::{AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList}; | ||
| use sp_core::{ | ||
| crypto::{ByteArray, KeyTypeId}, | ||
| ConstU128, OpaqueMetadata, H160, H256, U256, | ||
| ConstU128, Hasher, OpaqueMetadata, H160, H256, U256, | ||
| }; | ||
| use sp_runtime::{ | ||
| create_runtime_str, generic, impl_opaque_keys, | ||
|
|
@@ -50,6 +50,7 @@ use sp_genesis_builder::PresetId; | |
| use fp_account::EthereumSignature; | ||
| use fp_evm::weight_per_gas; | ||
| use fp_rpc::TransactionStatus; | ||
| use frame_system::pallet_prelude::BlockNumberFor; | ||
| use pallet_ethereum::{Call::transact, PostLogContent, Transaction as EthereumTransaction}; | ||
| use pallet_evm::{ | ||
| Account as EVMAccount, EnsureAccountId20, FeeCalculator, IdentityAddressMapping, Runner, | ||
|
|
@@ -336,6 +337,26 @@ impl<F: FindAuthor<u32>> FindAuthor<H160> for FindAuthorTruncated<F> { | |
| } | ||
| } | ||
|
|
||
| /// WARNING! THIS IS JUST AN EXAMPLE AND SHOULD NOT BE USED AS A MEANS TO RETRIEVE SECURE RANDOMNESS | ||
| 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) | ||
| } | ||
| } | ||
|
Comment on lines
+341
to
+358
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example is dangerous and we should change it. Suggestion in a comment above.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could return a zero hash instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for coming back to this, could we append And also add a note saying |
||
|
|
||
| const BLOCK_GAS_LIMIT: u64 = 75_000_000; | ||
| const MAX_POV_SIZE: u64 = 5 * 1024 * 1024; | ||
| /// The maximum storage growth per block in bytes. | ||
|
|
@@ -374,6 +395,7 @@ impl pallet_evm::Config for Runtime { | |
| type GasLimitStorageGrowthRatio = GasLimitStorageGrowthRatio; | ||
| type Timestamp = Timestamp; | ||
| type WeightInfo = pallet_evm::weights::SubstrateWeight<Self>; | ||
| type RandomnessProvider = RandomnessProvider; | ||
| } | ||
|
|
||
| parameter_types! { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The runtime implementing this pallet may not what to enabled randomness. (For example, the template runtime which currently has a really dangerous example)
We can also put this type behind a
feature, or just allow the random result to beNone.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to avoid changing this type to
Optionbecause I wouldn't be able to pass the randomness pallet directly as an impl since it implements<Hash, ...>. I would have to wrap it.