Skip to content

Commit 78c48f7

Browse files
committed
Use block timestamps as the min for generated update messages.
Fixes issue #493 and should resolve some issues where other nodes (incorrectly) reject channel_update/node_announcement messages which have a serial number that is not a relatively recent timestamp.
1 parent c2ca6d3 commit 78c48f7

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

lightning/src/ln/channel.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
295295
holding_cell_update_fee: Option<u64>,
296296
next_local_htlc_id: u64,
297297
next_remote_htlc_id: u64,
298-
channel_update_count: u32,
298+
update_time_counter: u32,
299299
feerate_per_kw: u64,
300300

301301
#[cfg(debug_assertions)]
@@ -490,7 +490,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
490490
holding_cell_update_fee: None,
491491
next_local_htlc_id: 0,
492492
next_remote_htlc_id: 0,
493-
channel_update_count: 1,
493+
update_time_counter: 1,
494494

495495
resend_order: RAACommitmentOrder::CommitmentFirst,
496496

@@ -714,7 +714,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
714714
holding_cell_update_fee: None,
715715
next_local_htlc_id: 0,
716716
next_remote_htlc_id: 0,
717-
channel_update_count: 1,
717+
update_time_counter: 1,
718718

719719
resend_order: RAACommitmentOrder::CommitmentFirst,
720720

@@ -1586,7 +1586,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
15861586
self.channel_state |= ChannelState::TheirFundingLocked as u32;
15871587
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) {
15881588
self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS);
1589-
self.channel_update_count += 1;
1589+
self.update_time_counter += 1;
15901590
} else if (self.channel_state & (ChannelState::ChannelFunded as u32) != 0 &&
15911591
// Note that funding_signed/funding_created will have decremented both by 1!
15921592
self.cur_local_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 &&
@@ -2480,7 +2480,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
24802480
}
24812481
Channel::<ChanSigner>::check_remote_fee(fee_estimator, msg.feerate_per_kw)?;
24822482
self.pending_update_fee = Some(msg.feerate_per_kw as u64);
2483-
self.channel_update_count += 1;
2483+
self.update_time_counter += 1;
24842484
Ok(())
24852485
}
24862486

@@ -2763,7 +2763,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
27632763
// From here on out, we may not fail!
27642764

27652765
self.channel_state |= ChannelState::RemoteShutdownSent as u32;
2766-
self.channel_update_count += 1;
2766+
self.update_time_counter += 1;
27672767

27682768
// We can't send our shutdown until we've committed all of our pending HTLCs, but the
27692769
// remote side is unlikely to accept any new HTLCs, so we go ahead and "free" any holding
@@ -2793,7 +2793,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
27932793
};
27942794

27952795
self.channel_state |= ChannelState::LocalShutdownSent as u32;
2796-
self.channel_update_count += 1;
2796+
self.update_time_counter += 1;
27972797

27982798
Ok((our_shutdown, self.maybe_propose_first_closing_signed(fee_estimator), dropped_outbound_htlcs))
27992799
}
@@ -2860,7 +2860,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
28602860
if last_fee == msg.fee_satoshis {
28612861
self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &our_sig);
28622862
self.channel_state = ChannelState::ShutdownComplete as u32;
2863-
self.channel_update_count += 1;
2863+
self.update_time_counter += 1;
28642864
return Ok((None, Some(closing_tx)));
28652865
}
28662866
}
@@ -2910,7 +2910,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
29102910
self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &our_sig);
29112911

29122912
self.channel_state = ChannelState::ShutdownComplete as u32;
2913-
self.channel_update_count += 1;
2913+
self.update_time_counter += 1;
29142914

29152915
Ok((Some(msgs::ClosingSigned {
29162916
channel_id: self.channel_id,
@@ -3022,8 +3022,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
30223022
}
30233023

30243024
/// Allowed in any state (including after shutdown)
3025-
pub fn get_channel_update_count(&self) -> u32 {
3026-
self.channel_update_count
3025+
pub fn get_update_time_counter(&self) -> u32 {
3026+
self.update_time_counter
30273027
}
30283028

30293029
pub fn get_latest_monitor_update_id(&self) -> u64 {
@@ -3149,7 +3149,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
31493149
panic!("Client called ChannelManager::funding_transaction_generated with bogus transaction!");
31503150
}
31513151
self.channel_state = ChannelState::ShutdownComplete as u32;
3152-
self.channel_update_count += 1;
3152+
self.update_time_counter += 1;
31533153
return Err(msgs::ErrorMessage {
31543154
channel_id: self.channel_id(),
31553155
data: "funding tx had wrong script/value".to_owned()
@@ -3175,6 +3175,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
31753175
}
31763176
if header.bitcoin_hash() != self.last_block_connected {
31773177
self.last_block_connected = header.bitcoin_hash();
3178+
self.update_time_counter = cmp::max(self.update_time_counter, header.time);
31783179
if let Some(channel_monitor) = self.channel_monitor.as_mut() {
31793180
channel_monitor.last_block_hash = self.last_block_connected;
31803181
}
@@ -3185,7 +3186,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
31853186
true
31863187
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::TheirFundingLocked as u32) {
31873188
self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS);
3188-
self.channel_update_count += 1;
3189+
self.update_time_counter += 1;
31893190
true
31903191
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) {
31913192
// We got a reorg but not enough to trigger a force close, just update
@@ -3728,7 +3729,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
37283729
} else {
37293730
self.channel_state |= ChannelState::LocalShutdownSent as u32;
37303731
}
3731-
self.channel_update_count += 1;
3732+
self.update_time_counter += 1;
37323733

37333734
// Go ahead and drop holding cell updates as we'd rather fail payments than wait to send
37343735
// our shutdown until we've committed all of the pending changes.
@@ -3777,7 +3778,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
37773778
}
37783779

37793780
self.channel_state = ChannelState::ShutdownComplete as u32;
3780-
self.channel_update_count += 1;
3781+
self.update_time_counter += 1;
37813782
if self.channel_monitor.is_some() {
37823783
(self.channel_monitor.as_mut().unwrap().get_latest_local_commitment_txn(), dropped_outbound_htlcs)
37833784
} else {
@@ -3964,7 +3965,7 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
39643965

39653966
self.next_local_htlc_id.write(writer)?;
39663967
(self.next_remote_htlc_id - dropped_inbound_htlcs).write(writer)?;
3967-
self.channel_update_count.write(writer)?;
3968+
self.update_time_counter.write(writer)?;
39683969
self.feerate_per_kw.write(writer)?;
39693970

39703971
match self.last_sent_closing_fee {
@@ -4124,7 +4125,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for Channel<C
41244125

41254126
let next_local_htlc_id = Readable::read(reader)?;
41264127
let next_remote_htlc_id = Readable::read(reader)?;
4127-
let channel_update_count = Readable::read(reader)?;
4128+
let update_time_counter = Readable::read(reader)?;
41284129
let feerate_per_kw = Readable::read(reader)?;
41294130

41304131
let last_sent_closing_fee = match <u8 as Readable>::read(reader)? {
@@ -4203,7 +4204,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for Channel<C
42034204
holding_cell_update_fee,
42044205
next_local_htlc_id,
42054206
next_remote_htlc_id,
4206-
channel_update_count,
4207+
update_time_counter,
42074208
feerate_per_kw,
42084209

42094210
#[cfg(debug_assertions)]

lightning/src/ln/channelmanager.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
11241124
let unsigned = msgs::UnsignedChannelUpdate {
11251125
chain_hash: self.genesis_hash,
11261126
short_channel_id: short_channel_id,
1127-
timestamp: chan.get_channel_update_count(),
1127+
timestamp: chan.get_update_time_counter(),
11281128
flags: (!were_node_one) as u16 | ((!chan.is_live() as u16) << 1),
11291129
cltv_expiry_delta: CLTV_EXPIRY_DELTA,
11301130
htlc_minimum_msat: chan.get_our_htlc_minimum_msat(),
@@ -2776,6 +2776,18 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
27762776
}
27772777
self.latest_block_height.store(height as usize, Ordering::Release);
27782778
*self.last_block_hash.try_lock().expect("block_(dis)connected must not be called in parallel") = header_hash;
2779+
loop {
2780+
// Update last_node_announcement_serial to be the max of its current value and the
2781+
// block timestamp. This should keep us close to the current time without relying on
2782+
// having an explicit local time source.
2783+
// Just in case we end up in a race, we loop until we either successfully update
2784+
// last_node_announcement_serial or decide we don't need to.
2785+
let old_serial = self.last_node_announcement_serial.load(Ordering::Acquire);
2786+
if old_serial >= header.time as usize { break; }
2787+
if self.last_node_announcement_serial.compare_exchange(old_serial, header.time as usize, Ordering::AcqRel, Ordering::Relaxed).is_ok() {
2788+
break;
2789+
}
2790+
}
27792791
}
27802792

27812793
/// We force-close the channel without letting our counterparty participate in the shutdown

0 commit comments

Comments
 (0)