Skip to content

Commit 3a75881

Browse files
committed
f un-option chan creation height and fix comments and sp
1 parent 3e5b410 commit 3a75881

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

lightning/src/ln/channel.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,10 @@ pub(super) struct Channel<Signer: Sign> {
457457
funding_tx_confirmed_in: Option<BlockHash>,
458458
funding_tx_confirmation_height: u32,
459459
short_channel_id: Option<u64>,
460-
/// If this channel was created in recent software, this will be set to the height at which
461-
/// channel creation was intiated. This is used to close if funding is never broadcasted.
462-
channel_creation_height: Option<u32>,
460+
/// Either the height at which this channel was created or the height at which it was last
461+
/// serialized by older software.
462+
/// We use this to close if funding is never broadcasted.
463+
channel_creation_height: u32,
463464

464465
counterparty_dust_limit_satoshis: u64,
465466
#[cfg(test)]
@@ -715,7 +716,7 @@ impl<Signer: Sign> Channel<Signer> {
715716
funding_tx_confirmed_in: None,
716717
funding_tx_confirmation_height: 0,
717718
short_channel_id: None,
718-
channel_creation_height: Some(current_chain_heigt),
719+
channel_creation_height: current_chain_heigt,
719720

720721
feerate_per_kw: feerate,
721722
counterparty_dust_limit_satoshis: 0,
@@ -982,7 +983,7 @@ impl<Signer: Sign> Channel<Signer> {
982983
funding_tx_confirmed_in: None,
983984
funding_tx_confirmation_height: 0,
984985
short_channel_id: None,
985-
channel_creation_height: Some(current_chain_heigt),
986+
channel_creation_height: current_chain_heigt,
986987

987988
feerate_per_kw: msg.feerate_per_kw,
988989
channel_value_satoshis: msg.funding_satoshis,
@@ -4148,18 +4149,16 @@ impl<Signer: Sign> Channel<Signer> {
41484149
data: format!("Funding transaction was un-confirmed. Locked at {} confs, now have {} confs.", self.minimum_depth.unwrap(), funding_tx_confirmations),
41494150
});
41504151
}
4151-
} else if let Some(creation_height) = self.channel_creation_height {
4152-
if !self.is_outbound() && self.funding_tx_confirmed_in.is_none() &&
4153-
creation_height + FUNDING_CONF_DEADLINE_BLOCKS <= height {
4154-
log_info!(logger, "Closing channel {} due to funding timeout", log_bytes!(self.channel_id));
4155-
// If funding_tx_confirmed_in is unset, the channel must not be active
4156-
assert!(non_shutdown_state <= ChannelState::ChannelFunded as u32);
4157-
assert_eq!(non_shutdown_state & ChannelState::OurFundingLocked as u32, 0);
4158-
return Err(msgs::ErrorMessage {
4159-
channel_id: self.channel_id(),
4160-
data: format!("Funding transaction failed to confirm in {} blocks", FUNDING_CONF_DEADLINE_BLOCKS),
4161-
});
4162-
}
4152+
} else if !self.is_outbound() && self.funding_tx_confirmed_in.is_none() &&
4153+
self.channel_creation_height + FUNDING_CONF_DEADLINE_BLOCKS <= height {
4154+
log_info!(logger, "Closing channel {} due to funding timeout", log_bytes!(self.channel_id));
4155+
// If funding_tx_confirmed_in is unset, the channel must not be active
4156+
assert!(non_shutdown_state <= ChannelState::ChannelFunded as u32);
4157+
assert_eq!(non_shutdown_state & ChannelState::OurFundingLocked as u32, 0);
4158+
return Err(msgs::ErrorMessage {
4159+
channel_id: self.channel_id(),
4160+
data: format!("Funding transaction failed to confirm within {} blocks", FUNDING_CONF_DEADLINE_BLOCKS),
4161+
});
41634162
}
41644163

41654164
Ok((None, timed_out_htlcs))
@@ -5188,17 +5187,18 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
51885187
(5, self.config, required),
51895188
(7, self.shutdown_scriptpubkey, option),
51905189
(9, self.target_closing_feerate_sats_per_kw, option),
5191-
(11, self.channel_creation_height, option),
5190+
(11, self.channel_creation_height, required),
51925191
});
51935192

51945193
Ok(())
51955194
}
51965195
}
51975196

51985197
const MAX_ALLOC_SIZE: usize = 64*1024;
5199-
impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5198+
impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
52005199
where K::Target: KeysInterface<Signer = Signer> {
5201-
fn read<R : io::Read>(reader: &mut R, keys_source: &'a K) -> Result<Self, DecodeError> {
5200+
fn read<R : io::Read>(reader: &mut R, args: (&'a K, u32)) -> Result<Self, DecodeError> {
5201+
let (keys_source, serialized_height) = args;
52025202
let ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
52035203

52045204
let user_id = Readable::read(reader)?;
@@ -5422,7 +5422,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
54225422

54235423
let mut announcement_sigs = None;
54245424
let mut target_closing_feerate_sats_per_kw = None;
5425-
let mut channel_creation_height = None;
5425+
let mut channel_creation_height = Some(serialized_height);
54265426
read_tlv_fields!(reader, {
54275427
(0, announcement_sigs, option),
54285428
(1, minimum_depth, option),
@@ -5487,7 +5487,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
54875487
funding_tx_confirmed_in,
54885488
funding_tx_confirmation_height,
54895489
short_channel_id,
5490-
channel_creation_height,
5490+
channel_creation_height: channel_creation_height.unwrap(),
54915491

54925492
counterparty_dust_limit_satoshis,
54935493
holder_dust_limit_satoshis,

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5357,7 +5357,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
53575357
let mut by_id = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
53585358
let mut short_to_id = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
53595359
for _ in 0..channel_count {
5360-
let mut channel: Channel<Signer> = Channel::read(reader, &args.keys_manager)?;
5360+
let mut channel: Channel<Signer> = Channel::read(reader, (&args.keys_manager, best_block_height))?;
53615361
let funding_txo = channel.get_funding_txo().ok_or(DecodeError::InvalidValue)?;
53625362
funding_txo_set.insert(funding_txo.clone());
53635363
if let Some(ref mut monitor) = args.channel_monitors.get_mut(&funding_txo) {

lightning/src/ln/functional_tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8029,6 +8029,8 @@ fn test_channel_conf_timeout() {
80298029
let _funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 100_000, InitFeatures::known(), InitFeatures::known());
80308030

80318031
// The outbound node should wait forever for confirmation:
8032+
// This matches `channel::FUNDING_CONF_DEADLINE_BLOCKS` and BOLT 2's suggested timeout, thus is
8033+
// copied here instead of directly referencing the constant.
80328034
connect_blocks(&nodes[0], 2016);
80338035
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
80348036

@@ -8044,7 +8046,7 @@ fn test_channel_conf_timeout() {
80448046
match close_ev[0] {
80458047
MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, ref node_id } => {
80468048
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8047-
assert_eq!(msg.data, "Funding transaction failed to confirm in 2016 blocks");
8049+
assert_eq!(msg.data, "Funding transaction failed to confirm within 2016 blocks");
80488050
},
80498051
_ => panic!("Unexpected event"),
80508052
}

0 commit comments

Comments
 (0)