Skip to content

Commit 11f9547

Browse files
committed
Remove mutable accessors from NetworkGraph
1 parent 7204fde commit 11f9547

File tree

2 files changed

+708
-716
lines changed

2 files changed

+708
-716
lines changed

lightning/src/routing/network_graph.rs

Lines changed: 51 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,6 @@ impl Readable for NodeInfo {
441441
#[derive(PartialEq)]
442442
pub struct NetworkGraph {
443443
channels: BTreeMap<u64, ChannelInfo>,
444-
/// A set of known Lightning nodes and their properties
445444
nodes: BTreeMap<PublicKey, NodeInfo>,
446445
}
447446

@@ -501,20 +500,8 @@ impl std::fmt::Display for NetworkGraph {
501500
impl NetworkGraph {
502501
/// Returns a list of known valid channels
503502
pub fn get_channels<'a>(&'a self) -> &'a BTreeMap<u64, ChannelInfo> { &self.channels }
504-
/// Returns a mutable reference to an existing channel
505-
pub fn get_channel_mut<'a>(&'a mut self, chan_id: &u64) -> Option<&mut ChannelInfo> { self.channels.get_mut(chan_id) }
506-
/// Store a valid channel
507-
pub fn add_channel(&mut self, chan_id: u64, channel_info: ChannelInfo) {
508-
self.channels.insert(chan_id, channel_info);
509-
}
510503
/// Returns a list of known nodes
511504
pub fn get_nodes<'a>(&'a self) -> &'a BTreeMap<PublicKey, NodeInfo> { &self.nodes }
512-
/// Returns a mutable reference to an existing node
513-
pub fn get_node_mut<'a>(&'a mut self, node_id: &PublicKey) -> Option<&mut NodeInfo> { self.nodes.get_mut(node_id) }
514-
/// Store a valid node
515-
pub fn add_node(&mut self, node_id: PublicKey, node_info: NodeInfo) {
516-
self.nodes.insert(node_id, node_info);
517-
}
518505

519506
fn process_node_announcement(&mut self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
520507
match self.nodes.get_mut(&msg.contents.node_id) {
@@ -756,7 +743,7 @@ impl NetworkGraph {
756743
mod tests {
757744
use chain::chaininterface;
758745
use ln::features::{ChannelFeatures, NodeFeatures};
759-
use routing::network_graph::{NetGraphMsgHandler, NetworkGraph, DirectionalChannelInfo, ChannelInfo, NodeInfo, RoutingFees};
746+
use routing::network_graph::{NetGraphMsgHandler, NetworkGraph};
760747
use ln::msgs::{RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
761748
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate, HTLCFailChannelUpdate};
762749
use util::test_utils;
@@ -1560,60 +1547,69 @@ mod tests {
15601547

15611548
#[test]
15621549
fn network_graph_serialization() {
1563-
let (secp_ctx, our_id, net_graph_msg_handler) = create_net_graph_msg_handler();
1564-
let node1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1565-
let node1 = PublicKey::from_secret_key(&secp_ctx, node1_privkey);
1550+
let (secp_ctx, _, net_graph_msg_handler) = create_net_graph_msg_handler();
15661551

1567-
let mut network = net_graph_msg_handler.network_graph.write().unwrap();
1568-
let zero_hash = Sha256dHash::hash(&[0; 32]);
1552+
let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1553+
let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1554+
let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1555+
let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
15691556

1557+
// Announce a channel to add a corresponding node.
1558+
let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1559+
let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1560+
let unsigned_announcement = UnsignedChannelAnnouncement {
1561+
features: ChannelFeatures::supported(),
1562+
chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
1563+
short_channel_id: 0,
1564+
node_id_1,
1565+
node_id_2,
1566+
bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1567+
bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1568+
excess_data: Vec::new(),
1569+
};
15701570

1571-
let node_routing_fees = RoutingFees {
1572-
base_msat: 100,
1573-
proportional_millionths: 0,
1571+
let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1572+
let valid_announcement = ChannelAnnouncement {
1573+
node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1574+
node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1575+
bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1576+
bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1577+
contents: unsigned_announcement.clone(),
15741578
};
1575-
network.add_node(node1.clone(), NodeInfo {
1576-
channels: vec!(1, 3),
1577-
lowest_inbound_channel_fees: Some(node_routing_fees),
1579+
match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1580+
Ok(res) => assert!(res),
1581+
_ => panic!()
1582+
};
1583+
1584+
1585+
let node_id = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1586+
let unsigned_announcement = UnsignedNodeAnnouncement {
15781587
features: NodeFeatures::supported(),
1579-
last_update: Some(1),
1588+
timestamp: 100,
1589+
node_id,
15801590
rgb: [0; 3],
15811591
alias: [0; 32],
15821592
addresses: Vec::new(),
1583-
announcement_message: None,
1584-
});
1585-
network.add_channel(1, ChannelInfo {
1586-
features: ChannelFeatures::supported(),
1587-
one_to_two: DirectionalChannelInfo {
1588-
src_node_id: our_id.clone(),
1589-
last_update: 0,
1590-
enabled: false,
1591-
cltv_expiry_delta: u16::max_value(),
1592-
htlc_minimum_msat: 0,
1593-
fees: RoutingFees {
1594-
base_msat: u32::max_value(),
1595-
proportional_millionths: u32::max_value(),
1596-
},
1597-
last_update_message: None,
1598-
}, two_to_one: DirectionalChannelInfo {
1599-
src_node_id: node1.clone(),
1600-
last_update: 0,
1601-
enabled: true,
1602-
cltv_expiry_delta: 0,
1603-
htlc_minimum_msat: 0,
1604-
fees: RoutingFees {
1605-
base_msat: u32::max_value(),
1606-
proportional_millionths: u32::max_value(),
1607-
},
1608-
last_update_message: None,
1609-
},
1610-
announcement_message: None,
1611-
});
1593+
excess_address_data: Vec::new(),
1594+
excess_data: Vec::new(),
1595+
};
1596+
let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1597+
let valid_announcement = NodeAnnouncement {
1598+
signature: secp_ctx.sign(&msghash, node_1_privkey),
1599+
contents: unsigned_announcement.clone()
1600+
};
1601+
1602+
match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
1603+
Ok(_) => (),
1604+
Err(_) => panic!()
1605+
};
16121606

1607+
let network = net_graph_msg_handler.network_graph.write().unwrap();
16131608
let mut w = test_utils::TestVecWriter(Vec::new());
16141609
assert!(!network.channels.is_empty());
16151610
assert!(!network.nodes.is_empty());
16161611
network.write(&mut w).unwrap();
16171612
assert!(<NetworkGraph>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == *network);
16181613
}
1614+
16191615
}

0 commit comments

Comments
 (0)