Skip to content

Commit cc89f77

Browse files
committed
Relay/store channel/node announces w/ unknown req'd feature bits
This change was made in the flat features BOLT PR, as if a channel requires some unknown feature bits we should still rumor it, we just shouldn't route through it.
1 parent dfdd29a commit cc89f77

File tree

2 files changed

+76
-31
lines changed

2 files changed

+76
-31
lines changed

lightning/src/ln/msgs.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,23 @@ impl<T: FeatureContext> Features<T> {
179179
( idx != 0 && byte != 0 ) || ( idx == 0 && (byte & 0xc4) != 0 )
180180
})
181181
}
182+
183+
#[cfg(test)]
184+
pub(crate) fn set_require_unknown_bits(&mut self) {
185+
let newlen = cmp::max(2, self.flags.len());
186+
self.flags.resize(newlen, 0u8);
187+
self.flags[1] |= 0x40;
188+
}
189+
190+
#[cfg(test)]
191+
pub(crate) fn clear_require_unknown_bits(&mut self) {
192+
let newlen = cmp::max(2, self.flags.len());
193+
self.flags.resize(newlen, 0u8);
194+
self.flags[1] &= !0x40;
195+
if self.flags.len() == 2 && self.flags[1] == 0 {
196+
self.flags.resize(1, 0u8);
197+
}
198+
}
182199
}
183200

184201
impl<T: FeatureContextInitNode> Features<T> {
@@ -1249,13 +1266,7 @@ impl Writeable for UnsignedChannelAnnouncement {
12491266
impl<R: Read> Readable<R> for UnsignedChannelAnnouncement {
12501267
fn read(r: &mut R) -> Result<Self, DecodeError> {
12511268
Ok(Self {
1252-
features: {
1253-
let f: Features<FeatureContextChannel> = Readable::read(r)?;
1254-
if f.requires_unknown_bits() {
1255-
return Err(DecodeError::UnknownRequiredFeature);
1256-
}
1257-
f
1258-
},
1269+
features: Readable::read(r)?,
12591270
chain_hash: Readable::read(r)?,
12601271
short_channel_id: Readable::read(r)?,
12611272
node_id_1: Readable::read(r)?,
@@ -1383,9 +1394,6 @@ impl Writeable for UnsignedNodeAnnouncement {
13831394
impl<R: Read> Readable<R> for UnsignedNodeAnnouncement {
13841395
fn read(r: &mut R) -> Result<Self, DecodeError> {
13851396
let features: Features<FeatureContextNode> = Readable::read(r)?;
1386-
if features.requires_unknown_bits() {
1387-
return Err(DecodeError::UnknownRequiredFeature);
1388-
}
13891397
let timestamp: u32 = Readable::read(r)?;
13901398
let node_id: PublicKey = Readable::read(r)?;
13911399
let mut rgb = [0; 3];

lightning/src/ln/router.rs

+58-21
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,6 @@ impl RoutingMessageHandler for Router {
414414
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
415415
secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
416416

417-
if msg.contents.features.requires_unknown_bits() {
418-
panic!("Unknown-required-features NodeAnnouncements should never deserialize!");
419-
}
420-
421417
let mut network = self.network_map.write().unwrap();
422418
match network.nodes.get_mut(&msg.contents.node_id) {
423419
None => Err(LightningError{err: "No existing channels for node_announcement", action: ErrorAction::IgnoreError}),
@@ -432,7 +428,7 @@ impl RoutingMessageHandler for Router {
432428
node.alias = msg.contents.alias;
433429
node.addresses = msg.contents.addresses.clone();
434430

435-
let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty() && !msg.contents.features.supports_unknown_bits();
431+
let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty();
436432
node.announcement_message = if should_relay { Some(msg.clone()) } else { None };
437433
Ok(should_relay)
438434
}
@@ -450,10 +446,6 @@ impl RoutingMessageHandler for Router {
450446
secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
451447
secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
452448

453-
if msg.contents.features.requires_unknown_bits() {
454-
panic!("Unknown-required-features ChannelAnnouncements should never deserialize!");
455-
}
456-
457449
let checked_utxo = match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) {
458450
Ok((script_pubkey, _value)) => {
459451
let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
@@ -483,7 +475,7 @@ impl RoutingMessageHandler for Router {
483475
let mut network_lock = self.network_map.write().unwrap();
484476
let network = network_lock.borrow_parts();
485477

486-
let should_relay = msg.contents.excess_data.is_empty() && !msg.contents.features.supports_unknown_bits();
478+
let should_relay = msg.contents.excess_data.is_empty();
487479

488480
let chan_info = ChannelInfo {
489481
features: msg.contents.features.clone(),
@@ -941,17 +933,19 @@ impl Router {
941933

942934
for chan_id in $node.channels.iter() {
943935
let chan = network.channels.get(chan_id).unwrap();
944-
if chan.one_to_two.src_node_id == *$node_id {
945-
// ie $node is one, ie next hop in A* is two, via the two_to_one channel
946-
if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id {
947-
if chan.two_to_one.enabled {
948-
add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, $fee_to_target_msat);
936+
if !chan.features.requires_unknown_bits() {
937+
if chan.one_to_two.src_node_id == *$node_id {
938+
// ie $node is one, ie next hop in A* is two, via the two_to_one channel
939+
if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id {
940+
if chan.two_to_one.enabled {
941+
add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, $fee_to_target_msat);
942+
}
949943
}
950-
}
951-
} else {
952-
if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id {
953-
if chan.one_to_two.enabled {
954-
add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, $fee_to_target_msat);
944+
} else {
945+
if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id {
946+
if chan.one_to_two.enabled {
947+
add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, $fee_to_target_msat);
948+
}
955949
}
956950
}
957951
}
@@ -1015,7 +1009,7 @@ mod tests {
10151009
use chain::chaininterface;
10161010
use ln::channelmanager;
10171011
use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
1018-
use ln::msgs::{Features, FeatureContextChannel, FeatureContextNode};
1012+
use ln::msgs::{Features, FeatureContextChannel, FeatureContextNode, LightningError, ErrorAction};
10191013
use util::test_utils;
10201014
use util::test_utils::TestVecWriter;
10211015
use util::logger::Logger;
@@ -1441,6 +1435,49 @@ mod tests {
14411435
assert_eq!(route.hops[1].cltv_expiry_delta, 42);
14421436
}
14431437

1438+
{ // Disable 4 and 12 by requiring unknown feature bits
1439+
let mut network = router.network_map.write().unwrap();
1440+
network.channels.get_mut(&NetworkMap::get_key(4, zero_hash.clone())).unwrap().features.set_require_unknown_bits();
1441+
network.channels.get_mut(&NetworkMap::get_key(12, zero_hash.clone())).unwrap().features.set_require_unknown_bits();
1442+
}
1443+
1444+
{ // If all the channels require some features we don't understand, route should fail
1445+
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = router.get_route(&node3, None, &Vec::new(), 100, 42) {
1446+
assert_eq!(err, "Failed to find a path to the given destination");
1447+
} else { panic!(); }
1448+
}
1449+
1450+
{ // If we specify a channel to node8, that overrides our local channel view and that gets used
1451+
let our_chans = vec![channelmanager::ChannelDetails {
1452+
channel_id: [0; 32],
1453+
short_channel_id: Some(42),
1454+
remote_network_id: node8.clone(),
1455+
channel_value_satoshis: 0,
1456+
user_id: 0,
1457+
outbound_capacity_msat: 0,
1458+
inbound_capacity_msat: 0,
1459+
is_live: true,
1460+
}];
1461+
let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1462+
assert_eq!(route.hops.len(), 2);
1463+
1464+
assert_eq!(route.hops[0].pubkey, node8);
1465+
assert_eq!(route.hops[0].short_channel_id, 42);
1466+
assert_eq!(route.hops[0].fee_msat, 200);
1467+
assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1468+
1469+
assert_eq!(route.hops[1].pubkey, node3);
1470+
assert_eq!(route.hops[1].short_channel_id, 13);
1471+
assert_eq!(route.hops[1].fee_msat, 100);
1472+
assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1473+
}
1474+
1475+
{ // Re-enable 4 and 12 by wiping the unknown feature bits
1476+
let mut network = router.network_map.write().unwrap();
1477+
network.channels.get_mut(&NetworkMap::get_key(4, zero_hash.clone())).unwrap().features.clear_require_unknown_bits();
1478+
network.channels.get_mut(&NetworkMap::get_key(12, zero_hash.clone())).unwrap().features.clear_require_unknown_bits();
1479+
}
1480+
14441481
{ // Route to 1 via 2 and 3 because our channel to 1 is disabled
14451482
let route = router.get_route(&node1, None, &Vec::new(), 100, 42).unwrap();
14461483
assert_eq!(route.hops.len(), 3);

0 commit comments

Comments
 (0)