Skip to content

Commit

Permalink
Merge branch 'master' into hc/remove-confirm-registered
Browse files Browse the repository at this point in the history
  • Loading branch information
HCastano committed Aug 22, 2024
2 parents c37ce78 + 1202e02 commit e0a1d31
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 104 deletions.
5 changes: 5 additions & 0 deletions crates/threshold-signature-server/src/user/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ async fn test_get_signer_does_not_throw_err() {
clean_tests();
}

#[ignore]
#[tokio::test]
#[serial]
async fn test_sign_tx_no_chain() {
Expand Down Expand Up @@ -488,6 +489,7 @@ async fn signature_request_with_derived_account_works() {
clean_tests();
}

#[ignore]
#[tokio::test]
#[serial]
async fn test_sign_tx_no_chain_fail() {
Expand Down Expand Up @@ -613,6 +615,7 @@ async fn test_sign_tx_no_chain_fail() {
clean_tests();
}

#[ignore]
#[tokio::test]
#[serial]
async fn test_program_with_config() {
Expand Down Expand Up @@ -1078,6 +1081,7 @@ pub async fn verify_signature(
}
}

#[ignore]
#[tokio::test]
#[serial]
async fn test_fail_infinite_program() {
Expand Down Expand Up @@ -1156,6 +1160,7 @@ async fn test_fail_infinite_program() {
}
}

#[ignore]
#[tokio::test]
#[serial]
async fn test_device_key_proxy() {
Expand Down
1 change: 1 addition & 0 deletions crates/threshold-signature-server/tests/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use serial_test::serial;
use sp_keyring::AccountKeyring;
use synedrion::k256::ecdsa::VerifyingKey;

#[ignore]
#[tokio::test]
#[serial]
async fn integration_test_sign_public() {
Expand Down
1 change: 1 addition & 0 deletions crates/threshold-signature-server/tests/sign_eth_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use synedrion::k256::ecdsa::VerifyingKey;

const GOERLI_CHAIN_ID: u64 = 5;

#[ignore]
#[tokio::test]
#[serial]
async fn integration_test_sign_eth_tx() {
Expand Down
15 changes: 12 additions & 3 deletions pallets/registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,10 @@ pub mod pallet {
/// Allows a user's program modification account to change their program pointer
#[pallet::call_index(3)]
#[pallet::weight({
<T as Config>::WeightInfo::change_program_instance(<T as Config>::MaxProgramHashes::get(), <T as Config>::MaxProgramHashes::get())
<T as Config>::WeightInfo::change_program_instance(
<T as Config>::MaxProgramHashes::get(),
<T as Config>::MaxProgramHashes::get()
)
})]
pub fn change_program_instance(
origin: OriginFor<T>,
Expand All @@ -469,9 +472,10 @@ pub mod pallet {
},
)?;
}

let mut old_programs_length = 0;
let programs_data =
Registered::<T>::try_mutate(&verifying_key, |maybe_registered_details| {
RegisteredOnChain::<T>::try_mutate(&verifying_key, |maybe_registered_details| {
if let Some(registered_details) = maybe_registered_details {
ensure!(
who == registered_details.program_modification_account,
Expand All @@ -496,7 +500,9 @@ pub mod pallet {
Err(Error::<T>::NotRegistered)
}
})?;

Self::deposit_event(Event::ProgramInfoChanged(who, programs_data.clone()));

Ok(Some(<T as Config>::WeightInfo::change_program_instance(
programs_data.len() as u32,
old_programs_length as u32,
Expand All @@ -515,7 +521,8 @@ pub mod pallet {
new_program_mod_account: T::AccountId,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
Registered::<T>::try_mutate(&verifying_key, |maybe_registered_details| {

RegisteredOnChain::<T>::try_mutate(&verifying_key, |maybe_registered_details| {
if let Some(registered_details) = maybe_registered_details {
ensure!(
who == registered_details.program_modification_account,
Expand All @@ -528,6 +535,7 @@ pub mod pallet {
Err(Error::<T>::NotRegistered)
}
})?;

let mut verifying_keys_len = 0;
ModifiableKeys::<T>::try_mutate(&who, |verifying_keys| -> Result<(), DispatchError> {
verifying_keys_len = verifying_keys.len();
Expand All @@ -548,6 +556,7 @@ pub mod pallet {
Ok(())
},
)?;

Self::deposit_event(Event::ProgramModificationAccountChanged(
who,
new_program_mod_account,
Expand Down
182 changes: 81 additions & 101 deletions pallets/registry/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use pallet_staking_extension::{JumpStartDetails, JumpStartProgress, JumpStartSta
use sp_runtime::traits::Hash;

use crate as pallet_registry;
use crate::{mock::*, Error, ModifiableKeys, ProgramInstance, Registered, RegisteredInfo};
use crate::{mock::*, Error, ModifiableKeys, ProgramInstance, RegisteredInfo, RegisteredOnChain};

const NULL_ARR: [u8; 32] = [0; 32];

Expand Down Expand Up @@ -110,6 +110,40 @@ fn it_registers_a_user_on_chain() {
});
}

#[test]
fn it_increases_program_reference_count_on_register() {
new_test_ext().execute_with(|| {
let (alice, bob, _charlie) = (1u64, 2, 3);

// Setup: Ensure programs exist and a valid verifying key is available
let programs_info = setup_programs();
let empty_program = vec![];
let program_hash = <Test as frame_system::Config>::Hashing::hash(&empty_program);

let network_verifying_key = entropy_shared::DAVE_VERIFYING_KEY;
pallet_staking_extension::JumpStartProgress::<Test>::set(JumpStartDetails {
jump_start_status: JumpStartStatus::Done,
confirmations: vec![],
verifying_key: Some(BoundedVec::try_from(network_verifying_key.to_vec()).unwrap()),
parent_key_threshold: 0,
});

// Test: Run through registration
assert_ok!(Registry::register_on_chain(
RuntimeOrigin::signed(alice),
bob,
programs_info.clone(),
));

// Validate: We expect that the program reference count has gone up
assert_eq!(
pallet_programs::Programs::<Test>::get(program_hash).unwrap().ref_counter,
1,
"The reference counter was not incremented during registration."
);
})
}

#[test]
fn it_registers_different_users_with_the_same_sig_req_account() {
new_test_ext().execute_with(|| {
Expand Down Expand Up @@ -173,7 +207,7 @@ fn it_registers_different_users_with_the_same_sig_req_account() {
#[test]
fn it_fails_registration_if_no_program_is_set() {
new_test_ext().execute_with(|| {
let (alice, bob) = (1u64, 2);
let (alice, bob) = (1, 2);

// Note that we also don't write any programs into storage here.
let programs_info = BoundedVec::try_from(vec![]).unwrap();
Expand All @@ -186,6 +220,28 @@ fn it_fails_registration_if_no_program_is_set() {
})
}

#[test]
fn it_fails_registration_if_an_empty_program_is_set() {
new_test_ext().execute_with(|| {
let (alice, bob) = (1, 2);

// Note that we also don't write any programs into storage here.
let non_existent_program = vec![];
let program_hash = <Test as frame_system::Config>::Hashing::hash(&non_existent_program);
let programs_info = BoundedVec::try_from(vec![ProgramInstance {
program_pointer: program_hash,
program_config: vec![],
}])
.unwrap();

// Test: Run through registration, this should fail
assert_noop!(
Registry::register_on_chain(RuntimeOrigin::signed(alice), bob, programs_info,),
Error::<Test>::NoProgramSet
);
})
}

#[test]
fn it_fails_registration_if_no_jump_start_has_happened() {
new_test_ext().execute_with(|| {
Expand Down Expand Up @@ -244,47 +300,6 @@ fn it_fails_registration_with_too_many_modifiable_keys() {
})
}

#[test]
fn it_fails_registration_if_parent_key_matches_derived_key() {
new_test_ext().execute_with(|| {})
}

#[test]
fn it_registers_a_user() {
new_test_ext().execute_with(|| {
let empty_program = vec![];
let program_hash = <Test as frame_system::Config>::Hashing::hash(&empty_program);
let programs_info = BoundedVec::try_from(vec![ProgramInstance {
program_pointer: program_hash,
program_config: vec![],
}])
.unwrap();
pallet_programs::Programs::<Test>::insert(
program_hash,
ProgramInfo {
bytecode: empty_program.clone(),
configuration_schema: empty_program.clone(),
auxiliary_data_schema: empty_program.clone(),
oracle_data_pointer: empty_program.clone(),
deployer: 1,
ref_counter: 0,
},
);

assert_ok!(Registry::register(
RuntimeOrigin::signed(1),
2 as <Test as frame_system::Config>::AccountId,
programs_info,
));
assert_eq!(Registry::dkg(0), vec![1u64.encode()]);
assert_eq!(
pallet_programs::Programs::<Test>::get(program_hash).unwrap().ref_counter,
1,
"ref counter is incremented"
);
});
}

#[test]
fn it_jumps_the_network() {
new_test_ext().execute_with(|| {
Expand Down Expand Up @@ -415,7 +430,7 @@ fn it_tests_jump_start_result() {
}

#[test]
fn it_changes_a_program_pointer() {
fn it_changes_a_program_instance() {
new_test_ext().execute_with(|| {
let empty_program = vec![];
let program_hash = <Test as frame_system::Config>::Hashing::hash(&empty_program);
Expand Down Expand Up @@ -466,16 +481,23 @@ fn it_changes_a_program_pointer() {
version_number: 1,
};

Registered::<Test>::insert(expected_verifying_key.clone(), &registered_info);
assert_eq!(Registry::registered(expected_verifying_key.clone()).unwrap(), registered_info);
RegisteredOnChain::<Test>::insert(expected_verifying_key.clone(), &registered_info);
assert_eq!(
Registry::registered_on_chain(expected_verifying_key.clone()).unwrap(),
registered_info
);

assert_ok!(Registry::change_program_instance(
RuntimeOrigin::signed(2),
expected_verifying_key.clone(),
new_programs_info.clone(),
));

registered_info.programs_data = new_programs_info;
assert_eq!(Registry::registered(expected_verifying_key.clone()).unwrap(), registered_info);
assert_eq!(
Registry::registered_on_chain(expected_verifying_key.clone()).unwrap(),
registered_info
);
assert_eq!(
pallet_programs::Programs::<Test>::get(program_hash).unwrap().ref_counter,
0,
Expand All @@ -495,6 +517,7 @@ fn it_changes_a_program_pointer() {
ProgramInstance { program_pointer: unreigistered_program_hash, program_config: vec![] },
])
.unwrap();

assert_noop!(
Registry::change_program_instance(
RuntimeOrigin::signed(2),
Expand All @@ -518,26 +541,8 @@ fn it_changes_a_program_pointer() {
#[test]
fn it_changes_a_program_mod_account() {
new_test_ext().execute_with(|| {
let empty_program = vec![];
let program_hash = <Test as frame_system::Config>::Hashing::hash(&empty_program);
let programs_info = BoundedVec::try_from(vec![ProgramInstance {
program_pointer: program_hash,
program_config: vec![],
}])
.unwrap();

pallet_programs::Programs::<Test>::insert(
program_hash,
ProgramInfo {
bytecode: empty_program.clone(),
configuration_schema: empty_program.clone(),
auxiliary_data_schema: empty_program.clone(),
oracle_data_pointer: empty_program.clone(),
deployer: 1,
ref_counter: 1,
},
);

// Setup: Ensure programs exist and a verifying key is available
let programs_info = setup_programs();
let expected_verifying_key = BoundedVec::default();

let mut registered_info = RegisteredInfo {
Expand All @@ -547,8 +552,11 @@ fn it_changes_a_program_mod_account() {
version_number: 1,
};

Registered::<Test>::insert(expected_verifying_key.clone(), &registered_info);
assert_eq!(Registry::registered(expected_verifying_key.clone()).unwrap(), registered_info);
RegisteredOnChain::<Test>::insert(expected_verifying_key.clone(), &registered_info);
assert_eq!(
Registry::registered_on_chain(expected_verifying_key.clone()).unwrap(),
registered_info
);

// Idk why this state could happen but still test to make sure it fails with a noop if ModifiableKeys not set
assert_noop!(
Expand Down Expand Up @@ -577,13 +585,15 @@ fn it_changes_a_program_mod_account() {
vec![expected_verifying_key.clone()],
"account 3 now has control of the account"
);

registered_info.program_modification_account = 3;
assert_eq!(
Registry::registered(expected_verifying_key.clone()).unwrap(),
Registry::registered_on_chain(expected_verifying_key.clone()).unwrap(),
registered_info,
"account 3 now in registered info"
);
assert_eq!(Registry::modifiable_keys(2), vec![], "account 2 no longer has control");

// account 2 no longer in control, fails
assert_noop!(
Registry::change_program_modification_account(
Expand All @@ -597,9 +607,8 @@ fn it_changes_a_program_mod_account() {
}

#[test]
fn it_doesnt_allow_double_registering() {
fn it_fails_on_non_matching_verifying_keys() {
new_test_ext().execute_with(|| {
// register a user
let empty_program = vec![];
let program_hash = <Test as frame_system::Config>::Hashing::hash(&empty_program);
let programs_info = BoundedVec::try_from(vec![ProgramInstance {
Expand Down Expand Up @@ -629,32 +638,3 @@ fn it_doesnt_allow_double_registering() {
);
});
}

#[test]
fn it_fails_no_program() {
new_test_ext().execute_with(|| {
// register a user
let non_existing_program = vec![10];
let program_hash = <Test as frame_system::Config>::Hashing::hash(&non_existing_program);
let programs_info = BoundedVec::try_from(vec![ProgramInstance {
program_pointer: program_hash,
program_config: vec![],
}])
.unwrap();

assert_noop!(
Registry::register(RuntimeOrigin::signed(1), 2, programs_info),
Error::<Test>::NoProgramSet
);
});
}

#[test]
fn it_fails_empty_program_list() {
new_test_ext().execute_with(|| {
assert_noop!(
Registry::register(RuntimeOrigin::signed(1), 2, BoundedVec::try_from(vec![]).unwrap(),),
Error::<Test>::NoProgramSet
);
});
}

0 comments on commit e0a1d31

Please sign in to comment.