Skip to content

Commit fb6d8d1

Browse files
committed
Improve unit tests and remove unnecessary prefix checks
1 parent c4fc779 commit fb6d8d1

File tree

2 files changed

+116
-62
lines changed

2 files changed

+116
-62
lines changed

lightning-graph-sync/src/processing.rs

+107-61
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ where
3737
read_cursor.read_exact(&mut prefix)?;
3838

3939
let _deserialize_unsigned = match &prefix[..] {
40-
&[71, 83, 80, 1] => {
41-
return Err(GraphSyncError::ProcessingError(
42-
"Signed deserialization not supported".to_string(),
43-
));
44-
}
4540
&[76, 68, 75, 2] => true,
4641
_ => {
4742
return Err(GraphSyncError::ProcessingError(
@@ -120,7 +115,7 @@ where
120115

121116
} else {
122117
// incremental update
123-
println!("channel flags: {:b}, incremental update", channel_flags);
118+
// println!("channel flags: {:b}, incremental update", channel_flags);
124119

125120
// locate the last channel update by this id
126121
let mut channel_mutation_properties: Vec<ChannelMutationProperty> = Vec::with_capacity(5);
@@ -165,18 +160,8 @@ where
165160
}
166161
}
167162

168-
network_graph.override_directional_channel_properties(short_channel_id, standard_channel_flags, channel_mutation_properties);
163+
network_graph.override_directional_channel_properties(short_channel_id, standard_channel_flags, channel_mutation_properties)?;
169164
}
170-
171-
// println!("work in progress");
172-
173-
// let mut restricted_cursor = read_cursor.take(message_length.0);
174-
// let mut channel_update: msgs::UnsignedChannelUpdate =
175-
// Readable::read(&mut restricted_cursor).unwrap();
176-
// channel_update.timestamp = current_timestamp;
177-
// network_graph.update_channel_unsigned(&channel_update)?;
178-
//
179-
// read_cursor = restricted_cursor.into_inner();
180165
}
181166

182167
Ok(())
@@ -193,50 +178,6 @@ mod tests {
193178

194179
use crate::processing::update_network_graph;
195180

196-
#[test]
197-
fn network_graph_updates_from_example_binary_input() {
198-
let block_hash = genesis_block(Network::Bitcoin).block_hash();
199-
let network_graph = NetworkGraph::new(block_hash);
200-
201-
let before = network_graph.to_string();
202-
assert_eq!(before.len(), 31);
203-
204-
let chain_source: Option<Box<dyn lightning::chain::Access>> = None;
205-
206-
let example_input = vec![
207-
76, 68, 75, 2, 176, 1, 0, 0, 0, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162,
208-
70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0,
209-
10, 209, 73, 0, 2, 144, 0, 0, 2, 96, 250, 182, 51, 6, 110, 215, 177, 217, 185, 184,
210-
160, 250, 200, 126, 21, 121, 209, 112, 158, 135, 77, 40, 160, 209, 113, 161, 245, 196,
211-
59, 184, 119, 2, 180, 101, 113, 124, 204, 150, 157, 103, 192, 42, 110, 145, 224, 176,
212-
14, 26, 197, 136, 6, 143, 13, 46, 3, 200, 220, 34, 228, 191, 104, 238, 134, 80, 3, 167,
213-
152, 93, 150, 15, 179, 70, 61, 160, 42, 198, 253, 53, 249, 37, 29, 21, 176, 28, 136,
214-
240, 237, 138, 165, 218, 254, 14, 245, 123, 228, 145, 150, 2, 25, 120, 160, 148, 15,
215-
56, 179, 42, 198, 99, 96, 81, 85, 53, 227, 51, 62, 134, 4, 175, 0, 151, 217, 104, 180,
216-
201, 249, 117, 196, 87, 94, 150, 74, 1, 2, 111, 226, 140, 10, 182, 241, 179, 114, 193,
217-
166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0,
218-
0, 0, 0, 10, 209, 73, 0, 2, 144, 0, 0, 97, 139, 55, 91, 1, 2, 0, 40, 0, 0, 0, 0, 0, 0,
219-
3, 232, 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 123, 235, 5, 192, 74, 1, 2, 111, 226,
220-
140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101,
221-
225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 10, 209, 73, 0, 2, 144, 0, 0, 97, 138,
222-
144, 189, 1, 1, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 0, 0, 0, 0, 5, 220, 0, 0, 0, 0,
223-
123, 235, 5, 192,
224-
];
225-
let update_result = update_network_graph(&network_graph, &example_input[..], &chain_source);
226-
assert!(update_result.is_ok());
227-
228-
let after = network_graph.to_string();
229-
assert_eq!(after.len(), 1132);
230-
assert!(after.contains("779484474903625728: features: 0000"));
231-
assert!(after.contains(
232-
"node_one: 0260fab633066ed7b1d9b9b8a0fac87e1579d1709e874d28a0d171a1f5c43bb877"
233-
));
234-
assert!(after.contains(
235-
"node_two: 02b465717ccc969d67c02a6e91e0b00e1ac588068f0d2e03c8dc22e4bf68ee8650"
236-
));
237-
assert!(after.contains("channels: [779484474903625728]"));
238-
}
239-
240181
#[test]
241182
fn network_graph_fails_to_update_from_clipped_input() {
242183
let block_hash = genesis_block(Network::Bitcoin).block_hash();
@@ -341,4 +282,109 @@ mod tests {
341282
assert!(false);
342283
}
343284
}
285+
286+
#[test]
287+
fn incremental_only_update_fails_without_prior_announcements() {
288+
let incremental_update_input = vec![76, 68, 75, 2, 0, 2, 8, 153, 192, 0, 2, 27, 0, 0, 136, 0, 0, 2, 217, 10, 222, 162, 0, 8, 38, 0, 1, 128];
289+
290+
let block_hash = genesis_block(Network::Bitcoin).block_hash();
291+
let network_graph = NetworkGraph::new(block_hash);
292+
293+
let before = network_graph.to_string();
294+
assert_eq!(before.len(), 31);
295+
296+
let chain_source: Option<Box<dyn lightning::chain::Access>> = None;
297+
298+
let update_result = update_network_graph(&network_graph, &incremental_update_input[..], &chain_source);
299+
assert!(update_result.is_err());
300+
if let Err(GraphSyncError::LightningError(lightning_error)) = update_result {
301+
assert_eq!(lightning_error.err, "Couldn't find channel for update");
302+
}else {
303+
assert!(false);
304+
}
305+
}
306+
307+
#[test]
308+
fn incremental_only_update_fails_without_prior_updates() {
309+
let announced_update_input = vec![76, 68, 75, 2, 2, 174, 0, 0, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 8, 153, 192, 0, 2, 27, 0, 0, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251, 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125, 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136, 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 163, 216, 116, 204, 120, 152, 52, 172, 208, 143, 140, 71, 242, 209, 153, 111, 23, 61, 222, 28, 106, 198, 149, 87, 166, 201, 180, 51, 64, 252, 143, 166, 2, 57, 240, 15, 230, 190, 255, 224, 70, 176, 137, 155, 249, 125, 20, 170, 114, 97, 221, 142, 89, 231, 233, 205, 126, 199, 216, 149, 155, 0, 210, 45, 67, 174, 0, 0, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 10, 222, 162, 0, 8, 38, 0, 1, 2, 158, 1, 242, 121, 152, 106, 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138, 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175, 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128, 76, 97, 2, 155, 9, 173, 229, 75, 82, 137, 57, 21, 112, 207, 99, 92, 145, 58, 48, 229, 10, 228, 163, 3, 106, 147, 39, 177, 90, 242, 186, 249, 11, 210, 54, 2, 212, 16, 225, 46, 42, 209, 254, 11, 28, 254, 196, 167, 139, 164, 241, 7, 176, 120, 192, 199, 148, 152, 38, 17, 118, 41, 122, 240, 223, 249, 158, 145, 2, 8, 153, 192, 0, 2, 27, 0, 0, 136, 0, 0, 2, 217, 10, 222, 162, 0, 8, 38, 0, 1, 128];
310+
311+
let block_hash = genesis_block(Network::Bitcoin).block_hash();
312+
let network_graph = NetworkGraph::new(block_hash);
313+
314+
let before = network_graph.to_string();
315+
assert_eq!(before.len(), 31);
316+
317+
let chain_source: Option<Box<dyn lightning::chain::Access>> = None;
318+
319+
let update_result = update_network_graph(&network_graph, &announced_update_input[..], &chain_source);
320+
assert!(update_result.is_err());
321+
if let Err(GraphSyncError::LightningError(lightning_error)) = update_result {
322+
assert_eq!(lightning_error.err, "Couldn't find previous directional data for update");
323+
}else {
324+
assert!(false);
325+
}
326+
}
327+
328+
#[test]
329+
fn incremental_only_update_fails_without_prior_same_direction_updates() {
330+
let initialization_input = vec![76, 68, 75, 2, 2, 174, 0, 0, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 8, 153, 192, 0, 2, 27, 0, 0, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251, 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125, 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136, 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 163, 216, 116, 204, 120, 152, 52, 172, 208, 143, 140, 71, 242, 209, 153, 111, 23, 61, 222, 28, 106, 198, 149, 87, 166, 201, 180, 51, 64, 252, 143, 166, 2, 57, 240, 15, 230, 190, 255, 224, 70, 176, 137, 155, 249, 125, 20, 170, 114, 97, 221, 142, 89, 231, 233, 205, 126, 199, 216, 149, 155, 0, 210, 45, 67, 174, 0, 0, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 10, 222, 162, 0, 8, 38, 0, 1, 2, 158, 1, 242, 121, 152, 106, 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138, 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175, 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128, 76, 97, 2, 155, 9, 173, 229, 75, 82, 137, 57, 21, 112, 207, 99, 92, 145, 58, 48, 229, 10, 228, 163, 3, 106, 147, 39, 177, 90, 242, 186, 249, 11, 210, 54, 2, 212, 16, 225, 46, 42, 209, 254, 11, 28, 254, 196, 167, 139, 164, 241, 7, 176, 120, 192, 199, 148, 152, 38, 17, 118, 41, 122, 240, 223, 249, 158, 145, 2, 8, 153, 192, 0, 2, 27, 0, 0, 1, 27, 1, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 0, 1, 0, 0, 0, 125, 0, 0, 0, 0, 58, 85, 116, 216, 10, 222, 162, 0, 8, 38, 0, 1, 1, 27, 1, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 3, 232, 0, 0, 0, 1, 0, 0, 0, 0, 29, 129, 25, 192];
331+
332+
let block_hash = genesis_block(Network::Bitcoin).block_hash();
333+
let network_graph = NetworkGraph::new(block_hash);
334+
335+
let before = network_graph.to_string();
336+
assert_eq!(before.len(), 31);
337+
338+
let chain_source: Option<Box<dyn lightning::chain::Access>> = None;
339+
340+
let initialization_result = update_network_graph(&network_graph, &initialization_input[..], &chain_source);
341+
assert!(initialization_result.is_ok());
342+
343+
let initialized = network_graph.to_string();
344+
assert_eq!(initialized.len(), 1737);
345+
assert!(initialized.contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643:"));
346+
assert!(initialized.contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b:"));
347+
assert!(initialized.contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432:"));
348+
assert!(initialized.contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61:"));
349+
assert!(initialized.contains("channels: [619737530008010752]"));
350+
assert!(initialized.contains("channels: [783241506229452801]"));
351+
352+
let opposite_direction_incremental_update_input = vec![76, 68, 75, 2, 0, 2, 8, 153, 192, 0, 2, 27, 0, 0, 136, 0, 0, 2, 217, 10, 222, 162, 0, 8, 38, 0, 1, 128];
353+
let update_result = update_network_graph(&network_graph, &opposite_direction_incremental_update_input[..], &chain_source);
354+
assert!(update_result.is_err());
355+
if let Err(GraphSyncError::LightningError(lightning_error)) = update_result {
356+
assert_eq!(lightning_error.err, "Couldn't find previous directional data for update");
357+
}else {
358+
assert!(false);
359+
}
360+
}
361+
362+
#[test]
363+
fn incremental_update_succeeds_with_prior_announcements_and_full_updates() {
364+
let initialization_input = vec![76, 68, 75, 2, 2, 174, 0, 0, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 8, 153, 192, 0, 2, 27, 0, 0, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251, 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125, 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136, 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 163, 216, 116, 204, 120, 152, 52, 172, 208, 143, 140, 71, 242, 209, 153, 111, 23, 61, 222, 28, 106, 198, 149, 87, 166, 201, 180, 51, 64, 252, 143, 166, 2, 57, 240, 15, 230, 190, 255, 224, 70, 176, 137, 155, 249, 125, 20, 170, 114, 97, 221, 142, 89, 231, 233, 205, 126, 199, 216, 149, 155, 0, 210, 45, 67, 174, 0, 0, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 10, 222, 162, 0, 8, 38, 0, 1, 2, 158, 1, 242, 121, 152, 106, 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138, 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175, 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128, 76, 97, 2, 155, 9, 173, 229, 75, 82, 137, 57, 21, 112, 207, 99, 92, 145, 58, 48, 229, 10, 228, 163, 3, 106, 147, 39, 177, 90, 242, 186, 249, 11, 210, 54, 2, 212, 16, 225, 46, 42, 209, 254, 11, 28, 254, 196, 167, 139, 164, 241, 7, 176, 120, 192, 199, 148, 152, 38, 17, 118, 41, 122, 240, 223, 249, 158, 145, 4, 8, 153, 192, 0, 2, 27, 0, 0, 0, 27, 1, 0, 40, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 100, 0, 0, 2, 224, 0, 0, 0, 0, 58, 85, 116, 216, 8, 153, 192, 0, 2, 27, 0, 0, 1, 27, 1, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 0, 1, 0, 0, 0, 125, 0, 0, 0, 0, 58, 85, 116, 216, 10, 222, 162, 0, 8, 38, 0, 1, 0, 27, 1, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 3, 232, 0, 0, 0, 1, 0, 0, 0, 0, 29, 129, 25, 192, 10, 222, 162, 0, 8, 38, 0, 1, 1, 27, 1, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 3, 232, 0, 0, 0, 1, 0, 0, 0, 0, 29, 129, 25, 192];
365+
366+
let block_hash = genesis_block(Network::Bitcoin).block_hash();
367+
let network_graph = NetworkGraph::new(block_hash);
368+
369+
let before = network_graph.to_string();
370+
assert_eq!(before.len(), 31);
371+
372+
let chain_source: Option<Box<dyn lightning::chain::Access>> = None;
373+
374+
let initialization_result = update_network_graph(&network_graph, &initialization_input[..], &chain_source);
375+
assert!(initialization_result.is_ok());
376+
377+
let single_direction_incremental_update_input = vec![76, 68, 75, 2, 0, 2, 8, 153, 192, 0, 2, 27, 0, 0, 136, 0, 0, 2, 217, 10, 222, 162, 0, 8, 38, 0, 1, 128];
378+
let update_result = update_network_graph(&network_graph, &single_direction_incremental_update_input[..], &chain_source);
379+
assert!(update_result.is_ok());
380+
381+
let after = network_graph.to_string();
382+
assert_eq!(after.len(), 2348);
383+
assert!(after.contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643:"));
384+
assert!(after.contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b:"));
385+
assert!(after.contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432:"));
386+
assert!(after.contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61:"));
387+
assert!(after.contains("channels: [619737530008010752]"));
388+
assert!(after.contains("channels: [783241506229452801]"));
389+
}
344390
}

lightning/src/routing/network_graph.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,15 @@ impl NetworkGraph {
11911191
}
11921192

11931193
#[cfg(feature = "rapid-graph-sync")]
1194-
/// DANGER DO NOT CALL YET
1194+
/// Override a (directed) channel's individual properties without having a full update
1195+
/// This method should be used exclusively in conjunction with rapid graph sync, which is intended
1196+
/// for mobile devices.
1197+
///
1198+
/// `short_channel_id`: Channel to be updated
1199+
///
1200+
/// `flags`: Channel flags to set. Least significant bit serves as directional indicator
1201+
///
1202+
/// `mutations: Vec<ChannelMutationProperty>`: Array of properties to mutate
11951203
pub fn override_directional_channel_properties(&self, short_channel_id: u64, flags: u8, mutations: Vec<ChannelMutationProperty>) -> Result<(), LightningError> {
11961204
let mut channels = self.channels.write().unwrap();
11971205
let direction = flags & 1u8;

0 commit comments

Comments
 (0)