Skip to content

Commit

Permalink
feat: disable history validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ogenev committed Jan 23, 2025
1 parent 695f14b commit 024a4aa
Showing 1 changed file with 94 additions and 102 deletions.
196 changes: 94 additions & 102 deletions crates/subnetworks/history/src/validation.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
use std::sync::Arc;

use alloy::primitives::B256;
use anyhow::{anyhow, ensure};
use ethportal_api::{
types::execution::{
block_body::BlockBody, header::Header, header_with_proof::HeaderWithProof,
receipts::Receipts,
},
utils::bytes::hex_encode,
HistoryContentKey,
};
use ssz::Decode;
use ethportal_api::HistoryContentKey;
use tokio::sync::RwLock;
use trin_validation::{
oracle::HeaderOracle,
Expand All @@ -24,101 +14,103 @@ pub struct ChainHistoryValidator {
impl Validator<HistoryContentKey> for ChainHistoryValidator {
async fn validate_content(
&self,
content_key: &HistoryContentKey,
content: &[u8],
_content_key: &HistoryContentKey,
_content: &[u8],
) -> anyhow::Result<ValidationResult<HistoryContentKey>> {
match content_key {
HistoryContentKey::BlockHeaderByHash(key) => {
let header_with_proof =
HeaderWithProof::from_ssz_bytes(content).map_err(|err| {
anyhow!("Header by hash content has invalid encoding: {err:?}")
})?;
let header_hash = header_with_proof.header.hash();
ensure!(
header_hash == B256::from(key.block_hash),
"Content validation failed: Invalid header hash. Found: {header_hash:?} - Expected: {:?}",
hex_encode(header_hash)
);
self.header_oracle
.read()
.await
.header_validator
.validate_header_with_proof(&header_with_proof)?;

Ok(ValidationResult::new(true))
}
HistoryContentKey::BlockHeaderByNumber(key) => {
let header_with_proof =
HeaderWithProof::from_ssz_bytes(content).map_err(|err| {
anyhow!("Header by number content has invalid encoding: {err:?}")
})?;
let header_number = header_with_proof.header.number;
ensure!(
header_number == key.block_number,
"Content validation failed: Invalid header number. Found: {header_number} - Expected: {}",
key.block_number
);
self.header_oracle
.read()
.await
.header_validator
.validate_header_with_proof(&header_with_proof)?;

Ok(ValidationResult::new(true))
}
HistoryContentKey::BlockBody(key) => {
let block_body = BlockBody::from_ssz_bytes(content)
.map_err(|msg| anyhow!("Block Body content has invalid encoding: {:?}", msg))?;
let trusted_header: Header = self
.header_oracle
.read()
.await
.recursive_find_header_by_hash_with_proof(B256::from(key.block_hash))
.await?
.header;
let actual_uncles_root = block_body.uncles_root();
if actual_uncles_root != trusted_header.uncles_hash {
return Err(anyhow!(
"Content validation failed: Invalid uncles root. Found: {:?} - Expected: {:?}",
actual_uncles_root,
trusted_header.uncles_hash
));
}
let actual_txs_root = block_body.transactions_root()?;
if actual_txs_root != trusted_header.transactions_root {
return Err(anyhow!(
"Content validation failed: Invalid transactions root. Found: {:?} - Expected: {:?}",
actual_txs_root,
trusted_header.transactions_root
));
}
Ok(ValidationResult::new(true))
}
HistoryContentKey::BlockReceipts(key) => {
let receipts = Receipts::from_ssz_bytes(content).map_err(|msg| {
anyhow!("Block Receipts content has invalid encoding: {:?}", msg)
})?;
let trusted_header: Header = self
.header_oracle
.read()
.await
.recursive_find_header_by_hash_with_proof(B256::from(key.block_hash))
.await?
.header;
let actual_receipts_root = receipts.root()?;
if actual_receipts_root != trusted_header.receipts_root {
return Err(anyhow!(
"Content validation failed: Invalid receipts root. Found: {:?} - Expected: {:?}",
actual_receipts_root,
trusted_header.receipts_root
));
}
Ok(ValidationResult::new(true))
}
}
// match content_key {
// HistoryContentKey::BlockHeaderByHash(key) => {
// let header_with_proof =
// HeaderWithProof::from_ssz_bytes(content).map_err(|err| {
// anyhow!("Header by hash content has invalid encoding: {err:?}")
// })?;
// let header_hash = header_with_proof.header.hash();
// ensure!(
// header_hash == B256::from(key.block_hash),
// "Content validation failed: Invalid header hash. Found: {header_hash:?} -
// Expected: {:?}", hex_encode(header_hash)
// );
// self.header_oracle
// .read()
// .await
// .header_validator
// .validate_header_with_proof(&header_with_proof)?;
//
// Ok(ValidationResult::new(true))
// }
// HistoryContentKey::BlockHeaderByNumber(key) => {
// let header_with_proof =
// HeaderWithProof::from_ssz_bytes(content).map_err(|err| {
// anyhow!("Header by number content has invalid encoding: {err:?}")
// })?;
// let header_number = header_with_proof.header.number;
// ensure!(
// header_number == key.block_number,
// "Content validation failed: Invalid header number. Found: {header_number} -
// Expected: {}", key.block_number
// );
// self.header_oracle
// .read()
// .await
// .header_validator
// .validate_header_with_proof(&header_with_proof)?;
//
// Ok(ValidationResult::new(true))
// }
// HistoryContentKey::BlockBody(key) => {
// let block_body = BlockBody::from_ssz_bytes(content)
// .map_err(|msg| anyhow!("Block Body content has invalid encoding: {:?}",
// msg))?; let trusted_header: Header = self
// .header_oracle
// .read()
// .await
// .recursive_find_header_by_hash_with_proof(B256::from(key.block_hash))
// .await?
// .header;
// let actual_uncles_root = block_body.uncles_root();
// if actual_uncles_root != trusted_header.uncles_hash {
// return Err(anyhow!(
// "Content validation failed: Invalid uncles root. Found: {:?} - Expected:
// {:?}", actual_uncles_root,
// trusted_header.uncles_hash
// ));
// }
// let actual_txs_root = block_body.transactions_root()?;
// if actual_txs_root != trusted_header.transactions_root {
// return Err(anyhow!(
// "Content validation failed: Invalid transactions root. Found: {:?} -
// Expected: {:?}", actual_txs_root,
// trusted_header.transactions_root
// ));
// }
// Ok(ValidationResult::new(true))
// }
// HistoryContentKey::BlockReceipts(key) => {
// let receipts = Receipts::from_ssz_bytes(content).map_err(|msg| {
// anyhow!("Block Receipts content has invalid encoding: {:?}", msg)
// })?;
// let trusted_header: Header = self
// .header_oracle
// .read()
// .await
// .recursive_find_header_by_hash_with_proof(B256::from(key.block_hash))
// .await?
// .header;
// let actual_receipts_root = receipts.root()?;
// if actual_receipts_root != trusted_header.receipts_root {
// return Err(anyhow!(
// "Content validation failed: Invalid receipts root. Found: {:?} -
// Expected: {:?}", actual_receipts_root,
// trusted_header.receipts_root
// ));
// }
// Ok(ValidationResult::new(true))
// }
// }
Ok(ValidationResult::new(true))
}
}

#[cfg(any())]
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
Expand Down

0 comments on commit 024a4aa

Please sign in to comment.