|
| 1 | +#![deny(missing_docs)] |
| 2 | +#![deny(unsafe_code)] |
| 3 | +#![deny(broken_intra_doc_links)] |
| 4 | +#![deny(non_upper_case_globals)] |
| 5 | +#![deny(non_camel_case_types)] |
| 6 | +#![deny(non_snake_case)] |
| 7 | +#![deny(unused_mut)] |
| 8 | +#![deny(unused_variables)] |
| 9 | +#![deny(unused_imports)] |
| 10 | + |
| 11 | +//! This crate exposes functionality to rapidly sync gossip data, aimed primarily at mobile |
| 12 | +//! devices. |
| 13 | +
|
| 14 | +use std::fs::File; |
| 15 | + |
| 16 | +use lightning::routing::network_graph; |
| 17 | + |
| 18 | +use crate::error::GraphSyncError; |
| 19 | + |
| 20 | +/// Error types that these functions can return |
| 21 | +pub mod error; |
| 22 | + |
| 23 | +/// Core functionality of this crate |
| 24 | +pub mod processing; |
| 25 | + |
| 26 | +/// Sync gossip data from a file |
| 27 | +/// |
| 28 | +/// `network_graph`: The network graph to apply the updates to |
| 29 | +/// |
| 30 | +/// `sync_path`: Path to the file where the gossip update data is located |
| 31 | +/// |
| 32 | +pub fn sync_network_graph_with_file_path( |
| 33 | + network_graph: &network_graph::NetworkGraph, |
| 34 | + sync_path: &str, |
| 35 | +) -> Result<(), GraphSyncError> { |
| 36 | + let mut file = File::open(sync_path)?; |
| 37 | + processing::read_network_graph(&network_graph, &mut file) |
| 38 | +} |
| 39 | + |
| 40 | +#[cfg(test)] |
| 41 | +mod tests { |
| 42 | + use std::fs; |
| 43 | + |
| 44 | + use bitcoin::blockdata::constants::genesis_block; |
| 45 | + use bitcoin::Network; |
| 46 | + |
| 47 | + use lightning::routing::network_graph::NetworkGraph; |
| 48 | + |
| 49 | + use crate::sync_network_graph_with_file_path; |
| 50 | + |
| 51 | + #[test] |
| 52 | + fn test_sync_from_file() { |
| 53 | + // same as incremental_only_update_fails_without_prior_same_direction_updates |
| 54 | + // (OldestDataDirection1, timestamp 0) |
| 55 | + let valid_response = vec![ |
| 56 | + 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, |
| 57 | + 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218, |
| 58 | + 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251, |
| 59 | + 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125, |
| 60 | + 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136, |
| 61 | + 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106, |
| 62 | + 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138, |
| 63 | + 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175, |
| 64 | + 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128, |
| 65 | + 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68, |
| 66 | + 226, 0, 6, 11, 0, 1, 2, 3, 0, 0, 0, 2, 1, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 3, |
| 67 | + 232, 0, 0, 0, 1, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 29, 0, |
| 68 | + 0, 0, 1, 0, 0, 0, 125, 0, 0, 0, 0, 58, 85, 116, 216, 255, 2, 68, 226, 0, 6, 11, 0, 1, |
| 69 | + 1, |
| 70 | + ]; |
| 71 | + |
| 72 | + fs::create_dir_all("./tmp/graph-sync-tests").unwrap(); |
| 73 | + fs::write("./tmp/graph-sync-tests/test_data.lngossip", valid_response).unwrap(); |
| 74 | + |
| 75 | + let block_hash = genesis_block(Network::Bitcoin).block_hash(); |
| 76 | + let network_graph = NetworkGraph::new(block_hash); |
| 77 | + |
| 78 | + let before = network_graph.to_string(); |
| 79 | + assert_eq!(before.len(), 31); |
| 80 | + |
| 81 | + let sync_result = sync_network_graph_with_file_path( |
| 82 | + &network_graph, |
| 83 | + "./tmp/graph-sync-tests/test_data.lngossip", |
| 84 | + ); |
| 85 | + |
| 86 | + assert!(sync_result.is_ok()); |
| 87 | + |
| 88 | + let after = network_graph.to_string(); |
| 89 | + assert_eq!(after.len(), 1727); |
| 90 | + assert!( |
| 91 | + after.contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643:") |
| 92 | + ); |
| 93 | + assert!( |
| 94 | + after.contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b:") |
| 95 | + ); |
| 96 | + assert!( |
| 97 | + after.contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432:") |
| 98 | + ); |
| 99 | + assert!( |
| 100 | + after.contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61:") |
| 101 | + ); |
| 102 | + assert!(after.contains("channels: [619737530008010752]")); |
| 103 | + assert!(after.contains("channels: [783241506229452801]")); |
| 104 | + } |
| 105 | +} |
0 commit comments