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

Update Staking #973

Merged
merged 8 commits into from
Dec 6, 2021
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.

1 change: 1 addition & 0 deletions frame/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ rand_chacha = { version = "0.3" }
darwinia-balances = { path = "../../frame/balances" }
darwinia-support = { features = ["easy-testing"], path = "../support" }
# paritytech
frame-benchmarking = { git = "https://github.com/darwinia-network/substrate", tag = "darwinia-v0.11.6-1" }
max-encoded-len = { git = "https://github.com/darwinia-network/substrate", tag = "darwinia-v0.11.6-1" }
pallet-timestamp = { git = "https://github.com/darwinia-network/substrate", tag = "darwinia-v0.11.6-1" }
sp-storage = { git = "https://github.com/darwinia-network/substrate", tag = "darwinia-v0.11.6-1" }
Expand Down
169 changes: 87 additions & 82 deletions frame/staking/src/darwinia_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ fn bond_zero_should_fail() {
RewardDestination::Stash,
0,
),
StakingError::InsufficientValue
StakingError::InsufficientBond
);

gen_paired_account!(s(234), c(567));
Expand All @@ -231,7 +231,7 @@ fn bond_zero_should_fail() {
RewardDestination::Stash,
0,
),
StakingError::InsufficientValue
StakingError::InsufficientBond
);
});
}
Expand Down Expand Up @@ -433,7 +433,8 @@ fn time_deposit_ring_unbond_and_withdraw_automatically_should_work() {
}],
);

// Unbond all
// Unbond all of it. Must be chilled first.
assert_ok!(Staking::chill(Origin::signed(controller)));
assert_ok!(Staking::unbond(
Origin::signed(controller),
StakingBalance::RingBalance(COIN)
Expand All @@ -442,6 +443,7 @@ fn time_deposit_ring_unbond_and_withdraw_automatically_should_work() {
assert_eq!(Ring::locks(&stash).len(), 1);

System::set_block_number(BondingDurationInBlockNumber::get() + unbond_start_2 + 1);
// Trigger the update lock.
assert_ok!(Staking::unbond(
Origin::signed(controller),
StakingBalance::RingBalance(10)
Expand Down Expand Up @@ -866,93 +868,96 @@ fn check_stash_already_bonded_and_controller_already_paired() {

#[test]
fn pool_should_be_increased_and_decreased_correctly() {
ExtBuilder::default().build().execute_with(|| {
start_active_era(0);

let mut ring_pool = Staking::ring_pool();
let mut kton_pool = Staking::kton_pool();
ExtBuilder::default()
.min_validator_bond(0)
.build()
.execute_with(|| {
start_active_era(0);

// bond: 100COIN
gen_paired_account!(stash_1(111), controller_1(222), 0);
gen_paired_account!(stash_2(333), controller_2(444), promise_month(12));
ring_pool += 100 * COIN;
kton_pool += 100 * COIN;
assert_eq!(Staking::ring_pool(), ring_pool);
assert_eq!(Staking::kton_pool(), kton_pool);
let mut ring_pool = Staking::ring_pool();
let mut kton_pool = Staking::kton_pool();

// unbond: 50Ring 25Kton
assert_ok!(Staking::unbond(
Origin::signed(controller_1),
StakingBalance::RingBalance(50 * COIN)
));
assert_ok!(Staking::unbond(
Origin::signed(controller_1),
StakingBalance::KtonBalance(25 * COIN)
));
// not yet expired: promise for 12 months
assert_ok!(Staking::unbond(
Origin::signed(controller_2),
StakingBalance::RingBalance(50 * COIN)
));
assert_ok!(Staking::unbond(
Origin::signed(controller_2),
StakingBalance::KtonBalance(25 * COIN)
));
ring_pool -= 50 * COIN;
kton_pool -= 50 * COIN;
assert_eq!(Staking::ring_pool(), ring_pool);
assert_eq!(Staking::kton_pool(), kton_pool);
// bond: 100COIN
gen_paired_account!(stash_1(111), controller_1(222), 0);
gen_paired_account!(stash_2(333), controller_2(444), promise_month(12));
ring_pool += 100 * COIN;
kton_pool += 100 * COIN;
assert_eq!(Staking::ring_pool(), ring_pool);
assert_eq!(Staking::kton_pool(), kton_pool);

// claim: 50Ring
assert_ok!(Staking::try_claim_deposits_with_punish(
Origin::signed(controller_2),
promise_month * MONTH_IN_MILLISECONDS,
));
// unbond deposit items: 12.5Ring
let backup_ts = Timestamp::now();
Timestamp::set_timestamp(INIT_TIMESTAMP + promise_month * MONTH_IN_MILLISECONDS);
assert_ok!(Staking::unbond(
Origin::signed(controller_2),
StakingBalance::RingBalance(125 * COIN / 10),
));
ring_pool -= 125 * COIN / 10;
assert_eq!(Staking::ring_pool(), ring_pool);
// unbond: 50Ring 25Kton
assert_ok!(Staking::unbond(
Origin::signed(controller_1),
StakingBalance::RingBalance(50 * COIN)
));
assert_ok!(Staking::unbond(
Origin::signed(controller_1),
StakingBalance::KtonBalance(25 * COIN)
));
// not yet expired: promise for 12 months
assert_ok!(Staking::unbond(
Origin::signed(controller_2),
StakingBalance::RingBalance(50 * COIN)
));
assert_ok!(Staking::unbond(
Origin::signed(controller_2),
StakingBalance::KtonBalance(25 * COIN)
));
ring_pool -= 50 * COIN;
kton_pool -= 50 * COIN;
assert_eq!(Staking::ring_pool(), ring_pool);
assert_eq!(Staking::kton_pool(), kton_pool);

// claim: 50Ring
assert_ok!(Staking::try_claim_deposits_with_punish(
Origin::signed(controller_2),
promise_month * MONTH_IN_MILLISECONDS,
));
// unbond deposit items: 12.5Ring
let backup_ts = Timestamp::now();
Timestamp::set_timestamp(INIT_TIMESTAMP + promise_month * MONTH_IN_MILLISECONDS);
assert_ok!(Staking::unbond(
Origin::signed(controller_2),
StakingBalance::RingBalance(125 * COIN / 10),
));
ring_pool -= 125 * COIN / 10;
assert_eq!(Staking::ring_pool(), ring_pool);

Timestamp::set_timestamp(backup_ts);
assert_ok!(Staking::validate(
Origin::signed(controller_1),
ValidatorPrefs::default()
));
assert_ok!(Staking::validate(
Origin::signed(controller_2),
ValidatorPrefs::default()
));
Timestamp::set_timestamp(backup_ts);
assert_ok!(Staking::validate(
Origin::signed(controller_1),
ValidatorPrefs::default()
));
assert_ok!(Staking::validate(
Origin::signed(controller_2),
ValidatorPrefs::default()
));

start_active_era(1);
start_active_era(1);

assert_eq_uvec!(validator_controllers(), vec![controller_1, controller_2]);
assert_eq_uvec!(validator_controllers(), vec![controller_1, controller_2]);

// slash: 37.5Ring 50Kton
on_offence_now(
&[OffenceDetails {
offender: (stash_1, Staking::eras_stakers(active_era(), stash_1)),
reporters: vec![],
}],
&[Perbill::from_percent(100)],
);
on_offence_now(
&[OffenceDetails {
offender: (stash_2, Staking::eras_stakers(active_era(), stash_2)),
reporters: vec![],
}],
&[Perbill::from_percent(100)],
);
// slash: 37.5Ring 50Kton
on_offence_now(
&[OffenceDetails {
offender: (stash_1, Staking::eras_stakers(active_era(), stash_1)),
reporters: vec![],
}],
&[Perbill::from_percent(100)],
);
on_offence_now(
&[OffenceDetails {
offender: (stash_2, Staking::eras_stakers(active_era(), stash_2)),
reporters: vec![],
}],
&[Perbill::from_percent(100)],
);

ring_pool -= 375 * COIN / 10;
kton_pool -= 50 * COIN;
assert_eq!(Staking::ring_pool(), ring_pool);
assert_eq!(Staking::kton_pool(), kton_pool);
});
ring_pool -= 375 * COIN / 10;
kton_pool -= 50 * COIN;
assert_eq!(Staking::ring_pool(), ring_pool);
assert_eq!(Staking::kton_pool(), kton_pool);
});

ExtBuilder::default()
.has_stakers(false)
Expand Down
Loading