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 option to include raw block cbor #127

Merged
merged 6 commits into from
Feb 6, 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
21 changes: 18 additions & 3 deletions src/mapper/crawl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ impl EventWriter {
Ok(())
}

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

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

Expand Down Expand Up @@ -221,6 +221,21 @@ impl EventWriter {
Ok(())
}

pub fn crawl_with_cbor(&self, block: &Block, cbor: &[u8]) -> Result<(), Error> {
let hash = crypto::hash_block_header(&block.header);

let child = self.child_writer(EventContext {
block_hash: Some(hex::encode(&hash)),
block_number: Some(block.header.header_body.block_number),
slot: Some(block.header.header_body.slot),
timestamp: self.compute_timestamp(block.header.header_body.slot),
..EventContext::default()
});

child.crawl_block(block, &hash, cbor)
}

#[deprecated(note = "use crawl_with_cbor instead")]
pub fn crawl(&self, block: &Block) -> Result<(), Error> {
let hash = crypto::hash_block_header(&block.header);

Expand All @@ -232,6 +247,6 @@ impl EventWriter {
..EventContext::default()
});

child.crawl_block(block, &hash)
child.crawl_block(block, &hash, &[])
}
}
11 changes: 10 additions & 1 deletion src/mapper/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,12 @@ impl EventWriter {
Ok(record)
}

pub fn to_block_record(&self, source: &Block, hash: &Hash<32>) -> Result<BlockRecord, Error> {
pub fn to_block_record(
&self,
source: &Block,
hash: &Hash<32>,
cbor: &[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(),
Expand All @@ -357,6 +362,10 @@ impl EventWriter {
number: source.header.header_body.block_number,
slot: source.header.header_body.slot,
previous_hash: hex::encode(source.header.header_body.prev_hash),
cbor_hex: match self.config.include_block_cbor {
true => hex::encode(cbor).into(),
false => None,
},
})
}
}
3 changes: 3 additions & 0 deletions src/mapper/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub struct Config {

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

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

#[derive(Clone)]
Expand Down
1 change: 1 addition & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub struct BlockRecord {
pub hash: String,
pub number: u64,
pub previous_hash: String,
pub cbor_hex: Option<String>,
}

#[derive(Serialize, Deserialize, Display, Debug, Clone)]
Expand Down
8 changes: 4 additions & 4 deletions src/sources/n2c/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use pallas::{
use crate::{mapper::EventWriter, model::EventData, utils::SwallowResult, Error};

#[derive(Debug)]
struct Content(Block);
struct Content(Block, Vec<u8>);

impl EncodePayload for Content {
fn encode_payload(&self, _e: &mut PayloadEncoder) -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -30,7 +30,7 @@ impl DecodePayload for Content {
d.tag()?;
let bytes = d.bytes()?;
let BlockWrapper(_, block) = BlockWrapper::decode_fragment(bytes)?;
Ok(Content(block))
Ok(Content(block, Vec::from(bytes)))
}
}

Expand Down Expand Up @@ -63,10 +63,10 @@ impl Observer<Content> for ChainObserver {
content: &Content,
) -> Result<(), Box<dyn std::error::Error>> {
let Self(writer) = self;
let Content(block) = content;
let Content(block, cbor) = content;

writer
.crawl(block)
.crawl_with_cbor(block, cbor)
.ok_or_warn("error crawling block for events");

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/sources/n2n/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl BlockObserver for Block2EventMapper {
let Self(writer) = self;

writer
.crawl(&block)
.crawl_with_cbor(&block, &body)
.ok_or_warn("error crawling block for events");
}
Err(err) => {
Expand Down