Skip to content

Commit

Permalink
Add parent verifying key to chain (#951)
Browse files Browse the repository at this point in the history
* Add parent veryfying key to chain

* Clean up verifying key check

---------

Co-authored-by: Hernando Castano <hernando@hcastano.com>
  • Loading branch information
JesseAbram and HCastano committed Jul 19, 2024
1 parent 3c04c96 commit 16d6b0d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 14 deletions.
Binary file modified crates/client/entropy_metadata.scale
Binary file not shown.
4 changes: 3 additions & 1 deletion crates/threshold-signature-server/src/user/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,9 @@ pub async fn confirm_registered(
// TODO: Understand this better, potentially use sign_and_submit_default
// or other method under sign_and_*
if who.encode() == NETWORK_PARENT_KEY.encode() {
let jump_start_request = entropy::tx().registry().confirm_jump_start();
let jump_start_request = entropy::tx().registry().confirm_jump_start(
entropy::runtime_types::bounded_collections::bounded_vec::BoundedVec(verifying_key),
);
submit_transaction(api, rpc, signer, &jump_start_request, Some(nonce)).await?;
} else {
let confirm_register_request = entropy::tx().registry().confirm_register(
Expand Down
6 changes: 6 additions & 0 deletions crates/threshold-signature-server/src/user/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,13 @@ async fn test_jumpstart_network() {
let key_share: Option<KeyShareWithAuxInfo> =
entropy_kvdb::kv_manager::helpers::deserialize(&response_key);
assert_eq!(key_share.is_some(), true);
let jump_start_progress_query = entropy::storage().registry().jump_start_progress();
let jump_start_progress =
query_chain(&api, &rpc, jump_start_progress_query, None).await.unwrap().unwrap();
let verifying_key =
key_share.unwrap().0.verifying_key().to_encoded_point(true).as_bytes().to_vec();

assert_eq!(jump_start_progress.verifying_key.unwrap().0, verifying_key);
clean_tests();
}

Expand Down
8 changes: 6 additions & 2 deletions pallets/registry/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ benchmarks! {
let c in 0 .. SIGNING_PARTY_SIZE as u32;
let sig_req_account: T::AccountId = whitelisted_caller();
let validator_account: T::AccountId = whitelisted_caller();
let expected_verifying_key = BoundedVec::default();

let mut accounts = vec![];
for i in 0..SIGNING_PARTY_SIZE {
Expand All @@ -98,12 +99,13 @@ benchmarks! {
<JumpStartProgress<T>>::put(JumpStartDetails {
jump_start_status: JumpStartStatus::InProgress(0),
confirmations: vec![validators[0].clone()],
verifying_key: None
});


let balance = <T as pallet_staking_extension::Config>::Currency::minimum_balance() * 100u32.into();
let _ = <T as pallet_staking_extension::Config>::Currency::make_free_balance_be(&accounts[1], balance);
}: confirm_jump_start(RawOrigin::Signed(accounts[1].clone()))
}: confirm_jump_start(RawOrigin::Signed(accounts[1].clone()), expected_verifying_key)
verify {
assert_last_event::<T>(Event::<T>::FinishedNetworkJumpStart().into());
}
Expand All @@ -113,6 +115,7 @@ benchmarks! {
let sig_req_account: T::AccountId = whitelisted_caller();
let validator_account: T::AccountId = whitelisted_caller();
let threshold_account: T::AccountId = whitelisted_caller();
let expected_verifying_key = BoundedVec::default();

// add validators and a registering user
for i in 0..SIGNING_PARTY_SIZE {
Expand All @@ -123,12 +126,13 @@ benchmarks! {
<JumpStartProgress<T>>::put(JumpStartDetails {
jump_start_status: JumpStartStatus::InProgress(0),
confirmations: vec![],
verifying_key: None
});


let balance = <T as pallet_staking_extension::Config>::Currency::minimum_balance() * 100u32.into();
let _ = <T as pallet_staking_extension::Config>::Currency::make_free_balance_be(&threshold_account, balance);
}: confirm_jump_start(RawOrigin::Signed(threshold_account.clone()))
}: confirm_jump_start(RawOrigin::Signed(threshold_account.clone()), expected_verifying_key)
verify {
let validator_stash =
pallet_staking_extension::Pallet::<T>::threshold_to_stash(&threshold_account).unwrap();
Expand Down
18 changes: 17 additions & 1 deletion pallets/registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub mod pallet {
pub struct JumpStartDetails<T: Config> {
pub jump_start_status: JumpStartStatus,
pub confirmations: Vec<T::ValidatorId>,
pub verifying_key: Option<VerifyingKey>,
}

#[pallet::genesis_config]
Expand Down Expand Up @@ -261,6 +262,7 @@ pub mod pallet {
NoProgramSet,
TooManyModifiableKeys,
MismatchedVerifyingKeyLength,
MismatchedVerifyingKey,
NotValidator,
JumpStartProgressNotReady,
JumpStartNotInProgress,
Expand Down Expand Up @@ -305,6 +307,7 @@ pub mod pallet {
JumpStartProgress::<T>::put(JumpStartDetails {
jump_start_status: JumpStartStatus::InProgress(converted_block_number),
confirmations: vec![],
verifying_key: None,
});
Self::deposit_event(Event::StartedNetworkJumpStart());
Ok(())
Expand All @@ -316,7 +319,10 @@ pub mod pallet {
<T as Config>::WeightInfo::confirm_jump_start_confirm(SIGNING_PARTY_SIZE as u32)
.max(<T as Config>::WeightInfo::confirm_jump_start_done(SIGNING_PARTY_SIZE as u32))
})]
pub fn confirm_jump_start(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
pub fn confirm_jump_start(
origin: OriginFor<T>,
verifying_key: VerifyingKey,
) -> DispatchResultWithPostInfo {
// check is validator
let ts_server_account = ensure_signed(origin)?;

Expand All @@ -327,12 +333,21 @@ pub mod pallet {
ensure!(validators.contains(&validator_stash), Error::<T>::NotValidator);

let mut jump_start_info = JumpStartProgress::<T>::get();
match jump_start_info.verifying_key {
Some(ref key) => {
ensure!(key == &verifying_key, Error::<T>::MismatchedVerifyingKey);
},
None => {
jump_start_info.verifying_key = Some(verifying_key);
},
}

// check in progress
ensure!(
matches!(jump_start_info.jump_start_status, JumpStartStatus::InProgress(_)),
Error::<T>::JumpStartNotInProgress
);

ensure!(
!jump_start_info.confirmations.contains(&validator_stash),
Error::<T>::AlreadyConfirmed
Expand All @@ -350,6 +365,7 @@ pub mod pallet {
JumpStartProgress::<T>::put(JumpStartDetails {
jump_start_status: JumpStartStatus::Done,
confirmations: vec![],
verifying_key: jump_start_info.verifying_key,
});

Self::deposit_event(Event::FinishedNetworkJumpStart());
Expand Down
46 changes: 36 additions & 10 deletions pallets/registry/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ fn it_jumps_the_network() {
new_test_ext().execute_with(|| {
assert_eq!(
Registry::jump_start_progress(),
JumpStartDetails { jump_start_status: JumpStartStatus::Ready, confirmations: vec![] },
JumpStartDetails {
jump_start_status: JumpStartStatus::Ready,
confirmations: vec![],
verifying_key: None
},
"Checks default status of jump start detail"
);
assert_ok!(Registry::jump_start_network(RuntimeOrigin::signed(1)));
Expand All @@ -105,6 +109,7 @@ fn it_jumps_the_network() {
JumpStartDetails {
jump_start_status: JumpStartStatus::InProgress(0),
confirmations: vec![],
verifying_key: None
},
"Checks that jump start is in progress"
);
Expand All @@ -121,7 +126,8 @@ fn it_jumps_the_network() {
Registry::jump_start_progress(),
JumpStartDetails {
jump_start_status: JumpStartStatus::InProgress(100),
confirmations: vec![]
confirmations: vec![],
verifying_key: None
},
"ensures jump start is called again if too many blocks passed"
);
Expand All @@ -131,44 +137,64 @@ fn it_jumps_the_network() {
#[test]
fn it_tests_jump_start_result() {
new_test_ext().execute_with(|| {
let expected_verifying_key = BoundedVec::default();

assert_noop!(
Registry::confirm_jump_start(RuntimeOrigin::signed(1)),
Registry::confirm_jump_start(RuntimeOrigin::signed(1), expected_verifying_key.clone()),
Error::<Test>::NoThresholdKey
);
pallet_staking_extension::ThresholdToStash::<Test>::insert(1, 1);

pallet_staking_extension::ThresholdToStash::<Test>::insert(7, 7);
assert_noop!(
Registry::confirm_jump_start(RuntimeOrigin::signed(7)),
Registry::confirm_jump_start(RuntimeOrigin::signed(7), expected_verifying_key.clone()),
Error::<Test>::NotValidator
);

assert_noop!(
Registry::confirm_jump_start(RuntimeOrigin::signed(1)),
Registry::confirm_jump_start(RuntimeOrigin::signed(1), expected_verifying_key.clone()),
Error::<Test>::JumpStartNotInProgress
);
// trigger jump start
assert_ok!(Registry::jump_start_network(RuntimeOrigin::signed(1)));

assert_ok!(Registry::confirm_jump_start(RuntimeOrigin::signed(1)));
assert_ok!(Registry::confirm_jump_start(
RuntimeOrigin::signed(1),
expected_verifying_key.clone()
));
assert_eq!(
Registry::jump_start_progress(),
JumpStartDetails {
jump_start_status: JumpStartStatus::InProgress(0),
confirmations: vec![1]
confirmations: vec![1],
verifying_key: Some(expected_verifying_key.clone())
},
"Jump start recieves a confirmation"
);
assert_noop!(
Registry::confirm_jump_start(RuntimeOrigin::signed(1)),
Registry::confirm_jump_start(RuntimeOrigin::signed(1), expected_verifying_key.clone()),
Error::<Test>::AlreadyConfirmed
);

let bad_verifying_key =
BoundedVec::try_from(vec![0; VERIFICATION_KEY_LENGTH as usize]).unwrap();
assert_noop!(
Registry::confirm_jump_start(RuntimeOrigin::signed(1), bad_verifying_key.clone()),
Error::<Test>::MismatchedVerifyingKey
);

pallet_staking_extension::ThresholdToStash::<Test>::insert(2, 2);
assert_ok!(Registry::confirm_jump_start(RuntimeOrigin::signed(2)));
assert_ok!(Registry::confirm_jump_start(
RuntimeOrigin::signed(2),
expected_verifying_key.clone()
));
assert_eq!(
Registry::jump_start_progress(),
JumpStartDetails { jump_start_status: JumpStartStatus::Done, confirmations: vec![] },
JumpStartDetails {
jump_start_status: JumpStartStatus::Done,
confirmations: vec![],
verifying_key: Some(expected_verifying_key)
},
"Jump start in done status after all confirmations"
);
});
Expand Down

0 comments on commit 16d6b0d

Please sign in to comment.