Skip to content

Commit dfdd29a

Browse files
committed
Implement Flat Features
This merges local and global features into one struct, which is parameterized by where it appers. The parameterization restricts which queries can be made and which features can be set, in line with the latest BOLT 9. Closes #427.
1 parent 9310813 commit dfdd29a

13 files changed

+425
-351
lines changed

fuzz/src/chanmon_consistency.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use lightning::ln::channelmonitor;
2929
use lightning::ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, HTLCUpdate};
3030
use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, ChannelManagerReadArgs};
3131
use lightning::ln::router::{Route, RouteHop};
32-
use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, LightningError, UpdateAddHTLC, LocalFeatures};
32+
use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, LightningError, UpdateAddHTLC, Features, FeatureContextInit};
3333
use lightning::util::enforcing_trait_impls::EnforcingChannelKeys;
3434
use lightning::util::events;
3535
use lightning::util::logger::Logger;
@@ -252,7 +252,7 @@ pub fn do_test(data: &[u8]) {
252252
} else { panic!("Wrong event type"); }
253253
};
254254

255-
$dest.handle_open_channel(&$source.get_our_node_id(), LocalFeatures::new(), &open_channel).unwrap();
255+
$dest.handle_open_channel(&$source.get_our_node_id(), Features::<FeatureContextInit>::new(), &open_channel).unwrap();
256256
let accept_channel = {
257257
let events = $dest.get_and_clear_pending_msg_events();
258258
assert_eq!(events.len(), 1);
@@ -261,7 +261,7 @@ pub fn do_test(data: &[u8]) {
261261
} else { panic!("Wrong event type"); }
262262
};
263263

264-
$source.handle_accept_channel(&$dest.get_our_node_id(), LocalFeatures::new(), &accept_channel).unwrap();
264+
$source.handle_accept_channel(&$dest.get_our_node_id(), Features::<FeatureContextInit>::new(), &accept_channel).unwrap();
265265
{
266266
let events = $source.get_and_clear_pending_events();
267267
assert_eq!(events.len(), 1);

fuzz/src/msg_targets/gen_target.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ GEN_TEST DecodedOnionErrorPacket test_msg ""
2020
GEN_TEST FundingCreated test_msg ""
2121
GEN_TEST FundingLocked test_msg ""
2222
GEN_TEST FundingSigned test_msg ""
23-
GEN_TEST Init test_msg ""
2423
GEN_TEST OpenChannel test_msg ""
2524
GEN_TEST RevokeAndACK test_msg ""
2625
GEN_TEST Shutdown test_msg ""
@@ -37,5 +36,6 @@ GEN_TEST UpdateAddHTLC test_msg_hole ", 85, 33"
3736
GEN_TEST ErrorMessage test_msg_hole ", 32, 2"
3837
GEN_TEST OnionHopData test_msg_hole ", 1+8+8+4, 12"
3938

39+
GEN_TEST Init test_msg_simple ""
4040
GEN_TEST Ping test_msg_simple ""
4141
GEN_TEST Pong test_msg_simple ""

fuzz/src/msg_targets/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub mod msg_decoded_onion_error_packet;
88
pub mod msg_funding_created;
99
pub mod msg_funding_locked;
1010
pub mod msg_funding_signed;
11-
pub mod msg_init;
1211
pub mod msg_open_channel;
1312
pub mod msg_revoke_and_ack;
1413
pub mod msg_shutdown;
@@ -22,5 +21,6 @@ pub mod msg_node_announcement;
2221
pub mod msg_update_add_htlc;
2322
pub mod msg_error_message;
2423
pub mod msg_onion_hop_data;
24+
pub mod msg_init;
2525
pub mod msg_ping;
2626
pub mod msg_pong;

fuzz/src/msg_targets/msg_init.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use msg_targets::utils::VecWriter;
77

88
#[inline]
99
pub fn do_test(data: &[u8]) {
10-
test_msg!(msgs::Init, data);
10+
test_msg_simple!(msgs::Init, data);
1111
}
1212

1313
#[no_mangle]

lightning/src/ln/chanmon_update_fail_tests.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use ln::channelmanager::{RAACommitmentOrder, PaymentPreimage, PaymentHash};
77
use ln::channelmonitor::ChannelMonitorUpdateErr;
88
use ln::msgs;
9-
use ln::msgs::{ChannelMessageHandler, LocalFeatures, RoutingMessageHandler};
9+
use ln::msgs::{ChannelMessageHandler, Features, FeatureContextInit, RoutingMessageHandler};
1010
use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
1111
use util::errors::APIError;
1212

@@ -19,7 +19,7 @@ use ln::functional_test_utils::*;
1919
fn test_simple_monitor_permanent_update_fail() {
2020
// Test that we handle a simple permanent monitor update failure
2121
let mut nodes = create_network(2, &[None, None]);
22-
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
22+
create_announced_chan_between_nodes(&nodes, 0, 1, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
2323

2424
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
2525
let (_, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
@@ -49,7 +49,7 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) {
4949
// Test that we can recover from a simple temporary monitor update failure optionally with
5050
// a disconnect in between
5151
let mut nodes = create_network(2, &[None, None]);
52-
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
52+
create_announced_chan_between_nodes(&nodes, 0, 1, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
5353

5454
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
5555
let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
@@ -148,7 +148,7 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
148148
// through, swapping message ordering based on disconnect_count & 8 and optionally
149149
// disconnect/reconnecting based on disconnect_count.
150150
let mut nodes = create_network(2, &[None, None]);
151-
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
151+
create_announced_chan_between_nodes(&nodes, 0, 1, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
152152

153153
let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
154154

@@ -474,7 +474,7 @@ fn test_monitor_temporary_update_fail_c() {
474474
fn test_monitor_update_fail_cs() {
475475
// Tests handling of a monitor update failure when processing an incoming commitment_signed
476476
let mut nodes = create_network(2, &[None, None]);
477-
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
477+
create_announced_chan_between_nodes(&nodes, 0, 1, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
478478

479479
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
480480
let (payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
@@ -553,7 +553,7 @@ fn test_monitor_update_fail_no_rebroadcast() {
553553
// test_restore_channel_monitor() is required. Backported from
554554
// chanmon_fail_consistency fuzz tests.
555555
let mut nodes = create_network(2, &[None, None]);
556-
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
556+
create_announced_chan_between_nodes(&nodes, 0, 1, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
557557

558558
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
559559
let (payment_preimage_1, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
@@ -595,7 +595,7 @@ fn test_monitor_update_raa_while_paused() {
595595
// Tests handling of an RAA while monitor updating has already been marked failed.
596596
// Backported from chanmon_fail_consistency fuzz tests as this used to be broken.
597597
let mut nodes = create_network(2, &[None, None]);
598-
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
598+
create_announced_chan_between_nodes(&nodes, 0, 1, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
599599

600600
send_payment(&nodes[0], &[&nodes[1]], 5000000, 5_000_000);
601601

@@ -662,8 +662,8 @@ fn test_monitor_update_raa_while_paused() {
662662
fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
663663
// Tests handling of a monitor update failure when processing an incoming RAA
664664
let mut nodes = create_network(3, &[None, None, None]);
665-
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
666-
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
665+
create_announced_chan_between_nodes(&nodes, 0, 1, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
666+
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
667667

668668
// Rebalance a bit so that we can send backwards from 2 to 1.
669669
send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000, 5_000_000);
@@ -915,8 +915,8 @@ fn test_monitor_update_fail_reestablish() {
915915
// channel_reestablish generating a monitor update (which comes from freeing holding cell
916916
// HTLCs).
917917
let mut nodes = create_network(3, &[None, None, None]);
918-
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
919-
create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
918+
create_announced_chan_between_nodes(&nodes, 0, 1, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
919+
create_announced_chan_between_nodes(&nodes, 1, 2, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
920920

921921
let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
922922

@@ -993,7 +993,7 @@ fn raa_no_response_awaiting_raa_state() {
993993
// in question (assuming it intends to respond with a CS after monitor updating is restored).
994994
// Backported from chanmon_fail_consistency fuzz tests as this used to be broken.
995995
let mut nodes = create_network(2, &[None, None]);
996-
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
996+
create_announced_chan_between_nodes(&nodes, 0, 1, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
997997

998998
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
999999
let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
@@ -1106,7 +1106,7 @@ fn claim_while_disconnected_monitor_update_fail() {
11061106
// code introduced a regression in this test (specifically, this caught a removal of the
11071107
// channel_reestablish handling ensuring the order was sensical given the messages used).
11081108
let mut nodes = create_network(2, &[None, None]);
1109-
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1109+
create_announced_chan_between_nodes(&nodes, 0, 1, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
11101110

11111111
// Forward a payment for B to claim
11121112
let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
@@ -1221,7 +1221,7 @@ fn monitor_failed_no_reestablish_response() {
12211221
// Backported from chanmon_fail_consistency fuzz tests as it caught a long-standing
12221222
// debug_assert!() failure in channel_reestablish handling.
12231223
let mut nodes = create_network(2, &[None, None]);
1224-
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1224+
create_announced_chan_between_nodes(&nodes, 0, 1, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
12251225

12261226
// Route the payment and deliver the initial commitment_signed (with a monitor update failure
12271227
// on receipt).
@@ -1287,7 +1287,7 @@ fn first_message_on_recv_ordering() {
12871287
// payment applied).
12881288
// Backported from chanmon_fail_consistency fuzz tests as it caught a bug here.
12891289
let mut nodes = create_network(2, &[None, None]);
1290-
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1290+
create_announced_chan_between_nodes(&nodes, 0, 1, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
12911291

12921292
// Route the first payment outbound, holding the last RAA for B until we are set up so that we
12931293
// can deliver it and fail the monitor update.
@@ -1372,8 +1372,8 @@ fn test_monitor_update_fail_claim() {
13721372
// payment from B to A fail due to the paused channel. Finally, we restore the channel monitor
13731373
// updating and claim the payment on B.
13741374
let mut nodes = create_network(3, &[None, None, None]);
1375-
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1376-
create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
1375+
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
1376+
create_announced_chan_between_nodes(&nodes, 1, 2, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
13771377

13781378
// Rebalance a bit so that we can send backwards from 3 to 2.
13791379
send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000, 5_000_000);
@@ -1442,8 +1442,8 @@ fn test_monitor_update_on_pending_forwards() {
14421442
// The payment from A to C will be failed by C and pending a back-fail to A, while the payment
14431443
// from C to A will be pending a forward to A.
14441444
let mut nodes = create_network(3, &[None, None, None]);
1445-
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1446-
create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
1445+
create_announced_chan_between_nodes(&nodes, 0, 1, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
1446+
create_announced_chan_between_nodes(&nodes, 1, 2, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
14471447

14481448
// Rebalance a bit so that we can send backwards from 3 to 1.
14491449
send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000, 5_000_000);
@@ -1506,7 +1506,7 @@ fn monitor_update_claim_fail_no_response() {
15061506
// Backported from chanmon_fail_consistency fuzz tests as an unmerged version of the handling
15071507
// code was broken.
15081508
let mut nodes = create_network(2, &[None, None]);
1509-
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1509+
create_announced_chan_between_nodes(&nodes, 0, 1, Features::<FeatureContextInit>::new(), Features::<FeatureContextInit>::new());
15101510

15111511
// Forward a payment for B to claim
15121512
let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
@@ -1565,8 +1565,8 @@ fn do_during_funding_monitor_fail(fail_on_generate: bool, restore_between_fails:
15651565
let mut nodes = create_network(2, &[None, None]);
15661566

15671567
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 43).unwrap();
1568-
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), LocalFeatures::new(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id())).unwrap();
1569-
nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), LocalFeatures::new(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id())).unwrap();
1568+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), Features::<FeatureContextInit>::new(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id())).unwrap();
1569+
nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), Features::<FeatureContextInit>::new(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id())).unwrap();
15701570

15711571
let (temporary_channel_id, funding_tx, funding_output) = create_funding_transaction(&nodes[0], 100000, 43);
15721572

lightning/src/ln/channel.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use secp256k1::{Secp256k1,Signature};
1616
use secp256k1;
1717

1818
use ln::msgs;
19-
use ln::msgs::{DecodeError, OptionalField, LocalFeatures, DataLossProtect};
19+
use ln::msgs::{DecodeError, OptionalField, Features, FeatureContextInit, DataLossProtect};
2020
use ln::channelmonitor::ChannelMonitor;
2121
use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingForwardHTLCInfo, RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT, MAX_LOCAL_BREAKDOWN_TIMEOUT};
2222
use ln::chan_utils::{LocalCommitmentTransaction,TxCreationKeys,HTLCOutputInCommitment,HTLC_SUCCESS_TX_WEIGHT,HTLC_TIMEOUT_TX_WEIGHT};
@@ -543,7 +543,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
543543

544544
/// Creates a new channel from a remote sides' request for one.
545545
/// Assumes chain_hash has already been checked and corresponds with what we expect!
546-
pub fn new_from_req(fee_estimator: &FeeEstimator, keys_provider: &Arc<KeysInterface<ChanKeySigner = ChanSigner>>, their_node_id: PublicKey, their_local_features: LocalFeatures, msg: &msgs::OpenChannel, user_id: u64, logger: Arc<Logger>, config: &UserConfig) -> Result<Channel<ChanSigner>, ChannelError> {
546+
pub fn new_from_req(fee_estimator: &FeeEstimator, keys_provider: &Arc<KeysInterface<ChanKeySigner = ChanSigner>>, their_node_id: PublicKey, their_features: Features<FeatureContextInit>, msg: &msgs::OpenChannel, user_id: u64, logger: Arc<Logger>, config: &UserConfig) -> Result<Channel<ChanSigner>, ChannelError> {
547547
let chan_keys = keys_provider.get_channel_keys(true);
548548
let mut local_config = (*config).channel_options.clone();
549549

@@ -648,7 +648,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
648648
chan_keys.htlc_base_key(), chan_keys.payment_base_key(), &keys_provider.get_shutdown_pubkey(), config.own_channel_config.our_to_self_delay,
649649
keys_provider.get_destination_script(), logger.clone());
650650

651-
let their_shutdown_scriptpubkey = if their_local_features.supports_upfront_shutdown_script() {
651+
let their_shutdown_scriptpubkey = if their_features.supports_upfront_shutdown_script() {
652652
match &msg.shutdown_scriptpubkey {
653653
&OptionalField::Present(ref script) => {
654654
// Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. We enforce it while receiving shutdown msg
@@ -1306,7 +1306,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
13061306

13071307
// Message handlers:
13081308

1309-
pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel, config: &UserConfig, their_local_features: LocalFeatures) -> Result<(), ChannelError> {
1309+
pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel, config: &UserConfig, their_features: Features<FeatureContextInit>) -> Result<(), ChannelError> {
13101310
// Check sanity of message fields:
13111311
if !self.channel_outbound {
13121312
return Err(ChannelError::Close("Got an accept_channel message from an inbound peer"));
@@ -1365,7 +1365,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
13651365
return Err(ChannelError::Close("We consider the minimum depth to be unreasonably large"));
13661366
}
13671367

1368-
let their_shutdown_scriptpubkey = if their_local_features.supports_upfront_shutdown_script() {
1368+
let their_shutdown_scriptpubkey = if their_features.supports_upfront_shutdown_script() {
13691369
match &msg.shutdown_scriptpubkey {
13701370
&OptionalField::Present(ref script) => {
13711371
// Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. We enforce it while receiving shutdown msg
@@ -3225,7 +3225,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
32253225
let our_bitcoin_key = PublicKey::from_secret_key(&self.secp_ctx, self.local_keys.funding_key());
32263226

32273227
let msg = msgs::UnsignedChannelAnnouncement {
3228-
features: msgs::GlobalFeatures::new(),
3228+
features: msgs::Features::<msgs::FeatureContextChannel>::new(),
32293229
chain_hash: chain_hash,
32303230
short_channel_id: self.get_short_channel_id().unwrap(),
32313231
node_id_1: if were_node_one { our_node_id } else { self.get_their_node_id() },

0 commit comments

Comments
 (0)