Skip to content

Commit

Permalink
add benchmarking for pallet_oracle_feed
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Dec 5, 2023
1 parent a1dd284 commit 0ffeb48
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 14 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.

1 change: 1 addition & 0 deletions pallets/oracle-data-collection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ pub mod types {
storage::{bounded_btree_map::BoundedBTreeMap, bounded_vec::BoundedVec},
};
use sp_runtime::{traits::Zero, RuntimeDebug};
use sp_std::vec::Vec;

use crate::pallet::{Config, Error};

Expand Down
12 changes: 12 additions & 0 deletions pallets/oracle-feed/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ sp-runtime = { workspace = true }

cfg-traits = { workspace = true }

# Optionals for benchmarking
frame-benchmarking = { workspace = true, optional = true }

[dev-dependencies]
cfg-mocks = { workspace = true, features = ["std"] }

Expand All @@ -32,6 +35,15 @@ std = [
"scale-info/std",
"frame-support/std",
"frame-system/std",
"frame-benchmarking?/std",
"sp-runtime/std",
"cfg-traits/std",
]
runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"frame-benchmarking/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"cfg-traits/runtime-benchmarks",
"cfg-mocks/runtime-benchmarks",
]
47 changes: 47 additions & 0 deletions pallets/oracle-feed/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use frame_benchmarking::{v2::*, whitelisted_caller};
use frame_system::RawOrigin;

use crate::pallet::{Call, Config, Pallet};

#[benchmarks(
where
T::OracleKey: From<u32>,
T::OracleValue: Default,
)]
mod benchmarks {
use super::*;

#[benchmark]
fn feed_first() -> Result<(), BenchmarkError> {
let feeder: T::AccountId = whitelisted_caller();

#[extrinsic_call]
feed(
RawOrigin::Signed(feeder),
1.into(),
T::OracleValue::default(),
);

Ok(())
}

#[benchmark]
fn feed_again() -> Result<(), BenchmarkError> {
let feeder: T::AccountId = whitelisted_caller();

Pallet::<T>::feed(
RawOrigin::Signed(feeder.clone()).into(),
1.into(),
T::OracleValue::default(),
)?;

#[extrinsic_call]
feed(
RawOrigin::Signed(feeder),
1.into(),
T::OracleValue::default(),
);

Ok(())
}
}
46 changes: 32 additions & 14 deletions pallets/oracle-feed/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@ mod mock;
#[cfg(test)]
mod tests;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

pub mod weights;

pub use pallet::*;
pub use weights::WeightInfo;

#[frame_support::pallet]
pub mod pallet {
use cfg_traits::{fees::PayFee, ValueProvider};
use frame_support::{pallet_prelude::*, traits::Time};
use frame_system::pallet_prelude::*;

use crate::weights::WeightInfo;

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

type MomentOf<T> = <<T as Config>::Time as Time>::Moment;
Expand All @@ -56,6 +64,9 @@ pub mod pallet {

/// Fee for the first time a feeder feeds a value
type FirstValuePayFee: PayFee<Self::AccountId>;

/// The weight information for this pallet extrinsics.
type WeightInfo: WeightInfo;
}

/// Store all oracle values indexed by feeder
Expand Down Expand Up @@ -90,32 +101,39 @@ pub mod pallet {
/// Permissionles call to feed an oracle key from a source with value.
/// The first time the value is set, an extra fee is required for the
/// feeder.
#[pallet::weight(1_000_000)]
#[pallet::weight(T::WeightInfo::feed_first())]
#[pallet::call_index(0)]
pub fn feed(
origin: OriginFor<T>,
key: T::OracleKey,
value: T::OracleValue,
) -> DispatchResult {
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;

FedValues::<T>::mutate(&who, key, |prev_value| {
if prev_value.is_none() {
T::FirstValuePayFee::pay(&who)?;
}
let new_weight = match prev_value {
None => {
T::FirstValuePayFee::pay(&who)?;
// The weight used is the predefined one.
None
}
Some(_) => {
// The weight used is less than the predefined,
// because we do not need to pay an extra fee
Some(T::WeightInfo::feed_again())
}
};

*prev_value = Some((value, T::Time::now()));

Ok::<_, DispatchError>(())
})?;

Self::deposit_event(Event::<T>::Fed {
account_id: who,
key,
value,
});
Self::deposit_event(Event::<T>::Fed {
account_id: who.clone(),
key,
value,
});

Ok(())
Ok(new_weight.into())
})
}
}

Expand Down
1 change: 1 addition & 0 deletions pallets/oracle-feed/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl pallet_oracle_feed::Config for Runtime {
type OracleValue = OracleValue;
type RuntimeEvent = RuntimeEvent;
type Time = MockTime;
type WeightInfo = ();
}

pub fn new_test_ext() -> TestExternalities {
Expand Down
29 changes: 29 additions & 0 deletions pallets/oracle-feed/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2021 Centrifuge Foundation (centrifuge.io).
// This file is part of Centrifuge chain project.

// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).

// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use frame_support::weights::Weight;

pub trait WeightInfo {
fn feed_first() -> Weight;
fn feed_again() -> Weight;
}

impl WeightInfo for () {
fn feed_first() -> Weight {
Weight::zero()
}

fn feed_again() -> Weight {
Weight::zero()
}
}
4 changes: 4 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ std = [
"pallet-multisig/std",
"pallet-nft/std",
"pallet-nft-sales/std",
"pallet-oracle-feed/std",
"pallet-oracle-data-collection/std",
"pallet-order-book/std",
"pallet-permissions/std",
"pallet-pool-registry/std",
Expand Down Expand Up @@ -277,6 +279,8 @@ runtime-benchmarks = [
"pallet-multisig/runtime-benchmarks",
"pallet-nft/runtime-benchmarks",
"pallet-nft-sales/runtime-benchmarks",
"pallet-oracle-feed/runtime-benchmarks",
"pallet-oracle-data-collection/runtime-benchmarks",
"pallet-order-book/runtime-benchmarks",
"pallet-permissions/runtime-benchmarks",
"pallet-pool-registry/runtime-benchmarks",
Expand Down
4 changes: 4 additions & 0 deletions runtime/development/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ std = [
"pallet-multisig/std",
"pallet-nft/std",
"pallet-nft-sales/std",
"pallet-oracle-feed/std",
"pallet-oracle-data-collection/std",
"pallet-order-book/std",
"pallet-permissions/std",
"pallet-pool-registry/std",
Expand Down Expand Up @@ -331,6 +333,8 @@ runtime-benchmarks = [
"pallet-multisig/runtime-benchmarks",
"pallet-nft/runtime-benchmarks",
"pallet-nft-sales/runtime-benchmarks",
"pallet-oracle-feed/runtime-benchmarks",
"pallet-oracle-data-collection/runtime-benchmarks",
"pallet-order-book/runtime-benchmarks",
"pallet-permissions/runtime-benchmarks",
"pallet-pool-registry/runtime-benchmarks",
Expand Down
5 changes: 5 additions & 0 deletions runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,7 @@ impl pallet_oracle_feed::Config for Runtime {
type OracleValue = Quantity;
type RuntimeEvent = RuntimeEvent;
type Time = Timestamp;
type WeightInfo = weights::pallet_oracle_feed::SubstrateWeight<Self>;
}

impl pallet_oracle_data_collection::Config for Runtime {
Expand Down Expand Up @@ -2650,6 +2651,8 @@ impl_runtime_apis! {
add_benchmark!(params, batches, pallet_liquidity_pools, LiquidityPools);
add_benchmark!(params, batches, pallet_nft_sales, NftSales);
add_benchmark!(params, batches, pallet_investments, Investments);
add_benchmark!(params, batches, pallet_oracle_feed, OraclePriceFeed);
//add_benchmark!(params, batches, pallet_oracle_data_collection, OraclePriceCollection);

if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
Ok(batches)
Expand Down Expand Up @@ -2706,6 +2709,8 @@ impl_runtime_apis! {
list_benchmark!(list, extra, pallet_liquidity_pools, LiquidityPools);
list_benchmark!(list, extra, pallet_nft_sales, NftSales);
list_benchmark!(list, extra, pallet_investments, Investments);
list_benchmark!(list, extra, pallet_oracle_feed, OraclePriceFeed);
//list_benchmark!(list, extra, pallet_oracle_data_collection, OraclePriceCollection);

let storage_info = AllPalletsWithSystem::storage_info();

Expand Down
1 change: 1 addition & 0 deletions runtime/development/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub mod pallet_loans;
pub mod pallet_migration_manager;
pub mod pallet_multisig;
pub mod pallet_nft_sales;
pub mod pallet_oracle_feed;
pub mod pallet_order_book;
pub mod pallet_permissions;
pub mod pallet_pool_registry;
Expand Down
16 changes: 16 additions & 0 deletions runtime/development/src/weights/pallet_oracle_feed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// TODO: pending to regenerate

use core::marker::PhantomData;

use frame_support::weights::Weight;

pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_investments::WeightInfo for WeightInfo<T> {
fn feed_first() -> Weight {
Weight::zero()
}

fn feed_again() -> Weight {
Weight::zero()
}
}

0 comments on commit 0ffeb48

Please sign in to comment.