Skip to content

Commit 8ba1a35

Browse files
committed
feat: add block_time to /new_block event payload
1 parent 2be69cb commit 8ba1a35

File tree

8 files changed

+22
-0
lines changed

8 files changed

+22
-0
lines changed

stackslib/src/chainstate/coordinator/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ pub trait BlockEventDispatcher {
179179
pox_constants: &PoxConstants,
180180
reward_set_data: &Option<RewardSetData>,
181181
signer_bitvec: &Option<BitVec<4000>>,
182+
block_timestamp: Option<u64>,
182183
);
183184

184185
/// called whenever a burn block is about to be

stackslib/src/chainstate/coordinator/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ impl BlockEventDispatcher for NullEventDispatcher {
430430
_pox_constants: &PoxConstants,
431431
_reward_set_data: &Option<RewardSetData>,
432432
_signer_bitvec: &Option<BitVec<4000>>,
433+
_block_timestamp: Option<u64>,
433434
) {
434435
assert!(
435436
false,

stackslib/src/chainstate/nakamoto/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,6 +2049,8 @@ impl NakamotoChainState {
20492049

20502050
let signer_bitvec = (&next_ready_block).header.pox_treatment.clone();
20512051

2052+
let block_timestamp = next_ready_block.header.timestamp;
2053+
20522054
// set stacks block accepted
20532055
let mut sort_tx = sort_db.tx_handle_begin(canonical_sortition_tip)?;
20542056
sort_tx.set_stacks_block_accepted(
@@ -2088,6 +2090,7 @@ impl NakamotoChainState {
20882090
&pox_constants,
20892091
&reward_set_data,
20902092
&Some(signer_bitvec),
2093+
Some(block_timestamp),
20912094
);
20922095
}
20932096

stackslib/src/chainstate/stacks/db/blocks.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ impl BlockEventDispatcher for DummyEventDispatcher {
190190
_pox_constants: &PoxConstants,
191191
_reward_set_data: &Option<RewardSetData>,
192192
_signer_bitvec: &Option<BitVec<4000>>,
193+
_block_timestamp: Option<u64>,
193194
) {
194195
assert!(
195196
false,
@@ -6409,6 +6410,7 @@ impl StacksChainState {
64096410
&pox_constants,
64106411
&reward_set_data,
64116412
&None,
6413+
None,
64126414
);
64136415
}
64146416

stackslib/src/net/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,7 @@ pub mod test {
20172017
pox_constants: &PoxConstants,
20182018
reward_set_data: &Option<RewardSetData>,
20192019
_signer_bitvec: &Option<BitVec<4000>>,
2020+
_block_timestamp: Option<u64>,
20202021
) {
20212022
self.blocks.lock().unwrap().push(TestEventObserverBlock {
20222023
block: block.clone(),

testnet/stacks-node/src/event_dispatcher.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ impl EventObserver {
594594
pox_constants: &PoxConstants,
595595
reward_set_data: &Option<RewardSetData>,
596596
signer_bitvec_opt: &Option<BitVec<4000>>,
597+
block_timestamp: Option<u64>,
597598
) -> serde_json::Value {
598599
// Serialize events to JSON
599600
let serialized_events: Vec<serde_json::Value> = filtered_events
@@ -631,6 +632,7 @@ impl EventObserver {
631632
let mut payload = json!({
632633
"block_hash": format!("0x{}", block.block_hash),
633634
"block_height": metadata.stacks_block_height,
635+
"block_time": block_timestamp,
634636
"burn_block_hash": format!("0x{}", metadata.burn_header_hash),
635637
"burn_block_height": metadata.burn_header_height,
636638
"miner_txid": format!("0x{}", winner_txid),
@@ -852,6 +854,7 @@ impl BlockEventDispatcher for EventDispatcher {
852854
pox_constants: &PoxConstants,
853855
reward_set_data: &Option<RewardSetData>,
854856
signer_bitvec: &Option<BitVec<4000>>,
857+
block_timestamp: Option<u64>,
855858
) {
856859
self.process_chain_tip(
857860
block,
@@ -869,6 +872,7 @@ impl BlockEventDispatcher for EventDispatcher {
869872
pox_constants,
870873
reward_set_data,
871874
signer_bitvec,
875+
block_timestamp,
872876
);
873877
}
874878

@@ -1051,6 +1055,7 @@ impl EventDispatcher {
10511055
pox_constants: &PoxConstants,
10521056
reward_set_data: &Option<RewardSetData>,
10531057
signer_bitvec: &Option<BitVec<4000>>,
1058+
block_timestamp: Option<u64>,
10541059
) {
10551060
let all_receipts = receipts.to_owned();
10561061
let (dispatch_matrix, events) = self.create_dispatch_matrix_and_event_vector(&all_receipts);
@@ -1102,6 +1107,7 @@ impl EventDispatcher {
11021107
pox_constants,
11031108
reward_set_data,
11041109
signer_bitvec,
1110+
block_timestamp,
11051111
);
11061112

11071113
// Send payload

testnet/stacks-node/src/run_loop/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,6 @@ pub fn announce_boot_receipts(
197197
pox_constants,
198198
&None,
199199
&None,
200+
None,
200201
);
201202
}

testnet/stacks-node/src/tests/nakamoto_integrations.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,6 +2271,13 @@ fn correct_burn_outs() {
22712271
"Blocks should be sorted by cycle number already"
22722272
);
22732273

2274+
let block_times: Vec<u64> = new_blocks_with_reward_set
2275+
.iter()
2276+
.filter_map(|block| block.get("block_time").and_then(|cn| cn.as_u64()))
2277+
.collect();
2278+
// Assert that block_times are all greater than 0
2279+
assert!(block_times.iter().all(|&t| t > 0));
2280+
22742281
for block in new_blocks_with_reward_set.iter() {
22752282
let cycle_number = block["cycle_number"].as_u64().unwrap();
22762283
let reward_set = block["reward_set"].as_object().unwrap();

0 commit comments

Comments
 (0)