Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add whitelist for reorg handling #88

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

parketh
Copy link
Collaborator

@parketh parketh commented Nov 27, 2024

Summary

This PR adds whitelisting for reorg handling.

The current FG cannot handle L2 block reorgs because the new block hash breaks the finality votes query. This causes the finalized head to become stuck.

We implement a new whitelist system in the CW contract to allow:

  • a privileged admin to add whitelisted blocks
  • FG to check blocks against the whitelist and skip them to unblock the derivation pipeline

Test plan

cargo build
cargo optimize
cargo test

@parketh parketh requested review from lesterli, bap2pecs and SebastianElvis and removed request for lesterli November 27, 2024 17:17
@@ -303,6 +305,29 @@ pub(crate) fn verify_finality_signature(
Ok(())
}

/// `whitelist_forked_blocks` adds a set of forked blocks to the whitelist. These blocks will be skipped by FG
/// (ie. treated as finalized) duringconsecutive quorum checks to unblock the OP derivation pipeline.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

during consecutive

check_admin(&deps, info)?;

// Check array is non-empty
if forked_blocks.is_empty() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also check the first element is less or equal to the second element in the tuple?

Comment on lines +324 to +327
FORKED_BLOCKS.update::<_, ContractError>(deps.storage, |mut blocks| {
blocks.extend(forked_blocks);
Ok(blocks)
})?;
Copy link
Collaborator

@bap2pecs bap2pecs Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to allow overalp?

e.g. [(1,10) (5, 8) (3, 20)]

should we enforce

  • no overlap
  • always incrementing

so it can only be

e.g. [(1,10) (11, 18), (300,400)]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, would suggest we do not allow overlap. This simplifies some CRUD around the forked blocks

Comment on lines +63 to +65
if height < *start {
break;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't understand the logic here. shouldn't we have a continue statement somewhere

lets say we have [(1,5), (8,13)] and height is 3

the function will return false b/c of the break here but it should return true

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually we can just do a binary search

image

Copy link
Member

@SebastianElvis SebastianElvis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

use std::collections::HashSet;

/// Map of signatures by block height and fp
pub(crate) const SIGNATURES: Map<(u64, &str), Vec<u8>> = Map::new("fp_sigs");

/// Map of (block height, block hash) tuples to the list of fps that voted for this combination
pub(crate) const BLOCK_VOTES: Map<(u64, &[u8]), HashSet<String>> = Map::new("block_hashes");

/// Ordered list of forked blocks [(start_height_1, end_height_1), (start_height_2, end_height_2), ...]
pub(crate) const FORKED_BLOCKS: Item<Vec<(u64, u64)>> = Item::new("forked_blocks");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This structure means that the vector of start/end heights is considered as a single item. If we track a lot of forks (which might be the case), then the DB ops over it could be some bottleneck.

Would we consider other structures like Map<u64, u64> where key is the start height and value is the end height?

Copy link
Collaborator

@bap2pecs bap2pecs Nov 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then the DB ops over it could be some bottleneck.

i think if it's ordered with no overlap, in practice it's gonna be efficient b/c we can just use a binary search

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we store it as a map, i don't know how to design an efficient algorithm to decide if a block is reorged or not

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think prefix_range or other iterator functions can be useful here. Check out prefixed_range_works under cw-storage-plus` repo

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just took a look. i think the mental model will be cleaner to simply use (u64, u64) and query with a naive binary search like this

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually there is a much simpler way: given a height, use prefix_range to find the first key that is smaller than the height, and then check if the height is between the key and value

Comment on lines +324 to +327
FORKED_BLOCKS.update::<_, ContractError>(deps.storage, |mut blocks| {
blocks.extend(forked_blocks);
Ok(blocks)
})?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, would suggest we do not allow overlap. This simplifies some CRUD around the forked blocks

@bap2pecs
Copy link
Collaborator

bap2pecs commented Dec 1, 2024

ok we discussed offline. so the issue was unlikely caused by an L1 reorg. instead, it's likely caused by the L1 RPC rate limiting thus causing the L2 reorgs

so this can still be useful but probably not a high-pri for now b/c usually L1 reorg depth is just 1 block and it has to get very unlucky to have a batch tx hit the exact L1 reorg block

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants