Skip to content

Commit bb48447

Browse files
committed
Change is_timed_out check to use any block proposal as sign of miner activity
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 95139fb commit bb48447

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

stacks-signer/src/chainstate.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ impl SortitionState {
8989
if self.miner_status != SortitionMinerStatus::Valid {
9090
return Ok(false);
9191
}
92-
// if we've already signed a block in this tenure, the miner can't have timed out.
93-
let has_blocks = signer_db
94-
.get_last_signed_block_in_tenure(&self.consensus_hash)?
95-
.is_some();
92+
// if we've already seen a proposed block from this miner. It cannot have timed out.
93+
let has_blocks = signer_db.has_proposed_block_in_tenure(&self.consensus_hash)?;
9694
if has_blocks {
9795
return Ok(false);
9896
}

stacks-signer/src/signerdb.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -711,15 +711,13 @@ impl SignerDb {
711711
try_deserialize(result)
712712
}
713713

714-
/// Return the last signed block in a tenure (identified by its consensus hash)
715-
pub fn get_last_signed_block_in_tenure(
716-
&self,
717-
tenure: &ConsensusHash,
718-
) -> Result<Option<BlockInfo>, DBError> {
719-
let query = "SELECT block_info FROM blocks WHERE consensus_hash = ? AND signed_over = 1 ORDER BY stacks_height DESC LIMIT 1";
714+
/// Return whether a block proposal has been stored for a tenure (identified by its consensus hash)
715+
/// Does not consider the block's state.
716+
pub fn has_proposed_block_in_tenure(&self, tenure: &ConsensusHash) -> Result<bool, DBError> {
717+
let query = "SELECT block_info FROM blocks WHERE consensus_hash = ? LIMIT 1";
720718
let result: Option<String> = query_row(&self.db, query, [tenure])?;
721719

722-
try_deserialize(result)
720+
Ok(result.is_some())
723721
}
724722

725723
/// Return the first signed block in a tenure (identified by its consensus hash)
@@ -1734,4 +1732,28 @@ mod tests {
17341732
< block_infos[0].proposed_time
17351733
);
17361734
}
1735+
1736+
#[test]
1737+
fn has_proposed_block() {
1738+
let db_path = tmp_db_path();
1739+
let consensus_hash_1 = ConsensusHash([0x01; 20]);
1740+
let consensus_hash_2 = ConsensusHash([0x02; 20]);
1741+
let mut db = SignerDb::new(db_path).expect("Failed to create signer db");
1742+
let (mut block_info, _) = create_block_override(|b| {
1743+
b.block.header.consensus_hash = consensus_hash_1.clone();
1744+
b.block.header.chain_length = 1;
1745+
});
1746+
1747+
assert!(!db.has_proposed_block_in_tenure(&consensus_hash_1).unwrap());
1748+
assert!(!db.has_proposed_block_in_tenure(&consensus_hash_2).unwrap());
1749+
1750+
db.insert_block(&block_info).unwrap();
1751+
1752+
block_info.block.header.chain_length = 2;
1753+
1754+
db.insert_block(&block_info).unwrap();
1755+
1756+
assert!(db.has_proposed_block_in_tenure(&consensus_hash_1).unwrap());
1757+
assert!(!db.has_proposed_block_in_tenure(&consensus_hash_2).unwrap());
1758+
}
17371759
}

0 commit comments

Comments
 (0)