Skip to content

Commit 2b039e6

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 640ae63 commit 2b039e6

File tree

4 files changed

+102
-93
lines changed

4 files changed

+102
-93
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ use ln::msgs::DecodeError;
3939
use ln::chan_utils;
4040
use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HolderCommitmentTransaction, HTLCType};
4141
use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
42-
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
42+
use ln::onchaintx::OnchainTxHandler;
43+
use ln::onchain_utils::InputDescriptors;
4344
use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface, FeeEstimator};
4445
use chain::transaction::OutPoint;
4546
use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys};
@@ -2582,7 +2583,8 @@ mod tests {
25822583
use chain::transaction::OutPoint;
25832584
use ln::channelmanager::{PaymentPreimage, PaymentHash};
25842585
use ln::channelmonitor::ChannelMonitor;
2585-
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
2586+
use ln::onchain_utils::InputDescriptors;
2587+
use ln::onchain_utils;
25862588
use ln::chan_utils;
25872589
use ln::chan_utils::{HTLCOutputInCommitment, HolderCommitmentTransaction};
25882590
use util::test_utils::TestLogger;
@@ -2774,7 +2776,7 @@ mod tests {
27742776
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
27752777
}
27762778
}
2777-
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));
2779+
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));
27782780

27792781
// Claim tx with 1 offered HTLCs, 3 received HTLCs
27802782
claim_tx.input.clear();
@@ -2798,7 +2800,7 @@ mod tests {
27982800
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
27992801
}
28002802
}
2801-
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));
2803+
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));
28022804

28032805
// Justice tx with 1 revoked HTLC-Success tx output
28042806
claim_tx.input.clear();
@@ -2820,7 +2822,7 @@ mod tests {
28202822
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
28212823
}
28222824
}
2823-
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));
2825+
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));
28242826
}
28252827

28262828
// 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
@@ -32,6 +32,7 @@ pub mod peer_channel_encryptor;
3232
pub(crate) mod peer_channel_encryptor;
3333

3434
mod channel;
35+
mod onchain_utils;
3536
mod onion_utils;
3637
mod wire;
3738

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 & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use ln::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMateri
2626
use ln::channelmanager::PaymentPreimage;
2727
use ln::chan_utils;
2828
use ln::chan_utils::{TxCreationKeys, HolderCommitmentTransaction};
29+
use ln::onchain_utils::InputDescriptors;
30+
use ln::onchain_utils;
2931
use chain::chaininterface::{FeeEstimator, BroadcasterInterface, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
3032
use chain::keysinterface::ChannelKeys;
3133
use util::logger::Logger;
@@ -101,62 +103,6 @@ impl Readable for ClaimTxBumpMaterial {
101103
}
102104
}
103105

104-
#[derive(PartialEq, Clone, Copy)]
105-
pub(crate) enum InputDescriptors {
106-
RevokedOfferedHTLC,
107-
RevokedReceivedHTLC,
108-
OfferedHTLC,
109-
ReceivedHTLC,
110-
RevokedOutput, // either a revoked to_holder output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output
111-
}
112-
113-
impl Writeable for InputDescriptors {
114-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
115-
match self {
116-
&InputDescriptors::RevokedOfferedHTLC => {
117-
writer.write_all(&[0; 1])?;
118-
},
119-
&InputDescriptors::RevokedReceivedHTLC => {
120-
writer.write_all(&[1; 1])?;
121-
},
122-
&InputDescriptors::OfferedHTLC => {
123-
writer.write_all(&[2; 1])?;
124-
},
125-
&InputDescriptors::ReceivedHTLC => {
126-
writer.write_all(&[3; 1])?;
127-
}
128-
&InputDescriptors::RevokedOutput => {
129-
writer.write_all(&[4; 1])?;
130-
}
131-
}
132-
Ok(())
133-
}
134-
}
135-
136-
impl Readable for InputDescriptors {
137-
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
138-
let input_descriptor = match <u8 as Readable>::read(reader)? {
139-
0 => {
140-
InputDescriptors::RevokedOfferedHTLC
141-
},
142-
1 => {
143-
InputDescriptors::RevokedReceivedHTLC
144-
},
145-
2 => {
146-
InputDescriptors::OfferedHTLC
147-
},
148-
3 => {
149-
InputDescriptors::ReceivedHTLC
150-
},
151-
4 => {
152-
InputDescriptors::RevokedOutput
153-
}
154-
_ => return Err(DecodeError::InvalidValue),
155-
};
156-
Ok(input_descriptor)
157-
}
158-
}
159-
160106
macro_rules! subtract_high_prio_fee {
161107
($logger: ident, $fee_estimator: expr, $value: expr, $predicted_weight: expr, $used_feerate: expr) => {
162108
{
@@ -425,36 +371,6 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
425371
}
426372
}
427373

428-
pub(super) fn get_witnesses_weight(inputs: &[InputDescriptors]) -> usize {
429-
let mut tx_weight = 2; // count segwit flags
430-
for inp in inputs {
431-
// We use expected weight (and not actual) as signatures and time lock delays may vary
432-
tx_weight += match inp {
433-
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
434-
&InputDescriptors::RevokedOfferedHTLC => {
435-
1 + 1 + 73 + 1 + 33 + 1 + 133
436-
},
437-
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
438-
&InputDescriptors::RevokedReceivedHTLC => {
439-
1 + 1 + 73 + 1 + 33 + 1 + 139
440-
},
441-
// number_of_witness_elements + sig_length + counterpartyhtlc_sig + preimage_length + preimage + witness_script_length + witness_script
442-
&InputDescriptors::OfferedHTLC => {
443-
1 + 1 + 73 + 1 + 32 + 1 + 133
444-
},
445-
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
446-
&InputDescriptors::ReceivedHTLC => {
447-
1 + 1 + 73 + 1 + 1 + 1 + 139
448-
},
449-
// number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
450-
&InputDescriptors::RevokedOutput => {
451-
1 + 1 + 73 + 1 + 1 + 1 + 77
452-
},
453-
};
454-
}
455-
tx_weight
456-
}
457-
458374
/// In LN, output claimed are time-sensitive, which means we have to spend them before reaching some timelock expiration. At in-channel
459375
/// 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
460376
/// height than once reached we should generate a new bumped "version" of the claim tx to be sure than we safely claim outputs before
@@ -544,11 +460,11 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
544460
for per_outp_material in cached_claim_datas.per_input_material.values() {
545461
match per_outp_material {
546462
&InputMaterial::Revoked { ref input_descriptor, ref amount, .. } => {
547-
inputs_witnesses_weight += Self::get_witnesses_weight(&[*input_descriptor]);
463+
inputs_witnesses_weight += onchain_utils::get_witnesses_weight(&[*input_descriptor]);
548464
amt += *amount;
549465
},
550466
&InputMaterial::CounterpartyHTLC { ref preimage, ref htlc, .. } => {
551-
inputs_witnesses_weight += Self::get_witnesses_weight(if preimage.is_some() { &[InputDescriptors::OfferedHTLC] } else { &[InputDescriptors::ReceivedHTLC] });
467+
inputs_witnesses_weight += onchain_utils::get_witnesses_weight(if preimage.is_some() { &[InputDescriptors::OfferedHTLC] } else { &[InputDescriptors::ReceivedHTLC] });
552468
amt += htlc.amount_msat / 1000;
553469
},
554470
&InputMaterial::HolderHTLC { .. } => {

0 commit comments

Comments
 (0)