@@ -159,7 +159,7 @@ impl Decrypter {
159
159
. dkg_stores ( dkg_stores. clone ( ) )
160
160
. current ( committee. id ( ) )
161
161
. net ( Overlay :: new ( net) )
162
- . dkg_state ( DkgState :: Genesis )
162
+ . dkg_state ( DkgState :: default ( ) )
163
163
. tx ( dec_tx)
164
164
. rx ( cmd_rx)
165
165
. enc_key ( cfg. threshold_enc_key . clone ( ) )
@@ -350,21 +350,23 @@ impl Drop for Decrypter {
350
350
351
351
#[ derive( Debug , Clone ) ]
352
352
enum DkgState {
353
- /// The node has not yet received sufficient DkgBundles to obtain the threshold
354
- /// decryption key material.
355
- Genesis ,
356
-
357
353
/// The node received inclusion lists with encrypted bundles but did not receive enough
358
354
/// DkgBundles to obtain the key. This suggests that the node is catching up.
359
355
///
360
356
/// In this state, the node will broadcast a DKG subset request and will obtain the key
361
357
/// once sufficient subsets have been received from remote nodes over the network.
362
- Recover ( HashMap < PublicKey , Subset > ) ,
358
+ Pending ( HashMap < PublicKey , Subset > ) ,
363
359
364
360
/// The node has completed DKG and can now combine/produce threshold decryption shares.
365
361
Completed ( DecryptionKey ) ,
366
362
}
367
363
364
+ impl Default for DkgState {
365
+ fn default ( ) -> Self {
366
+ Self :: Pending ( HashMap :: new ( ) )
367
+ }
368
+ }
369
+
368
370
/// Worker is responsible for "hatching" ciphertexts.
369
371
///
370
372
/// When ciphertexts in a round have received t+1 decryption shares
@@ -450,6 +452,12 @@ impl Worker {
450
452
pub async fn go ( mut self ) -> EndOfPlay {
451
453
let node = self . label ;
452
454
455
+ // always try to catchup first, if other nodes haven't finished, they simply won't respond
456
+ if let Err ( e) = self . dkg_catchup ( ) . await {
457
+ debug ! ( "err during dkg_catchup: {:?}" , e) ;
458
+ return EndOfPlay :: NetworkDown ;
459
+ }
460
+
453
461
loop {
454
462
let mut cache_modified = false ;
455
463
// process any pending inclusion lists received during recovery
@@ -557,7 +565,7 @@ impl Worker {
557
565
let conf = bincode:: config:: standard ( ) . with_limit :: < MAX_MESSAGE_SIZE > ( ) ;
558
566
match bincode:: serde:: decode_from_slice ( & bytes, conf) ?. 0 {
559
567
Protocol :: GetRequest ( cid) => self . on_get_request ( src, cid) . await ?,
560
- Protocol :: GetResponse ( subset ) => self . on_get_response ( src, subset ) . await ?,
568
+ Protocol :: GetResponse ( res ) => self . on_get_response ( src, res ) . await ?,
561
569
Protocol :: Batch ( batch) => {
562
570
self . on_batch_msg ( src, batch) . await ?;
563
571
return Ok ( true ) ;
@@ -612,12 +620,12 @@ impl Worker {
612
620
}
613
621
614
622
/// A get response for DKG subset has been received.
615
- async fn on_get_response ( & mut self , src : PublicKey , subset : SubsetResponse ) -> Result < ( ) > {
616
- let SubsetResponse { round, subset } = subset ;
623
+ async fn on_get_response ( & mut self , src : PublicKey , res : SubsetResponse ) -> Result < ( ) > {
624
+ let SubsetResponse { round, subset } = res ;
617
625
let ( round_num, committee_id) = round. into_parts ( ) ;
618
626
trace ! ( node = %self . label, from=%src, %committee_id, round=%round_num, "received get_response" ) ;
619
627
620
- let DkgState :: Recover ( ref mut subsets) = self . dkg_state else {
628
+ let DkgState :: Pending ( ref mut subsets) = self . dkg_state else {
621
629
trace ! ( "received get_response but not in a recovering state" ) ;
622
630
return Ok ( ( ) ) ;
623
631
} ;
@@ -823,11 +831,12 @@ impl Worker {
823
831
Ok ( ( ) )
824
832
}
825
833
826
- /// The node entered recover state and will catchup with the help of remote nodes.
827
- async fn recover ( & mut self , incl : InclusionList ) -> Result < ( ) > {
828
- let req = Protocol :: GetRequest ( Round :: new ( incl. round ( ) , self . current ) ) ;
834
+ /// The node will always try to catchup with the help of remote nodes first.
835
+ async fn dkg_catchup ( & mut self ) -> Result < ( ) > {
836
+ let round = self . first_requested_round . unwrap_or_default ( ) ;
837
+ let req = Protocol :: GetRequest ( Round :: new ( round, self . current ) ) ;
829
838
self . net
830
- . broadcast ( incl . round ( ) . u64 ( ) , serialize ( & req) ?)
839
+ . broadcast ( round. u64 ( ) , serialize ( & req) ?)
831
840
. await
832
841
. map_err ( |e| DecrypterError :: End ( e. into ( ) ) ) ?;
833
842
Ok ( ( ) )
@@ -877,14 +886,7 @@ impl Worker {
877
886
/// but will later be marked as decrypted during `hatch()`
878
887
async fn decrypt ( & mut self , incl : & InclusionList ) -> Result < DecShareBatch > {
879
888
let dec_sk = match & self . dkg_state {
880
- DkgState :: Genesis => {
881
- // received encrypted bundles but haven't received enough DkgBundles.
882
- self . recover ( incl. clone ( ) ) . await ?;
883
- self . pending . insert ( incl. round ( ) , incl. clone ( ) ) ;
884
- self . dkg_state = DkgState :: Recover ( HashMap :: default ( ) ) ;
885
- return Err ( DecrypterError :: DkgPending ) ;
886
- }
887
- DkgState :: Recover ( _) => {
889
+ DkgState :: Pending ( _) => {
888
890
// we already initiated catchup; awaiting response from remote nodes.
889
891
self . pending . insert ( incl. round ( ) , incl. clone ( ) ) ;
890
892
return Err ( DecrypterError :: DkgPending ) ;
0 commit comments