Skip to content

Commit

Permalink
feat: update test to use multi-node env (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA authored Feb 21, 2025
1 parent f5881a0 commit 528a49b
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 162 deletions.
13 changes: 11 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ edition = "2021"
manager = { Evm = "BlsBlueprint" }

[dependencies]
blueprint-sdk = { git = "https://github.com/tangle-network/gadget.git", features = ["std", "macros", "tangle", "networking", "local-store", "round-based-compat", "networking-sp-core-ecdsa"] }
blueprint-sdk = { git = "https://github.com/tangle-network/gadget.git", features = [
"std",
"macros",
"tangle",
"networking",
"local-store",
"round-based-compat",
"networking-sp-core-ecdsa"
] }
color-eyre = { version = "0.6", features = ["tracing-error", "color-spantrace"] }
hex = { version = "0.4.3", default-features = false }
serde = { version = "1.0.214", features = ["derive"] }
round-based = { version = "0.3.2", features = ["runtime-tokio", "derive"] }
round-based = { version = "0.4.1", features = ["runtime-tokio", "derive"] }
thiserror = "2.0.3"
itertools = "0.13.0"
tracing = "0.1.41"

# MPC specific deps
bls12_381_plus = "0.8.18"
Expand Down
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ remappings_location = "txt"
remappings_version = false

[dependencies]
tnt-core = "0.2.0"
tnt-core = "0.3.0"

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
2 changes: 1 addition & 1 deletion remappings.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tnt-core/=dependencies/tnt-core-0.2.0/src
tnt-core/=dependencies/tnt-core-0.3.0/src
8 changes: 4 additions & 4 deletions soldeer.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[[dependencies]]
name = "tnt-core"
version = "0.2.0"
url = "https://soldeer-revisions.s3.amazonaws.com/tnt-core/0_2_0_28-01-2025_15:09:29_tnt-core.zip"
checksum = "77f5a4fb2e7257f46ac8642ce7daa5ae98a2107a934fc0c65a6143ccca36ea7e"
integrity = "db8abe72d2bada2a6730fbe0abf447ddb44a63d31e1a08ed97d049c9253d592a"
version = "0.3.0"
url = "https://soldeer-revisions.s3.amazonaws.com/tnt-core/0_3_0_21-02-2025_19:54:56_tnt-core.zip"
checksum = "f4939e5485b12372bf1e81e148fe48535295fb13fc14a1572e340475b52ff3d1"
integrity = "63fac62953c0dc031f4f12e3143d0ae4adfade69f5c6b1263ac610dc6db6b3ba"
31 changes: 19 additions & 12 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::keygen_state_machine::BlsState;
use blueprint_sdk as sdk;
use blueprint_sdk::networking::service_handle::NetworkServiceHandle;
use blueprint_sdk::networking::InstanceMsgPublicKey;
use color_eyre::eyre::eyre;
use color_eyre::{Report, Result};
use sdk::clients::GadgetServicesClient;
Expand All @@ -11,18 +13,17 @@ use sdk::crypto::tangle_pair_signer::sp_core;
use sdk::keystore::backends::Backend;
use sdk::logging;
use sdk::macros::contexts::{KeystoreContext, ServicesContext, TangleClientContext};
use sdk::networking::networking::NetworkMultiplexer;
use sdk::stores::local_database::LocalDatabase;
use sdk::tangle_subxt;
use sdk::tangle_subxt::tangle_testnet_runtime::api;
use sp_core::ecdsa::Public;
use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashSet};
use std::path::PathBuf;
use std::sync::Arc;
use tangle_subxt::subxt_core::utils::AccountId32;

/// The network protocol version for the BLS service
const NETWORK_PROTOCOL: &str = "/bls/gennaro/1.0.0";
pub(crate) const NETWORK_PROTOCOL: &str = "bls/gennaro/1.0.0";

/// BLS Service Context that holds all the necessary context for the service
/// to run. This structure implements various traits for keystore, client, and service
Expand All @@ -33,7 +34,7 @@ pub struct BlsContext {
pub config: GadgetConfiguration,
#[call_id]
pub call_id: Option<u64>,
pub network_backend: Arc<NetworkMultiplexer>,
pub network_backend: NetworkServiceHandle,
pub store: Arc<LocalDatabase<BlsState>>,
pub identity: sp_core::ecdsa::Pair,
}
Expand All @@ -46,14 +47,20 @@ impl BlsContext {
/// Returns an error if:
/// - Network initialization fails
/// - Configuration is invalid
pub fn new(config: GadgetConfiguration) -> Result<Self> {
let network_config = config
.libp2p_network_config(NETWORK_PROTOCOL)
.map_err(|err| eyre!("Failed to create network configuration: {err}"))?;
pub async fn new(config: GadgetConfiguration) -> Result<Self> {
let operator_keys: HashSet<InstanceMsgPublicKey> = config
.tangle_client()
.await?
.get_operators()
.await?
.values()
.map(|key| InstanceMsgPublicKey(*key))
.collect();

let identity = network_config.secret_key.0.clone();
let gossip_handle = sdk::networking::setup::start_p2p_network(network_config)
.map_err(|err| eyre!("Failed to start the P2P network: {err}"))?;
let network_config = config.libp2p_network_config(NETWORK_PROTOCOL)?;
let identity = network_config.instance_key_pair.0.clone();

let network_backend = config.libp2p_start_network(network_config, operator_keys)?;

let keystore_dir = PathBuf::from(&config.keystore_uri).join("bls.json");
let store = Arc::new(LocalDatabase::open(keystore_dir));
Expand All @@ -63,7 +70,7 @@ impl BlsContext {
identity,
call_id: None,
config,
network_backend: Arc::new(NetworkMultiplexer::new(gossip_handle)),
network_backend,
})
}

Expand Down
18 changes: 8 additions & 10 deletions src/keygen.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use crate::context::BlsContext;
use crate::keygen_state_machine::KeygenMsg;
use blueprint_sdk as sdk;
use round_based::PartyIndex;
use sdk::error::Error as GadgetError;
use sdk::event_listeners::tangle::events::TangleEventListener;
use sdk::event_listeners::tangle::services::{services_post_processor, services_pre_processor};
use sdk::job;
use sdk::logging;
use sdk::networking::round_based_compat::NetworkDeliveryWrapper;
use sdk::networking::GossipMsgPublicKey;
use sdk::networking::round_based_compat::RoundBasedNetworkAdapter;
use sdk::networking::InstanceMsgPublicKey;
use sdk::tangle_subxt::tangle_testnet_runtime::api::services::events::JobCalled;
use std::collections::BTreeMap;
use std::collections::HashMap;

#[job(
id = 0,
Expand Down Expand Up @@ -44,19 +45,16 @@ pub async fn keygen(t: u16, context: BlsContext) -> Result<Vec<u8>, GadgetError>
.current_call_id()
.await
.map_err(|e| KeygenError::ContextError(e.to_string()))?;

// Setup party information
let (i, operators) = context
.get_party_index_and_operators()
.await
.map_err(|e| KeygenError::ContextError(e.to_string()))?;

let parties: BTreeMap<u16, GossipMsgPublicKey> = operators
let parties: HashMap<u16, InstanceMsgPublicKey> = operators
.into_iter()
.enumerate()
.map(|(j, (_, ecdsa))| (j as PartyIndex, GossipMsgPublicKey(ecdsa)))
.map(|(j, (_, ecdsa))| (j as PartyIndex, InstanceMsgPublicKey(ecdsa)))
.collect();

let n = parties.len() as u16;
let i = i as u16;

Expand All @@ -68,11 +66,11 @@ pub async fn keygen(t: u16, context: BlsContext) -> Result<Vec<u8>, GadgetError>
hex::encode(deterministic_hash)
);

let network = NetworkDeliveryWrapper::new(
let network = RoundBasedNetworkAdapter::<KeygenMsg>::new(
context.network_backend.clone(),
i,
deterministic_hash,
parties.clone(),
crate::context::NETWORK_PROTOCOL,
);

let party = round_based::party::MpcParty::connected(network);
Expand Down
Loading

0 comments on commit 528a49b

Please sign in to comment.