Skip to content

Commit

Permalink
bitcoin: return spent block height & time separately
Browse files Browse the repository at this point in the history
  • Loading branch information
jp1ac4 committed Aug 8, 2024
1 parent dd11ed3 commit 36fbeeb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
22 changes: 8 additions & 14 deletions src/bitcoin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use std::{fmt, sync};

use miniscript::bitcoin::{self, address, bip32::ChildNumber};

// A spent coin's outpoint together with its spend transaction's txid, height and time.
type SpentCoin = (bitcoin::OutPoint, bitcoin::Txid, i32, u32);

const COINBASE_MATURITY: i32 = 100;

/// Information about a block
Expand Down Expand Up @@ -93,10 +96,7 @@ pub trait BitcoinInterface: Send {
fn spent_coins(
&self,
outpoints: &[(bitcoin::OutPoint, bitcoin::Txid)],
) -> (
Vec<(bitcoin::OutPoint, bitcoin::Txid, Block)>,
Vec<bitcoin::OutPoint>,
);
) -> (Vec<SpentCoin>, Vec<bitcoin::OutPoint>);

/// Get the common ancestor between the Bitcoin backend's tip and the given tip.
fn common_ancestor(&self, tip: &BlockChainTip) -> Option<BlockChainTip>;
Expand Down Expand Up @@ -279,10 +279,7 @@ impl BitcoinInterface for d::BitcoinD {
fn spent_coins(
&self,
outpoints: &[(bitcoin::OutPoint, bitcoin::Txid)],
) -> (
Vec<(bitcoin::OutPoint, bitcoin::Txid, Block)>,
Vec<bitcoin::OutPoint>,
) {
) -> (Vec<SpentCoin>, Vec<bitcoin::OutPoint>) {
// Spend coins to be returned.
let mut spent = Vec::with_capacity(outpoints.len());
// Coins whose spending transaction isn't in our local mempool anymore.
Expand All @@ -300,7 +297,7 @@ impl BitcoinInterface for d::BitcoinD {

// If the transaction was confirmed, mark it as such.
if let Some(block) = res.block {
spent.push((*op, *txid, block));
spent.push((*op, *txid, block.height, block.time));
continue;
}

Expand All @@ -323,7 +320,7 @@ impl BitcoinInterface for d::BitcoinD {
})
});
if let Some((txid, block)) = conflict {
spent.push((*op, txid, block));
spent.push((*op, txid, block.height, block.time));
continue;
}

Expand Down Expand Up @@ -463,10 +460,7 @@ impl BitcoinInterface for sync::Arc<sync::Mutex<dyn BitcoinInterface + 'static>>
fn spent_coins(
&self,
outpoints: &[(bitcoin::OutPoint, bitcoin::Txid)],
) -> (
Vec<(bitcoin::OutPoint, bitcoin::Txid, Block)>,
Vec<bitcoin::OutPoint>,
) {
) -> (Vec<SpentCoin>, Vec<bitcoin::OutPoint>) {
self.lock().unwrap().spent_coins(outpoints)
}

Expand Down
4 changes: 0 additions & 4 deletions src/bitcoin/poller/looper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ fn update_coins(
.chain(spending.iter().cloned())
.collect();
let (spent, expired_spending) = bit.spent_coins(spending_coins.as_slice());
let spent = spent
.into_iter()
.map(|(oupoint, txid, block)| (oupoint, txid, block.height, block.time))
.collect();
log::debug!("Newly spent coins: {:?}", spent);

UpdatedCoins {
Expand Down
2 changes: 1 addition & 1 deletion src/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl BitcoinInterface for DummyBitcoind {
&self,
_: &[(bitcoin::OutPoint, bitcoin::Txid)],
) -> (
Vec<(bitcoin::OutPoint, bitcoin::Txid, Block)>,
Vec<(bitcoin::OutPoint, bitcoin::Txid, i32, u32)>,
Vec<bitcoin::OutPoint>,
) {
(Vec::new(), Vec::new())
Expand Down

0 comments on commit 36fbeeb

Please sign in to comment.