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 / tx 'end' events #60

Merged
merged 2 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions src/bin/oura/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,24 @@ pub fn run(args: &ArgMatches) -> Result<(), Error> {
(false, BearerKind::Unix) => PeerMode::AsClient,
};

let mapper = MapperConfig {
include_block_end_events: true,
..Default::default()
};

let source_setup = match mode {
PeerMode::AsNode => WatchSource::N2N(N2NConfig {
address: AddressArg(bearer, socket),
magic,
well_known: None,
mapper: MapperConfig::default(),
mapper,
since,
}),
PeerMode::AsClient => WatchSource::N2C(N2CConfig {
address: AddressArg(bearer, socket),
magic,
well_known: None,
mapper: MapperConfig::default(),
mapper,
since,
}),
};
Expand Down
8 changes: 8 additions & 0 deletions src/filters/fingerpint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,18 @@ fn build_fingerprint(event: &Event, seed: u32) -> Result<String, Error> {
.with_slot(&event.context.slot)
.with_prefix("blck")
.append_optional(&event.context.block_hash)?,
EventData::BlockEnd { .. } => b
.with_slot(&event.context.slot)
.with_prefix("blckend")
.append_optional(&event.context.block_hash)?,
EventData::Transaction { .. } => b
.with_slot(&event.context.slot)
.with_prefix("tx")
.append_optional(&event.context.tx_hash)?,
EventData::TransactionEnd { .. } => b
.with_slot(&event.context.slot)
.with_prefix("txend")
.append_optional(&event.context.tx_hash)?,
EventData::TxInput { .. } => b
.with_slot(&event.context.slot)
.with_prefix("stxi")
Expand Down
21 changes: 13 additions & 8 deletions src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,23 @@ pub enum StakeCredential {
Scripthash(String),
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct BlockRecord {
pub body_size: usize,
pub issuer_vkey: String,
pub tx_count: usize,
pub slot: u64,
pub hash: String,
pub number: u64,
}

#[derive(Serialize, Deserialize, Display, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub enum EventData {
Block {
body_size: usize,
issuer_vkey: String,
tx_count: usize,
slot: u64,
hash: String,
number: u64,
},
Block(BlockRecord),
BlockEnd(BlockRecord),
Transaction(TransactionRecord),
TransactionEnd(TransactionRecord),
TxInput(TxInputRecord),
TxOutput(TxOutputRecord),
OutputAsset(OutputAssetRecord),
Expand Down
18 changes: 14 additions & 4 deletions src/mapper/crawl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pallas::ledger::alonzo::{
TransactionBodyComponent, TransactionInput, TransactionOutput, Value,
};

use crate::framework::{Error, EventContext};
use crate::framework::{Error, EventContext, EventData};

use super::{map::ToBech32, EventWriter};

Expand Down Expand Up @@ -121,7 +121,8 @@ impl EventWriter {
aux_data: Option<&AuxiliaryData>,
) -> Result<(), Error> {
let record = self.to_transaction_record(tx, aux_data)?;
self.append_from(record)?;

self.append_from(record.clone())?;

for component in tx.iter() {
match component {
Expand Down Expand Up @@ -173,12 +174,17 @@ impl EventWriter {
self.crawl_auxdata(aux_data)?;
}

if self.config.include_transaction_end_events {
self.append(EventData::TransactionEnd(record))?;
}

Ok(())
}

fn crawl_block(&self, block: &Block, hash: &[u8]) -> Result<(), Error> {
let data = self.to_block_event(block, hash)?;
self.append(data)?;
let record = self.to_block_record(block, hash)?;

self.append(EventData::Block(record.clone()))?;

for (idx, tx) in block.transaction_bodies.iter().enumerate() {
let aux_data = block
Expand All @@ -204,6 +210,10 @@ impl EventWriter {
child.crawl_transaction(tx, aux_data)?;
}

if self.config.include_block_end_events {
self.append(EventData::BlockEnd(record))?;
}

Ok(())
}

Expand Down
8 changes: 4 additions & 4 deletions src/mapper/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use bech32::{self, ToBase32};
use serde_json::{json, Value as JsonValue};

use crate::framework::{
EventData, MetadataRecord, MetadatumRendition, MintRecord, OutputAssetRecord, StakeCredential,
TransactionRecord, TxInputRecord, TxOutputRecord,
BlockRecord, EventData, MetadataRecord, MetadatumRendition, MintRecord, OutputAssetRecord,
StakeCredential, TransactionRecord, TxInputRecord, TxOutputRecord,
};

use crate::framework::Error;
Expand Down Expand Up @@ -349,8 +349,8 @@ impl EventWriter {
Ok(record)
}

pub fn to_block_event(&self, source: &Block, hash: &[u8]) -> Result<EventData, Error> {
Ok(EventData::Block {
pub fn to_block_record(&self, source: &Block, hash: &[u8]) -> Result<BlockRecord, Error> {
Ok(BlockRecord {
body_size: source.header.header_body.block_body_size as usize,
issuer_vkey: source.header.header_body.issuer_vkey.to_hex(),
tx_count: source.transaction_bodies.len(),
Expand Down
6 changes: 6 additions & 0 deletions src/mapper/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ impl ChainWellKnownInfo {

#[derive(Deserialize, Clone, Debug, Default)]
pub struct Config {
#[serde(default)]
pub include_block_end_events: bool,

#[serde(default)]
pub include_transaction_details: bool,

#[serde(default)]
pub include_transaction_end_events: bool,
}

#[derive(Clone, Debug)]
Expand Down
35 changes: 32 additions & 3 deletions src/sinks/terminal/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::{Display, Write};
use crossterm::style::{Attribute, Color, Stylize};

use crate::framework::{
CIP25AssetRecord, Event, EventData, MetadataRecord, MintRecord, OutputAssetRecord,
BlockRecord, CIP25AssetRecord, Event, EventData, MetadataRecord, MintRecord, OutputAssetRecord,
TransactionRecord, TxInputRecord, TxOutputRecord,
};

Expand All @@ -18,15 +18,15 @@ pub struct LogLine {
impl LogLine {
pub fn new(source: Event, max_width: usize) -> LogLine {
match &source.data {
EventData::Block {
EventData::Block(BlockRecord {
body_size,
issuer_vkey,
tx_count,
slot,
hash,
number,
..
} => {
}) => {
LogLine {
prefix: "BLOCK",
color: Color::Magenta,
Expand All @@ -44,6 +44,25 @@ impl LogLine {
max_width,
}
}
EventData::BlockEnd(BlockRecord {
slot,
hash,
number,
..
}) => {
LogLine {
prefix: "ENDBLK",
color: Color::DarkMagenta,
content: format!(
"{{ slot: {}, hash: {}, number: {} }}",
slot,
hash,
number,
),
source,
max_width,
}
}
EventData::Transaction(TransactionRecord {
total_output,
fee,
Expand All @@ -59,6 +78,16 @@ impl LogLine {
source,
max_width,
},
EventData::TransactionEnd(TransactionRecord { .. }) => LogLine {
prefix: "ENDTX",
color: Color::DarkBlue,
content: format!(
"{{ hash: {:?} }}",
&source.context.tx_hash
),
source,
max_width,
},
EventData::TxInput(TxInputRecord { tx_id, index }) => LogLine {
prefix: "STXI",
color: Color::Blue,
Expand Down