Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions auction/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
serde = { workspace = true, optional = true }

frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
sp-runtime = { workspace = true }
Expand All @@ -26,6 +27,7 @@ sp-io = { workspace = true, features = ["std"] }
[features]
default = [ "std" ]
std = [
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"orml-traits/std",
Expand All @@ -35,6 +37,12 @@ std = [
"sp-runtime/std",
"sp-std/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
Expand Down
108 changes: 108 additions & 0 deletions auction/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
pub use crate::*;

use frame_benchmarking::v2::*;
use frame_support::assert_ok;
use frame_system::{EventRecord, RawOrigin};

/// Helper trait for benchmarking.
pub trait BenchmarkHelper<BlockNumber, AccountId, Balance> {
fn setup_bid() -> Option<(AccountId, Balance)>;
fn setup_on_finalize(rand: u32) -> Option<BlockNumber>;
}

impl<BlockNumber, AccountId, Balance> BenchmarkHelper<BlockNumber, AccountId, Balance> for () {
fn setup_bid() -> Option<(AccountId, Balance)> {
None
}
fn setup_on_finalize(_rand: u32) -> Option<BlockNumber> {
None
}
}

pub struct BaseBenchmarkHelper<T>(sp_std::marker::PhantomData<T>);

impl<T: Config> BenchmarkHelper<BlockNumberFor<T>, T::AccountId, T::Balance> for BaseBenchmarkHelper<T> {
fn setup_bid() -> Option<(T::AccountId, T::Balance)> {
let end_block: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number() + 10u32.into();
assert_ok!(Pallet::<T>::new_auction(
frame_system::Pallet::<T>::block_number(),
Some(end_block),
));

let auction_id: T::AuctionId = 0u32.into();
let pre_bidder: T::AccountId = account("pre_bidder", 0, 0);
let pre_bid_price: T::Balance = 10_000u32.into();

assert_ok!(Pallet::<T>::bid(
RawOrigin::Signed(pre_bidder).into(),
auction_id,
pre_bid_price
));

let bidder: T::AccountId = account("bidder", 0, 0);
let bid_price: T::Balance = 20_000u32.into();

Some((bidder, bid_price))
}

fn setup_on_finalize(rand: u32) -> Option<BlockNumberFor<T>> {
let end_block: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number() + 10u32.into();

for _auction_id in 0..rand {
assert_ok!(Pallet::<T>::new_auction(
frame_system::Pallet::<T>::block_number(),
Some(end_block),
));
}
Some(end_block)
}
}

fn assert_last_event<T: Config>(generic_event: <T as frame_system::Config>::RuntimeEvent) {
let events = frame_system::Pallet::<T>::events();
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
// compare to the last event record
let EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}

#[benchmarks]
mod benchmarks {
use super::*;

// `bid` a collateral auction, worst cases:
// there's bidder before and bid price will exceed target amount
#[benchmark]
fn bid() {
let auction_id: T::AuctionId = 0u32.into();
let (bidder, bid_price) = T::BenchmarkHelper::setup_bid().unwrap();

#[extrinsic_call]
_(RawOrigin::Signed(bidder.clone()), auction_id, bid_price);

assert_last_event::<T>(
Event::Bid {
auction_id,
bidder: bidder,
amount: bid_price,
}
.into(),
);
}

#[benchmark]
fn on_finalize(c: Liner<1, 100>) {
let end_block = T::BenchmarkHelper::setup_on_finalize(c).unwrap();

#[block]
{
Pallet::<T>::on_finalize(end_block);
}
}

impl_benchmark_test_suite! {
Pallet,
crate::mock::ExtBuilder::default().build(),
crate::mock::Runtime,
}
}
9 changes: 8 additions & 1 deletion auction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ use sp_runtime::{
DispatchError, DispatchResult,
};

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

#[cfg(feature = "runtime-benchmarks")]
pub use benchmarking::{BaseBenchmarkHelper, BenchmarkHelper};
pub use module::*;
pub use weights::WeightInfo;

Expand Down Expand Up @@ -61,6 +65,9 @@ pub mod module {

/// Weight information for extrinsics in this module.
type WeightInfo: WeightInfo;

#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper: BenchmarkHelper<BlockNumberFor<Self>, Self::AccountId, Self::Balance>;
}

#[pallet::error]
Expand Down Expand Up @@ -130,7 +137,7 @@ pub mod module {
/// The dispatch origin for this call must be `Signed` by the
/// transactor.
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::bid_collateral_auction())]
#[pallet::weight(T::WeightInfo::bid())]
pub fn bid(origin: OriginFor<T>, id: T::AuctionId, #[pallet::compact] value: T::Balance) -> DispatchResult {
let from = ensure_signed(origin)?;

Expand Down
7 changes: 4 additions & 3 deletions auction/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ impl AuctionHandler<AccountId, Balance, BlockNumber, AuctionId> for Handler {
now: BlockNumber,
_id: AuctionId,
new_bid: (AccountId, Balance),
_last_bid: Option<(AccountId, Balance)>,
last_bid: Option<(AccountId, Balance)>,
) -> OnNewBidResult<BlockNumber> {
if new_bid.0 == ALICE {
if last_bid.is_none() || last_bid.unwrap().0 != new_bid.0 {
OnNewBidResult {
accept_bid: true,
auction_end_change: Change::NewValue(Some(now + BID_EXTEND_BLOCK)),
Expand All @@ -52,6 +52,8 @@ impl Config for Runtime {
type AuctionId = AuctionId;
type Handler = Handler;
type WeightInfo = ();
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = BaseBenchmarkHelper<Runtime>;
}

type Block = frame_system::mocking::MockBlock<Runtime>;
Expand All @@ -64,7 +66,6 @@ construct_runtime!(
);

pub const ALICE: AccountId = 1;
pub const BOB: AccountId = 2;
pub const BID_EXTEND_BLOCK: BlockNumber = 10;

pub struct ExtBuilder;
Expand Down
3 changes: 2 additions & 1 deletion auction/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ fn bid_should_fail() {
AuctionModule::bid(RuntimeOrigin::signed(ALICE), 0, 20),
Error::<Runtime>::AuctionNotStarted
);
assert_ok!(AuctionModule::bid(RuntimeOrigin::signed(ALICE), 1, 20));
assert_noop!(
AuctionModule::bid(RuntimeOrigin::signed(BOB), 1, 20),
AuctionModule::bid(RuntimeOrigin::signed(ALICE), 1, 30),
Error::<Runtime>::BidNotAccepted,
);
assert_noop!(
Expand Down
4 changes: 2 additions & 2 deletions auction/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ use sp_std::marker::PhantomData;

/// Weight functions needed for orml_auction.
pub trait WeightInfo {
fn bid_collateral_auction() -> Weight;
fn bid() -> Weight;
fn on_finalize(c: u32, ) -> Weight;
}

/// Default weights.
impl WeightInfo for () {
fn bid_collateral_auction() -> Weight {
fn bid() -> Weight {
Weight::from_parts(108_000_000, 0)
.saturating_add(RocksDbWeight::get().reads(8 as u64))
.saturating_add(RocksDbWeight::get().writes(9 as u64))
Expand Down
29 changes: 0 additions & 29 deletions currencies/src/default_weight.rs

This file was deleted.

27 changes: 0 additions & 27 deletions vesting/src/default_weight.rs

This file was deleted.