@@ -711,15 +711,13 @@ impl SignerDb {
711
711
try_deserialize ( result)
712
712
}
713
713
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" ;
720
718
let result: Option < String > = query_row ( & self . db , query, [ tenure] ) ?;
721
719
722
- try_deserialize ( result)
720
+ Ok ( result. is_some ( ) )
723
721
}
724
722
725
723
/// Return the first signed block in a tenure (identified by its consensus hash)
@@ -1734,4 +1732,28 @@ mod tests {
1734
1732
< block_infos[ 0 ] . proposed_time
1735
1733
) ;
1736
1734
}
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
+ }
1737
1759
}
0 commit comments