Skip to content

Commit fa3eac9

Browse files
committed
improve get_globally_accepted_block_count_in_tenure to trigger Error only if the consensus_hash does not exist
1 parent 2dac2c2 commit fa3eac9

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

stacks-signer/src/signerdb.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -858,15 +858,15 @@ impl SignerDb {
858858
&self,
859859
tenure: &ConsensusHash,
860860
) -> Result<u64, DBError> {
861-
let query = "SELECT COALESCE((MAX(stacks_height) - MIN(stacks_height) + 1), 0) AS block_count FROM blocks WHERE consensus_hash = ?1 AND state = ?2";
861+
let query = "SELECT COALESCE((MAX(stacks_height) - MIN(stacks_height) + 1), 0), (SELECT COUNT(consensus_hash) FROM blocks WHERE consensus_hash = ?1) FROM blocks WHERE consensus_hash = ?1 AND state = ?2";
862862
let args = params![tenure, &BlockState::GloballyAccepted.to_string()];
863-
let block_count_opt: Option<u64> = query_row(&self.db, query, args)?;
863+
let block_count_opt: Option<(u64, u64)> = query_row(&self.db, query, args)?;
864864
match block_count_opt {
865-
Some(block_count) => {
866-
if block_count > 0 {
867-
Ok(block_count)
868-
} else {
865+
Some((block_count, tenure_count)) => {
866+
if tenure_count == 0 {
869867
Err(DBError::NotFoundError)
868+
} else {
869+
Ok(block_count)
870870
}
871871
}
872872
None => Err(DBError::NotFoundError),
@@ -2080,15 +2080,16 @@ mod tests {
20802080
DBError::NotFoundError
20812081
));
20822082

2083+
// locally accepted will return 0 (instead of DBError::NotFoundError) as the tenure is valid
20832084
block_info.signed_over = true;
2084-
block_info.state = BlockState::GloballyAccepted;
2085+
block_info.state = BlockState::LocallyAccepted;
20852086
block_info.block.header.chain_length = 1;
20862087
db.insert_block(&block_info).unwrap();
20872088

20882089
assert_eq!(
20892090
db.get_globally_accepted_block_count_in_tenure(&consensus_hash_1)
20902091
.unwrap(),
2091-
1
2092+
0
20922093
);
20932094

20942095
block_info.signed_over = true;
@@ -2104,7 +2105,7 @@ mod tests {
21042105
assert_eq!(
21052106
db.get_globally_accepted_block_count_in_tenure(&consensus_hash_1)
21062107
.unwrap(),
2107-
3
2108+
2
21082109
);
21092110

21102111
// add an unsigned block
@@ -2116,7 +2117,7 @@ mod tests {
21162117
assert_eq!(
21172118
db.get_globally_accepted_block_count_in_tenure(&consensus_hash_1)
21182119
.unwrap(),
2119-
4
2120+
3
21202121
);
21212122

21222123
// add a locally signed block
@@ -2128,7 +2129,7 @@ mod tests {
21282129
assert_eq!(
21292130
db.get_globally_accepted_block_count_in_tenure(&consensus_hash_1)
21302131
.unwrap(),
2131-
4
2132+
3
21322133
);
21332134
}
21342135

0 commit comments

Comments
 (0)