diff --git a/consensus/types/src/light_client_header.rs b/consensus/types/src/light_client_header.rs deleted file mode 100644 index cb1cd126764..00000000000 --- a/consensus/types/src/light_client_header.rs +++ /dev/null @@ -1,141 +0,0 @@ -use crate::BeaconBlockHeader; -use crate::{test_utils::TestRandom, EthSpec, ExecutionPayloadHeader, SignedBeaconBlock, Hash256}; -use merkle_proof::{verify_merkle_proof, MerkleTree}; -use serde::{Deserialize, Serialize}; -use ssz_derive::{Decode, Encode}; -use test_random_derive::TestRandom; -use tree_hash::TreeHash; -use ssz::Encode; - -const EXECUTION_PAYLOAD_INDEX: u32 = 25; -const FLOOR_LOG2_EXECUTION_PAYLOAD_INDEX: u32 = 4; - -#[derive( - Debug, - Clone, - PartialEq, - Serialize, - Deserialize, - Encode, - Decode, - TestRandom, - arbitrary::Arbitrary, -)] -pub struct ExecutionBranch(pub [u8; FLOOR_LOG2_EXECUTION_PAYLOAD_INDEX as usize]); - -#[derive( - Debug, - Clone, - PartialEq, - Serialize, - Deserialize, - Encode, - Decode, - TestRandom, - arbitrary::Arbitrary, -)] -#[serde(bound = "E: EthSpec")] -#[arbitrary(bound = "T: EthSpec")] -pub struct LightClientHeader { - pub beacon: BeaconBlockHeader, - pub execution: Option>, - pub execution_branch: Option, -} - -impl From for LightClientHeader { - fn from(beacon: BeaconBlockHeader) -> Self { - LightClientHeader { - beacon, - execution: None, - execution_branch: None, - } - } -} - -impl From> for LightClientHeader { - fn from(block: SignedBeaconBlock) -> Self { - let epoch = block.message().slot().epoch(E::slots_per_epoch()); - - // TODO epoch greater than or equal to capella - if epoch >= 0 { - let payload = block.message().body().execution_payload(); - - // TODO fix unwrap - let header = ExecutionPayloadHeader::::from(payload.unwrap().into()); - - let leaves = block - .message() - .body_capella() - .unwrap() - .as_ssz_bytes() - .iter() - .map(|data| data.tree_hash_root()) - .collect::>(); - - let tree = MerkleTree::create(&leaves, EXECUTION_PAYLOAD_INDEX as usize); - - let execution_branch = tree.generate_proof(index, depth) - - let execution_branch = generate_proof(block.message().body_capella().unwrap().as_ssz_bytes(), EXECUTION_PAYLOAD_INDEX); - }; - - LightClientHeader { - beacon, - execution: None, - execution_branch: None, - } - } -} - -impl LightClientHeader { - fn get_lc_execution_root(&self) -> Option { - let epoch = self.beacon.slot.epoch(E::slots_per_epoch()); - - // TODO greater than or equal to CAPELLA - if epoch >= 0 { - if let Some(execution) = self.execution { - return Some(execution.tree_hash_root()); - } - } - - return None; - } - - fn is_valid_light_client_header(&self) -> bool { - let epoch = self.beacon.slot.epoch(E::slots_per_epoch()); - - // TODO LESS THAN CAPELLA - if epoch < 0 { - return self.execution == None && self.execution_branch == None; - } - - let Some(execution_root) = self.get_lc_execution_root() else { - return false - }; - - let Some(execution_branch) = self.execution_branch else { - return false - }; - - return verify_merkle_proof( - execution_root, - &execution_branch.into(), - FLOOR_LOG2_EXECUTION_PAYLOAD_INDEX, - get_subtree_index(EXECUTION_PAYLOAD_INDEX), - self.beacon.body_root, - ); - } -} - -// TODO move to the relevant place -fn get_subtree_index(generalized_index: u32) -> u32 { - return generalized_index % 2 * (log2_int(generalized_index)); -} - -// TODO move to the relevant place -fn log2_int(x: u32) -> u32 { - if x == 0 { - return 0; - } - 31 - x.leading_zeros() -}