-
Notifications
You must be signed in to change notification settings - Fork 7
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
base: main
Are you sure you want to change the base?
Changes from all commits
965fa6d
802ff7e
3a27c18
2dac083
3494630
342b36e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,19 +3,21 @@ use std::collections::HashSet; | |
use crate::error::ContractError; | ||
use crate::queries::query_last_pub_rand_commit; | ||
use crate::state::config::CONFIG; | ||
use crate::state::finality::{BLOCK_VOTES, SIGNATURES}; | ||
use crate::state::finality::{BLOCK_VOTES, FORKED_BLOCKS, SIGNATURES}; | ||
use crate::state::public_randomness::{ | ||
get_pub_rand_commit_for_height, PUB_RAND_COMMITS, PUB_RAND_VALUES, | ||
}; | ||
use crate::utils::query_finality_provider; | ||
|
||
use babylon_apis::finality_api::PubRandCommit; | ||
use babylon_merkle::Proof; | ||
use cosmwasm_std::{Deps, DepsMut, Env, Event, Response}; | ||
use cosmwasm_std::{Deps, DepsMut, Env, Event, MessageInfo, Response}; | ||
use k256::ecdsa::signature::Verifier; | ||
use k256::schnorr::{Signature, VerifyingKey}; | ||
use k256::sha2::{Digest, Sha256}; | ||
|
||
use super::admin::check_admin; | ||
|
||
// Most logic copied from contracts/btc-staking/src/finality.rs | ||
pub fn handle_public_randomness_commit( | ||
deps: DepsMut, | ||
|
@@ -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. | ||
pub fn whitelist_forked_blocks( | ||
deps: DepsMut, | ||
info: MessageInfo, | ||
forked_blocks: Vec<(u64, u64)>, | ||
) -> Result<Response, ContractError> { | ||
// Check caller is admin | ||
check_admin(&deps, info)?; | ||
|
||
// Check array is non-empty | ||
if forked_blocks.is_empty() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
return Err(ContractError::EmptyBlockRange); | ||
} | ||
|
||
// Append blocks to whitelist | ||
FORKED_BLOCKS.update::<_, ContractError>(deps.storage, |mut blocks| { | ||
blocks.extend(forked_blocks); | ||
Ok(blocks) | ||
})?; | ||
Comment on lines
+324
to
+327
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we want to allow overalp? e.g. should we enforce
so it can only be e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
Ok(Response::new()) | ||
} | ||
|
||
/// `msg_to_sign` returns the message for an EOTS signature. | ||
/// | ||
/// The EOTS signature on a block will be (block_height || block_hash) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
use crate::error::ContractError; | ||
use crate::state::config::{Config, ADMIN, CONFIG, IS_ENABLED}; | ||
use crate::state::finality::BLOCK_VOTES; | ||
use crate::state::finality::{BLOCK_VOTES, FORKED_BLOCKS}; | ||
use crate::state::public_randomness::get_pub_rand_commit; | ||
use babylon_apis::finality_api::PubRandCommit; | ||
use cosmwasm_std::{Deps, StdResult, Storage}; | ||
use cw_controllers::AdminResponse; | ||
use std::cmp::{max, min}; | ||
use std::collections::HashSet; | ||
|
||
pub fn query_config(deps: Deps) -> StdResult<Config> { | ||
|
@@ -46,6 +47,51 @@ pub fn query_last_pub_rand_commit( | |
Ok(res.into_iter().next()) | ||
} | ||
|
||
pub fn query_forked_blocks(deps: Deps) -> StdResult<Vec<(u64, u64)>> { | ||
FORKED_BLOCKS.load(deps.storage) | ||
} | ||
|
||
pub fn query_is_block_forked(deps: Deps, height: u64) -> StdResult<bool> { | ||
// loop over forked blocks, starting from last entry | ||
let forked_blocks = FORKED_BLOCKS.load(deps.storage)?; | ||
for (start, end) in forked_blocks.iter().rev() { | ||
// if block is in range of any forked block, return true | ||
if height >= *start && height <= *end { | ||
return Ok(true); | ||
} | ||
// terminate early once we reach a forked block range lower than the queried block | ||
if height < *start { | ||
break; | ||
} | ||
Comment on lines
+63
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't understand the logic here. shouldn't we have a lets say we have [(1,5), (8,13)] and height is 3 the function will return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
Ok(false) | ||
} | ||
|
||
pub fn query_forked_blocks_in_range( | ||
deps: Deps, | ||
start: u64, | ||
end: u64, | ||
) -> StdResult<Vec<(u64, u64)>> { | ||
// loop over forked blocks, starting from last entry | ||
let mut block_ranges = Vec::new(); | ||
let forked_blocks = FORKED_BLOCKS.load(deps.storage)?; | ||
for (fork_start, fork_end) in forked_blocks.iter().rev() { | ||
// append any overlapping forked block ranges to the list, moving cursor forward | ||
let overlap_start = max(*fork_start, start); | ||
let overlap_end = min(*fork_end, end); | ||
if overlap_start <= overlap_end { | ||
block_ranges.push((overlap_start, overlap_end)); | ||
} | ||
// terminate early once we reach a forked block range lower than the queried block | ||
if *fork_start < start { | ||
break; | ||
} | ||
} | ||
// reverse block ranges so they are ordered from highest to lowest | ||
block_ranges.reverse(); | ||
Ok(block_ranges) | ||
} | ||
|
||
pub fn query_is_enabled(deps: Deps) -> StdResult<bool> { | ||
IS_ENABLED.load(deps.storage) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
use cw_storage_plus::Map; | ||
use cw_storage_plus::{Item, Map}; | ||
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually there is a much simpler way: given a height, use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
during consecutive