From a2d485e91a853c173b5dd14e281c3da8ea2ff4ce Mon Sep 17 00:00:00 2001 From: Noah Citron Date: Fri, 12 Aug 2022 18:39:08 -0400 Subject: [PATCH] verify next sync and finality proofs on update --- src/main.rs | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index d190311..9884d81 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,14 +63,15 @@ impl LightClient { let updates = self.get_updates(next_period).await?; - for update in updates { - self.proccess_update(update)? + for mut update in updates { + self.proccess_update(&mut update)?; + return Ok(()); } Ok(()) } - fn proccess_update(&mut self, update: Update) -> Result<()> { + fn proccess_update(&mut self, update: &mut Update) -> Result<()> { let current_slot = self.store.header.slot; let update_slot = update.attested_header.slot; @@ -80,12 +81,29 @@ impl LightClient { println!("current period: {}", current_period); println!("update period: {}", update_period); - assert!(update_period == current_period + 1); + if !(update_period == current_period + 1) { + return Err(eyre::eyre!("Invalid Update")); + } - // TODO: all validations - + if !(update.signature_slot > update.attested_header.slot && update.attested_header.slot > update.finalized_header.slot) { + return Err(eyre::eyre!("Invalid Update")); + } + + let finality_header_hash = update.finalized_header.hash_tree_root()?; + let update_header_root = Node::from_bytes(update.attested_header.state_root); + let finality_branch = branch_to_nodes(update.finality_branch.clone()); + let finality_branch_valid = is_valid_merkle_branch(&finality_header_hash, finality_branch.iter(), 6, 41, &update_header_root); + println!("finality branch valid: {}", finality_branch_valid); + + let next_committee_hash = update.next_sync_committee.hash_tree_root()?; + let next_committee_branch = branch_to_nodes(update.next_sync_committee_branch.clone()); + let next_committee_branch_valid = is_valid_merkle_branch(&next_committee_hash, next_committee_branch.iter(), 5, 23, &update_header_root); + println!("next sync committee branch valid: {}", next_committee_branch_valid); + + if !(finality_branch_valid && next_committee_branch_valid) { + return Err(eyre::eyre!("Invalid Update")); + } - self.store.header = update.attested_header.clone(); Ok(()) } @@ -203,10 +221,14 @@ struct UpdateResponse { struct Update { attested_header: Header, next_sync_committee: SyncCommittee, - next_sync_committee_branch: Vec, + #[serde(deserialize_with = "branch_deserialize")] + next_sync_committee_branch: Vec, finalized_header: Header, - finality_branch: Vec, + #[serde(deserialize_with = "branch_deserialize")] + finality_branch: Vec, sync_aggregate: SyncAggregate, + #[serde(deserialize_with = "u64_deserialize")] + signature_slot: u64, } #[derive(serde::Deserialize, Debug, Clone)]