Skip to content

Commit 8c61a64

Browse files
committed
Use consistent logging across the Decrypter.
1 parent 4895fd6 commit 8c61a64

File tree

2 files changed

+38
-50
lines changed

2 files changed

+38
-50
lines changed

timeboost-sequencer/src/decrypt.rs

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ impl Decrypter {
259259
let (ct, cm) = vess
260260
.encrypt_shares(store.committee(), store.sorted_keys(), secret, DKG_AAD)
261261
.ok()?;
262+
debug!(node = %self.label, curr = %self.current, "produced dkg bundle");
262263
Some(DkgBundle::new((node_idx, self.label), self.current, ct, cm))
263264
}
264265

@@ -293,7 +294,7 @@ impl Decrypter {
293294
DKG_AAD,
294295
)
295296
.ok()?;
296-
info!(node = %self.label, curr = %self.current, next = %committee_id, "produced dkg bundle");
297+
debug!(node = %self.label, curr = %self.current, next = %committee_id, "produced resharing bundle");
297298
Some(DkgBundle::new((node_idx, self.label), committee_id, ct, cm))
298299
}
299300

@@ -467,8 +468,7 @@ struct Worker {
467468
#[builder(default)]
468469
dec_shares: DecShareCache,
469470

470-
/// Map of received decryption shares for each round.
471-
/// Useful to prevent DOS or DecShareBatch flooding by malicious peers.
471+
/// Map of received decryption shares for each round (DOS prevention).
472472
#[builder(default)]
473473
acks: BTreeMap<RoundNumber, HashSet<PublicKey>>,
474474

@@ -609,7 +609,7 @@ impl Worker {
609609
/// A message from another node has been received.
610610
/// Returns true if decryption shares have been updated.
611611
async fn on_inbound(&mut self, src: PublicKey, bytes: Bytes) -> Result<bool> {
612-
// ignore msg sent to self during broadcast
612+
// ignore msg sent to self
613613
if src == self.label {
614614
return Ok(false);
615615
}
@@ -710,21 +710,23 @@ impl Worker {
710710

711711
if let Some((&subset, _)) = counts.iter().find(|(_, count)| **count >= threshold) {
712712
let acc = DkgAccumulator::from_subset(current.clone(), subset.to_owned());
713+
let mode = acc.mode().clone();
713714
self.tracker.insert(committee.id(), acc);
714715
let dec_key = subset
715716
.extract_key(current, &self.dkg_sk, prev.as_ref())
716717
.map_err(|e| DecrypterError::Dkg(e.to_string()))?;
717718

718719
self.dec_key.set(dec_key);
719720
self.state = WorkerState::Running;
720-
info!(node = %self.label, committee_id = %committee.id(), "dkg finished");
721+
info!(node = %self.label, committee_id = %committee.id(), "{} finished", mode);
721722
}
722723

723724
Ok(())
724725
}
725726

726727
/// A batch of decryption shares from another node has been received.
727728
async fn on_batch_msg(&mut self, src: PublicKey, batch: DecShareBatch) -> Result<()> {
729+
trace!(node = %self.label, from=%src, round=%batch.round, "received batch message");
728730
let round = batch.round.num();
729731
let committee_id = batch.round.committee();
730732
if committee_id != self.current {
@@ -748,7 +750,6 @@ impl Worker {
748750
if round <= self.last_hatched_round {
749751
return Ok(());
750752
}
751-
trace!(node = %self.label, from=%src, %round, "inserting decrypted shares");
752753

753754
self.insert_shares(batch)?;
754755

@@ -772,39 +773,38 @@ impl Worker {
772773
return Ok(());
773774
}
774775

775-
if is_resharing && self.dec_key.get().is_none() {
776-
warn!(node = %self.label, "initial DKG incomplete");
777-
return Ok(());
778-
}
776+
let mode = if is_resharing {
777+
if let Some(dec_key) = self.dec_key.get() {
778+
AccumulatorMode::Resharing(dec_key.combkey().to_owned())
779+
} else {
780+
return Err(DecrypterError::Dkg("initial DKG incomplete".into()));
781+
}
782+
} else {
783+
AccumulatorMode::Dkg
784+
};
779785

780786
let Some(key_store) = self.key_stores.read().get(*committee_id).cloned() else {
781787
return Err(DecrypterError::NoCommittee(*committee_id));
782788
};
783789

784-
let acc = if is_resharing {
785-
let dec_key = self.dec_key.get().expect("dec_key exists for resharing");
786-
self.tracker.entry(*committee_id).or_insert_with(|| {
787-
DkgAccumulator::new_resharing(key_store.clone(), dec_key.combkey().clone())
788-
})
789-
} else {
790-
self.tracker
791-
.entry(*committee_id)
792-
.or_insert_with(|| DkgAccumulator::new_dkg(key_store.clone()))
793-
};
790+
let acc = self
791+
.tracker
792+
.entry(*committee_id)
793+
.or_insert_with(|| DkgAccumulator::new(key_store.clone(), mode.clone()));
794794

795795
acc.try_add(bundle)
796796
.await
797797
.map_err(|e| DecrypterError::Dkg(format!("failed to add bundle: {e}")))?;
798798

799799
// for initial DKG, try to finalize if we have enough bundles
800-
if !is_resharing {
800+
if matches!(mode, AccumulatorMode::Dkg) {
801801
if let Some(subset) = acc.try_finalize() {
802802
let dec_key = subset
803803
.extract_key(&key_store.clone(), &self.dkg_sk, None)
804804
.map_err(|e| DecrypterError::Dkg(e.to_string()))?;
805805
self.dec_key.set(dec_key);
806806
self.state = WorkerState::Running;
807-
info!(committee_id = %key_store.committee().id(), node = %self.label, "DKG completed");
807+
info!(committee_id = %key_store.committee().id(), node = %self.label, "initial DKG completed");
808808
}
809809
}
810810

@@ -1106,7 +1106,6 @@ impl Worker {
11061106
return CombineResult::InsufficientShares;
11071107
}
11081108

1109-
// skip if no ciphertext
11101109
let Some(ct) = maybe_ct else {
11111110
return CombineResult::InsufficientShares;
11121111
};
@@ -1179,18 +1178,7 @@ impl Worker {
11791178
/// Adds a new committee to the worker and updates network connections.
11801179
async fn on_next_committee(&mut self, c: AddressableCommittee, k: KeyStore) -> Result<()> {
11811180
info!(node = %self.label, committee = %c.committee().id(), "add next committee");
1182-
let key_store = {
1183-
let key_stores = self.key_stores.read();
1184-
if key_stores.contains(c.committee().id()) {
1185-
warn!(node = %self.label, committee = %c.committee().id(), "committee already added");
1186-
return Ok(());
1187-
}
1188-
let Some(key_store) = key_stores.get(self.current) else {
1189-
error!(node = %self.label, committee = %self.current, "current committee not found");
1190-
return Err(DecrypterError::NoCommittee(self.current));
1191-
};
1192-
key_store.clone()
1193-
};
1181+
let key_store = self.current_store()?;
11941182
let mut additional = Vec::new();
11951183
for (k, x, a) in c
11961184
.entries()
@@ -1253,7 +1241,7 @@ impl Worker {
12531241

12541242
self.next_committee =
12551243
NextCommittee::Use(round, Some(NextKey::new(new_dkg_sk, new_dec_key)));
1256-
debug!(node = %self.label, %round, "completed key extraction");
1244+
trace!(node = %self.label, %round, "completed key extraction");
12571245
} else {
12581246
// not in new committee - request DKG subset from current committee
12591247
let dest: Vec<_> = old.committee().parties().copied().collect();
@@ -1264,7 +1252,7 @@ impl Worker {
12641252
.map_err(|e| DecrypterError::End(e.into()))?;
12651253

12661254
self.next_committee = NextCommittee::Use(round, None);
1267-
debug!(node = %self.label, %round, "requested DKG subset from current");
1255+
trace!(node = %self.label, %round, "requested DKG subset from current");
12681256
}
12691257
Ok(())
12701258
}

timeboost-types/src/decryption.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use parking_lot::RwLock;
66
use rayon::prelude::*;
77
use sailfish_types::{Evidence, RoundNumber};
88
use serde::{Deserialize, Serialize};
9+
use std::fmt;
910
use std::ops::Deref;
1011
use std::{
1112
collections::{BTreeMap, btree_map},
@@ -350,6 +351,15 @@ pub enum AccumulatorMode {
350351
Resharing(CombKey),
351352
}
352353

354+
impl fmt::Display for AccumulatorMode {
355+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
356+
match self {
357+
Self::Dkg => f.write_str("initial DKG"),
358+
Self::Resharing(_) => f.write_str("resharing"),
359+
}
360+
}
361+
}
362+
353363
/// Accumulates DKG bundles for a given committee and finalizes when enough have been collected.
354364
///
355365
/// DkgAccumulator tracks received bundles and determines when the threshold for finalizing
@@ -364,22 +374,12 @@ pub struct DkgAccumulator {
364374
}
365375

366376
impl DkgAccumulator {
367-
/// Create a new accumulator for DKG operations.
368-
pub fn new_dkg(store: KeyStore) -> Self {
369-
Self {
370-
store,
371-
bundles: Vec::new(),
372-
mode: AccumulatorMode::Dkg,
373-
complete: false,
374-
}
375-
}
376-
377-
/// Create a new accumulator for resharing operations.
378-
pub fn new_resharing(store: KeyStore, combkey: CombKey) -> Self {
377+
/// Create a new accumulator with the specified mode.
378+
pub fn new(store: KeyStore, mode: AccumulatorMode) -> Self {
379379
Self {
380380
store,
381381
bundles: Vec::new(),
382-
mode: AccumulatorMode::Resharing(combkey),
382+
mode,
383383
complete: false,
384384
}
385385
}

0 commit comments

Comments
 (0)