Skip to content

Commit 8d59498

Browse files
committed
feat: use block data for u5c
1 parent 887e49a commit 8d59498

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

src/sources/u5c.rs

+32-27
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use pallas::interop::utxorpc::spec::sync::BlockRef;
33
use pallas::network::miniprotocols::Point;
44
use serde::Deserialize;
55
use tracing::debug;
6-
use utxorpc::{CardanoSyncClient, ClientBuilder, TipEvent};
6+
use utxorpc::{CardanoSyncClient, ChainBlock, ClientBuilder, TipEvent};
77

88
use crate::framework::*;
99

@@ -22,45 +22,49 @@ pub struct Worker {
2222
}
2323

2424
impl Worker {
25+
fn block_to_record(
26+
&self,
27+
stage: &Stage,
28+
block: &ChainBlock<utxorpc::spec::cardano::Block>,
29+
) -> Result<(Point, Record), WorkerError> {
30+
let parsed = block.parsed.as_ref().ok_or(WorkerError::Panic)?;
31+
32+
let record = if stage.config.use_parsed_blocks {
33+
Record::ParsedBlock(parsed.clone())
34+
} else {
35+
Record::CborBlock(block.native.to_vec())
36+
};
37+
38+
let point = parsed
39+
.header
40+
.as_ref()
41+
.map(|h| Point::Specific(h.slot, h.hash.to_vec()))
42+
.ok_or(WorkerError::Panic)?;
43+
44+
Ok((point, record))
45+
}
46+
2547
async fn process_next(
2648
&self,
2749
stage: &mut Stage,
2850
unit: &TipEvent<utxorpc::Cardano>,
2951
) -> Result<(), WorkerError> {
3052
match unit {
3153
TipEvent::Apply(block) => {
32-
if let Some(block) = &block.parsed {
33-
let header = block.header.as_ref().unwrap();
54+
let (point, record) = self.block_to_record(stage, block)?;
3455

35-
let block = block.body.as_ref().unwrap();
56+
let evt = ChainEvent::Apply(point.clone(), record);
3657

37-
for tx in block.tx.clone() {
38-
let evt = ChainEvent::Apply(
39-
Point::Specific(header.slot, header.hash.to_vec()),
40-
Record::ParsedTx(tx),
41-
);
42-
43-
stage.output.send(evt.into()).await.or_panic()?;
44-
stage.chain_tip.set(header.slot as i64);
45-
}
46-
}
58+
stage.output.send(evt.into()).await.or_panic()?;
59+
stage.chain_tip.set(point.slot_or_default() as i64);
4760
}
4861
TipEvent::Undo(block) => {
49-
if let Some(block) = &block.parsed {
50-
let header = block.header.as_ref().unwrap();
51-
52-
let block = block.body.as_ref().unwrap();
62+
let (point, record) = self.block_to_record(stage, block)?;
5363

54-
for tx in block.tx.clone() {
55-
let evt = ChainEvent::Undo(
56-
Point::Specific(header.slot, header.hash.to_vec()),
57-
Record::ParsedTx(tx),
58-
);
64+
let evt = ChainEvent::Undo(point.clone(), record);
5965

60-
stage.output.send(evt.into()).await.or_panic()?;
61-
stage.chain_tip.set(header.slot as i64);
62-
}
63-
}
66+
stage.output.send(evt.into()).await.or_panic()?;
67+
stage.chain_tip.set(point.slot_or_default() as i64);
6468
}
6569
TipEvent::Reset(block) => {
6670
stage
@@ -154,6 +158,7 @@ pub struct Stage {
154158
#[derive(Deserialize)]
155159
pub struct Config {
156160
url: String,
161+
use_parsed_blocks: bool,
157162
}
158163

159164
impl Config {

0 commit comments

Comments
 (0)