Skip to content

Commit 21ed477

Browse files
committed
Include channel_value_satoshis in ChannelTransactionParameters
Since channel_value_satoshis is needed by the signer and may change for each new FundingScope, included it in ChannelTransactionParameters so it can be passed to each signer call in the upcoming commits.
1 parent e23d32d commit 21ed477

File tree

8 files changed

+55
-24
lines changed

8 files changed

+55
-24
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5421,7 +5421,8 @@ mod tests {
54215421
selected_contest_delay: 67,
54225422
}),
54235423
funding_outpoint: Some(funding_outpoint),
5424-
channel_type_features: ChannelTypeFeatures::only_static_remote_key()
5424+
channel_type_features: ChannelTypeFeatures::only_static_remote_key(),
5425+
channel_value_satoshis: 0,
54255426
};
54265427
// Prune with one old state and a holder commitment tx holding a few overlaps with the
54275428
// old state.
@@ -5430,11 +5431,11 @@ mod tests {
54305431
let monitor = ChannelMonitor::new(Secp256k1::new(), keys,
54315432
Some(ShutdownScript::new_p2wpkh_from_pubkey(shutdown_pubkey).into_inner()), 0, &ScriptBuf::new(),
54325433
(OutPoint { txid: Txid::from_slice(&[43; 32]).unwrap(), index: 0 }, ScriptBuf::new()),
5433-
&channel_parameters, true, ScriptBuf::new(), 46, 0, HolderCommitmentTransaction::dummy(&mut Vec::new()),
5434+
&channel_parameters, true, ScriptBuf::new(), 46, 0, HolderCommitmentTransaction::dummy(0, &mut Vec::new()),
54345435
best_block, dummy_key, channel_id);
54355436

54365437
let mut htlcs = preimages_slice_to_htlcs!(preimages[0..10]);
5437-
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(&mut htlcs);
5438+
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(0, &mut htlcs);
54385439

54395440
monitor.provide_latest_holder_commitment_tx(dummy_commitment_tx.clone(),
54405441
htlcs.into_iter().map(|(htlc, _)| (htlc, Some(dummy_sig), None)).collect()).unwrap();
@@ -5473,7 +5474,7 @@ mod tests {
54735474
// Now update holder commitment tx info, pruning only element 18 as we still care about the
54745475
// previous commitment tx's preimages too
54755476
let mut htlcs = preimages_slice_to_htlcs!(preimages[0..5]);
5476-
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(&mut htlcs);
5477+
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(0, &mut htlcs);
54775478
monitor.provide_latest_holder_commitment_tx(dummy_commitment_tx.clone(),
54785479
htlcs.into_iter().map(|(htlc, _)| (htlc, Some(dummy_sig), None)).collect()).unwrap();
54795480
secret[0..32].clone_from_slice(&<Vec<u8>>::from_hex("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
@@ -5484,7 +5485,7 @@ mod tests {
54845485

54855486
// But if we do it again, we'll prune 5-10
54865487
let mut htlcs = preimages_slice_to_htlcs!(preimages[0..3]);
5487-
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(&mut htlcs);
5488+
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(0, &mut htlcs);
54885489
monitor.provide_latest_holder_commitment_tx(dummy_commitment_tx,
54895490
htlcs.into_iter().map(|(htlc, _)| (htlc, Some(dummy_sig), None)).collect()).unwrap();
54905491
secret[0..32].clone_from_slice(&<Vec<u8>>::from_hex("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
@@ -5673,14 +5674,15 @@ mod tests {
56735674
selected_contest_delay: 67,
56745675
}),
56755676
funding_outpoint: Some(funding_outpoint),
5676-
channel_type_features: ChannelTypeFeatures::only_static_remote_key()
5677+
channel_type_features: ChannelTypeFeatures::only_static_remote_key(),
5678+
channel_value_satoshis: 0,
56775679
};
56785680
let shutdown_pubkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
56795681
let best_block = BestBlock::from_network(Network::Testnet);
56805682
let monitor = ChannelMonitor::new(Secp256k1::new(), keys,
56815683
Some(ShutdownScript::new_p2wpkh_from_pubkey(shutdown_pubkey).into_inner()), 0, &ScriptBuf::new(),
56825684
(OutPoint { txid: Txid::from_slice(&[43; 32]).unwrap(), index: 0 }, ScriptBuf::new()),
5683-
&channel_parameters, true, ScriptBuf::new(), 46, 0, HolderCommitmentTransaction::dummy(&mut Vec::new()),
5685+
&channel_parameters, true, ScriptBuf::new(), 46, 0, HolderCommitmentTransaction::dummy(0, &mut Vec::new()),
56845686
best_block, dummy_key, channel_id);
56855687

56865688
let chan_id = monitor.inner.lock().unwrap().channel_id();

lightning/src/chain/onchaintx.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
368368
let prev_holder_commitment = Readable::read(reader)?;
369369
let _prev_holder_htlc_sigs: Option<Vec<Option<(usize, Signature)>>> = Readable::read(reader)?;
370370

371-
let channel_parameters = Readable::read(reader)?;
371+
let channel_parameters = ReadableArgs::<u64>::read(reader, channel_value_satoshis)?;
372372

373373
// Read the serialized signer bytes, but don't deserialize them, as we'll obtain our signer
374374
// by re-deriving the private key material.
@@ -1355,6 +1355,7 @@ mod tests {
13551355
}),
13561356
funding_outpoint: Some(funding_outpoint),
13571357
channel_type_features: ChannelTypeFeatures::only_static_remote_key(),
1358+
channel_value_satoshis: 0,
13581359
};
13591360

13601361
// Create an OnchainTxHandler for a commitment containing HTLCs with CLTV expiries of 0, 1,
@@ -1374,7 +1375,7 @@ mod tests {
13741375
(),
13751376
));
13761377
}
1377-
let holder_commit = HolderCommitmentTransaction::dummy(&mut htlcs);
1378+
let holder_commit = HolderCommitmentTransaction::dummy(1000000, &mut htlcs);
13781379
let mut tx_handler = OnchainTxHandler::new(
13791380
1000000,
13801381
[0; 32],

lightning/src/events/bump_transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ mod tests {
956956
channel_derivation_parameters: ChannelDerivationParameters {
957957
value_satoshis: 42_000_000,
958958
keys_id: [42; 32],
959-
transaction_parameters: ChannelTransactionParameters::test_dummy(),
959+
transaction_parameters: ChannelTransactionParameters::test_dummy(42_000_000),
960960
},
961961
outpoint: OutPoint { txid: Txid::from_byte_array([42; 32]), vout: 0 },
962962
},

lightning/src/ln/chan_utils.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::chain::package::WEIGHT_REVOKED_OUTPUT;
3030
use crate::sign::EntropySource;
3131
use crate::types::payment::{PaymentHash, PaymentPreimage};
3232
use crate::ln::msgs::DecodeError;
33-
use crate::util::ser::{Readable, RequiredWrapper, Writeable, Writer};
33+
use crate::util::ser::{Readable, ReadableArgs, RequiredWrapper, Writeable, Writer};
3434
use crate::util::transaction_utils;
3535

3636
use bitcoin::locktime::absolute::LockTime;
@@ -882,7 +882,9 @@ pub struct ChannelTransactionParameters {
882882
pub funding_outpoint: Option<chain::transaction::OutPoint>,
883883
/// This channel's type, as negotiated during channel open. For old objects where this field
884884
/// wasn't serialized, it will default to static_remote_key at deserialization.
885-
pub channel_type_features: ChannelTypeFeatures
885+
pub channel_type_features: ChannelTypeFeatures,
886+
/// The value locked in the channel, denominated in satoshis.
887+
pub channel_value_satoshis: u64,
886888
}
887889

888890
/// Late-bound per-channel counterparty data used to build transactions.
@@ -930,7 +932,7 @@ impl ChannelTransactionParameters {
930932
}
931933

932934
#[cfg(test)]
933-
pub fn test_dummy() -> Self {
935+
pub fn test_dummy(channel_value_satoshis: u64) -> Self {
934936
let dummy_keys = ChannelPublicKeys {
935937
funding_pubkey: PublicKey::from_slice(&[2; 33]).unwrap(),
936938
revocation_basepoint: PublicKey::from_slice(&[2; 33]).unwrap().into(),
@@ -950,6 +952,7 @@ impl ChannelTransactionParameters {
950952
txid: Txid::from_byte_array([42; 32]), index: 0
951953
}),
952954
channel_type_features: ChannelTypeFeatures::empty(),
955+
channel_value_satoshis,
953956
}
954957
}
955958
}
@@ -970,20 +973,22 @@ impl Writeable for ChannelTransactionParameters {
970973
(8, self.funding_outpoint, option),
971974
(10, legacy_deserialization_prevention_marker, option),
972975
(11, self.channel_type_features, required),
976+
(13, self.channel_value_satoshis, required),
973977
});
974978
Ok(())
975979
}
976980
}
977981

978-
impl Readable for ChannelTransactionParameters {
979-
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
982+
impl ReadableArgs<u64> for ChannelTransactionParameters {
983+
fn read<R: io::Read>(reader: &mut R, read_args: u64) -> Result<Self, DecodeError> {
980984
let mut holder_pubkeys = RequiredWrapper(None);
981985
let mut holder_selected_contest_delay = RequiredWrapper(None);
982986
let mut is_outbound_from_holder = RequiredWrapper(None);
983987
let mut counterparty_parameters = None;
984988
let mut funding_outpoint = None;
985989
let mut _legacy_deserialization_prevention_marker: Option<()> = None;
986990
let mut channel_type_features = None;
991+
let mut channel_value_satoshis = None;
987992

988993
read_tlv_fields!(reader, {
989994
(0, holder_pubkeys, required),
@@ -993,8 +998,14 @@ impl Readable for ChannelTransactionParameters {
993998
(8, funding_outpoint, option),
994999
(10, _legacy_deserialization_prevention_marker, option),
9951000
(11, channel_type_features, option),
1001+
(13, channel_value_satoshis, option),
9961002
});
9971003

1004+
let channel_value_satoshis = channel_value_satoshis.unwrap_or(read_args);
1005+
if channel_value_satoshis != read_args {
1006+
return Err(DecodeError::InvalidValue);
1007+
}
1008+
9981009
let mut additional_features = ChannelTypeFeatures::empty();
9991010
additional_features.set_anchors_nonzero_fee_htlc_tx_required();
10001011
chain::package::verify_channel_type_features(&channel_type_features, Some(&additional_features))?;
@@ -1005,7 +1016,8 @@ impl Readable for ChannelTransactionParameters {
10051016
is_outbound_from_holder: is_outbound_from_holder.0.unwrap(),
10061017
counterparty_parameters,
10071018
funding_outpoint,
1008-
channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key())
1019+
channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key()),
1020+
channel_value_satoshis,
10091021
})
10101022
}
10111023
}
@@ -1105,7 +1117,7 @@ impl_writeable_tlv_based!(HolderCommitmentTransaction, {
11051117

11061118
impl HolderCommitmentTransaction {
11071119
#[cfg(test)]
1108-
pub fn dummy(htlcs: &mut Vec<(HTLCOutputInCommitment, ())>) -> Self {
1120+
pub fn dummy(channel_value_satoshis: u64, htlcs: &mut Vec<(HTLCOutputInCommitment, ())>) -> Self {
11091121
let secp_ctx = Secp256k1::new();
11101122
let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
11111123
let dummy_sig = sign(&secp_ctx, &secp256k1::Message::from_digest([42; 32]), &SecretKey::from_slice(&[42; 32]).unwrap());
@@ -1131,6 +1143,7 @@ impl HolderCommitmentTransaction {
11311143
counterparty_parameters: Some(CounterpartyChannelTransactionParameters { pubkeys: channel_pubkeys.clone(), selected_contest_delay: 0 }),
11321144
funding_outpoint: Some(chain::transaction::OutPoint { txid: Txid::all_zeros(), index: 0 }),
11331145
channel_type_features: ChannelTypeFeatures::only_static_remote_key(),
1146+
channel_value_satoshis,
11341147
};
11351148
let mut counterparty_htlc_sigs = Vec::new();
11361149
for _ in 0..htlcs.len() {
@@ -1942,6 +1955,7 @@ mod tests {
19421955
counterparty_parameters: Some(CounterpartyChannelTransactionParameters { pubkeys: counterparty_pubkeys.clone(), selected_contest_delay: 0 }),
19431956
funding_outpoint: Some(chain::transaction::OutPoint { txid: Txid::all_zeros(), index: 0 }),
19441957
channel_type_features: ChannelTypeFeatures::only_static_remote_key(),
1958+
channel_value_satoshis: 3000,
19451959
};
19461960
let htlcs_with_aux = Vec::new();
19471961

lightning/src/ln/channel.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2597,7 +2597,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25972597
pubkeys: counterparty_pubkeys,
25982598
}),
25992599
funding_outpoint: None,
2600-
channel_type_features: channel_type.clone()
2600+
channel_type_features: channel_type.clone(),
2601+
channel_value_satoshis,
26012602
},
26022603
funding_transaction: None,
26032604
};
@@ -2832,7 +2833,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
28322833
is_outbound_from_holder: true,
28332834
counterparty_parameters: None,
28342835
funding_outpoint: None,
2835-
channel_type_features: channel_type.clone()
2836+
channel_type_features: channel_type.clone(),
2837+
channel_value_satoshis,
28362838
},
28372839
funding_transaction: None,
28382840
};
@@ -10512,7 +10514,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1051210514
_ => return Err(DecodeError::InvalidValue),
1051310515
};
1051410516

10515-
let mut channel_parameters: ChannelTransactionParameters = Readable::read(reader)?;
10517+
let mut channel_parameters: ChannelTransactionParameters = ReadableArgs::<u64>::read(reader, channel_value_satoshis)?;
1051610518
let funding_transaction: Option<Transaction> = Readable::read(reader)?;
1051710519

1051810520
let counterparty_cur_commitment_point = Readable::read(reader)?;
@@ -10657,6 +10659,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1065710659
// ChannelTransactionParameters may have had an empty features set upon deserialization.
1065810660
// To account for that, we're proactively setting/overriding the field here.
1065910661
channel_parameters.channel_type_features = chan_features.clone();
10662+
// ChannelTransactionParameters::channel_value_satoshis defaults to 0 prior to version 0.2.
10663+
channel_parameters.channel_value_satoshis = channel_value_satoshis;
1066010664

1066110665
let mut secp_ctx = Secp256k1::new();
1066210666
secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());

lightning/src/ln/dual_funding_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ fn do_test_v2_channel_establishment(session: V2ChannelEstablishmentTestSession)
171171
is_outbound_from_holder: true,
172172
funding_outpoint,
173173
channel_type_features,
174+
channel_value_satoshis: funding_satoshis,
174175
};
175176

176177
channel

lightning/src/sign/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use crate::ln::msgs::{UnsignedChannelAnnouncement, UnsignedGossipMessage};
5858
use crate::ln::script::ShutdownScript;
5959
use crate::offers::invoice::UnsignedBolt12Invoice;
6060
use crate::types::payment::PaymentPreimage;
61-
use crate::util::ser::Writeable;
61+
use crate::util::ser::{ReadableArgs, Writeable};
6262
use crate::util::transaction_utils;
6363

6464
use crate::crypto::chacha20::ChaCha20;
@@ -127,7 +127,7 @@ impl_writeable_tlv_based!(DelayedPaymentOutputDescriptor, {
127127
(8, revocation_pubkey, required),
128128
(10, channel_keys_id, required),
129129
(12, channel_value_satoshis, required),
130-
(13, channel_transaction_parameters, option),
130+
(13, channel_transaction_parameters, (option: ReadableArgs, channel_value_satoshis.0.unwrap())),
131131
});
132132

133133
pub(crate) const P2WPKH_WITNESS_WEIGHT: u64 = 1 /* num stack items */ +
@@ -196,7 +196,7 @@ impl_writeable_tlv_based!(StaticPaymentOutputDescriptor, {
196196
(2, output, required),
197197
(4, channel_keys_id, required),
198198
(6, channel_value_satoshis, required),
199-
(7, channel_transaction_parameters, option),
199+
(7, channel_transaction_parameters, (option: ReadableArgs, channel_value_satoshis.0.unwrap())),
200200
});
201201

202202
/// Describes the necessary information to spend a spendable output.
@@ -562,7 +562,7 @@ pub struct ChannelDerivationParameters {
562562
impl_writeable_tlv_based!(ChannelDerivationParameters, {
563563
(0, value_satoshis, required),
564564
(2, keys_id, required),
565-
(4, transaction_parameters, required),
565+
(4, transaction_parameters, (required: ReadableArgs, value_satoshis.0.unwrap())),
566566
});
567567

568568
/// A descriptor used to sign for a commitment transaction's HTLC output.

lightning/src/util/ser_macros.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ macro_rules! _encode_tlv {
2929
BigSize($field.serialized_length() as u64).write($stream)?;
3030
$field.write($stream)?;
3131
};
32+
($stream: expr, $type: expr, $field: expr, (required: $trait: ident $(, $read_arg: expr)?) $(, $self: ident)?) => {
33+
$crate::_encode_tlv!($stream, $type, $field, required);
34+
};
3235
($stream: expr, $type: expr, $field: expr, required_vec $(, $self: ident)?) => {
3336
$crate::_encode_tlv!($stream, $type, $crate::util::ser::WithoutLength(&$field), required);
3437
};
@@ -185,6 +188,9 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
185188
.expect("No in-memory data may fail to serialize");
186189
$len.0 += field_len;
187190
};
191+
($len: expr, $type: expr, $field: expr, (required: $trait: ident $(, $read_arg: expr)?) $(, $self: ident)?) => {
192+
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required);
193+
};
188194
($len: expr, $type: expr, $field: expr, required_vec $(, $self: ident)?) => {
189195
let field = $crate::util::ser::WithoutLength(&$field);
190196
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, field, required);
@@ -801,6 +807,9 @@ macro_rules! _init_tlv_based_struct_field {
801807
($field: ident, required) => {
802808
$field.0.unwrap()
803809
};
810+
($field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {
811+
$crate::_init_tlv_based_struct_field!($field, required)
812+
};
804813
($field: ident, required_vec) => {
805814
$field
806815
};

0 commit comments

Comments
 (0)