Skip to content

Commit 6097917

Browse files
committed
Track ChannelTransactionParameters in ChannelMonitor FundingScope
The `ChannelTransactionParameters` will change across `FundingScope`s, so we make sure to track it to ensure it is updated accordingly when a new `FundingScope` is applied/considered. Along the way, we also clean up some unnecessary function arguments to `ChannelMonitor::new` that we can just obtain from the `ChannelTransactionParameters` already provided.
1 parent 352e752 commit 6097917

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -872,10 +872,9 @@ impl<Signer: EcdsaChannelSigner> Clone for ChannelMonitor<Signer> where Signer:
872872

873873
#[derive(Clone, PartialEq)]
874874
struct FundingScope {
875-
outpoint: OutPoint,
876875
script_pubkey: ScriptBuf,
877876
redeem_script: ScriptBuf,
878-
channel_value_satoshis: u64,
877+
channel_parameters: ChannelTransactionParameters,
879878

880879
current_counterparty_commitment_txid: Option<Txid>,
881880
prev_counterparty_commitment_txid: Option<Txid>,
@@ -1116,15 +1115,16 @@ impl<Signer: EcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signer> {
11161115

11171116
self.channel_keys_id.write(writer)?;
11181117
self.holder_revocation_basepoint.write(writer)?;
1119-
writer.write_all(&self.funding.outpoint.txid[..])?;
1120-
writer.write_all(&self.funding.outpoint.index.to_be_bytes())?;
1118+
let funding_outpoint = self.get_funding_txo();
1119+
writer.write_all(&funding_outpoint.txid[..])?;
1120+
writer.write_all(&funding_outpoint.index.to_be_bytes())?;
11211121
self.funding.script_pubkey.write(writer)?;
11221122
self.funding.current_counterparty_commitment_txid.write(writer)?;
11231123
self.funding.prev_counterparty_commitment_txid.write(writer)?;
11241124

11251125
self.counterparty_commitment_params.write(writer)?;
11261126
self.funding.redeem_script.write(writer)?;
1127-
self.funding.channel_value_satoshis.write(writer)?;
1127+
self.funding.channel_parameters.channel_value_satoshis.write(writer)?;
11281128

11291129
match self.their_cur_per_commitment_points {
11301130
Some((idx, pubkey, second_option)) => {
@@ -1381,10 +1381,9 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
13811381

13821382
pub(crate) fn new(
13831383
secp_ctx: Secp256k1<secp256k1::All>, keys: Signer, shutdown_script: Option<ScriptBuf>,
1384-
on_counterparty_tx_csv: u16, destination_script: &Script, funding_outpoint: OutPoint,
1385-
funding_script: ScriptBuf, channel_parameters: &ChannelTransactionParameters,
1386-
holder_pays_commitment_tx_fee: bool, funding_redeemscript: ScriptBuf,
1387-
channel_value_satoshis: u64, commitment_transaction_number_obscure_factor: u64,
1384+
on_counterparty_tx_csv: u16, destination_script: &Script,
1385+
channel_parameters: &ChannelTransactionParameters, holder_pays_commitment_tx_fee: bool,
1386+
commitment_transaction_number_obscure_factor: u64,
13881387
initial_holder_commitment_tx: HolderCommitmentTransaction, best_block: BestBlock,
13891388
counterparty_node_id: PublicKey, channel_id: ChannelId,
13901389
) -> ChannelMonitor<Signer> {
@@ -1424,21 +1423,24 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
14241423
};
14251424

14261425
let onchain_tx_handler = OnchainTxHandler::new(
1427-
channel_value_satoshis, channel_keys_id, destination_script.into(), keys,
1428-
channel_parameters.clone(), initial_holder_commitment_tx, secp_ctx
1426+
channel_parameters.channel_value_satoshis, channel_keys_id, destination_script.into(),
1427+
keys, channel_parameters.clone(), initial_holder_commitment_tx, secp_ctx
14291428
);
14301429

1430+
let funding_outpoint = channel_parameters.funding_outpoint
1431+
.expect("Funding outpoint must be known during initialization");
1432+
let funding_redeem_script = channel_parameters.make_funding_redeemscript();
1433+
let funding_script = funding_redeem_script.to_p2wsh();
14311434
let mut outputs_to_watch = new_hash_map();
14321435
outputs_to_watch.insert(
14331436
funding_outpoint.txid, vec![(funding_outpoint.index as u32, funding_script.clone())],
14341437
);
14351438

14361439
Self::from_impl(ChannelMonitorImpl {
14371440
funding: FundingScope {
1438-
outpoint: funding_outpoint,
14391441
script_pubkey: funding_script,
1440-
redeem_script: funding_redeemscript,
1441-
channel_value_satoshis,
1442+
redeem_script: funding_redeem_script,
1443+
channel_parameters: channel_parameters.clone(),
14421444

14431445
current_counterparty_commitment_txid: None,
14441446
prev_counterparty_commitment_txid: None,
@@ -1669,7 +1671,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
16691671
let lock = self.inner.lock().unwrap();
16701672
let logger = WithChannelMonitor::from_impl(logger, &*lock, None);
16711673
log_trace!(&logger, "Registering funding outpoint {}", &lock.get_funding_txo());
1672-
filter.register_tx(&lock.funding.outpoint.txid, &lock.funding.script_pubkey);
1674+
let funding_outpoint = lock.get_funding_txo();
1675+
filter.register_tx(&funding_outpoint.txid, &lock.funding.script_pubkey);
16731676
for (txid, outputs) in lock.get_outputs_to_watch().iter() {
16741677
for (index, script_pubkey) in outputs.iter() {
16751678
assert!(*index <= u16::MAX as u32);
@@ -3152,18 +3155,19 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
31523155
fn generate_claimable_outpoints_and_watch_outputs(&mut self, reason: ClosureReason) -> (Vec<PackageTemplate>, Vec<TransactionOutputs>) {
31533156
let funding_outp = HolderFundingOutput::build(
31543157
self.funding.redeem_script.clone(),
3155-
self.funding.channel_value_satoshis,
3158+
self.funding.channel_parameters.channel_value_satoshis,
31563159
self.onchain_tx_handler.channel_type_features().clone()
31573160
);
3161+
let funding_outpoint = self.get_funding_txo();
31583162
let commitment_package = PackageTemplate::build_package(
3159-
self.funding.outpoint.txid.clone(), self.funding.outpoint.index as u32,
3163+
funding_outpoint.txid.clone(), funding_outpoint.index as u32,
31603164
PackageSolvingData::HolderFundingOutput(funding_outp),
31613165
self.best_block.height,
31623166
);
31633167
let mut claimable_outpoints = vec![commitment_package];
31643168
let event = MonitorEvent::HolderForceClosedWithInfo {
31653169
reason,
3166-
outpoint: self.funding.outpoint,
3170+
outpoint: funding_outpoint,
31673171
channel_id: self.channel_id,
31683172
};
31693173
self.pending_monitor_events.push(event);
@@ -3366,7 +3370,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33663370
}
33673371

33683372
fn get_funding_txo(&self) -> OutPoint {
3369-
self.funding.outpoint
3373+
self.funding.channel_parameters.funding_outpoint
3374+
.expect("Funding outpoint must be set for active monitor")
33703375
}
33713376

33723377
fn get_funding_script(&self) -> ScriptBuf {
@@ -3409,7 +3414,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
34093414
let commitment_txid = commitment_tx.compute_txid();
34103415
debug_assert_eq!(self.funding.current_holder_commitment_tx.txid, commitment_txid);
34113416
let pending_htlcs = self.funding.current_holder_commitment_tx.non_dust_htlcs();
3412-
let commitment_tx_fee_satoshis = self.funding.channel_value_satoshis -
3417+
let channel_value_satoshis = self.funding.channel_parameters.channel_value_satoshis;
3418+
let commitment_tx_fee_satoshis = channel_value_satoshis -
34133419
commitment_tx.output.iter().fold(0u64, |sum, output| sum + output.value.to_sat());
34143420
ret.push(Event::BumpTransaction(BumpTransactionEvent::ChannelClose {
34153421
channel_id,
@@ -3421,7 +3427,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
34213427
anchor_descriptor: AnchorDescriptor {
34223428
channel_derivation_parameters: ChannelDerivationParameters {
34233429
keys_id: self.channel_keys_id,
3424-
value_satoshis: self.funding.channel_value_satoshis,
3430+
value_satoshis: channel_value_satoshis,
34253431
transaction_parameters: self.onchain_tx_handler.channel_transaction_parameters.clone(),
34263432
},
34273433
outpoint: BitcoinOutPoint {
@@ -3442,7 +3448,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
34423448
htlc_descriptors.push(HTLCDescriptor {
34433449
channel_derivation_parameters: ChannelDerivationParameters {
34443450
keys_id: self.channel_keys_id,
3445-
value_satoshis: self.funding.channel_value_satoshis,
3451+
value_satoshis: self.funding.channel_parameters.channel_value_satoshis,
34463452
transaction_parameters: self.onchain_tx_handler.channel_transaction_parameters.clone(),
34473453
},
34483454
commitment_txid: htlc.commitment_txid,
@@ -4145,7 +4151,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
41454151
// (except for HTLC transactions for channels with anchor outputs), which is an easy
41464152
// way to filter out any potential non-matching txn for lazy filters.
41474153
let prevout = &tx.input[0].previous_output;
4148-
if prevout.txid == self.funding.outpoint.txid && prevout.vout == self.funding.outpoint.index as u32 {
4154+
let funding_outpoint = self.get_funding_txo();
4155+
if prevout.txid == funding_outpoint.txid && prevout.vout == funding_outpoint.index as u32 {
41494156
let mut balance_spendable_csv = None;
41504157
log_info!(logger, "Channel {} closed by funding output spend in txid {}.",
41514158
&self.channel_id(), txid);
@@ -4815,7 +4822,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
48154822
output: outp.clone(),
48164823
revocation_pubkey: broadcasted_holder_revokable_script.2,
48174824
channel_keys_id: self.channel_keys_id,
4818-
channel_value_satoshis: self.funding.channel_value_satoshis,
4825+
channel_value_satoshis: self.funding.channel_parameters.channel_value_satoshis,
48194826
channel_transaction_parameters: Some(self.onchain_tx_handler.channel_transaction_parameters.clone()),
48204827
}));
48214828
}
@@ -4825,7 +4832,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
48254832
outpoint: OutPoint { txid: tx.compute_txid(), index: i as u16 },
48264833
output: outp.clone(),
48274834
channel_keys_id: self.channel_keys_id,
4828-
channel_value_satoshis: self.funding.channel_value_satoshis,
4835+
channel_value_satoshis: self.funding.channel_parameters.channel_value_satoshis,
48294836
channel_transaction_parameters: Some(self.onchain_tx_handler.channel_transaction_parameters.clone()),
48304837
}));
48314838
}
@@ -5189,12 +5196,13 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
51895196
To continue, run a v0.1 release, send/route a payment over the channel or close it.", channel_id);
51905197
}
51915198

5199+
let channel_parameters = onchain_tx_handler.channel_transaction_parameters.clone();
5200+
51925201
Ok((best_block.block_hash, ChannelMonitor::from_impl(ChannelMonitorImpl {
51935202
funding: FundingScope {
5194-
outpoint,
51955203
script_pubkey: funding_script,
51965204
redeem_script: funding_redeemscript,
5197-
channel_value_satoshis,
5205+
channel_parameters,
51985206

51995207
current_counterparty_commitment_txid,
52005208
prev_counterparty_commitment_txid,
@@ -5487,12 +5495,11 @@ mod tests {
54875495
// old state.
54885496
let shutdown_pubkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
54895497
let shutdown_script = ShutdownScript::new_p2wpkh_from_pubkey(shutdown_pubkey);
5490-
let funding_txo = OutPoint { txid: Txid::from_slice(&[43; 32]).unwrap(), index: 0 };
54915498
let best_block = BestBlock::from_network(Network::Testnet);
54925499
let monitor = ChannelMonitor::new(
54935500
Secp256k1::new(), keys, Some(shutdown_script.into_inner()), 0, &ScriptBuf::new(),
5494-
funding_txo, ScriptBuf::new(), &channel_parameters, true, ScriptBuf::new(), 46, 0,
5495-
HolderCommitmentTransaction::dummy(0, &mut Vec::new()), best_block, dummy_key, channel_id,
5501+
&channel_parameters, true, 0, HolderCommitmentTransaction::dummy(0, &mut Vec::new()),
5502+
best_block, dummy_key, channel_id,
54965503
);
54975504

54985505
let mut htlcs = preimages_slice_to_htlcs!(preimages[0..10]);
@@ -5740,12 +5747,11 @@ mod tests {
57405747
};
57415748
let shutdown_pubkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
57425749
let shutdown_script = ShutdownScript::new_p2wpkh_from_pubkey(shutdown_pubkey);
5743-
let funding_txo = OutPoint { txid: Txid::from_slice(&[43; 32]).unwrap(), index: 0 };
57445750
let best_block = BestBlock::from_network(Network::Testnet);
57455751
let monitor = ChannelMonitor::new(
57465752
Secp256k1::new(), keys, Some(shutdown_script.into_inner()), 0, &ScriptBuf::new(),
5747-
funding_txo, ScriptBuf::new(), &channel_parameters, true, ScriptBuf::new(), 46, 0,
5748-
HolderCommitmentTransaction::dummy(0, &mut Vec::new()), best_block, dummy_key, channel_id,
5753+
&channel_parameters, true, 0, HolderCommitmentTransaction::dummy(0, &mut Vec::new()),
5754+
best_block, dummy_key, channel_id,
57495755
);
57505756

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

lightning/src/ln/channel.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,19 +2125,15 @@ trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvide
21252125

21262126
let context = self.context();
21272127
let funding = self.funding();
2128-
let funding_redeemscript = funding.get_funding_redeemscript();
2129-
let funding_txo = funding.get_funding_txo().unwrap();
2130-
let funding_txo_script = funding_redeemscript.to_p2wsh();
21312128
let obscure_factor = get_commitment_transaction_number_obscure_factor(&funding.get_holder_pubkeys().payment_point, &funding.get_counterparty_pubkeys().payment_point, funding.is_outbound());
21322129
let shutdown_script = context.shutdown_scriptpubkey.clone().map(|script| script.into_inner());
21332130
let monitor_signer = signer_provider.derive_channel_signer(context.channel_keys_id);
21342131
// TODO(RBF): When implementing RBF, the funding_txo passed here must only update
21352132
// ChannelMonitorImp::first_confirmed_funding_txo during channel establishment, not splicing
21362133
let channel_monitor = ChannelMonitor::new(
21372134
context.secp_ctx.clone(), monitor_signer, shutdown_script,
2138-
funding.get_holder_selected_contest_delay(), &context.destination_script, funding_txo,
2139-
funding_txo_script, &funding.channel_transaction_parameters, funding.is_outbound(),
2140-
funding_redeemscript, funding.get_value_satoshis(), obscure_factor,
2135+
funding.get_holder_selected_contest_delay(), &context.destination_script,
2136+
&funding.channel_transaction_parameters, funding.is_outbound(), obscure_factor,
21412137
holder_commitment_tx, best_block, context.counterparty_node_id, context.channel_id(),
21422138
);
21432139
channel_monitor.provide_initial_counterparty_commitment_tx(

0 commit comments

Comments
 (0)