Skip to content

Commit

Permalink
feat: add next batch link verification
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
ametel01 committed Jan 13, 2025
1 parent 6bf82e2 commit 301eb8e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config/anvil.messaging.docker.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"sender_address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"private_key": "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
"interval": 2,
"from_block": 7477808
"from_block": 7480941
}
7 changes: 4 additions & 3 deletions crates/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions crates/client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
7 changes: 7 additions & 0 deletions crates/guest-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub struct CombinedInput {
headers: Vec<BlockHeader>,
mmr_input: MMRInput,
batch_link: Option<String>,
next_batch_link: Option<String>,
skip_proof_verification: bool,
}

Expand All @@ -124,6 +125,7 @@ impl CombinedInput {
headers: Vec<BlockHeader>,
mmr_input: MMRInput,
batch_link: Option<String>,
next_batch_link: Option<String>,
skip_proof_verification: bool,
) -> Self {
Self {
Expand All @@ -132,6 +134,7 @@ impl CombinedInput {
headers,
mmr_input,
batch_link,
next_batch_link,
skip_proof_verification,
}
}
Expand All @@ -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
}
Expand Down
10 changes: 10 additions & 0 deletions crates/methods/mmr-append/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions crates/publisher/src/core/batch_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand Down

0 comments on commit 301eb8e

Please sign in to comment.