Skip to content

Commit

Permalink
Merge pull request #5333 from stacks-network/feat/new_block_event_ten…
Browse files Browse the repository at this point in the history
…ure_height

Include `tenure_height` in `/new_block` event payload
  • Loading branch information
zone117x authored Oct 18, 2024
2 parents 3d1f829 + d09e034 commit 76b3094
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions stackslib/src/chainstate/coordinator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ pub trait BlockEventDispatcher {
reward_set_data: &Option<RewardSetData>,
signer_bitvec: &Option<BitVec<4000>>,
block_timestamp: Option<u64>,
coinbase_height: u64,
);

/// called whenever a burn block is about to be
Expand Down
1 change: 1 addition & 0 deletions stackslib/src/chainstate/coordinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ impl BlockEventDispatcher for NullEventDispatcher {
_reward_set_data: &Option<RewardSetData>,
_signer_bitvec: &Option<BitVec<4000>>,
_block_timestamp: Option<u64>,
_coinbase_height: u64,
) {
assert!(
false,
Expand Down
2 changes: 2 additions & 0 deletions stackslib/src/chainstate/nakamoto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2118,6 +2118,7 @@ impl NakamotoChainState {
&reward_set_data,
&Some(signer_bitvec),
Some(block_timestamp),
receipt.coinbase_height,
);
}

Expand Down Expand Up @@ -4382,6 +4383,7 @@ impl NakamotoChainState {
evaluated_epoch,
epoch_transition: applied_epoch_transition,
signers_updated,
coinbase_height,
};

Ok((epoch_receipt, clarity_commit, reward_set_data))
Expand Down
11 changes: 10 additions & 1 deletion stackslib/src/chainstate/stacks/db/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ impl BlockEventDispatcher for DummyEventDispatcher {
_reward_set_data: &Option<RewardSetData>,
_signer_bitvec: &Option<BitVec<4000>>,
_block_timestamp: Option<u64>,
_coinbase_height: u64,
) {
assert!(
false,
Expand Down Expand Up @@ -5809,8 +5810,10 @@ impl StacksChainState {
.map(|(_, _, _, info)| info.clone());

if do_not_advance {
let regtest_genesis_header = StacksHeaderInfo::regtest_genesis();
let coinbase_height = regtest_genesis_header.stacks_block_height;
let epoch_receipt = StacksEpochReceipt {
header: StacksHeaderInfo::regtest_genesis(),
header: regtest_genesis_header,
tx_receipts,
matured_rewards,
matured_rewards_info,
Expand All @@ -5822,6 +5825,7 @@ impl StacksChainState {
evaluated_epoch,
epoch_transition: applied_epoch_transition,
signers_updated: false,
coinbase_height,
};

return Ok((epoch_receipt, clarity_commit, None));
Expand Down Expand Up @@ -5898,6 +5902,9 @@ impl StacksChainState {
);
set_last_execution_cost_observed(&block_execution_cost, &block_limit);

// // The coinbase height is the same as the stacks block height in epoch 2.x
let coinbase_height = new_tip.stacks_block_height;

let epoch_receipt = StacksEpochReceipt {
header: new_tip,
tx_receipts,
Expand All @@ -5911,6 +5918,7 @@ impl StacksChainState {
evaluated_epoch,
epoch_transition: applied_epoch_transition,
signers_updated,
coinbase_height,
};

Ok((epoch_receipt, clarity_commit, reward_set_data))
Expand Down Expand Up @@ -6411,6 +6419,7 @@ impl StacksChainState {
&reward_set_data,
&None,
None,
next_staging_block.height,
);
}

Expand Down
1 change: 1 addition & 0 deletions stackslib/src/chainstate/stacks/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ pub struct StacksEpochReceipt {
pub epoch_transition: bool,
/// Was .signers updated during this block?
pub signers_updated: bool,
pub coinbase_height: u64,
}

/// Headers we serve over the network
Expand Down
1 change: 1 addition & 0 deletions stackslib/src/cost_estimates/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ pub fn make_block_receipt(tx_receipts: Vec<StacksTransactionReceipt>) -> StacksE
evaluated_epoch: StacksEpochId::Epoch20,
epoch_transition: false,
signers_updated: false,
coinbase_height: 1234,
}
}
1 change: 1 addition & 0 deletions stackslib/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2037,6 +2037,7 @@ pub mod test {
reward_set_data: &Option<RewardSetData>,
_signer_bitvec: &Option<BitVec<4000>>,
_block_timestamp: Option<u64>,
_coinbase_height: u64,
) {
self.blocks.lock().unwrap().push(TestEventObserverBlock {
block: block.clone(),
Expand Down
10 changes: 10 additions & 0 deletions testnet/stacks-node/src/event_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ impl EventObserver {
reward_set_data: &Option<RewardSetData>,
signer_bitvec_opt: &Option<BitVec<4000>>,
block_timestamp: Option<u64>,
coinbase_height: u64,
) -> serde_json::Value {
// Serialize events to JSON
let serialized_events: Vec<serde_json::Value> = filtered_events
Expand Down Expand Up @@ -820,6 +821,7 @@ impl EventObserver {
"signer_bitvec": signer_bitvec_value,
"reward_set": reward_set_value,
"cycle_number": cycle_number_value,
"tenure_height": coinbase_height,
});

let as_object_mut = payload.as_object_mut().unwrap();
Expand Down Expand Up @@ -1019,6 +1021,7 @@ impl BlockEventDispatcher for EventDispatcher {
reward_set_data: &Option<RewardSetData>,
signer_bitvec: &Option<BitVec<4000>>,
block_timestamp: Option<u64>,
coinbase_height: u64,
) {
self.process_chain_tip(
block,
Expand All @@ -1037,6 +1040,7 @@ impl BlockEventDispatcher for EventDispatcher {
reward_set_data,
signer_bitvec,
block_timestamp,
coinbase_height,
);
}

Expand Down Expand Up @@ -1220,6 +1224,7 @@ impl EventDispatcher {
reward_set_data: &Option<RewardSetData>,
signer_bitvec: &Option<BitVec<4000>>,
block_timestamp: Option<u64>,
coinbase_height: u64,
) {
let all_receipts = receipts.to_owned();
let (dispatch_matrix, events) = self.create_dispatch_matrix_and_event_vector(&all_receipts);
Expand Down Expand Up @@ -1272,6 +1277,7 @@ impl EventDispatcher {
reward_set_data,
signer_bitvec,
block_timestamp,
coinbase_height,
);

// Send payload
Expand Down Expand Up @@ -1680,6 +1686,7 @@ mod test {
let pox_constants = PoxConstants::testnet_default();
let signer_bitvec = BitVec::zeros(2).expect("Failed to create BitVec with length 2");
let block_timestamp = Some(123456);
let coinbase_height = 1234;

let payload = observer.make_new_block_processed_payload(
filtered_events,
Expand All @@ -1698,6 +1705,7 @@ mod test {
&None,
&Some(signer_bitvec.clone()),
block_timestamp,
coinbase_height,
);
assert_eq!(
payload
Expand Down Expand Up @@ -1748,6 +1756,7 @@ mod test {
let pox_constants = PoxConstants::testnet_default();
let signer_bitvec = BitVec::zeros(2).expect("Failed to create BitVec with length 2");
let block_timestamp = Some(123456);
let coinbase_height = 1234;

let payload = observer.make_new_block_processed_payload(
filtered_events,
Expand All @@ -1766,6 +1775,7 @@ mod test {
&None,
&Some(signer_bitvec.clone()),
block_timestamp,
coinbase_height,
);

let event_signer_signature = payload
Expand Down
1 change: 1 addition & 0 deletions testnet/stacks-node/src/run_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,6 @@ pub fn announce_boot_receipts(
&None,
&None,
None,
0,
);
}
46 changes: 46 additions & 0 deletions testnet/stacks-node/src/tests/nakamoto_integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4866,6 +4866,7 @@ fn burn_ops_integration_test() {
"Stack-stx tx without a signer_key shouldn't have been submitted"
);
assert!(transfer_stx_found, "Expected transfer STX op");

assert!(delegate_stx_found, "Expected delegate STX op");
let sortdb = btc_regtest_controller.sortdb_mut();
let sortdb_conn = sortdb.conn();
Expand Down Expand Up @@ -8327,6 +8328,51 @@ fn check_block_info() {
"Contract 3 should be able to fetch the StacksBlockId of the tip"
);

let mut blocks = test_observer::get_blocks();
blocks.sort_by_key(|block| block["block_height"].as_u64().unwrap());

let mut last_tenture_height = 0;
for block in blocks.iter() {
let transactions = block.get("transactions").unwrap().as_array().unwrap();
let mut block_has_tenure_change = false;
for tx in transactions.iter().rev() {
let raw_tx = tx.get("raw_tx").unwrap().as_str().unwrap();
if raw_tx != "0x00" {
let tx_bytes = hex_bytes(&raw_tx[2..]).unwrap();
let parsed =
StacksTransaction::consensus_deserialize(&mut tx_bytes.as_slice()).unwrap();
if let TransactionPayload::TenureChange(_tenure_change) = parsed.payload {
block_has_tenure_change = true;
continue;
}
}
}
// if `signer_bitvec` is set on a block, then it's a nakamoto block
let is_nakamoto_block = block.get("signer_bitvec").map_or(false, |v| !v.is_null());
let tenure_height = block.get("tenure_height").unwrap().as_u64().unwrap();
let block_height = block.get("block_height").unwrap().as_u64().unwrap();

if block_height == 0 {
// genesis block
continue;
}

if is_nakamoto_block {
if block_has_tenure_change {
// tenure change block should have tenure height 1 more than the last tenure height
assert_eq!(last_tenture_height + 1, tenure_height);
last_tenture_height = tenure_height;
} else {
// tenure extend block should have the same tenure height as the last tenure height
assert_eq!(last_tenture_height, tenure_height);
}
} else {
// epoch2.x block tenure height is the same as the block height
assert_eq!(tenure_height, block_height);
last_tenture_height = block_height;
}
}

coord_channel
.lock()
.expect("Mutex poisoned")
Expand Down

0 comments on commit 76b3094

Please sign in to comment.