Skip to content

Commit 19ed2e6

Browse files
committed
refactor: remove ExtHeaderRecord
1 parent 550fa74 commit 19ed2e6

File tree

6 files changed

+48
-132
lines changed

6 files changed

+48
-132
lines changed

crates/header-accumulator/README.md

+13-16
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ cd crates/header-accumulator && cargo doc --open
3030
- **`generate_inclusion_proof`**: Generates inclusion proofs for a
3131
specified range of blocks, useful for verifying the presence of
3232
specific blocks within a dataset.
33-
- **`verify_inclusion_proof`**: Verifies inclusion proofs for a
33+
- **`verify_inclusion_proof`**: Verifies inclusion proofs for a
3434
specified range of blocks. Use this to confirm the accuracy of
3535
inclusion proofs.
3636

3737
### Options
3838

39-
- `-h, --help`: Displays a help message that includes usage
39+
- `-h, --help`: Displays a help message that includes usage
4040
information, commands, and options.
4141

4242
## Goals
@@ -64,11 +64,11 @@ cargo test
6464
use std::{fs::File, io::BufReader};
6565
use flat_files_decoder::{read_blocks_from_reader, Compression};
6666
use header_accumulator::{
67-
generate_inclusion_proofs, verify_inclusion_proofs, Epoch, EraValidateError, ExtHeaderRecord,
67+
generate_inclusion_proofs, verify_inclusion_proofs, Epoch, EraValidateError, Header,
6868
};
6969
7070
fn main() -> Result<(), EraValidateError> {
71-
let mut headers: Vec<ExtHeaderRecord> = Vec::new();
71+
let mut headers: Vec<Header> = Vec::new();
7272
7373
for flat_file_number in (0..=8200).step_by(100) {
7474
let file = format!(
@@ -83,8 +83,8 @@ fn main() -> Result<(), EraValidateError> {
8383
headers.extend(
8484
blocks
8585
.iter()
86-
.map(|block| ExtHeaderRecord::try_from(block).unwrap())
87-
.collect::<Vec<ExtHeaderRecord>>(),
86+
.map(|block| Header::try_from(block).unwrap())
87+
.collect::<Vec<Header>>(),
8888
);
8989
}
9090
Err(e) => {
@@ -96,10 +96,7 @@ fn main() -> Result<(), EraValidateError> {
9696
9797
let start_block = 301;
9898
let end_block = 402;
99-
let headers_to_prove: Vec<_> = headers[start_block..end_block]
100-
.iter()
101-
.map(|ext| ext.full_header.as_ref().unwrap().clone())
102-
.collect();
99+
let headers_to_prove = headers[start_block..end_block].to_vec();
103100
let epoch: Epoch = headers.try_into().unwrap();
104101
105102
let inclusion_proof = generate_inclusion_proofs(vec![epoch], headers_to_prove.clone())
@@ -121,7 +118,7 @@ fn main() -> Result<(), EraValidateError> {
121118
println!("Inclusion proof verified successfully!");
122119
123120
Ok(())
124-
}
121+
}
125122
```
126123

127124
### Era validator
@@ -130,15 +127,15 @@ fn main() -> Result<(), EraValidateError> {
130127
use std::{fs::File, io::BufReader};
131128
132129
use flat_files_decoder::{read_blocks_from_reader, Compression};
133-
use header_accumulator::{Epoch, EraValidateError, EraValidator, ExtHeaderRecord};
130+
use header_accumulator::{Epoch, EraValidateError, EraValidator, Header};
134131
use tree_hash::Hash256;
135132
136133
fn create_test_reader(path: &str) -> BufReader<File> {
137134
BufReader::new(File::open(path).unwrap())
138135
}
139136
140137
fn main() -> Result<(), EraValidateError> {
141-
let mut headers: Vec<ExtHeaderRecord> = Vec::new();
138+
let mut headers: Vec<Header> = Vec::new();
142139
for number in (0..=8200).step_by(100) {
143140
let file_name = format!(
144141
"your-test-assets/ethereum_firehose_first_8200/{:010}.dbin",
@@ -149,12 +146,12 @@ fn main() -> Result<(), EraValidateError> {
149146
let successful_headers = blocks
150147
.iter()
151148
.cloned()
152-
.map(|block| ExtHeaderRecord::try_from(&block))
149+
.map(|block| Header::try_from(&block))
153150
.collect::<Result<Vec<_>, _>>()?;
154151
headers.extend(successful_headers);
155152
}
156153
assert_eq!(headers.len(), 8300);
157-
assert_eq!(headers[0].block_number, 0);
154+
assert_eq!(headers[0].number, 0);
158155
let era_verifier = EraValidator::default();
159156
let epoch: Epoch = headers.try_into().unwrap();
160157
let result = era_verifier.validate_era(&epoch)?;
@@ -168,4 +165,4 @@ fn main() -> Result<(), EraValidateError> {
168165
169166
Ok(())
170167
}
171-
```
168+
```

crates/header-accumulator/src/epoch.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
use std::array::IntoIter;
55

66
use alloy_primitives::map::HashSet;
7-
use ethportal_api::types::execution::accumulator::{EpochAccumulator, HeaderRecord};
7+
use ethportal_api::{
8+
types::execution::accumulator::{EpochAccumulator, HeaderRecord},
9+
Header,
10+
};
811

9-
use crate::{errors::EraValidateError, types::ExtHeaderRecord};
12+
use crate::errors::EraValidateError;
1013

1114
/// The maximum number of slots per epoch in Ethereum.
1215
///
@@ -39,25 +42,25 @@ pub struct Epoch {
3942
data: Box<[HeaderRecord; MAX_EPOCH_SIZE]>,
4043
}
4144

42-
impl TryFrom<Vec<ExtHeaderRecord>> for Epoch {
45+
impl TryFrom<Vec<Header>> for Epoch {
4346
type Error = EraValidateError;
4447

45-
fn try_from(mut data: Vec<ExtHeaderRecord>) -> Result<Self, Self::Error> {
48+
fn try_from(mut data: Vec<Header>) -> Result<Self, Self::Error> {
4649
// all data must be sorted
47-
data.sort_by(|b1, b2| b1.block_number.cmp(&b2.block_number));
50+
data.sort_by(|b1, b2| b1.number.cmp(&b2.number));
4851
// max MAX_EPOCH_SIZE in the array
4952
data.truncate(MAX_EPOCH_SIZE);
5053
let len = data.len();
5154
// get the first block to get the block number
5255
let epoch_number = data
5356
.first()
54-
.map(|block| block.block_number / MAX_EPOCH_SIZE as u64)
57+
.map(|block| block.number / MAX_EPOCH_SIZE as u64)
5558
.ok_or(EraValidateError::InvalidEpochLength(0))?;
5659
// cannot have any missing blocks
5760
let blocks_missing: Vec<_> = data
5861
.windows(2)
59-
.filter(|w| w[1].block_number - w[0].block_number != 1)
60-
.map(|w| w[0].block_number + 1)
62+
.filter(|w| w[1].number - w[0].number != 1)
63+
.map(|w| w[0].number + 1)
6164
.collect();
6265
if !blocks_missing.is_empty() {
6366
return Err(EraValidateError::MissingBlock {
@@ -69,12 +72,18 @@ impl TryFrom<Vec<ExtHeaderRecord>> for Epoch {
6972
// check if all blocks are in the same era
7073
let epochs_found: HashSet<u64> = data
7174
.iter()
72-
.map(|block| block.block_number / MAX_EPOCH_SIZE as u64)
75+
.map(|block| block.number / MAX_EPOCH_SIZE as u64)
7376
.collect();
7477
if epochs_found.len() > 1 {
7578
return Err(EraValidateError::InvalidBlockInEpoch(epochs_found));
7679
}
77-
let data: Box<[HeaderRecord]> = data.into_iter().map(Into::into).collect();
80+
let data: Box<[HeaderRecord]> = data
81+
.into_iter()
82+
.map(|header| HeaderRecord {
83+
block_hash: header.hash(),
84+
total_difficulty: header.difficulty,
85+
})
86+
.collect();
7887
let data: Box<[HeaderRecord; MAX_EPOCH_SIZE]> = data
7988
.try_into()
8089
.map_err(|_| EraValidateError::InvalidEpochLength(len))?;

crates/header-accumulator/src/inclusion_proof.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::{epoch::MAX_EPOCH_SIZE, errors::EraValidateError, Epoch};
5+
pub use ethportal_api::Header;
56

67
use alloy_primitives::FixedBytes;
7-
use ethportal_api::{
8-
types::execution::{
9-
accumulator::EpochAccumulator,
10-
header_with_proof::{
11-
BlockHeaderProof, HeaderWithProof as PortalHeaderWithProof, PreMergeAccumulatorProof,
12-
},
8+
use ethportal_api::types::execution::{
9+
accumulator::EpochAccumulator,
10+
header_with_proof::{
11+
BlockHeaderProof, HeaderWithProof as PortalHeaderWithProof, PreMergeAccumulatorProof,
1312
},
14-
Header,
1513
};
1614
use trin_validation::{
1715
accumulator::PreMergeAccumulator, header_validator::HeaderValidator,

crates/header-accumulator/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ mod epoch;
88
mod era_validator;
99
mod errors;
1010
mod inclusion_proof;
11-
mod types;
1211

1312
pub use epoch::*;
1413
pub use era_validator::*;
1514
pub use errors::*;
1615
pub use inclusion_proof::*;
17-
pub use types::*;

crates/header-accumulator/src/types.rs

-83
This file was deleted.

crates/vee/README.md

+11-14
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Verifiable Extraction for Blockchain.
1010
use std::{fs::File, io::BufReader};
1111
use vee::{
1212
generate_inclusion_proofs, read_blocks_from_reader, verify_inclusion_proofs, Compression,
13-
Epoch, EraValidateError, ExtHeaderRecord,
13+
Epoch, EraValidateError, Header,
1414
};
1515
1616
fn main() -> Result<(), EraValidateError> {
17-
let mut headers: Vec<ExtHeaderRecord> = Vec::new();
17+
let mut headers: Vec<Header> = Vec::new();
1818
1919
for flat_file_number in (0..=8200).step_by(100) {
2020
let file = format!(
@@ -29,8 +29,8 @@ fn main() -> Result<(), EraValidateError> {
2929
headers.extend(
3030
blocks
3131
.iter()
32-
.map(|block| ExtHeaderRecord::try_from(block).unwrap())
33-
.collect::<Vec<ExtHeaderRecord>>(),
32+
.map(|block| Header::try_from(block).unwrap())
33+
.collect::<Vec<Header>>(),
3434
);
3535
}
3636
Err(e) => {
@@ -42,10 +42,7 @@ fn main() -> Result<(), EraValidateError> {
4242
4343
let start_block = 301;
4444
let end_block = 402;
45-
let headers_to_prove: Vec<_> = headers[start_block..end_block]
46-
.iter()
47-
.map(|ext| ext.full_header.as_ref().unwrap().clone())
48-
.collect();
45+
let headers_to_prove = headers[start_block..end_block].to_vec();
4946
let epoch: Epoch = headers.try_into().unwrap();
5047
5148
let inclusion_proof = generate_inclusion_proofs(vec![epoch], headers_to_prove.clone())
@@ -67,7 +64,7 @@ fn main() -> Result<(), EraValidateError> {
6764
println!("Inclusion proof verified successfully!");
6865
6966
Ok(())
70-
}
67+
}
7168
```
7269

7370
### Era validator
@@ -77,15 +74,15 @@ use std::{fs::File, io::BufReader};
7774
use tree_hash::Hash256;
7875
use vee::{
7976
read_blocks_from_reader, Compression, Epoch, EraValidateError, EraValidator,
80-
ExtHeaderRecord,
77+
Header,
8178
};
8279
8380
fn create_test_reader(path: &str) -> BufReader<File> {
8481
BufReader::new(File::open(path).unwrap())
8582
}
8683
8784
fn main() -> Result<(), EraValidateError> {
88-
let mut headers: Vec<ExtHeaderRecord> = Vec::new();
85+
let mut headers: Vec<Header> = Vec::new();
8986
9087
for number in (0..=8200).step_by(100) {
9188
let file_name = format!(
@@ -97,15 +94,15 @@ fn main() -> Result<(), EraValidateError> {
9794
let successful_headers = blocks
9895
.iter()
9996
.cloned()
100-
.map(|block| ExtHeaderRecord::try_from(&block))
97+
.map(|block| Header::try_from(&block))
10198
.collect::<Result<Vec<_>, _>>()?;
10299
headers.extend(successful_headers);
103100
}
104101
105102
assert_eq!(headers.len(), 8300);
106-
assert_eq!(headers[0].block_number, 0);
103+
assert_eq!(headers[0].number, 0);
107104
108-
let era_verifier = EraValidator::default();
105+
let era_verifier = EraValidator::default();
109106
let epoch: Epoch = headers.try_into().unwrap();
110107
let result = era_verifier.validate_era(&epoch)?;
111108
let expected = Hash256::new([

0 commit comments

Comments
 (0)