Skip to content

Commit

Permalink
continue debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Braun committed Aug 30, 2023
1 parent b59153c commit e590b43
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 49 deletions.
8 changes: 5 additions & 3 deletions dkg-gadget/src/async_protocols/keygen/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ where
let bad_actors_rx = params.handle.current_round_blame.clone();

let protocol = async move {
params.logger.info_keygen(
"Will execute keygen since local is in best authority set".to_string(),
);
params.logger.info_keygen(format!(
"Will execute keygen since local is in best authority set len={} | {:?}",
params.best_authorities.len(),
params.best_authorities
));
let t = threshold;
let n = params.best_authorities.len() as u16;
// wait for the start signal
Expand Down
7 changes: 6 additions & 1 deletion dkg-gadget/src/async_protocols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl<
KeygenRound::Next => "NEXT",
};
let keygen_protocol_hash = hex::encode(keygen_protocol_hash);
write!(f, "{ty} | Keygen: (i, t, n, r) | hash: {keygen_protocol_hash} = ({i}, {t}, {n}, {associated_round_id:?})")
write!(f, "{ty} | Keygen: (i, t, n, r) = ({i}, {t}, {n}, {associated_round_id:?}) | hash: {keygen_protocol_hash}")
},
ProtocolType::Offline { i, unsigned_proposal_batch, .. } => {
write!(
Expand Down Expand Up @@ -452,6 +452,11 @@ where
<SM as StateMachine>::MessageBody: Send + Serialize + MessageRoundID,
<SM as StateMachine>::Output: Send,
{
params.logger.debug(format!(
"Will be starting an async protocol for authority {:?}",
params.authority_public_key
));

let (incoming_tx_proto, incoming_rx_proto) = SM::generate_channel();
let (outgoing_tx, outgoing_rx) = futures::channel::mpsc::unbounded();

Expand Down
6 changes: 3 additions & 3 deletions dkg-gadget/src/async_protocols/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
async_protocols::CurrentRoundBlame, debug_logger::DebugLogger, worker::ProtoStageType,
};
use atomic::Atomic;
use dkg_primitives::types::{DKGError, NetworkMsgPayload, SessionId, SignedDKGMessage};
use dkg_primitives::types::{DKGError, NetworkMsgPayload, SessionId, SignedDKGMessage, SSID};
use dkg_runtime_primitives::{
crypto::{AuthorityId, Public},
KEYGEN_TIMEOUT, SIGN_TIMEOUT,
Expand Down Expand Up @@ -46,7 +46,7 @@ pub struct AsyncProtocolRemote<C> {
pub(crate) session_id: SessionId,
pub(crate) associated_block_id: u64,
/// The signing set index. For keygen, this is always 0
pub(crate) ssid: u8,
pub(crate) ssid: SSID,
pub(crate) logger: DebugLogger,
status_history: Arc<Mutex<Vec<MetaHandlerStatus>>>,
/// Contains the mapping of index to authority id for this specific protocol. Varies between
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<C: AtLeast32BitUnsigned + Copy + Send> AsyncProtocolRemote<C> {
session_id: SessionId,
logger: DebugLogger,
associated_block_id: u64,
ssid: u8,
ssid: SSID,
proto_stage_type: ProtoStageType,
index_to_authority_mapping: Arc<HashMap<usize, AuthorityId>>,
) -> Self {
Expand Down
4 changes: 3 additions & 1 deletion dkg-gadget/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub mod worker {
// ============= Signing Manager ======================= //

pub mod signing_manager {
use dkg_primitives::types::SSID;

// the maximum number of tasks that the work manager tries to assign
pub const MAX_RUNNING_TASKS: usize = 1;

Expand All @@ -34,7 +36,7 @@ pub mod signing_manager {

// Max potential number of signing sets to generate for every proposal (equal to the number of
// retries)
pub const MAX_POTENTIAL_RETRIES_PER_UNSIGNED_PROPOSAL: u8 = u8::MAX - 1;
pub const MAX_POTENTIAL_RETRIES_PER_UNSIGNED_PROPOSAL: SSID = SSID::MAX - 1;
}

// ============= Networking ======================= //
Expand Down
4 changes: 2 additions & 2 deletions dkg-gadget/src/dkg_modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
Client,
};
use async_trait::async_trait;
use dkg_primitives::types::DKGError;
use dkg_primitives::types::{DKGError, SSID};
use dkg_runtime_primitives::{
crypto::{AuthorityId, Public},
BatchId, DKGApi, MaxAuthorities, MaxProposalLength, MaxProposalsInBatch, SessionId,
Expand Down Expand Up @@ -54,7 +54,7 @@ pub enum SigningProtocolSetupParameters<B: Block> {
>,
signing_set: Vec<KeygenPartyId>,
associated_block_id: NumberFor<B>,
ssid: u8,
ssid: SSID,
unsigned_proposal_hash: [u8; 32],
},
WTFrost {},
Expand Down
4 changes: 2 additions & 2 deletions dkg-gadget/src/dkg_modules/mp_ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
Client,
};
use async_trait::async_trait;
use dkg_primitives::types::DKGError;
use dkg_primitives::types::{DKGError, SSID};
use dkg_runtime_primitives::{crypto::AuthorityId, DKGApi, MaxAuthorities, MaxProposalLength};
use sc_client_api::Backend;
use sp_runtime::traits::{Block, NumberFor};
Expand Down Expand Up @@ -51,7 +51,7 @@ where
keygen_protocol_hash,
} = params
{
const KEYGEN_SSID: u8 = 0;
const KEYGEN_SSID: SSID = 0;
match self.dkg_worker.generate_async_proto_params(
best_authorities,
authority_public_key,
Expand Down
49 changes: 27 additions & 22 deletions dkg-gadget/src/signing_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
*,
};
use codec::Encode;
use dkg_primitives::utils::select_random_set;
use dkg_primitives::{types::SSID, utils::select_random_set};
use dkg_runtime_primitives::{
crypto::Public, BatchId, MaxProposalsInBatch, SessionId, StoredUnsignedProposalBatch,
SIGN_TIMEOUT,
Expand All @@ -32,6 +32,7 @@ use sp_arithmetic::traits::SaturatedConversion;
use sp_runtime::traits::Header;
use std::sync::atomic::{AtomicBool, Ordering};
use webb_proposals::TypedChainId;

/// For balancing the amount of work done by each node
pub mod work_manager;

Expand Down Expand Up @@ -59,7 +60,7 @@ pub struct SigningManager<B: Block, BE, C, GE> {

#[derive(Default)]
struct SigningSetHistoryTracker {
ssids_attempted: HashSet<u8>,
ssids_attempted: HashSet<SSID>,
attempted_sets: HashSet<ProposedSigningSet>,
}

Expand Down Expand Up @@ -179,6 +180,7 @@ where
.into_iter()
.flat_map(|(i, p)| KeygenPartyId::try_from(i).map(|i| (i, p)))
.collect();

let threshold = dkg_worker.get_signature_threshold(header).await;
let authority_public_key = dkg_worker.get_authority_public_key();

Expand Down Expand Up @@ -261,16 +263,19 @@ where
.ssids_attempted
.insert(next_ssid);

if let Some(signing_set) = self.maybe_create_signing_set(
dkg_worker,
&dkg_pub_key,
&unsigned_proposal_bytes,
next_ssid,
threshold,
session_id,
&best_authorities,
header,
) {
if let Some(signing_set) = self
.maybe_create_signing_set(
dkg_worker,
&dkg_pub_key,
&unsigned_proposal_bytes,
next_ssid,
threshold,
session_id,
&best_authorities,
header,
)
.await
{
let ssid = next_ssid;
let local_in_this_set = signing_set.signing_set.contains(&party_i);

Expand Down Expand Up @@ -305,7 +310,6 @@ where
break
}

// TODO: why do some nodes think SSID 14 is novel, while others don't?
// If this set is not novel, we must continue looping until we find a novel set
if !is_novel {
dkg_worker.logger.debug(format!("Attempted SSID {ssid}={signing_set:?}, however, this set is not novel. Continuing to loop"));
Expand All @@ -325,7 +329,7 @@ where
session_id,
ssid,
threshold,
best_authorities.len(),
signing_set.signing_set.len(),
signing_set,
));

Expand Down Expand Up @@ -438,12 +442,12 @@ where
/// Create a seed s where s is keccak256(pk, fN=at, unsignedProposal)
/// This seed is used in the random number generator to generate a
/// deterministic signing set of size t+1
fn maybe_create_signing_set(
async fn maybe_create_signing_set(
&self,
dkg_worker: &DKGWorker<B, BE, C, GE>,
dkg_pub_key: &[u8],
unsigned_proposal_bytes: &[u8],
ssid: u8,
ssid: SSID,
threshold: u16,
_session_id: SessionId,
best_authorities: &[(KeygenPartyId, Public)],
Expand All @@ -458,7 +462,8 @@ where
let seed = sp_core::keccak_256(&concat_data);
let locally_proposed_at: u64 = (*header.number()).saturated_into();

let maybe_set = self.generate_signers(&seed, threshold, best_authorities, dkg_worker).ok();
let maybe_set =
self.generate_signers(&seed, threshold, best_authorities, dkg_worker).await.ok();

maybe_set.map(|signing_set| ProposedSigningSet {
locally_proposed_at,
Expand All @@ -470,18 +475,18 @@ where
/// After keygen, this should be called to generate a random set of signers
/// NOTE: since the random set is called using a deterministic seed to and RNG,
/// the resulting set is deterministic
fn generate_signers(
async fn generate_signers(
&self,
seed: &[u8],
t: u16,
best_authorities: &[(KeygenPartyId, Public)],
dkg_worker: &DKGWorker<B, BE, C, GE>,
) -> Result<Vec<KeygenPartyId>, DKGError> {
let only_public_keys = best_authorities.iter().map(|(_, p)| p).cloned().collect::<Vec<_>>();
let mut final_set = dkg_worker.get_unjailed_signers(&only_public_keys)?;
let mut final_set = dkg_worker.get_unjailed_signers(&only_public_keys).await?;
// Mutate the final set if we don't have enough unjailed signers
if final_set.len() <= t as usize {
let jailed_set = dkg_worker.get_jailed_signers(&only_public_keys)?;
let jailed_set = dkg_worker.get_jailed_signers(&only_public_keys).await?;
let diff = t as usize + 1 - final_set.len();
final_set = final_set
.iter()
Expand All @@ -500,7 +505,7 @@ where

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProposedSigningSet {
pub ssid: u8,
pub ssid: SSID,
pub locally_proposed_at: u64,
pub signing_set: HashSet<KeygenPartyId>,
}
Expand All @@ -519,5 +524,5 @@ impl Hash for ProposedSigningSet {

pub enum SigningResult {
Success { unsigned_proposal_hash: [u8; 32] },
Failure { unsigned_proposal_hash: [u8; 32], ssid: u8 },
Failure { unsigned_proposal_hash: [u8; 32], ssid: SSID },
}
4 changes: 2 additions & 2 deletions dkg-gadget/src/signing_manager/work_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};
use dkg_primitives::{
crypto::Public,
types::{DKGError, SignedDKGMessage},
types::{DKGError, SignedDKGMessage, SSID},
};
use dkg_runtime_primitives::{associated_block_id_acceptable, SessionId};
use parking_lot::RwLock;
Expand Down Expand Up @@ -43,7 +43,7 @@ pub struct WorkManagerInner<B: BlockT> {
pub active_tasks: HashSet<Job<B>>,
pub enqueued_tasks: VecDeque<Job<B>>,
// task hash => SSID => enqueued messages
pub enqueued_messages: HashMap<[u8; 32], HashMap<u8, VecDeque<SignedDKGMessage<Public>>>>,
pub enqueued_messages: HashMap<[u8; 32], HashMap<SSID, VecDeque<SignedDKGMessage<Public>>>>,
}

#[derive(Debug)]
Expand Down
3 changes: 2 additions & 1 deletion dkg-gadget/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ impl<F> Future for ExplicitPanicFuture<F> {
}
}

pub fn bad_actors_to_authorities<T: Into<usize>>(
pub fn bad_actors_to_authorities<T: Into<usize> + Debug>(
bad_actors: Vec<T>,
mapping: &Arc<HashMap<usize, AuthorityId>>,
) -> Vec<AuthorityId> {
let mut ret = Vec::new();

for bad_actor in bad_actors {
if let Some(authority) = mapping.get(&bad_actor.into()) {
ret.push(authority.clone());
Expand Down
45 changes: 34 additions & 11 deletions dkg-gadget/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use sc_network_sync::SyncingService;
use sp_consensus::SyncOracle;

use crate::signing_manager::SigningManager;
use dkg_primitives::types::SSID;
use futures::StreamExt;
use multi_party_ecdsa::protocols::multi_party_ecdsa::gg_2020::state_machine::keygen::LocalKey;
use parking_lot::{Mutex, RwLock};
Expand Down Expand Up @@ -306,7 +307,7 @@ where
stage: ProtoStageType,
protocol_name: &str,
associated_block: NumberFor<B>,
ssid: u8,
ssid: SSID,
) -> Result<
AsyncProtocolParameters<
DKGProtocolEngine<
Expand Down Expand Up @@ -456,6 +457,24 @@ where
None
}

/// Get the keygen threshold at a specific block
pub async fn get_keygen_threshold(&self, header: &B::Header) -> u16 {
let at = header.hash();
self.exec_client_function(move |client| {
client.runtime_api().keygen_threshold(at).unwrap_or_default()
})
.await
}

/// Get the next keygen threshold at a specific block
pub async fn get_next_keygen_threshold(&self, header: &B::Header) -> u16 {
let at = header.hash();
self.exec_client_function(move |client| {
client.runtime_api().next_keygen_threshold(at).unwrap_or_default()
})
.await
}

/// Get the signature threshold at a specific block
pub async fn get_signature_threshold(&self, header: &B::Header) -> u16 {
let at = header.hash();
Expand Down Expand Up @@ -957,25 +976,29 @@ where
Ok(Public::from(signer))
}

fn get_jailed_signers_inner(
async fn get_jailed_signers_inner(
&self,
best_authorities: &[Public],
) -> Result<Vec<Public>, DKGError> {
let now = self.latest_header.read().clone().ok_or_else(|| DKGError::CriticalError {
reason: "latest header does not exist!".to_string(),
})?;
let best_authorities = best_authorities.to_vec();
let at = now.hash();
Ok(self
.client
.runtime_api()
.get_signing_jailed(at, best_authorities.to_vec())
.unwrap_or_default())
exec_client_function(&self.client, move |client| {
Ok(client
.runtime_api()
.get_signing_jailed(at, best_authorities)
.unwrap_or_default())
})
.await
}
pub(crate) fn get_unjailed_signers(

pub(crate) async fn get_unjailed_signers(
&self,
best_authorities: &[Public],
) -> Result<Vec<u16>, DKGError> {
let jailed_signers = self.get_jailed_signers_inner(best_authorities)?;
let jailed_signers = self.get_jailed_signers_inner(best_authorities).await?;
Ok(best_authorities
.iter()
.enumerate()
Expand All @@ -985,11 +1008,11 @@ where
}

/// Get the jailed signers
pub(crate) fn get_jailed_signers(
pub(crate) async fn get_jailed_signers(
&self,
best_authorities: &[Public],
) -> Result<Vec<u16>, DKGError> {
let jailed_signers = self.get_jailed_signers_inner(best_authorities)?;
let jailed_signers = self.get_jailed_signers_inner(best_authorities).await?;
Ok(best_authorities
.iter()
.enumerate()
Expand Down
Loading

0 comments on commit e590b43

Please sign in to comment.