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

refactor: rewrite BHW as a state machine #5515

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions engine/src/witness/btc_e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use cf_utilities::task_scope::{self, Scope};
use futures::FutureExt;
use pallet_cf_elections::{
electoral_system::ElectoralSystem,
electoral_systems::block_height_tracking::{primitives::Header, BlockHeightTrackingProperties},
electoral_systems::block_height_tracking::{
primitives::Header, BlockHeightTrackingProperties, InputHeaders,
},
vote_storage::VoteStorage,
};
use sp_core::bounded::alloc::collections::VecDeque;
Expand Down Expand Up @@ -132,7 +134,7 @@ impl VoterApi<BitcoinBlockHeightTracking> for BitcoinBlockHeightTrackingVoter {
Err(anyhow::anyhow!("btc: no new blocks found since best block height is {} for witness_from={witness_from_index}", best_block_header.block_height))
} else if witness_from_index == 0 {
headers.push_back(best_block_header);
Ok(headers)
Ok(InputHeaders(headers))
} else {
// fetch the headers we haven't got yet
for index in witness_from_index..best_block_header.block_height {
Expand All @@ -154,7 +156,7 @@ impl VoterApi<BitcoinBlockHeightTracking> for BitcoinBlockHeightTrackingVoter {
"bht: Submitting vote for (witness_from={witness_from_index})with {} headers",
headers.len()
);
Ok(headers)
Ok(InputHeaders(headers))
} else {
Err(anyhow::anyhow!("bht: Headers do not form a chain"))
}
Expand Down
62 changes: 61 additions & 1 deletion state-chain/chains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ pub mod instances;
pub mod mocks;

pub mod witness_period {
use core::ops::{Rem, Sub};
use core::{
iter::Step,
ops::{Rem, Sub},
};

use sp_runtime::traits::Block;
use sp_std::ops::RangeInclusive;
Expand Down Expand Up @@ -163,6 +166,63 @@ pub mod witness_period {
let floored_block_number = block_witness_floor(witness_period, block_number);
floored_block_number..=floored_block_number.saturating_add(witness_period - One::one())
}

impl<I: PartialOrd + Clone + Saturating> Step for BlockWitnessRange<I> {
fn steps_between(start: &Self, end: &Self) -> (usize, Option<usize>) {
// assert_eq!(start.period, end.period);
todo!()
}

fn forward_checked(mut start: Self, count: usize) -> Option<Self> {
(0..count)
.for_each(|_| start.root = start.root.clone().saturating_add(start.period.clone()));
Some(start)
}

fn backward_checked(mut start: Self, count: usize) -> Option<Self> {
(0..count)
.for_each(|_| start.root = start.root.clone().saturating_sub(start.period.clone()));
Some(start)
}
}

pub trait BlockZero {
fn zero() -> Self;
fn is_zero(&self) -> bool;
}

impl<I: BlockZero + From<u32>> BlockZero for BlockWitnessRange<I> {
fn zero() -> Self {
Self {
root: I::zero(),
period: 1.into(), // NOTE: of course this is horribly wrong
}
}

fn is_zero(&self) -> bool {
self.root.is_zero()
}
}

impl BlockZero for u64 {
fn zero() -> Self {
0
}

fn is_zero(&self) -> bool {
*self == 0
}
}

impl BlockZero for u32 {
fn zero() -> Self {
0
}

fn is_zero(&self) -> bool {
*self == 0
}
}
}

/// A trait representing all the types and constants that need to be implemented for supported
Expand Down
2 changes: 2 additions & 0 deletions state-chain/pallets/cf-elections/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ sp-std = { workspace = true }
[dev-dependencies]
cf-test-utilities = { workspace = true, default-features = true }
rand = { workspace = true, features = ["std"] }
proptest = { version = "1.6"}
proptest-derive = { version = "0.5.1" }

[features]
default = ["std"]
Expand Down
Loading