Skip to content

Commit ccb8c79

Browse files
committed
test(chain): TxGraph and tx_graph::Update conversion tests
1 parent 60b14f0 commit ccb8c79

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

crates/chain/tests/test_tx_graph.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,3 +1136,130 @@ fn call_map_anchors_with_non_deterministic_anchor() {
11361136
]
11371137
);
11381138
}
1139+
1140+
/// Tests `From` impls for conversion between [`TxGraph`] and [`tx_graph::Update`].
1141+
#[test]
1142+
fn tx_graph_update_conversion() {
1143+
use tx_graph::Update;
1144+
1145+
type TestCase = (&'static str, Update<ConfirmationBlockTime>);
1146+
1147+
fn make_tx(v: i32) -> Transaction {
1148+
Transaction {
1149+
version: transaction::Version(v),
1150+
lock_time: absolute::LockTime::ZERO,
1151+
input: vec![],
1152+
output: vec![],
1153+
}
1154+
}
1155+
1156+
fn make_txout(a: u64) -> TxOut {
1157+
TxOut {
1158+
value: Amount::from_sat(a),
1159+
script_pubkey: ScriptBuf::default(),
1160+
}
1161+
}
1162+
1163+
let test_cases: &[TestCase] = &[
1164+
("empty_update", Update::default()),
1165+
(
1166+
"single_tx",
1167+
Update {
1168+
txs: vec![make_tx(0).into()],
1169+
..Default::default()
1170+
},
1171+
),
1172+
(
1173+
"two_txs",
1174+
Update {
1175+
txs: vec![make_tx(0).into(), make_tx(1).into()],
1176+
..Default::default()
1177+
},
1178+
),
1179+
(
1180+
"with_floating_txouts",
1181+
Update {
1182+
txs: vec![make_tx(0).into(), make_tx(1).into()],
1183+
txouts: [
1184+
(OutPoint::new(h!("a"), 0), make_txout(0)),
1185+
(OutPoint::new(h!("a"), 1), make_txout(1)),
1186+
(OutPoint::new(h!("b"), 0), make_txout(2)),
1187+
]
1188+
.into(),
1189+
..Default::default()
1190+
},
1191+
),
1192+
(
1193+
"with_anchors",
1194+
Update {
1195+
txs: vec![make_tx(0).into(), make_tx(1).into()],
1196+
txouts: [
1197+
(OutPoint::new(h!("a"), 0), make_txout(0)),
1198+
(OutPoint::new(h!("a"), 1), make_txout(1)),
1199+
(OutPoint::new(h!("b"), 0), make_txout(2)),
1200+
]
1201+
.into(),
1202+
anchors: [
1203+
(ConfirmationBlockTime::default(), h!("a")),
1204+
(ConfirmationBlockTime::default(), h!("b")),
1205+
]
1206+
.into(),
1207+
..Default::default()
1208+
},
1209+
),
1210+
(
1211+
"with_seen_ats",
1212+
Update {
1213+
txs: vec![make_tx(0).into(), make_tx(1).into()],
1214+
txouts: [
1215+
(OutPoint::new(h!("a"), 0), make_txout(0)),
1216+
(OutPoint::new(h!("a"), 1), make_txout(1)),
1217+
(OutPoint::new(h!("d"), 0), make_txout(2)),
1218+
]
1219+
.into(),
1220+
anchors: [
1221+
(ConfirmationBlockTime::default(), h!("a")),
1222+
(ConfirmationBlockTime::default(), h!("b")),
1223+
]
1224+
.into(),
1225+
seen_ats: [(h!("c"), 12346)].into_iter().collect(),
1226+
},
1227+
),
1228+
];
1229+
1230+
for (test_name, update) in test_cases {
1231+
let mut tx_graph = TxGraph::<ConfirmationBlockTime>::default();
1232+
let _ = tx_graph.apply_update_at(update.clone(), None);
1233+
let update_from_tx_graph: Update<ConfirmationBlockTime> = tx_graph.into();
1234+
1235+
assert_eq!(
1236+
update
1237+
.txs
1238+
.iter()
1239+
.map(|tx| tx.compute_txid())
1240+
.collect::<HashSet<Txid>>(),
1241+
update_from_tx_graph
1242+
.txs
1243+
.iter()
1244+
.map(|tx| tx.compute_txid())
1245+
.collect::<HashSet<Txid>>(),
1246+
"{}: txs do not match",
1247+
test_name
1248+
);
1249+
assert_eq!(
1250+
update.txouts, update_from_tx_graph.txouts,
1251+
"{}: txouts do not match",
1252+
test_name
1253+
);
1254+
assert_eq!(
1255+
update.anchors, update_from_tx_graph.anchors,
1256+
"{}: anchors do not match",
1257+
test_name
1258+
);
1259+
assert_eq!(
1260+
update.seen_ats, update_from_tx_graph.seen_ats,
1261+
"{}: seen_ats do not match",
1262+
test_name
1263+
);
1264+
}
1265+
}

0 commit comments

Comments
 (0)