From 8ba1a3555290f505f4555ebf1de10b1d6eeb9672 Mon Sep 17 00:00:00 2001 From: Matthew Little Date: Tue, 27 Aug 2024 11:43:19 +0200 Subject: [PATCH 1/3] feat: add `block_time` to `/new_block` event payload --- stackslib/src/chainstate/coordinator/mod.rs | 1 + stackslib/src/chainstate/coordinator/tests.rs | 1 + stackslib/src/chainstate/nakamoto/mod.rs | 3 +++ stackslib/src/chainstate/stacks/db/blocks.rs | 2 ++ stackslib/src/net/mod.rs | 1 + testnet/stacks-node/src/event_dispatcher.rs | 6 ++++++ testnet/stacks-node/src/run_loop/mod.rs | 1 + testnet/stacks-node/src/tests/nakamoto_integrations.rs | 7 +++++++ 8 files changed, 22 insertions(+) diff --git a/stackslib/src/chainstate/coordinator/mod.rs b/stackslib/src/chainstate/coordinator/mod.rs index 72e44f981c..d5ffe3e55d 100644 --- a/stackslib/src/chainstate/coordinator/mod.rs +++ b/stackslib/src/chainstate/coordinator/mod.rs @@ -179,6 +179,7 @@ pub trait BlockEventDispatcher { pox_constants: &PoxConstants, reward_set_data: &Option, signer_bitvec: &Option>, + block_timestamp: Option, ); /// called whenever a burn block is about to be diff --git a/stackslib/src/chainstate/coordinator/tests.rs b/stackslib/src/chainstate/coordinator/tests.rs index 50127af176..be5f862839 100644 --- a/stackslib/src/chainstate/coordinator/tests.rs +++ b/stackslib/src/chainstate/coordinator/tests.rs @@ -430,6 +430,7 @@ impl BlockEventDispatcher for NullEventDispatcher { _pox_constants: &PoxConstants, _reward_set_data: &Option, _signer_bitvec: &Option>, + _block_timestamp: Option, ) { assert!( false, diff --git a/stackslib/src/chainstate/nakamoto/mod.rs b/stackslib/src/chainstate/nakamoto/mod.rs index 28ba89d59d..2ccf6c1157 100644 --- a/stackslib/src/chainstate/nakamoto/mod.rs +++ b/stackslib/src/chainstate/nakamoto/mod.rs @@ -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( @@ -2088,6 +2090,7 @@ impl NakamotoChainState { &pox_constants, &reward_set_data, &Some(signer_bitvec), + Some(block_timestamp), ); } diff --git a/stackslib/src/chainstate/stacks/db/blocks.rs b/stackslib/src/chainstate/stacks/db/blocks.rs index 47cace8c4b..a45a8d60cb 100644 --- a/stackslib/src/chainstate/stacks/db/blocks.rs +++ b/stackslib/src/chainstate/stacks/db/blocks.rs @@ -190,6 +190,7 @@ impl BlockEventDispatcher for DummyEventDispatcher { _pox_constants: &PoxConstants, _reward_set_data: &Option, _signer_bitvec: &Option>, + _block_timestamp: Option, ) { assert!( false, @@ -6409,6 +6410,7 @@ impl StacksChainState { &pox_constants, &reward_set_data, &None, + None, ); } diff --git a/stackslib/src/net/mod.rs b/stackslib/src/net/mod.rs index 3ba4292f1c..ba8575a032 100644 --- a/stackslib/src/net/mod.rs +++ b/stackslib/src/net/mod.rs @@ -2017,6 +2017,7 @@ pub mod test { pox_constants: &PoxConstants, reward_set_data: &Option, _signer_bitvec: &Option>, + _block_timestamp: Option, ) { self.blocks.lock().unwrap().push(TestEventObserverBlock { block: block.clone(), diff --git a/testnet/stacks-node/src/event_dispatcher.rs b/testnet/stacks-node/src/event_dispatcher.rs index 34e42501ac..99b500dc52 100644 --- a/testnet/stacks-node/src/event_dispatcher.rs +++ b/testnet/stacks-node/src/event_dispatcher.rs @@ -594,6 +594,7 @@ impl EventObserver { pox_constants: &PoxConstants, reward_set_data: &Option, signer_bitvec_opt: &Option>, + block_timestamp: Option, ) -> serde_json::Value { // Serialize events to JSON let serialized_events: Vec = filtered_events @@ -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), @@ -852,6 +854,7 @@ impl BlockEventDispatcher for EventDispatcher { pox_constants: &PoxConstants, reward_set_data: &Option, signer_bitvec: &Option>, + block_timestamp: Option, ) { self.process_chain_tip( block, @@ -869,6 +872,7 @@ impl BlockEventDispatcher for EventDispatcher { pox_constants, reward_set_data, signer_bitvec, + block_timestamp, ); } @@ -1051,6 +1055,7 @@ impl EventDispatcher { pox_constants: &PoxConstants, reward_set_data: &Option, signer_bitvec: &Option>, + block_timestamp: Option, ) { let all_receipts = receipts.to_owned(); let (dispatch_matrix, events) = self.create_dispatch_matrix_and_event_vector(&all_receipts); @@ -1102,6 +1107,7 @@ impl EventDispatcher { pox_constants, reward_set_data, signer_bitvec, + block_timestamp, ); // Send payload diff --git a/testnet/stacks-node/src/run_loop/mod.rs b/testnet/stacks-node/src/run_loop/mod.rs index b824793e17..2be02659cd 100644 --- a/testnet/stacks-node/src/run_loop/mod.rs +++ b/testnet/stacks-node/src/run_loop/mod.rs @@ -197,5 +197,6 @@ pub fn announce_boot_receipts( pox_constants, &None, &None, + None, ); } diff --git a/testnet/stacks-node/src/tests/nakamoto_integrations.rs b/testnet/stacks-node/src/tests/nakamoto_integrations.rs index a658bfbcf6..5bd9ba87e7 100644 --- a/testnet/stacks-node/src/tests/nakamoto_integrations.rs +++ b/testnet/stacks-node/src/tests/nakamoto_integrations.rs @@ -2271,6 +2271,13 @@ fn correct_burn_outs() { "Blocks should be sorted by cycle number already" ); + let block_times: Vec = 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() { let cycle_number = block["cycle_number"].as_u64().unwrap(); let reward_set = block["reward_set"].as_object().unwrap(); From 880b345f2ddcf13a6094303b5b2d4f320156f600 Mon Sep 17 00:00:00 2001 From: Matthew Little Date: Tue, 27 Aug 2024 12:09:59 +0200 Subject: [PATCH 2/3] chore: fix tests --- testnet/stacks-node/src/event_dispatcher.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/testnet/stacks-node/src/event_dispatcher.rs b/testnet/stacks-node/src/event_dispatcher.rs index 99b500dc52..53a17aca97 100644 --- a/testnet/stacks-node/src/event_dispatcher.rs +++ b/testnet/stacks-node/src/event_dispatcher.rs @@ -1514,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, @@ -1531,6 +1532,7 @@ mod test { &pox_constants, &None, &Some(signer_bitvec.clone()), + block_timestamp, ); assert_eq!( payload @@ -1582,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, @@ -1599,6 +1602,7 @@ mod test { &pox_constants, &None, &Some(signer_bitvec.clone()), + block_timestamp, ); let event_signer_signature = payload From 4d3a200cb128a580426438b7dca9a930033dd5a4 Mon Sep 17 00:00:00 2001 From: Aaron Blankstein Date: Thu, 12 Sep 2024 11:48:51 -0500 Subject: [PATCH 3/3] test: assert block times increasing Co-authored-by: Brice Dobry --- .../src/tests/nakamoto_integrations.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/testnet/stacks-node/src/tests/nakamoto_integrations.rs b/testnet/stacks-node/src/tests/nakamoto_integrations.rs index 5bd9ba87e7..4f09a7a102 100644 --- a/testnet/stacks-node/src/tests/nakamoto_integrations.rs +++ b/testnet/stacks-node/src/tests/nakamoto_integrations.rs @@ -2271,16 +2271,18 @@ fn correct_burn_outs() { "Blocks should be sorted by cycle number already" ); - let block_times: Vec = 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)); - + let mut last_block_time = None; for block in new_blocks_with_reward_set.iter() { let cycle_number = block["cycle_number"].as_u64().unwrap(); let reward_set = block["reward_set"].as_object().unwrap(); + if let Some(block_time) = block["block_time"].as_u64() { + if let Some(last) = last_block_time { + assert!(block_time > last, "Block times should be increasing"); + } + last_block_time = Some(block_time); + } + let cycle_number = block["cycle_number"].as_u64().unwrap(); + let reward_set = block["reward_set"].as_object().unwrap(); if cycle_number < first_epoch_3_cycle { assert!(