@@ -1020,14 +1020,22 @@ impl SignerDb {
1020
1020
) )
1021
1021
}
1022
1022
1023
- /// Calculate the tenure extend timestamp
1023
+ /// Calculate the tenure extend timestamp. If determine the timestamp for a block rejection, check_tenure_extend should be set to false to avoid recalculating
1024
+ /// the tenure extend timestamp for a tenure extend block.
1024
1025
pub fn calculate_tenure_extend_timestamp (
1025
1026
& self ,
1026
1027
tenure_idle_timeout : Duration ,
1027
- tenure : & ConsensusHash ,
1028
+ block : & NakamotoBlock ,
1029
+ check_tenure_extend : bool ,
1028
1030
) -> u64 {
1031
+ if check_tenure_extend && block. get_tenure_tx_payload ( ) . is_some ( ) {
1032
+ let tenure_extend_timestamp =
1033
+ get_epoch_time_secs ( ) . wrapping_add ( tenure_idle_timeout. as_secs ( ) ) ;
1034
+ debug ! ( "Calculated tenure extend timestamp for a tenure extend block. Rolling over timestamp: {tenure_extend_timestamp}" ) ;
1035
+ return tenure_extend_timestamp;
1036
+ }
1029
1037
let tenure_idle_timeout_secs = tenure_idle_timeout. as_secs ( ) ;
1030
- let ( tenure_start_time, tenure_process_time_ms) = self . get_tenure_times ( tenure ) . inspect_err ( |e| error ! ( "Error occurred calculating tenure extend timestamp: {e:?}. Defaulting to {tenure_idle_timeout_secs} from now." ) ) . unwrap_or ( ( get_epoch_time_secs ( ) , 0 ) ) ;
1038
+ let ( tenure_start_time, tenure_process_time_ms) = self . get_tenure_times ( & block . header . consensus_hash ) . inspect_err ( |e| error ! ( "Error occurred calculating tenure extend timestamp: {e:?}. Defaulting to {tenure_idle_timeout_secs} from now." ) ) . unwrap_or ( ( get_epoch_time_secs ( ) , 0 ) ) ;
1031
1039
// Plus (ms + 999)/1000 to round up to the nearest second
1032
1040
let tenure_extend_timestamp = tenure_start_time
1033
1041
. saturating_add ( tenure_idle_timeout_secs)
@@ -1038,7 +1046,7 @@ impl SignerDb {
1038
1046
"tenure_process_time_ms" => tenure_process_time_ms,
1039
1047
"tenure_idle_timeout_secs" => tenure_idle_timeout_secs,
1040
1048
"tenure_extend_in" => tenure_extend_timestamp. saturating_sub( get_epoch_time_secs( ) ) ,
1041
- "consensus_hash" => %tenure ,
1049
+ "consensus_hash" => %block . header . consensus_hash ,
1042
1050
) ;
1043
1051
tenure_extend_timestamp
1044
1052
}
@@ -1705,17 +1713,16 @@ mod tests {
1705
1713
let mut db = SignerDb :: new ( db_path) . expect ( "Failed to create signer db" ) ;
1706
1714
1707
1715
let block_infos = generate_tenure_blocks ( ) ;
1708
- let consensus_hash_1 = block_infos[ 0 ] . block . header . consensus_hash ;
1709
- let consensus_hash_2 = block_infos. last ( ) . unwrap ( ) . block . header . consensus_hash ;
1710
- let consensus_hash_3 = ConsensusHash ( [ 0x03 ; 20 ] ) ;
1716
+ let mut unknown_block = block_infos[ 0 ] . block . clone ( ) ;
1717
+ unknown_block. header . consensus_hash = ConsensusHash ( [ 0x03 ; 20 ] ) ;
1711
1718
1712
1719
db. insert_block ( & block_infos[ 0 ] ) . unwrap ( ) ;
1713
1720
db. insert_block ( & block_infos[ 1 ] ) . unwrap ( ) ;
1714
1721
1715
1722
let tenure_idle_timeout = Duration :: from_secs ( 10 ) ;
1716
1723
// Verify tenure consensus_hash_1
1717
1724
let timestamp_hash_1_before =
1718
- db. calculate_tenure_extend_timestamp ( tenure_idle_timeout, & consensus_hash_1 ) ;
1725
+ db. calculate_tenure_extend_timestamp ( tenure_idle_timeout, & block_infos [ 0 ] . block , true ) ;
1719
1726
assert_eq ! (
1720
1727
timestamp_hash_1_before,
1721
1728
block_infos[ 0 ]
@@ -1728,7 +1735,8 @@ mod tests {
1728
1735
db. insert_block ( & block_infos[ 3 ] ) . unwrap ( ) ;
1729
1736
1730
1737
let timestamp_hash_1_after =
1731
- db. calculate_tenure_extend_timestamp ( tenure_idle_timeout, & consensus_hash_1) ;
1738
+ db. calculate_tenure_extend_timestamp ( tenure_idle_timeout, & block_infos[ 0 ] . block , true ) ;
1739
+
1732
1740
assert_eq ! (
1733
1741
timestamp_hash_1_after,
1734
1742
block_infos[ 2 ]
@@ -1741,8 +1749,11 @@ mod tests {
1741
1749
db. insert_block ( & block_infos[ 5 ] ) . unwrap ( ) ;
1742
1750
1743
1751
// Verify tenure consensus_hash_2
1744
- let timestamp_hash_2 =
1745
- db. calculate_tenure_extend_timestamp ( tenure_idle_timeout, & consensus_hash_2) ;
1752
+ let timestamp_hash_2 = db. calculate_tenure_extend_timestamp (
1753
+ tenure_idle_timeout,
1754
+ & block_infos. last ( ) . unwrap ( ) . block ,
1755
+ true ,
1756
+ ) ;
1746
1757
assert_eq ! (
1747
1758
timestamp_hash_2,
1748
1759
block_infos[ 4 ]
@@ -1751,9 +1762,15 @@ mod tests {
1751
1762
. saturating_add( 20 )
1752
1763
) ;
1753
1764
1765
+ let now = get_epoch_time_secs ( ) . saturating_add ( tenure_idle_timeout. as_secs ( ) ) ;
1766
+ let timestamp_hash_2_no_tenure_extend =
1767
+ db. calculate_tenure_extend_timestamp ( tenure_idle_timeout, & block_infos[ 0 ] . block , false ) ;
1768
+ assert_ne ! ( timestamp_hash_2, timestamp_hash_2_no_tenure_extend) ;
1769
+ assert ! ( now < timestamp_hash_2_no_tenure_extend) ;
1770
+
1754
1771
// Verify tenure consensus_hash_3 (unknown hash)
1755
1772
let timestamp_hash_3 =
1756
- db. calculate_tenure_extend_timestamp ( tenure_idle_timeout, & consensus_hash_3 ) ;
1773
+ db. calculate_tenure_extend_timestamp ( tenure_idle_timeout, & unknown_block , true ) ;
1757
1774
assert ! (
1758
1775
timestamp_hash_3. saturating_add( tenure_idle_timeout. as_secs( ) )
1759
1776
< block_infos[ 0 ] . proposed_time
0 commit comments