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

BlockId should not have a default and passing a chain tip should be an Option #1107 #1116

Closed
wants to merge 5 commits into from
Closed
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
31 changes: 7 additions & 24 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ impl<D> Wallet<D> {
.graph()
.filter_chain_unspents(
&self.chain,
self.chain.tip().map(|cp| cp.block_id()).unwrap_or_default(),
self.indexed_graph.index.outpoints().iter().cloned(),
)
.map(|((k, i), full_txo)| new_local_utxo(k, i, full_txo))
Expand Down Expand Up @@ -485,11 +484,7 @@ impl<D> Wallet<D> {
let (&spk_i, _) = self.indexed_graph.index.txout(op)?;
self.indexed_graph
.graph()
.filter_chain_unspents(
&self.chain,
self.chain.tip().map(|cp| cp.block_id()).unwrap_or_default(),
core::iter::once((spk_i, op)),
)
.filter_chain_unspents(&self.chain, core::iter::once((spk_i, op)))
.map(|((k, i), full_txo)| new_local_utxo(k, i, full_txo))
.next()
}
Expand Down Expand Up @@ -658,11 +653,7 @@ impl<D> Wallet<D> {
let graph = self.indexed_graph.graph();

Some(CanonicalTx {
chain_position: graph.get_chain_position(
&self.chain,
self.chain.tip().map(|cp| cp.block_id()).unwrap_or_default(),
txid,
)?,
chain_position: graph.get_chain_position(&self.chain, txid)?,
tx_node: graph.get_tx_node(txid)?,
})
}
Expand Down Expand Up @@ -748,18 +739,14 @@ impl<D> Wallet<D> {
pub fn transactions(
&self,
) -> impl Iterator<Item = CanonicalTx<'_, Transaction, ConfirmationTimeAnchor>> + '_ {
self.indexed_graph.graph().list_chain_txs(
&self.chain,
self.chain.tip().map(|cp| cp.block_id()).unwrap_or_default(),
)
self.indexed_graph.graph().list_chain_txs(&self.chain)
}

/// Return the balance, separated into available, trusted-pending, untrusted-pending and immature
/// values.
pub fn get_balance(&self) -> Balance {
self.indexed_graph.graph().balance(
&self.chain,
self.chain.tip().map(|cp| cp.block_id()).unwrap_or_default(),
self.indexed_graph.index.outpoints().iter().cloned(),
|&(k, _), _| k == KeychainKind::Internal,
)
Expand Down Expand Up @@ -1239,15 +1226,14 @@ impl<D> Wallet<D> {
) -> Result<TxBuilder<'_, D, DefaultCoinSelectionAlgorithm, BumpFee>, Error> {
let graph = self.indexed_graph.graph();
let txout_index = &self.indexed_graph.index;
let chain_tip = self.chain.tip().map(|cp| cp.block_id()).unwrap_or_default();

let mut tx = graph
.get_tx(txid)
.ok_or(Error::TransactionNotFound)?
.clone();

let pos = graph
.get_chain_position(&self.chain, chain_tip, txid)
.get_chain_position(&self.chain, txid)
.ok_or(Error::TransactionNotFound)?;
if let ChainPosition::Confirmed(_) = pos {
return Err(Error::TransactionConfirmed);
Expand Down Expand Up @@ -1279,7 +1265,7 @@ impl<D> Wallet<D> {
let txout = &prev_tx.output[txin.previous_output.vout as usize];

let confirmation_time: ConfirmationTime = graph
.get_chain_position(&self.chain, chain_tip, txin.previous_output.txid)
.get_chain_position(&self.chain, txin.previous_output.txid)
.ok_or(Error::UnknownUtxo)?
.cloned()
.into();
Expand Down Expand Up @@ -1474,8 +1460,6 @@ impl<D> Wallet<D> {
psbt: &mut psbt::PartiallySignedTransaction,
sign_options: SignOptions,
) -> Result<bool, Error> {
let chain_tip = self.chain.tip().map(|cp| cp.block_id()).unwrap_or_default();

let tx = &psbt.unsigned_tx;
let mut finished = true;

Expand All @@ -1490,7 +1474,7 @@ impl<D> Wallet<D> {
let confirmation_height = self
.indexed_graph
.graph()
.get_chain_position(&self.chain, chain_tip, input.previous_output.txid)
.get_chain_position(&self.chain, input.previous_output.txid)
.map(|chain_position| match chain_position {
ChainPosition::Confirmed(a) => a.confirmation_height,
ChainPosition::Unconfirmed(_) => u32::MAX,
Expand Down Expand Up @@ -1643,7 +1627,6 @@ impl<D> Wallet<D> {
must_only_use_confirmed_tx: bool,
current_height: Option<u32>,
) -> (Vec<WeightedUtxo>, Vec<WeightedUtxo>) {
let chain_tip = self.chain.tip().map(|cp| cp.block_id()).unwrap_or_default();
// must_spend <- manually selected utxos
// may_spend <- all other available utxos
let mut may_spend = self.get_available_utxos();
Expand Down Expand Up @@ -1672,7 +1655,7 @@ impl<D> Wallet<D> {
let confirmation_time: ConfirmationTime = match self
.indexed_graph
.graph()
.get_chain_position(&self.chain, chain_tip, txid)
.get_chain_position(&self.chain, txid)
{
Some(chain_position) => chain_position.cloned().into(),
None => return false,
Expand Down
15 changes: 3 additions & 12 deletions crates/chain/src/chain_data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bitcoin::{hashes::Hash, BlockHash, OutPoint, TxOut, Txid};
use bitcoin::{BlockHash, OutPoint, TxOut, Txid};

use crate::{Anchor, COINBASE_MATURITY};

Expand Down Expand Up @@ -109,15 +109,6 @@ impl Anchor for BlockId {
}
}

impl Default for BlockId {
fn default() -> Self {
Self {
height: Default::default(),
hash: BlockHash::all_zeros(),
}
}
}

impl From<(u32, BlockHash)> for BlockId {
fn from((height, hash): (u32, BlockHash)) -> Self {
Self { height, hash }
Expand All @@ -142,7 +133,7 @@ impl From<(&u32, &BlockHash)> for BlockId {
/// An [`Anchor`] implementation that also records the exact confirmation height of the transaction.
///
/// Refer to [`Anchor`] for more details.
#[derive(Debug, Default, Clone, PartialEq, Eq, Copy, PartialOrd, Ord, core::hash::Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Copy, PartialOrd, Ord, core::hash::Hash)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
Expand Down Expand Up @@ -172,7 +163,7 @@ impl Anchor for ConfirmationHeightAnchor {
/// transaction.
///
/// Refer to [`Anchor`] for more details.
#[derive(Debug, Default, Clone, PartialEq, Eq, Copy, PartialOrd, Ord, core::hash::Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Copy, PartialOrd, Ord, core::hash::Hash)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
Expand Down
Loading
Loading