Skip to content
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

Do not attempt to treat miner as a coordinator when updating DKG #4591

Merged
merged 9 commits into from Apr 3, 2024
14 changes: 13 additions & 1 deletion stacks-signer/src/cli.rs
Expand Up @@ -271,7 +271,19 @@ pub fn parse_pox_addr(pox_address_literal: &str) -> Result<PoxAddress, String> {
let parsed_addr = PoxAddress::from_b58(pox_address_literal).map_or_else(
|| Err(format!("Invalid pox address: {pox_address_literal}")),
Ok,
)
);
match parsed_addr {
Ok(PoxAddress::Standard(addr, None)) => match addr.version {
C32_ADDRESS_VERSION_MAINNET_MULTISIG | C32_ADDRESS_VERSION_TESTNET_MULTISIG => Ok(
PoxAddress::Standard(addr, Some(AddressHashMode::SerializeP2SH)),
),
C32_ADDRESS_VERSION_MAINNET_SINGLESIG | C32_ADDRESS_VERSION_TESTNET_SINGLESIG => Ok(
PoxAddress::Standard(addr, Some(AddressHashMode::SerializeP2PKH)),
),
_ => Err(format!("Invalid address version: {}", addr.version)),
},
_ => parsed_addr,
}
}

/// Parse the hexadecimal Stacks private key
Expand Down
7 changes: 1 addition & 6 deletions stacks-signer/src/coordinator.rs
Expand Up @@ -92,12 +92,7 @@ impl CoordinatorSelector {
}
new_index
} else if ROTATE_COORDINATORS {
let mut new_index = self.coordinator_index.saturating_add(1);
if new_index == self.coordinator_ids.len() {
// We have exhausted all potential coordinators. Go back to the start
new_index = 0;
}
new_index
self.coordinator_index.saturating_add(1) % self.coordinator_ids.len()
} else {
self.coordinator_index
};
Expand Down
27 changes: 16 additions & 11 deletions stacks-signer/src/signer.rs
Expand Up @@ -439,7 +439,7 @@ impl Signer {
) {
match &self.state {
State::Idle => {
let Some(command) = self.commands.pop_front() else {
let Some(command) = self.commands.front() else {
debug!("{self}: Nothing to process. Waiting for command...");
return;
};
Expand All @@ -453,10 +453,12 @@ impl Signer {
debug!(
"{self}: Coordinator is {coordinator_id:?}. Will not process any commands...",
);
// Put the command back in the queue for later processing.
self.commands.push_front(command);
return;
}
let command = self
.commands
.pop_front()
.expect("BUG: Already asserted that the command queue was not empty");
self.execute_command(stacks_client, &command);
}
State::OperationInProgress => {
Expand Down Expand Up @@ -1268,14 +1270,17 @@ impl Signer {
}
return Ok(());
};
let coordinator_id = self.get_coordinator_dkg().0;
if self.signer_id == coordinator_id && self.state == State::Idle {
debug!("{self}: Checking if old vote transaction exists in StackerDB...");
// Have I already voted and have a pending transaction? Check stackerdb for the same round number and reward cycle vote transaction
// Only get the account nonce of THIS signer as we only care about our own votes, not other signer votes
let signer_address = stacks_client.get_signer_address();
let account_nonces = self.get_account_nonces(stacks_client, &[*signer_address]);
let old_transactions = self.get_signer_transactions(&account_nonces).map_err(|e| {
if self.state != State::Idle || self.signer_id != self.get_coordinator_dkg().0 {
// We are not the coordinator or we are in the middle of an operation. Do not attempt to queue DKG
return Ok(());
}
debug!("{self}: Checking if old DKG vote transaction exists in StackerDB...");
debug!("{self}: Checking if old DKG vote transaction exists in StackerDB...");
jferrant marked this conversation as resolved.
Show resolved Hide resolved
// Have I already voted, but the vote is still pending in StackerDB? Check stackerdb for the same round number and reward cycle vote transaction
// Only get the account nonce of THIS signer as we only care about our own votes, not other signer votes
let signer_address = stacks_client.get_signer_address();
let account_nonces = self.get_account_nonces(stacks_client, &[*signer_address]);
let old_transactions = self.get_signer_transactions(&account_nonces).map_err(|e| {
warn!("{self}: Failed to get old signer transactions: {e:?}. May trigger DKG unnecessarily");
}).unwrap_or_default();
// Check if we have an existing vote transaction for the same round and reward cycle
Expand Down
35 changes: 5 additions & 30 deletions stacks-signer/src/signerdb.rs
Expand Up @@ -118,15 +118,10 @@ impl SignerDb {
let result: Option<String> = query_row(
&self.db,
"SELECT block_info FROM blocks WHERE reward_cycle = ? AND signer_signature_hash = ?",
[&reward_cycle.to_string(), &format!("{}", hash)],
params![&u64_to_sql(reward_cycle)?, hash.to_string()],
)?;
if let Some(block_info) = result {
let block_info: BlockInfo =
serde_json::from_str(&block_info).map_err(DBError::SerializationError)?;
Ok(Some(block_info))
} else {
Ok(None)
}

try_deserialize(result)
}

/// Insert a block into the database.
Expand Down Expand Up @@ -154,28 +149,8 @@ impl SignerDb {
self.db
.execute(
"INSERT OR REPLACE INTO blocks (reward_cycle, signer_signature_hash, block_info) VALUES (?1, ?2, ?3)",
[reward_cycle.to_string(), format!("{}", hash), block_json],
)
.map_err(|e| {
DBError::Other(format!(
"Unable to insert block into db: {:?}",
e.to_string()
))
})?;
Ok(())
}

/// Remove a block
pub fn remove_block(
&mut self,
reward_cycle: u64,
hash: &Sha512Trunc256Sum,
) -> Result<(), DBError> {
debug!("Deleting block_info: sighash = {hash}");
self.db.execute(
"DELETE FROM blocks WHERE reward_cycle = ? AND signer_signature_hash = ?",
[reward_cycle.to_string(), format!("{}", hash)],
)?;
params![&u64_to_sql(reward_cycle)?, hash.to_string(), &block_json],
)?;

Ok(())
}
Expand Down