From 8f2e8dcf33101d50f020479a7eb9810b248a3b83 Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Thu, 11 Jul 2024 21:51:04 +0200 Subject: [PATCH] prepare orml_oracle benchmarking --- primitives/src/oracle.rs | 9 ++-- runtime/astar/src/lib.rs | 13 +++--- runtime/shibuya/src/lib.rs | 81 +++++++++++++++------------------ runtime/shiden/src/lib.rs | 15 +++--- tests/integration/src/oracle.rs | 13 +++--- tests/integration/src/setup.rs | 4 +- 6 files changed, 64 insertions(+), 71 deletions(-) diff --git a/primitives/src/oracle.rs b/primitives/src/oracle.rs index 48a1289120..643075cb09 100644 --- a/primitives/src/oracle.rs +++ b/primitives/src/oracle.rs @@ -23,9 +23,10 @@ use sp_std::vec::Vec; /// Interface for fetching price of the native token. pub trait PriceProvider { /// Get the price of the native token. - fn average_price() -> CurrencyAmount; + fn average_price() -> Price; } +pub type Price = FixedU128; pub type CurrencyAmount = FixedU128; #[derive(Encode, Decode, MaxEncodedLen, Clone, Copy, Debug, PartialEq, Eq, TypeInfo)] @@ -34,10 +35,8 @@ pub enum CurrencyId { SDN, } -type TimestampedValue = orml_oracle::TimestampedValue< - CurrencyAmount, - <>::Time as Time>::Moment, ->; +type TimestampedValue = + orml_oracle::TimestampedValue>::Time as Time>::Moment>; /// A dummy implementation of `CombineData` trait that does nothing. pub struct DummyCombineData(PhantomData<(T, I)>); diff --git a/runtime/astar/src/lib.rs b/runtime/astar/src/lib.rs index 6f7e365fd3..eaeb291390 100644 --- a/runtime/astar/src/lib.rs +++ b/runtime/astar/src/lib.rs @@ -75,7 +75,7 @@ use astar_primitives::{ PeriodNumber, RankedTier, SmartContract, StandardTierSlots, }, evm::EvmRevertCodeHandler, - oracle::{CurrencyAmount, CurrencyId, DummyCombineData}, + oracle::{CurrencyId, DummyCombineData, Price}, xcm::AssetLocationIdConverter, Address, AssetId, BlockNumber, Hash, Header, Nonce, }; @@ -364,12 +364,12 @@ impl pallet_dapp_staking_v3::BenchmarkHelper, AccountId } } #[cfg(feature = "runtime-benchmarks")] -impl orml_oracle::BenchmarkHelper> +impl orml_oracle::BenchmarkHelper> for BenchmarkHelper { - fn get_currency_id_value_pairs() -> sp_runtime::BoundedVec<(CurrencyId, FixedU128), ConstU32<2>> - { - sp_runtime::BoundedVec::default() + fn get_currency_id_value_pairs() -> sp_runtime::BoundedVec<(CurrencyId, Price), ConstU32<2>> { + sp_runtime::BoundedVec::try_from(vec![(CurrencyId::ASTR, Price::from_rational(15, 100))]) + .expect("out of bounds") } } @@ -1119,7 +1119,7 @@ impl orml_oracle::Config for Runtime { type CombineData = DummyCombineData; type Time = Timestamp; type OracleKey = CurrencyId; - type OracleValue = CurrencyAmount; + type OracleValue = Price; type RootOperatorAccountId = RootOperatorAccountId; type Members = OracleMembership; type MaxHasDispatchedSize = ConstU32<8>; @@ -1327,6 +1327,7 @@ mod benches { [pallet_dynamic_evm_base_fee, DynamicEvmBaseFee] [xcm_benchmarks_generic, XcmGeneric] [xcm_benchmarks_fungible, XcmFungible] + [orml_oracle, Oracle] ); } diff --git a/runtime/shibuya/src/lib.rs b/runtime/shibuya/src/lib.rs index 7304dbae8d..2e733bb677 100644 --- a/runtime/shibuya/src/lib.rs +++ b/runtime/shibuya/src/lib.rs @@ -86,7 +86,7 @@ use astar_primitives::{ MainCouncilMembershipInst, MainTreasuryInst, OracleMembershipInst, TechnicalCommitteeCollectiveInst, TechnicalCommitteeMembershipInst, }, - oracle::{CurrencyAmount, CurrencyId, DummyCombineData}, + oracle::{CurrencyId, DummyCombineData, Price}, xcm::AssetLocationIdConverter, Address, AssetId, BlockNumber, Hash, Header, Nonce, }; @@ -415,10 +415,10 @@ impl pallet_preimage::Config for Runtime { } #[cfg(feature = "runtime-benchmarks")] -pub struct BenchmarkHelper(sp_std::marker::PhantomData<(SC, ACC)>); +pub struct DAppStakingBenchmarkHelper(sp_std::marker::PhantomData<(SC, ACC)>); #[cfg(feature = "runtime-benchmarks")] impl pallet_dapp_staking_v3::BenchmarkHelper, AccountId> - for BenchmarkHelper, AccountId> + for DAppStakingBenchmarkHelper, AccountId> { fn get_smart_contract(id: u32) -> SmartContract { let id_bytes = id.to_le_bytes(); @@ -434,15 +434,6 @@ impl pallet_dapp_staking_v3::BenchmarkHelper, AccountId .expect("Must succeed in test/benchmark environment."); } } -#[cfg(feature = "runtime-benchmarks")] -impl orml_oracle::BenchmarkHelper> - for BenchmarkHelper -{ - fn get_currency_id_value_pairs() -> sp_runtime::BoundedVec<(CurrencyId, FixedU128), ConstU32<2>> - { - sp_runtime::BoundedVec::default() - } -} pub struct AccountCheck; impl DappStakingAccountCheck for AccountCheck { @@ -483,7 +474,7 @@ impl pallet_dapp_staking_v3::Config for Runtime { type RankingEnabled = ConstBool; type WeightInfo = weights::pallet_dapp_staking_v3::SubstrateWeight; #[cfg(feature = "runtime-benchmarks")] - type BenchmarkHelper = BenchmarkHelper, AccountId>; + type BenchmarkHelper = DAppStakingBenchmarkHelper, AccountId>; } pub struct InflationPayoutPerBlock; @@ -1206,6 +1197,19 @@ impl pallet_price_aggregator::Config for Runtime { type WeightInfo = pallet_price_aggregator::weights::SubstrateWeight; } +#[cfg(feature = "runtime-benchmarks")] +pub struct OracleBenchmarkHelper; +#[cfg(feature = "runtime-benchmarks")] +impl orml_oracle::BenchmarkHelper> for OracleBenchmarkHelper { + fn get_currency_id_value_pairs() -> sp_runtime::BoundedVec<(CurrencyId, Price), ConstU32<2>> { + sp_runtime::BoundedVec::try_from(vec![ + (CurrencyId::ASTR, Price::from_rational(15, 100)), + (CurrencyId::ASTR, Price::from_rational(15, 100)), + ]) + .expect("out of bounds") + } +} + parameter_types! { // Cannot specify `Root` so need to do it like this, unfortunately. pub RootOperatorAccountId: AccountId = AccountId::from([0xffu8; 32]); @@ -1217,8 +1221,11 @@ impl orml_oracle::Config for Runtime { type CombineData = DummyCombineData; type Time = Timestamp; type OracleKey = CurrencyId; - type OracleValue = CurrencyAmount; + type OracleValue = Price; type RootOperatorAccountId = RootOperatorAccountId; + #[cfg(feature = "runtime-benchmarks")] + type Members = OracleMembershipWrapper; + #[cfg(not(feature = "runtime-benchmarks"))] type Members = OracleMembership; type MaxHasDispatchedSize = ConstU32<8>; type WeightInfo = oracle_benchmarks::weights::SubstrateWeight; @@ -1227,7 +1234,7 @@ impl orml_oracle::Config for Runtime { #[cfg(not(feature = "runtime-benchmarks"))] type MaxFeedValues = ConstU32<1>; #[cfg(feature = "runtime-benchmarks")] - type BenchmarkHelper = BenchmarkHelper, AccountId>; + type BenchmarkHelper = OracleBenchmarkHelper; } impl pallet_membership::Config for Runtime { @@ -1244,33 +1251,22 @@ impl pallet_membership::Config for Runtime { type WeightInfo = pallet_membership::weights::SubstrateWeight; } -// The oracle-benchmarks pallet should be removed once we uplift to high enough version -// (assumption is `polkadot-v1.10.0`) to have access to normal oracle pallet benchmarks). -// -// The pallet is stateless so in order to remove it, only code needs to be cleaned up. -pub struct DummyKeyPairValue; -impl Get<(CurrencyId, CurrencyAmount)> for DummyKeyPairValue { - fn get() -> (CurrencyId, CurrencyAmount) { - (CurrencyId::ASTR, CurrencyAmount::from_rational(15, 100)) - } -} -pub struct AddMemberBenchmark; -impl oracle_benchmarks::AddMember for AddMemberBenchmark { - fn add_member(account: AccountId) { - use frame_support::assert_ok; - use frame_system::RawOrigin; - assert_ok!( - pallet_membership::Pallet::::add_member( - RawOrigin::Root.into(), - account.into() - ) - ); +/// OracleMembership wrapper used by benchmarks +#[cfg(feature = "runtime-benchmarks")] +pub struct OracleMembershipWrapper; + +#[cfg(feature = "runtime-benchmarks")] +impl frame_support::traits::SortedMembers for OracleMembershipWrapper { + fn sorted_members() -> Vec { + OracleMembership::sorted_members() } -} -impl oracle_benchmarks::Config for Runtime { - type BenchmarkCurrencyIdValuePair = DummyKeyPairValue; - type AddMember = AddMemberBenchmark; + fn add(account: &AccountId) { + frame_support::assert_ok!(OracleMembership::add_member( + frame_system::RawOrigin::Root.into(), + account.to_owned().into() + )); + } } parameter_types! { @@ -1579,9 +1575,6 @@ construct_runtime!( Treasury: pallet_treasury:: = 107, CommunityTreasury: pallet_treasury:: = 108, CollectiveProxy: pallet_collective_proxy = 109, - - // Remove after benchmarks are available in orml_oracle - OracleBenchmarks: oracle_benchmarks = 251, } ); @@ -1721,8 +1714,8 @@ mod benches { [xcm_benchmarks_fungible, XcmFungible] [pallet_price_aggregator, PriceAggregator] [pallet_membership, OracleMembership] - [oracle_benchmarks, OracleBenchmarks] [pallet_collective_proxy, CollectiveProxy] + [orml_oracle, Oracle] ); } diff --git a/runtime/shiden/src/lib.rs b/runtime/shiden/src/lib.rs index 376b164406..5352d31ab9 100644 --- a/runtime/shiden/src/lib.rs +++ b/runtime/shiden/src/lib.rs @@ -75,7 +75,7 @@ use astar_primitives::{ }, evm::EvmRevertCodeHandler, governance::OracleMembershipInst, - oracle::{CurrencyAmount, CurrencyId, DummyCombineData}, + oracle::{CurrencyId, DummyCombineData, Price}, xcm::AssetLocationIdConverter, Address, AssetId, BlockNumber, Hash, Header, Nonce, }; @@ -389,12 +389,12 @@ impl pallet_dapp_staking_v3::BenchmarkHelper, AccountId } } #[cfg(feature = "runtime-benchmarks")] -impl orml_oracle::BenchmarkHelper> +impl orml_oracle::BenchmarkHelper> for BenchmarkHelper { - fn get_currency_id_value_pairs() -> sp_runtime::BoundedVec<(CurrencyId, FixedU128), ConstU32<2>> - { - sp_runtime::BoundedVec::default() + fn get_currency_id_value_pairs() -> sp_runtime::BoundedVec<(CurrencyId, Price), ConstU32<2>> { + sp_runtime::BoundedVec::try_from(vec![(CurrencyId::ASTR, Price::from_rational(15, 100))]) + .expect("out of bounds") } } @@ -407,7 +407,7 @@ impl DappStakingAccountCheck for AccountCheck { pub struct ShidenTierSlots; impl TierSlotsFunc for ShidenTierSlots { - fn number_of_slots(price: CurrencyAmount) -> u16 { + fn number_of_slots(price: Price) -> u16 { // According to the forum proposal, the original formula's factor is reduced from 1000x to 100x. let result: u64 = price.saturating_mul_int(100_u64).saturating_add(50); result.unique_saturated_into() @@ -1120,7 +1120,7 @@ impl orml_oracle::Config for Runtime { type CombineData = DummyCombineData; type Time = Timestamp; type OracleKey = CurrencyId; - type OracleValue = CurrencyAmount; + type OracleValue = Price; type RootOperatorAccountId = RootOperatorAccountId; type Members = OracleMembership; type MaxHasDispatchedSize = ConstU32<8>; @@ -1331,6 +1331,7 @@ mod benches { [xcm_benchmarks_fungible, XcmFungible] [pallet_price_aggregator, PriceAggregator] [pallet_membership, OracleMembership] + [orml_oracle, Oracle] ); } diff --git a/tests/integration/src/oracle.rs b/tests/integration/src/oracle.rs index 7215f27027..44b5249b09 100644 --- a/tests/integration/src/oracle.rs +++ b/tests/integration/src/oracle.rs @@ -18,7 +18,7 @@ use crate::setup::*; -use astar_primitives::oracle::{CurrencyAmount, PriceProvider}; +use astar_primitives::oracle::{Price, PriceProvider}; use pallet_price_aggregator::{IntermediateValueAggregator, ValueAggregator}; #[test] @@ -33,13 +33,13 @@ fn price_submission_works() { IntermediateValueAggregator::::put(ValueAggregator::new(limit_block)); // 1. Submit a price for a valid asset - the native currency - let price_1 = CurrencyAmount::from_rational(15, 100); + let price_1 = Price::from_rational(15, 100); assert_ok!(Oracle::feed_values( RuntimeOrigin::signed(ALICE.clone()), vec![(native_currency_id, price_1)].try_into().unwrap() )); - let price_2 = CurrencyAmount::from_rational(17, 100); + let price_2 = Price::from_rational(17, 100); assert_ok!(Oracle::feed_values( RuntimeOrigin::signed(BOB.clone()), vec![(native_currency_id, price_2)].try_into().unwrap() @@ -48,7 +48,7 @@ fn price_submission_works() { // 2. Advance a block, and check price aggregator intermediate state is as expected // (perhaps a bit detailed, but still good to check whether it's integrated) run_for_blocks(1); - let expected_average = (price_1 + price_2) * CurrencyAmount::from_rational(1, 2); + let expected_average = (price_1 + price_2) * Price::from_rational(1, 2); assert_eq!( IntermediateValueAggregator::::get().average(), expected_average @@ -57,7 +57,7 @@ fn price_submission_works() { // 3. Keep advancing blocks, adding new values only each other block, and verify the average is as expected at the end for i in System::block_number() + 1..limit_block { if i % 2 == 0 { - let step = CurrencyAmount::from_rational(i as u128 % 5, 100); + let step = Price::from_rational(i as u128 % 5, 100); assert_ok!(Oracle::feed_values( RuntimeOrigin::signed(ALICE.clone()), @@ -77,8 +77,7 @@ fn price_submission_works() { // 4. Execute limit block and verify state is updated as expected run_for_blocks(2); // Need to run on_finalize of the limit block - let expected_moving_average = - (expected_average + INIT_PRICE) * CurrencyAmount::from_rational(1, 2); + let expected_moving_average = (expected_average + INIT_PRICE) * Price::from_rational(1, 2); assert_eq!(PriceAggregator::average_price(), expected_moving_average); // 5. Run until next limit block without any transactions, don't expect any changes diff --git a/tests/integration/src/setup.rs b/tests/integration/src/setup.rs index 952fc612eb..a6d08bb828 100644 --- a/tests/integration/src/setup.rs +++ b/tests/integration/src/setup.rs @@ -40,7 +40,7 @@ pub use astar_primitives::{ CommunityCouncilMembershipInst, MainCouncilMembershipInst, OracleMembershipInst, TechnicalCommitteeMembershipInst, }, - oracle::CurrencyAmount, + oracle::Price, BlockNumber, }; @@ -135,7 +135,7 @@ pub const CAT: AccountId32 = AccountId32::new([3_u8; 32]); pub const INITIAL_AMOUNT: u128 = 100_000 * UNIT; -pub const INIT_PRICE: CurrencyAmount = CurrencyAmount::from_rational(1, 10); +pub const INIT_PRICE: Price = Price::from_rational(1, 10); pub type SystemError = frame_system::Error; pub use pallet_balances::Call as BalancesCall;