Skip to content

Commit 761ae4d

Browse files
committed
Rollover tenure extend timestamp for tenure change blocks that are accepted
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 7b265f7 commit 761ae4d

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

stacks-signer/src/chainstate.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ impl SortitionsView {
367367
tenure_extend.burn_view_consensus_hash != sortition_consensus_hash;
368368
let extend_timestamp = signer_db.calculate_tenure_extend_timestamp(
369369
self.config.tenure_idle_timeout,
370-
&sortition_consensus_hash,
370+
&block,
371+
false,
371372
);
372373
let epoch_time = get_epoch_time_secs();
373374
let enough_time_passed = epoch_time > extend_timestamp;

stacks-signer/src/signerdb.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,14 +1020,22 @@ impl SignerDb {
10201020
))
10211021
}
10221022

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.
10241025
pub fn calculate_tenure_extend_timestamp(
10251026
&self,
10261027
tenure_idle_timeout: Duration,
1027-
tenure: &ConsensusHash,
1028+
block: &NakamotoBlock,
1029+
check_tenure_extend: bool,
10281030
) -> 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+
}
10291037
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));
10311039
// Plus (ms + 999)/1000 to round up to the nearest second
10321040
let tenure_extend_timestamp = tenure_start_time
10331041
.saturating_add(tenure_idle_timeout_secs)
@@ -1038,7 +1046,7 @@ impl SignerDb {
10381046
"tenure_process_time_ms" => tenure_process_time_ms,
10391047
"tenure_idle_timeout_secs" => tenure_idle_timeout_secs,
10401048
"tenure_extend_in" => tenure_extend_timestamp.saturating_sub(get_epoch_time_secs()),
1041-
"consensus_hash" => %tenure,
1049+
"consensus_hash" => %block.header.consensus_hash,
10421050
);
10431051
tenure_extend_timestamp
10441052
}
@@ -1705,17 +1713,16 @@ mod tests {
17051713
let mut db = SignerDb::new(db_path).expect("Failed to create signer db");
17061714

17071715
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]);
17111718

17121719
db.insert_block(&block_infos[0]).unwrap();
17131720
db.insert_block(&block_infos[1]).unwrap();
17141721

17151722
let tenure_idle_timeout = Duration::from_secs(10);
17161723
// Verify tenure consensus_hash_1
17171724
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);
17191726
assert_eq!(
17201727
timestamp_hash_1_before,
17211728
block_infos[0]
@@ -1728,7 +1735,8 @@ mod tests {
17281735
db.insert_block(&block_infos[3]).unwrap();
17291736

17301737
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+
17321740
assert_eq!(
17331741
timestamp_hash_1_after,
17341742
block_infos[2]
@@ -1741,8 +1749,11 @@ mod tests {
17411749
db.insert_block(&block_infos[5]).unwrap();
17421750

17431751
// 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+
);
17461757
assert_eq!(
17471758
timestamp_hash_2,
17481759
block_infos[4]
@@ -1751,9 +1762,15 @@ mod tests {
17511762
.saturating_add(20)
17521763
);
17531764

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+
17541771
// Verify tenure consensus_hash_3 (unknown hash)
17551772
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);
17571774
assert!(
17581775
timestamp_hash_3.saturating_add(tenure_idle_timeout.as_secs())
17591776
< block_infos[0].proposed_time

stacks-signer/src/v0/signer.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,8 @@ impl Signer {
305305
signature,
306306
self.signer_db.calculate_tenure_extend_timestamp(
307307
self.proposal_config.tenure_idle_timeout,
308-
&block_info.block.header.consensus_hash,
308+
&block_info.block,
309+
true,
309310
),
310311
)
311312
} else {
@@ -317,7 +318,8 @@ impl Signer {
317318
self.mainnet,
318319
self.signer_db.calculate_tenure_extend_timestamp(
319320
self.proposal_config.tenure_idle_timeout,
320-
&block_info.block.header.consensus_hash,
321+
&block_info.block,
322+
false,
321323
),
322324
)
323325
};
@@ -422,7 +424,8 @@ impl Signer {
422424
self.mainnet,
423425
self.signer_db.calculate_tenure_extend_timestamp(
424426
self.proposal_config.tenure_idle_timeout,
425-
&block_proposal.block.header.consensus_hash,
427+
&block_proposal.block,
428+
false,
426429
),
427430
))
428431
}
@@ -440,7 +443,8 @@ impl Signer {
440443
self.mainnet,
441444
self.signer_db.calculate_tenure_extend_timestamp(
442445
self.proposal_config.tenure_idle_timeout,
443-
&block_proposal.block.header.consensus_hash,
446+
&block_proposal.block,
447+
false,
444448
),
445449
))
446450
}
@@ -460,7 +464,8 @@ impl Signer {
460464
self.mainnet,
461465
self.signer_db.calculate_tenure_extend_timestamp(
462466
self.proposal_config.tenure_idle_timeout,
463-
&block_proposal.block.header.consensus_hash,
467+
&block_proposal.block,
468+
false,
464469
),
465470
))
466471
};
@@ -602,7 +607,8 @@ impl Signer {
602607
signature,
603608
self.signer_db.calculate_tenure_extend_timestamp(
604609
self.proposal_config.tenure_idle_timeout,
605-
&block_info.block.header.consensus_hash,
610+
&block_info.block,
611+
true,
606612
),
607613
);
608614
// have to save the signature _after_ the block info
@@ -657,7 +663,8 @@ impl Signer {
657663
self.mainnet,
658664
self.signer_db.calculate_tenure_extend_timestamp(
659665
self.proposal_config.tenure_idle_timeout,
660-
&block_info.block.header.consensus_hash,
666+
&block_info.block,
667+
false,
661668
),
662669
);
663670
self.signer_db
@@ -755,7 +762,8 @@ impl Signer {
755762
self.mainnet,
756763
self.signer_db.calculate_tenure_extend_timestamp(
757764
self.proposal_config.tenure_idle_timeout,
758-
&block_proposal.block.header.consensus_hash,
765+
&block_proposal.block,
766+
false,
759767
),
760768
);
761769
if let Err(e) = block_info.mark_locally_rejected() {
@@ -1131,7 +1139,8 @@ impl Signer {
11311139
self.mainnet,
11321140
self.signer_db.calculate_tenure_extend_timestamp(
11331141
self.proposal_config.tenure_idle_timeout,
1134-
&block_proposal.block.header.consensus_hash,
1142+
&block_proposal.block,
1143+
false,
11351144
),
11361145
))
11371146
} else {

0 commit comments

Comments
 (0)