Skip to content

Commit f2a8d3e

Browse files
committed
Fix tenure extend to build off the chain tip correclty
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 80c6a72 commit f2a8d3e

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

testnet/stacks-node/src/nakamoto_node/relayer.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ impl RelayerThread {
887887
let mining_pkh = Hash160::from_node_public_key(&StacksPublicKey::from_private(mining_key));
888888

889889
// If we won the last sortition, then we should start a new tenure off of it.
890-
let block_election_snapshot = {
890+
let last_block_election_snapshot = {
891891
let ih = self.sortdb.index_handle(&burn_tip.sortition_id);
892892
ih.get_last_snapshot_with_sortition(burn_tip.block_height)
893893
.map_err(|e| {
@@ -896,13 +896,13 @@ impl RelayerThread {
896896
})?
897897
};
898898

899-
let won_last_sortition = block_election_snapshot.miner_pk_hash == Some(mining_pkh);
899+
let won_last_sortition = last_block_election_snapshot.miner_pk_hash == Some(mining_pkh);
900900
debug!(
901901
"Relayer: Current burn block had no sortition. Checking for tenure continuation.";
902902
"won_last_sortition" => won_last_sortition,
903903
"current_mining_pkh" => %mining_pkh,
904-
"block_election_snapshot.consensus_hash" => %block_election_snapshot.consensus_hash,
905-
"block_election_snapshot.miner_pk_hash" => ?block_election_snapshot.miner_pk_hash,
904+
"last_block_election_snapshot.consensus_hash" => %last_block_election_snapshot.consensus_hash,
905+
"last_block_election_snapshot.miner_pk_hash" => ?last_block_election_snapshot.miner_pk_hash,
906906
"canonical_stacks_tip_id" => %canonical_stacks_tip,
907907
"canonical_stacks_tip_ch" => %canonical_stacks_tip_ch,
908908
"burn_view_ch" => %new_burn_view,
@@ -912,42 +912,40 @@ impl RelayerThread {
912912
return Ok(());
913913
}
914914

915-
let tip_info =
916-
NakamotoChainState::get_canonical_block_header(self.chainstate.db(), &self.sortdb)
917-
.map_err(|e| {
918-
error!("Relayer: failed to get canonical block header: {e:?}");
919-
NakamotoNodeError::SnapshotNotFoundForChainTip
920-
})?
921-
.ok_or_else(|| {
922-
error!("Relayer: failed to get canonical block header");
923-
NakamotoNodeError::SnapshotNotFoundForChainTip
924-
})?;
925-
926915
let last_non_empty_sortition_snapshot =
927-
SortitionDB::get_block_snapshot_consensus(self.sortdb.conn(), &tip_info.consensus_hash)
916+
SortitionDB::get_block_snapshot_consensus(self.sortdb.conn(), &canonical_stacks_tip_ch)
928917
.map_err(|e| {
929-
error!("Relayer: failed to get last non-empty sortition snapshot: {e:?}");
918+
error!("Relayer: failed to get block snapshot for canonical tip: {e:?}");
930919
NakamotoNodeError::SnapshotNotFoundForChainTip
931920
})?
932921
.ok_or_else(|| {
933-
error!("Relayer: failed to get last non-empty sortition snapshot");
922+
error!("Relayer: failed to get block snapshot for canonical tip");
934923
NakamotoNodeError::SnapshotNotFoundForChainTip
935924
})?;
936925

937926
let won_last_non_empty_sortition_snapshot =
938927
last_non_empty_sortition_snapshot.miner_pk_hash == Some(mining_pkh);
939928

940-
let reason = if !won_last_non_empty_sortition_snapshot {
941-
debug!("Relayer: Failed to issue a tenure change payload in our last tenure. Issue a tenure change payload again.");
942-
MinerReason::EmptyTenure
943-
} else {
944-
debug!("Relayer: Successfully issued a tenure change payload in our last tenure. Issue a continue extend.");
945-
MinerReason::Extended {
946-
burn_view_consensus_hash: new_burn_view,
947-
}
948-
};
929+
let (parent_tenure_start, block_election_snapshot, reason) =
930+
if !won_last_non_empty_sortition_snapshot {
931+
debug!("Relayer: Failed to issue a tenure change payload in our last tenure. Issue a new tenure change payload.");
932+
(
933+
canonical_stacks_tip, // TODO: what should this be? is this correct?
934+
last_block_election_snapshot,
935+
MinerReason::EmptyTenure,
936+
)
937+
} else {
938+
debug!("Relayer: Successfully issued a tenure change payload in its tenure. Issue a continue extend from the chain tip.");
939+
(
940+
canonical_stacks_tip, //For tenure extend, we sould be extending off the canonical tip
941+
last_non_empty_sortition_snapshot,
942+
MinerReason::Extended {
943+
burn_view_consensus_hash: new_burn_view,
944+
},
945+
)
946+
};
949947
match self.start_new_tenure(
950-
canonical_stacks_tip, // For tenure extend, we should be extending off the canonical tip
948+
parent_tenure_start,
951949
block_election_snapshot,
952950
burn_tip,
953951
reason,

0 commit comments

Comments
 (0)