Skip to content

Commit e9e3cfb

Browse files
author
Aaron Blankstein
authored
Merge pull request #1983 from blockstack/feat/burn-info-block-events
#1950 - add burn block info to events
2 parents 7ed580f + 7ab74a7 commit e9e3cfb

File tree

6 files changed

+41
-8
lines changed

6 files changed

+41
-8
lines changed

src/chainstate/burn/db/sortdb.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2799,7 +2799,6 @@ impl SortitionDB {
27992799
}
28002800

28012801
/// Get a block snapshot for a winning block hash in a given burn chain fork.
2802-
#[cfg(test)]
28032802
pub fn get_block_snapshot_for_winning_stacks_block(
28042803
ic: &SortitionDBConn,
28052804
tip: &SortitionId,

src/chainstate/coordinator/mod.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::time::Duration;
2020

2121
use burnchains::{
2222
db::{BurnchainBlockData, BurnchainDB},
23-
Burnchain, BurnchainBlockHeader, BurnchainHeaderHash, Error as BurnchainError,
23+
Burnchain, BurnchainBlockHeader, BurnchainHeaderHash, Error as BurnchainError, Txid,
2424
};
2525
use chainstate::burn::{
2626
db::sortdb::{PoxId, SortitionDB, SortitionId},
@@ -107,6 +107,7 @@ pub trait BlockEventDispatcher {
107107
metadata: StacksHeaderInfo,
108108
receipts: Vec<StacksTransactionReceipt>,
109109
parent: &StacksBlockId,
110+
winner_txid: Txid,
110111
);
111112

112113
fn dispatch_boot_receipts(&mut self, receipts: Vec<StacksTransactionReceipt>);
@@ -572,15 +573,16 @@ impl<'a, T: BlockEventDispatcher, N: CoordinatorNotices, U: RewardSetProvider>
572573
&block_receipt.header.anchored_header.block_hash(),
573574
)?;
574575
if in_sortition_set {
575-
let new_canonical_stacks_block = SortitionDB::get_block_snapshot(
576+
let new_canonical_block_snapshot = SortitionDB::get_block_snapshot(
576577
self.sortition_db.conn(),
577578
canonical_sortition_tip,
578579
)?
579580
.expect(&format!(
580581
"FAIL: could not find data for the canonical sortition {}",
581582
canonical_sortition_tip
582-
))
583-
.get_canonical_stacks_block_id();
583+
));
584+
let new_canonical_stacks_block =
585+
new_canonical_block_snapshot.get_canonical_stacks_block_id();
584586
self.canonical_chain_tip = Some(new_canonical_stacks_block);
585587
debug!("Bump blocks processed");
586588
self.notifier.notify_stacks_block_processed();
@@ -589,6 +591,15 @@ impl<'a, T: BlockEventDispatcher, N: CoordinatorNotices, U: RewardSetProvider>
589591

590592
if let Some(dispatcher) = self.dispatcher {
591593
let metadata = &block_receipt.header;
594+
let winner_txid = SortitionDB::get_block_snapshot_for_winning_stacks_block(
595+
&self.sortition_db.index_conn(),
596+
canonical_sortition_tip,
597+
&block_hash,
598+
)
599+
.expect("FAIL: could not find block snapshot for winning block hash")
600+
.expect("FAIL: could not find block snapshot for winning block hash")
601+
.winning_block_txid;
602+
592603
let block: StacksBlock = {
593604
let block_path = StacksChainState::get_block_path(
594605
&self.chain_state_db.blocks_path,
@@ -609,6 +620,7 @@ impl<'a, T: BlockEventDispatcher, N: CoordinatorNotices, U: RewardSetProvider>
609620
block_receipt.header,
610621
block_receipt.tx_receipts,
611622
&parent,
623+
winner_txid,
612624
);
613625
}
614626

src/chainstate/coordinator/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ impl BlockEventDispatcher for NullEventDispatcher {
228228
_metadata: StacksHeaderInfo,
229229
_receipts: Vec<StacksTransactionReceipt>,
230230
_parent: &StacksBlockId,
231+
_winner_txid: Txid,
231232
) {
232233
assert!(
233234
false,

testnet/stacks-node/src/event_dispatcher.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ impl EventObserver {
173173
chain_tip: &ChainTip,
174174
parent_index_hash: &StacksBlockId,
175175
boot_receipts: Option<&Vec<StacksTransactionReceipt>>,
176+
winner_txid: &Txid,
176177
) {
177178
// Serialize events to JSON
178179
let serialized_events: Vec<serde_json::Value> = filtered_events
@@ -201,6 +202,9 @@ impl EventObserver {
201202
let payload = json!({
202203
"block_hash": format!("0x{}", chain_tip.block.block_hash()),
203204
"block_height": chain_tip.metadata.block_height,
205+
"burn_block_hash": format!("0x{}", chain_tip.metadata.burn_header_hash),
206+
"burn_block_height": chain_tip.metadata.burn_header_height,
207+
"miner_txid": format!("0x{}", winner_txid),
204208
"burn_block_time": chain_tip.metadata.burn_header_timestamp,
205209
"index_block_hash": format!("0x{}", chain_tip.metadata.index_block_hash()),
206210
"parent_block_hash": format!("0x{}", chain_tip.block.header.parent_block),
@@ -233,13 +237,14 @@ impl BlockEventDispatcher for EventDispatcher {
233237
metadata: StacksHeaderInfo,
234238
receipts: Vec<StacksTransactionReceipt>,
235239
parent: &StacksBlockId,
240+
winner_txid: Txid,
236241
) {
237242
let chain_tip = ChainTip {
238243
metadata,
239244
block,
240245
receipts,
241246
};
242-
self.process_chain_tip(&chain_tip, parent)
247+
self.process_chain_tip(&chain_tip, parent, winner_txid)
243248
}
244249

245250
fn dispatch_boot_receipts(&mut self, receipts: Vec<StacksTransactionReceipt>) {
@@ -260,7 +265,12 @@ impl EventDispatcher {
260265
}
261266
}
262267

263-
pub fn process_chain_tip(&self, chain_tip: &ChainTip, parent_index_hash: &StacksBlockId) {
268+
pub fn process_chain_tip(
269+
&self,
270+
chain_tip: &ChainTip,
271+
parent_index_hash: &StacksBlockId,
272+
winner_txid: Txid,
273+
) {
264274
let mut dispatch_matrix: Vec<HashSet<usize>> = self
265275
.registered_observers
266276
.iter()
@@ -346,6 +356,7 @@ impl EventDispatcher {
346356
chain_tip,
347357
parent_index_hash,
348358
boot_receipts,
359+
&winner_txid,
349360
);
350361
}
351362
}

testnet/stacks-node/src/node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ impl Node {
631631
};
632632

633633
self.event_dispatcher
634-
.process_chain_tip(&chain_tip, &parent_index_hash);
634+
.process_chain_tip(&chain_tip, &parent_index_hash, Txid([0; 32]));
635635

636636
self.chain_tip = Some(chain_tip.clone());
637637

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,16 @@ fn microblock_integration_test() {
491491
assert_eq!(&parent_index_hash, previous_index_hash);
492492
}
493493

494+
// make sure we have a burn_block_hash, burn_block_height and miner_txid
495+
496+
eprintln!("{}", block);
497+
498+
let _burn_block_hash = block.get("burn_block_hash").unwrap().as_str().unwrap();
499+
500+
let _burn_block_height = block.get("burn_block_height").unwrap().as_u64().unwrap();
501+
502+
let _miner_txid = block.get("miner_txid").unwrap().as_str().unwrap();
503+
494504
prior = Some(my_index_hash);
495505
}
496506

0 commit comments

Comments
 (0)