Skip to content

Commit 079626e

Browse files
committed
Add ChannelSigner::get_to_remote_witness_weight
1 parent 3d1aa57 commit 079626e

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4611,6 +4611,7 @@ impl<Signer: ChannelSigner> ChannelMonitorImpl<Signer> {
46114611

46124612
fn get_spendable_outputs(&self, tx: &Transaction) -> Vec<SpendableOutputDescriptor> {
46134613
let mut spendable_outputs = Vec::new();
4614+
let signer = &self.onchain_tx_handler.signer;
46144615
for (i, outp) in tx.output.iter().enumerate() {
46154616
if outp.script_pubkey == self.destination_script {
46164617
spendable_outputs.push(SpendableOutputDescriptor::StaticOutput {
@@ -4640,6 +4641,7 @@ impl<Signer: ChannelSigner> ChannelMonitorImpl<Signer> {
46404641
channel_keys_id: self.channel_keys_id,
46414642
channel_value_satoshis: self.channel_value_satoshis,
46424643
channel_transaction_parameters: Some(self.onchain_tx_handler.channel_transaction_parameters.clone()),
4644+
witness_weight: signer.get_to_remote_witness_weight(),
46434645
}));
46444646
}
46454647
if self.shutdown_script.as_ref() == Some(&outp.script_pubkey) {

lightning/src/sign/mod.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ pub struct StaticPaymentOutputDescriptor {
167167
///
168168
/// Added as optional, but always `Some` if the descriptor was produced in v0.0.117 or later.
169169
pub channel_transaction_parameters: Option<ChannelTransactionParameters>,
170+
/// Witness weight
171+
pub witness_weight: u64,
170172
}
171173

172174
impl StaticPaymentOutputDescriptor {
@@ -184,27 +186,22 @@ impl StaticPaymentOutputDescriptor {
184186
}
185187
})
186188
}
187-
188-
/// The maximum length a well-formed witness spending one of these should have.
189-
/// Note: If you have the grind_signatures feature enabled, this will be at least 1 byte
190-
/// shorter.
191-
pub fn max_witness_length(&self) -> u64 {
192-
if self.channel_transaction_parameters.as_ref().map_or(false, |p| p.supports_anchors()) {
193-
let witness_script_weight = 1 /* pubkey push */ + 33 /* pubkey */ +
194-
1 /* OP_CHECKSIGVERIFY */ + 1 /* OP_1 */ + 1 /* OP_CHECKSEQUENCEVERIFY */;
195-
1 /* num witness items */ + 1 /* sig push */ + 73 /* sig including sighash flag */ +
196-
1 /* witness script push */ + witness_script_weight
197-
} else {
198-
P2WPKH_WITNESS_WEIGHT
199-
}
200-
}
201189
}
202190
impl_writeable_tlv_based!(StaticPaymentOutputDescriptor, {
203191
(0, outpoint, required),
204192
(2, output, required),
205193
(4, channel_keys_id, required),
206194
(6, channel_value_satoshis, required),
207195
(7, channel_transaction_parameters, option),
196+
// Don't break downgrades ?
197+
(9, witness_weight, (default_value,
198+
if channel_transaction_parameters.as_ref().map_or(false, |p: &ChannelTransactionParameters| p.supports_anchors()) {
199+
let witness_script_weight = 1 /* pubkey push */ + 33 /* pubkey */ + 1 /* OP_CHECKSIGVERIFY */ + 1 /* OP_1 */ + 1 /* OP_CHECKSEQUENCEVERIFY */;
200+
1 /* num witness items */ + 1 /* sig push */ + 73 /* sig including sighash flag */ + 1 /* witness script push */ + witness_script_weight
201+
} else {
202+
P2WPKH_WITNESS_WEIGHT
203+
})
204+
),
208205
});
209206

210207
/// Describes the necessary information to spend a spendable output.
@@ -468,7 +465,7 @@ impl SpendableOutputDescriptor {
468465
sequence,
469466
witness: Witness::new(),
470467
});
471-
witness_weight += descriptor.max_witness_length();
468+
witness_weight += descriptor.witness_weight;
472469
#[cfg(feature = "grind_signatures")]
473470
{
474471
// Guarantees a low R signature
@@ -1088,6 +1085,21 @@ pub trait ChannelSigner {
10881085
fn get_holder_anchor_input_witness_weight(&self) -> u64 {
10891086
ANCHOR_INPUT_WITNESS_WEIGHT
10901087
}
1088+
1089+
/// Gets the weight of the witness that spends a `to_remote` output
1090+
fn get_to_remote_witness_weight(&self) -> u64 {
1091+
// The maximum length a well-formed witness spending one of these should have.
1092+
// Note: If you have the grind_signatures feature enabled, this will be at least 1 byte
1093+
// shorter.
1094+
if self.get_channel_parameters().as_ref().map_or(false, |p| p.supports_anchors()) {
1095+
let witness_script_weight = 1 /* pubkey push */ + 33 /* pubkey */ +
1096+
1 /* OP_CHECKSIGVERIFY */ + 1 /* OP_1 */ + 1 /* OP_CHECKSEQUENCEVERIFY */;
1097+
1 /* num witness items */ + 1 /* sig push */ + 73 /* sig including sighash flag */ +
1098+
1 /* witness script push */ + witness_script_weight
1099+
} else {
1100+
P2WPKH_WITNESS_WEIGHT
1101+
}
1102+
}
10911103
}
10921104

10931105
/// Specifies the recipient of an invoice.

0 commit comments

Comments
 (0)