Skip to content

Commit

Permalink
refactor: set concrete type BlockId instead of generic `B: Into<Blo…
Browse files Browse the repository at this point in the history
…ckId>` for `chain_tip`
  • Loading branch information
SpaghettiSats committed Sep 14, 2023
1 parent 0eb0e1b commit be86650
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 104 deletions.
34 changes: 9 additions & 25 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,8 @@ impl<D> Wallet<D> {
pub fn list_unspent(&self) -> impl Iterator<Item = LocalUtxo> + '_ {
self.indexed_graph
.graph()
.filter_subchain_unspents(
.filter_chain_unspents(
&self.chain,
self.chain.tip().map(|cp| cp.block_id()),
self.indexed_graph.index.outpoints().iter().cloned(),
)
.map(|((k, i), full_txo)| new_local_utxo(k, i, full_txo))
Expand Down Expand Up @@ -422,11 +421,7 @@ impl<D> Wallet<D> {
let (&spk_i, _) = self.indexed_graph.index.txout(op)?;
self.indexed_graph
.graph()
.filter_subchain_unspents(
&self.chain,
self.chain.tip().map(|cp| cp.block_id()),
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 @@ -595,11 +590,7 @@ impl<D> Wallet<D> {
let graph = self.indexed_graph.graph();

Some(CanonicalTx {
chain_position: graph.get_subchain_position(
&self.chain,
self.chain.tip().map(|cp| cp.block_id()),
txid,
)?,
chain_position: graph.get_chain_position(&self.chain, txid)?,
tx_node: graph.get_tx_node(txid)?,
})
}
Expand Down Expand Up @@ -685,17 +676,14 @@ impl<D> Wallet<D> {
pub fn transactions(
&self,
) -> impl Iterator<Item = CanonicalTx<'_, Transaction, ConfirmationTimeAnchor>> + '_ {
self.indexed_graph
.graph()
.list_subchain_txs(&self.chain, self.chain.tip().map(|cp| cp.block_id()))
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().subchain_balance(
self.indexed_graph.graph().balance(
&self.chain,
self.chain.tip().map(|cp| cp.block_id()),
self.indexed_graph.index.outpoints().iter().cloned(),
|&(k, _), _| k == KeychainKind::Internal,
)
Expand Down Expand Up @@ -1175,15 +1163,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());

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

let pos = graph
.get_subchain_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 @@ -1215,7 +1202,7 @@ impl<D> Wallet<D> {
let txout = &prev_tx.output[txin.previous_output.vout as usize];

let confirmation_time: ConfirmationTime = graph
.get_subchain_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 @@ -1410,8 +1397,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());

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

Expand All @@ -1426,7 +1411,7 @@ impl<D> Wallet<D> {
let confirmation_height = self
.indexed_graph
.graph()
.get_subchain_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 @@ -1579,7 +1564,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());
// must_spend <- manually selected utxos
// may_spend <- all other available utxos
let mut may_spend = self.get_available_utxos();
Expand Down Expand Up @@ -1608,7 +1592,7 @@ impl<D> Wallet<D> {
let confirmation_time: ConfirmationTime = match self
.indexed_graph
.graph()
.get_subchain_position(&self.chain, chain_tip, txid)
.get_chain_position(&self.chain, txid)
{
Some(chain_position) => chain_position.cloned().into(),
None => return false,
Expand Down
4 changes: 2 additions & 2 deletions crates/chain/src/chain_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ pub trait ChainOracle {
///
/// If `None` is returned, it means the implementation cannot determine whether `block` exists
/// under `chain_tip`.
fn is_block_in_chain<B: Into<BlockId>>(
fn is_block_in_chain(
&self,
block: BlockId,
chain_tip: Option<B>,
chain_tip: Option<&BlockId>,
) -> Result<Option<bool>, Self::Error>;

/// Get the best chain's chain tip.
Expand Down
8 changes: 4 additions & 4 deletions crates/chain/src/local_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ impl From<BTreeMap<u32, BlockHash>> for LocalChain {
impl ChainOracle for LocalChain {
type Error = Infallible;

fn is_block_in_chain<B: Into<BlockId>>(
fn is_block_in_chain(
&self,
block: BlockId,
chain_tip: Option<B>,
chain_tip: Option<&BlockId>,
) -> Result<Option<bool>, Self::Error> {
let chain_tip: BlockId = match chain_tip {
Some(x) => x.into(),
let chain_tip = match chain_tip {
Some(x) => x,
None => return Ok(None),
};

Expand Down
Loading

0 comments on commit be86650

Please sign in to comment.