Skip to content

Commit 8b47754

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 646b042 commit 8b47754

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>,
@@ -1110,15 +1109,16 @@ impl<Signer: EcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signer> {
11101109

11111110
self.channel_keys_id.write(writer)?;
11121111
self.holder_revocation_basepoint.write(writer)?;
1113-
writer.write_all(&self.funding.outpoint.txid[..])?;
1114-
writer.write_all(&self.funding.outpoint.index.to_be_bytes())?;
1112+
let funding_outpoint = self.get_funding_txo();
1113+
writer.write_all(&funding_outpoint.txid[..])?;
1114+
writer.write_all(&funding_outpoint.index.to_be_bytes())?;
11151115
self.funding.script_pubkey.write(writer)?;
11161116
self.funding.current_counterparty_commitment_txid.write(writer)?;
11171117
self.funding.prev_counterparty_commitment_txid.write(writer)?;
11181118

11191119
self.counterparty_commitment_params.write(writer)?;
11201120
self.funding.redeem_script.write(writer)?;
1121-
self.funding.channel_value_satoshis.write(writer)?;
1121+
self.funding.channel_parameters.channel_value_satoshis.write(writer)?;
11221122

11231123
match self.their_cur_per_commitment_points {
11241124
Some((idx, pubkey, second_option)) => {
@@ -1375,10 +1375,9 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
13751375

13761376
pub(crate) fn new(
13771377
secp_ctx: Secp256k1<secp256k1::All>, keys: Signer, shutdown_script: Option<ScriptBuf>,
1378-
on_counterparty_tx_csv: u16, destination_script: &Script, funding_outpoint: OutPoint,
1379-
funding_script: ScriptBuf, channel_parameters: &ChannelTransactionParameters,
1380-
holder_pays_commitment_tx_fee: bool, funding_redeemscript: ScriptBuf,
1381-
channel_value_satoshis: u64, commitment_transaction_number_obscure_factor: u64,
1378+
on_counterparty_tx_csv: u16, destination_script: &Script,
1379+
channel_parameters: &ChannelTransactionParameters, holder_pays_commitment_tx_fee: bool,
1380+
commitment_transaction_number_obscure_factor: u64,
13821381
initial_holder_commitment_tx: HolderCommitmentTransaction, best_block: BestBlock,
13831382
counterparty_node_id: PublicKey, channel_id: ChannelId,
13841383
) -> ChannelMonitor<Signer> {
@@ -1418,21 +1417,24 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
14181417
};
14191418

14201419
let onchain_tx_handler = OnchainTxHandler::new(
1421-
channel_value_satoshis, channel_keys_id, destination_script.into(), keys,
1422-
channel_parameters.clone(), initial_holder_commitment_tx, secp_ctx
1420+
channel_parameters.channel_value_satoshis, channel_keys_id, destination_script.into(),
1421+
keys, channel_parameters.clone(), initial_holder_commitment_tx, secp_ctx
14231422
);
14241423

1424+
let funding_outpoint = channel_parameters.funding_outpoint
1425+
.expect("Funding outpoint must be known during initialization");
1426+
let funding_redeem_script = channel_parameters.make_funding_redeemscript();
1427+
let funding_script = funding_redeem_script.to_p2wsh();
14251428
let mut outputs_to_watch = new_hash_map();
14261429
outputs_to_watch.insert(
14271430
funding_outpoint.txid, vec![(funding_outpoint.index as u32, funding_script.clone())],
14281431
);
14291432

14301433
Self::from_impl(ChannelMonitorImpl {
14311434
funding: FundingScope {
1432-
outpoint: funding_outpoint,
14331435
script_pubkey: funding_script,
1434-
redeem_script: funding_redeemscript,
1435-
channel_value_satoshis,
1436+
redeem_script: funding_redeem_script,
1437+
channel_parameters: channel_parameters.clone(),
14361438

14371439
current_counterparty_commitment_txid: None,
14381440
prev_counterparty_commitment_txid: None,
@@ -1663,7 +1665,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
16631665
let lock = self.inner.lock().unwrap();
16641666
let logger = WithChannelMonitor::from_impl(logger, &*lock, None);
16651667
log_trace!(&logger, "Registering funding outpoint {}", &lock.get_funding_txo());
1666-
filter.register_tx(&lock.funding.outpoint.txid, &lock.funding.script_pubkey);
1668+
let funding_outpoint = lock.get_funding_txo();
1669+
filter.register_tx(&funding_outpoint.txid, &lock.funding.script_pubkey);
16671670
for (txid, outputs) in lock.get_outputs_to_watch().iter() {
16681671
for (index, script_pubkey) in outputs.iter() {
16691672
assert!(*index <= u16::MAX as u32);
@@ -3146,18 +3149,19 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
31463149
fn generate_claimable_outpoints_and_watch_outputs(&mut self, reason: ClosureReason) -> (Vec<PackageTemplate>, Vec<TransactionOutputs>) {
31473150
let funding_outp = HolderFundingOutput::build(
31483151
self.funding.redeem_script.clone(),
3149-
self.funding.channel_value_satoshis,
3152+
self.funding.channel_parameters.channel_value_satoshis,
31503153
self.onchain_tx_handler.channel_type_features().clone()
31513154
);
3155+
let funding_outpoint = self.get_funding_txo();
31523156
let commitment_package = PackageTemplate::build_package(
3153-
self.funding.outpoint.txid.clone(), self.funding.outpoint.index as u32,
3157+
funding_outpoint.txid.clone(), funding_outpoint.index as u32,
31543158
PackageSolvingData::HolderFundingOutput(funding_outp),
31553159
self.best_block.height,
31563160
);
31573161
let mut claimable_outpoints = vec![commitment_package];
31583162
let event = MonitorEvent::HolderForceClosedWithInfo {
31593163
reason,
3160-
outpoint: self.funding.outpoint,
3164+
outpoint: funding_outpoint,
31613165
channel_id: self.channel_id,
31623166
};
31633167
self.pending_monitor_events.push(event);
@@ -3360,7 +3364,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33603364
}
33613365

33623366
fn get_funding_txo(&self) -> OutPoint {
3363-
self.funding.outpoint
3367+
self.funding.channel_parameters.funding_outpoint
3368+
.expect("Funding outpoint must be set for active monitor")
33643369
}
33653370

33663371
fn get_funding_script(&self) -> ScriptBuf {
@@ -3403,7 +3408,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
34033408
let commitment_txid = commitment_tx.compute_txid();
34043409
debug_assert_eq!(self.funding.current_holder_commitment_tx.txid, commitment_txid);
34053410
let pending_htlcs = self.funding.current_holder_commitment_tx.non_dust_htlcs();
3406-
let commitment_tx_fee_satoshis = self.funding.channel_value_satoshis -
3411+
let channel_value_satoshis = self.funding.channel_parameters.channel_value_satoshis;
3412+
let commitment_tx_fee_satoshis = channel_value_satoshis -
34073413
commitment_tx.output.iter().fold(0u64, |sum, output| sum + output.value.to_sat());
34083414
ret.push(Event::BumpTransaction(BumpTransactionEvent::ChannelClose {
34093415
channel_id,
@@ -3415,7 +3421,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
34153421
anchor_descriptor: AnchorDescriptor {
34163422
channel_derivation_parameters: ChannelDerivationParameters {
34173423
keys_id: self.channel_keys_id,
3418-
value_satoshis: self.funding.channel_value_satoshis,
3424+
value_satoshis: channel_value_satoshis,
34193425
transaction_parameters: self.onchain_tx_handler.channel_transaction_parameters.clone(),
34203426
},
34213427
outpoint: BitcoinOutPoint {
@@ -3436,7 +3442,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
34363442
htlc_descriptors.push(HTLCDescriptor {
34373443
channel_derivation_parameters: ChannelDerivationParameters {
34383444
keys_id: self.channel_keys_id,
3439-
value_satoshis: self.funding.channel_value_satoshis,
3445+
value_satoshis: self.funding.channel_parameters.channel_value_satoshis,
34403446
transaction_parameters: self.onchain_tx_handler.channel_transaction_parameters.clone(),
34413447
},
34423448
commitment_txid: htlc.commitment_txid,
@@ -4139,7 +4145,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
41394145
// (except for HTLC transactions for channels with anchor outputs), which is an easy
41404146
// way to filter out any potential non-matching txn for lazy filters.
41414147
let prevout = &tx.input[0].previous_output;
4142-
if prevout.txid == self.funding.outpoint.txid && prevout.vout == self.funding.outpoint.index as u32 {
4148+
let funding_outpoint = self.get_funding_txo();
4149+
if prevout.txid == funding_outpoint.txid && prevout.vout == funding_outpoint.index as u32 {
41434150
let mut balance_spendable_csv = None;
41444151
log_info!(logger, "Channel {} closed by funding output spend in txid {}.",
41454152
&self.channel_id(), txid);
@@ -4809,7 +4816,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
48094816
output: outp.clone(),
48104817
revocation_pubkey: broadcasted_holder_revokable_script.2,
48114818
channel_keys_id: self.channel_keys_id,
4812-
channel_value_satoshis: self.funding.channel_value_satoshis,
4819+
channel_value_satoshis: self.funding.channel_parameters.channel_value_satoshis,
48134820
channel_transaction_parameters: Some(self.onchain_tx_handler.channel_transaction_parameters.clone()),
48144821
}));
48154822
}
@@ -4819,7 +4826,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
48194826
outpoint: OutPoint { txid: tx.compute_txid(), index: i as u16 },
48204827
output: outp.clone(),
48214828
channel_keys_id: self.channel_keys_id,
4822-
channel_value_satoshis: self.funding.channel_value_satoshis,
4829+
channel_value_satoshis: self.funding.channel_parameters.channel_value_satoshis,
48234830
channel_transaction_parameters: Some(self.onchain_tx_handler.channel_transaction_parameters.clone()),
48244831
}));
48254832
}
@@ -5183,12 +5190,13 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
51835190
To continue, run a v0.1 release, send/route a payment over the channel or close it.", channel_id);
51845191
}
51855192

5193+
let channel_parameters = onchain_tx_handler.channel_transaction_parameters.clone();
5194+
51865195
Ok((best_block.block_hash, ChannelMonitor::from_impl(ChannelMonitorImpl {
51875196
funding: FundingScope {
5188-
outpoint,
51895197
script_pubkey: funding_script,
51905198
redeem_script: funding_redeemscript,
5191-
channel_value_satoshis,
5199+
channel_parameters,
51925200

51935201
current_counterparty_commitment_txid,
51945202
prev_counterparty_commitment_txid,
@@ -5481,12 +5489,11 @@ mod tests {
54815489
// old state.
54825490
let shutdown_pubkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
54835491
let shutdown_script = ShutdownScript::new_p2wpkh_from_pubkey(shutdown_pubkey);
5484-
let funding_txo = OutPoint { txid: Txid::from_slice(&[43; 32]).unwrap(), index: 0 };
54855492
let best_block = BestBlock::from_network(Network::Testnet);
54865493
let monitor = ChannelMonitor::new(
54875494
Secp256k1::new(), keys, Some(shutdown_script.into_inner()), 0, &ScriptBuf::new(),
5488-
funding_txo, ScriptBuf::new(), &channel_parameters, true, ScriptBuf::new(), 46, 0,
5489-
HolderCommitmentTransaction::dummy(0, &mut Vec::new()), best_block, dummy_key, channel_id,
5495+
&channel_parameters, true, 0, HolderCommitmentTransaction::dummy(0, &mut Vec::new()),
5496+
best_block, dummy_key, channel_id,
54905497
);
54915498

54925499
let mut htlcs = preimages_slice_to_htlcs!(preimages[0..10]);
@@ -5734,12 +5741,11 @@ mod tests {
57345741
};
57355742
let shutdown_pubkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
57365743
let shutdown_script = ShutdownScript::new_p2wpkh_from_pubkey(shutdown_pubkey);
5737-
let funding_txo = OutPoint { txid: Txid::from_slice(&[43; 32]).unwrap(), index: 0 };
57385744
let best_block = BestBlock::from_network(Network::Testnet);
57395745
let monitor = ChannelMonitor::new(
57405746
Secp256k1::new(), keys, Some(shutdown_script.into_inner()), 0, &ScriptBuf::new(),
5741-
funding_txo, ScriptBuf::new(), &channel_parameters, true, ScriptBuf::new(), 46, 0,
5742-
HolderCommitmentTransaction::dummy(0, &mut Vec::new()), best_block, dummy_key, channel_id,
5747+
&channel_parameters, true, 0, HolderCommitmentTransaction::dummy(0, &mut Vec::new()),
5748+
best_block, dummy_key, channel_id,
57435749
);
57445750

57455751
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)