Skip to content

Commit

Permalink
Correct OffenceSlasher logic after changes in pallet-im-online at sub…
Browse files Browse the repository at this point in the history
…strate 0.9.40 (#1292)

* Implement offence slasher test logic at humanode-runtime

* Hotfix of OffenceSlasher logic due to substrate: im-online: don't disable offending validators #13493
  • Loading branch information
dmitrylavrenov authored Oct 12, 2024
1 parent f25a1b4 commit 3b41d25
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 4 deletions.
5 changes: 1 addition & 4 deletions crates/humanode-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,8 @@ impl
>],
_slash_fraction: &[Perbill],
_session: sp_staking::SessionIndex,
disable_strategy: sp_staking::offence::DisableStrategy,
_disable_strategy: sp_staking::offence::DisableStrategy,
) -> Weight {
if disable_strategy == sp_staking::offence::DisableStrategy::Never {
return Weight::zero();
}
let mut weight: Weight = Weight::zero();
let weights = <Runtime as frame_system::Config>::DbWeight::get();
let mut should_be_deauthenticated = Vec::with_capacity(offenders.len());
Expand Down
1 change: 1 addition & 0 deletions crates/humanode-runtime/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ mod currency_swap;
mod fees;
mod fixed_supply;
mod genesis_config;
mod offence_slasher;
151 changes: 151 additions & 0 deletions crates/humanode-runtime/src/tests/offence_slasher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
//! Tests to verify offence slasher logic.
// Allow simple integer arithmetic in tests.
#![allow(clippy::arithmetic_side_effects)]

use sp_runtime::BoundedVec;
use sp_staking::offence::ReportOffence;

use super::*;
use crate::dev_utils::*;
use crate::opaque::SessionKeys;

const INIT_BALANCE: u128 = 10u128.pow(18 + 6);

/// Build test externalities from the custom genesis.
/// Using this call requires manual assertions on the genesis init logic.
fn new_test_ext_with() -> sp_io::TestExternalities {
let authorities = [authority_keys("Alice"), authority_keys("Bioauth-1")];
let bootnodes = vec![account_id("Alice")];

let endowed_accounts = [account_id("Alice"), account_id("Bob")];
let pot_accounts = vec![FeesPot::account_id()];

let evm_endowed_accounts = vec![evm_account_id("EvmAlice"), evm_account_id("EvmBob")];
// Build test genesis.
let config = GenesisConfig {
balances: BalancesConfig {
balances: {
endowed_accounts
.iter()
.cloned()
.chain(pot_accounts)
.map(|k| (k, INIT_BALANCE))
.chain(
[(
TreasuryPot::account_id(), 10 * INIT_BALANCE
),
(
TokenClaimsPot::account_id(),
<Balances as frame_support::traits::Currency<AccountId>>::minimum_balance(),
),
(
NativeToEvmSwapBridgePot::account_id(),
<Balances as frame_support::traits::Currency<AccountId>>::minimum_balance(),
)]
)
.collect()
},
},
session: SessionConfig {
keys: authorities
.iter()
.map(|x| {
(
x.0.clone(),
x.0.clone(),
SessionKeys {
babe: x.1.clone(),
grandpa: x.2.clone(),
im_online: x.3.clone(),
},
)
})
.collect::<Vec<_>>(),
},
babe: BabeConfig {
authorities: vec![],
epoch_config: Some(BABE_GENESIS_EPOCH_CONFIG),
},
bootnodes: BootnodesConfig {
bootnodes: bootnodes.try_into().unwrap(),
},
bioauth: BioauthConfig {
active_authentications: BoundedVec::try_from(vec![pallet_bioauth::Authentication {
public_key: account_id("Bioauth-1"),
expires_at: 1000,
}])
.unwrap(),
..Default::default()
},
evm: EVMConfig {
accounts: {
let init_genesis_account = fp_evm::GenesisAccount {
balance: INIT_BALANCE.into(),
code: Default::default(),
nonce: Default::default(),
storage: Default::default(),
};

evm_endowed_accounts
.into_iter()
.map(|k| (k, init_genesis_account.clone()))
.chain([(
EvmToNativeSwapBridgePot::account_id(),
fp_evm::GenesisAccount {
balance: <EvmBalances as frame_support::traits::Currency<
EvmAccountId,
>>::minimum_balance()
.into(),
code: Default::default(),
nonce: Default::default(),
storage: Default::default(),
},
)])
.collect()
},
},
..Default::default()
};
let storage = config.build_storage().unwrap();

// Make test externalities from the storage.
storage.into()
}

#[test]
fn works() {
// Build the state from the config.
new_test_ext_with().execute_with(move || {
// Check test preconditions.
assert_eq!(
Bioauth::active_authentications(),
vec![pallet_bioauth::Authentication {
public_key: account_id("Bioauth-1"),
expires_at: 1000,
}]
);

// Report unresponsiveness offence.
Offences::report_offence(
vec![],
pallet_im_online::UnresponsivenessOffence {
session_index: 0,
validator_set_count: 2,
offenders: vec![(
account_id("Bioauth-1"),
pallet_humanode_session::Identification::Bioauth(
pallet_bioauth::Authentication {
public_key: account_id("Bioauth-1"),
expires_at: 1000,
},
),
)],
},
)
.unwrap();

// Assert state changes.
assert!(Bioauth::active_authentications().is_empty());
})
}

0 comments on commit 3b41d25

Please sign in to comment.