Skip to content

Commit a80c26a

Browse files
committed
[wip] Port monitor_tests to P2A anchors
1 parent 7655dbf commit a80c26a

File tree

3 files changed

+198
-108
lines changed

3 files changed

+198
-108
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5800,6 +5800,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
58005800
assert_eq!(&bitcoin::Address::p2wsh(&ScriptBuf::from(input.witness.last().unwrap().to_vec()), bitcoin::Network::Bitcoin).script_pubkey(), _script_pubkey);
58015801
} else if _script_pubkey.is_p2wpkh() {
58025802
assert_eq!(&bitcoin::Address::p2wpkh(&bitcoin::CompressedPublicKey(bitcoin::PublicKey::from_slice(&input.witness.last().unwrap()).unwrap().inner), bitcoin::Network::Bitcoin).script_pubkey(), _script_pubkey);
5803+
} else if _script_pubkey == &chan_utils::shared_anchor_script_pubkey() {
5804+
assert!(input.witness.is_empty());
58035805
} else { panic!(); }
58045806
}
58055807
return true;

lightning/src/ln/functional_test_utils.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,11 +1941,15 @@ pub fn update_nodes_with_chan_announce<'a, 'b, 'c, 'd>(
19411941
pub fn do_check_spends<F: Fn(&bitcoin::transaction::OutPoint) -> Option<TxOut>>(
19421942
tx: &Transaction, get_output: F,
19431943
) {
1944+
let mut single_output_below_dust = false;
19441945
for outp in tx.output.iter() {
1945-
assert!(
1946-
outp.value >= outp.script_pubkey.minimal_non_dust(),
1947-
"Spending tx output didn't meet dust limit"
1948-
);
1946+
if outp.value < outp.script_pubkey.minimal_non_dust() {
1947+
if single_output_below_dust {
1948+
panic!("Spending tx output didn't meet dust limit");
1949+
} else {
1950+
single_output_below_dust = true;
1951+
}
1952+
};
19491953
}
19501954
let mut total_value_in = 0;
19511955
for input in tx.input.iter() {
@@ -1955,9 +1959,15 @@ pub fn do_check_spends<F: Fn(&bitcoin::transaction::OutPoint) -> Option<TxOut>>(
19551959
for output in tx.output.iter() {
19561960
total_value_out += output.value.to_sat();
19571961
}
1958-
let min_fee = (tx.weight().to_wu() as u64 + 3) / 4; // One sat per vbyte (ie per weight/4, rounded up)
1959-
// Input amount - output amount = fee, so check that out + min_fee is smaller than input
1960-
assert!(total_value_out + min_fee <= total_value_in);
1962+
if single_output_below_dust {
1963+
assert_eq!(
1964+
total_value_in, total_value_out,
1965+
"Spending tx has one output below dust, while not zero fee"
1966+
);
1967+
} else {
1968+
let min_fee = (tx.weight().to_wu() as u64 + 3) / 4; // One sat per vbyte (ie per weight/4, rounded up)
1969+
assert!(total_value_out + min_fee <= total_value_in);
1970+
}
19611971
tx.verify(get_output).unwrap();
19621972
}
19631973

@@ -1966,8 +1976,15 @@ macro_rules! check_spends {
19661976
($tx: expr, $($spends_txn: expr),*) => {
19671977
{
19681978
$(
1979+
let mut single_output_below_dust = false;
19691980
for outp in $spends_txn.output.iter() {
1970-
assert!(outp.value >= outp.script_pubkey.minimal_non_dust(), "Input tx output didn't meet dust limit");
1981+
if outp.value.lt(&outp.script_pubkey.minimal_non_dust()) {
1982+
if single_output_below_dust || outp.script_pubkey != $crate::ln::chan_utils::shared_anchor_script_pubkey() {
1983+
panic!("Input tx output didn't meet dust limit");
1984+
} else {
1985+
single_output_below_dust = true;
1986+
}
1987+
}
19711988
}
19721989
)*
19731990
let get_output = |out_point: &bitcoin::transaction::OutPoint| {
@@ -1980,7 +1997,10 @@ macro_rules! check_spends {
19801997
};
19811998
$crate::ln::functional_test_utils::do_check_spends(&$tx, get_output);
19821999
}
1983-
}
2000+
};
2001+
($tx: expr, $($spends_txn: expr),*) => {
2002+
check_spends!($tx, $($spends_txn; false),*)
2003+
};
19842004
}
19852005

19862006
macro_rules! get_closing_signed_broadcast {

0 commit comments

Comments
 (0)