Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use SessionId instead of Scope for nonces and signatures exchange messages #19

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

24 changes: 12 additions & 12 deletions crates/db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use async_trait::async_trait;
use bitcoin::{hashes::sha256, OutPoint, XOnlyPublicKey};
use bitcoin::{OutPoint, XOnlyPublicKey};
use libp2p_identity::PeerId;
use musig2::{PartialSignature, PubNonce};
use serde::{de::DeserializeOwned, Serialize};
use strata_p2p_types::OperatorPubKey;
use strata_p2p_types::{OperatorPubKey, Scope, SessionId};
use thiserror::Error;

mod prost_serde;
Expand Down Expand Up @@ -97,51 +97,51 @@ where
async fn get_partial_signatures(
&self,
operator_pk: &OperatorPubKey,
scope: sha256::Hash,
session_id: SessionId,
) -> DBResult<Option<PartialSignaturesEntry>> {
let key = format!("sigs-{operator_pk}_{scope}");
let key = format!("sigs-{operator_pk}_{session_id}");
self.get(key).await
}

async fn set_partial_signatures_if_not_exists(
&self,
scope: sha256::Hash,
session_id: SessionId,
entry: PartialSignaturesEntry,
) -> DBResult<bool> {
let key = format!("sigs-{}_{scope}", entry.key);
let key = format!("sigs-{}_{session_id}", entry.key);
self.set_if_not_exists(key, entry).await
}

async fn get_pub_nonces(
&self,
operator_pk: &OperatorPubKey,
scope: sha256::Hash,
session_id: SessionId,
) -> DBResult<Option<NoncesEntry>> {
let key = format!("nonces-{operator_pk}_{scope}");
let key = format!("nonces-{operator_pk}_{session_id}");
self.get(key).await
}

async fn set_pub_nonces_if_not_exist(
&self,
scope: sha256::Hash,
session_id: SessionId,
entry: NoncesEntry,
) -> DBResult<bool> {
let key = format!("nonces-{}_{scope}", entry.key);
let key = format!("nonces-{}_{session_id}", entry.key);
self.set_if_not_exists(key, entry).await
}

async fn get_deposit_setup(
&self,
operator_pk: &OperatorPubKey,
scope: sha256::Hash,
scope: Scope,
) -> DBResult<Option<DepositSetupEntry<DepositSetupPayload>>> {
let key = format!("setup-{operator_pk}_{scope}");
self.get(key).await
}

async fn set_deposit_setup_if_not_exists(
&self,
scope: sha256::Hash,
scope: Scope,
setup: DepositSetupEntry<DepositSetupPayload>,
) -> DBResult<bool> {
let key = format!("setup-{}_{scope}", setup.key);
Expand Down
17 changes: 7 additions & 10 deletions crates/db/src/sled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,11 @@ impl From<sled::Error> for RepositoryError {
mod tests {
use std::sync::Arc;

use bitcoin::{
hashes::{sha256, Hash},
OutPoint, XOnlyPublicKey,
};
use bitcoin::{OutPoint, XOnlyPublicKey};
use musig2::{sign_partial, AggNonce, KeyAggContext, SecNonce};
use rand::thread_rng;
use secp256k1::{All, Keypair, Secp256k1};
use strata_p2p_types::OperatorPubKey;
use strata_p2p_types::{OperatorPubKey, SessionId};

use crate::{
sled::AsyncDB, GenesisInfoEntry, NoncesEntry, PartialSignaturesEntry, RepositoryExt,
Expand All @@ -128,15 +125,15 @@ mod tests {
let pub_nonce = sec_nonce.public_nonce();

let operator_pk = OperatorPubKey::from(vec![0x8; 32]);
let scope = sha256::Hash::all_zeros();
let session_id = SessionId::hash(b"session_id");

let nonces_entry = NoncesEntry {
entry: vec![pub_nonce.clone()],
signature: vec![0x8; 32],
key: operator_pk.clone(),
};

db.set_pub_nonces_if_not_exist(scope, nonces_entry)
db.set_pub_nonces_if_not_exist(session_id, nonces_entry)
.await
.unwrap();

Expand All @@ -152,12 +149,12 @@ mod tests {
key: operator_pk.clone(),
};

db.set_partial_signatures_if_not_exists(scope, sigs_entry)
db.set_partial_signatures_if_not_exists(session_id, sigs_entry)
.await
.expect("Failed to set signature");

let retrieved_signature = db
.get_partial_signatures(&operator_pk, scope)
.get_partial_signatures(&operator_pk, session_id)
.await
.unwrap()
.expect("Failed to retrieve signature");
Expand All @@ -182,7 +179,7 @@ mod tests {
assert_eq!(got_keys, checkpoint_pubkeys);

let retrieved_pub_nonces = db
.get_pub_nonces(&operator_pk, scope)
.get_pub_nonces(&operator_pk, session_id)
.await
.unwrap()
.unwrap();
Expand Down
52 changes: 27 additions & 25 deletions crates/p2p/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//! Commands for P2P implementation from operator implementation.

use bitcoin::{hashes::sha256, OutPoint, XOnlyPublicKey};
use bitcoin::{OutPoint, XOnlyPublicKey};
use libp2p::identity::secp256k1;
use musig2::{PartialSignature, PubNonce};
use prost::Message;
use strata_p2p_types::OperatorPubKey;
use strata_p2p_types::{OperatorPubKey, Scope, SessionId};
use strata_p2p_wire::p2p::v1::{
DepositNonces, DepositSetup, DepositSigs, GenesisInfo, GetMessageRequest, GossipsubMsg,
GossipsubMsgDepositKind, GossipsubMsgKind,
DepositSetup, GenesisInfo, GetMessageRequest, GossipsubMsg, UnsignedGossipsubMsg,
};

/// Ask P2P implementation to distribute some data across network.
Expand All @@ -34,15 +33,15 @@ pub enum UnsignedPublishMessage<DepositSetupPayload> {
checkpoint_pubkeys: Vec<XOnlyPublicKey>,
},
DepositSetup {
scope: sha256::Hash,
scope: Scope,
payload: DepositSetupPayload,
},
DepositNonces {
scope: sha256::Hash,
Musig2NoncesExchange {
session_id: SessionId,
pub_nonces: Vec<PubNonce>,
},
PartialSignatures {
scope: sha256::Hash,
Musig2SignaturesExchange {
session_id: SessionId,
partial_sigs: Vec<PartialSignature>,
},
}
Expand All @@ -52,7 +51,7 @@ impl<DSP: Message + Clone> From<PublishMessage<DSP>> for GossipsubMsg<DSP> {
GossipsubMsg {
signature: value.signature,
key: value.key,
kind: value.msg.into(),
unsigned: value.msg.into(),
}
}
}
Expand All @@ -61,7 +60,7 @@ impl<DSP: Message + Default + Clone> UnsignedPublishMessage<DSP> {
/// Sign `self` using supplied `keypair`. Returns a `Command`
/// with resulting signature and public key from `keypair`.
pub fn sign_secp256k1(&self, keypair: &secp256k1::Keypair) -> PublishMessage<DSP> {
let kind: GossipsubMsgKind<DSP> = self.clone().into();
let kind: UnsignedGossipsubMsg<DSP> = self.clone().into();
let msg = kind.content();
let signature = keypair.secret().sign(&msg);

Expand All @@ -73,32 +72,35 @@ impl<DSP: Message + Default + Clone> UnsignedPublishMessage<DSP> {
}
}

impl<DSP: Message + Clone> From<UnsignedPublishMessage<DSP>> for GossipsubMsgKind<DSP> {
impl<DSP: Message + Clone> From<UnsignedPublishMessage<DSP>> for UnsignedGossipsubMsg<DSP> {
fn from(value: UnsignedPublishMessage<DSP>) -> Self {
match value {
UnsignedPublishMessage::GenesisInfo {
pre_stake_outpoint,
checkpoint_pubkeys,
} => GossipsubMsgKind::GenesisInfo(GenesisInfo {
} => UnsignedGossipsubMsg::GenesisInfo(GenesisInfo {
checkpoint_pubkeys,
pre_stake_outpoint,
}),
UnsignedPublishMessage::DepositSetup { scope, payload } => GossipsubMsgKind::Deposit {
scope,
kind: GossipsubMsgDepositKind::Setup(DepositSetup { payload }),
},
UnsignedPublishMessage::DepositNonces { scope, pub_nonces } => {
GossipsubMsgKind::Deposit {
UnsignedPublishMessage::DepositSetup { scope, payload } => {
UnsignedGossipsubMsg::DepositSetup {
scope,
kind: GossipsubMsgDepositKind::Nonces(DepositNonces { nonces: pub_nonces }),
setup: DepositSetup { payload },
}
}
UnsignedPublishMessage::PartialSignatures {
scope,
UnsignedPublishMessage::Musig2NoncesExchange {
session_id,
pub_nonces,
} => UnsignedGossipsubMsg::Musig2NoncesExchange {
session_id,
nonces: pub_nonces,
},
UnsignedPublishMessage::Musig2SignaturesExchange {
session_id,
partial_sigs,
} => GossipsubMsgKind::Deposit {
scope,
kind: GossipsubMsgDepositKind::Sigs(DepositSigs { partial_sigs }),
} => UnsignedGossipsubMsg::Musig2SignaturesExchange {
session_id,
signatures: partial_sigs,
},
}
}
Expand Down
Loading
Loading