Skip to content

Commit

Permalink
dapp staking v3 - Freeze Improvements (#1164)
Browse files Browse the repository at this point in the history
* Init commit

* Collator selection change

* Collator selection update

* Integration tests

* Update astar spec due to changes

* Weight updates
  • Loading branch information
Dinonard authored Feb 6, 2024
1 parent 4727b60 commit 9865dcb
Show file tree
Hide file tree
Showing 26 changed files with 898 additions and 528 deletions.
3 changes: 2 additions & 1 deletion 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 pallets/collator-selection/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ std = [
"pallet-authorship/std",
"pallet-session/std",
"pallet-aura/std",
"pallet-balances/std",
]

try-runtime = ["frame-support/try-runtime"]
20 changes: 20 additions & 0 deletions pallets/collator-selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ pub mod pallet {
}
}

/// Used to check whether an account is allowed to be a candidate.
pub trait AccountCheck<AccountId> {
/// `true` if the account is allowed to be a candidate, `false` otherwise.
fn allowed_candidacy(account: &AccountId) -> bool;
}

/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config {
Expand Down Expand Up @@ -157,6 +163,9 @@ pub mod pallet {
/// How many in perc kicked collators should be slashed (set 0 to disable)
type SlashRatio: Get<Perbill>;

/// Used to check whether an account is allowed to be a candidate.
type AccountCheck: AccountCheck<Self::AccountId>;

/// The weight information of this pallet.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -287,6 +296,8 @@ pub mod pallet {
NoAssociatedValidatorId,
/// Validator ID is not yet registered
ValidatorNotRegistered,
/// Account is now allowed to be a candidate due to an external reason (e.g. it might be participating in dApp staking)
NotAllowedCandidate,
}

#[pallet::hooks]
Expand Down Expand Up @@ -375,6 +386,10 @@ pub mod pallet {
!Self::invulnerables().contains(&who),
Error::<T>::AlreadyInvulnerable
);
ensure!(
T::AccountCheck::allowed_candidacy(&who),
Error::<T>::NotAllowedCandidate
);

let validator_key = T::ValidatorIdOf::convert(who.clone())
.ok_or(Error::<T>::NoAssociatedValidatorId)?;
Expand Down Expand Up @@ -502,6 +517,11 @@ pub mod pallet {
})
.collect::<Vec<_>>()
}

/// Check whether an account is a candidate.
pub fn is_account_candidate(account: &T::AccountId) -> bool {
Self::candidates().iter().any(|c| &c.who == account)
}
}

/// Keep track of number of authored blocks per authority, uncles are counted as well since
Expand Down
10 changes: 10 additions & 0 deletions pallets/collator-selection/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ impl ValidatorRegistration<u64> for IsRegistered {
}
}

pub(crate) const BLACKLISTED_ACCOUNT: u64 = 987654321;

pub struct DummyAccountCheck;
impl AccountCheck<u64> for DummyAccountCheck {
fn allowed_candidacy(account: &u64) -> bool {
*account != BLACKLISTED_ACCOUNT
}
}

impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
Expand All @@ -225,6 +234,7 @@ impl Config for Test {
type ValidatorIdOf = IdentityCollator;
type ValidatorRegistration = IsRegistered;
type SlashRatio = SlashRatio;
type AccountCheck = DummyAccountCheck;
type WeightInfo = ();
}

Expand Down
10 changes: 10 additions & 0 deletions pallets/collator-selection/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ fn cannot_register_as_candidate_if_poor() {
});
}

#[test]
fn cannot_register_candidate_if_externally_blacklisted() {
new_test_ext().execute_with(|| {
assert_noop!(
CollatorSelection::register_as_candidate(RuntimeOrigin::signed(BLACKLISTED_ACCOUNT)),
Error::<Test>::NotAllowedCandidate,
);
})
}

#[test]
fn register_as_candidate_works() {
new_test_ext().execute_with(|| {
Expand Down
Loading

0 comments on commit 9865dcb

Please sign in to comment.