Skip to content

Commit 2ff3a90

Browse files
authored
Merge branch 'main' into 2024-04-ext-test
2 parents 21098e6 + c9a7bfe commit 2ff3a90

File tree

12 files changed

+2020
-201
lines changed

12 files changed

+2020
-201
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 78 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,9 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
981981
lock_fundings!(nodes);
982982

983983
let chan_a = nodes[0].list_usable_channels()[0].short_channel_id.unwrap();
984+
let chan_a_id = nodes[0].list_usable_channels()[0].channel_id;
984985
let chan_b = nodes[2].list_usable_channels()[0].short_channel_id.unwrap();
986+
let chan_b_id = nodes[2].list_usable_channels()[0].channel_id;
985987

986988
let mut p_id: u8 = 0;
987989
let mut p_idx: u64 = 0;
@@ -1042,6 +1044,10 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
10421044
if Some(*node_id) == expect_drop_id { panic!("peer_disconnected should drop msgs bound for the disconnected peer"); }
10431045
*node_id == a_id
10441046
},
1047+
events::MessageSendEvent::SendStfu { ref node_id, .. } => {
1048+
if Some(*node_id) == expect_drop_id { panic!("peer_disconnected should drop msgs bound for the disconnected peer"); }
1049+
*node_id == a_id
1050+
},
10451051
events::MessageSendEvent::SendChannelReady { .. } => continue,
10461052
events::MessageSendEvent::SendAnnouncementSignatures { .. } => continue,
10471053
events::MessageSendEvent::SendChannelUpdate { ref node_id, ref msg } => {
@@ -1104,7 +1110,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
11041110
for (idx, dest) in nodes.iter().enumerate() {
11051111
if dest.get_our_node_id() == node_id {
11061112
for update_add in update_add_htlcs.iter() {
1107-
out.locked_write(format!("Delivering update_add_htlc to node {}.\n", idx).as_bytes());
1113+
out.locked_write(format!("Delivering update_add_htlc from node {} to node {}.\n", $node, idx).as_bytes());
11081114
if !$corrupt_forward {
11091115
dest.handle_update_add_htlc(nodes[$node].get_our_node_id(), update_add);
11101116
} else {
@@ -1119,19 +1125,19 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
11191125
}
11201126
}
11211127
for update_fulfill in update_fulfill_htlcs.iter() {
1122-
out.locked_write(format!("Delivering update_fulfill_htlc to node {}.\n", idx).as_bytes());
1128+
out.locked_write(format!("Delivering update_fulfill_htlc from node {} to node {}.\n", $node, idx).as_bytes());
11231129
dest.handle_update_fulfill_htlc(nodes[$node].get_our_node_id(), update_fulfill);
11241130
}
11251131
for update_fail in update_fail_htlcs.iter() {
1126-
out.locked_write(format!("Delivering update_fail_htlc to node {}.\n", idx).as_bytes());
1132+
out.locked_write(format!("Delivering update_fail_htlc from node {} to node {}.\n", $node, idx).as_bytes());
11271133
dest.handle_update_fail_htlc(nodes[$node].get_our_node_id(), update_fail);
11281134
}
11291135
for update_fail_malformed in update_fail_malformed_htlcs.iter() {
1130-
out.locked_write(format!("Delivering update_fail_malformed_htlc to node {}.\n", idx).as_bytes());
1136+
out.locked_write(format!("Delivering update_fail_malformed_htlc from node {} to node {}.\n", $node, idx).as_bytes());
11311137
dest.handle_update_fail_malformed_htlc(nodes[$node].get_our_node_id(), update_fail_malformed);
11321138
}
11331139
if let Some(msg) = update_fee {
1134-
out.locked_write(format!("Delivering update_fee to node {}.\n", idx).as_bytes());
1140+
out.locked_write(format!("Delivering update_fee from node {} to node {}.\n", $node, idx).as_bytes());
11351141
dest.handle_update_fee(nodes[$node].get_our_node_id(), &msg);
11361142
}
11371143
let processed_change = !update_add_htlcs.is_empty() || !update_fulfill_htlcs.is_empty() ||
@@ -1148,7 +1154,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
11481154
} });
11491155
break;
11501156
}
1151-
out.locked_write(format!("Delivering commitment_signed to node {}.\n", idx).as_bytes());
1157+
out.locked_write(format!("Delivering commitment_signed from node {} to node {}.\n", $node, idx).as_bytes());
11521158
dest.handle_commitment_signed(nodes[$node].get_our_node_id(), &commitment_signed);
11531159
break;
11541160
}
@@ -1157,19 +1163,27 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
11571163
events::MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
11581164
for (idx, dest) in nodes.iter().enumerate() {
11591165
if dest.get_our_node_id() == *node_id {
1160-
out.locked_write(format!("Delivering revoke_and_ack to node {}.\n", idx).as_bytes());
1166+
out.locked_write(format!("Delivering revoke_and_ack from node {} to node {}.\n", $node, idx).as_bytes());
11611167
dest.handle_revoke_and_ack(nodes[$node].get_our_node_id(), msg);
11621168
}
11631169
}
11641170
},
11651171
events::MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } => {
11661172
for (idx, dest) in nodes.iter().enumerate() {
11671173
if dest.get_our_node_id() == *node_id {
1168-
out.locked_write(format!("Delivering channel_reestablish to node {}.\n", idx).as_bytes());
1174+
out.locked_write(format!("Delivering channel_reestablish from node {} to node {}.\n", $node, idx).as_bytes());
11691175
dest.handle_channel_reestablish(nodes[$node].get_our_node_id(), msg);
11701176
}
11711177
}
11721178
},
1179+
events::MessageSendEvent::SendStfu { ref node_id, ref msg } => {
1180+
for (idx, dest) in nodes.iter().enumerate() {
1181+
if dest.get_our_node_id() == *node_id {
1182+
out.locked_write(format!("Delivering stfu from node {} to node {}.\n", $node, idx).as_bytes());
1183+
dest.handle_stfu(nodes[$node].get_our_node_id(), msg);
1184+
}
1185+
}
1186+
}
11731187
events::MessageSendEvent::SendChannelReady { .. } => {
11741188
// Can be generated as a reestablish response
11751189
},
@@ -1222,6 +1236,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
12221236
events::MessageSendEvent::UpdateHTLCs { .. } => {},
12231237
events::MessageSendEvent::SendRevokeAndACK { .. } => {},
12241238
events::MessageSendEvent::SendChannelReestablish { .. } => {},
1239+
events::MessageSendEvent::SendStfu { .. } => {},
12251240
events::MessageSendEvent::SendChannelReady { .. } => {},
12261241
events::MessageSendEvent::SendAnnouncementSignatures { .. } => {},
12271242
events::MessageSendEvent::SendChannelUpdate { ref msg, .. } => {
@@ -1248,6 +1263,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
12481263
events::MessageSendEvent::UpdateHTLCs { .. } => {},
12491264
events::MessageSendEvent::SendRevokeAndACK { .. } => {},
12501265
events::MessageSendEvent::SendChannelReestablish { .. } => {},
1266+
events::MessageSendEvent::SendStfu { .. } => {},
12511267
events::MessageSendEvent::SendChannelReady { .. } => {},
12521268
events::MessageSendEvent::SendAnnouncementSignatures { .. } => {},
12531269
events::MessageSendEvent::SendChannelUpdate { ref msg, .. } => {
@@ -1691,6 +1707,19 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
16911707
nodes[2].maybe_update_chan_fees();
16921708
},
16931709

1710+
0xa0 => {
1711+
nodes[0].maybe_propose_quiescence(&nodes[1].get_our_node_id(), &chan_a_id).unwrap()
1712+
},
1713+
0xa1 => {
1714+
nodes[1].maybe_propose_quiescence(&nodes[0].get_our_node_id(), &chan_a_id).unwrap()
1715+
},
1716+
0xa2 => {
1717+
nodes[1].maybe_propose_quiescence(&nodes[2].get_our_node_id(), &chan_b_id).unwrap()
1718+
},
1719+
0xa3 => {
1720+
nodes[2].maybe_propose_quiescence(&nodes[1].get_our_node_id(), &chan_b_id).unwrap()
1721+
},
1722+
16941723
0xf0 => complete_monitor_update(&monitor_a, &chan_1_id, &complete_first),
16951724
0xf1 => complete_monitor_update(&monitor_a, &chan_1_id, &complete_second),
16961725
0xf2 => complete_monitor_update(&monitor_a, &chan_1_id, &Vec::pop),
@@ -1756,34 +1785,49 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
17561785
chan_b_disconnected = false;
17571786
}
17581787

1759-
for i in 0..std::usize::MAX {
1760-
if i == 100 {
1761-
panic!("It may take may iterations to settle the state, but it should not take forever");
1762-
}
1763-
// Then, make sure any current forwards make their way to their destination
1764-
if process_msg_events!(0, false, ProcessMessages::AllMessages) {
1765-
continue;
1766-
}
1767-
if process_msg_events!(1, false, ProcessMessages::AllMessages) {
1768-
continue;
1769-
}
1770-
if process_msg_events!(2, false, ProcessMessages::AllMessages) {
1771-
continue;
1772-
}
1773-
// ...making sure any pending PendingHTLCsForwardable events are handled and
1774-
// payments claimed.
1775-
if process_events!(0, false) {
1776-
continue;
1777-
}
1778-
if process_events!(1, false) {
1779-
continue;
1780-
}
1781-
if process_events!(2, false) {
1782-
continue;
1783-
}
1784-
break;
1788+
macro_rules! process_all_events {
1789+
() => {
1790+
for i in 0..std::usize::MAX {
1791+
if i == 100 {
1792+
panic!("It may take may iterations to settle the state, but it should not take forever");
1793+
}
1794+
// Then, make sure any current forwards make their way to their destination
1795+
if process_msg_events!(0, false, ProcessMessages::AllMessages) {
1796+
continue;
1797+
}
1798+
if process_msg_events!(1, false, ProcessMessages::AllMessages) {
1799+
continue;
1800+
}
1801+
if process_msg_events!(2, false, ProcessMessages::AllMessages) {
1802+
continue;
1803+
}
1804+
// ...making sure any pending PendingHTLCsForwardable events are handled and
1805+
// payments claimed.
1806+
if process_events!(0, false) {
1807+
continue;
1808+
}
1809+
if process_events!(1, false) {
1810+
continue;
1811+
}
1812+
if process_events!(2, false) {
1813+
continue;
1814+
}
1815+
break;
1816+
}
1817+
};
17851818
}
17861819

1820+
// At this point, we may be pending quiescence, so we'll process all messages to
1821+
// ensure we can complete its handshake. We'll then exit quiescence and process all
1822+
// messages again, to resolve any pending HTLCs (only irrevocably committed ones)
1823+
// before attempting to send more payments.
1824+
process_all_events!();
1825+
nodes[0].exit_quiescence(&nodes[1].get_our_node_id(), &chan_a_id).unwrap();
1826+
nodes[1].exit_quiescence(&nodes[0].get_our_node_id(), &chan_a_id).unwrap();
1827+
nodes[1].exit_quiescence(&nodes[2].get_our_node_id(), &chan_b_id).unwrap();
1828+
nodes[2].exit_quiescence(&nodes[1].get_our_node_id(), &chan_b_id).unwrap();
1829+
process_all_events!();
1830+
17871831
// Finally, make sure that at least one end of each channel can make a substantial payment
17881832
assert!(
17891833
send_payment(&nodes[0], &nodes[1], chan_a, 10_000_000, &mut p_id, &mut p_idx)

lightning-types/src/features.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
//! (see [bLIP 32](https://github.com/lightning/blips/blob/master/blip-0032.md) for more information).
7575
//! - `ProvideStorage` - Indicates that we offer the capability to store data of our peers
7676
//! (see https://github.com/lightning/bolts/pull/1110 for more info).
77+
//! - `Quiescence` - protocol to quiesce a channel by indicating that "SomeThing Fundamental is Underway"
78+
//! (see [BOLT-2](https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#channel-quiescence) for more information).
7779
//!
7880
//! LDK knows about the following features, but does not support them:
7981
//! - `AnchorsNonzeroFeeHtlcTx` - the initial version of anchor outputs, which was later found to be
@@ -152,7 +154,7 @@ mod sealed {
152154
// Byte 3
153155
RouteBlinding | ShutdownAnySegwit | DualFund | Taproot,
154156
// Byte 4
155-
OnionMessages,
157+
Quiescence | OnionMessages,
156158
// Byte 5
157159
ProvideStorage | ChannelType | SCIDPrivacy,
158160
// Byte 6
@@ -173,7 +175,7 @@ mod sealed {
173175
// Byte 3
174176
RouteBlinding | ShutdownAnySegwit | DualFund | Taproot,
175177
// Byte 4
176-
OnionMessages,
178+
Quiescence | OnionMessages,
177179
// Byte 5
178180
ProvideStorage | ChannelType | SCIDPrivacy,
179181
// Byte 6
@@ -536,6 +538,16 @@ mod sealed {
536538
supports_taproot,
537539
requires_taproot
538540
);
541+
define_feature!(
542+
35,
543+
Quiescence,
544+
[InitContext, NodeContext],
545+
"Feature flags for `option_quiesce`.",
546+
set_quiescence_optional,
547+
set_quiescence_required,
548+
supports_quiescence,
549+
requires_quiescence
550+
);
539551
define_feature!(
540552
39,
541553
OnionMessages,
@@ -1195,6 +1207,7 @@ mod tests {
11951207
init_features.set_channel_type_optional();
11961208
init_features.set_scid_privacy_optional();
11971209
init_features.set_zero_conf_optional();
1210+
init_features.set_quiescence_optional();
11981211

11991212
assert!(init_features.initial_routing_sync());
12001213
assert!(!init_features.supports_upfront_shutdown_script());
@@ -1215,7 +1228,7 @@ mod tests {
12151228
assert_eq!(node_features.flags[1], 0b01010001);
12161229
assert_eq!(node_features.flags[2], 0b10001010);
12171230
assert_eq!(node_features.flags[3], 0b00001010);
1218-
assert_eq!(node_features.flags[4], 0b10000000);
1231+
assert_eq!(node_features.flags[4], 0b10001000);
12191232
assert_eq!(node_features.flags[5], 0b10100000);
12201233
assert_eq!(node_features.flags[6], 0b00001000);
12211234
}

lightning/src/chain/channelmonitor.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use bitcoin::secp256k1::{self, SecretKey, PublicKey, Secp256k1, ecdsa::Signature
3434

3535
use crate::ln::channel::INITIAL_COMMITMENT_NUMBER;
3636
use crate::ln::types::ChannelId;
37+
use crate::types::features::ChannelTypeFeatures;
3738
use crate::types::payment::{PaymentHash, PaymentPreimage};
3839
use crate::ln::msgs::DecodeError;
3940
use crate::ln::channel_keys::{DelayedPaymentKey, DelayedPaymentBasepoint, HtlcBasepoint, HtlcKey, RevocationKey, RevocationBasepoint};
@@ -1611,6 +1612,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
16111612
self.inner.lock().unwrap().channel_id()
16121613
}
16131614

1615+
/// Gets the channel type of the corresponding channel.
1616+
pub fn channel_type_features(&self) -> ChannelTypeFeatures {
1617+
self.inner.lock().unwrap().channel_type_features()
1618+
}
1619+
16141620
/// Gets a list of txids, with their output scripts (in the order they appear in the
16151621
/// transaction), which we must learn about spends of via block_connected().
16161622
pub fn get_outputs_to_watch(&self) -> Vec<(Txid, Vec<(u32, ScriptBuf)>)> {
@@ -4806,6 +4812,10 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
48064812
self.onchain_events_awaiting_threshold_conf.push(entry);
48074813
}
48084814
}
4815+
4816+
fn channel_type_features(&self) -> ChannelTypeFeatures {
4817+
self.onchain_tx_handler.channel_type_features().clone()
4818+
}
48094819
}
48104820

48114821
impl<Signer: EcdsaChannelSigner, T: Deref, F: Deref, L: Deref> chain::Listen for (ChannelMonitor<Signer>, T, F, L)

lightning/src/ln/chan_utils.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,16 @@ pub(crate) fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize, channel_t
196196
/ 1000
197197
}
198198

199-
pub(crate) fn per_outbound_htlc_counterparty_commit_tx_fee_msat(feerate_per_kw: u32, channel_type_features: &ChannelTypeFeatures) -> u64 {
200-
// Note that we need to divide before multiplying to round properly,
201-
// since the lowest denomination of bitcoin on-chain is the satoshi.
202-
let commitment_tx_fee = COMMITMENT_TX_WEIGHT_PER_HTLC * feerate_per_kw as u64 / 1000 * 1000;
203-
if channel_type_features.supports_anchors_zero_fee_htlc_tx() {
204-
commitment_tx_fee + htlc_success_tx_weight(channel_type_features) * feerate_per_kw as u64 / 1000
199+
pub(crate) fn commit_and_htlc_tx_fees_sat(feerate_per_kw: u32, num_accepted_htlcs: usize, num_offered_htlcs: usize, channel_type_features: &ChannelTypeFeatures) -> u64 {
200+
let num_htlcs = num_accepted_htlcs + num_offered_htlcs;
201+
let commit_tx_fees_sat = commit_tx_fee_sat(feerate_per_kw, num_htlcs, channel_type_features);
202+
let htlc_tx_fees_sat = if !channel_type_features.supports_anchors_zero_fee_htlc_tx() {
203+
num_accepted_htlcs as u64 * htlc_success_tx_weight(channel_type_features) * feerate_per_kw as u64 / 1000
204+
+ num_offered_htlcs as u64 * htlc_timeout_tx_weight(channel_type_features) * feerate_per_kw as u64 / 1000
205205
} else {
206-
commitment_tx_fee
207-
}
206+
0
207+
};
208+
commit_tx_fees_sat + htlc_tx_fees_sat
208209
}
209210

210211
// Various functions for key derivation and transaction creation for use within channels. Primarily

0 commit comments

Comments
 (0)