From 07ba0d8699e88d157e30e1261609abf97b9c92a1 Mon Sep 17 00:00:00 2001 From: cryptoAtwill Date: Wed, 15 Nov 2023 16:26:48 +0800 Subject: [PATCH 1/4] look ahead limit use finalized height --- fendermint/vm/topdown/src/sync.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/fendermint/vm/topdown/src/sync.rs b/fendermint/vm/topdown/src/sync.rs index a1270f59..3170d425 100644 --- a/fendermint/vm/topdown/src/sync.rs +++ b/fendermint/vm/topdown/src/sync.rs @@ -219,16 +219,17 @@ where return Ok(()); } - let ending_height = parent_chain_head_height - config.chain_head_delay; + // we consider the chain head finalized only after the `chain_head_delay` + let finalized_chain_head = parent_chain_head_height - config.chain_head_delay; tracing::debug!( - "last recorded height: {}, parent chain head: {}, ending_height: {}", + "last recorded height: {}, parent chain head: {}, finalized chain head: {}", last_recorded_height, parent_chain_head_height, - ending_height + finalized_chain_head ); - if last_recorded_height == ending_height { + if last_recorded_height == finalized_chain_head { tracing::debug!( "the parent has yet to produce a new block, stops at height: {last_recorded_height}" ); @@ -238,9 +239,9 @@ where // we are going backwards in terms of block height, the latest block height is lower // than our previously fetched head. It could be a chain reorg. We clear all the cache // in `provider` and start from scratch - if last_recorded_height > ending_height { + if last_recorded_height > finalized_chain_head { tracing::warn!( - "last recorded height: {last_recorded_height} more than ending height: {ending_height}" + "last recorded height: {last_recorded_height} more than finalized chain head: {finalized_chain_head}" ); return reset_cache(parent_proxy, provider, query).await; } @@ -248,7 +249,10 @@ where // we are adding 1 to the height because we are fetching block by block, we also configured // the sequential cache to use increment == 1. let starting_height = last_recorded_height + 1; - let ending_height = min(ending_height, MAX_PARENT_VIEW_BLOCK_GAP + starting_height); + let ending_height = min( + finalized_chain_head, + MAX_PARENT_VIEW_BLOCK_GAP + starting_height, + ); tracing::debug!("parent view range: {starting_height}-{ending_height}"); let new_parent_views = parent_views_in_block_range( @@ -256,6 +260,7 @@ where last_height_hash, starting_height, ending_height, + finalized_chain_head, ) .await?; @@ -377,12 +382,13 @@ async fn parent_views_in_block_range( mut previous_hash: BlockHash, start_height: BlockHeight, end_height: BlockHeight, + look_ahead_limit: BlockHeight, ) -> Result { let mut updates = vec![]; let mut total_top_down_msgs = 0; for h in start_height..=end_height { - match parent_views_at_height(parent_proxy, &previous_hash, h, end_height).await { + match parent_views_at_height(parent_proxy, &previous_hash, h, look_ahead_limit).await { Ok((hash, changeset, cross_msgs)) => { total_top_down_msgs += cross_msgs.len(); From 975cf644756fd34745dbb8b4bf4e530ed7b4c6d7 Mon Sep 17 00:00:00 2001 From: cryptoAtwill Date: Wed, 15 Nov 2023 19:41:16 +0800 Subject: [PATCH 2/4] rename variable --- fendermint/vm/topdown/src/sync.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/fendermint/vm/topdown/src/sync.rs b/fendermint/vm/topdown/src/sync.rs index 68d30511..7d8ac778 100644 --- a/fendermint/vm/topdown/src/sync.rs +++ b/fendermint/vm/topdown/src/sync.rs @@ -224,16 +224,16 @@ where } // we consider the chain head finalized only after the `chain_head_delay` - let finalized_chain_head = parent_chain_head_height - config.chain_head_delay; + let max_ending_height = parent_chain_head_height - config.chain_head_delay; tracing::debug!( last_recorded_height = last_recorded_height, parent_chain_head_height = parent_chain_head_height, - finalized_chain_head = finalized_chain_head, + max_ending_height = max_ending_height, "syncing heights", ); - if last_recorded_height == finalized_chain_head { + if last_recorded_height == max_ending_height { tracing::debug!( last_recorded_height = last_recorded_height, "the parent has yet to produce a new block" @@ -244,11 +244,11 @@ where // we are going backwards in terms of block height, the latest block height is lower // than our previously fetched head. It could be a chain reorg. We clear all the cache // in `provider` and start from scratch - if last_recorded_height > finalized_chain_head { + if last_recorded_height > max_ending_height { tracing::warn!( last_recorded_height = last_recorded_height, - finalized_chain_head = finalized_chain_head, - "last recorded height more than ending height" + max_ending_height = max_ending_height, + "last recorded height more than max ending height" ); return reset_cache(parent_proxy, provider, query).await; } @@ -257,7 +257,7 @@ where // the sequential cache to use increment == 1. let starting_height = last_recorded_height + 1; let ending_height = min( - finalized_chain_head, + max_ending_height, MAX_PARENT_VIEW_BLOCK_GAP + starting_height, ); tracing::debug!( @@ -271,7 +271,7 @@ where last_height_hash, starting_height, ending_height, - finalized_chain_head, + max_ending_height, ) .await?; From 37da6f33b8fa4fe810757b6973387e548e0bff04 Mon Sep 17 00:00:00 2001 From: cryptoAtwill Date: Wed, 15 Nov 2023 19:48:10 +0800 Subject: [PATCH 3/4] more comments --- fendermint/vm/topdown/src/sync.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/fendermint/vm/topdown/src/sync.rs b/fendermint/vm/topdown/src/sync.rs index 7d8ac778..fd89be75 100644 --- a/fendermint/vm/topdown/src/sync.rs +++ b/fendermint/vm/topdown/src/sync.rs @@ -447,7 +447,16 @@ async fn parent_views_in_block_range( Ok(updates) } -/// Obtain the new parent views for the target height +/// Obtain the new parent views for the target height. +/// +/// For `look_ahead_limit`, the explanation is as follows: +/// Say the current height is h and we need to fetch the top down messages. For `lotus`, the state +/// at height h is only finalized at h + 1. The block hash at height h will return empty top down +/// messages. In this case, we need to get the block hash at height h + 1 to query the top down messages. +/// Sadly, the height h + 1 could be null block, we need to continuously look ahead until we found +/// a height that is not null. But we cannot go all the way to the block head as it's not considered +/// final yet. So we need to use a `look_ahead_limit` that restricts how far as head we should go. +/// If we still cannot find a height that is non-null, maybe we should try later async fn parent_views_at_height( parent_proxy: &Arc, previous_hash: &BlockHash, From 2212751a1458ff3606081b531666e55f8a8cfd92 Mon Sep 17 00:00:00 2001 From: cryptoAtwill Date: Wed, 15 Nov 2023 19:49:37 +0800 Subject: [PATCH 4/4] more logs --- fendermint/vm/topdown/src/error.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fendermint/vm/topdown/src/error.rs b/fendermint/vm/topdown/src/error.rs index ca045ae1..b826e351 100644 --- a/fendermint/vm/topdown/src/error.rs +++ b/fendermint/vm/topdown/src/error.rs @@ -15,6 +15,8 @@ pub enum Error { ParentChainReorgDetected, #[error("Cannot query parent at height {1}: {0}")] CannotQueryParent(String, BlockHeight), + /// This error happens when querying top down messages, the block ahead are all null rounds. + /// See `parent_views_at_height` for detailed explanation #[error("Look ahead limit reached from {0}: {1}")] LookAheadLimitReached(BlockHeight, BlockHeight), }