Skip to content

refactor!: Implement generics for CheckPoint, LocalChain, and spk_client types #1582

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions crates/bitcoind_rpc/src/bip158.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct FilterIter<'c, C> {
// SPK inventory
spks: Vec<ScriptBuf>,
// local cp
cp: Option<CheckPoint>,
cp: Option<CheckPoint<BlockHash>>,
// blocks map
blocks: BTreeMap<Height, BlockHash>,
// best height counter
Expand All @@ -53,7 +53,7 @@ impl<'c, C: RpcApi> FilterIter<'c, C> {
}

/// Construct [`FilterIter`] from a given `client` and [`CheckPoint`].
pub fn new_with_checkpoint(client: &'c C, cp: CheckPoint) -> Self {
pub fn new_with_checkpoint(client: &'c C, cp: CheckPoint<BlockHash>) -> Self {
let mut filter_iter = Self::new_with_height(client, cp.height());
filter_iter.cp = Some(cp);
filter_iter
Expand Down Expand Up @@ -199,7 +199,7 @@ impl<C: RpcApi> Iterator for FilterIter<'_, C> {

impl<C: RpcApi> FilterIter<'_, C> {
/// Returns the point of agreement between `self` and the given `cp`.
fn find_base_with(&mut self, mut cp: CheckPoint) -> Result<BlockId, Error> {
fn find_base_with(&mut self, mut cp: CheckPoint<BlockHash>) -> Result<BlockId, Error> {
loop {
let height = cp.height();
let fetched_hash = match self.blocks.get(&height) {
Expand All @@ -222,7 +222,7 @@ impl<C: RpcApi> FilterIter<'_, C> {
///
/// Returns `None` if this [`FilterIter`] was not constructed using a [`CheckPoint`], or
/// if no blocks have been fetched for example by using [`get_tip`](Self::get_tip).
pub fn chain_update(&mut self) -> Option<CheckPoint> {
pub fn chain_update(&mut self) -> Option<CheckPoint<BlockHash>> {
if self.cp.is_none() || self.blocks.is_empty() {
return None;
}
Expand Down
12 changes: 6 additions & 6 deletions crates/bitcoind_rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Emitter<C> {

/// The checkpoint of the last-emitted block that is in the best chain. If it is later found
/// that the block is no longer in the best chain, it will be popped off from here.
last_cp: CheckPoint,
last_cp: CheckPoint<BlockHash>,

/// The block result returned from rpc of the last-emitted block. As this result contains the
/// next block's block hash (which we use to fetch the next block), we set this to `None`
Expand Down Expand Up @@ -79,7 +79,7 @@ where
/// known that the wallet is empty, [`NO_EXPECTED_MEMPOOL_TXIDS`] can be used.
pub fn new(
client: C,
last_cp: CheckPoint,
last_cp: CheckPoint<BlockHash>,
start_height: u32,
expected_mempool_txids: impl IntoIterator<Item = impl Into<Txid>>,
) -> Self {
Expand Down Expand Up @@ -268,7 +268,7 @@ pub struct BlockEvent<B> {
///
/// This is important as BDK structures require block-to-apply to be connected with another
/// block in the original chain.
pub checkpoint: CheckPoint,
pub checkpoint: CheckPoint<BlockHash>,
}

impl<B> BlockEvent<B> {
Expand Down Expand Up @@ -302,7 +302,7 @@ enum PollResponse {
NoMoreBlocks,
/// Fetched block is not in the best chain.
BlockNotInBestChain,
AgreementFound(bitcoincore_rpc_json::GetBlockResult, CheckPoint),
AgreementFound(bitcoincore_rpc_json::GetBlockResult, CheckPoint<BlockHash>),
/// Force the genesis checkpoint down the receiver's throat.
AgreementPointNotFound(BlockHash),
}
Expand Down Expand Up @@ -364,7 +364,7 @@ where
fn poll<C, V, F>(
emitter: &mut Emitter<C>,
get_item: F,
) -> Result<Option<(CheckPoint, V)>, bitcoincore_rpc::Error>
) -> Result<Option<(CheckPoint<BlockHash>, V)>, bitcoincore_rpc::Error>
where
C: Deref,
C::Target: RpcApi,
Expand All @@ -380,7 +380,7 @@ where
let new_cp = emitter
.last_cp
.clone()
.push(BlockId { height, hash })
.push_block_id(BlockId { height, hash })
.expect("must push");
emitter.last_cp = new_cp.clone();
emitter.last_block = Some(res);
Expand Down
10 changes: 8 additions & 2 deletions crates/bitcoind_rpc/tests/test_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,10 @@ fn process_block(
block: Block,
block_height: u32,
) -> anyhow::Result<()> {
recv_chain.apply_update(CheckPoint::from_header(&block.header, block_height))?;
recv_chain.apply_update(CheckPoint::blockhash_checkpoint_from_header(
&block.header,
block_height,
))?;
let _ = recv_graph.apply_block(block, block_height);
Ok(())
}
Expand Down Expand Up @@ -808,7 +811,10 @@ fn test_expect_tx_evicted() -> anyhow::Result<()> {
let mut emitter = Emitter::new(env.rpc_client(), chain.tip(), 1, HashSet::from([txid_1]));
while let Some(emission) = emitter.next_block()? {
let height = emission.block_height();
chain.apply_update(CheckPoint::from_header(&emission.block.header, height))?;
chain.apply_update(CheckPoint::blockhash_checkpoint_from_header(
&emission.block.header,
height,
))?;
}

let changeset = graph.batch_insert_unconfirmed(emitter.mempool()?.new_txs);
Expand Down
Loading
Loading