From d5512ea0516ce87415ef561a4f586406c41fcffa Mon Sep 17 00:00:00 2001 From: 1xstj <106580853+1xstj@users.noreply.github.com> Date: Mon, 16 Oct 2023 22:56:13 +0530 Subject: [PATCH] fix: only increment nonce by 1 on force_change_authorities (#722) * fix: updates for light client read * fix: only increment nonce by 1 on force_change_authorities --- pallets/dkg-metadata/src/lib.rs | 5 ++++- pallets/dkg-metadata/src/tests.rs | 36 ++++++++++++++++++++++++++++++- standalone/runtime/src/lib.rs | 2 +- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/pallets/dkg-metadata/src/lib.rs b/pallets/dkg-metadata/src/lib.rs index 33519a20e..f856cc80e 100644 --- a/pallets/dkg-metadata/src/lib.rs +++ b/pallets/dkg-metadata/src/lib.rs @@ -1375,7 +1375,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::::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(()) => { diff --git a/pallets/dkg-metadata/src/tests.rs b/pallets/dkg-metadata/src/tests.rs index 339601326..f01e5c36c 100644 --- a/pallets/dkg-metadata/src/tests.rs +++ b/pallets/dkg-metadata/src/tests.rs @@ -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}; @@ -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::::put(1); + let input: BoundedVec<_, _> = mock_pub_key().to_raw_vec().try_into().unwrap(); + crate::pallet::NextDKGPublicKey::::put((session_id, input)); + NextKeygenThreshold::::put(keygen_threshold); + NextSignatureThreshold::::put(signature_threshold); + NextAuthorities::::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::::get(), expected_refresh_nonce); + }); +} diff --git a/standalone/runtime/src/lib.rs b/standalone/runtime/src/lib.rs index c4249022b..101fc48d0 100644 --- a/standalone/runtime/src/lib.rs +++ b/standalone/runtime/src/lib.rs @@ -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; }