From 301eb8e797a1a385aa0539f3edd64fd8dd6a1d1c Mon Sep 17 00:00:00 2001 From: Alex Metelli Date: Mon, 13 Jan 2025 15:04:19 +0800 Subject: [PATCH] feat: add next batch link verification - Add next_batch_link field to CombinedInput struct - Fetch parent hash of next block in batch processor - Add verification check for next batch link in MMR append method - Ensure proper chain continuity between batches --- config/anvil.messaging.docker.json | 2 +- crates/client/src/client.rs | 7 ++++--- crates/client/src/main.rs | 5 +++-- crates/guest-types/src/lib.rs | 7 +++++++ crates/methods/mmr-append/src/main.rs | 10 ++++++++++ crates/publisher/src/core/batch_processor.rs | 7 +++++++ 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/config/anvil.messaging.docker.json b/config/anvil.messaging.docker.json index eff5725..19f16ef 100644 --- a/config/anvil.messaging.docker.json +++ b/config/anvil.messaging.docker.json @@ -5,5 +5,5 @@ "sender_address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", "private_key": "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", "interval": 2, - "from_block": 7477808 + "from_block": 7480941 } diff --git a/crates/client/src/client.rs b/crates/client/src/client.rs index 6e477bc..dc155be 100644 --- a/crates/client/src/client.rs +++ b/crates/client/src/client.rs @@ -129,7 +129,7 @@ impl LightClient { let to_block = if self.blocks_per_run > 0 { BlockId::Number(std::cmp::min( self.latest_processed_block + self.blocks_per_run, - latest_block + latest_block, )) } else { BlockId::Number(latest_block) @@ -163,8 +163,9 @@ impl LightClient { .unwrap_or(self.latest_processed_block); // Check if we've reached the block limit for this run - if self.blocks_per_run > 0 && - new_latest_block > self.latest_processed_block + self.blocks_per_run { + if self.blocks_per_run > 0 + && new_latest_block > self.latest_processed_block + self.blocks_per_run + { info!( "Reached block limit for this run. Stopping at block {}", self.latest_processed_block + self.blocks_per_run diff --git a/crates/client/src/main.rs b/crates/client/src/main.rs index 70aab9b..cdbcfbb 100644 --- a/crates/client/src/main.rs +++ b/crates/client/src/main.rs @@ -41,11 +41,12 @@ async fn main() -> Result<()> { tracing::info!("Starting Fossil Light Client..."); let mut client = LightClient::new( - args.polling_interval, + args.polling_interval, args.batch_size, args.start_block, args.blocks_per_run, - ).await?; + ) + .await?; client.run().await?; Ok(()) } diff --git a/crates/guest-types/src/lib.rs b/crates/guest-types/src/lib.rs index 63bb122..60f944f 100644 --- a/crates/guest-types/src/lib.rs +++ b/crates/guest-types/src/lib.rs @@ -114,6 +114,7 @@ pub struct CombinedInput { headers: Vec, mmr_input: MMRInput, batch_link: Option, + next_batch_link: Option, skip_proof_verification: bool, } @@ -124,6 +125,7 @@ impl CombinedInput { headers: Vec, mmr_input: MMRInput, batch_link: Option, + next_batch_link: Option, skip_proof_verification: bool, ) -> Self { Self { @@ -132,6 +134,7 @@ impl CombinedInput { headers, mmr_input, batch_link, + next_batch_link, skip_proof_verification, } } @@ -156,6 +159,10 @@ impl CombinedInput { self.batch_link.as_deref() } + pub fn next_batch_link(&self) -> Option<&str> { + self.next_batch_link.as_deref() + } + pub fn skip_proof_verification(&self) -> bool { self.skip_proof_verification } diff --git a/crates/methods/mmr-append/src/main.rs b/crates/methods/mmr-append/src/main.rs index 9b37b68..57a7ac7 100644 --- a/crates/methods/mmr-append/src/main.rs +++ b/crates/methods/mmr-append/src/main.rs @@ -58,6 +58,16 @@ fn main() { } } + // Add next batch link check + match (input.next_batch_link(), &last_header.block_hash) { + (Some(next_link), _) => { + assert!(next_link == last_header.block_hash, "Next batch link mismatch"); + } + (None, _) => { + // It's okay if there's no next batch link - might be the latest block + } + } + // Create output let output = GuestOutput::new( first_batch_index, diff --git a/crates/publisher/src/core/batch_processor.rs b/crates/publisher/src/core/batch_processor.rs index 890c619..1118df8 100644 --- a/crates/publisher/src/core/batch_processor.rs +++ b/crates/publisher/src/core/batch_processor.rs @@ -172,12 +172,19 @@ impl<'a> BatchProcessor<'a> { None }; + let next_batch_link = db_connection + .get_block_header_by_number(adjusted_end_block + 1) + .await? + .map(|header| header.parent_hash) + .flatten(); + let combined_input = CombinedInput::new( chain_id, self.batch_size, headers.clone(), mmr_input, batch_link, + next_batch_link, self.skip_proof_verification, );