Skip to content
This repository was archived by the owner on Mar 13, 2023. It is now read-only.
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
17 changes: 8 additions & 9 deletions frame/bridge/relay-authorities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,22 +830,21 @@ where

fn check_authorities_change_to_sync(
term: Term,
mut authorities: Vec<Self::Signer>,
mut authorities_change_to_sync: Vec<Self::Signer>,
) -> DispatchResult {
ensure!(
term == <NextTerm<I>>::get(),
<Error<T, I>>::TermMis
);
ensure!(term == <NextTerm<I>>::get(), <Error<T, I>>::TermMis);

let mut chain_authorities = <Authorities<T, I>>::get()
let mut next_authorities = <NextAuthorities<T, I>>::get()
.ok_or(<Error<T, I>>::NextAuthoritiesNE)?
.next_authorities
.into_iter()
.map(|authority| authority.signer)
.collect::<Vec<_>>();

authorities.sort();
chain_authorities.sort();
authorities_change_to_sync.sort();
next_authorities.sort();

if authorities == chain_authorities {
if authorities_change_to_sync == next_authorities {
Ok(())
} else {
Err(<Error<T, I>>::AuthoritiesMis)?
Expand Down
13 changes: 10 additions & 3 deletions frame/bridge/relay-authorities/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub type RelayAuthorities = Module<Test>;

pub type RelayAuthoritiesError = Error<Test, DefaultInstance>;

pub const DEFAULT_MMR_ROOT: H256 = H256([0; 32]);
pub const DEFAULT_SIGNATURE: [u8; 65] = [0; 65];

impl_outer_origin! {
pub enum Origin for Test {}
}
Expand Down Expand Up @@ -173,7 +176,7 @@ pub fn new_test_ext() -> TestExternalities {
.assimilate_storage(&mut storage)
.unwrap();
GenesisConfig::<Test> {
authorities: vec![(9, Default::default(), 1)],
authorities: vec![(9, signer_of(9), 1)],
}
.assimilate_storage(&mut storage)
.unwrap();
Expand All @@ -200,9 +203,13 @@ pub fn relay_authorities_events() -> Vec<Event> {
}

pub fn request_authority(account_id: AccountId) -> DispatchResult {
RelayAuthorities::request_authority(Origin::signed(account_id), 1, [0; 20])
RelayAuthorities::request_authority(Origin::signed(account_id), 1, signer_of(account_id))
}

pub fn request_authority_with_stake(account_id: AccountId, stake: Balance) -> DispatchResult {
RelayAuthorities::request_authority(Origin::signed(account_id), stake, [0; 20])
RelayAuthorities::request_authority(Origin::signed(account_id), stake, signer_of(account_id))
}

pub fn signer_of(account_id: AccountId) -> [u8; 20] {
[account_id as _; 20]
}
72 changes: 60 additions & 12 deletions frame/bridge/relay-authorities/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ fn mmr_root_signed_event_should_work() {
assert_ok!(RelayAuthorities::add_authority(Origin::root(), vec![1]));
assert_ok!(RelayAuthorities::submit_signed_authorities(
Origin::signed(9),
[0; 65]
DEFAULT_SIGNATURE
));

RelayAuthorities::apply_authorities_change().unwrap();
Expand All @@ -371,20 +371,20 @@ fn mmr_root_signed_event_should_work() {
assert_ok!(RelayAuthorities::submit_signed_mmr_root(
Origin::signed(9),
10,
[0; 65],
DEFAULT_SIGNATURE,
));
assert!(relay_authorities_events().is_empty());
assert_ok!(RelayAuthorities::submit_signed_mmr_root(
Origin::signed(1),
10,
[0; 65],
DEFAULT_SIGNATURE,
));
assert_eq!(
relay_authorities_events(),
vec![Event::relay_authorities(RawEvent::MMRRootSigned(
10,
Default::default(),
vec![(9, [0; 65]), (1, [0; 65])]
DEFAULT_MMR_ROOT,
vec![(9, DEFAULT_SIGNATURE), (1, DEFAULT_SIGNATURE)]
))]
);
});
Expand All @@ -402,15 +402,15 @@ fn authorities_change_signed_event_should_work() {

assert_ok!(RelayAuthorities::submit_signed_authorities(
Origin::signed(9),
[0; 65]
DEFAULT_SIGNATURE
));

assert_eq!(
relay_authorities_events(),
vec![Event::relay_authorities(RawEvent::AuthoritiesChangeSigned(
0,
vec![Default::default(), Default::default()],
vec![(9, [0; 65])]
vec![signer_of(9), signer_of(1)],
vec![(9, DEFAULT_SIGNATURE)]
))]
);

Expand All @@ -424,22 +424,22 @@ fn authorities_change_signed_event_should_work() {

assert_ok!(RelayAuthorities::submit_signed_authorities(
Origin::signed(9),
[0; 65]
DEFAULT_SIGNATURE
));
// Not enough signatures, `1 / 2 < 60%`
assert!(relay_authorities_events().is_empty());
assert_ok!(RelayAuthorities::submit_signed_authorities(
Origin::signed(1),
[0; 65]
DEFAULT_SIGNATURE
));

// Enough signatures, `2 / 2 > 60%`
assert_eq!(
relay_authorities_events(),
vec![Event::relay_authorities(RawEvent::AuthoritiesChangeSigned(
1,
vec![Default::default(), Default::default(), Default::default()],
vec![(9, [0; 65]), (1, [0; 65])]
vec![signer_of(9), signer_of(1), signer_of(2)],
vec![(9, DEFAULT_SIGNATURE), (1, DEFAULT_SIGNATURE)]
))]
);
});
Expand Down Expand Up @@ -621,3 +621,51 @@ fn lock_after_authorities_change_should_work() {
assert!(Ring::locks(2).is_empty());
});
}

#[test]
fn check_authorities_change_to_sync_should_work() {
new_test_ext().execute_with(|| {
assert_ok!(request_authority(1));
assert_ok!(request_authority(2));
assert_ok!(request_authority(3));
assert_ok!(RelayAuthorities::add_authority(
Origin::root(),
vec![1, 2, 3]
));
RelayAuthorities::apply_authorities_change().unwrap();

assert_err!(
RelayAuthorities::check_authorities_change_to_sync(
0,
vec![signer_of(1), signer_of(2), signer_of(3)]
),
RelayAuthoritiesError::AuthoritiesMis
);
assert_err!(
RelayAuthorities::check_authorities_change_to_sync(
0,
vec![signer_of(3), signer_of(1), signer_of(2)]
),
RelayAuthoritiesError::AuthoritiesMis
);
assert_err!(
RelayAuthorities::check_authorities_change_to_sync(
0,
vec![signer_of(3), signer_of(2), signer_of(1)]
),
RelayAuthoritiesError::AuthoritiesMis
);
assert_ok!(RelayAuthorities::check_authorities_change_to_sync(
0,
vec![signer_of(9), signer_of(1), signer_of(2), signer_of(3)]
));
assert_ok!(RelayAuthorities::check_authorities_change_to_sync(
0,
vec![signer_of(9), signer_of(3), signer_of(2), signer_of(1)]
));
assert_ok!(RelayAuthorities::check_authorities_change_to_sync(
0,
vec![signer_of(1), signer_of(3), signer_of(9), signer_of(2)]
));
});
}