Skip to content

Commit 0ea7dd8

Browse files
author
Antoine Riard
committed
Add package.rs file
Package.rs 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 499d84c commit 0ea7dd8

File tree

4 files changed

+105
-96
lines changed

4 files changed

+105
-96
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 8 additions & 6 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, HTLCType, ChannelTransactionParameters, HolderCommitmentTransaction};
4141
use ln::channelmanager::{BestBlock, HTLCSource};
42-
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
42+
use ln::onchaintx::OnchainTxHandler;
43+
use ln::package::InputDescriptors;
4344
use chain;
4445
use chain::WatchedOutput;
4546
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
@@ -352,7 +353,7 @@ pub(crate) enum InputMaterial {
352353
}
353354
}
354355

355-
impl Writeable for InputMaterial {
356+
impl Writeable for InputMaterial {
356357
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
357358
match self {
358359
&InputMaterial::Revoked { ref per_commitment_point, ref counterparty_delayed_payment_base_key, ref counterparty_htlc_base_key, ref per_commitment_key, ref input_descriptor, ref amount, ref htlc, ref on_counterparty_tx_csv} => {
@@ -3049,7 +3050,8 @@ mod tests {
30493050
use chain::transaction::OutPoint;
30503051
use ln::{PaymentPreimage, PaymentHash};
30513052
use ln::channelmanager::BestBlock;
3052-
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
3053+
use ln::package;
3054+
use ln::package::InputDescriptors;
30533055
use ln::chan_utils;
30543056
use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, ChannelTransactionParameters, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
30553057
use util::test_utils::{TestLogger, TestBroadcaster, TestFeeEstimator};
@@ -3263,7 +3265,7 @@ mod tests {
32633265
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
32643266
}
32653267
}
3266-
assert_eq!(base_weight + OnchainTxHandler::<InMemorySigner>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
3268+
assert_eq!(base_weight + package::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
32673269

32683270
// Claim tx with 1 offered HTLCs, 3 received HTLCs
32693271
claim_tx.input.clear();
@@ -3287,7 +3289,7 @@ mod tests {
32873289
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
32883290
}
32893291
}
3290-
assert_eq!(base_weight + OnchainTxHandler::<InMemorySigner>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
3292+
assert_eq!(base_weight + package::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
32913293

32923294
// Justice tx with 1 revoked HTLC-Success tx output
32933295
claim_tx.input.clear();
@@ -3309,7 +3311,7 @@ mod tests {
33093311
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
33103312
}
33113313
}
3312-
assert_eq!(base_weight + OnchainTxHandler::<InMemorySigner>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs));
3314+
assert_eq!(base_weight + package::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs));
33133315
}
33143316

33153317
// 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
@@ -28,6 +28,7 @@ pub mod peer_handler;
2828
pub mod chan_utils;
2929
pub mod features;
3030
pub(crate) mod onchaintx;
31+
pub(crate) mod package;
3132

3233
#[cfg(feature = "fuzztarget")]
3334
pub mod peer_channel_encryptor;

lightning/src/ln/onchaintx.rs

Lines changed: 6 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use ln::msgs::DecodeError;
2525
use ln::PaymentPreimage;
2626
use ln::chan_utils;
2727
use ln::chan_utils::{TxCreationKeys, ChannelTransactionParameters, HolderCommitmentTransaction};
28+
use ln::package::InputDescriptors;
29+
use ln::package;
2830
use chain::chaininterface::{FeeEstimator, BroadcasterInterface, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
2931
use chain::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMaterial, ClaimRequest};
3032
use chain::keysinterface::{Sign, KeysInterface};
@@ -123,62 +125,6 @@ impl Readable for ClaimTxBumpMaterial {
123125
}
124126
}
125127

126-
#[derive(PartialEq, Clone, Copy)]
127-
pub(crate) enum InputDescriptors {
128-
RevokedOfferedHTLC,
129-
RevokedReceivedHTLC,
130-
OfferedHTLC,
131-
ReceivedHTLC,
132-
RevokedOutput, // either a revoked to_holder output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output
133-
}
134-
135-
impl Writeable for InputDescriptors {
136-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
137-
match self {
138-
&InputDescriptors::RevokedOfferedHTLC => {
139-
writer.write_all(&[0; 1])?;
140-
},
141-
&InputDescriptors::RevokedReceivedHTLC => {
142-
writer.write_all(&[1; 1])?;
143-
},
144-
&InputDescriptors::OfferedHTLC => {
145-
writer.write_all(&[2; 1])?;
146-
},
147-
&InputDescriptors::ReceivedHTLC => {
148-
writer.write_all(&[3; 1])?;
149-
}
150-
&InputDescriptors::RevokedOutput => {
151-
writer.write_all(&[4; 1])?;
152-
}
153-
}
154-
Ok(())
155-
}
156-
}
157-
158-
impl Readable for InputDescriptors {
159-
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
160-
let input_descriptor = match <u8 as Readable>::read(reader)? {
161-
0 => {
162-
InputDescriptors::RevokedOfferedHTLC
163-
},
164-
1 => {
165-
InputDescriptors::RevokedReceivedHTLC
166-
},
167-
2 => {
168-
InputDescriptors::OfferedHTLC
169-
},
170-
3 => {
171-
InputDescriptors::ReceivedHTLC
172-
},
173-
4 => {
174-
InputDescriptors::RevokedOutput
175-
}
176-
_ => return Err(DecodeError::InvalidValue),
177-
};
178-
Ok(input_descriptor)
179-
}
180-
}
181-
182128
macro_rules! subtract_high_prio_fee {
183129
($logger: ident, $fee_estimator: expr, $value: expr, $predicted_weight: expr, $used_feerate: expr) => {
184130
{
@@ -271,7 +217,7 @@ pub struct OnchainTxHandler<ChannelSigner: Sign> {
271217
prev_holder_commitment: Option<HolderCommitmentTransaction>,
272218
prev_holder_htlc_sigs: Option<Vec<Option<(usize, Signature)>>>,
273219

274-
signer: ChannelSigner,
220+
pub(super) signer: ChannelSigner,
275221
pub(crate) channel_transaction_parameters: ChannelTransactionParameters,
276222

277223
// Used to track claiming requests. If claim tx doesn't confirm before height timer expiration we need to bump
@@ -305,7 +251,7 @@ pub struct OnchainTxHandler<ChannelSigner: Sign> {
305251

306252
latest_height: u32,
307253

308-
secp_ctx: Secp256k1<secp256k1::All>,
254+
pub(super) secp_ctx: Secp256k1<secp256k1::All>,
309255
}
310256

311257
const SERIALIZATION_VERSION: u8 = 1;
@@ -471,36 +417,6 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
471417
}
472418
}
473419

474-
pub(crate) fn get_witnesses_weight(inputs: &[InputDescriptors]) -> usize {
475-
let mut tx_weight = 2; // count segwit flags
476-
for inp in inputs {
477-
// We use expected weight (and not actual) as signatures and time lock delays may vary
478-
tx_weight += match inp {
479-
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
480-
&InputDescriptors::RevokedOfferedHTLC => {
481-
1 + 1 + 73 + 1 + 33 + 1 + 133
482-
},
483-
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
484-
&InputDescriptors::RevokedReceivedHTLC => {
485-
1 + 1 + 73 + 1 + 33 + 1 + 139
486-
},
487-
// number_of_witness_elements + sig_length + counterpartyhtlc_sig + preimage_length + preimage + witness_script_length + witness_script
488-
&InputDescriptors::OfferedHTLC => {
489-
1 + 1 + 73 + 1 + 32 + 1 + 133
490-
},
491-
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
492-
&InputDescriptors::ReceivedHTLC => {
493-
1 + 1 + 73 + 1 + 1 + 1 + 139
494-
},
495-
// number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
496-
&InputDescriptors::RevokedOutput => {
497-
1 + 1 + 73 + 1 + 1 + 1 + 77
498-
},
499-
};
500-
}
501-
tx_weight
502-
}
503-
504420
/// In LN, output claimed are time-sensitive, which means we have to spend them before reaching some timelock expiration. At in-channel
505421
/// 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
506422
/// height than once reached we should generate a new bumped "version" of the claim tx to be sure than we safely claim outputs before
@@ -592,11 +508,11 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
592508
for per_outp_material in cached_claim_datas.per_input_material.values() {
593509
match per_outp_material {
594510
&InputMaterial::Revoked { ref input_descriptor, ref amount, .. } => {
595-
inputs_witnesses_weight += Self::get_witnesses_weight(&[*input_descriptor]);
511+
inputs_witnesses_weight += package::get_witnesses_weight(&[*input_descriptor]);
596512
amt += *amount;
597513
},
598514
&InputMaterial::CounterpartyHTLC { ref preimage, ref htlc, .. } => {
599-
inputs_witnesses_weight += Self::get_witnesses_weight(if preimage.is_some() { &[InputDescriptors::OfferedHTLC] } else { &[InputDescriptors::ReceivedHTLC] });
515+
inputs_witnesses_weight += package::get_witnesses_weight(if preimage.is_some() { &[InputDescriptors::OfferedHTLC] } else { &[InputDescriptors::ReceivedHTLC] });
600516
amt += htlc.amount_msat / 1000;
601517
},
602518
&InputMaterial::HolderHTLC { .. } => {

lightning/src/ln/package.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_holder 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 + counterpartyhtlc_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+
}

0 commit comments

Comments
 (0)