Skip to content

Commit 6cf279b

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 d905268 commit 6cf279b

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
@@ -303,6 +303,8 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
303303

304304
last_sent_closing_fee: Option<(u64, u64, Signature)>, // (feerate, fee, our_sig)
305305

306+
funding_txo: Option<OutPoint>,
307+
306308
/// The hash of the block in which the funding transaction reached our CONF_TARGET. We use this
307309
/// to detect unconfirmation after a serialize-unserialize roundtrip where we may not see a full
308310
/// series of block_connected/block_disconnected calls. Obviously this is not a guarantee as we
@@ -498,6 +500,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
498500

499501
last_sent_closing_fee: None,
500502

503+
funding_txo: None,
501504
funding_tx_confirmed_in: None,
502505
short_channel_id: None,
503506
last_block_connected: Default::default(),
@@ -718,6 +721,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
718721

719722
last_sent_closing_fee: None,
720723

724+
funding_txo: None,
721725
funding_tx_confirmed_in: None,
722726
short_channel_id: None,
723727
last_block_connected: Default::default(),
@@ -814,7 +818,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
814818
let txins = {
815819
let mut ins: Vec<TxIn> = Vec::new();
816820
ins.push(TxIn {
817-
previous_output: self.channel_monitor.get_funding_txo().unwrap().into_bitcoin_outpoint(),
821+
previous_output: self.funding_txo.unwrap().into_bitcoin_outpoint(),
818822
script_sig: Script::new(),
819823
sequence: ((0x80 as u32) << 8*3) | ((obscured_commitment_transaction_number >> 3*8) as u32),
820824
witness: Vec::new(),
@@ -1033,7 +1037,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
10331037
let txins = {
10341038
let mut ins: Vec<TxIn> = Vec::new();
10351039
ins.push(TxIn {
1036-
previous_output: self.channel_monitor.get_funding_txo().unwrap().into_bitcoin_outpoint(),
1040+
previous_output: self.funding_txo.unwrap().into_bitcoin_outpoint(),
10371041
script_sig: Script::new(),
10381042
sequence: 0xffffffff,
10391043
witness: Vec::new(),
@@ -1461,17 +1465,19 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14611465
}
14621466

14631467
let funding_txo = OutPoint::new(msg.funding_txid, msg.funding_output_index);
1464-
let funding_txo_script = self.get_funding_redeemscript().to_v0_p2wsh();
1465-
self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
1468+
self.funding_txo = Some(funding_txo.clone());
14661469

14671470
let (remote_initial_commitment_tx, local_initial_commitment_tx, our_signature, local_keys) = match self.funding_created_signature(&msg.signature) {
14681471
Ok(res) => res,
14691472
Err(e) => {
1470-
self.channel_monitor.unset_funding_info();
1473+
self.funding_txo = None;
14711474
return Err(e);
14721475
}
14731476
};
14741477

1478+
let funding_txo_script = self.get_funding_redeemscript().to_v0_p2wsh();
1479+
self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
1480+
14751481
// Now that we're past error-generating stuff, update our local state:
14761482

14771483
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());
@@ -2825,7 +2831,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
28252831
/// Returns the funding_txo we either got from our peer, or were given by
28262832
/// get_outbound_funding_created.
28272833
pub fn get_funding_txo(&self) -> Option<OutPoint> {
2828-
self.channel_monitor.get_funding_txo()
2834+
self.funding_txo
28292835
}
28302836

28312837
/// Allowed in any state (including after shutdown)
@@ -3005,8 +3011,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
30053011
}
30063012
if non_shutdown_state & !(ChannelState::TheirFundingLocked as u32) == ChannelState::FundingSent as u32 {
30073013
for (ref tx, index_in_block) in txn_matched.iter().zip(indexes_of_txn_matched) {
3008-
if tx.txid() == self.channel_monitor.get_funding_txo().unwrap().txid {
3009-
let txo_idx = self.channel_monitor.get_funding_txo().unwrap().index as usize;
3014+
if tx.txid() == self.funding_txo.unwrap().txid {
3015+
let txo_idx = self.funding_txo.unwrap().index as usize;
30103016
if txo_idx >= tx.output.len() || tx.output[txo_idx].script_pubkey != self.get_funding_redeemscript().to_v0_p2wsh() ||
30113017
tx.output[txo_idx].value != self.channel_value_satoshis {
30123018
if self.channel_outbound {
@@ -3209,18 +3215,18 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
32093215
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
32103216
}
32113217

3212-
let funding_txo_script = self.get_funding_redeemscript().to_v0_p2wsh();
3213-
self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
3214-
3218+
self.funding_txo = Some(funding_txo.clone());
32153219
let (our_signature, commitment_tx) = match self.get_outbound_funding_created_signature() {
32163220
Ok(res) => res,
32173221
Err(e) => {
32183222
log_error!(self, "Got bad signatures: {:?}!", e);
3219-
self.channel_monitor.unset_funding_info();
3223+
self.funding_txo = None;
32203224
return Err(e);
32213225
}
32223226
};
32233227

3228+
let funding_txo_script = self.get_funding_redeemscript().to_v0_p2wsh();
3229+
self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
32243230
let temporary_channel_id = self.channel_id;
32253231

32263232
// Now that we're past error-generating stuff, update our local state:
@@ -3815,6 +3821,7 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
38153821
None => 0u8.write(writer)?,
38163822
}
38173823

3824+
write_option!(self.funding_txo);
38183825
write_option!(self.funding_tx_confirmed_in);
38193826
write_option!(self.short_channel_id);
38203827

@@ -3967,6 +3974,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
39673974
_ => return Err(DecodeError::InvalidValue),
39683975
};
39693976

3977+
let funding_txo = Readable::read(reader)?;
39703978
let funding_tx_confirmed_in = Readable::read(reader)?;
39713979
let short_channel_id = Readable::read(reader)?;
39723980

@@ -4043,6 +4051,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
40434051

40444052
last_sent_closing_fee,
40454053

4054+
funding_txo,
40464055
funding_tx_confirmed_in,
40474056
short_channel_id,
40484057
last_block_connected,
@@ -4183,7 +4192,7 @@ mod tests {
41834192
chan.our_dust_limit_satoshis = 546;
41844193

41854194
let funding_info = OutPoint::new(Sha256dHash::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(), 0);
4186-
chan.channel_monitor.set_funding_info((funding_info, Script::new()));
4195+
chan.funding_txo = Some(funding_info);
41874196

41884197
let their_pubkeys = ChannelPublicKeys {
41894198
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
@@ -3318,7 +3318,7 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref> R
33183318
return Err(DecodeError::InvalidValue);
33193319
}
33203320

3321-
let funding_txo = channel.channel_monitor().get_funding_txo().ok_or(DecodeError::InvalidValue)?;
3321+
let funding_txo = channel.get_funding_txo().ok_or(DecodeError::InvalidValue)?;
33223322
funding_txo_set.insert(funding_txo.clone());
33233323
if let Some(ref mut monitor) = args.channel_monitors.get_mut(&funding_txo) {
33243324
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)