Skip to content

Commit a1fef60

Browse files
committed
CRC: cleanup valid_signer check and remove unnecessary index
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent a584982 commit a1fef60

File tree

2 files changed

+34
-43
lines changed

2 files changed

+34
-43
lines changed

stacks-signer/src/signerdb.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -685,11 +685,6 @@ CREATE TABLE IF NOT EXISTS block_pre_commits (
685685
PRIMARY KEY (signer_signature_hash, signer_addr)
686686
) STRICT;"#;
687687

688-
/// Used by get_block_pre_committers
689-
static CREATE_BLOCK_PRE_COMMITS_BY_SIGHASH_INDEX: &str = r#"
690-
CREATE INDEX idx_block_pre_commits_by_sighash ON block_pre_commits(signer_signature_hash);
691-
"#;
692-
693688
static SCHEMA_1: &[&str] = &[
694689
DROP_SCHEMA_0,
695690
CREATE_DB_CONFIG,
@@ -805,7 +800,6 @@ static SCHEMA_16: &[&str] = &[
805800

806801
static SCHEMA_17: &[&str] = &[
807802
CREATE_BLOCK_PRE_COMMITS_TABLE,
808-
CREATE_BLOCK_PRE_COMMITS_BY_SIGHASH_INDEX,
809803
"INSERT INTO db_config (version) VALUES (17);",
810804
];
811805

stacks-signer/src/v0/signer.rs

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,15 @@ impl Signer {
511511
);
512512
// try and gather signatures
513513
for (signer_public_key, message) in messages {
514+
let signer_address = StacksAddress::p2pkh(self.mainnet, signer_public_key);
515+
if !self.is_valid_signer(&signer_address) {
516+
debug!("{self}: Received a message from an unknown signer. Ignoring...";
517+
"signer_public_key" => ?signer_public_key,
518+
"signer_address" => %signer_address,
519+
"message" => ?message,
520+
);
521+
return;
522+
}
514523
match message {
515524
SignerMessage::BlockResponse(block_response) => self.handle_block_response(
516525
stacks_client,
@@ -519,15 +528,12 @@ impl Signer {
519528
),
520529
SignerMessage::StateMachineUpdate(update) => self
521530
.handle_state_machine_update(signer_public_key, update, received_time),
522-
SignerMessage::BlockPreCommit(signer_signature_hash) => {
523-
let stacker_address =
524-
StacksAddress::p2pkh(self.mainnet, signer_public_key);
525-
self.handle_block_pre_commit(
531+
SignerMessage::BlockPreCommit(signer_signature_hash) => self
532+
.handle_block_pre_commit(
526533
stacks_client,
527-
&stacker_address,
534+
&signer_address,
528535
signer_signature_hash,
529-
)
530-
}
536+
),
531537
_ => {}
532538
}
533539
}
@@ -1004,16 +1010,6 @@ impl Signer {
10041010
);
10051011
return;
10061012
};
1007-
// Make sure the sender is part of our signing set
1008-
let is_valid_sender = self.signer_addresses.iter().any(|addr| {
1009-
// it only matters that the address hash bytes match
1010-
stacker_address.bytes() == addr.bytes()
1011-
});
1012-
1013-
if !is_valid_sender {
1014-
debug!("{self}: Received pre-commit message from an unknown sender {stacker_address:?}. Will not store.");
1015-
return;
1016-
}
10171013

10181014
if self.signer_db.has_committed(block_hash, stacker_address).inspect_err(|e| warn!("Failed to check if pre-commit message already considered for {stacker_address:?} for {block_hash}: {e}")).unwrap_or(false) {
10191015
debug!("{self}: Already considered pre-commit message from {stacker_address:?} for {block_hash}. Ignoring...");
@@ -1643,16 +1639,12 @@ impl Signer {
16431639
return;
16441640
};
16451641

1646-
let signer_address = StacksAddress::p2pkh(self.mainnet, &public_key);
1647-
16481642
// authenticate the signature -- it must be signed by one of the stacking set
1649-
let is_valid_sig = self.signer_addresses.iter().any(|addr| {
1650-
// it only matters that the address hash bytes match
1651-
signer_address.bytes() == addr.bytes()
1652-
});
1653-
1654-
if !is_valid_sig {
1655-
debug!("{self}: Receive block rejection with an invalid signature. Will not store.";
1643+
let signer_address = StacksAddress::p2pkh(self.mainnet, &public_key);
1644+
if !self.is_valid_signer(&signer_address) {
1645+
debug!("{self}: Received block rejection with an invalid signature. Will not store.";
1646+
"signer_public_key" => ?public_key,
1647+
"signer_address" => %signer_address,
16561648
"signer_signature_hash" => %block_hash,
16571649
"signature" => %signature
16581650
);
@@ -1785,20 +1777,17 @@ impl Signer {
17851777
};
17861778

17871779
// authenticate the signature -- it must be signed by one of the stacking set
1788-
let is_valid_sig = self.signer_addresses.iter().any(|addr| {
1789-
let stacker_address = StacksAddress::p2pkh(self.mainnet, &public_key);
1790-
1791-
// it only matters that the address hash bytes match
1792-
stacker_address.bytes() == addr.bytes()
1793-
});
1794-
1795-
if !is_valid_sig {
1796-
debug!("{self}: Receive invalid signature {signature}. Will not store.");
1780+
let signer_address = StacksAddress::p2pkh(self.mainnet, &public_key);
1781+
if !self.is_valid_signer(&signer_address) {
1782+
debug!("{self}: Received block acceptance with an invalid signature. Will not store.";
1783+
"signer_public_key" => ?public_key,
1784+
"signer_address" => %signer_address,
1785+
"signer_signature_hash" => %block_hash,
1786+
"signature" => %signature
1787+
);
17971788
return;
17981789
}
17991790

1800-
let signer_address = StacksAddress::p2pkh(self.mainnet, &public_key);
1801-
18021791
// signature is valid! store it.
18031792
// if this returns false, it means the signature already exists in the DB, so just return.
18041793
if !self
@@ -2036,6 +2025,14 @@ impl Signer {
20362025
}
20372026
}
20382027

2028+
/// Check if the signer identified by the StacksAddress is part of the signer's list of signer addresses
2029+
pub fn is_valid_signer(&self, address: &StacksAddress) -> bool {
2030+
self.signer_addresses.iter().any(|addr| {
2031+
// it only matters that the address hash bytes match
2032+
address.bytes() == addr.bytes()
2033+
})
2034+
}
2035+
20392036
#[cfg(not(any(test, feature = "testing")))]
20402037
fn get_signer_protocol_version(&self) -> u64 {
20412038
crate::v0::signer_state::SUPPORTED_SIGNER_PROTOCOL_VERSION

0 commit comments

Comments
 (0)