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

Commit

Permalink
Unit Test Change Authority Extrinsic (#261)
Browse files Browse the repository at this point in the history
* stub

* comment

* use real session manager

* check next_validators get cleared

* fmt
  • Loading branch information
maxwolff authored Mar 19, 2021
1 parent 0188a32 commit a824475
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 13 deletions.
95 changes: 94 additions & 1 deletion pallets/cash/src/internal/change_validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,101 @@ pub fn change_validators<T: Config>(validators: Vec<ValidatorKeys>) -> Result<()

internal::notices::dispatch_change_authority_notice::<T>(validators);

// rotate to the queued session, and rotate from this one when notices are fully signed
// rotate to the currently queued session, and queue a new session with the new validators in NextValidators
<T>::SessionInterface::rotate_session();

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{
chains::*, notices::*, reason::Reason, tests::*, AccountId32, LatestNotice, NoticeStates,
Notices, ValidatorKeys,
};
use frame_support::storage::{IterableStorageDoubleMap, StorageDoubleMap, StorageMap};
use mock::opaque::MockSessionKeys;

#[test]
fn test_change_val() {
new_test_ext().execute_with(|| {
let prev_substrate_id: AccountId32 = [8; 32].into();
let prev_keys = ValidatorKeys {
substrate_id: prev_substrate_id.clone(),
eth_address: [9; 20],
};

NextValidators::insert(prev_substrate_id, prev_keys);

let substrate_id: AccountId32 = [2; 32].into();
let eth_address = [1; 20];
let val_keys = vec![ValidatorKeys {
substrate_id: substrate_id.clone(),
eth_address: eth_address.clone(),
}];
let session_keys = MockSessionKeys { dummy: 1u64.into() };
assert_eq!(
Ok(()),
Session::set_keys(
frame_system::RawOrigin::Signed(substrate_id.clone()).into(),
session_keys.clone(),
vec![]
)
);
assert_eq!(change_validators::<Test>(val_keys.clone()), Ok(()));
let val_state_post: Vec<ValidatorKeys> =
NextValidators::iter().map(|x| x.1).collect::<Vec<_>>();
assert_eq!(val_state_post, val_keys);

let notice_state_post: Vec<(ChainId, NoticeId, NoticeState)> =
NoticeStates::iter().collect();
let notice_state = notice_state_post.into_iter().next().unwrap();
let notice = Notices::get(notice_state.0, notice_state.1);

// bumps era
let expected_notice_id = NoticeId(1, 0);
let expected_notice = Notice::ChangeAuthorityNotice(ChangeAuthorityNotice::Eth {
id: expected_notice_id,
parent: [0u8; 32],
new_authorities: vec![eth_address],
});

assert_eq!(
(
ChainId::Eth,
expected_notice_id,
NoticeState::Pending {
signature_pairs: ChainSignatureList::Eth(vec![])
}
),
notice_state
);
assert_eq!(notice, Some(expected_notice.clone()));
assert_eq!(
LatestNotice::get(ChainId::Eth),
Some((expected_notice_id, expected_notice.hash()))
);

assert_eq!(
vec![&(substrate_id, session_keys)],
Session::queued_keys().iter().collect::<Vec<_>>()
);
});
}

#[test]
fn test_keys_unset() {
new_test_ext().execute_with(|| {
let substrate_id: AccountId32 = [2; 32].into();
let vals = vec![ValidatorKeys {
substrate_id: substrate_id.clone(),
eth_address: [1; 20],
}];
assert_eq!(
change_validators::<Test>(vals.clone()),
Err(Reason::ChangeValidatorsError)
);
});
}
}
4 changes: 2 additions & 2 deletions pallets/cash/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ pub const TRANSFER_FEE: Quantity = Quantity::from_nominal("0.01", CASH);
/// Number of blocks between HTTP requests from offchain workers to open oracle price feed.
pub const ORACLE_POLL_INTERVAL_BLOCKS: u32 = 10;

// #[cfg(not(debug_assertions))]
pub const SESSION_PERIOD: u32 = 14400; // @ 6s blocks, 1 period per day
// The number of blocks in between periodic sessions
pub const SESSION_PERIOD: u32 = 14400; // Assuming 6s blocks, ~1 period per day
11 changes: 1 addition & 10 deletions pallets/cash/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,6 @@ impl pallet_session::SessionHandler<SubstrateId> for TestSessionHandler {
fn on_before_session_ending() {}
}

pub struct TestSessionManager;
impl pallet_session::SessionManager<SubstrateId> for TestSessionManager {
fn end_session(_: SessionIndex) {}
fn start_session(_: SessionIndex) {}
fn new_session(_: SessionIndex) -> Option<Vec<SubstrateId>> {
None
}
}

pub mod opaque {
use super::*;

Expand Down Expand Up @@ -138,7 +129,7 @@ impl pallet_session::Config for Test {
type ValidatorIdOf = ConvertInto;
type ShouldEndSession = TestShouldEndSession;
type NextSessionRotation = ();
type SessionManager = TestSessionManager;
type SessionManager = Cash;
type SessionHandler = TestSessionHandler;
type Keys = opaque::MockSessionKeys;
type DisabledValidatorsThreshold = (); //sp_runtime::Perbill::from_percent(33);
Expand Down

0 comments on commit a824475

Please sign in to comment.