Skip to content

Commit 554fc02

Browse files
authored
Merge pull request #1833 from johncantrell97/2022-11-rgs-handle-removed-channel
Ignore RGS channel updates for unknown channels
2 parents 9363137 + c044238 commit 554fc02

File tree

1 file changed

+79
-53
lines changed

1 file changed

+79
-53
lines changed

lightning-rapid-gossip-sync/src/processing.rs

Lines changed: 79 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,9 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
3737
let mut prefix = [0u8; 4];
3838
read_cursor.read_exact(&mut prefix)?;
3939

40-
match prefix {
41-
GOSSIP_PREFIX => {}
42-
_ => {
43-
return Err(DecodeError::UnknownVersion.into());
44-
}
45-
};
40+
if prefix != GOSSIP_PREFIX {
41+
return Err(DecodeError::UnknownVersion.into());
42+
}
4643

4744
let chain_hash: BlockHash = Readable::read(read_cursor)?;
4845
let latest_seen_timestamp: u32 = Readable::read(read_cursor)?;
@@ -75,6 +72,7 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
7572

7673
let node_id_1_index: BigSize = Readable::read(read_cursor)?;
7774
let node_id_2_index: BigSize = Readable::read(read_cursor)?;
75+
7876
if max(node_id_1_index.0, node_id_2_index.0) >= node_id_count as u64 {
7977
return Err(DecodeError::InvalidValue.into());
8078
};
@@ -123,49 +121,43 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
123121
// flags are always sent in full, and hence always need updating
124122
let standard_channel_flags = channel_flags & 0b_0000_0011;
125123

126-
let mut synthetic_update = if channel_flags & 0b_1000_0000 == 0 {
127-
// full update, field flags will indicate deviations from the default
128-
UnsignedChannelUpdate {
129-
chain_hash,
130-
short_channel_id,
131-
timestamp: backdated_timestamp,
132-
flags: standard_channel_flags,
133-
cltv_expiry_delta: default_cltv_expiry_delta,
134-
htlc_minimum_msat: default_htlc_minimum_msat,
135-
htlc_maximum_msat: default_htlc_maximum_msat,
136-
fee_base_msat: default_fee_base_msat,
137-
fee_proportional_millionths: default_fee_proportional_millionths,
138-
excess_data: Vec::new(),
139-
}
140-
} else {
124+
let mut synthetic_update = UnsignedChannelUpdate {
125+
chain_hash,
126+
short_channel_id,
127+
timestamp: backdated_timestamp,
128+
flags: standard_channel_flags,
129+
cltv_expiry_delta: default_cltv_expiry_delta,
130+
htlc_minimum_msat: default_htlc_minimum_msat,
131+
htlc_maximum_msat: default_htlc_maximum_msat,
132+
fee_base_msat: default_fee_base_msat,
133+
fee_proportional_millionths: default_fee_proportional_millionths,
134+
excess_data: Vec::new(),
135+
};
136+
137+
let mut skip_update_for_unknown_channel = false;
138+
139+
if (channel_flags & 0b_1000_0000) != 0 {
141140
// incremental update, field flags will indicate mutated values
142141
let read_only_network_graph = network_graph.read_only();
143-
let channel = read_only_network_graph
142+
if let Some(channel) = read_only_network_graph
144143
.channels()
145-
.get(&short_channel_id)
146-
.ok_or(LightningError {
147-
err: "Couldn't find channel for update".to_owned(),
148-
action: ErrorAction::IgnoreError,
149-
})?;
150-
151-
let directional_info = channel
152-
.get_directional_info(channel_flags)
153-
.ok_or(LightningError {
154-
err: "Couldn't find previous directional data for update".to_owned(),
155-
action: ErrorAction::IgnoreError,
156-
})?;
157-
158-
UnsignedChannelUpdate {
159-
chain_hash,
160-
short_channel_id,
161-
timestamp: backdated_timestamp,
162-
flags: standard_channel_flags,
163-
cltv_expiry_delta: directional_info.cltv_expiry_delta,
164-
htlc_minimum_msat: directional_info.htlc_minimum_msat,
165-
htlc_maximum_msat: directional_info.htlc_maximum_msat,
166-
fee_base_msat: directional_info.fees.base_msat,
167-
fee_proportional_millionths: directional_info.fees.proportional_millionths,
168-
excess_data: Vec::new(),
144+
.get(&short_channel_id) {
145+
146+
let directional_info = channel
147+
.get_directional_info(channel_flags)
148+
.ok_or(LightningError {
149+
err: "Couldn't find previous directional data for update".to_owned(),
150+
action: ErrorAction::IgnoreError,
151+
})?;
152+
153+
synthetic_update.cltv_expiry_delta = directional_info.cltv_expiry_delta;
154+
synthetic_update.htlc_minimum_msat = directional_info.htlc_minimum_msat;
155+
synthetic_update.htlc_maximum_msat = directional_info.htlc_maximum_msat;
156+
synthetic_update.fee_base_msat = directional_info.fees.base_msat;
157+
synthetic_update.fee_proportional_millionths = directional_info.fees.proportional_millionths;
158+
159+
} else {
160+
skip_update_for_unknown_channel = true;
169161
}
170162
};
171163

@@ -194,6 +186,10 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
194186
synthetic_update.htlc_maximum_msat = htlc_maximum_msat;
195187
}
196188

189+
if skip_update_for_unknown_channel {
190+
continue;
191+
}
192+
197193
match network_graph.update_channel_unsigned(&synthetic_update) {
198194
Ok(_) => {},
199195
Err(LightningError { action: ErrorAction::IgnoreDuplicateGossip, .. }) => {},
@@ -254,7 +250,7 @@ mod tests {
254250
}
255251

256252
#[test]
257-
fn incremental_only_update_fails_without_prior_announcements() {
253+
fn incremental_only_update_ignores_missing_channel() {
258254
let incremental_update_input = vec![
259255
76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
260256
79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
@@ -271,12 +267,7 @@ mod tests {
271267

272268
let rapid_sync = RapidGossipSync::new(&network_graph);
273269
let update_result = rapid_sync.update_network_graph(&incremental_update_input[..]);
274-
assert!(update_result.is_err());
275-
if let Err(GraphSyncError::LightningError(lightning_error)) = update_result {
276-
assert_eq!(lightning_error.err, "Couldn't find channel for update");
277-
} else {
278-
panic!("Unexpected update result: {:?}", update_result)
279-
}
270+
assert!(update_result.is_ok());
280271
}
281272

282273
#[test]
@@ -534,4 +525,39 @@ mod tests {
534525
assert!(after.contains("619737530008010752"));
535526
assert!(after.contains("783241506229452801"));
536527
}
528+
529+
#[test]
530+
pub fn update_fails_with_unknown_version() {
531+
let unknown_version_input = vec![
532+
76, 68, 75, 2, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
533+
79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
534+
0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
535+
187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
536+
157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
537+
88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
538+
204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
539+
181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
540+
110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
541+
76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
542+
226, 0, 6, 11, 0, 1, 2, 3, 0, 0, 0, 4, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 3, 232,
543+
0, 0, 0, 1, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 60, 0, 0,
544+
0, 0, 0, 0, 0, 1, 0, 0, 0, 100, 0, 0, 2, 224, 0, 0, 0, 0, 58, 85, 116, 216, 0, 29, 0,
545+
0, 0, 1, 0, 0, 0, 125, 0, 0, 0, 0, 58, 85, 116, 216, 255, 2, 68, 226, 0, 6, 11, 0, 1,
546+
0, 0, 1,
547+
];
548+
549+
let block_hash = genesis_block(Network::Bitcoin).block_hash();
550+
let logger = TestLogger::new();
551+
let network_graph = NetworkGraph::new(block_hash, &logger);
552+
let rapid_sync = RapidGossipSync::new(&network_graph);
553+
let update_result = rapid_sync.update_network_graph(&unknown_version_input[..]);
554+
555+
assert!(update_result.is_err());
556+
557+
if let Err(GraphSyncError::DecodeError(DecodeError::UnknownVersion)) = update_result {
558+
// this is the expected error type
559+
} else {
560+
panic!("Unexpected update result: {:?}", update_result)
561+
}
562+
}
537563
}

0 commit comments

Comments
 (0)