Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add block_time to /new_block event payload #5113

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stackslib/src/chainstate/coordinator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ pub trait BlockEventDispatcher {
pox_constants: &PoxConstants,
reward_set_data: &Option<RewardSetData>,
signer_bitvec: &Option<BitVec<4000>>,
block_timestamp: Option<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 @@ -430,6 +430,7 @@ impl BlockEventDispatcher for NullEventDispatcher {
_pox_constants: &PoxConstants,
_reward_set_data: &Option<RewardSetData>,
_signer_bitvec: &Option<BitVec<4000>>,
_block_timestamp: Option<u64>,
) {
assert!(
false,
Expand Down
3 changes: 3 additions & 0 deletions stackslib/src/chainstate/nakamoto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2049,6 +2049,8 @@ impl NakamotoChainState {

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

let block_timestamp = next_ready_block.header.timestamp;

// set stacks block accepted
let mut sort_tx = sort_db.tx_handle_begin(canonical_sortition_tip)?;
sort_tx.set_stacks_block_accepted(
Expand Down Expand Up @@ -2088,6 +2090,7 @@ impl NakamotoChainState {
&pox_constants,
&reward_set_data,
&Some(signer_bitvec),
Some(block_timestamp),
);
}

Expand Down
2 changes: 2 additions & 0 deletions stackslib/src/chainstate/stacks/db/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ impl BlockEventDispatcher for DummyEventDispatcher {
_pox_constants: &PoxConstants,
_reward_set_data: &Option<RewardSetData>,
_signer_bitvec: &Option<BitVec<4000>>,
_block_timestamp: Option<u64>,
) {
assert!(
false,
Expand Down Expand Up @@ -6409,6 +6410,7 @@ impl StacksChainState {
&pox_constants,
&reward_set_data,
&None,
None,
);
}

Expand Down
1 change: 1 addition & 0 deletions stackslib/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2017,6 +2017,7 @@ pub mod test {
pox_constants: &PoxConstants,
reward_set_data: &Option<RewardSetData>,
_signer_bitvec: &Option<BitVec<4000>>,
_block_timestamp: Option<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 @@ -594,6 +594,7 @@ impl EventObserver {
pox_constants: &PoxConstants,
reward_set_data: &Option<RewardSetData>,
signer_bitvec_opt: &Option<BitVec<4000>>,
block_timestamp: Option<u64>,
) -> serde_json::Value {
// Serialize events to JSON
let serialized_events: Vec<serde_json::Value> = filtered_events
Expand Down Expand Up @@ -631,6 +632,7 @@ impl EventObserver {
let mut payload = json!({
"block_hash": format!("0x{}", block.block_hash),
"block_height": metadata.stacks_block_height,
"block_time": block_timestamp,
"burn_block_hash": format!("0x{}", metadata.burn_header_hash),
"burn_block_height": metadata.burn_header_height,
"miner_txid": format!("0x{}", winner_txid),
Expand Down Expand Up @@ -852,6 +854,7 @@ impl BlockEventDispatcher for EventDispatcher {
pox_constants: &PoxConstants,
reward_set_data: &Option<RewardSetData>,
signer_bitvec: &Option<BitVec<4000>>,
block_timestamp: Option<u64>,
) {
self.process_chain_tip(
block,
Expand All @@ -869,6 +872,7 @@ impl BlockEventDispatcher for EventDispatcher {
pox_constants,
reward_set_data,
signer_bitvec,
block_timestamp,
);
}

Expand Down Expand Up @@ -1051,6 +1055,7 @@ impl EventDispatcher {
pox_constants: &PoxConstants,
reward_set_data: &Option<RewardSetData>,
signer_bitvec: &Option<BitVec<4000>>,
block_timestamp: Option<u64>,
) {
let all_receipts = receipts.to_owned();
let (dispatch_matrix, events) = self.create_dispatch_matrix_and_event_vector(&all_receipts);
Expand Down Expand Up @@ -1102,6 +1107,7 @@ impl EventDispatcher {
pox_constants,
reward_set_data,
signer_bitvec,
block_timestamp,
);

// Send payload
Expand Down Expand Up @@ -1508,6 +1514,7 @@ mod test {
let mblock_confirmed_consumed = ExecutionCost::zero();
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 payload = observer.make_new_block_processed_payload(
filtered_events,
Expand All @@ -1525,6 +1532,7 @@ mod test {
&pox_constants,
&None,
&Some(signer_bitvec.clone()),
block_timestamp,
);
assert_eq!(
payload
Expand Down Expand Up @@ -1576,6 +1584,7 @@ mod test {
let mblock_confirmed_consumed = ExecutionCost::zero();
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 payload = observer.make_new_block_processed_payload(
filtered_events,
Expand All @@ -1593,6 +1602,7 @@ mod test {
&pox_constants,
&None,
&Some(signer_bitvec.clone()),
block_timestamp,
);

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 @@ -197,5 +197,6 @@ pub fn announce_boot_receipts(
pox_constants,
&None,
&None,
None,
);
}
7 changes: 7 additions & 0 deletions testnet/stacks-node/src/tests/nakamoto_integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2271,6 +2271,13 @@ fn correct_burn_outs() {
"Blocks should be sorted by cycle number already"
);

let block_times: Vec<u64> = new_blocks_with_reward_set
.iter()
.filter_map(|block| block.get("block_time").and_then(|cn| cn.as_u64()))
.collect();
// Assert that block_times are all greater than 0
assert!(block_times.iter().all(|&t| t > 0));

for block in new_blocks_with_reward_set.iter() {
kantai marked this conversation as resolved.
Show resolved Hide resolved
let cycle_number = block["cycle_number"].as_u64().unwrap();
let reward_set = block["reward_set"].as_object().unwrap();
Expand Down