@@ -6,6 +6,7 @@ use crate::{
6
6
} ;
7
7
use anyhow:: { anyhow, Context } ;
8
8
use async_trait:: async_trait;
9
+ use pallas_hardano:: storage:: immutable:: chunk:: { read_blocks, Reader } ;
9
10
use pallas_traverse:: MultiEraBlock ;
10
11
use std:: path:: Path ;
11
12
use tokio:: sync:: RwLock ;
@@ -115,60 +116,61 @@ impl CardanoTransactionParser {
115
116
116
117
/// Read blocks from immutable file
117
118
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
+ )
127
151
} ) ?;
128
152
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) ?;
148
169
149
170
Ok ( blocks)
150
171
}
151
172
}
152
173
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
-
172
174
impl Default for CardanoTransactionParser {
173
175
fn default ( ) -> Self {
174
176
Self :: new ( )
0 commit comments