-
Notifications
You must be signed in to change notification settings - Fork 645
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
Feat/signers resend timed out messages #4737
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
use std::collections::VecDeque; | ||
use std::path::PathBuf; | ||
use std::sync::mpsc::Sender; | ||
use std::time::Instant; | ||
use std::time::{Duration, Instant}; | ||
|
||
use blockstack_lib::chainstate::burn::ConsensusHashExtensions; | ||
use blockstack_lib::chainstate::nakamoto::signer_set::NakamotoSigners; | ||
|
@@ -47,7 +47,7 @@ use wsts::state_machine::coordinator::fire::Coordinator as FireCoordinator; | |
use wsts::state_machine::coordinator::{ | ||
Config as CoordinatorConfig, Coordinator, State as CoordinatorState, | ||
}; | ||
use wsts::state_machine::signer::Signer as SignerStateMachine; | ||
use wsts::state_machine::signer::{Signer as SignerStateMachine, State as SignerState}; | ||
use wsts::state_machine::{OperationResult, SignError}; | ||
use wsts::traits::Signer as _; | ||
use wsts::v2; | ||
|
@@ -409,6 +409,39 @@ impl Signer { | |
self.coordinator_selector.last_message_time = Some(Instant::now()); | ||
} | ||
|
||
/// Resend outbound messages if no state change has occurred within the configured timeout | ||
pub fn resend_outbound_messages(&mut self, response_wait_timeout: Duration) { | ||
// We do not need to resend outbound messages if we are in the Idle state. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be the case then that |
||
if self.state == State::Idle { | ||
return; | ||
} | ||
match self.signer_db.outbound_messages_lookup(self.reward_cycle) { | ||
Ok(Some(info)) => { | ||
if info.insertion_time < chrono::Utc::now() - response_wait_timeout | ||
jcnelson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
&& info.coordinator_state | ||
== coordinator_state_to_string(&self.coordinator.state) | ||
&& info.signer_state == signer_state_to_string(&self.state_machine.state) | ||
{ | ||
info!("{self}: timed out waiting for responses. Resending {:?} outbound messages", info.outbound_messages.len(); | ||
"coordinator_state" => info.coordinator_state, | ||
"signer_state" => info.signer_state | ||
); | ||
self.send_outbound_messages(info.outbound_messages); | ||
if let Err(e) = self | ||
.signer_db | ||
.update_outbound_messages_time(self.reward_cycle) | ||
{ | ||
error!("{self}: Failed to update outbound messages in DB: {e:?}"); | ||
} | ||
} | ||
} | ||
Ok(None) => {} | ||
Err(e) => { | ||
error!("{self}: failed to read outbound messages from signer DB. {e:?}"); | ||
} | ||
} | ||
} | ||
|
||
/// Execute the given command and update state accordingly | ||
fn execute_command(&mut self, stacks_client: &StacksClient, command: &Command) { | ||
match command { | ||
|
@@ -738,8 +771,26 @@ impl Signer { | |
self.save_signer_state() | ||
.unwrap_or_else(|_| panic!("{self}: Failed to save signer state")); | ||
} | ||
self.send_outbound_messages(signer_outbound_messages); | ||
self.send_outbound_messages(coordinator_outbound_messages); | ||
let mut outbound_messages = Vec::with_capacity( | ||
signer_outbound_messages.len() + coordinator_outbound_messages.len(), | ||
); | ||
outbound_messages.extend(signer_outbound_messages); | ||
outbound_messages.extend(coordinator_outbound_messages); | ||
self.send_outbound_messages(outbound_messages.clone()); | ||
if !outbound_messages.is_empty() || self.state == State::Idle { | ||
debug!( | ||
"{self}: Saving outbound {:?} message(s) to SignerDB.", | ||
outbound_messages.len() | ||
); | ||
if let Err(e) = self.signer_db.insert_outbound_messages( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm understanding this correctly, couldn't this overwrite existing messages that we would've wanted to re-send after a timeout? For example:
If I'm understanding that correctly, then maybe a better implementation would be to insert these messages with the existing outbound messages, but only if the signer's state hasn't changed |
||
self.reward_cycle, | ||
&self.coordinator.state, | ||
&self.state_machine.state, | ||
&outbound_messages, | ||
) { | ||
error!("{self}: Failed to save outbound message info. {e:?}"); | ||
} | ||
} | ||
} | ||
|
||
/// Validate a signature share request, updating its message where appropriate. | ||
|
@@ -1374,7 +1425,7 @@ impl Signer { | |
return Ok(()); | ||
} | ||
// Check stackerdb for any missed DKG messages to catch up our state. | ||
self.read_dkg_stackerdb_messages(&stacks_client, res, current_reward_cycle)?; | ||
self.read_dkg_stackerdb_messages(stacks_client, res, current_reward_cycle)?; | ||
// Check if we should still queue DKG | ||
if !self.should_queue_dkg(stacks_client)? { | ||
return Ok(()); | ||
|
@@ -1605,6 +1656,37 @@ impl Signer { | |
} | ||
} | ||
|
||
/// Convert the signer state to a string | ||
pub fn signer_state_to_string(state: &SignerState) -> String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: can these methods here instead be part of a |
||
match state { | ||
SignerState::Idle => "Idle", | ||
SignerState::DkgPublicDistribute => "DkgPublicDistribute", | ||
SignerState::DkgPublicGather => "DkgPublicGather", | ||
SignerState::DkgPrivateDistribute => "DkgPrivateDistribute", | ||
SignerState::DkgPrivateGather => "DkgPrivateGather", | ||
SignerState::SignGather => "SignGather", | ||
} | ||
.to_string() | ||
} | ||
|
||
/// Convert the coordinator state to a string | ||
pub fn coordinator_state_to_string(state: &CoordinatorState) -> String { | ||
match state { | ||
CoordinatorState::Idle => "Idle", | ||
CoordinatorState::DkgPublicDistribute => "DkgPublicDistribute", | ||
CoordinatorState::DkgPublicGather => "DkgPublicGather", | ||
CoordinatorState::DkgPrivateDistribute => "DkgPrivateGather", | ||
CoordinatorState::DkgPrivateGather => "DkgPrivateGather", | ||
CoordinatorState::DkgEndDistribute => "DkgEndDistribute", | ||
CoordinatorState::DkgEndGather => "DkgEndGather", | ||
CoordinatorState::NonceRequest(_, _) => "NonceRequest", | ||
CoordinatorState::NonceGather(_, _) => "NonceGather", | ||
CoordinatorState::SigShareRequest(_, _) => "SigShareRequest", | ||
CoordinatorState::SigShareGather(_, _) => "SigShareGather", | ||
} | ||
.to_string() | ||
} | ||
|
||
fn load_encrypted_signer_state<S: SignerStateStorage>( | ||
storage: S, | ||
id: S::IdType, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: can you add a test here to ensure that the duration config field is set correctly when deserializing from a string?