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
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.

127 changes: 77 additions & 50 deletions pallets/salp/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

// 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_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_support::assert_ok;
use frame_system::RawOrigin;
use sp_runtime::traits::Bounded;
use node_primitives::ParaId;
use sp_runtime::{traits::Bounded, SaturatedConversion};
use sp_std::prelude::*;

pub use crate::{Pallet as Salp, *};
Expand All @@ -40,75 +41,101 @@ fn create_fund<T: Config>(id: u32) -> ParaId {
let last_period = (7 as u32).into();
let para_id = id;

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

assert_ok!(<T as Config>::MultiCurrency::deposit(
<T as Config>::DepositToken::get(),
&caller,
T::SubmissionDeposit::get()
));

assert_ok!(Salp::<T>::create(
RawOrigin::Signed(caller).into(),
para_id,
cap,
first_period,
last_period,
));
assert_ok!(Salp::<T>::create(RawOrigin::Root.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();

let value = T::MinContribution::get();
assert_ok!(Salp::<T>::set_balance(who, value));
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();

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

<T as Config>::MultiCurrency::deposit(
<T as Config>::DepositToken::get(),
&caller,
T::SubmissionDeposit::get(),
)?;

}: _(RawOrigin::Signed(caller), para_id, cap, first_period, last_period)
let contribution = T::MinContribution::get();
assert_ok!(Salp::<T>::set_balance(&caller, contribution));
}: _(RawOrigin::Signed(caller.clone()), fund_index, contribution)
verify {
assert_last_event::<T>(Event::<T>::Created(para_id).into())
let fund = Salp::<T>::funds(fund_index).unwrap();
let (_, status) = Salp::<T>::contribution(fund.trie_index, &caller);
assert_eq!(status, ContributionStatus::Contributing(contribution));
}

contribute {
refund {
let fund_index = create_fund::<T>(1);
let caller: T::AccountId = whitelisted_caller();
let caller_origin: T::Origin = RawOrigin::Signed(caller.clone()).into();
let contribution = T::MinContribution::get();

}: _(RawOrigin::Signed(caller.clone()), fund_index, contribution)
contribute_fund::<T>(&caller,fund_index);
assert_ok!(Salp::<T>::confirm_contribute(
RawOrigin::Root.into(),
caller.clone(),
fund_index,
true,
[0; 32]
));
assert_ok!(Salp::<T>::fund_fail(RawOrigin::Root.into(), fund_index));
assert_ok!(Salp::<T>::withdraw(RawOrigin::Root.into(), fund_index));
assert_eq!(Salp::<T>::refund_pool(), T::MinContribution::get());
let fund = Salp::<T>::funds(fund_index).unwrap();
let (_, status) = Salp::<T>::contribution(fund.trie_index, &caller);
assert_eq!(status, ContributionStatus::Idle);
}: _(RawOrigin::Signed(caller.clone()), fund_index)
verify {
assert_last_event::<T>(Event::<T>::Contributing(caller, fund_index, contribution).into());
assert_eq!(Salp::<T>::refund_pool(), 0_u32.saturated_into());
let (_, status) = Salp::<T>::contribution(fund.trie_index, &caller);
assert_eq!(status, ContributionStatus::Refunded);
assert_last_event::<T>(Event::<T>::Refunded(caller.clone(), 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();
unlock {
let fund_index = create_fund::<T>(1);
let caller: T::AccountId = whitelisted_caller();
let caller_origin: T::Origin = RawOrigin::Signed(caller.clone()).into();
let contribution = T::MinContribution::get();
contribute_fund::<T>(&caller,fund_index);
assert_ok!(Salp::<T>::confirm_contribute(
RawOrigin::Root.into(),
caller.clone(),
fund_index,
true,
[0; 32]
));
assert_ok!(Salp::<T>::fund_success(RawOrigin::Root.into(), fund_index));
}: _(RawOrigin::Root, caller.clone(),fund_index)
verify {
let fund = Salp::<T>::funds(fund_index).unwrap();
let (_, status) = Salp::<T>::contribution(fund.trie_index, &caller);
assert_eq!(status, ContributionStatus::Unlocked);
}

Salp::<T>::contribute(RawOrigin::Signed(contributor).into(), fund_index, contribution)?;
}
}: {
Salp::<T>::on_finalize(end_block);
redeem {
let fund_index = create_fund::<T>(1);
let caller: T::AccountId = whitelisted_caller();
let caller_origin: T::Origin = RawOrigin::Signed(caller.clone()).into();
let contribution = T::MinContribution::get();
contribute_fund::<T>(&caller,fund_index);
assert_ok!(Salp::<T>::confirm_contribute(
RawOrigin::Root.into(),
caller.clone(),
fund_index,
true,
[0; 32]
));
assert_ok!(Salp::<T>::fund_success(RawOrigin::Root.into(), fund_index));
assert_ok!(Salp::<T>::unlock(RawOrigin::Root.into(), caller.clone(), fund_index));
assert_ok!(Salp::<T>::fund_retire(RawOrigin::Root.into(), fund_index));
assert_ok!(Salp::<T>::withdraw(RawOrigin::Root.into(), fund_index));
assert_eq!(Salp::<T>::redeem_pool(), T::MinContribution::get());
}: _(RawOrigin::Signed(caller.clone()), fund_index,contribution)
verify {
assert_eq!(Salp::<T>::redeem_pool(), 0_u32.saturated_into());
assert_last_event::<T>(Event::<T>::Redeemed(caller.clone(), fund_index, (0 as u32).into(),(7 as u32).into(),contribution).into())
}
}

Expand Down
20 changes: 10 additions & 10 deletions pallets/salp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub mod migration {
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;
#[cfg(test)]
mod mock;
pub mod mock;
#[cfg(test)]
mod tests;

Expand All @@ -40,14 +40,10 @@ pub use pallet::*;
use sp_std::convert::TryFrom;

pub trait WeightInfo {
fn create() -> Weight;
fn contribute() -> Weight;
fn unlock() -> Weight;
fn withdraw() -> Weight;
fn refund() -> Weight;
fn redeem() -> Weight;
fn dissolve(n: u32) -> Weight;
fn on_initialize(n: u32) -> Weight;
}

#[allow(type_alias_bounds)]
Expand Down Expand Up @@ -99,9 +95,10 @@ pub struct FundInfo<Balance, LeasePeriod> {
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, Copy)]
pub enum ContributionStatus<BalanceOf> {
Idle,
Contributing(BalanceOf),
Refunded,
Unlocked,
Contributing(BalanceOf),
Redeemed,
}

impl<BalanceOf> ContributionStatus<BalanceOf>
Expand Down Expand Up @@ -566,7 +563,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(T::WeightInfo::contribute())]
#[pallet::weight(T::BifrostXcmExecutor::transact_weight(T::ContributionWeight::get(),0 as u32) + T::WeightInfo::contribute())]
#[transactional]
pub fn contribute(
origin: OriginFor<T>,
Expand Down Expand Up @@ -822,6 +819,7 @@ pub mod pallet {
value,
)?;
}
Self::put_contribution(fund.trie_index, &who, value, ContributionStatus::Refunded);
Self::deposit_event(Event::Redeemed(
who,
index,
Expand Down Expand Up @@ -947,9 +945,7 @@ pub mod pallet {
}
}
}
<T as Config>::WeightInfo::on_initialize(n)

// TODO: Auto unlock vsToken/vsBond?
T::BaseXcmWeight::get()
}
}

Expand Down Expand Up @@ -1073,6 +1069,10 @@ pub mod pallet {
who.using_encoded(|b| child::kill(&Self::id_from_index(index), b));
}

pub(crate) fn set_balance(who: &AccountIdOf<T>, value: BalanceOf<T>) -> DispatchResult {
T::MultiCurrency::deposit(T::RelayChainToken::get(), who, value)
}

fn xcm_ump_contribute(
_origin: OriginFor<T>,
index: ParaId,
Expand Down
24 changes: 2 additions & 22 deletions pallets/salp/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,8 @@ impl EnsureOrigin<Origin> for EnsureConfirmAsMultiSig {

fn try_origin(o: Origin) -> Result<Self::Success, Origin> {
Into::<Result<RawOrigin<AccountId>, Origin>>::into(o).and_then(|o| match o {
RawOrigin::Signed(who) =>
if who == PrimaryAccount::get() || who == ConfirmMuitiSigAccount::get() {
Ok(who)
} else {
Err(Origin::from(Some(who)))
},
RawOrigin::Signed(who) => Ok(who),
RawOrigin::Root => Ok(Default::default()),
r => Err(Origin::from(r)),
})
}
Expand Down Expand Up @@ -317,10 +313,6 @@ impl salp::Config for Test {

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

fn contribute() -> Weight {
0
}
Expand All @@ -329,25 +321,13 @@ impl WeightInfo for SalpWeightInfo {
0
}

fn withdraw() -> Weight {
0
}

fn redeem() -> Weight {
0
}

fn refund() -> Weight {
0
}

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

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

// To control the result returned by `MockXcmExecutor`
Expand Down
24 changes: 1 addition & 23 deletions pallets/salp/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ fn create_fund_should_work() {
#[test]
fn create_fund_with_wrong_origin_should_fail() {
new_test_ext().execute_with(|| {
assert_noop!(
Salp::create(Origin::root(), 3_000, 1_000, 1, SlotLength::get()),
DispatchError::BadOrigin,
);

assert_noop!(
Salp::create(Origin::none(), 3_000, 1_000, 1, SlotLength::get()),
DispatchError::BadOrigin,
Expand Down Expand Up @@ -95,9 +90,7 @@ fn set_fund_success_should_work() {
fn set_fund_success_with_wrong_origin_should_fail() {
new_test_ext().execute_with(|| {
assert_ok!(Salp::create(Some(ALICE).into(), 3_000, 1_000, 1, SlotLength::get()));
assert_noop!(Salp::fund_success(Origin::root(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::fund_success(Origin::none(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::fund_success(Some(BRUCE).into(), 3_000), DispatchError::BadOrigin);
})
}

Expand Down Expand Up @@ -137,9 +130,7 @@ fn set_fund_fail_should_work() {
fn set_fund_fail_with_wrong_origin_should_fail() {
new_test_ext().execute_with(|| {
assert_ok!(Salp::create(Some(ALICE).into(), 3_000, 1_000, 1, SlotLength::get()));
assert_noop!(Salp::fund_fail(Origin::root(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::fund_fail(Origin::none(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::fund_fail(Some(BRUCE).into(), 3_000), DispatchError::BadOrigin);
});
}

Expand Down Expand Up @@ -178,9 +169,7 @@ fn set_fund_retire_with_wrong_origin_should_fail() {
new_test_ext().execute_with(|| {
assert_ok!(Salp::create(Some(ALICE).into(), 3_000, 1_000, 1, SlotLength::get()));
assert_ok!(Salp::fund_success(Some(ALICE).into(), 3_000));
assert_noop!(Salp::fund_retire(Origin::root(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::fund_retire(Origin::none(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::fund_retire(Some(BRUCE).into(), 3_000), DispatchError::BadOrigin);
});
}

Expand Down Expand Up @@ -227,9 +216,7 @@ fn set_fund_end_with_wrong_origin_should_fail() {
assert_ok!(Salp::fund_retire(Some(ALICE).into(), 3_000));
assert_ok!(Salp::withdraw(Some(ALICE).into(), 3_000));

assert_noop!(Salp::fund_end(Origin::root(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::fund_end(Origin::none(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::fund_end(Some(BRUCE).into(), 3_000), DispatchError::BadOrigin);
});
}

Expand Down Expand Up @@ -492,21 +479,16 @@ fn confirm_contribute_later_should_work() {
fn contribute_with_wrong_origin_should_fail() {
new_test_ext().execute_with(|| {
assert_ok!(Salp::create(Some(ALICE).into(), 3_000, 1_000, 1, SlotLength::get()));
assert_noop!(Salp::contribute(Origin::root(), 3_000, 100), DispatchError::BadOrigin);
assert_noop!(Salp::contribute(Origin::none(), 3_000, 100), DispatchError::BadOrigin);

assert_noop!(
Salp::confirm_contribute(Origin::root(), BRUCE, 3000, true, CONTRIBUTON_INDEX),
DispatchError::BadOrigin,
Error::<Test>::InvalidContributionStatus,
);
assert_noop!(
Salp::confirm_contribute(Origin::none(), BRUCE, 3000, true, CONTRIBUTON_INDEX),
DispatchError::BadOrigin,
);
assert_noop!(
Salp::confirm_contribute(Some(BRUCE).into(), BRUCE, 3000, true, CONTRIBUTON_INDEX),
DispatchError::BadOrigin,
);
});
}

Expand Down Expand Up @@ -732,9 +714,7 @@ fn withdraw_with_wrong_origin_should_fail() {
assert_ok!(Salp::fund_success(Some(ALICE).into(), 3_000));
assert_ok!(Salp::fund_retire(Some(ALICE).into(), 3_000));

assert_noop!(Salp::withdraw(Origin::root(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::withdraw(Origin::none(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::withdraw(Some(BRUCE).into(), 3_000), DispatchError::BadOrigin);
});
}

Expand Down Expand Up @@ -1022,9 +1002,7 @@ fn dissolve_with_wrong_origin_should_fail() {
assert_ok!(Salp::withdraw(Some(ALICE).into(), 3_000));
assert_ok!(Salp::fund_end(Some(ALICE).into(), 3_000));

assert_noop!(Salp::dissolve(Origin::root(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::dissolve(Origin::none(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::dissolve(Some(BRUCE).into(), 3_000), DispatchError::BadOrigin);
});
}

Expand Down
Loading