Skip to content

Commit ce3158c

Browse files
authored
Merge pull request #5677 from stacks-network/feat/consensus-hash-in-events
feat: include consensus_hash in emitted events
2 parents 08f65e1 + 36ab9ab commit ce3158c

File tree

10 files changed

+121
-8
lines changed

10 files changed

+121
-8
lines changed

stackslib/src/burnchains/burnchain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ impl Burnchain {
11281128
burnchain,
11291129
&sortition_tip,
11301130
None,
1131-
|_| {},
1131+
|_, _| {},
11321132
)
11331133
}
11341134

stackslib/src/chainstate/burn/db/sortdb.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4285,7 +4285,7 @@ impl SortitionDB {
42854285
/// * `next_pox_info` - iff this sortition is the first block in a reward cycle, this should be Some
42864286
/// * `announce_to` - a function that will be invoked with the calculated reward set before this method
42874287
/// commits its results. This is used to post the calculated reward set to an event observer.
4288-
pub fn evaluate_sortition<F: FnOnce(Option<RewardSetInfo>)>(
4288+
pub fn evaluate_sortition<F: FnOnce(Option<RewardSetInfo>, ConsensusHash)>(
42894289
&mut self,
42904290
mainnet: bool,
42914291
burn_header: &BurnchainBlockHeader,
@@ -4381,7 +4381,7 @@ impl SortitionDB {
43814381
.store_transition_ops(&new_snapshot.0.sortition_id, &new_snapshot.1)?;
43824382
}
43834383

4384-
announce_to(reward_set_info);
4384+
announce_to(reward_set_info, new_snapshot.0.consensus_hash);
43854385

43864386
if !dryrun {
43874387
// commit everything!

stackslib/src/chainstate/coordinator/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ pub trait BlockEventDispatcher {
194194
rewards: Vec<(PoxAddress, u64)>,
195195
burns: u64,
196196
reward_recipients: Vec<PoxAddress>,
197+
consensus_hash: &ConsensusHash,
197198
);
198199
}
199200

@@ -938,6 +939,7 @@ pub fn dispatcher_announce_burn_ops<T: BlockEventDispatcher>(
938939
burn_header: &BurnchainBlockHeader,
939940
paid_rewards: PaidRewards,
940941
reward_recipient_info: Option<RewardSetInfo>,
942+
consensus_hash: &ConsensusHash,
941943
) {
942944
let recipients = if let Some(recip_info) = reward_recipient_info {
943945
recip_info
@@ -955,6 +957,7 @@ pub fn dispatcher_announce_burn_ops<T: BlockEventDispatcher>(
955957
paid_rewards.pox,
956958
paid_rewards.burns,
957959
recipients,
960+
consensus_hash,
958961
);
959962
}
960963

@@ -2705,13 +2708,14 @@ impl<
27052708
&self.burnchain,
27062709
&last_processed_ancestor,
27072710
reward_cycle_info,
2708-
|reward_set_info| {
2711+
|reward_set_info, consensus_hash| {
27092712
if let Some(dispatcher) = dispatcher_ref {
27102713
dispatcher_announce_burn_ops(
27112714
*dispatcher,
27122715
&header,
27132716
paid_rewards,
27142717
reward_set_info,
2718+
&consensus_hash,
27152719
);
27162720
}
27172721
},

stackslib/src/chainstate/coordinator/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ impl BlockEventDispatcher for NullEventDispatcher {
446446
_rewards: Vec<(PoxAddress, u64)>,
447447
_burns: u64,
448448
_slot_holders: Vec<PoxAddress>,
449+
_consensus_hash: &ConsensusHash,
449450
) {
450451
}
451452
}

stackslib/src/chainstate/nakamoto/coordinator/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,13 +1169,14 @@ impl<
11691169
&self.burnchain,
11701170
&last_processed_ancestor,
11711171
reward_cycle_info,
1172-
|reward_set_info| {
1172+
|reward_set_info, consensus_hash| {
11731173
if let Some(dispatcher) = dispatcher_ref {
11741174
dispatcher_announce_burn_ops(
11751175
*dispatcher,
11761176
&header,
11771177
paid_rewards,
11781178
reward_set_info,
1179+
&consensus_hash,
11791180
);
11801181
}
11811182
},

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ impl BlockEventDispatcher for DummyEventDispatcher {
206206
_rewards: Vec<(PoxAddress, u64)>,
207207
_burns: u64,
208208
_slot_holders: Vec<PoxAddress>,
209+
_consensus_hash: &ConsensusHash,
209210
) {
210211
assert!(
211212
false,

stackslib/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,7 @@ check if the associated microblocks can be downloaded
14841484
&burnchain,
14851485
&sortition_tip.sortition_id,
14861486
None,
1487-
|_| {},
1487+
|_, _| {},
14881488
)
14891489
.unwrap()
14901490
};
@@ -1981,7 +1981,7 @@ fn analyze_sortition_mev(argv: Vec<String>) {
19811981
&burnchain,
19821982
&tip_sort_id,
19831983
rc_info_opt,
1984-
|_| (),
1984+
|_, _| (),
19851985
)
19861986
.unwrap();
19871987

stackslib/src/net/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,6 +2554,7 @@ pub mod test {
25542554
_rewards: Vec<(PoxAddress, u64)>,
25552555
_burns: u64,
25562556
_reward_recipients: Vec<PoxAddress>,
2557+
_consensus_hash: &ConsensusHash,
25572558
) {
25582559
// pass
25592560
}

testnet/stacks-node/src/event_dispatcher.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ impl EventObserver {
593593
rewards: Vec<(PoxAddress, u64)>,
594594
burns: u64,
595595
slot_holders: Vec<PoxAddress>,
596+
consensus_hash: &ConsensusHash,
596597
) -> serde_json::Value {
597598
let reward_recipients = rewards
598599
.into_iter()
@@ -614,7 +615,8 @@ impl EventObserver {
614615
"burn_block_height": burn_block_height,
615616
"reward_recipients": serde_json::Value::Array(reward_recipients),
616617
"reward_slot_holders": serde_json::Value::Array(reward_slot_holders),
617-
"burn_amount": burns
618+
"burn_amount": burns,
619+
"consensus_hash": format!("0x{consensus_hash}"),
618620
})
619621
}
620622

@@ -867,6 +869,7 @@ impl EventObserver {
867869
"reward_set": reward_set_value,
868870
"cycle_number": cycle_number_value,
869871
"tenure_height": coinbase_height,
872+
"consensus_hash": format!("0x{}", metadata.consensus_hash),
870873
});
871874

872875
let as_object_mut = payload.as_object_mut().unwrap();
@@ -1103,13 +1106,15 @@ impl BlockEventDispatcher for EventDispatcher {
11031106
rewards: Vec<(PoxAddress, u64)>,
11041107
burns: u64,
11051108
recipient_info: Vec<PoxAddress>,
1109+
consensus_hash: &ConsensusHash,
11061110
) {
11071111
self.process_burn_block(
11081112
burn_block,
11091113
burn_block_height,
11101114
rewards,
11111115
burns,
11121116
recipient_info,
1117+
consensus_hash,
11131118
)
11141119
}
11151120
}
@@ -1146,6 +1151,7 @@ impl EventDispatcher {
11461151
rewards: Vec<(PoxAddress, u64)>,
11471152
burns: u64,
11481153
recipient_info: Vec<PoxAddress>,
1154+
consensus_hash: &ConsensusHash,
11491155
) {
11501156
// lazily assemble payload only if we have observers
11511157
let interested_observers = self.filter_observers(&self.burn_block_observers_lookup, true);
@@ -1159,6 +1165,7 @@ impl EventDispatcher {
11591165
rewards,
11601166
burns,
11611167
recipient_info,
1168+
consensus_hash,
11621169
);
11631170

11641171
for observer in interested_observers.iter() {

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10492,3 +10492,101 @@ fn clarity_cost_spend_down() {
1049210492

1049310493
run_loop_thread.join().unwrap();
1049410494
}
10495+
10496+
#[test]
10497+
#[ignore]
10498+
fn consensus_hash_event_dispatcher() {
10499+
if env::var("BITCOIND_TEST") != Ok("1".into()) {
10500+
return;
10501+
}
10502+
10503+
let (mut conf, _miner_account) = naka_neon_integration_conf(None);
10504+
let password = "12345".to_string();
10505+
conf.connection_options.auth_token = Some(password.clone());
10506+
conf.miner.wait_on_interim_blocks = Duration::from_secs(1);
10507+
let stacker_sk = setup_stacker(&mut conf);
10508+
let signer_sk = Secp256k1PrivateKey::new();
10509+
let signer_addr = tests::to_addr(&signer_sk);
10510+
let sender_sk = Secp256k1PrivateKey::new();
10511+
// setup sender + recipient for some test stx transfers
10512+
// these are necessary for the interim blocks to get mined at all
10513+
let sender_addr = tests::to_addr(&sender_sk);
10514+
let send_amt = 100;
10515+
let send_fee = 180;
10516+
conf.add_initial_balance(
10517+
PrincipalData::from(sender_addr).to_string(),
10518+
send_amt + send_fee,
10519+
);
10520+
conf.add_initial_balance(PrincipalData::from(signer_addr).to_string(), 100000);
10521+
10522+
// only subscribe to the block proposal events
10523+
test_observer::spawn();
10524+
test_observer::register(&mut conf, &[EventKeyType::AnyEvent]);
10525+
10526+
let mut btcd_controller = BitcoinCoreController::new(conf.clone());
10527+
btcd_controller
10528+
.start_bitcoind()
10529+
.expect("Failed starting bitcoind");
10530+
let mut btc_regtest_controller = BitcoinRegtestController::new(conf.clone(), None);
10531+
btc_regtest_controller.bootstrap_chain(201);
10532+
10533+
let mut run_loop = boot_nakamoto::BootRunLoop::new(conf.clone()).unwrap();
10534+
let run_loop_stopper = run_loop.get_termination_switch();
10535+
let Counters {
10536+
blocks_processed,
10537+
naka_submitted_commits: commits_submitted,
10538+
naka_proposed_blocks: proposals_submitted,
10539+
..
10540+
} = run_loop.counters();
10541+
10542+
let coord_channel = run_loop.coordinator_channels();
10543+
10544+
let run_loop_thread = thread::spawn(move || run_loop.start(None, 0));
10545+
let mut signers = TestSigners::new(vec![signer_sk]);
10546+
wait_for_runloop(&blocks_processed);
10547+
boot_to_epoch_3(
10548+
&conf,
10549+
&blocks_processed,
10550+
&[stacker_sk],
10551+
&[signer_sk],
10552+
&mut Some(&mut signers),
10553+
&mut btc_regtest_controller,
10554+
);
10555+
10556+
info!("------------------------- Reached Epoch 3.0 -------------------------");
10557+
10558+
blind_signer(&conf, &signers, proposals_submitted);
10559+
10560+
wait_for_first_naka_block_commit(60, &commits_submitted);
10561+
10562+
let burnchain = conf.get_burnchain();
10563+
let sortdb = burnchain.open_sortition_db(true).unwrap();
10564+
10565+
let tip = SortitionDB::get_canonical_burn_chain_tip(sortdb.conn()).unwrap();
10566+
let expected_consensus_hash = format!("0x{}", tip.consensus_hash);
10567+
10568+
let burn_blocks = test_observer::get_burn_blocks();
10569+
let burn_block = burn_blocks.last().unwrap();
10570+
assert_eq!(
10571+
burn_block.get("consensus_hash").unwrap().as_str().unwrap(),
10572+
expected_consensus_hash
10573+
);
10574+
10575+
let stacks_blocks = test_observer::get_blocks();
10576+
for block in stacks_blocks.iter() {
10577+
if block.get("block_height").unwrap().as_u64().unwrap() == tip.stacks_block_height {
10578+
assert_eq!(
10579+
block.get("consensus_hash").unwrap().as_str().unwrap(),
10580+
expected_consensus_hash
10581+
);
10582+
}
10583+
}
10584+
10585+
coord_channel
10586+
.lock()
10587+
.expect("Mutex poisoned")
10588+
.stop_chains_coordinator();
10589+
run_loop_stopper.store(false, Ordering::SeqCst);
10590+
10591+
run_loop_thread.join().unwrap();
10592+
}

0 commit comments

Comments
 (0)