Skip to content

Commit 68d7b16

Browse files
committed
Refactor block conversion and add missing command in README
1 parent 4d6b012 commit 68d7b16

File tree

2 files changed

+50
-47
lines changed

2 files changed

+50
-47
lines changed

mithril-common/src/cardano_transaction_parser.rs

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
};
77
use anyhow::{anyhow, Context};
88
use async_trait::async_trait;
9+
use pallas_hardano::storage::immutable::chunk::{read_blocks, Reader};
910
use pallas_traverse::MultiEraBlock;
1011
use std::path::Path;
1112
use tokio::sync::RwLock;
@@ -115,60 +116,61 @@ impl CardanoTransactionParser {
115116

116117
/// Read blocks from immutable file
117118
fn read_blocks_from_immutable_file(immutable_file: &ImmutableFile) -> StdResult<Vec<Block>> {
118-
let hardano_blocks = hardano_blocks(immutable_file)?;
119-
120-
let mut blocks = Vec::new();
121-
for block in hardano_blocks {
122-
let block = block.map_err(|e| {
123-
anyhow!(e).context(format!(
124-
"Error while reading block in immutable file: '{:?}'",
125-
immutable_file.path
126-
))
119+
let cardano_blocks_reader =
120+
CardanoTransactionParser::cardano_blocks_reader(immutable_file)?;
121+
122+
cardano_blocks_reader
123+
.into_iter()
124+
.map(|b| CardanoTransactionParser::convert_to_block(b, immutable_file))
125+
.collect::<Result<Vec<_>, _>>()
126+
.map_err(|e| anyhow!(e))
127+
}
128+
129+
fn convert_to_block(
130+
block: Result<Vec<u8>, std::io::Error>,
131+
immutable_file: &ImmutableFile,
132+
) -> StdResult<Block> {
133+
let block = block.map_err(|e| {
134+
anyhow!(e).context(format!(
135+
"Error while reading block in immutable file: '{:?}'",
136+
immutable_file.path
137+
))
138+
})?;
139+
let multi_era_block = MultiEraBlock::decode(&block).map_err(|e| {
140+
anyhow!(e).context(format!(
141+
"Error while decoding block in immutable file: '{:?}'",
142+
immutable_file.path
143+
))
144+
})?;
145+
let block =
146+
Block::try_convert(multi_era_block, immutable_file.number).with_context(|| {
147+
format!(
148+
"CardanoTransactionParser could not read data from block in immutable file: {:?}",
149+
immutable_file.path
150+
)
127151
})?;
128152

129-
match MultiEraBlock::decode(&block) {
130-
Ok(multi_era_block) => {
131-
let block = Block::try_convert(multi_era_block, immutable_file.number)
132-
.with_context(|| {
133-
format!(
134-
"CardanoTransactionParser could not read data from block in immutable file: {:?}",
135-
immutable_file.path
136-
)
137-
})?;
138-
blocks.push(block);
139-
}
140-
Err(err) => {
141-
return Err(anyhow!(err).context(format!(
142-
"Error while decoding block in immutable file: '{:?}'",
143-
immutable_file.path
144-
)))
145-
}
146-
}
147-
}
153+
Ok(block)
154+
}
155+
156+
fn cardano_blocks_reader(immutable_file: &ImmutableFile) -> StdResult<Reader> {
157+
let dir_path = immutable_file.path.parent().ok_or(anyhow!(format!(
158+
"Could not retrieve immutable file directory with immutable file path: '{:?}'",
159+
immutable_file.path
160+
)))?;
161+
let file_name = &Path::new(&immutable_file.filename)
162+
.file_stem()
163+
.ok_or(anyhow!(format!(
164+
"Could not extract immutable file name from file: '{}'",
165+
immutable_file.filename
166+
)))?
167+
.to_string_lossy();
168+
let blocks = read_blocks(dir_path, file_name)?;
148169

149170
Ok(blocks)
150171
}
151172
}
152173

153-
fn hardano_blocks(
154-
immutable_file: &ImmutableFile,
155-
) -> StdResult<pallas_hardano::storage::immutable::chunk::Reader> {
156-
let dir_path = immutable_file.path.parent().ok_or(anyhow!(format!(
157-
"Could not retrieve immutable file directory with immutable file path: '{:?}'",
158-
immutable_file.path
159-
)))?;
160-
let file_name = &Path::new(&immutable_file.filename)
161-
.file_stem()
162-
.ok_or(anyhow!(format!(
163-
"Could not extract immutable file name from file: '{}'",
164-
immutable_file.filename
165-
)))?
166-
.to_string_lossy();
167-
let blocks = pallas_hardano::storage::immutable::chunk::read_blocks(dir_path, file_name)?;
168-
169-
Ok(blocks)
170-
}
171-
172174
impl Default for CardanoTransactionParser {
173175
fn default() -> Self {
174176
Self::new()

mithril-test-lab/mithril-end-to-end/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ git clone https://github.com/input-output-hk/mithril
2121
make -C mithril-aggregator build
2222
make -C mithril-signer build
2323
make -C mithril-client-cli build
24+
make -C mithril-relay build
2425

2526
# Go to sources directory
2627
cd mithril-test-lab/mithril-end-to-end

0 commit comments

Comments
 (0)