Skip to content

Commit

Permalink
CRC: use the last snapshot with a sortition to determine if we should…
Browse files Browse the repository at this point in the history
… proceed with a continue_tenure

Signed-off-by: Jacinta Ferrant <jacinta@trustmachines.co>
  • Loading branch information
jferrant committed Jun 17, 2024
1 parent 24b5e79 commit 8281f17
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
14 changes: 12 additions & 2 deletions testnet/stacks-node/src/nakamoto_node/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,20 @@ impl RelayerThread {
return Ok(());
};
let mining_pkh = Hash160::from_node_public_key(&StacksPublicKey::from_private(mining_key));
if block_election_snapshot.miner_pk_hash != Some(mining_pkh) {

let last_winner_snapshot = {
let ih = self.sortdb.index_handle(&burn_tip.sortition_id);
ih.get_last_snapshot_with_sortition(burn_tip.block_height)
.map_err(|e| {
error!("Relayer: failed to get last snapshot with sortition: {e:?}");
NakamotoNodeError::SnapshotNotFoundForChainTip
})?
};

if last_winner_snapshot.miner_pk_hash != Some(mining_pkh) {
debug!("Relayer: the miner did not win the last sortition. No tenure to continue.";
"current_mining_pkh" => %mining_pkh,
"block_snapshot.miner_pk_hash" => ?block_election_snapshot.miner_pk_hash,
"last_winner_snapshot.miner_pk_hash" => ?last_winner_snapshot.miner_pk_hash,
);
return Ok(());
} else {
Expand Down
42 changes: 38 additions & 4 deletions testnet/stacks-node/src/tests/nakamoto_integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ use stacks::chainstate::stacks::miner::{
BlockBuilder, BlockLimitFunction, TransactionEvent, TransactionResult, TransactionSuccessEvent,
};
use stacks::chainstate::stacks::{
SinglesigHashMode, SinglesigSpendingCondition, StacksTransaction, TenureChangePayload,
TransactionAnchorMode, TransactionAuth, TransactionPayload, TransactionPostConditionMode,
TransactionPublicKeyEncoding, TransactionSpendingCondition, TransactionVersion, MAX_BLOCK_LEN,
SinglesigHashMode, SinglesigSpendingCondition, StacksTransaction, TenureChangeCause,
TenureChangePayload, TransactionAnchorMode, TransactionAuth, TransactionPayload,
TransactionPostConditionMode, TransactionPublicKeyEncoding, TransactionSpendingCondition,
TransactionVersion, MAX_BLOCK_LEN,
};
use stacks::core::mempool::MAXIMUM_MEMPOOL_TX_CHAINING;
use stacks::core::{
Expand Down Expand Up @@ -5233,6 +5234,40 @@ fn continue_tenure_extend() {
.unwrap()
.unwrap();

// assert that the tenure extend tx was observed
let extend_tx_included = test_observer::get_blocks()
.into_iter()
.find(|block_json| {
block_json["transactions"]
.as_array()
.unwrap()
.iter()
.find(|tx_json| {
let raw_tx = tx_json["raw_tx"].as_str().unwrap();
if raw_tx == "0x00" {
return false;
}
let tx_bytes = hex_bytes(&raw_tx[2..]).unwrap();
let parsed =
StacksTransaction::consensus_deserialize(&mut &tx_bytes[..]).unwrap();
match parsed.payload {
TransactionPayload::TenureChange(payload) => {
if payload.cause == TenureChangeCause::Extended {
return true;
}
}
_ => {}
};
false
})
.is_some()
})
.is_some();
assert!(
extend_tx_included,
"Nakamoto node failed to include the tenure extend tx"
);

// assert that the transfer tx was observed
let transfer_tx_included = test_observer::get_blocks()
.into_iter()
Expand All @@ -5245,7 +5280,6 @@ fn continue_tenure_extend() {
.is_some()
})
.is_some();

assert!(
transfer_tx_included,
"Nakamoto node failed to include the transfer tx"
Expand Down

0 comments on commit 8281f17

Please sign in to comment.