Skip to content

Commit f7ea5de

Browse files
Expand test to include mpp
1 parent 109a412 commit f7ea5de

File tree

1 file changed

+111
-78
lines changed

1 file changed

+111
-78
lines changed

lightning/src/routing/network_graph.rs

Lines changed: 111 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,16 +1753,30 @@ mod tests {
17531753

17541754
#[test]
17551755
fn network_graph_score_payment() {
1756-
// Set up a network that consists of just a channel between node_1 and node_2
1756+
// Set up a network that consists of the following channels:
1757+
// node_1 -> node_2
1758+
// node_1 -> node_3
1759+
// node_2 -> node_4
1760+
// node_3 -> node_4
1761+
// We can use this to simulate a MPP from node_1 to node_4 via node_2 and node_3.
17571762
let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1758-
let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1759-
let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1763+
let node_0_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1764+
let node_1_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1765+
let node_2_privkey = &SecretKey::from_slice(&[40; 32]).unwrap();
1766+
let node_3_privkey = &SecretKey::from_slice(&[39; 32]).unwrap();
1767+
let node_privkeys = vec![node_0_privkey, node_1_privkey, node_2_privkey, node_3_privkey];
1768+
let node_id_0 = PublicKey::from_secret_key(&secp_ctx, node_0_privkey);
17601769
let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
17611770
let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1762-
let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1763-
let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1764-
1765-
let short_channel_id = 0;
1771+
let node_id_3 = PublicKey::from_secret_key(&secp_ctx, node_3_privkey);
1772+
let node_ids = vec![node_id_0, node_id_1, node_id_2, node_id_3];
1773+
let node_0_btckey = &SecretKey::from_slice(&[38; 32]).unwrap();
1774+
let node_1_btckey = &SecretKey::from_slice(&[37; 32]).unwrap();
1775+
let node_2_btckey = &SecretKey::from_slice(&[36; 32]).unwrap();
1776+
let node_3_btckey = &SecretKey::from_slice(&[35; 32]).unwrap();
1777+
let node_btckeys = vec![node_0_btckey, node_1_btckey, node_2_btckey, node_3_btckey];
1778+
1779+
let mut short_channel_id = 0;
17661780
let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash();
17671781

17681782
{
@@ -1772,84 +1786,103 @@ mod tests {
17721786
}
17731787

17741788
{
1775-
// Announce a channel we will update
1776-
let unsigned_announcement = UnsignedChannelAnnouncement {
1777-
features: ChannelFeatures::empty(),
1778-
chain_hash,
1779-
short_channel_id,
1780-
node_id_1,
1781-
node_id_2,
1782-
bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1783-
bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1784-
excess_data: Vec::new(),
1785-
};
1789+
// Create channels between each node
1790+
for i in 0..=3 {
1791+
for j in 0..=3 {
1792+
if i >= j {
1793+
continue;
1794+
} else {
1795+
let node_i = node_ids[i];
1796+
let node_j = node_ids[j];
1797+
let unsigned_announcement = UnsignedChannelAnnouncement {
1798+
features: ChannelFeatures::empty(),
1799+
chain_hash,
1800+
short_channel_id,
1801+
node_id_1: node_i,
1802+
node_id_2: node_j,
1803+
bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_btckeys[i]),
1804+
bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_btckeys[j]),
1805+
excess_data: Vec::new(),
1806+
};
17861807

1787-
let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1788-
let valid_channel_announcement = ChannelAnnouncement {
1789-
node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1790-
node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1791-
bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1792-
bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1793-
contents: unsigned_announcement.clone(),
1794-
};
1795-
match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1796-
Ok(_) => (),
1797-
Err(_) => panic!()
1798-
};
1808+
let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1809+
let valid_channel_announcement = ChannelAnnouncement {
1810+
node_signature_1: secp_ctx.sign(&msghash, node_privkeys[i]),
1811+
node_signature_2: secp_ctx.sign(&msghash, node_privkeys[j]),
1812+
bitcoin_signature_1: secp_ctx.sign(&msghash, node_btckeys[i]),
1813+
bitcoin_signature_2: secp_ctx.sign(&msghash, node_btckeys[j]),
1814+
contents: unsigned_announcement.clone(),
1815+
};
1816+
match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1817+
Ok(_) => (),
1818+
Err(_) => panic!()
1819+
};
1820+
short_channel_id += 1;
1821+
}
1822+
}
1823+
}
1824+
// There is no nodes in the table at the beginning.
1825+
let network = net_graph_msg_handler.network_graph.read().unwrap();
1826+
assert_eq!(network.get_nodes().len(), 4);
1827+
assert_eq!(network.get_channels().len(), 6);
17991828
}
18001829

18011830
{
1802-
// At the start, the node_1 <-> node_2 channel's DirectionalChannelInfos should be 0.
1831+
// Create the DirectionalChannelInfo, RoutingFee and ChannelScore objects for each
1832+
// channel
18031833
let mut network = net_graph_msg_handler.network_graph.write().unwrap();
1804-
let chan_id = network.get_nodes().get(&node_id_1).unwrap().channels[0];
1805-
let channel_info = network.channels.get_mut(&chan_id).unwrap();
1806-
// Assign a DirectionalChannelInfo and ChannelScore object to alice_to_bob of
1807-
// channel_info
1808-
let chan_score_1 = ChannelScore {
1809-
payment_sent_score: 0,
1810-
payment_failed_score: 0,
1811-
};
1812-
let chan_score_2 = ChannelScore {
1813-
payment_sent_score: 0,
1814-
payment_failed_score: 0,
1815-
};
1834+
for channel_id in 0..short_channel_id {
1835+
//let chan_id = network.get_nodes().get(&node_ids[0]).unwrap().channels[0];
1836+
let channel_info = network.channels.get_mut(&channel_id).unwrap();
1837+
// Assign a DirectionalChannelInfo and ChannelScore object to alice_to_bob of
1838+
// channel_info
1839+
let chan_score_alice_to_bob = ChannelScore {
1840+
payment_sent_score: 0,
1841+
payment_failed_score: 0,
1842+
};
1843+
let chan_score_bob_to_alice = ChannelScore {
1844+
payment_sent_score: 0,
1845+
payment_failed_score: 0,
1846+
};
18161847

1817-
let dummy_routing_fee = RoutingFees {
1818-
base_msat: 0,
1819-
proportional_millionths: 0,
1820-
};
1821-
let dir_chan_info_1 = DirectionalChannelInfo {
1822-
last_update: 0,
1823-
enabled: true,
1824-
cltv_expiry_delta: 0,
1825-
htlc_minimum_msat: 0,
1826-
fees: dummy_routing_fee,
1827-
last_update_message: None,
1828-
channel_score: chan_score_1,
1829-
};
1848+
let dummy_routing_fee = RoutingFees {
1849+
base_msat: 0,
1850+
proportional_millionths: 0,
1851+
};
1852+
let dir_chan_info_alice_to_bob = DirectionalChannelInfo {
1853+
last_update: 0,
1854+
enabled: true,
1855+
cltv_expiry_delta: 0,
1856+
htlc_minimum_msat: 0,
1857+
fees: dummy_routing_fee,
1858+
last_update_message: None,
1859+
channel_score: chan_score_alice_to_bob,
1860+
};
18301861

1831-
let dir_chan_info_2 = DirectionalChannelInfo {
1832-
last_update: 0,
1833-
enabled: true,
1834-
cltv_expiry_delta: 0,
1835-
htlc_minimum_msat: 0,
1836-
fees: dummy_routing_fee,
1837-
last_update_message: None,
1838-
channel_score: chan_score_2,
1839-
};
1862+
let dir_chan_info_bob_to_alice = DirectionalChannelInfo {
1863+
last_update: 0,
1864+
enabled: true,
1865+
cltv_expiry_delta: 0,
1866+
htlc_minimum_msat: 0,
1867+
fees: dummy_routing_fee,
1868+
last_update_message: None,
1869+
channel_score: chan_score_bob_to_alice,
1870+
};
18401871

1841-
channel_info.alice_to_bob = Some(dir_chan_info_1);
1842-
channel_info.bob_to_alice = Some(dir_chan_info_2);
1843-
let dir_alice_to_bob = channel_info.alice_to_bob.as_mut().unwrap();
1844-
assert_eq!(dir_alice_to_bob.channel_score.payment_sent_score, 0);
1845-
assert_eq!(dir_alice_to_bob.channel_score.payment_failed_score, 0);
1872+
channel_info.alice_to_bob = Some(dir_chan_info_alice_to_bob);
1873+
channel_info.bob_to_alice = Some(dir_chan_info_bob_to_alice);
1874+
let dir_alice_to_bob = channel_info.alice_to_bob.as_mut().unwrap();
1875+
assert_eq!(dir_alice_to_bob.channel_score.payment_sent_score, 0);
1876+
assert_eq!(dir_alice_to_bob.channel_score.payment_failed_score, 0);
1877+
}
18461878
}
18471879

1880+
// From here on out we simulate the network learning of payments.
18481881
{
1849-
// Assume a payment is made, and node 1 is learning of a PaymentSent event. It now calls
1850-
// its score_payment_sent_for_route on itself.
1882+
// Assume a PaymentSent event is picked up and it consists of a simple payment from
1883+
// node_0 to node_1
18511884
let route_hop = RouteHop {
1852-
pubkey: node_id_1,
1885+
pubkey: node_ids[0],
18531886
node_features: NodeFeatures::empty(),
18541887
short_channel_id: 0,
18551888
channel_features: ChannelFeatures::empty(),
@@ -1862,7 +1895,7 @@ mod tests {
18621895
let sent_res = network.score_payment_sent_for_route(sent_route);
18631896
assert!(sent_res.unwrap() == true);
18641897
// Check that score_payment_sent_for_route incremented the appropriate ChannelScore
1865-
let chan_id = network.get_nodes().get(&node_id_1).unwrap().channels[0];
1898+
let chan_id = network.get_nodes().get(&node_ids[0]).unwrap().channels[0];
18661899
let channel_info = network.channels.get_mut(&chan_id).unwrap();
18671900
let dir_alice_to_bob = &channel_info.alice_to_bob.as_ref();
18681901
assert_eq!(dir_alice_to_bob.unwrap().channel_score.payment_sent_score, 1);
@@ -1871,7 +1904,7 @@ mod tests {
18711904

18721905
{
18731906
let route_hop = RouteHop {
1874-
pubkey: node_id_1,
1907+
pubkey: node_ids[0],
18751908
node_features: NodeFeatures::empty(),
18761909
short_channel_id: 0,
18771910
channel_features: ChannelFeatures::empty(),
@@ -1881,15 +1914,15 @@ mod tests {
18811914
// Assume a payment fails due to node_1 (identified by its PublicKey), verify that its
18821915
// directional channel's score (node_1 -> node_2) has its PaymentFailed score
18831916
// incremented.
1884-
let faultive_nodes: Vec<PublicKey> = vec![node_id_1];
1917+
let faultive_nodes: Vec<PublicKey> = vec![node_ids[0]];
18851918
let attempted_path: Vec<RouteHop> = vec![route_hop];
18861919
let failed_route: Vec<Vec<RouteHop>> = vec![attempted_path];
18871920
let mut network = net_graph_msg_handler.network_graph.write().unwrap();
18881921
let failed_res = network.score_payment_failed_for_route(failed_route, faultive_nodes);
18891922
assert!(failed_res.unwrap() == true);
1890-
let chan_id = network.get_nodes().get(&node_id_1).unwrap().channels[0];
1923+
let chan_id = network.get_nodes().get(&node_ids[0]).unwrap().channels[0];
18911924
let channel_info = network.channels.get_mut(&chan_id).unwrap();
1892-
assert!(channel_info.node_two == node_id_2);
1925+
assert!(channel_info.node_two == node_ids[1]);
18931926
let dir_alice_to_bob = &channel_info.alice_to_bob.as_ref();
18941927
assert_eq!(dir_alice_to_bob.unwrap().channel_score.payment_sent_score, 1);
18951928
assert_eq!(dir_alice_to_bob.unwrap().channel_score.payment_failed_score, 1);

0 commit comments

Comments
 (0)