Skip to content

Commit

Permalink
Tests for Job result submission event (#550)
Browse files Browse the repository at this point in the history
* submit dkg phase1 result test

* update test

* update tests

* update seed while inserting into key store
  • Loading branch information
salman01zp authored Mar 12, 2024
1 parent 57ec71c commit d208ef5
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 11 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 4 additions & 11 deletions node/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,19 @@ fn insert_account_keys_into_keystore<TPublic: Public>(
"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 = <TPublic::Pair as Pair>::from_seed_slice(&seed).unwrap().public();
let pub_key = get_from_seed::<TPublic>(&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
Expand Down
9 changes: 9 additions & 0 deletions tangle-subxt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

3 changes: 3 additions & 0 deletions tangle-subxt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ pub mod tangle_runtime;
pub use parity_scale_codec;
pub use scale_info;
pub use subxt;

#[cfg(test)]
mod test;
125 changes: 125 additions & 0 deletions tangle-subxt/src/test.rs
Original file line number Diff line number Diff line change
@@ -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::<PolkadotConfig>::new().await.unwrap();
let alice = subxt_signer::sr25519::dev::alice();
let bob = subxt_signer::sr25519::dev::bob();
let alice_account_id =
<subxt_signer::sr25519::Keypair as Signer<PolkadotConfig>>::account_id(&alice);
let bob_account_id =
<subxt_signer::sr25519::Keypair as Signer<PolkadotConfig>>::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::<AccountId32>(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();
}

0 comments on commit d208ef5

Please sign in to comment.