Skip to content

Commit

Permalink
feat(node): Output block hash as event data
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega committed Dec 25, 2021
1 parent 351c4f2 commit 2e839f2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl ChainWellKnownInfo {

#[derive(Serialize, Deserialize, Debug, Clone, Merge, Default)]
pub struct EventContext {
pub block_hash: Option<String>,
pub block_number: Option<u64>,
pub slot: Option<u64>,
pub timestamp: Option<u64>,
Expand Down
3 changes: 2 additions & 1 deletion src/sinks/terminal/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ impl LogLine {
prefix: "BLOCK",
color: Color::Magenta,
content: format!(
"{{ body size: {}, issues vkey: {}, timestamp: {} }}",
"{{ hash: {}, body size: {}, issues vkey: {}, timestamp: {} }}",
&source.context.block_hash.as_ref().unwrap_or(&"".to_string()),
body_size,
issuer_vkey,
source.context.timestamp.unwrap_or_default(),
Expand Down
15 changes: 12 additions & 3 deletions src/sources/n2c/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use pallas::{
use std::sync::mpsc::Sender;

use crate::{
framework::{ChainWellKnownInfo, Error, Event, EventData, EventSource, EventWriter},
framework::{
ChainWellKnownInfo, Error, Event, EventContext, EventData, EventSource, EventWriter,
},
mapping::ToHex,
};

Expand Down Expand Up @@ -54,11 +56,18 @@ pub struct ChainObserver(EventWriter);
impl Observer<Content> for ChainObserver {
fn on_block(
&self,
_cursor: &Option<Point>,
cursor: &Option<Point>,
content: &Content,
) -> Result<(), Box<dyn std::error::Error>> {
let Content(block) = content;
block.write_events(&self.0)?;

// inject the block hash we already have as part of the context for nested events
let writer = self.0.child_writer(EventContext {
block_hash: cursor.as_ref().map(|p| p.1.to_hex()),
..EventContext::default()
});

block.write_events(&writer)?;

Ok(())
}
Expand Down
12 changes: 10 additions & 2 deletions src/sources/n2n/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use pallas::{
use std::sync::mpsc::{Receiver, Sender};

use crate::{
framework::{ChainWellKnownInfo, Error, Event, EventData, EventSource, EventWriter},
framework::{ChainWellKnownInfo, Error, Event, EventData, EventSource, EventWriter, EventContext},
mapping::ToHex,
};

Expand Down Expand Up @@ -66,7 +66,15 @@ impl BlockObserver for Block2EventMapper {

match maybe_block {
Ok(alonzo::BlockWrapper(_, block)) => {
block.write_events(&self.0)?;
// inject the block hash into the context for nested events
let hash = crypto::hash_block_header(&block.header)?;

let writer = self.0.child_writer(EventContext {
block_hash: Some(hex::encode(hash)),
..EventContext::default()
});

block.write_events(&writer)?;
}
Err(err) => {
log::error!("{:?}", err);
Expand Down

0 comments on commit 2e839f2

Please sign in to comment.