Skip to content

Commit 596cea6

Browse files
committed
use globbaly signed blocks instead of locally ones
1 parent 4f0d975 commit 596cea6

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

stacks-signer/src/chainstate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ impl SortitionsView {
444444

445445
// disallow reorg if more than one block has already been signed
446446
let signed_blocks =
447-
signer_db.get_signed_block_count_in_tenure(&tenure.consensus_hash)?;
447+
signer_db.get_globally_signed_block_count_in_tenure(&tenure.consensus_hash)?;
448448
if signed_blocks > 1 {
449449
warn!(
450450
"Miner is not building off of most recent tenure, but a tenure they attempted to reorg has already more than one signed block.";

stacks-signer/src/signerdb.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -826,10 +826,14 @@ impl SignerDb {
826826
try_deserialize(result)
827827
}
828828

829-
/// Return the count of signed blocks in a tenure (identified by its consensus hash)
830-
pub fn get_signed_block_count_in_tenure(&self, tenure: &ConsensusHash) -> Result<i64, DBError> {
831-
let query = "SELECT COUNT(*) FROM blocks WHERE consensus_hash = ? AND signed_over = 1";
832-
query_count(&self.db, query, [tenure])
829+
/// Return the count of globally signed blocks in a tenure (identified by its consensus hash)
830+
pub fn get_globally_signed_block_count_in_tenure(
831+
&self,
832+
tenure: &ConsensusHash,
833+
) -> Result<i64, DBError> {
834+
let query = "SELECT COUNT(*) FROM blocks WHERE consensus_hash = ?1 AND state = ?2 AND signed_over = 1";
835+
let args = params![tenure, &BlockState::GloballyAccepted.to_string()];
836+
query_count(&self.db, query, args)
833837
}
834838

835839
/// Return the last accepted block in a tenure (identified by its consensus hash).
@@ -1994,7 +1998,7 @@ mod tests {
19941998
}
19951999

19962000
#[test]
1997-
fn check_signed_block_count() {
2001+
fn check_globally_signed_block_count() {
19982002
let db_path = tmp_db_path();
19992003
let consensus_hash_1 = ConsensusHash([0x01; 20]);
20002004
let mut db = SignerDb::new(db_path).expect("Failed to create signer db");
@@ -2003,42 +2007,58 @@ mod tests {
20032007
});
20042008

20052009
assert_eq!(
2006-
db.get_signed_block_count_in_tenure(&consensus_hash_1)
2010+
db.get_globally_signed_block_count_in_tenure(&consensus_hash_1)
20072011
.unwrap(),
20082012
0
20092013
);
20102014

20112015
block_info.signed_over = true;
2016+
block_info.state = BlockState::GloballyAccepted;
20122017
block_info.block.header.chain_length = 1;
20132018
db.insert_block(&block_info).unwrap();
20142019

20152020
assert_eq!(
2016-
db.get_signed_block_count_in_tenure(&consensus_hash_1)
2021+
db.get_globally_signed_block_count_in_tenure(&consensus_hash_1)
20172022
.unwrap(),
20182023
1
20192024
);
20202025

20212026
block_info.signed_over = true;
2027+
block_info.state = BlockState::GloballyAccepted;
20222028
block_info.block.header.chain_length = 2;
20232029
db.insert_block(&block_info).unwrap();
20242030

20252031
block_info.signed_over = true;
2032+
block_info.state = BlockState::GloballyAccepted;
20262033
block_info.block.header.chain_length = 3;
20272034
db.insert_block(&block_info).unwrap();
20282035

20292036
assert_eq!(
2030-
db.get_signed_block_count_in_tenure(&consensus_hash_1)
2037+
db.get_globally_signed_block_count_in_tenure(&consensus_hash_1)
20312038
.unwrap(),
20322039
3
20332040
);
20342041

20352042
// add an unsigned block
20362043
block_info.signed_over = false;
2044+
block_info.state = BlockState::GloballyAccepted;
20372045
block_info.block.header.chain_length = 4;
20382046
db.insert_block(&block_info).unwrap();
20392047

20402048
assert_eq!(
2041-
db.get_signed_block_count_in_tenure(&consensus_hash_1)
2049+
db.get_globally_signed_block_count_in_tenure(&consensus_hash_1)
2050+
.unwrap(),
2051+
3
2052+
);
2053+
2054+
// add a locally signed block
2055+
block_info.signed_over = true;
2056+
block_info.state = BlockState::LocallyAccepted;
2057+
block_info.block.header.chain_length = 5;
2058+
db.insert_block(&block_info).unwrap();
2059+
2060+
assert_eq!(
2061+
db.get_globally_signed_block_count_in_tenure(&consensus_hash_1)
20422062
.unwrap(),
20432063
3
20442064
);

0 commit comments

Comments
 (0)