|
| 1 | +use alloy::primitives::B256; |
| 2 | +use jsonrpsee::core::Serialize; |
| 3 | +use serde::Deserialize; |
| 4 | +use ssz::SszDecoderBuilder; |
| 5 | +use ssz_derive::{Decode, Encode}; |
| 6 | +use ssz_types::{typenum, FixedVector, VariableList}; |
| 7 | + |
| 8 | +use crate::{ |
| 9 | + types::{ |
| 10 | + bytes::ByteList1024, |
| 11 | + execution::block_body::{MERGE_TIMESTAMP, SHANGHAI_TIMESTAMP}, |
| 12 | + }, |
| 13 | + Header, |
| 14 | +}; |
| 15 | + |
| 16 | +/// The accumulator proof for the pre-merge blocks. |
| 17 | +pub type HistoricalHashesAccumulatorProof = FixedVector<B256, typenum::U15>; |
| 18 | + |
| 19 | +/// Proof that execution header root is part of BeaconBlock, post-Merge and pre-Capella |
| 20 | +pub type BeaconBlockProofPreCapella = FixedVector<B256, typenum::U11>; |
| 21 | +/// Proof that execution header root is part of BeaconBlock, post-Capella |
| 22 | +pub type BeaconBlockProof = VariableList<B256, typenum::U12>; |
| 23 | +/// Proof that BeaconBlockHeader root is part of HistoricalRoots |
| 24 | +pub type HistoricalRootsProof = FixedVector<B256, typenum::U14>; |
| 25 | +/// Proof that BeaconBlockHeader root is part of HistoricalSummaries |
| 26 | +pub type HistoricalSummariesProof = FixedVector<B256, typenum::U13>; |
| 27 | + |
| 28 | +/// A block header with accumulator proof. |
| 29 | +/// Type definition: |
| 30 | +/// https://github.com/status-im/nimbus-eth1/blob/master/fluffy/network/history/history_content.nim#L136 |
| 31 | +#[derive(Debug, Clone, PartialEq, Eq, Encode, Deserialize)] |
| 32 | +pub struct HeaderWithProof { |
| 33 | + #[ssz(with = "ssz_header")] |
| 34 | + pub header: Header, |
| 35 | + pub proof: BlockHeaderProof, |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] |
| 39 | +pub enum BlockHeaderProof { |
| 40 | + HistoricalHashesAccumulatorProof(HistoricalHashesAccumulatorProof), |
| 41 | + HistoricalRootsBlockProof(HistoricalRootsBlockProof), |
| 42 | + HistoricalSummariesBlockProof(HistoricalSummariesBlockProof), |
| 43 | +} |
| 44 | + |
| 45 | +impl ssz::Decode for HeaderWithProof { |
| 46 | + fn is_ssz_fixed_len() -> bool { |
| 47 | + false |
| 48 | + } |
| 49 | + |
| 50 | + fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, ssz::DecodeError> { |
| 51 | + let mut builder = SszDecoderBuilder::new(bytes); |
| 52 | + |
| 53 | + builder.register_anonymous_variable_length_item()?; |
| 54 | + builder.register_anonymous_variable_length_item()?; |
| 55 | + |
| 56 | + let mut decoder = builder.build()?; |
| 57 | + |
| 58 | + let header = decoder.decode_next_with(ssz_header::decode::from_ssz_bytes)?; |
| 59 | + |
| 60 | + let proof = decoder.decode_next::<ByteList1024>()?; |
| 61 | + let proof = if header.timestamp <= MERGE_TIMESTAMP { |
| 62 | + BlockHeaderProof::HistoricalHashesAccumulatorProof( |
| 63 | + HistoricalHashesAccumulatorProof::from_ssz_bytes(&proof)?, |
| 64 | + ) |
| 65 | + } else if header.number <= SHANGHAI_TIMESTAMP { |
| 66 | + BlockHeaderProof::HistoricalRootsBlockProof(HistoricalRootsBlockProof::from_ssz_bytes( |
| 67 | + &proof, |
| 68 | + )?) |
| 69 | + } else { |
| 70 | + BlockHeaderProof::HistoricalSummariesBlockProof( |
| 71 | + HistoricalSummariesBlockProof::from_ssz_bytes(&proof)?, |
| 72 | + ) |
| 73 | + }; |
| 74 | + Ok(Self { header, proof }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl ssz::Encode for BlockHeaderProof { |
| 79 | + fn is_ssz_fixed_len() -> bool { |
| 80 | + false |
| 81 | + } |
| 82 | + |
| 83 | + fn ssz_append(&self, buf: &mut Vec<u8>) { |
| 84 | + match self { |
| 85 | + BlockHeaderProof::HistoricalHashesAccumulatorProof(proof) => { |
| 86 | + proof.ssz_append(buf); |
| 87 | + } |
| 88 | + BlockHeaderProof::HistoricalRootsBlockProof(proof) => { |
| 89 | + proof.ssz_append(buf); |
| 90 | + } |
| 91 | + BlockHeaderProof::HistoricalSummariesBlockProof(proof) => { |
| 92 | + proof.ssz_append(buf); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + fn ssz_bytes_len(&self) -> usize { |
| 98 | + match self { |
| 99 | + BlockHeaderProof::HistoricalHashesAccumulatorProof(proof) => proof.ssz_bytes_len(), |
| 100 | + BlockHeaderProof::HistoricalRootsBlockProof(proof) => proof.ssz_bytes_len(), |
| 101 | + BlockHeaderProof::HistoricalSummariesBlockProof(proof) => proof.ssz_bytes_len(), |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/// The struct holds a chain of proofs. This chain of proofs allows for verifying that an EL |
| 107 | +/// `BlockHeader` is part of the canonical chain. The only requirement is having access to the |
| 108 | +/// beacon chain `historical_roots`. |
| 109 | +// Total size (8 + 1 + 3 + 1 + 14) * 32 bytes + 4 bytes = 868 bytes |
| 110 | +#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Serialize, Deserialize)] |
| 111 | +pub struct HistoricalRootsBlockProof { |
| 112 | + pub beacon_block_proof: BeaconBlockProofPreCapella, |
| 113 | + pub beacon_block_root: B256, |
| 114 | + pub historical_roots_proof: HistoricalRootsProof, |
| 115 | + pub slot: u64, |
| 116 | +} |
| 117 | + |
| 118 | +/// The struct holds a chain of proofs. This chain of proofs allows for verifying that an EL |
| 119 | +/// `BlockHeader` is part of the canonical chain. The only requirement is having access to the |
| 120 | +/// beacon chain `historical_summaries`. |
| 121 | +#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Serialize, Deserialize)] |
| 122 | +pub struct HistoricalSummariesBlockProof { |
| 123 | + pub beacon_block_proof: BeaconBlockProof, |
| 124 | + pub beacon_block_root: B256, |
| 125 | + pub historical_summaries_proof: HistoricalSummariesProof, |
| 126 | + pub slot: u64, |
| 127 | +} |
| 128 | + |
| 129 | +pub mod ssz_header { |
| 130 | + |
| 131 | + use crate::{types::bytes::ByteList2048, Header}; |
| 132 | + |
| 133 | + pub mod encode { |
| 134 | + use ssz::Encode; |
| 135 | + |
| 136 | + use super::*; |
| 137 | + |
| 138 | + pub fn is_ssz_fixed_len() -> bool { |
| 139 | + ByteList2048::is_ssz_fixed_len() |
| 140 | + } |
| 141 | + |
| 142 | + pub fn ssz_append(header: &Header, buf: &mut Vec<u8>) { |
| 143 | + let header = alloy::rlp::encode(header); |
| 144 | + ByteList2048::from(header).ssz_append(buf); |
| 145 | + } |
| 146 | + |
| 147 | + pub fn ssz_fixed_len() -> usize { |
| 148 | + ByteList2048::ssz_fixed_len() |
| 149 | + } |
| 150 | + |
| 151 | + pub fn ssz_bytes_len(header: &Header) -> usize { |
| 152 | + // The ssz encoded length is the same as rlp encoded length. |
| 153 | + alloy_rlp::Encodable::length(header) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + pub mod decode { |
| 158 | + use alloy_rlp::Decodable; |
| 159 | + use ssz::Decode; |
| 160 | + |
| 161 | + use super::*; |
| 162 | + |
| 163 | + pub fn is_ssz_fixed_len() -> bool { |
| 164 | + ByteList2048::is_ssz_fixed_len() |
| 165 | + } |
| 166 | + |
| 167 | + pub fn ssz_fixed_len() -> usize { |
| 168 | + ByteList2048::ssz_fixed_len() |
| 169 | + } |
| 170 | + |
| 171 | + pub fn from_ssz_bytes(bytes: &[u8]) -> Result<Header, ssz::DecodeError> { |
| 172 | + let rlp_encoded_header = ByteList2048::from_ssz_bytes(bytes)?; |
| 173 | + Header::decode(&mut &*rlp_encoded_header).map_err(|_| { |
| 174 | + ssz::DecodeError::BytesInvalid("Unable to decode bytes into header.".to_string()) |
| 175 | + }) |
| 176 | + } |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +#[cfg(test)] |
| 181 | +#[allow(clippy::unwrap_used)] |
| 182 | +mod tests { |
| 183 | + use std::fs; |
| 184 | + |
| 185 | + use serde_json::Value; |
| 186 | + use ssz::Decode; |
| 187 | + |
| 188 | + use super::*; |
| 189 | + use crate::utils::bytes::{hex_decode, hex_encode}; |
| 190 | + |
| 191 | + #[test_log::test] |
| 192 | + fn decode_encode_headers_with_proof() { |
| 193 | + let file = |
| 194 | + fs::read_to_string("../validation/src/assets/fluffy/1000001-1000010.json").unwrap(); |
| 195 | + let json: Value = serde_json::from_str(&file).unwrap(); |
| 196 | + let hwps = json.as_object().unwrap(); |
| 197 | + for (block_number, obj) in hwps { |
| 198 | + let block_number: u64 = block_number.parse().unwrap(); |
| 199 | + let actual_hwp = obj.get("content_value").unwrap().as_str().unwrap(); |
| 200 | + let hwp = HeaderWithProof::from_ssz_bytes(&hex_decode(actual_hwp).unwrap()).unwrap(); |
| 201 | + assert_eq!(block_number, hwp.header.number); |
| 202 | + let encoded = hex_encode(ssz::Encode::as_ssz_bytes(&hwp)); |
| 203 | + assert_eq!(encoded, actual_hwp); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + #[rstest::rstest] |
| 208 | + #[case("1000010")] |
| 209 | + #[case("14764013")] |
| 210 | + #[case("15537392")] |
| 211 | + #[case("15537393")] |
| 212 | + fn decode_encode_more_headers_with_proofs(#[case] filename: &str) { |
| 213 | + let file = fs::read_to_string(format!("../validation/src/assets/fluffy/{filename}.yaml",)) |
| 214 | + .unwrap(); |
| 215 | + let yaml: serde_yaml::Value = serde_yaml::from_str(&file).unwrap(); |
| 216 | + let actual_hwp = yaml.get("content_value").unwrap().as_str().unwrap(); |
| 217 | + let hwp = HeaderWithProof::from_ssz_bytes(&hex_decode(actual_hwp).unwrap()).unwrap(); |
| 218 | + assert_eq!(hwp.header.number, filename.parse::<u64>().unwrap()); |
| 219 | + let encoded = hex_encode(ssz::Encode::as_ssz_bytes(&hwp)); |
| 220 | + assert_eq!(encoded, actual_hwp); |
| 221 | + } |
| 222 | +} |
0 commit comments