Skip to content

Commit

Permalink
begin integrating into DKG (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Braun committed Sep 15, 2023
1 parent 4746ceb commit 3e66bad
Show file tree
Hide file tree
Showing 14 changed files with 461 additions and 496 deletions.
2 changes: 2 additions & 0 deletions dkg-gadget/src/async_protocols/ecdsa/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod keygen;
pub mod sign;
File renamed without changes.
98 changes: 98 additions & 0 deletions dkg-gadget/src/async_protocols/frost/keygen/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use std::collections::HashSet;
use sc_client_api::Backend;
use sp_core::hashing::sha2_256;
use sp_runtime::traits::Block;
use tokio::sync::mpsc::UnboundedReceiver;
use dkg_primitives::types::{DKGError, DKGMessage, NetworkMsgPayload, SignedDKGMessage};
use dkg_runtime_primitives::crypto::AuthorityId;
use dkg_runtime_primitives::gossip_messages::DKGKeygenMessage;
use crate::async_protocols::blockchain_interface::BlockchainInterface;
use crate::async_protocols::remote::AsyncProtocolRemote;
use crate::Client;
use crate::dkg_modules::wt_frost::{FrostMessage, NetInterface};
use crate::gossip_engine::GossipEngineIface;
use crate::worker::DKGWorker;

pub struct FrostKeygen<B, BE, C, GE, BI>
where
B: Block,
BE: Backend<B>,
C: Client<B, BE>,
GE: GossipEngineIface, {
pub dkg_worker: DKGWorker<B, BE, C, GE>,
pub remote: AsyncProtocolRemote<C>,
pub message_receiver: UnboundedReceiver<SignedDKGMessage<AuthorityId>>,
pub authority_id: AuthorityId,
pub keygen_protocol_hash: [u8; 32],
pub received_messages: HashSet<[u8;32]>
pub engine: BI
}

impl<B, BE, C, GE, BI: BlockchainInterface> FrostKeygen<B, BE, C, GE, BI> {
pub fn new(dkg_worker: DKGWorker<B, BE, C, GE>, engine: BI, remote: AsyncProtocolRemote<C>, authority_id: AuthorityId, retry_id: usize) -> Self {
let message_receiver = remote
.rx_keygen_signing
.lock()
.take()
.expect("rx_keygen_signing already taken");

let mut data = retry_id.to_be_bytes().to_vec();
data.extend_from_slice(&remote.session_id.to_be_bytes());

let keygen_protocol_hash = sha2_256(&data);
let received_messages = HashSet::new();

Self { dkg_worker, engine, remote, message_receiver, authority_id, keygen_protocol_hash, received_messages }
}
}

impl<B, BE, C, GE, BI: BlockchainInterface> NetInterface for FrostKeygen<B, BE, C, GE, BI>
where
B: Block,
BE: Backend<B>,
C: Client<B, BE>,
GE: GossipEngineIface {
type Error = DKGError;

async fn next_message(&mut self) -> Result<Option<FrostMessage>, Self::Error> {
loop {
let message = self.message_receiver.recv().await?;
// When we receive a message, it is filtered through the Job Manager, and as such
// we have these guarantees:
// * The SSID is correct, the block ID and session ID are acceptable, and the task hash is correct
// We do not need to check these things here, but we do need to check the signature
let message = self.engine.verify_signature_against_authorities(message).await?;
let message_bin = message.payload.payload();
let message_hash = sha2_256(message_bin);

if !self.received_messages.insert(message_hash) {
self.dkg_worker.logger.info("Received duplicate FROST keygen message, ignoring");
continue;
}

// Check to make sure we haven't already received the message
let deserialized = bincode2::deserialize::<FrostMessage>(message_bin)
.map_err(|err| DKGError::GenericError { reason: err.to_string() })?;
return Ok(Some(deserialized))
}
}

async fn send_message(&mut self, msg: FrostMessage) -> Result<(), Self::Error> {
let keygen_msg = bincode2::serialize(&msg)
.map_err(|err| DKGError::GenericError { reason: err.to_string() })?;
let message = DKGMessage {
sender_id: self.authority_id.clone(),
recipient_id: None, // We always gossip in FROST
payload: NetworkMsgPayload::Keygen(DKGKeygenMessage {
sender_id: 0, // We do not care to put the sender ID in the message for FROST, since it is already inside the FrostMessage
keygen_msg,// The Frost Message
keygen_protocol_hash: self.keygen_protocol_hash,
}),
session_id: self.remote.session_id,
associated_block_id: self.remote.associated_block_id,
ssid: self.remote.ssid,
};

self.engine.sign_and_send_msg(message)
}
}
2 changes: 2 additions & 0 deletions dkg-gadget/src/async_protocols/frost/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod keygen;
pub mod sign;
Empty file.
4 changes: 2 additions & 2 deletions dkg-gadget/src/async_protocols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

pub mod blockchain_interface;
pub mod incoming;
pub mod keygen;
pub mod remote;
pub mod sign;
pub mod state_machine;
pub mod state_machine_wrapper;
pub mod ecdsa;
pub mod frost;
use sp_runtime::traits::Get;
#[cfg(test)]
pub mod test_utils;
Expand Down
1 change: 0 additions & 1 deletion dkg-gadget/src/dkg_modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use wt_frost::WTFrostDKG;

pub mod mp_ecdsa;
pub mod wt_frost;
pub mod wt_frost_wsts;

/// Setup parameters for the Keygen protocol
pub enum KeygenProtocolSetupParameters<B: Block> {
Expand Down
Loading

0 comments on commit 3e66bad

Please sign in to comment.