Skip to content

Commit 4dfa0bb

Browse files
committed
feat: enable post-capella HistoricalSummary Header validation
1 parent d0dd1b5 commit 4dfa0bb

File tree

6 files changed

+223
-107
lines changed

6 files changed

+223
-107
lines changed

bin/portal-bridge/src/bridge/e2hs.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ use e2store::{
1717
use ethportal_api::{
1818
jsonrpsee::http_client::HttpClient,
1919
types::{
20-
execution::header_with_proof::HeaderWithProof, network::Subnetwork, portal_wire::OfferTrace,
20+
execution::header_with_proof::{BlockHeaderProof, HeaderWithProof},
21+
network::Subnetwork,
22+
portal_wire::OfferTrace,
2123
},
2224
BlockBody, ContentValue, HistoryContentKey, HistoryContentValue, HistoryNetworkApiClient,
2325
OverlayContentKey, RawContentValue, Receipts,
@@ -39,7 +41,7 @@ use tokio::{
3941
};
4042
use tracing::{debug, error, info, warn};
4143
use trin_metrics::bridge::BridgeMetricsReporter;
42-
use trin_validation::header_validator::HeaderValidator;
44+
use trin_validation::header_validator::{HeaderValidator, HistoricalSummariesProvider};
4345

4446
use super::offer_report::{GlobalOfferReport, OfferReport};
4547
use crate::{
@@ -173,7 +175,7 @@ impl E2HSBridge {
173175
continue;
174176
}
175177
}
176-
if let Err(err) = self.validate_block_tuple(&block_tuple) {
178+
if let Err(err) = self.validate_block_tuple(&block_tuple).await {
177179
error!("Failed to validate block tuple: {err:?}");
178180
continue;
179181
}
@@ -208,10 +210,18 @@ impl E2HSBridge {
208210
.unwrap_or_else(|err| panic!("unable to read e2hs file at path: {e2hs_path:?} : {err}"))
209211
}
210212

211-
fn validate_block_tuple(&self, block_tuple: &BlockTuple) -> anyhow::Result<()> {
213+
async fn validate_block_tuple(&self, block_tuple: &BlockTuple) -> anyhow::Result<()> {
212214
let header_with_proof = &block_tuple.header_with_proof.header_with_proof;
213-
self.header_validator
214-
.validate_header_with_proof(header_with_proof)?;
215+
// The E2HS bridge doesn't have access to a provider so it can't validate historical summary
216+
// Header with Proofs
217+
if !matches!(
218+
header_with_proof.proof,
219+
BlockHeaderProof::HistoricalSummaries(_)
220+
) {
221+
self.header_validator
222+
.validate_header_with_proof(header_with_proof, HistoricalSummariesProvider::None)
223+
.await?;
224+
}
215225
let body = &block_tuple.body.body;
216226
body.validate_against_header(&header_with_proof.header)?;
217227
let receipts = &block_tuple.receipts.receipts;

bin/trin/src/lib.rs

-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::sync::Arc;
88
use cli::TrinConfig;
99
use ethportal_api::{
1010
types::{distance::Distance, network::Subnetwork},
11-
utils::bytes::hex_encode,
1211
version::get_trin_version,
1312
};
1413
use portalnet::{
@@ -19,7 +18,6 @@ use portalnet::{
1918
use rpc::{launch_jsonrpc_server, RpcServerHandle};
2019
use tokio::sync::{mpsc, RwLock};
2120
use tracing::info;
22-
use tree_hash::TreeHash;
2321
use trin_beacon::initialize_beacon_network;
2422
use trin_history::initialize_history_network;
2523
use trin_state::initialize_state_network;
@@ -68,10 +66,6 @@ pub async fn run_trin(
6866

6967
// Initialize validation oracle
7068
let header_oracle = HeaderOracle::default();
71-
info!(
72-
hash_tree_root = %hex_encode(header_oracle.header_validator.pre_merge_acc.tree_hash_root().0),
73-
"Loaded pre-merge accumulator."
74-
);
7569
let header_oracle = Arc::new(RwLock::new(header_oracle));
7670

7771
// Initialize and spawn uTP socket

crates/subnetworks/history/src/network.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl HistoryNetwork {
5555
..Default::default()
5656
};
5757
let storage = Arc::new(Mutex::new(HistoryStorage::new(storage_config)?));
58-
let validator = Arc::new(ChainHistoryValidator { header_oracle });
58+
let validator = Arc::new(ChainHistoryValidator::new(header_oracle));
5959
let ping_extensions = Arc::new(HistoryPingExtensions {});
6060
let overlay = OverlayProtocol::new(
6161
config,

crates/subnetworks/history/src/validation.rs

+36-16
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,31 @@ use ethportal_api::{
1111
};
1212
use ssz::Decode;
1313
use tokio::sync::RwLock;
14+
use tracing::info;
15+
use tree_hash::TreeHash;
1416
use trin_validation::{
17+
header_validator::{HeaderValidator, HistoricalSummariesProvider},
1518
oracle::HeaderOracle,
1619
validator::{ValidationResult, Validator},
1720
};
1821

1922
pub struct ChainHistoryValidator {
2023
pub header_oracle: Arc<RwLock<HeaderOracle>>,
24+
pub header_validator: HeaderValidator,
25+
}
26+
27+
impl ChainHistoryValidator {
28+
pub fn new(header_oracle: Arc<RwLock<HeaderOracle>>) -> Self {
29+
let header_validator = HeaderValidator::default();
30+
info!(
31+
hash_tree_root = %hex_encode(header_validator.pre_merge_acc.tree_hash_root().0),
32+
"Loaded pre-merge accumulator."
33+
);
34+
Self {
35+
header_oracle,
36+
header_validator,
37+
}
38+
}
2139
}
2240

2341
impl Validator<HistoryContentKey> for ChainHistoryValidator {
@@ -38,11 +56,12 @@ impl Validator<HistoryContentKey> for ChainHistoryValidator {
3856
"Content validation failed: Invalid header hash. Found: {header_hash:?} - Expected: {:?}",
3957
hex_encode(header_hash)
4058
);
41-
self.header_oracle
42-
.read()
43-
.await
44-
.header_validator
45-
.validate_header_with_proof(&header_with_proof)?;
59+
self.header_validator
60+
.validate_header_with_proof(
61+
&header_with_proof,
62+
HistoricalSummariesProvider::HeaderOracle(self.header_oracle.clone()),
63+
)
64+
.await?;
4665

4766
Ok(ValidationResult::new(true))
4867
}
@@ -57,11 +76,12 @@ impl Validator<HistoryContentKey> for ChainHistoryValidator {
5776
"Content validation failed: Invalid header number. Found: {header_number} - Expected: {}",
5877
key.block_number
5978
);
60-
self.header_oracle
61-
.read()
62-
.await
63-
.header_validator
64-
.validate_header_with_proof(&header_with_proof)?;
79+
self.header_validator
80+
.validate_header_with_proof(
81+
&header_with_proof,
82+
HistoricalSummariesProvider::HeaderOracle(self.header_oracle.clone()),
83+
)
84+
.await?;
6585

6686
Ok(ValidationResult::new(true))
6787
}
@@ -152,7 +172,7 @@ mod tests {
152172
let header_with_proof =
153173
HeaderWithProof::from_ssz_bytes(&header_with_proof_ssz).expect("error decoding header");
154174
let header_oracle = default_header_oracle();
155-
let chain_history_validator = ChainHistoryValidator { header_oracle };
175+
let chain_history_validator = ChainHistoryValidator::new(header_oracle);
156176
let content_key =
157177
HistoryContentKey::new_block_header_by_hash(header_with_proof.header.hash_slow());
158178
chain_history_validator
@@ -173,7 +193,7 @@ mod tests {
173193

174194
let content_value = header.as_ssz_bytes();
175195
let header_oracle = default_header_oracle();
176-
let chain_history_validator = ChainHistoryValidator { header_oracle };
196+
let chain_history_validator = ChainHistoryValidator::new(header_oracle);
177197
let content_key = HistoryContentKey::new_block_header_by_hash(header.header.hash_slow());
178198
chain_history_validator
179199
.validate_content(&content_key, &content_value)
@@ -194,7 +214,7 @@ mod tests {
194214

195215
let content_value = header.as_ssz_bytes();
196216
let header_oracle = default_header_oracle();
197-
let chain_history_validator = ChainHistoryValidator { header_oracle };
217+
let chain_history_validator = ChainHistoryValidator::new(header_oracle);
198218
let content_key = HistoryContentKey::new_block_header_by_hash(header.header.hash_slow());
199219
chain_history_validator
200220
.validate_content(&content_key, &content_value)
@@ -208,7 +228,7 @@ mod tests {
208228
let header_with_proof =
209229
HeaderWithProof::from_ssz_bytes(&header_with_proof_ssz).expect("error decoding header");
210230
let header_oracle = default_header_oracle();
211-
let chain_history_validator = ChainHistoryValidator { header_oracle };
231+
let chain_history_validator = ChainHistoryValidator::new(header_oracle);
212232
let content_key =
213233
HistoryContentKey::new_block_header_by_number(header_with_proof.header.number);
214234
chain_history_validator
@@ -229,7 +249,7 @@ mod tests {
229249

230250
let content_value = header.as_ssz_bytes();
231251
let header_oracle = default_header_oracle();
232-
let chain_history_validator = ChainHistoryValidator { header_oracle };
252+
let chain_history_validator = ChainHistoryValidator::new(header_oracle);
233253
let content_key = HistoryContentKey::new_block_header_by_number(header.header.number);
234254
chain_history_validator
235255
.validate_content(&content_key, &content_value)
@@ -250,7 +270,7 @@ mod tests {
250270

251271
let content_value = header.as_ssz_bytes();
252272
let header_oracle = default_header_oracle();
253-
let chain_history_validator = ChainHistoryValidator { header_oracle };
273+
let chain_history_validator = ChainHistoryValidator::new(header_oracle);
254274
let content_key = HistoryContentKey::new_block_header_by_number(header.header.number);
255275
chain_history_validator
256276
.validate_content(&content_key, &content_value)

0 commit comments

Comments
 (0)