Skip to content

Commit 862b82a

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 f755ae5 commit 862b82a

10 files changed

+421
-348
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);

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::{TxCreationKeys,HTLCOutputInCommitment,HTLC_SUCCESS_TX_WEIGHT,HTLC_TIMEOUT_TX_WEIGHT};
@@ -551,7 +551,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
551551

552552
/// Creates a new channel from a remote sides' request for one.
553553
/// Assumes chain_hash has already been checked and corresponds with what we expect!
554-
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> {
554+
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> {
555555
let chan_keys = keys_provider.get_channel_keys(true);
556556
let mut local_config = (*config).channel_options.clone();
557557

@@ -658,7 +658,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
658658
channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
659659
channel_monitor.set_their_to_self_delay(msg.to_self_delay);
660660

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

13981398
// Message handlers:
13991399

1400-
pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel, config: &UserConfig, their_local_features: LocalFeatures) -> Result<(), ChannelError> {
1400+
pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel, config: &UserConfig, their_features: Features<FeatureContextInit>) -> Result<(), ChannelError> {
14011401
// Check sanity of message fields:
14021402
if !self.channel_outbound {
14031403
return Err(ChannelError::Close("Got an accept_channel message from an inbound peer"));
@@ -1456,7 +1456,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14561456
return Err(ChannelError::Close("We consider the minimum depth to be unreasonably large"));
14571457
}
14581458

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

33313331
let msg = msgs::UnsignedChannelAnnouncement {
3332-
features: msgs::GlobalFeatures::new(),
3332+
features: msgs::Features::<msgs::FeatureContextChannel>::new(),
33333333
chain_hash: chain_hash,
33343334
short_channel_id: self.get_short_channel_id().unwrap(),
33353335
node_id_1: if were_node_one { our_node_id } else { self.get_their_node_id() },

0 commit comments

Comments
 (0)