Skip to content

Commit

Permalink
Impl block prevrandao (#2736)
Browse files Browse the repository at this point in the history
* add Randomness

* change the randomness source from the relay chain

* update ts-tests

* remove difficulty

* Apply suggestions

* update evm-tests

* Apply suggestions

* Update comments
  • Loading branch information
zjb0807 authored Apr 19, 2024
1 parent 3fd4ab0 commit 4115cd5
Show file tree
Hide file tree
Showing 55 changed files with 46,532 additions and 24,465 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion evm-tests
14 changes: 9 additions & 5 deletions modules/asset-registry/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ use frame_support::{
traits::{ConstU128, ConstU32, ConstU64},
};
use frame_system::EnsureSignedBy;
use module_support::{mocks::MockAddressMapping, AddressMapping};
use module_support::{
mocks::{MockAddressMapping, TestRandomness},
AddressMapping,
};
use primitives::{
evm::convert_decimals_to_evm, evm::EvmAddress, AccountId, Balance, CurrencyId, ReserveIdentifier, TokenSymbol,
};
Expand Down Expand Up @@ -99,6 +102,7 @@ impl module_evm::Config for Runtime {

type Runner = module_evm::runner::stack::Runner<Self>;
type FindAuthor = ();
type Randomness = TestRandomness<Self>;
type Task = ();
type IdleScheduler = ();
type WeightInfo = ();
Expand Down Expand Up @@ -183,8 +187,8 @@ pub fn deploy_contracts() {
H256::from_slice(&buf).as_bytes().to_vec()
},
}],
used_gas: 1235455,
used_storage: 5131,
used_gas: 1215220,
used_storage: 4996,
}));
}

Expand Down Expand Up @@ -219,8 +223,8 @@ pub fn deploy_contracts_same_prefix() {
H256::from_slice(&buf).as_bytes().to_vec()
},
}],
used_gas: 1235455,
used_storage: 5131,
used_gas: 1215220,
used_storage: 4996,
}));
}

Expand Down
6 changes: 5 additions & 1 deletion modules/cdp-engine/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ use frame_support::{
PalletId,
};
use frame_system::EnsureSignedBy;
use module_support::{mocks::MockStableAsset, AuctionManager, EmergencyShutdown, SpecificJointsSwap};
use module_support::{
mocks::{MockStableAsset, TestRandomness},
AuctionManager, EmergencyShutdown, SpecificJointsSwap,
};
use orml_traits::parameter_type_with_key;
use primitives::{evm::convert_decimals_to_evm, DexShare, Moment, ReserveIdentifier, TokenSymbol, TradingPair};
use sp_core::crypto::AccountId32;
Expand Down Expand Up @@ -296,6 +299,7 @@ impl module_evm::Config for Runtime {

type Runner = module_evm::runner::stack::Runner<Self>;
type FindAuthor = ();
type Randomness = TestRandomness<Self>;
type Task = ();
type IdleScheduler = ();
type WeightInfo = ();
Expand Down
10 changes: 7 additions & 3 deletions modules/currencies/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ use frame_support::{
PalletId,
};
use frame_system::EnsureSignedBy;
use module_support::{mocks::MockAddressMapping, AddressMapping};
use module_support::{
mocks::{MockAddressMapping, TestRandomness},
AddressMapping,
};
use orml_traits::{currency::MutationHooks, parameter_type_with_key};
use primitives::{evm::convert_decimals_to_evm, CurrencyId, ReserveIdentifier, TokenSymbol};
use sp_core::H256;
Expand Down Expand Up @@ -175,6 +178,7 @@ impl module_evm::Config for Runtime {

type Runner = module_evm::runner::stack::Runner<Self>;
type FindAuthor = ();
type Randomness = TestRandomness<Self>;
type Task = ();
type IdleScheduler = ();
type WeightInfo = ();
Expand Down Expand Up @@ -286,8 +290,8 @@ pub fn deploy_contracts() {
H256::from_slice(&buf).as_bytes().to_vec()
},
}],
used_gas: 1235455,
used_storage: 5131,
used_gas: 1215220,
used_storage: 4996,
}));
}

Expand Down
36 changes: 18 additions & 18 deletions modules/currencies/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,11 +1299,11 @@ fn fungible_inspect_trait_should_work() {
// Test for Inspect::balance and Inspect::total_balance
assert_eq!(
<Currencies as fungibles::Inspect<_>>::balance(NATIVE_CURRENCY_ID, &alice()),
148690
150040
);
assert_eq!(
<Currencies as fungibles::Inspect<_>>::total_balance(NATIVE_CURRENCY_ID, &alice()),
148690
150040
);
assert_eq!(
<Currencies as fungibles::Inspect<_>>::balance(X_TOKEN_ID, &alice()),
Expand All @@ -1313,10 +1313,10 @@ fn fungible_inspect_trait_should_work() {
<Currencies as fungibles::Inspect<_>>::balance(CurrencyId::Erc20(erc20_address()), &alice()),
ALICE_BALANCE
);
assert_eq!(<NativeCurrency as fungible::Inspect<_>>::balance(&alice()), 148690);
assert_eq!(<NativeCurrency as fungible::Inspect<_>>::balance(&alice()), 150040);
assert_eq!(
<AdaptedBasicCurrency as fungible::Inspect<_>>::balance(&alice()),
148690
150040
);

// Test for Inspect::reducible_balance. No locks or reserves
Expand All @@ -1328,23 +1328,23 @@ fn fungible_inspect_trait_should_work() {
Preservation::Preserve,
Fortitude::Polite
),
148688
150038
);
assert_eq!(
<NativeCurrency as fungible::Inspect<_>>::reducible_balance(
&alice(),
Preservation::Preserve,
Fortitude::Polite
),
148688
150038
);
assert_eq!(
<AdaptedBasicCurrency as fungible::Inspect<_>>::reducible_balance(
&alice(),
Preservation::Preserve,
Fortitude::Polite
),
148688
150038
);
assert_eq!(
<Currencies as fungibles::Inspect<_>>::reducible_balance(
Expand Down Expand Up @@ -1374,7 +1374,7 @@ fn fungible_inspect_trait_should_work() {
Preservation::Expendable,
Fortitude::Polite
),
148690
150040
);
assert_eq!(
<Currencies as fungibles::Inspect<_>>::reducible_balance(
Expand All @@ -1400,15 +1400,15 @@ fn fungible_inspect_trait_should_work() {
Preservation::Expendable,
Fortitude::Polite
),
148690
150040
);
assert_eq!(
<AdaptedBasicCurrency as fungible::Inspect<_>>::reducible_balance(
&alice(),
Preservation::Expendable,
Fortitude::Polite
),
148690
150040
);

// Set some locks
Expand All @@ -1423,7 +1423,7 @@ fn fungible_inspect_trait_should_work() {
Preservation::Preserve,
Fortitude::Polite
),
147690
149040
);
assert_eq!(
<Currencies as fungibles::Inspect<_>>::reducible_balance(
Expand All @@ -1449,15 +1449,15 @@ fn fungible_inspect_trait_should_work() {
Preservation::Preserve,
Fortitude::Polite
),
147690
149040
);
assert_eq!(
<AdaptedBasicCurrency as fungible::Inspect<_>>::reducible_balance(
&alice(),
Preservation::Preserve,
Fortitude::Polite
),
147690
149040
);

assert_eq!(
Expand All @@ -1467,7 +1467,7 @@ fn fungible_inspect_trait_should_work() {
Preservation::Expendable,
Fortitude::Polite
),
147690
149040
);
assert_eq!(
<Currencies as fungibles::Inspect<_>>::reducible_balance(
Expand All @@ -1493,15 +1493,15 @@ fn fungible_inspect_trait_should_work() {
Preservation::Expendable,
Fortitude::Polite
),
147690
149040
);
assert_eq!(
<AdaptedBasicCurrency as fungible::Inspect<_>>::reducible_balance(
&alice(),
Preservation::Expendable,
Fortitude::Polite
),
147690
149040
);

// Test for Inspect::can_deposit
Expand Down Expand Up @@ -1626,11 +1626,11 @@ fn fungible_inspect_trait_should_work() {
);

assert_eq!(
<Currencies as fungibles::Inspect<_>>::can_withdraw(NATIVE_CURRENCY_ID, &alice(), 147690 + 1),
<Currencies as fungibles::Inspect<_>>::can_withdraw(NATIVE_CURRENCY_ID, &alice(), 149040 + 1),
WithdrawConsequence::Frozen
);
assert_eq!(
<AdaptedBasicCurrency as fungible::Inspect<_>>::can_withdraw(&alice(), 147690 + 1),
<AdaptedBasicCurrency as fungible::Inspect<_>>::can_withdraw(&alice(), 149040 + 1),
WithdrawConsequence::Frozen
);
assert_eq!(
Expand Down
18 changes: 11 additions & 7 deletions modules/evm-bridge/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ use frame_support::{
traits::{ConstU128, ConstU32, ConstU64},
};
use frame_system::EnsureSignedBy;
use module_support::{mocks::MockAddressMapping, AddressMapping};
use module_support::{
mocks::{MockAddressMapping, TestRandomness},
AddressMapping,
};
use primitives::{evm::convert_decimals_to_evm, evm::EvmAddress, ReserveIdentifier};
use sp_core::{crypto::AccountId32, H256};
use sp_runtime::{traits::IdentityLookup, BuildStorage};
Expand Down Expand Up @@ -104,6 +107,7 @@ impl module_evm::Config for Runtime {

type Runner = module_evm::runner::stack::Runner<Self>;
type FindAuthor = ();
type Randomness = TestRandomness<Self>;
type Task = ();
type IdleScheduler = ();
type WeightInfo = ();
Expand Down Expand Up @@ -185,8 +189,8 @@ pub fn deploy_contracts() {
H256::from_slice(&buf).as_bytes().to_vec()
},
}],
used_gas: 1235455,
used_storage: 5131,
used_gas: 1215220,
used_storage: 4996,
}));
}

Expand All @@ -207,8 +211,8 @@ pub fn deploy_liquidation_ok_contracts() {
from: alice_evm_addr(),
contract: erc20_address(),
logs: vec![],
used_gas: 235330,
used_storage: 844,
used_gas: 234056,
used_storage: 837,
}));
}

Expand All @@ -229,8 +233,8 @@ pub fn deploy_liquidation_err_contracts() {
from: alice_evm_addr(),
contract: erc20_address(),
logs: vec![],
used_gas: 228338,
used_storage: 818,
used_gas: 226860,
used_storage: 810,
}));
}

Expand Down
8 changes: 4 additions & 4 deletions modules/evm-bridge/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ fn liquidation_works() {
buf.to_vec()
},
}],
used_gas: 25083,
used_gas: 25061,
used_storage: 0,
}));
});
Expand Down Expand Up @@ -281,7 +281,7 @@ fn on_collateral_transfer_works() {
buf.to_vec()
},
}],
used_gas: 23573,
used_gas: 23560,
used_storage: 0,
}));
});
Expand Down Expand Up @@ -321,7 +321,7 @@ fn on_repayment_refund_works() {
buf.to_vec()
},
}],
used_gas: 23595,
used_gas: 23582,
used_storage: 0,
}));
});
Expand Down Expand Up @@ -428,7 +428,7 @@ fn tracing_should_work() {
"input": "0xa9059cbb00000000000000000000000010000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000064",
"value": "0x0",
"gas": 200000,
"gasUsed": 51929,
"gasUsed": 51897,
"output": "0x0000000000000000000000000000000000000000000000000000000000000001",
"error": null,
"revertReason": null,
Expand Down
3 changes: 2 additions & 1 deletion modules/evm/src/bench/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use frame_support::{
};
use frame_system::EnsureSignedBy;
use module_support::{
mocks::{MockAddressMapping, MockErc20InfoMapping},
mocks::{MockAddressMapping, MockErc20InfoMapping, TestRandomness},
DEXIncentives, Price, PriceProvider, SpecificJointsSwap,
};
use orml_traits::{parameter_type_with_key, MultiReservableCurrency};
Expand Down Expand Up @@ -196,6 +196,7 @@ impl Config for Runtime {

type Runner = crate::runner::stack::Runner<Self>;
type FindAuthor = AuthorGiven;
type Randomness = TestRandomness<Self>;
type Task = ScheduledTasks;
type IdleScheduler = IdleScheduler;
type WeightInfo = ();
Expand Down
10 changes: 9 additions & 1 deletion modules/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use frame_support::{
parameter_types,
traits::{
BalanceStatus, Currency, EitherOfDiverse, EnsureOrigin, ExistenceRequirement, FindAuthor, Get,
NamedReservableCurrency, OnKilledAccount,
NamedReservableCurrency, OnKilledAccount, Randomness,
},
transactional,
weights::Weight,
Expand Down Expand Up @@ -241,6 +241,9 @@ pub mod module {
/// Find author for the current block.
type FindAuthor: FindAuthor<Self::AccountId>;

/// Provides randomness in the runtime.
type Randomness: Randomness<Self::Hash, BlockNumberFor<Self>>;

/// Dispatchable tasks
type Task: DispatchableTask + FullCodec + Debug + Clone + PartialEq + TypeInfo + From<EvmTask<Self>>;

Expand Down Expand Up @@ -1904,6 +1907,11 @@ impl<T: Config> Pallet<T> {
}
});
}

fn get_randomness() -> H256 {
let (random, _block_number) = T::Randomness::random(&("EVM-Random").encode());
H256::from_slice(random.as_ref())
}
}

impl<T: Config> EVMTrait<T::AccountId> for Pallet<T> {
Expand Down
Loading

0 comments on commit 4115cd5

Please sign in to comment.