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
6 changes: 6 additions & 0 deletions Cargo.lock

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

10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,12 @@ copy-genesis-config-release:
.PHONY: format
format:
rustup component add rustfmt
cargo +nightly fmt --all -- --check
cargo +nightly fmt --all -- --check

.PHONY: test-benchmarking
test-benchmarking:
cargo test --features runtime-benchmarks --features with-all-runtime --features --all benchmarking

.PHONY: run-benchmarking
run-benchmarking:
./scripts/run_all_benches.sh
4 changes: 1 addition & 3 deletions node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ cli = [
"structopt",
"substrate-build-script-utils",
]
runtime-benchmarks = [
"frame-benchmarking-cli",
]
runtime-benchmarks = [ "node-service/runtime-benchmarks" ]
with-asgard-runtime = [
"node-service/with-asgard-runtime",
]
Expand Down
5 changes: 5 additions & 0 deletions node/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ sc-telemetry = { version = "3.0.0" }
sc-transaction-pool = { version = "3.0.0" }
sc-tracing = { version = "3.0.0" }
sc-service = { version = "0.9.0", default-features = false }
frame-benchmarking = { version = "3.0.0", default-features = false }

# Substrate Primitives
sp-consensus-aura = { version = "0.9.0" }
Expand Down Expand Up @@ -76,6 +77,10 @@ bifrost-charge-transaction-fee-rpc-runtime-api = { path = "../../pallets/charge-
[features]
default = [ "std" ]
std = []
runtime-benchmarks = [
"asgard-runtime/runtime-benchmarks",
"bifrost-runtime/runtime-benchmarks",
]
with-asgard-runtime = [
"asgard-runtime",
]
Expand Down
2 changes: 2 additions & 0 deletions node/service/src/collator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ native_executor_instance!(
pub AsgardExecutor,
asgard_runtime::api::dispatch,
asgard_runtime::native_version,
frame_benchmarking::benchmarking::HostFunctions,
);

#[cfg(feature = "with-bifrost-runtime")]
native_executor_instance!(
pub BifrostExecutor,
bifrost_runtime::api::dispatch,
bifrost_runtime::native_version,
frame_benchmarking::benchmarking::HostFunctions,
);

/// Starts a `ServiceBuilder` for a full service.
Expand Down
16 changes: 12 additions & 4 deletions pallets/salp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ log = { version = "0.4.14", default-features = false }
node-primitives = { path = "../../node/primitives", default-features = false }
frame-support = { version = "3.0.0", default-features = false }
frame-system = { version = "3.0.0", default-features = false }
frame-benchmarking = { version = "3.0.0", default-features = false, optional = true }
sp-std = { version = "3.0.0", default-features = false }
sp-runtime = { version = "3.0.0", default-features = false }
sp-arithmetic = { version = "3.0.0", default-features = false }
xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.8", default-features = false }
xcm-support = { path = "../../xcm-support", default-features = false }
xcm-builder = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.8",default-features = false }
pallet-xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.8",default-features = false }
orml-traits = { version = "0.4.1-dev", default-features = false }
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.8" }

Expand All @@ -23,8 +26,6 @@ sp-io = "3.0.0"
sp-core = "3.0.0"
orml-tokens = "0.4.1-dev"
bifrost-bancor = { path = "../../pallets/bancor" }
xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.8" }
xcm-builder = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.8" }

[features]
default = ["std"]
Expand All @@ -39,8 +40,15 @@ std = [
"sp-arithmetic/std",
"orml-traits/std",
"xcm/std",
"xcm-builder/std",
"xcm-support/std",
"polkadot-parachain/std",
]
with-asgard-runtime = ["std"]
with-bifrost-runtime = ["std"]

runtime-benchmarks = [
"frame-benchmarking",
"sp-runtime/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
]
111 changes: 111 additions & 0 deletions pallets/salp/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// This file is part of Bifrost.

// Copyright (C) 2019-2021 Liebi Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program 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.

// This program 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.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// Ensure we're `no_std` when compiling for Wasm.
#[cfg(feature = "runtime-benchmarks")]
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_support::assert_ok;
use frame_system::RawOrigin;
use sp_runtime::traits::Bounded;
use sp_std::prelude::*;

pub use crate::{Pallet as Salp, *};

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

fn create_fund<T: Config>(id: u32) -> ParaId {
let cap = BalanceOf::<T>::max_value();
let first_period = (0 as u32).into();
let last_period = (7 as u32).into();
let para_id = id;

let caller = account("fund_creator", id, 0);

assert_ok!(CurrencyOf::<T>::deposit(Salp::<T>::token(), &caller, T::SubmissionDeposit::get()));

assert_ok!(Salp::<T>::create(
RawOrigin::Signed(caller).into(),
para_id,
cap,
first_period,
last_period,
));

para_id
}

#[allow(dead_code)]
fn contribute_fund<T: Config>(who: &T::AccountId, index: ParaId) {
let value = T::SubmissionDeposit::get();

assert_ok!(Salp::<T>::contribute(RawOrigin::Signed(who.clone()).into(), index, value));
}

benchmarks! {
create {
let para_id = 1 as u32;
let cap = BalanceOf::<T>::max_value();
let first_period = 0u32.into();
let last_period = 3u32.into();

let caller: T::AccountId = whitelisted_caller();

CurrencyOf::<T>::deposit(
Salp::<T>::token(),
&caller,
T::SubmissionDeposit::get(),
)?;

}: _(RawOrigin::Signed(caller), para_id, cap, first_period, last_period)
verify {
assert_last_event::<T>(Event::<T>::Created(para_id).into())
}

contribute {
let fund_index = create_fund::<T>(1);
let caller: T::AccountId = whitelisted_caller();
let contribution = T::MinContribution::get();

}: _(RawOrigin::Signed(caller.clone()), fund_index, contribution)
verify {
assert_last_event::<T>(Event::<T>::Contributing(caller, fund_index, contribution).into());
}

on_finalize {
let end_block: T::BlockNumber = T::ReleaseCycle::get();
let n in 2 .. 100;

for i in 0 .. n {
let fund_index = create_fund::<T>(i);
let contributor: T::AccountId = account("contributor", i, 0);
let contribution = T::MinContribution::get() * (i + 1).into();

Salp::<T>::contribute(RawOrigin::Signed(contributor).into(), fund_index, contribution)?;
}
}: {
Salp::<T>::on_finalize(end_block);
}
}

impl_benchmark_test_suite!(Salp, crate::mock::new_test_ext(), crate::mock::Test);
51 changes: 41 additions & 10 deletions pallets/salp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]

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

// Re-export pallet items so that they can be accessed from the crate namespace.
Expand All @@ -30,11 +34,35 @@ pub use pallet::*;

type TrieIndex = u32;

pub trait WeightInfo {
fn create() -> Weight;
fn contribute() -> Weight;
fn on_finalize(n: u32) -> Weight;
}

pub struct TestWeightInfo;
impl WeightInfo for TestWeightInfo {
fn create() -> Weight {
0
}

fn contribute() -> Weight {
0
}

fn on_finalize(_n: u32) -> Weight {
0
}
}

#[allow(type_alias_bounds)]
pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;

#[allow(type_alias_bounds)]
type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
pub type CurrencyOf<T> = <T as Config>::MultiCurrency;

#[allow(type_alias_bounds)]
type BalanceOf<T> = <<T as Config>::MultiCurrency as MultiCurrency<AccountIdOf<T>>>::Balance;
pub type BalanceOf<T> = <<T as Config>::MultiCurrency as MultiCurrency<AccountIdOf<T>>>::Balance;

#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
pub enum FundStatus {
Expand Down Expand Up @@ -142,7 +170,7 @@ pub mod pallet {
PalletId,
};
use frame_system::pallet_prelude::*;
use node_primitives::{traits::BancorHandler, CurrencyId, LeasePeriod, ParaId, TokenSymbol};
use node_primitives::{traits::BancorHandler, CurrencyId, LeasePeriod, ParaId};
use orml_traits::{
currency::TransferAll, LockIdentifier, MultiCurrency, MultiCurrencyExtended,
MultiLockableCurrency, MultiReservableCurrency,
Expand Down Expand Up @@ -178,6 +206,9 @@ pub mod pallet {
#[pallet::constant]
type RelyChainToken: Get<CurrencyId>;

#[pallet::constant]
type DepositToken: Get<CurrencyId>;

/// The number of blocks over which a single period lasts.
#[pallet::constant]
type LeasePeriod: Get<BlockNumberFor<Self>>;
Expand Down Expand Up @@ -215,6 +246,9 @@ pub mod pallet {

#[pallet::constant]
type SlotLength: Get<LeasePeriod>;

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

#[pallet::pallet]
Expand Down Expand Up @@ -392,7 +426,7 @@ pub mod pallet {
}

/// Create a new crowdloaning campaign for a parachain slot deposit for the current auction.
#[pallet::weight(0)]
#[pallet::weight(T::WeightInfo::create())]
pub fn create(
origin: OriginFor<T>,
#[pallet::compact] index: ParaId,
Expand Down Expand Up @@ -438,7 +472,7 @@ pub mod pallet {
/// Contribute to a crowd sale. This will transfer some balance over to fund a parachain
/// slot. It will be withdrawable in two instances: the parachain becomes retired; or the
/// slot is unable to be purchased and the timeout expires.
#[pallet::weight(0)]
#[pallet::weight(T::WeightInfo::contribute())]
pub fn contribute(
origin: OriginFor<T>,
#[pallet::compact] index: ParaId,
Expand Down Expand Up @@ -849,11 +883,8 @@ pub mod pallet {
Ok(())
}

fn token() -> CurrencyId {
#[cfg(feature = "with-asgard-runtime")]
return CurrencyId::Token(TokenSymbol::ASG);
#[cfg(not(feature = "with-asgard-runtime"))]
return CurrencyId::Token(TokenSymbol::BNC);
pub fn token() -> CurrencyId {
T::DepositToken::get()
}

pub fn vstoken() -> CurrencyId {
Expand Down
Loading