Skip to content

Commit b5173b1

Browse files
committed
Merge #2150: chain 0.23.x: Fix assumed canonical tx always being unconfirmed
43850b5 fix(chain): Tx assumed to be canonical will not be forced unconfimred (志宇) Pull request description: ### Description Fixes #2088 Depends on #2148 Backport of #2110 ### Changelog notice ```md Fixed: - Previously, assumed-canonical transactions always became unconfirmed even though the transaction may be anchored in the best chain. ``` ### Checklists #### All Submissions: * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) #### New Features: * [x] I've added tests for the new feature * [x] I've added docs for the new feature #### Bugfixes: * [x] I've added tests to reproduce the issue which are now passing * [x] I'm linking the issue being fixed by this PR ACKs for top commit: luisschwab: ACK 43850b5 notmandatory: ACK 43850b5 Tree-SHA512: 72fa42111a470e4ab52b92213b54d5f6a1521e4994258a41764b18c141fcbc47d4ff58d9e5cdb5cd4dfae151e80e26b64eefc8e735e6c71768e114cefcc3ef8c
2 parents 2e4ff71 + 43850b5 commit b5173b1

2 files changed

Lines changed: 56 additions & 8 deletions

File tree

crates/chain/src/tx_graph.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,8 +1043,8 @@ impl<A: Anchor> TxGraph<A> {
10431043
res.map(|(txid, _, canonical_reason)| {
10441044
let tx_node = self.get_tx_node(txid).expect("must contain tx");
10451045
let chain_position = match canonical_reason {
1046-
CanonicalReason::Assumed { descendant } => match descendant {
1047-
Some(_) => match find_direct_anchor(&tx_node, chain, chain_tip)? {
1046+
CanonicalReason::Assumed { .. } => {
1047+
match find_direct_anchor(&tx_node, chain, chain_tip)? {
10481048
Some(anchor) => ChainPosition::Confirmed {
10491049
anchor,
10501050
transitively: None,
@@ -1053,12 +1053,8 @@ impl<A: Anchor> TxGraph<A> {
10531053
first_seen: tx_node.first_seen,
10541054
last_seen: tx_node.last_seen,
10551055
},
1056-
},
1057-
None => ChainPosition::Unconfirmed {
1058-
first_seen: tx_node.first_seen,
1059-
last_seen: tx_node.last_seen,
1060-
},
1061-
},
1056+
}
1057+
}
10621058
CanonicalReason::Anchor { anchor, descendant } => match descendant {
10631059
Some(_) => match find_direct_anchor(&tx_node, chain, chain_tip)? {
10641060
Some(anchor) => ChainPosition::Confirmed {

crates/chain/tests/test_tx_graph.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,58 @@ fn test_get_first_seen_of_a_tx() {
15451545
assert_eq!(first_seen, Some(seen_at));
15461546
}
15471547

1548+
#[test]
1549+
fn test_assumed_canonical_with_anchor_is_confirmed() {
1550+
use bdk_chain::ChainPosition;
1551+
1552+
let chain = LocalChain::from_blocks(
1553+
[(0, hash!("genesis")), (2, hash!("b2"))]
1554+
.into_iter()
1555+
.collect(),
1556+
)
1557+
.unwrap();
1558+
1559+
// Create an anchored transaction that will be assumed canonical via `CanonicalizationParams`.
1560+
let tx = Transaction {
1561+
input: vec![TxIn {
1562+
previous_output: OutPoint::new(hash!("parent"), 0),
1563+
..Default::default()
1564+
}],
1565+
output: vec![TxOut {
1566+
value: Amount::from_sat(50_000),
1567+
script_pubkey: ScriptBuf::new(),
1568+
}],
1569+
..new_tx(1)
1570+
};
1571+
let txid = tx.compute_txid();
1572+
1573+
let mut tx_graph = TxGraph::default();
1574+
let _ = tx_graph.insert_tx(tx);
1575+
let _ = tx_graph.insert_anchor(
1576+
txid,
1577+
ConfirmationBlockTime {
1578+
block_id: chain.get(2).unwrap().block_id(),
1579+
confirmation_time: 123456,
1580+
},
1581+
);
1582+
1583+
let canonical_tx = tx_graph
1584+
.list_canonical_txs(
1585+
&chain,
1586+
chain.tip().block_id(),
1587+
CanonicalizationParams {
1588+
assume_canonical: vec![txid],
1589+
},
1590+
)
1591+
.find(|c_tx| c_tx.tx_node.txid == txid)
1592+
.expect("tx must exist");
1593+
1594+
assert!(
1595+
matches!(canonical_tx.chain_position, ChainPosition::Confirmed { .. }),
1596+
"tx that is assumed canonical and has a direct anchor should have ChainPosition::Confirmed"
1597+
);
1598+
}
1599+
15481600
/// A helper structure to constructs multiple [`TxGraph`] scenarios, used in
15491601
/// `test_list_ordered_canonical_txs`.
15501602
struct Scenario<'a> {

0 commit comments

Comments
 (0)