Skip to content

Commit 2c96f00

Browse files
author
Antoine Riard
committed
Add onchain_utils
Onchain_utils aims to gather interfaces to communicate between onchain channel transactions parser (ChannelMonitor) and outputs claiming logic (OnchainTxHandler). These interfaces are data structures, generated per-case by ChannelMonitor and consumed blindly by OnchainTxHandler.
1 parent fa4ee8d commit 2c96f00

File tree

4 files changed

+102
-92
lines changed

4 files changed

+102
-92
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ use ln::msgs::DecodeError;
3131
use ln::chan_utils;
3232
use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, LocalCommitmentTransaction, HTLCType};
3333
use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
34-
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
34+
use ln::onchaintx::OnchainTxHandler;
35+
use ln::onchain_utils::InputDescriptors;
3536
use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface, FeeEstimator};
3637
use chain::transaction::OutPoint;
3738
use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys};
@@ -2538,7 +2539,8 @@ mod tests {
25382539
use chain::transaction::OutPoint;
25392540
use ln::channelmanager::{PaymentPreimage, PaymentHash};
25402541
use ln::channelmonitor::ChannelMonitor;
2541-
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
2542+
use ln::onchain_utils::InputDescriptors;
2543+
use ln::onchain_utils;
25422544
use ln::chan_utils;
25432545
use ln::chan_utils::{HTLCOutputInCommitment, LocalCommitmentTransaction};
25442546
use util::test_utils::TestLogger;
@@ -2731,7 +2733,7 @@ mod tests {
27312733
for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
27322734
sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
27332735
}
2734-
assert_eq!(base_weight + OnchainTxHandler::<InMemoryChannelKeys>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
2736+
assert_eq!(base_weight + onchain_utils::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
27352737

27362738
// Claim tx with 1 offered HTLCs, 3 received HTLCs
27372739
claim_tx.input.clear();
@@ -2753,7 +2755,7 @@ mod tests {
27532755
for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
27542756
sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
27552757
}
2756-
assert_eq!(base_weight + OnchainTxHandler::<InMemoryChannelKeys>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
2758+
assert_eq!(base_weight + onchain_utils::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
27572759

27582760
// Justice tx with 1 revoked HTLC-Success tx output
27592761
claim_tx.input.clear();
@@ -2773,7 +2775,7 @@ mod tests {
27732775
for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
27742776
sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
27752777
}
2776-
assert_eq!(base_weight + OnchainTxHandler::<InMemoryChannelKeys>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs));
2778+
assert_eq!(base_weight + onchain_utils::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs));
27772779
}
27782780

27792781
// Further testing is done in the ChannelManager integration tests.

lightning/src/ln/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod peer_channel_encryptor;
2323
pub(crate) mod peer_channel_encryptor;
2424

2525
mod channel;
26+
mod onchain_utils;
2627
mod onion_utils;
2728
mod wire;
2829

lightning/src/ln/onchain_utils.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//! Utilities for computing witnesses weight and feerate computation for onchain operation
2+
3+
use ln::msgs::DecodeError;
4+
use util::ser::{Readable, Writer, Writeable};
5+
6+
#[derive(PartialEq, Clone, Copy)]
7+
pub(crate) enum InputDescriptors {
8+
RevokedOfferedHTLC,
9+
RevokedReceivedHTLC,
10+
OfferedHTLC,
11+
ReceivedHTLC,
12+
RevokedOutput, // either a revoked to_local output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output
13+
}
14+
15+
impl Writeable for InputDescriptors {
16+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
17+
match self {
18+
&InputDescriptors::RevokedOfferedHTLC => {
19+
writer.write_all(&[0; 1])?;
20+
},
21+
&InputDescriptors::RevokedReceivedHTLC => {
22+
writer.write_all(&[1; 1])?;
23+
},
24+
&InputDescriptors::OfferedHTLC => {
25+
writer.write_all(&[2; 1])?;
26+
},
27+
&InputDescriptors::ReceivedHTLC => {
28+
writer.write_all(&[3; 1])?;
29+
}
30+
&InputDescriptors::RevokedOutput => {
31+
writer.write_all(&[4; 1])?;
32+
}
33+
}
34+
Ok(())
35+
}
36+
}
37+
38+
impl Readable for InputDescriptors {
39+
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
40+
let input_descriptor = match <u8 as Readable>::read(reader)? {
41+
0 => {
42+
InputDescriptors::RevokedOfferedHTLC
43+
},
44+
1 => {
45+
InputDescriptors::RevokedReceivedHTLC
46+
},
47+
2 => {
48+
InputDescriptors::OfferedHTLC
49+
},
50+
3 => {
51+
InputDescriptors::ReceivedHTLC
52+
},
53+
4 => {
54+
InputDescriptors::RevokedOutput
55+
}
56+
_ => return Err(DecodeError::InvalidValue),
57+
};
58+
Ok(input_descriptor)
59+
}
60+
}
61+
62+
pub(crate) fn get_witnesses_weight(inputs: &[InputDescriptors]) -> usize {
63+
let mut tx_weight = 2; // count segwit flags
64+
for inp in inputs {
65+
// We use expected weight (and not actual) as signatures and time lock delays may vary
66+
tx_weight += match inp {
67+
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
68+
&InputDescriptors::RevokedOfferedHTLC => {
69+
1 + 1 + 73 + 1 + 33 + 1 + 133
70+
},
71+
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
72+
&InputDescriptors::RevokedReceivedHTLC => {
73+
1 + 1 + 73 + 1 + 33 + 1 + 139
74+
},
75+
// number_of_witness_elements + sig_length + remotehtlc_sig + preimage_length + preimage + witness_script_length + witness_script
76+
&InputDescriptors::OfferedHTLC => {
77+
1 + 1 + 73 + 1 + 32 + 1 + 133
78+
},
79+
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
80+
&InputDescriptors::ReceivedHTLC => {
81+
1 + 1 + 73 + 1 + 1 + 1 + 139
82+
},
83+
// number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
84+
&InputDescriptors::RevokedOutput => {
85+
1 + 1 + 73 + 1 + 1 + 1 + 77
86+
},
87+
};
88+
}
89+
tx_weight
90+
}

lightning/src/ln/onchaintx.rs

Lines changed: 4 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use ln::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMateri
1717
use ln::channelmanager::PaymentPreimage;
1818
use ln::chan_utils;
1919
use ln::chan_utils::{TxCreationKeys, LocalCommitmentTransaction};
20+
use ln::onchain_utils::InputDescriptors;
21+
use ln::onchain_utils;
2022
use chain::chaininterface::{FeeEstimator, BroadcasterInterface, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
2123
use chain::keysinterface::ChannelKeys;
2224
use util::logger::Logger;
@@ -92,61 +94,6 @@ impl Readable for ClaimTxBumpMaterial {
9294
}
9395
}
9496

95-
#[derive(PartialEq, Clone, Copy)]
96-
pub(crate) enum InputDescriptors {
97-
RevokedOfferedHTLC,
98-
RevokedReceivedHTLC,
99-
OfferedHTLC,
100-
ReceivedHTLC,
101-
RevokedOutput, // either a revoked to_local output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output
102-
}
103-
104-
impl Writeable for InputDescriptors {
105-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
106-
match self {
107-
&InputDescriptors::RevokedOfferedHTLC => {
108-
writer.write_all(&[0; 1])?;
109-
},
110-
&InputDescriptors::RevokedReceivedHTLC => {
111-
writer.write_all(&[1; 1])?;
112-
},
113-
&InputDescriptors::OfferedHTLC => {
114-
writer.write_all(&[2; 1])?;
115-
},
116-
&InputDescriptors::ReceivedHTLC => {
117-
writer.write_all(&[3; 1])?;
118-
}
119-
&InputDescriptors::RevokedOutput => {
120-
writer.write_all(&[4; 1])?;
121-
}
122-
}
123-
Ok(())
124-
}
125-
}
126-
127-
impl Readable for InputDescriptors {
128-
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
129-
let input_descriptor = match <u8 as Readable>::read(reader)? {
130-
0 => {
131-
InputDescriptors::RevokedOfferedHTLC
132-
},
133-
1 => {
134-
InputDescriptors::RevokedReceivedHTLC
135-
},
136-
2 => {
137-
InputDescriptors::OfferedHTLC
138-
},
139-
3 => {
140-
InputDescriptors::ReceivedHTLC
141-
},
142-
4 => {
143-
InputDescriptors::RevokedOutput
144-
}
145-
_ => return Err(DecodeError::InvalidValue),
146-
};
147-
Ok(input_descriptor)
148-
}
149-
}
15097

15198
macro_rules! subtract_high_prio_fee {
15299
($logger: ident, $fee_estimator: expr, $value: expr, $predicted_weight: expr, $used_feerate: expr) => {
@@ -416,36 +363,6 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
416363
}
417364
}
418365

419-
pub(super) fn get_witnesses_weight(inputs: &[InputDescriptors]) -> usize {
420-
let mut tx_weight = 2; // count segwit flags
421-
for inp in inputs {
422-
// We use expected weight (and not actual) as signatures and time lock delays may vary
423-
tx_weight += match inp {
424-
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
425-
&InputDescriptors::RevokedOfferedHTLC => {
426-
1 + 1 + 73 + 1 + 33 + 1 + 133
427-
},
428-
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
429-
&InputDescriptors::RevokedReceivedHTLC => {
430-
1 + 1 + 73 + 1 + 33 + 1 + 139
431-
},
432-
// number_of_witness_elements + sig_length + remotehtlc_sig + preimage_length + preimage + witness_script_length + witness_script
433-
&InputDescriptors::OfferedHTLC => {
434-
1 + 1 + 73 + 1 + 32 + 1 + 133
435-
},
436-
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
437-
&InputDescriptors::ReceivedHTLC => {
438-
1 + 1 + 73 + 1 + 1 + 1 + 139
439-
},
440-
// number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
441-
&InputDescriptors::RevokedOutput => {
442-
1 + 1 + 73 + 1 + 1 + 1 + 77
443-
},
444-
};
445-
}
446-
tx_weight
447-
}
448-
449366
/// In LN, output claimed are time-sensitive, which means we have to spend them before reaching some timelock expiration. At in-channel
450367
/// output detection, we generate a first version of a claim tx and associate to it a height timer. A height timer is an absolute block
451368
/// height than once reached we should generate a new bumped "version" of the claim tx to be sure than we safely claim outputs before
@@ -535,11 +452,11 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
535452
for per_outp_material in cached_claim_datas.per_input_material.values() {
536453
match per_outp_material {
537454
&InputMaterial::Revoked { ref input_descriptor, ref amount, .. } => {
538-
inputs_witnesses_weight += Self::get_witnesses_weight(&[*input_descriptor]);
455+
inputs_witnesses_weight += onchain_utils::get_witnesses_weight(&[*input_descriptor]);
539456
amt += *amount;
540457
},
541458
&InputMaterial::RemoteHTLC { ref preimage, ref htlc, .. } => {
542-
inputs_witnesses_weight += Self::get_witnesses_weight(if preimage.is_some() { &[InputDescriptors::OfferedHTLC] } else { &[InputDescriptors::ReceivedHTLC] });
459+
inputs_witnesses_weight += onchain_utils::get_witnesses_weight(if preimage.is_some() { &[InputDescriptors::OfferedHTLC] } else { &[InputDescriptors::ReceivedHTLC] });
543460
amt += htlc.amount_msat / 1000;
544461
},
545462
&InputMaterial::LocalHTLC { .. } => {

0 commit comments

Comments
 (0)