Skip to content

Commit 696e505

Browse files
committed
Use Channel::funding_txo instead of its channel_monitor.funding_txo
Currently Channel relies on its own internal channel_monitor copy to keep track of funding_txo information, which is both a bit awkward and not ideal if we want to get rid of the ChannelMonitor copy in Channel. Instead, just duplicate it (its small) and keep it directly in Channel, allowing us to remove the (super awkward) ChannelMonitor::unset_funding_txo().
1 parent 5fa999d commit 696e505

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

lightning/src/chain/transaction.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ impl OutPoint {
3939
}
4040
}
4141

42+
impl_writeable!(OutPoint, 0, { txid, index });
43+
4244
#[cfg(test)]
4345
mod tests {
4446
use chain::transaction::OutPoint;

lightning/src/ln/channel.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
306306

307307
last_sent_closing_fee: Option<(u64, u64, Signature)>, // (feerate, fee, our_sig)
308308

309+
funding_txo: Option<OutPoint>,
310+
309311
/// The hash of the block in which the funding transaction reached our CONF_TARGET. We use this
310312
/// to detect unconfirmation after a serialize-unserialize roundtrip where we may not see a full
311313
/// series of block_connected/block_disconnected calls. Obviously this is not a guarantee as we
@@ -501,6 +503,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
501503

502504
last_sent_closing_fee: None,
503505

506+
funding_txo: None,
504507
funding_tx_confirmed_in: None,
505508
short_channel_id: None,
506509
last_block_connected: Default::default(),
@@ -721,6 +724,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
721724

722725
last_sent_closing_fee: None,
723726

727+
funding_txo: None,
724728
funding_tx_confirmed_in: None,
725729
short_channel_id: None,
726730
last_block_connected: Default::default(),
@@ -817,7 +821,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
817821
let txins = {
818822
let mut ins: Vec<TxIn> = Vec::new();
819823
ins.push(TxIn {
820-
previous_output: self.channel_monitor.get_funding_txo().unwrap().into_bitcoin_outpoint(),
824+
previous_output: self.funding_txo.unwrap().into_bitcoin_outpoint(),
821825
script_sig: Script::new(),
822826
sequence: ((0x80 as u32) << 8*3) | ((obscured_commitment_transaction_number >> 3*8) as u32),
823827
witness: Vec::new(),
@@ -1036,7 +1040,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
10361040
let txins = {
10371041
let mut ins: Vec<TxIn> = Vec::new();
10381042
ins.push(TxIn {
1039-
previous_output: self.channel_monitor.get_funding_txo().unwrap().into_bitcoin_outpoint(),
1043+
previous_output: self.funding_txo.unwrap().into_bitcoin_outpoint(),
10401044
script_sig: Script::new(),
10411045
sequence: 0xffffffff,
10421046
witness: Vec::new(),
@@ -1464,17 +1468,19 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14641468
}
14651469

14661470
let funding_txo = OutPoint::new(msg.funding_txid, msg.funding_output_index);
1467-
let funding_txo_script = self.get_funding_redeemscript().to_v0_p2wsh();
1468-
self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
1471+
self.funding_txo = Some(funding_txo.clone());
14691472

14701473
let (remote_initial_commitment_tx, local_initial_commitment_tx, our_signature, local_keys) = match self.funding_created_signature(&msg.signature) {
14711474
Ok(res) => res,
14721475
Err(e) => {
1473-
self.channel_monitor.unset_funding_info();
1476+
self.funding_txo = None;
14741477
return Err(e);
14751478
}
14761479
};
14771480

1481+
let funding_txo_script = self.get_funding_redeemscript().to_v0_p2wsh();
1482+
self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
1483+
14781484
// Now that we're past error-generating stuff, update our local state:
14791485

14801486
self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_initial_commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
@@ -2829,7 +2835,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
28292835
/// Returns the funding_txo we either got from our peer, or were given by
28302836
/// get_outbound_funding_created.
28312837
pub fn get_funding_txo(&self) -> Option<OutPoint> {
2832-
self.channel_monitor.get_funding_txo()
2838+
self.funding_txo
28332839
}
28342840

28352841
/// Allowed in any state (including after shutdown)
@@ -3021,8 +3027,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
30213027
}
30223028
if non_shutdown_state & !(ChannelState::TheirFundingLocked as u32) == ChannelState::FundingSent as u32 {
30233029
for (ref tx, index_in_block) in txn_matched.iter().zip(indexes_of_txn_matched) {
3024-
if tx.txid() == self.channel_monitor.get_funding_txo().unwrap().txid {
3025-
let txo_idx = self.channel_monitor.get_funding_txo().unwrap().index as usize;
3030+
if tx.txid() == self.funding_txo.unwrap().txid {
3031+
let txo_idx = self.funding_txo.unwrap().index as usize;
30263032
if txo_idx >= tx.output.len() || tx.output[txo_idx].script_pubkey != self.get_funding_redeemscript().to_v0_p2wsh() ||
30273033
tx.output[txo_idx].value != self.channel_value_satoshis {
30283034
if self.channel_outbound {
@@ -3225,18 +3231,18 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
32253231
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
32263232
}
32273233

3228-
let funding_txo_script = self.get_funding_redeemscript().to_v0_p2wsh();
3229-
self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
3230-
3234+
self.funding_txo = Some(funding_txo.clone());
32313235
let (our_signature, commitment_tx) = match self.get_outbound_funding_created_signature() {
32323236
Ok(res) => res,
32333237
Err(e) => {
32343238
log_error!(self, "Got bad signatures: {:?}!", e);
3235-
self.channel_monitor.unset_funding_info();
3239+
self.funding_txo = None;
32363240
return Err(e);
32373241
}
32383242
};
32393243

3244+
let funding_txo_script = self.get_funding_redeemscript().to_v0_p2wsh();
3245+
self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
32403246
let temporary_channel_id = self.channel_id;
32413247

32423248
// Now that we're past error-generating stuff, update our local state:
@@ -3828,6 +3834,7 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
38283834
None => 0u8.write(writer)?,
38293835
}
38303836

3837+
write_option!(self.funding_txo);
38313838
write_option!(self.funding_tx_confirmed_in);
38323839
write_option!(self.short_channel_id);
38333840

@@ -3980,6 +3987,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
39803987
_ => return Err(DecodeError::InvalidValue),
39813988
};
39823989

3990+
let funding_txo = Readable::read(reader)?;
39833991
let funding_tx_confirmed_in = Readable::read(reader)?;
39843992
let short_channel_id = Readable::read(reader)?;
39853993

@@ -4056,6 +4064,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
40564064

40574065
last_sent_closing_fee,
40584066

4067+
funding_txo,
40594068
funding_tx_confirmed_in,
40604069
short_channel_id,
40614070
last_block_connected,
@@ -4196,7 +4205,7 @@ mod tests {
41964205
chan.our_dust_limit_satoshis = 546;
41974206

41984207
let funding_info = OutPoint::new(Sha256dHash::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(), 0);
4199-
chan.channel_monitor.set_funding_info((funding_info, Script::new()));
4208+
chan.funding_txo = Some(funding_info);
42004209

42014210
let their_pubkeys = ChannelPublicKeys {
42024211
funding_pubkey: public_from_secret_hex(&secp_ctx, "1552dfba4f6cf29a62a0af13c8d6981d36d0ef8d61ba10fb0fe90da7634d7e13"),

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3527,7 +3527,7 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref> R
35273527
return Err(DecodeError::InvalidValue);
35283528
}
35293529

3530-
let funding_txo = channel.channel_monitor().get_funding_txo().ok_or(DecodeError::InvalidValue)?;
3530+
let funding_txo = channel.get_funding_txo().ok_or(DecodeError::InvalidValue)?;
35313531
funding_txo_set.insert(funding_txo.clone());
35323532
if let Some(ref mut monitor) = args.channel_monitors.get_mut(&funding_txo) {
35333533
if channel.get_cur_local_commitment_transaction_number() != monitor.get_cur_local_commitment_number() ||

lightning/src/ln/channelmonitor.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,8 +1284,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12841284

12851285
/// Allows this monitor to scan only for transactions which are applicable. Note that this is
12861286
/// optional, without it this monitor cannot be used in an SPV client, but you may wish to
1287-
/// avoid this (or call unset_funding_info) on a monitor you wish to send to a watchtower as it
1288-
/// provides slightly better privacy.
1287+
/// avoid this on a monitor you wish to send to a watchtower as it provides slightly better
1288+
/// privacy.
12891289
/// It's the responsibility of the caller to register outpoint and script with passing the former
12901290
/// value as key to add_update_monitor.
12911291
pub(super) fn set_funding_info(&mut self, new_funding_info: (OutPoint, Script)) {
@@ -1311,17 +1311,6 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
13111311
self.commitment_transaction_number_obscure_factor = commitment_transaction_number_obscure_factor;
13121312
}
13131313

1314-
pub(super) fn unset_funding_info(&mut self) {
1315-
match self.key_storage {
1316-
Storage::Local { ref mut funding_info, .. } => {
1317-
*funding_info = None;
1318-
},
1319-
Storage::Watchtower { .. } => {
1320-
panic!("Channel somehow ended up with its internal ChannelMonitor being in Watchtower mode?");
1321-
},
1322-
}
1323-
}
1324-
13251314
/// Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
13261315
pub fn get_funding_txo(&self) -> Option<OutPoint> {
13271316
match self.key_storage {

0 commit comments

Comments
 (0)