Skip to content

Commit

Permalink
fix: only increment nonce by 1 on force_change_authorities
Browse files Browse the repository at this point in the history
  • Loading branch information
1xstj committed Oct 16, 2023
1 parent 0885385 commit abdf567
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
5 changes: 4 additions & 1 deletion pallets/dkg-metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,10 @@ pub mod pallet {
// to sign our own key as a means of jumpstarting the mechanism.
if let Some(pub_key) = next_pub_key {
ShouldSubmitProposerVote::<T>::put(true);
let next_nonce = Self::refresh_nonce() + 1u32;
// we use the current nonce as the next nonce since the refreshNonce
// has been incremented in `change_authorities` incrementing here will lead to a
// difference of two
let next_nonce = Self::refresh_nonce();
let data = Self::create_refresh_proposal(pub_key.1.into(), next_nonce)?;
match T::ProposalHandler::handle_unsigned_proposal(data) {
Ok(()) => {
Expand Down
36 changes: 35 additions & 1 deletion pallets/dkg-metadata/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
mock::*, AggregatedMisbehaviourReports, AggregatedPublicKeys, Authorities,
AuthorityReputations, BestAuthorities, Config, Error, Event, JailedKeygenAuthorities,
NextAuthorities, NextBestAuthorities, NextKeygenThreshold, NextSignatureThreshold,
INITIAL_REPUTATION, REPUTATION_INCREMENT,
RefreshNonce, INITIAL_REPUTATION, REPUTATION_INCREMENT,
};
use codec::Encode;
use dkg_runtime_primitives::{keccak_256, utils::ecdsa, MisbehaviourType, KEY_TYPE};
Expand Down Expand Up @@ -875,3 +875,37 @@ fn reputation_is_set_correctly() {
}
})
}

#[test]
fn force_change_authorities_increments_nonce_correctly() {
new_test_ext(vec![1, 2, 3, 4, 5]).execute_with(|| {
let session_id = 1;

// prep the next authorities
let mut next_authorities: BoundedVec<_, _> = Default::default();
let mut next_authorities_raw: Vec<_> = Default::default();
for _ in 1..=5 {
let authority_id = mock_pub_key();
let dkg_id = DKGId::from(authority_id);
next_authorities_raw.push(authority_id);
next_authorities.try_push(dkg_id).unwrap();
}

let keygen_threshold = 5;
let signature_threshold = 3;

RefreshNonce::<Test>::put(1);
let input: BoundedVec<_, _> = mock_pub_key().to_raw_vec().try_into().unwrap();
crate::pallet::NextDKGPublicKey::<Test>::put((session_id, input));
NextKeygenThreshold::<Test>::put(keygen_threshold);
NextSignatureThreshold::<Test>::put(signature_threshold);
NextAuthorities::<Test>::put(&next_authorities);

// force change authorities
assert_ok!(DKGMetadata::force_change_authorities(RuntimeOrigin::root(),));

// the new refresh nonce should only increment by one
let expected_refresh_nonce: u32 = 2;
assert_eq!(RefreshNonce::<Test>::get(), expected_refresh_nonce);
});
}
2 changes: 1 addition & 1 deletion standalone/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ parameter_types! {
#[cfg(not(feature = "integration-tests"))]
parameter_types! {
// How often we trigger a new session.
pub const Period: BlockNumber = HOURS;
pub const Period: BlockNumber = MINUTES;
pub const Offset: BlockNumber = 0;
}

Expand Down

0 comments on commit abdf567

Please sign in to comment.