From d208ef58cc896e6c9e4d07f928c150b7c1958144 Mon Sep 17 00:00:00 2001 From: Salman Pathan Date: Tue, 12 Mar 2024 21:09:26 +0700 Subject: [PATCH] Tests for Job result submission event (#550) * submit dkg phase1 result test * update test * update tests * update seed while inserting into key store --- Cargo.lock | 27 +++++++++ node/src/utils.rs | 15 ++--- tangle-subxt/Cargo.toml | 9 +++ tangle-subxt/src/lib.rs | 3 + tangle-subxt/src/test.rs | 125 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 168 insertions(+), 11 deletions(-) create mode 100644 tangle-subxt/src/test.rs diff --git a/Cargo.lock b/Cargo.lock index aaef5d9b5..ecb7a94af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14037,6 +14037,27 @@ dependencies = [ "thiserror", ] +[[package]] +name = "subxt-signer" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f88a76a5d114bfae2f6f9cc1491c46173ecc3fb2b9e53948eb3c8d43d4b43ab5" +dependencies = [ + "bip39", + "hex", + "hmac 0.12.1", + "parity-scale-codec 3.6.9", + "pbkdf2 0.12.2", + "regex", + "schnorrkel", + "secrecy", + "sha2 0.10.8", + "sp-core-hashing 15.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "subxt", + "thiserror", + "zeroize", +] + [[package]] name = "svm-rs" version = "0.3.5" @@ -14365,9 +14386,15 @@ dependencies = [ name = "tangle-subxt" version = "0.6.1" dependencies = [ + "hex", "parity-scale-codec 3.6.9", "scale-info", + "sp-core", + "sp-io", "subxt", + "subxt-signer", + "tangle-crypto-primitives", + "tokio", ] [[package]] diff --git a/node/src/utils.rs b/node/src/utils.rs index 84e1b948c..685a65269 100644 --- a/node/src/utils.rs +++ b/node/src/utils.rs @@ -60,26 +60,19 @@ fn insert_account_keys_into_keystore( "Alice" | "Bob" | "Charlie" | "Dave" | "Eve" | "Ferdie" if chain_type == ChainType::Development || chain_type == ChainType::Local => { - let a = node_name.chars().next().unwrap() as u8; - vec![a; 32].try_into().unwrap() + node_name.to_string() }, _ => { let mut seed_raw = [0u8; 32]; rand::thread_rng().fill(&mut seed_raw[..]); - seed_raw + hex::encode(seed_raw) }, }; - let pub_key = ::from_seed_slice(&seed).unwrap().public(); + let pub_key = get_from_seed::(&seed).to_raw_vec(); if let Some(keystore) = key_store { - let _ = Keystore::insert( - &*keystore, - key_type, - &format!("0x{}", hex::encode(seed)), - &pub_key.to_raw_vec(), - ); + let _ = Keystore::insert(&*keystore, key_type, &format!("//{seed}"), &pub_key); } - println!("++++++++++++++++++++++++++++++++++++++++++++++++ AUTO GENERATED KEYS '{}' key inserted to keystore diff --git a/tangle-subxt/Cargo.toml b/tangle-subxt/Cargo.toml index 2dfb84c13..47a60a5ca 100644 --- a/tangle-subxt/Cargo.toml +++ b/tangle-subxt/Cargo.toml @@ -11,3 +11,12 @@ repository = { workspace = true } parity-scale-codec = { workspace = true } scale-info = { workspace = true } subxt = { version = "0.34.0" } + +[dev-dependencies] +subxt-signer = { version = "0.34.0", default-features = false, features = ["subxt", "sr25519", "native"] } +sp-core = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.7.0" } +sp-io = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.7.0" } +tokio = { version = "1.32.0", features = ["full"] } +tangle-crypto-primitives = { path = "../primitives/crypto"} +hex = {workspace = true } + diff --git a/tangle-subxt/src/lib.rs b/tangle-subxt/src/lib.rs index 38464183c..726e39162 100644 --- a/tangle-subxt/src/lib.rs +++ b/tangle-subxt/src/lib.rs @@ -12,3 +12,6 @@ pub mod tangle_runtime; pub use parity_scale_codec; pub use scale_info; pub use subxt; + +#[cfg(test)] +mod test; diff --git a/tangle-subxt/src/test.rs b/tangle-subxt/src/test.rs new file mode 100644 index 000000000..e27db9135 --- /dev/null +++ b/tangle-subxt/src/test.rs @@ -0,0 +1,125 @@ +use sp_core::Pair; +use subxt::OnlineClient; +use subxt::{self, tx::Signer, utils::AccountId32, PolkadotConfig}; +use tangle_runtime::api::runtime_types::pallet_roles::profile::Record; + +use crate::tangle_runtime; +use crate::tangle_runtime::api::{ + self, + runtime_types::{ + bounded_collections::bounded_vec::BoundedVec, + tangle_primitives::{jobs, roles}, + }, +}; + +#[tokio::test] +#[ignore = "need to be run manually"] +async fn test_job_submission_event() { + let subxt_client = OnlineClient::::new().await.unwrap(); + let alice = subxt_signer::sr25519::dev::alice(); + let bob = subxt_signer::sr25519::dev::bob(); + let alice_account_id = + >::account_id(&alice); + let bob_account_id = + >::account_id(&bob); + + let alice_role_key = sp_core::ecdsa::Pair::from_string("//Alice", None).unwrap(); + println!("Alice role key: {:?}", alice_role_key.public()); + + let bob_role_key = sp_core::ecdsa::Pair::from_string("//Bob", None).unwrap(); + println!("Bob role key: {:?}", bob_role_key.public()); + + let profile = api::runtime_types::pallet_roles::profile::Profile::Shared( + api::runtime_types::pallet_roles::profile::SharedRestakeProfile { + records: BoundedVec(vec![Record { + amount: None, + role: roles::RoleType::Tss( + roles::tss::ThresholdSignatureRoleType::DfnsCGGMP21Secp256k1, + ), + }]), + amount: 100 * 1_000_000_000, + }, + ); + let create_profile_tx = tangle_runtime::api::tx().roles().create_profile(profile, None); + + let _hash = subxt_client + .tx() + .sign_and_submit_then_watch_default(&create_profile_tx, &alice) + .await + .unwrap() + .wait_for_finalized_success() + .await + .unwrap(); + + let _hash = subxt_client + .tx() + .sign_and_submit_then_watch_default(&create_profile_tx, &bob) + .await + .unwrap() + .wait_for_finalized_success() + .await + .unwrap(); + + let dkg_phase_one = jobs::JobSubmission { + expiry: 100u64, + ttl: 100u64, + job_type: jobs::JobType::DKGTSSPhaseOne(jobs::tss::DKGTSSPhaseOneJobType { + participants: BoundedVec::(vec![ + alice_account_id.clone(), + bob_account_id.clone(), + ]), + threshold: 1u8, + permitted_caller: None, + role_type: roles::tss::ThresholdSignatureRoleType::DfnsCGGMP21Secp256k1, + __subxt_unused_type_params: Default::default(), + }), + fallback: jobs::FallbackOptions::Destroy, + }; + + let jobs_tx = tangle_runtime::api::tx().jobs().submit_job(dkg_phase_one); + let _hash = subxt_client + .tx() + .sign_and_submit_then_watch_default(&jobs_tx, &alice) + .await + .unwrap() + .wait_for_finalized_success() + .await + .unwrap(); + + let dkg_key = sp_core::ecdsa::Pair::from_seed(&[3u8; 32]); + let dkg_pubkey = dkg_key.public(); + let dkg_pubkey_hash = sp_core::hashing::keccak_256(dkg_pubkey.as_ref()); + + let alice_signature = alice_role_key.sign_prehashed(&dkg_pubkey_hash); + let bob_signature = bob_role_key.sign_prehashed(&dkg_pubkey_hash); + + let dkg_phase_one_result = jobs::JobResult::DKGPhaseOne(jobs::tss::DKGTSSKeySubmissionResult { + key: BoundedVec(dkg_pubkey.0.to_vec()), + participants: BoundedVec(vec![ + BoundedVec(alice_account_id.0.to_vec()), + BoundedVec(bob_account_id.0.to_vec()), + ]), + signature_scheme: jobs::tss::DigitalSignatureScheme::Ecdsa, + signatures: BoundedVec(vec![ + BoundedVec(alice_signature.0.to_vec()), + BoundedVec(bob_signature.0.to_vec()), + ]), + threshold: 1, + __subxt_unused_type_params: Default::default(), + }); + + let job_result_tx = tangle_runtime::api::tx().jobs().submit_job_result( + roles::RoleType::Tss(roles::tss::ThresholdSignatureRoleType::DfnsCGGMP21Secp256k1), + 0, + dkg_phase_one_result, + ); + + let _hash = subxt_client + .tx() + .sign_and_submit_then_watch_default(&job_result_tx, &alice) + .await + .unwrap() + .wait_for_finalized_success() + .await + .unwrap(); +}