Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
feat: ready base branch for pr
Browse files Browse the repository at this point in the history
  • Loading branch information
ASuciuX committed Dec 18, 2023
1 parent 8b203de commit 4785d8f
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 623 deletions.
12 changes: 0 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion stacks-signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ name = "stacks-signer"
path = "src/main.rs"

[dependencies]
backoff = "0.4"
bincode = "1.3.3"
clarity = { path = "../clarity" }
clap = { version = "4.1.1", features = ["derive", "env"] }
Expand Down
19 changes: 1 addition & 18 deletions stacks-signer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ pub struct Config {
pub endpoint: SocketAddr,
/// smart contract that controls the target stackerdb
pub stackerdb_contract_id: QualifiedContractIdentifier,
/// smart contract that controls the target stackerdb
pub pox_contract_id: Option<QualifiedContractIdentifier>,
/// The Scalar representation of the private key for signer communication
pub message_private_key: Scalar,
/// The signer's Stacks private key
Expand Down Expand Up @@ -139,11 +137,8 @@ struct RawConfigFile {
pub node_host: String,
/// endpoint to stackerdb receiver
pub endpoint: String,
// FIXME: these contract's should go away in non testing scenarios. Make them both optionals.
/// Stacker db contract identifier
/// contract identifier
pub stackerdb_contract_id: String,
/// pox contract identifier
pub pox_contract_id: Option<String>,
/// the 32 byte ECDSA private key used to sign blocks, chunks, and transactions
pub message_private_key: String,
/// The hex representation of the signer's Stacks private key used for communicating
Expand Down Expand Up @@ -223,17 +218,6 @@ impl TryFrom<RawConfigFile> for Config {
)
})?;

let pox_contract_id = if let Some(id) = raw_data.pox_contract_id.as_ref() {
Some(QualifiedContractIdentifier::parse(id).map_err(|_| {
ConfigError::BadField(
"pox_contract_id".to_string(),
raw_data.pox_contract_id.unwrap_or("".to_string()),
)
})?)
} else {
None
};

let message_private_key =
Scalar::try_from(raw_data.message_private_key.as_str()).map_err(|_| {
ConfigError::BadField(
Expand Down Expand Up @@ -285,7 +269,6 @@ impl TryFrom<RawConfigFile> for Config {
node_host,
endpoint,
stackerdb_contract_id,
pox_contract_id,
message_private_key,
stacks_private_key,
stacks_address,
Expand Down
1 change: 0 additions & 1 deletion stacks-signer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ fn handle_generate_files(args: GenerateFilesArgs) {
args.num_keys,
&args.db_args.host.to_string(),
&args.db_args.contract.to_string(),
None,
args.timeout.map(Duration::from_millis),
);
debug!("Built {:?} signer config tomls.", signer_config_tomls.len());
Expand Down
90 changes: 25 additions & 65 deletions stacks-signer/src/runloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use wsts::state_machine::{OperationResult, PublicKeys};
use wsts::v2;

use crate::config::Config;
use crate::stacks_client::{retry_with_exponential_backoff, ClientError, StacksClient};
use crate::stacks_client::StacksClient;

/// Which operation to perform
#[derive(PartialEq, Clone)]
Expand All @@ -36,9 +36,6 @@ pub enum RunLoopCommand {
/// The RunLoop state
#[derive(PartialEq, Debug)]
pub enum State {
// TODO: Uninitialized should indicate we need to replay events/configure the signer
/// The runloop signer is uninitialized
Uninitialized,
/// The runloop is idle
Idle,
/// The runloop is executing a DKG round
Expand All @@ -51,7 +48,7 @@ pub enum State {
pub struct RunLoop<C> {
/// The timeout for events
pub event_timeout: Duration,
/// The coordinator for inbound messages
/// the coordinator for inbound messages
pub coordinator: C,
/// The signing round used to sign messages
// TODO: update this to use frost_signer directly instead of the frost signing round
Expand All @@ -66,27 +63,7 @@ pub struct RunLoop<C> {
}

impl<C: Coordinatable> RunLoop<C> {
/// Initialize the signer, reading the stacker-db state and setting the aggregate public key
fn initialize(&mut self) -> Result<(), ClientError> {
// TODO: update to read stacker db to get state.
// Check if the aggregate key is set in the pox contract
if let Some(key) = self.stacks_client.get_aggregate_public_key()? {
debug!("Aggregate public key is set: {:?}", key);
self.coordinator.set_aggregate_public_key(Some(key));
} else {
// Update the state to IDLE so we don't needlessy requeue the DKG command.
let (coordinator_id, _) = calculate_coordinator(&self.signing_round.public_keys);
if coordinator_id == self.signing_round.signer_id
&& self.commands.front() != Some(&RunLoopCommand::Dkg)
{
self.commands.push_front(RunLoopCommand::Dkg);
}
}
self.state = State::Idle;
Ok(())
}

/// Execute the given command and update state accordingly
/// Helper function to actually execute the command and update state accordingly
/// Returns true when it is successfully executed, else false
fn execute_command(&mut self, command: &RunLoopCommand) -> bool {
match command {
Expand All @@ -96,7 +73,7 @@ impl<C: Coordinatable> RunLoop<C> {
Ok(msg) => {
let ack = self
.stacks_client
.send_message_with_retry(self.signing_round.signer_id, msg);
.send_message(self.signing_round.signer_id, msg);
debug!("ACK: {:?}", ack);
self.state = State::Dkg;
true
Expand All @@ -122,7 +99,7 @@ impl<C: Coordinatable> RunLoop<C> {
Ok(msg) => {
let ack = self
.stacks_client
.send_message_with_retry(self.signing_round.signer_id, msg);
.send_message(self.signing_round.signer_id, msg);
debug!("ACK: {:?}", ack);
self.state = State::Sign;
true
Expand All @@ -138,14 +115,9 @@ impl<C: Coordinatable> RunLoop<C> {
}
}

/// Attempt to process the next command in the queue, and update state accordingly
/// Helper function to check the current state, process the next command in the queue, and update state accordingly
fn process_next_command(&mut self) {
match self.state {
State::Uninitialized => {
debug!(
"Signer is uninitialized. Waiting for aggregate public key from stacks node..."
);
}
State::Idle => {
if let Some(command) = self.commands.pop_front() {
while !self.execute_command(&command) {
Expand Down Expand Up @@ -233,29 +205,26 @@ impl From<&Config> for RunLoop<FrostCoordinator<v2::Aggregator>> {
.iter()
.map(|i| i - 1) // SigningRound::new (unlike SigningRound::from) doesn't do this
.collect::<Vec<u32>>();
let coordinator = FrostCoordinator::new(
total_signers,
total_keys,
threshold,
config.message_private_key,
);
let signing_round = SigningRound::new(
threshold,
total_signers,
total_keys,
config.signer_id,
key_ids,
config.message_private_key,
config.signer_ids_public_keys.clone(),
);
let stacks_client = StacksClient::from(config);
RunLoop {
event_timeout: config.event_timeout,
coordinator,
signing_round,
stacks_client,
coordinator: FrostCoordinator::new(
total_signers,
total_keys,
threshold,
config.message_private_key,
),
signing_round: SigningRound::new(
threshold,
total_signers,
total_keys,
config.signer_id,
key_ids,
config.message_private_key,
config.signer_ids_public_keys.clone(),
),
stacks_client: StacksClient::from(config),
commands: VecDeque::new(),
state: State::Uninitialized,
state: State::Idle,
}
}
}
Expand All @@ -275,19 +244,10 @@ impl<C: Coordinatable> SignerRunLoop<Vec<OperationResult>, RunLoopCommand> for R
cmd: Option<RunLoopCommand>,
res: Sender<Vec<OperationResult>>,
) -> Option<Vec<OperationResult>> {
info!(
"Running one pass for signer ID# {}. Current state: {:?}",
self.signing_round.signer_id, self.state
);
if let Some(command) = cmd {
self.commands.push_back(command);
}
if self.state == State::Uninitialized {
let request_fn = || self.initialize().map_err(backoff::Error::transient);
retry_with_exponential_backoff(request_fn)
.expect("Failed to connect to initialize due to timeout. Stacks node may be down.");
}
// Process any arrived events
// First process any arrived events
if let Some(event) = event {
let (outbound_messages, operation_results) = self.process_event(&event);
debug!(
Expand All @@ -297,7 +257,7 @@ impl<C: Coordinatable> SignerRunLoop<Vec<OperationResult>, RunLoopCommand> for R
for msg in outbound_messages {
let ack = self
.stacks_client
.send_message_with_retry(self.signing_round.signer_id, msg);
.send_message(self.signing_round.signer_id, msg);
if let Ok(ack) = ack {
debug!("ACK: {:?}", ack);
} else {
Expand Down
Loading

0 comments on commit 4785d8f

Please sign in to comment.