Skip to content
This repository has been archived by the owner on Sep 28, 2023. It is now read-only.

DappsStaking bench improvements #112

Merged
merged 5 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
60 changes: 45 additions & 15 deletions frame/collator-selection/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ fn cannot_register_candidate_if_too_many() {

// reset desired candidates:
<crate::DesiredCandidates<Test>>::put(1);
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(4)));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(4)
));

// but no more
assert_noop!(
Expand All @@ -132,7 +134,9 @@ fn cannot_unregister_candidate_if_too_few() {
new_test_ext().execute_with(|| {
// reset desired candidates:
<crate::DesiredCandidates<Test>>::put(1);
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(4)));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(4)
));

// can not remove too few
assert_noop!(
Expand Down Expand Up @@ -170,7 +174,9 @@ fn cannot_register_as_candidate_if_keys_not_registered() {
fn cannot_register_dupe_candidate() {
new_test_ext().execute_with(|| {
// can add 3 as candidate
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3)));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(3)
));
let addition = CandidateInfo {
who: 3,
deposit: 10,
Expand All @@ -194,7 +200,9 @@ fn cannot_register_as_candidate_if_poor() {
assert_eq!(Balances::free_balance(&33), 0);

// works
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3)));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(3)
));

// poor
assert_noop!(
Expand All @@ -217,8 +225,12 @@ fn register_as_candidate_works() {
assert_eq!(Balances::free_balance(&3), 100);
assert_eq!(Balances::free_balance(&4), 100);

assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3)));
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(4)));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(3)
));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(4)
));

assert_eq!(Balances::free_balance(&3), 90);
assert_eq!(Balances::free_balance(&4), 90);
Expand All @@ -231,11 +243,15 @@ fn register_as_candidate_works() {
fn leave_intent() {
new_test_ext().execute_with(|| {
// register a candidate.
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3)));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(3)
));
assert_eq!(Balances::free_balance(3), 90);

// register too so can leave above min candidates
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(5)));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(5)
));
assert_eq!(Balances::free_balance(5), 90);

// cannot leave if not candidate.
Expand All @@ -259,7 +275,9 @@ fn authorship_event_handler() {

// 4 is the default author.
assert_eq!(Balances::free_balance(4), 100);
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(4)));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(4)
));
// triggers `note_author`
Authorship::on_initialize(1);

Expand Down Expand Up @@ -287,7 +305,9 @@ fn fees_edgecases() {
Balances::make_free_balance_be(&CollatorSelection::account_id(), 5);
// 4 is the default author.
assert_eq!(Balances::free_balance(4), 100);
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(4)));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(4)
));
// triggers `note_author`
Authorship::on_initialize(1);

Expand Down Expand Up @@ -319,7 +339,9 @@ fn session_management_works() {
assert_eq!(SessionHandlerCollators::get(), vec![1, 2]);

// add a new collator
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3)));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(3)
));

// session won't see this.
assert_eq!(SessionHandlerCollators::get(), vec![1, 2]);
Expand Down Expand Up @@ -348,8 +370,12 @@ fn kick_and_slash_mechanism() {
// Define slash destination account
<crate::SlashDestination<Test>>::put(5);
// add a new collator
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3)));
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(4)));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(3)
));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(4)
));
initialize_to_block(10);
assert_eq!(CollatorSelection::candidates().len(), 2);
initialize_to_block(20);
Expand Down Expand Up @@ -377,8 +403,12 @@ fn kick_and_slash_mechanism() {
fn should_not_kick_mechanism_too_few() {
new_test_ext().execute_with(|| {
// add a new collator
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3)));
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(5)));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(3)
));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(5)
));
initialize_to_block(10);
assert_eq!(CollatorSelection::candidates().len(), 2);
initialize_to_block(20);
Expand Down
1 change: 1 addition & 0 deletions frame/dapps-staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub mod pallet {

/// The current era index.
#[pallet::storage]
#[pallet::whitelist_storage]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're doing this, please whitelist all necessary storage:

  • PalletDisabled
  • ForceEra
  • NextEraStartingBlock

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true. I updated. thank you for pointing out

#[pallet::getter(fn current_era)]
pub type CurrentEra<T> = StorageValue<_, EraIndex, ValueQuery>;

Expand Down
12 changes: 6 additions & 6 deletions frame/dapps-staking/src/testing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,9 @@ pub(crate) fn assert_withdraw_from_unregistered(
RuntimeOrigin::signed(staker.clone()),
contract_id.clone()
));
System::assert_last_event(mock::RuntimeEvent::DappsStaking(Event::WithdrawFromUnregistered(
staker,
contract_id.clone(),
staked_value,
)));
System::assert_last_event(mock::RuntimeEvent::DappsStaking(
Event::WithdrawFromUnregistered(staker, contract_id.clone(), staked_value),
));

let final_state = MemorySnapshot::all(current_era, contract_id, staker);

Expand Down Expand Up @@ -365,7 +363,9 @@ pub(crate) fn assert_withdraw_unbonded(staker: AccountId) {
let expected_unbond_amount = valid_info.sum();

// Ensure op is successful and event is emitted
assert_ok!(DappsStaking::withdraw_unbonded(RuntimeOrigin::signed(staker),));
assert_ok!(DappsStaking::withdraw_unbonded(RuntimeOrigin::signed(
staker
),));
System::assert_last_event(mock::RuntimeEvent::DappsStaking(Event::Withdrawn(
staker,
expected_unbond_amount,
Expand Down
70 changes: 56 additions & 14 deletions frame/dapps-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ fn new_era_is_handled_with_maintenance_mode() {
// enable maintenance mode
assert_ok!(DappsStaking::maintenance_mode(RuntimeOrigin::root(), true));
assert!(PalletDisabled::<TestRuntime>::exists());
System::assert_last_event(mock::RuntimeEvent::DappsStaking(Event::MaintenanceMode(true)));
System::assert_last_event(mock::RuntimeEvent::DappsStaking(Event::MaintenanceMode(
true,
)));

// advance 9 blocks or 3 era lengths (advance_to_era() doesn't work in maintenance mode)
run_for_blocks(mock::BLOCKS_PER_ERA * 3);
Expand All @@ -131,7 +133,9 @@ fn new_era_is_handled_with_maintenance_mode() {

// disable maintenance mode
assert_ok!(DappsStaking::maintenance_mode(RuntimeOrigin::root(), false));
System::assert_last_event(mock::RuntimeEvent::DappsStaking(Event::MaintenanceMode(false)));
System::assert_last_event(mock::RuntimeEvent::DappsStaking(Event::MaintenanceMode(
false,
)));

// advance one era
run_for_blocks(mock::BLOCKS_PER_ERA);
Expand Down Expand Up @@ -479,7 +483,11 @@ fn unregister_stake_and_unstake_is_not_ok() {
Error::<TestRuntime>::NotOperatedContract
);
assert_noop!(
DappsStaking::unbond_and_unstake(RuntimeOrigin::signed(staker), contract_id.clone(), 100),
DappsStaking::unbond_and_unstake(
RuntimeOrigin::signed(staker),
contract_id.clone(),
100
),
Error::<TestRuntime>::NotOperatedContract
);
})
Expand Down Expand Up @@ -622,7 +630,10 @@ fn withdraw_from_unregistered_when_unclaimed_rewards_remaining() {

for _ in 1..DappsStaking::current_era() {
assert_noop!(
DappsStaking::withdraw_from_unregistered(RuntimeOrigin::signed(staker), contract_id),
DappsStaking::withdraw_from_unregistered(
RuntimeOrigin::signed(staker),
contract_id
),
Error::<TestRuntime>::UnclaimedRewardsRemaining
);
assert_claim_staker(staker, &contract_id);
Expand Down Expand Up @@ -746,7 +757,11 @@ fn bond_and_stake_on_unregistered_contract_fails() {
// Check not registered contract. Expect an error.
let evm_contract = MockSmartContract::Evm(H160::repeat_byte(0x01));
assert_noop!(
DappsStaking::bond_and_stake(RuntimeOrigin::signed(staker_id), evm_contract, stake_value),
DappsStaking::bond_and_stake(
RuntimeOrigin::signed(staker_id),
evm_contract,
stake_value
),
Error::<TestRuntime>::NotOperatedContract
);
})
Expand Down Expand Up @@ -1053,7 +1068,11 @@ fn unbond_and_unstake_with_no_chunks_allowed() {
assert_bond_and_stake(staker_id, &contract_id, 100);

assert_noop!(
DappsStaking::unbond_and_unstake(RuntimeOrigin::signed(staker_id), contract_id.clone(), 20),
DappsStaking::unbond_and_unstake(
RuntimeOrigin::signed(staker_id),
contract_id.clone(),
20
),
Error::<TestRuntime>::TooManyUnlockingChunks,
);
})
Expand Down Expand Up @@ -1090,15 +1109,19 @@ fn withdraw_unbonded_is_ok() {

// Advance one additional era and expect that the first chunk can be withdrawn
advance_to_era(DappsStaking::current_era() + 1);
assert_ok!(DappsStaking::withdraw_unbonded(RuntimeOrigin::signed(staker_id),));
assert_ok!(DappsStaking::withdraw_unbonded(RuntimeOrigin::signed(
staker_id
),));
System::assert_last_event(mock::RuntimeEvent::DappsStaking(Event::Withdrawn(
staker_id,
first_unbond_value,
)));

// Advance one additional era and expect that the first chunk can be withdrawn
advance_to_era(DappsStaking::current_era() + 1);
assert_ok!(DappsStaking::withdraw_unbonded(RuntimeOrigin::signed(staker_id),));
assert_ok!(DappsStaking::withdraw_unbonded(RuntimeOrigin::signed(
staker_id
),));
System::assert_last_event(mock::RuntimeEvent::DappsStaking(Event::Withdrawn(
staker_id,
second_unbond_value,
Expand Down Expand Up @@ -1420,7 +1443,11 @@ fn nomination_transfer_contracts_have_too_many_era_stake_values() {
advance_to_era(DappsStaking::current_era() + 1);
}
assert_noop!(
DappsStaking::bond_and_stake(RuntimeOrigin::signed(staker), origin_contract_id.clone(), 15),
DappsStaking::bond_and_stake(
RuntimeOrigin::signed(staker),
origin_contract_id.clone(),
15
),
Error::<TestRuntime>::TooManyEraStakeValues
);

Expand Down Expand Up @@ -1656,7 +1683,10 @@ fn claim_is_ok() {
// Shouldn't be possible to claim current era.
// Also, previous claim calls should have claimed everything prior to current era.
assert_noop!(
DappsStaking::claim_staker(RuntimeOrigin::signed(first_staker), first_contract_id.clone()),
DappsStaking::claim_staker(
RuntimeOrigin::signed(first_staker),
first_contract_id.clone()
),
Error::<TestRuntime>::EraOutOfBounds
);
assert_noop!(
Expand Down Expand Up @@ -1716,7 +1746,11 @@ fn claim_after_unregister_is_ok() {
for era in start_era..unregister_era {
if era >= full_unstake_era && era < restake_era {
assert_noop!(
DappsStaking::claim_dapp(RuntimeOrigin::signed(developer), contract_id.clone(), era),
DappsStaking::claim_dapp(
RuntimeOrigin::signed(developer),
contract_id.clone(),
era
),
Error::<TestRuntime>::NotStakedContract
);
} else {
Expand Down Expand Up @@ -1896,7 +1930,11 @@ fn claim_dapp_with_zero_stake_periods_is_ok() {
// Ensure that the empty interval cannot be claimed
for era in first_full_unstake_era..restake_era {
assert_noop!(
DappsStaking::claim_dapp(RuntimeOrigin::signed(developer), contract_id.clone(), era),
DappsStaking::claim_dapp(
RuntimeOrigin::signed(developer),
contract_id.clone(),
era
),
Error::<TestRuntime>::NotStakedContract
);
}
Expand Down Expand Up @@ -1934,7 +1972,9 @@ fn maintenance_mode_is_ok() {

assert_ok!(DappsStaking::maintenance_mode(RuntimeOrigin::root(), true));
assert!(PalletDisabled::<TestRuntime>::exists());
System::assert_last_event(mock::RuntimeEvent::DappsStaking(Event::MaintenanceMode(true)));
System::assert_last_event(mock::RuntimeEvent::DappsStaking(Event::MaintenanceMode(
true,
)));

let account = 1;
let contract_id = MockSmartContract::Evm(H160::repeat_byte(0x01));
Expand Down Expand Up @@ -1998,7 +2038,9 @@ fn maintenance_mode_is_ok() {
//
// 4
assert_ok!(DappsStaking::maintenance_mode(RuntimeOrigin::root(), false));
System::assert_last_event(mock::RuntimeEvent::DappsStaking(Event::MaintenanceMode(false)));
System::assert_last_event(mock::RuntimeEvent::DappsStaking(Event::MaintenanceMode(
false,
)));
assert_register(account, &contract_id);
})
}
Expand Down
2 changes: 1 addition & 1 deletion frame/pallet-xvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30", optional = true, default-features = false }

# EVM support
pallet-evm = { git = "https://github.com/AstarNetwork/frontier", branch = "polkadot-v0.9.30", default-features = false, optional = true, features = [ "forbid-evm-reentrancy" ] }
pallet-evm = { git = "https://github.com/AstarNetwork/frontier", branch = "polkadot-v0.9.30", default-features = false, optional = true, features = ["forbid-evm-reentrancy"] }

# Substrate WASM VM support
pallet-contracts = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30", default-features = false, optional = true }
Expand Down
Loading