Skip to content

Commit

Permalink
feat: add block header by number content value
Browse files Browse the repository at this point in the history
  • Loading branch information
ogenev committed Feb 26, 2025
1 parent e07ffa5 commit b048e23
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 35 deletions.
2 changes: 2 additions & 0 deletions crates/ethportal-api/src/types/content_value/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum HistoryContentValue {
BlockHeaderWithProof(HeaderWithProof),
BlockBody(BlockBody),
Receipts(Receipts),
BlockHeaderByNumber(HeaderWithProof),
}

impl ContentValue for HistoryContentValue {
Expand All @@ -26,6 +27,7 @@ impl ContentValue for HistoryContentValue {
Self::BlockHeaderWithProof(value) => value.as_ssz_bytes().into(),
Self::BlockBody(value) => value.as_ssz_bytes().into(),
Self::Receipts(value) => value.as_ssz_bytes().into(),
Self::BlockHeaderByNumber(value) => value.as_ssz_bytes().into(),
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/ethportal-api/src/types/content_value/history_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum HistoryContentValue {
BlockHeaderWithProof(HeaderWithProof),
BlockBody(BlockBody),
Receipts(Receipts),
BlockHeaderByNumber(HeaderWithProof),
}

impl ContentValue for HistoryContentValue {
Expand All @@ -26,6 +27,7 @@ impl ContentValue for HistoryContentValue {
Self::BlockHeaderWithProof(value) => value.as_ssz_bytes().into(),
Self::BlockBody(value) => value.as_ssz_bytes().into(),
Self::Receipts(value) => value.as_ssz_bytes().into(),
Self::BlockHeaderByNumber(value) => value.as_ssz_bytes().into(),
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/metrics/src/history_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ impl HistoryMigrationMetrics {

pub fn get_content_value_label(&self, content_value: &HistoryContentValue) -> &str {
match content_value {
HistoryContentValue::BlockHeaderWithProof(header_with_proof) => {
HistoryContentValue::BlockHeaderWithProof(header_with_proof)
| HistoryContentValue::BlockHeaderByNumber(header_with_proof) => {
match &header_with_proof.proof {
BlockHeaderProof::None(_) => "header_no_proof",
BlockHeaderProof::PreMergeAccumulatorProof(_) => "header_pre_merge_accumulator",
Expand Down
132 changes: 98 additions & 34 deletions crates/subnetworks/history/src/storage_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use ethportal_api::{
history_new::HistoryContentValue as NewHistoryContentValue,
},
execution::{
header_with_proof::BlockHeaderProof as OldBlockHeaderProof,
header_with_proof::{
BlockHeaderProof as OldBlockHeaderProof, HeaderWithProof as OldHeaderWithProof,
},
header_with_proof_new::{
BlockHeaderProof, BlockProofHistoricalHashesAccumulator, HeaderWithProof,
},
Expand Down Expand Up @@ -157,30 +159,7 @@ fn convert_content_value(
) -> Option<RawContentValue> {
match old_content_value {
OldHistoryContentValue::BlockHeaderWithProof(old_header_with_proof) => {
let proof = match old_header_with_proof.proof {
OldBlockHeaderProof::None(_) => return None,
OldBlockHeaderProof::PreMergeAccumulatorProof(pre_merge_accumulator_proof) => {
let proof = BlockProofHistoricalHashesAccumulator::new(
pre_merge_accumulator_proof.proof.to_vec(),
)
.expect("[B256; 15] should convert to FixedVector<B256, U15>");
BlockHeaderProof::HistoricalHashes(proof)
}
OldBlockHeaderProof::HistoricalRootsBlockProof(_) => {
warn!(
content_key = content_key.to_hex(),
"Unexpected HistoricalRootsBlockProof"
);
return None;
}
OldBlockHeaderProof::HistoricalSummariesBlockProof(_) => {
warn!(
content_key = content_key.to_hex(),
"Unexpected HistoricalSummariesBlockProof"
);
return None;
}
};
let proof = get_header_proof(content_key, old_header_with_proof.clone())?;
let new_content_value = NewHistoryContentValue::BlockHeaderWithProof(HeaderWithProof {
header: old_header_with_proof.header,
proof,
Expand All @@ -191,9 +170,48 @@ fn convert_content_value(
// TODO: consider whether to filter post-merge bodies and receipts
Some(old_content_value.encode())
}
OldHistoryContentValue::BlockHeaderByNumber(old_header_with_proof) => {
let proof = get_header_proof(content_key, old_header_with_proof.clone())?;
let new_content_value = NewHistoryContentValue::BlockHeaderByNumber(HeaderWithProof {
header: old_header_with_proof.header,
proof,
});
Some(new_content_value.encode())
}
}
}

fn get_header_proof(
content_key: &HistoryContentKey,
old_header_with_proof: OldHeaderWithProof,
) -> Option<BlockHeaderProof> {
let proof = match old_header_with_proof.proof {
OldBlockHeaderProof::None(_) => return None,
OldBlockHeaderProof::PreMergeAccumulatorProof(pre_merge_accumulator_proof) => {
let proof = BlockProofHistoricalHashesAccumulator::new(
pre_merge_accumulator_proof.proof.to_vec(),
)
.expect("[B256; 15] should convert to FixedVector<B256, U15>");
BlockHeaderProof::HistoricalHashes(proof)
}
OldBlockHeaderProof::HistoricalRootsBlockProof(_) => {
warn!(
content_key = content_key.to_hex(),
"Unexpected HistoricalRootsBlockProof"
);
return None;
}
OldBlockHeaderProof::HistoricalSummariesBlockProof(_) => {
warn!(
content_key = content_key.to_hex(),
"Unexpected HistoricalSummariesBlockProof"
);
return None;
}
};
Some(proof)
}

#[cfg(test)]
mod tests {
use std::{collections::HashMap, fs, str::FromStr};
Expand Down Expand Up @@ -312,6 +330,21 @@ mod tests {
Ok(())
}

fn header_by_hash_to_number(
old_content_value: RawContentValue,
new_content_value: Option<RawContentValue>,
) -> (HistoryContentKey, RawContentValue, Option<RawContentValue>) {
let block_number = OldHeaderWithProof::from_ssz_bytes(&old_content_value)
.unwrap()
.header
.number;
(
HistoryContentKey::new_block_header_by_number(block_number),
old_content_value,
new_content_value,
)
}

// Fixtures

/// Migration shouldn't crash with undecodable content value.
Expand Down Expand Up @@ -361,15 +394,7 @@ mod tests {
headers_by_hash_with_proof_1000001_1000010
.into_iter()
.map(|(_content_key, old_content_value, new_content_value)| {
let block_number = OldHeaderWithProof::from_ssz_bytes(&old_content_value)
.unwrap()
.header
.number;
(
HistoryContentKey::new_block_header_by_number(block_number),
old_content_value,
new_content_value,
)
header_by_hash_to_number(old_content_value, new_content_value)
})
.collect()
}
Expand Down Expand Up @@ -410,6 +435,33 @@ mod tests {
(content_key, content_value, None)
}

#[fixture]
fn header_by_number_without_proof_15600000(
header_by_hash_without_proof_15600000: MigrationContentItem,
) -> MigrationContentItem {
let (_content_key, old_content_value, new_content_value) =
header_by_hash_without_proof_15600000;
header_by_hash_to_number(old_content_value, new_content_value)
}

#[fixture]
fn header_by_number_without_proof_17510000(
header_by_hash_without_proof_17510000: MigrationContentItem,
) -> MigrationContentItem {
let (_content_key, old_content_value, new_content_value) =
header_by_hash_without_proof_17510000;
header_by_hash_to_number(old_content_value, new_content_value)
}

#[fixture]
fn header_by_number_without_proof_19463337(
header_by_hash_without_proof_19463337: MigrationContentItem,
) -> MigrationContentItem {
let (_content_key, old_content_value, new_content_value) =
header_by_hash_without_proof_19463337;
header_by_hash_to_number(old_content_value, new_content_value)
}

#[fixture]
fn block_body_14764013() -> MigrationContentItem {
let content_item: ContentItem = serde_yaml::from_str(
Expand Down Expand Up @@ -461,11 +513,17 @@ mod tests {
header_by_hash_without_proof_15600000: MigrationContentItem,
header_by_hash_without_proof_17510000: MigrationContentItem,
header_by_hash_without_proof_19463337: MigrationContentItem,
header_by_number_without_proof_15600000: MigrationContentItem,
header_by_number_without_proof_17510000: MigrationContentItem,
header_by_number_without_proof_19463337: MigrationContentItem,
) -> anyhow::Result<()> {
verify_migration(&[
header_by_hash_without_proof_15600000,
header_by_hash_without_proof_17510000,
header_by_hash_without_proof_19463337,
header_by_number_without_proof_15600000,
header_by_number_without_proof_17510000,
header_by_number_without_proof_19463337,
])?;
Ok(())
}
Expand All @@ -491,6 +549,9 @@ mod tests {
header_by_hash_without_proof_15600000: MigrationContentItem,
header_by_hash_without_proof_17510000: MigrationContentItem,
header_by_hash_without_proof_19463337: MigrationContentItem,
header_by_number_without_proof_15600000: MigrationContentItem,
header_by_number_without_proof_17510000: MigrationContentItem,
header_by_number_without_proof_19463337: MigrationContentItem,
block_body_14764013: MigrationContentItem,
block_receipt_14764013: MigrationContentItem,
) -> anyhow::Result<()> {
Expand All @@ -501,6 +562,9 @@ mod tests {
content.push(header_by_hash_without_proof_15600000);
content.push(header_by_hash_without_proof_17510000);
content.push(header_by_hash_without_proof_19463337);
content.push(header_by_number_without_proof_15600000);
content.push(header_by_number_without_proof_17510000);
content.push(header_by_number_without_proof_19463337);
content.push(block_body_14764013);
content.push(block_receipt_14764013);
content.shuffle(&mut rand::thread_rng());
Expand Down

0 comments on commit b048e23

Please sign in to comment.