Newer
Older
assert_ok!(Crowdloan::contribute(Origin::signed(1), 0, 10, Some(valid_signature_2)));
// Contributions appear in free balance of crowdloan
assert_eq!(Balances::free_balance(Crowdloan::fund_account_id(0)), 60);
// Contribution amount is correct
let fund = Crowdloan::funds(0).unwrap();
assert_eq!(fund.raised, 59);
});
}
#[test]
fn contribute_handles_basic_errors() {
new_test_ext().execute_with(|| {
// Cannot contribute to non-existing fund
assert_noop!(Crowdloan::contribute(Origin::signed(1), 0, 49, None), Error::<Test>::InvalidFundIndex);
// Cannot contribute below minimum contribution
assert_noop!(Crowdloan::contribute(Origin::signed(1), 0, 9, None), Error::<Test>::ContributionTooSmall);
assert_ok!(Crowdloan::create(Origin::signed(1), 1000, 1, 4, 9, None));
assert_ok!(Crowdloan::contribute(Origin::signed(1), 0, 101, None));
// Cannot contribute past the limit
assert_noop!(Crowdloan::contribute(Origin::signed(2), 0, 900, None), Error::<Test>::CapExceeded);
// Move past end date
run_to_block(10);
// Cannot contribute to ended fund
assert_noop!(Crowdloan::contribute(Origin::signed(1), 0, 49, None), Error::<Test>::ContributionPeriodOver);
});
}
#[test]
fn fix_deploy_data_works() {
new_test_ext().execute_with(|| {
assert_ok!(Crowdloan::create(Origin::signed(1), 1000, 1, 4, 9, None));
assert_eq!(Balances::free_balance(1), 999);
// Add deploy data
<Test as frame_system::Config>::Hash::default(),
asynchronous rob
committed
vec![0].into()
// Confirm deploy data is stored correctly
assert_eq!(
fund.deploy_data,
Some(DeployData {
code_hash: <Test as frame_system::Config>::Hash::default(),
code_size: 0,
asynchronous rob
committed
initial_head_data: vec![0].into(),
});
}
#[test]
fn fix_deploy_data_handles_basic_errors() {
new_test_ext().execute_with(|| {
assert_ok!(Crowdloan::create(Origin::signed(1), 1000, 1, 4, 9, None));
assert_eq!(Balances::free_balance(1), 999);
// Cannot set deploy data by non-owner
<Test as frame_system::Config>::Hash::default(),
asynchronous rob
committed
vec![0].into()),
Error::<Test>::InvalidOrigin
);
// Cannot set deploy data to an invalid index
<Test as frame_system::Config>::Hash::default(),
asynchronous rob
committed
vec![0].into()),
Error::<Test>::InvalidFundIndex
);
// Cannot set deploy data after it already has been set
<Test as frame_system::Config>::Hash::default(),
asynchronous rob
committed
vec![0].into(),
<Test as frame_system::Config>::Hash::default(),
asynchronous rob
committed
vec![1].into()),
Error::<Test>::ExistingDeployData
);
});
}
#[test]
fn onboard_works() {
new_test_ext().execute_with(|| {
thiolliere
committed
assert_ok!(Slots::new_auction(Origin::root(), 5, 1));
assert_ok!(Crowdloan::create(Origin::signed(1), 1000, 1, 4, 9, None));
assert_eq!(Balances::free_balance(1), 999);
// Add deploy data
<Test as frame_system::Config>::Hash::default(),
asynchronous rob
committed
vec![0].into(),
assert_ok!(Crowdloan::contribute(Origin::signed(2), 0, 1000, None));
run_to_block(10);
// Endings count incremented
// Onboard crowdloan
assert_ok!(Crowdloan::onboard(Origin::signed(1), 0, 0.into()));
let fund = Crowdloan::funds(0).unwrap();
// Crowdloan is now assigned a parachain id
assert_eq!(fund.parachain, Some(0.into()));
// This parachain is managed by Slots
assert_eq!(Slots::managed_ids(), vec![0.into()]);
});
}
#[test]
fn onboard_handles_basic_errors() {
new_test_ext().execute_with(|| {
thiolliere
committed
assert_ok!(Slots::new_auction(Origin::root(), 5, 1));
assert_ok!(Crowdloan::create(Origin::signed(1), 1000, 1, 4, 9, None));
assert_eq!(Balances::free_balance(1), 999);
assert_ok!(Crowdloan::contribute(Origin::signed(2), 0, 1000, None));
run_to_block(10);
// Cannot onboard invalid fund index
assert_noop!(Crowdloan::onboard(Origin::signed(1), 1, 0.into()), Error::<Test>::InvalidFundIndex);
// Cannot onboard crowdloan without deploy data
assert_noop!(Crowdloan::onboard(Origin::signed(1), 0, 0.into()), Error::<Test>::UnsetDeployData);
<Test as frame_system::Config>::Hash::default(),
asynchronous rob
committed
vec![0].into(),
));
// Cannot onboard fund with incorrect parachain id
assert_noop!(Crowdloan::onboard(Origin::signed(1), 0, 1.into()), SlotsError::<Test>::ParaNotOnboarding);
// Onboard crowdloan
assert_ok!(Crowdloan::onboard(Origin::signed(1), 0, 0.into()));
assert_noop!(Crowdloan::onboard(Origin::signed(1), 0, 0.into()), Error::<Test>::AlreadyOnboard);
});
}
#[test]
fn begin_retirement_works() {
new_test_ext().execute_with(|| {
thiolliere
committed
assert_ok!(Slots::new_auction(Origin::root(), 5, 1));
assert_ok!(Crowdloan::create(Origin::signed(1), 1000, 1, 4, 9, None));
assert_eq!(Balances::free_balance(1), 999);
// Add deploy data
<Test as frame_system::Config>::Hash::default(),
asynchronous rob
committed
vec![0].into(),
assert_ok!(Crowdloan::contribute(Origin::signed(2), 0, 1000, None));
// Onboard crowdloan
assert_ok!(Crowdloan::onboard(Origin::signed(1), 0, 0.into()));
assert_eq!(fund.parachain, Some(0.into()));
// Off-boarding is set to the crowdloan account
assert_eq!(Slots::offboarding(ParaId::from(0)), Crowdloan::fund_account_id(0));
// Retire crowdloan to remove parachain id
assert_ok!(Crowdloan::begin_retirement(Origin::signed(1), 0));
// Fund should no longer have parachain id
assert_eq!(fund.parachain, None);
});
}
#[test]
fn begin_retirement_handles_basic_errors() {
new_test_ext().execute_with(|| {
thiolliere
committed
assert_ok!(Slots::new_auction(Origin::root(), 5, 1));
assert_ok!(Crowdloan::create(Origin::signed(1), 1000, 1, 4, 9, None));
assert_eq!(Balances::free_balance(1), 999);
// Add deploy data
<Test as frame_system::Config>::Hash::default(),
asynchronous rob
committed
vec![0].into(),
assert_ok!(Crowdloan::contribute(Origin::signed(2), 0, 1000, None));
run_to_block(10);
// Cannot retire fund that is not onboarded
assert_noop!(Crowdloan::begin_retirement(Origin::signed(1), 0), Error::<Test>::NotParachain);
// Onboard crowdloan
assert_ok!(Crowdloan::onboard(Origin::signed(1), 0, 0.into()));
assert_eq!(fund.parachain, Some(0.into()));
// Cannot retire fund whose deposit has not been returned
assert_noop!(Crowdloan::begin_retirement(Origin::signed(1), 0), Error::<Test>::ParaHasDeposit);
run_to_block(50);
// Cannot retire invalid fund index
assert_noop!(Crowdloan::begin_retirement(Origin::signed(1), 1), Error::<Test>::InvalidFundIndex);
assert_ok!(Crowdloan::begin_retirement(Origin::signed(1), 0));
assert_noop!(Crowdloan::begin_retirement(Origin::signed(1), 0), Error::<Test>::NotParachain);
});
}
#[test]
fn withdraw_works() {
new_test_ext().execute_with(|| {
thiolliere
committed
assert_ok!(Slots::new_auction(Origin::root(), 5, 1));
assert_ok!(Crowdloan::create(Origin::signed(1), 1000, 1, 4, 9, None));
assert_ok!(Crowdloan::contribute(Origin::signed(1), 0, 100, None));
assert_ok!(Crowdloan::contribute(Origin::signed(2), 0, 200, None));
assert_ok!(Crowdloan::contribute(Origin::signed(3), 0, 300, None));
// Skip all the way to the end
run_to_block(50);
// Anyone can trigger withdraw of a user's balance without fees
assert_ok!(Crowdloan::withdraw(Origin::signed(1337), 1, 0));
assert_ok!(Crowdloan::withdraw(Origin::signed(1337), 2, 0));
assert_ok!(Crowdloan::withdraw(Origin::signed(1337), 3, 0));
#[test]
fn withdraw_handles_basic_errors() {
new_test_ext().execute_with(|| {
thiolliere
committed
assert_ok!(Slots::new_auction(Origin::root(), 5, 1));
assert_ok!(Crowdloan::create(Origin::signed(1), 1000, 1, 4, 9, None));
assert_ok!(Crowdloan::contribute(Origin::signed(1), 0, 49, None));
run_to_block(5);
// Cannot withdraw before fund ends
assert_noop!(Crowdloan::withdraw(Origin::signed(1337), 1, 0), Error::<Test>::FundNotEnded);
run_to_block(10);
// Cannot withdraw if they did not contribute
assert_noop!(Crowdloan::withdraw(Origin::signed(1337), 2, 0), Error::<Test>::NoContributions);
// Cannot withdraw from a non-existent fund
assert_noop!(Crowdloan::withdraw(Origin::signed(1337), 2, 1), Error::<Test>::InvalidFundIndex);
});
}
#[test]
fn dissolve_works() {
new_test_ext().execute_with(|| {
thiolliere
committed
assert_ok!(Slots::new_auction(Origin::root(), 5, 1));
assert_ok!(Crowdloan::create(Origin::signed(1), 1000, 1, 4, 9, None));
assert_ok!(Crowdloan::contribute(Origin::signed(1), 0, 100, None));
assert_ok!(Crowdloan::contribute(Origin::signed(2), 0, 200, None));
assert_ok!(Crowdloan::contribute(Origin::signed(3), 0, 300, None));
// Skip all the way to the end
run_to_block(50);
// Check initiator's balance.
assert_eq!(Balances::free_balance(1), 899);
// Check current funds (contributions + deposit)
assert_eq!(Balances::free_balance(Crowdloan::fund_account_id(0)), 601);
// Dissolve the crowdloan
assert_ok!(Crowdloan::dissolve(Origin::signed(1), 0));
assert_eq!(Balances::free_balance(Crowdloan::fund_account_id(0)), 0);
// Treasury account is filled
assert_eq!(Balances::free_balance(Treasury::account_id()), 600);
// Storage trie is removed
assert_eq!(Crowdloan::contribution_get(0,&0), 0);
assert_eq!(Crowdloan::funds(0), None);
});
}
#[test]
fn partial_dissolve_works() {
let mut ext = new_test_ext();
ext.execute_with(|| {
// Set up a crowdloan
assert_ok!(Slots::new_auction(Origin::root(), 5, 1));
assert_ok!(Crowdloan::create(Origin::signed(1), 100_000, 1, 4, 9, None));
// Add lots of contributors, beyond what we can delete in one go.
for i in 0 .. 30 {
Balances::make_free_balance_be(&i, 300);
assert_ok!(Crowdloan::contribute(Origin::signed(i), 0, 100, None));
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
assert_eq!(Crowdloan::contribution_get(0, &i), 100);
}
// Skip all the way to the end
run_to_block(50);
// Check current funds (contributions + deposit)
assert_eq!(Balances::free_balance(Crowdloan::fund_account_id(0)), 100 * 30 + 1);
});
ext.commit_all().unwrap();
ext.execute_with(|| {
// Partially dissolve the crowdloan
assert_ok!(Crowdloan::dissolve(Origin::signed(1), 0));
for i in 0 .. 10 {
assert_eq!(Crowdloan::contribution_get(0, &i), 0);
}
for i in 10 .. 30 {
assert_eq!(Crowdloan::contribution_get(0, &i), 100);
}
});
ext.commit_all().unwrap();
ext.execute_with(|| {
// Partially dissolve the crowdloan, again
assert_ok!(Crowdloan::dissolve(Origin::signed(1), 0));
for i in 0 .. 20 {
assert_eq!(Crowdloan::contribution_get(0, &i), 0);
}
for i in 20 .. 30 {
assert_eq!(Crowdloan::contribution_get(0, &i), 100);
}
});
ext.commit_all().unwrap();
ext.execute_with(|| {
// Fully dissolve the crowdloan
assert_ok!(Crowdloan::dissolve(Origin::signed(1), 0));
for i in 0 .. 30 {
assert_eq!(Crowdloan::contribution_get(0, &i), 0);
}
// Fund account is emptied
assert_eq!(Balances::free_balance(Crowdloan::fund_account_id(0)), 0);
// Deposit is returned
assert_eq!(Balances::free_balance(1), 201);
// Treasury account is filled
assert_eq!(Balances::free_balance(Treasury::account_id()), 100 * 30);
// Fund storage is removed
assert_eq!(Crowdloan::funds(0), None);
});
}
#[test]
fn dissolve_handles_basic_errors() {
new_test_ext().execute_with(|| {
thiolliere
committed
assert_ok!(Slots::new_auction(Origin::root(), 5, 1));
assert_ok!(Crowdloan::create(Origin::signed(1), 1000, 1, 4, 9, None));
assert_ok!(Crowdloan::contribute(Origin::signed(1), 0, 100, None));
assert_ok!(Crowdloan::contribute(Origin::signed(2), 0, 200, None));
assert_ok!(Crowdloan::contribute(Origin::signed(3), 0, 300, None));
// Cannot dissolve an invalid fund index
assert_noop!(Crowdloan::dissolve(Origin::signed(1), 1), Error::<Test>::InvalidFundIndex);
assert_noop!(Crowdloan::dissolve(Origin::signed(1), 0), Error::<Test>::InRetirementPeriod);
run_to_block(10);
// Onboard fund
<Test as frame_system::Config>::Hash::default(),
asynchronous rob
committed
vec![0].into(),
assert_ok!(Crowdloan::onboard(Origin::signed(1), 0, 0.into()));
assert_noop!(Crowdloan::dissolve(Origin::signed(1), 0), Error::<Test>::HasActiveParachain);
});
}
#[test]
fn fund_before_auction_works() {
new_test_ext().execute_with(|| {
// Create a crowdloan before an auction is created
assert_ok!(Crowdloan::create(Origin::signed(1), 1000, 1, 4, 9, None));
assert_ok!(Crowdloan::contribute(Origin::signed(1), 0, 49, None));
// Some blocks later...
run_to_block(2);
// Create an auction
thiolliere
committed
assert_ok!(Slots::new_auction(Origin::root(), 5, 1));
<Test as frame_system::Config>::Hash::default(),
asynchronous rob
committed
vec![0].into(),
));
// Move to the end of auction...
run_to_block(12);
// Endings count incremented
// Onboard crowdloan
assert_ok!(Crowdloan::onboard(Origin::signed(1), 0, 0.into()));
let fund = Crowdloan::funds(0).unwrap();
// Crowdloan is now assigned a parachain id
assert_eq!(fund.parachain, Some(0.into()));
// This parachain is managed by Slots
assert_eq!(Slots::managed_ids(), vec![0.into()]);
});
}
#[test]
fn fund_across_multiple_auctions_works() {
new_test_ext().execute_with(|| {
thiolliere
committed
assert_ok!(Slots::new_auction(Origin::root(), 5, 1));
// Create two competing crowdloans, with end dates across multiple auctions
// Each crowdloan is competing for the same slots, so only one can win
assert_ok!(Crowdloan::create(Origin::signed(1), 1000, 1, 4, 30, None));
assert_ok!(Crowdloan::create(Origin::signed(2), 1000, 1, 4, 30, None));
// Contribute to all, but more money to 0, less to 1
assert_ok!(Crowdloan::contribute(Origin::signed(1), 0, 300, None));
assert_ok!(Crowdloan::contribute(Origin::signed(1), 1, 200, None));
<Test as frame_system::Config>::Hash::default(),
asynchronous rob
committed
vec![0].into(),
<Test as frame_system::Config>::Hash::default(),
asynchronous rob
committed
vec![0].into(),
));
// End the current auction, fund 0 wins!
run_to_block(10);
assert_eq!(Crowdloan::endings_count(), 1);
// Onboard crowdloan
assert_ok!(Crowdloan::onboard(Origin::signed(1), 0, 0.into()));
let fund = Crowdloan::funds(0).unwrap();
// Crowdloan is now assigned a parachain id
assert_eq!(fund.parachain, Some(0.into()));
// This parachain is managed by Slots
assert_eq!(Slots::managed_ids(), vec![0.into()]);
// Create a second auction
thiolliere
committed
assert_ok!(Slots::new_auction(Origin::root(), 5, 1));
// Contribute to existing funds add to NewRaise
assert_ok!(Crowdloan::contribute(Origin::signed(1), 1, 10, None));
// End the current auction, fund 1 wins!
run_to_block(20);
assert_eq!(Crowdloan::endings_count(), 2);
// Onboard crowdloan
assert_ok!(Crowdloan::onboard(Origin::signed(2), 1, 1.into()));
let fund = Crowdloan::funds(1).unwrap();
// Crowdloan is now assigned a parachain id
assert_eq!(fund.parachain, Some(1.into()));
// This parachain is managed by Slots
assert_eq!(Slots::managed_ids(), vec![0.into(), 1.into()]);
});
}
}
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking {
use super::{*, Module as Crowdloan};
use crate::slots::Module as Slots;
use frame_system::RawOrigin;
use frame_support::{
assert_ok,
traits::OnInitialize,
};
use sp_runtime::traits::Bounded;
use sp_std::prelude::*;
use frame_benchmarking::{benchmarks, whitelisted_caller, account, whitelist_account};
// TODO: replace with T::Parachains::MAX_CODE_SIZE
const MAX_CODE_SIZE: u32 = 10;
const MAX_HEAD_DATA_SIZE: u32 = 10;
fn assert_last_event<T: Config>(generic_event: <T as Config>::Event) {
let events = frame_system::Module::<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>(end: T::BlockNumber) -> FundIndex {
let cap = BalanceOf::<T>::max_value();
let lease_period_index = end / T::LeasePeriod::get();
let first_slot = lease_period_index;
let last_slot = lease_period_index + 3u32.into();
let caller = account("fund_creator", 0, 0);
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
// Assume ed25519 is most complex signature format
let pubkey = crypto::create_ed25519_pubkey(b"//verifier".to_vec());
assert_ok!(Crowdloan::<T>::create(RawOrigin::Signed(caller).into(), cap, first_slot, last_slot, end, Some(pubkey)));
FundCount::get() - 1
}
fn contribute_fund<T: Config>(who: &T::AccountId, index: FundIndex) {
T::Currency::make_free_balance_be(&who, BalanceOf::<T>::max_value());
let value = T::MinContribution::get();
let pubkey = crypto::create_ed25519_pubkey(b"//verifier".to_vec());
let payload = (index, &who, BalanceOf::<T>::default(), value);
let sig = crypto::create_ed25519_signature(&payload.encode(), pubkey);
assert_ok!(Crowdloan::<T>::contribute(RawOrigin::Signed(who.clone()).into(), index, value, Some(sig)));
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
}
fn worst_validation_code<T: Config>() -> Vec<u8> {
// TODO: replace with T::Parachains::MAX_CODE_SIZE
let mut validation_code = vec![0u8; MAX_CODE_SIZE as usize];
// Replace first bytes of code with "WASM_MAGIC" to pass validation test.
let _ = validation_code.splice(
..crate::WASM_MAGIC.len(),
crate::WASM_MAGIC.iter().cloned(),
).collect::<Vec<_>>();
validation_code
}
fn worst_deploy_data<T: Config>() -> DeployData<T::Hash> {
let validation_code = worst_validation_code::<T>();
let code = primitives::v1::ValidationCode(validation_code);
// TODO: replace with T::Parachains::MAX_HEAD_DATA_SIZE
let head_data = HeadData(vec![0u8; MAX_HEAD_DATA_SIZE as usize]);
DeployData {
code_hash: T::Hashing::hash(&code.0),
// TODO: replace with T::Parachains::MAX_CODE_SIZE
code_size: MAX_CODE_SIZE,
initial_head_data: head_data,
}
}
fn setup_onboarding<T: Config>(
fund_index: FundIndex,
para_id: ParaId,
end_block: T::BlockNumber,
) -> DispatchResult {
// Matches fund creator in `create_fund`
let fund_creator = account("fund_creator", 0, 0);
let DeployData { code_hash, code_size, initial_head_data } = worst_deploy_data::<T>();
Crowdloan::<T>::fix_deploy_data(
RawOrigin::Signed(fund_creator).into(),
fund_index,
code_hash,
code_size,
initial_head_data
)?;
let lease_period_index = end_block / T::LeasePeriod::get();
Slots::<T>::new_auction(RawOrigin::Root.into(), end_block, lease_period_index)?;
let contributor: T::AccountId = account("contributor", 0, 0);
contribute_fund::<T>(&contributor, fund_index);
// TODO: Probably should use on_initialize
//Slots::<T>::on_initialize(end_block + T::EndingPeriod::get());
let onboarding_data = (lease_period_index, crate::slots::IncomingParachain::Unset(
crate::slots::NewBidder {
who: Crowdloan::<T>::fund_account_id(fund_index),
sub: Default::default(),
}
));
crate::slots::Onboarding::<T>::insert(para_id, onboarding_data);
Ok(())
}
benchmarks! {
create {
let cap = BalanceOf::<T>::max_value();
let first_slot = 0u32.into();
let last_slot = 3u32.into();
let end = T::BlockNumber::max_value();
let caller: T::AccountId = whitelisted_caller();
let verifier = account("verifier", 0, 0);
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
}: _(RawOrigin::Signed(caller), cap, first_slot, last_slot, end, Some(verifier))
verify {
assert_last_event::<T>(RawEvent::Created(FundCount::get() - 1).into())
}
// Contribute has two arms: PreEnding and Ending, but both are equal complexity.
contribute {
let fund_index = create_fund::<T>(100u32.into());
let caller: T::AccountId = whitelisted_caller();
let contribution = T::MinContribution::get();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let pubkey = crypto::create_ed25519_pubkey(b"//verifier".to_vec());
let payload = (fund_index, &caller, BalanceOf::<T>::default(), contribution);
let sig = crypto::create_ed25519_signature(&payload.encode(), pubkey);
}: _(RawOrigin::Signed(caller.clone()), fund_index, contribution, Some(sig))
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
verify {
// NewRaise is appended to, so we don't need to fill it up for worst case scenario.
assert!(!NewRaise::get().is_empty());
assert_last_event::<T>(RawEvent::Contributed(caller, fund_index, contribution).into());
}
fix_deploy_data {
let fund_index = create_fund::<T>(100u32.into());
// Matches fund creator in `create_fund`
let caller = account("fund_creator", 0, 0);
let DeployData { code_hash, code_size, initial_head_data } = worst_deploy_data::<T>();
whitelist_account!(caller);
}: _(RawOrigin::Signed(caller), fund_index, code_hash, code_size, initial_head_data)
verify {
assert_last_event::<T>(RawEvent::DeployDataFixed(fund_index).into());
}
onboard {
let end_block: T::BlockNumber = 100u32.into();
let fund_index = create_fund::<T>(end_block);
let para_id = Default::default();
setup_onboarding::<T>(fund_index, para_id, end_block)?;
let caller = whitelisted_caller();
}: _(RawOrigin::Signed(caller), fund_index, para_id)
verify {
assert_last_event::<T>(RawEvent::Onboarded(fund_index, para_id).into());
}
begin_retirement {
let end_block: T::BlockNumber = 100u32.into();
let fund_index = create_fund::<T>(end_block);
let para_id = Default::default();
setup_onboarding::<T>(fund_index, para_id, end_block)?;
let caller: T::AccountId = whitelisted_caller();
Crowdloan::<T>::onboard(RawOrigin::Signed(caller.clone()).into(), fund_index, para_id)?;
// Remove deposits to look like it is off-boarded
crate::slots::Deposits::<T>::remove(para_id);
}: _(RawOrigin::Signed(caller), fund_index)
verify {
assert_last_event::<T>(RawEvent::Retiring(fund_index).into());
}
withdraw {
let fund_index = create_fund::<T>(100u32.into());
let caller: T::AccountId = whitelisted_caller();
let contributor = account("contributor", 0, 0);
contribute_fund::<T>(&contributor, fund_index);
frame_system::Module::<T>::set_block_number(200u32.into());
}: _(RawOrigin::Signed(caller), contributor.clone(), fund_index)
verify {
assert_last_event::<T>(RawEvent::Withdrew(contributor, fund_index, T::MinContribution::get()).into());
}
// Worst case: Dissolve removes `RemoveKeysLimit` keys, and then finishes up the dissolution of the fund.
dissolve {
let fund_index = create_fund::<T>(100u32.into());
// Dissolve will remove at most `RemoveKeysLimit` at once.
for i in 0 .. T::RemoveKeysLimit::get() {
contribute_fund::<T>(&account("contributor", i, 0), fund_index);
}
let caller: T::AccountId = whitelisted_caller();
frame_system::Module::<T>::set_block_number(T::RetirementPeriod::get().saturating_add(200u32.into()));
}: _(RawOrigin::Signed(caller.clone()), fund_index)
verify {
assert_last_event::<T>(RawEvent::Dissolved(fund_index).into());
}
// Worst case scenario: N funds are all in the `NewRaise` list, we are
// in the beginning of the ending period, and each fund outbids the next
// over the same slot.
on_initialize {
// We test the complexity over different number of new raise
let n in 2 .. 100;
let end_block: T::BlockNumber = 100u32.into();
let pubkey = crypto::create_ed25519_pubkey(b"//verifier".to_vec());
for i in 0 .. n {
let fund_index = create_fund::<T>(end_block);
let contributor: T::AccountId = account("contributor", i, 0);
let contribution = T::MinContribution::get() * (i + 1).into();
T::Currency::make_free_balance_be(&contributor, BalanceOf::<T>::max_value());
let payload = (fund_index, &contributor, BalanceOf::<T>::default(), contribution);
let sig = crypto::create_ed25519_signature(&payload.encode(), pubkey.clone());
Crowdloan::<T>::contribute(RawOrigin::Signed(contributor).into(), fund_index, contribution, Some(sig))?;
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
}
let lease_period_index = end_block / T::LeasePeriod::get();
Slots::<T>::new_auction(RawOrigin::Root.into(), end_block, lease_period_index)?;
assert_eq!(<slots::Module<T>>::is_ending(end_block), Some(0u32.into()));
assert_eq!(NewRaise::get().len(), n as usize);
let old_endings_count = EndingsCount::get();
}: {
Crowdloan::<T>::on_initialize(end_block);
} verify {
assert_eq!(EndingsCount::get(), old_endings_count + 1);
assert_last_event::<T>(RawEvent::HandleBidResult((n - 1).into(), Ok(())).into());
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::crowdloan::tests::{new_test_ext, Test};
#[test]
fn test_benchmarks() {
new_test_ext().execute_with(|| {
assert_ok!(test_benchmark_create::<Test>());
assert_ok!(test_benchmark_contribute::<Test>());
assert_ok!(test_benchmark_fix_deploy_data::<Test>());
assert_ok!(test_benchmark_onboard::<Test>());
assert_ok!(test_benchmark_begin_retirement::<Test>());
assert_ok!(test_benchmark_withdraw::<Test>());
assert_ok!(test_benchmark_dissolve::<Test>());
assert_ok!(test_benchmark_on_initialize::<Test>());
});
}
}
}