@@ -29,7 +29,7 @@ use ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds, Reply
29
29
use ln:: msgs;
30
30
use util:: ser:: { Writeable , Readable , Writer } ;
31
31
use util:: logger:: { Logger , Level } ;
32
- use util:: events:: { MessageSendEvent , MessageSendEventsProvider } ;
32
+ use util:: events:: { Event , EventHandler , MessageSendEvent , MessageSendEventsProvider } ;
33
33
use util:: scid_utils:: { block_from_scid, scid_from_parts, MAX_SCID_BLOCK } ;
34
34
35
35
use io;
@@ -110,12 +110,28 @@ impl_writeable_tlv_based_enum_upgradable!(NetworkUpdate,
110
110
} ,
111
111
) ;
112
112
113
+ impl < C : Deref , L : Deref > EventHandler for NetGraphMsgHandler < C , L >
114
+ where C :: Target : chain:: Access , L :: Target : Logger {
115
+ fn handle_event ( & self , event : & Event ) {
116
+ if let Event :: PaymentFailed { payment_hash : _, rejected_by_dest : _, network_update, .. } = event {
117
+ if let Some ( network_update) = network_update {
118
+ self . handle_network_update ( network_update) ;
119
+ }
120
+ }
121
+ }
122
+ }
123
+
113
124
/// Receives and validates network updates from peers,
114
125
/// stores authentic and relevant data as a network graph.
115
126
/// This network graph is then used for routing payments.
116
127
/// Provides interface to help with initial routing sync by
117
128
/// serving historical announcements.
118
- pub struct NetGraphMsgHandler < C : Deref , L : Deref > where C :: Target : chain:: Access , L :: Target : Logger {
129
+ ///
130
+ /// Serves as an [`EventHandler`] for applying updates from [`Event::PaymentFailed`] to the
131
+ /// [`NetworkGraph`].
132
+ pub struct NetGraphMsgHandler < C : Deref , L : Deref >
133
+ where C :: Target : chain:: Access , L :: Target : Logger
134
+ {
119
135
secp_ctx : Secp256k1 < secp256k1:: VerifyOnly > ,
120
136
/// Representation of the payment channel network
121
137
pub network_graph : NetworkGraph ,
@@ -125,26 +141,15 @@ pub struct NetGraphMsgHandler<C: Deref, L: Deref> where C::Target: chain::Access
125
141
logger : L ,
126
142
}
127
143
128
- impl < C : Deref , L : Deref > NetGraphMsgHandler < C , L > where C :: Target : chain:: Access , L :: Target : Logger {
144
+ impl < C : Deref , L : Deref > NetGraphMsgHandler < C , L >
145
+ where C :: Target : chain:: Access , L :: Target : Logger
146
+ {
129
147
/// Creates a new tracker of the actual state of the network of channels and nodes,
130
- /// assuming a fresh network graph .
148
+ /// assuming an existing Network Graph .
131
149
/// Chain monitor is used to make sure announced channels exist on-chain,
132
150
/// channel data is correct, and that the announcement is signed with
133
151
/// channel owners' keys.
134
- pub fn new ( genesis_hash : BlockHash , chain_access : Option < C > , logger : L ) -> Self {
135
- NetGraphMsgHandler {
136
- secp_ctx : Secp256k1 :: verification_only ( ) ,
137
- network_graph : NetworkGraph :: new ( genesis_hash) ,
138
- full_syncs_requested : AtomicUsize :: new ( 0 ) ,
139
- chain_access,
140
- pending_events : Mutex :: new ( vec ! [ ] ) ,
141
- logger,
142
- }
143
- }
144
-
145
- /// Creates a new tracker of the actual state of the network of channels and nodes,
146
- /// assuming an existing Network Graph.
147
- pub fn from_net_graph ( chain_access : Option < C > , logger : L , network_graph : NetworkGraph ) -> Self {
152
+ pub fn new ( network_graph : NetworkGraph , chain_access : Option < C > , logger : L ) -> Self {
148
153
NetGraphMsgHandler {
149
154
secp_ctx : Secp256k1 :: verification_only ( ) ,
150
155
network_graph,
@@ -173,6 +178,29 @@ impl<C: Deref, L: Deref> NetGraphMsgHandler<C, L> where C::Target: chain::Access
173
178
false
174
179
}
175
180
}
181
+
182
+ /// Applies changes to the [`NetworkGraph`] from the given update.
183
+ fn handle_network_update ( & self , update : & NetworkUpdate ) {
184
+ match * update {
185
+ NetworkUpdate :: ChannelUpdateMessage { ref msg } => {
186
+ let short_channel_id = msg. contents . short_channel_id ;
187
+ let is_enabled = msg. contents . flags & ( 1 << 1 ) != ( 1 << 1 ) ;
188
+ let status = if is_enabled { "enabled" } else { "disabled" } ;
189
+ log_debug ! ( self . logger, "Updating channel with channel_update from a payment failure. Channel {} is {}." , short_channel_id, status) ;
190
+ let _ = self . network_graph . update_channel ( msg, & self . secp_ctx ) ;
191
+ } ,
192
+ NetworkUpdate :: ChannelClosed { short_channel_id, is_permanent } => {
193
+ let action = if is_permanent { "Removing" } else { "Disabling" } ;
194
+ log_debug ! ( self . logger, "{} channel graph entry for {} due to a payment failure." , action, short_channel_id) ;
195
+ self . network_graph . close_channel_from_update ( short_channel_id, is_permanent) ;
196
+ } ,
197
+ NetworkUpdate :: NodeFailure { ref node_id, is_permanent } => {
198
+ let action = if is_permanent { "Removing" } else { "Disabling" } ;
199
+ log_debug ! ( self . logger, "{} node graph entry for {} due to a payment failure." , action, node_id) ;
200
+ self . network_graph . fail_node ( node_id, is_permanent) ;
201
+ } ,
202
+ }
203
+ }
176
204
}
177
205
178
206
macro_rules! secp_verify_sig {
@@ -184,7 +212,9 @@ macro_rules! secp_verify_sig {
184
212
} ;
185
213
}
186
214
187
- impl < C : Deref , L : Deref > RoutingMessageHandler for NetGraphMsgHandler < C , L > where C :: Target : chain:: Access , L :: Target : Logger {
215
+ impl < C : Deref , L : Deref > RoutingMessageHandler for NetGraphMsgHandler < C , L >
216
+ where C :: Target : chain:: Access , L :: Target : Logger
217
+ {
188
218
fn handle_node_announcement ( & self , msg : & msgs:: NodeAnnouncement ) -> Result < bool , LightningError > {
189
219
self . network_graph . update_node_from_announcement ( msg, & self . secp_ctx ) ?;
190
220
Ok ( msg. contents . excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
@@ -1116,15 +1146,16 @@ impl ReadOnlyNetworkGraph<'_> {
1116
1146
#[ cfg( test) ]
1117
1147
mod tests {
1118
1148
use chain;
1149
+ use ln:: PaymentHash ;
1119
1150
use ln:: features:: { ChannelFeatures , InitFeatures , NodeFeatures } ;
1120
- use routing:: network_graph:: { NetGraphMsgHandler , NetworkGraph , MAX_EXCESS_BYTES_FOR_RELAY } ;
1151
+ use routing:: network_graph:: { NetGraphMsgHandler , NetworkGraph , NetworkUpdate , MAX_EXCESS_BYTES_FOR_RELAY } ;
1121
1152
use ln:: msgs:: { Init , OptionalField , RoutingMessageHandler , UnsignedNodeAnnouncement , NodeAnnouncement ,
1122
1153
UnsignedChannelAnnouncement , ChannelAnnouncement , UnsignedChannelUpdate , ChannelUpdate ,
1123
1154
ReplyChannelRange , ReplyShortChannelIdsEnd , QueryChannelRange , QueryShortChannelIds , MAX_VALUE_MSAT } ;
1124
1155
use util:: test_utils;
1125
1156
use util:: logger:: Logger ;
1126
1157
use util:: ser:: { Readable , Writeable } ;
1127
- use util:: events:: { MessageSendEvent , MessageSendEventsProvider } ;
1158
+ use util:: events:: { Event , EventHandler , MessageSendEvent , MessageSendEventsProvider } ;
1128
1159
use util:: scid_utils:: scid_from_parts;
1129
1160
1130
1161
use bitcoin:: hashes:: sha256d:: Hash as Sha256dHash ;
@@ -1148,7 +1179,8 @@ mod tests {
1148
1179
let secp_ctx = Secp256k1 :: new ( ) ;
1149
1180
let logger = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
1150
1181
let genesis_hash = genesis_block ( Network :: Testnet ) . header . block_hash ( ) ;
1151
- let net_graph_msg_handler = NetGraphMsgHandler :: new ( genesis_hash, None , Arc :: clone ( & logger) ) ;
1182
+ let network_graph = NetworkGraph :: new ( genesis_hash) ;
1183
+ let net_graph_msg_handler = NetGraphMsgHandler :: new ( network_graph, None , Arc :: clone ( & logger) ) ;
1152
1184
( secp_ctx, net_graph_msg_handler)
1153
1185
}
1154
1186
@@ -1309,7 +1341,8 @@ mod tests {
1309
1341
} ;
1310
1342
1311
1343
// Test if the UTXO lookups were not supported
1312
- let mut net_graph_msg_handler = NetGraphMsgHandler :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) , None , Arc :: clone ( & logger) ) ;
1344
+ let network_graph = NetworkGraph :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) ) ;
1345
+ let mut net_graph_msg_handler = NetGraphMsgHandler :: new ( network_graph, None , Arc :: clone ( & logger) ) ;
1313
1346
match net_graph_msg_handler. handle_channel_announcement ( & valid_announcement) {
1314
1347
Ok ( res) => assert ! ( res) ,
1315
1348
_ => panic ! ( )
@@ -1333,7 +1366,8 @@ mod tests {
1333
1366
// Test if an associated transaction were not on-chain (or not confirmed).
1334
1367
let chain_source = Arc :: new ( test_utils:: TestChainSource :: new ( Network :: Testnet ) ) ;
1335
1368
* chain_source. utxo_ret . lock ( ) . unwrap ( ) = Err ( chain:: AccessError :: UnknownTx ) ;
1336
- net_graph_msg_handler = NetGraphMsgHandler :: new ( chain_source. clone ( ) . genesis_hash , Some ( chain_source. clone ( ) ) , Arc :: clone ( & logger) ) ;
1369
+ let network_graph = NetworkGraph :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) ) ;
1370
+ net_graph_msg_handler = NetGraphMsgHandler :: new ( network_graph, Some ( chain_source. clone ( ) ) , Arc :: clone ( & logger) ) ;
1337
1371
unsigned_announcement. short_channel_id += 1 ;
1338
1372
1339
1373
msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
@@ -1457,7 +1491,8 @@ mod tests {
1457
1491
let secp_ctx = Secp256k1 :: new ( ) ;
1458
1492
let logger: Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
1459
1493
let chain_source = Arc :: new ( test_utils:: TestChainSource :: new ( Network :: Testnet ) ) ;
1460
- let net_graph_msg_handler = NetGraphMsgHandler :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) , Some ( chain_source. clone ( ) ) , Arc :: clone ( & logger) ) ;
1494
+ let network_graph = NetworkGraph :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) ) ;
1495
+ let net_graph_msg_handler = NetGraphMsgHandler :: new ( network_graph, Some ( chain_source. clone ( ) ) , Arc :: clone ( & logger) ) ;
1461
1496
1462
1497
let node_1_privkey = & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ;
1463
1498
let node_2_privkey = & SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ;
@@ -1621,8 +1656,14 @@ mod tests {
1621
1656
}
1622
1657
1623
1658
#[ test]
1624
- fn handling_htlc_fail_channel_update ( ) {
1625
- let ( secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler ( ) ;
1659
+ fn handling_network_update ( ) {
1660
+ let logger = test_utils:: TestLogger :: new ( ) ;
1661
+ let chain_source = Arc :: new ( test_utils:: TestChainSource :: new ( Network :: Testnet ) ) ;
1662
+ let genesis_hash = genesis_block ( Network :: Testnet ) . header . block_hash ( ) ;
1663
+ let network_graph = NetworkGraph :: new ( genesis_hash) ;
1664
+ let net_graph_msg_handler = NetGraphMsgHandler :: new ( network_graph, Some ( chain_source. clone ( ) ) , & logger) ;
1665
+ let secp_ctx = Secp256k1 :: new ( ) ;
1666
+
1626
1667
let node_1_privkey = & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ;
1627
1668
let node_2_privkey = & SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ;
1628
1669
let node_id_1 = PublicKey :: from_secret_key ( & secp_ctx, node_1_privkey) ;
@@ -1632,11 +1673,11 @@ mod tests {
1632
1673
1633
1674
let short_channel_id = 0 ;
1634
1675
let chain_hash = genesis_block ( Network :: Testnet ) . header . block_hash ( ) ;
1676
+ let network_graph = & net_graph_msg_handler. network_graph ;
1635
1677
1636
1678
{
1637
1679
// There is no nodes in the table at the beginning.
1638
- let network = & net_graph_msg_handler. network_graph ;
1639
- assert_eq ! ( network. read_only( ) . nodes( ) . len( ) , 0 ) ;
1680
+ assert_eq ! ( network_graph. read_only( ) . nodes( ) . len( ) , 0 ) ;
1640
1681
}
1641
1682
1642
1683
{
@@ -1660,10 +1701,9 @@ mod tests {
1660
1701
bitcoin_signature_2 : secp_ctx. sign ( & msghash, node_2_btckey) ,
1661
1702
contents : unsigned_announcement. clone ( ) ,
1662
1703
} ;
1663
- match net_graph_msg_handler. handle_channel_announcement ( & valid_channel_announcement) {
1664
- Ok ( _) => ( ) ,
1665
- Err ( _) => panic ! ( )
1666
- } ;
1704
+ let chain_source: Option < & test_utils:: TestChainSource > = None ;
1705
+ assert ! ( network_graph. update_channel_from_announcement( & valid_channel_announcement, & chain_source, & secp_ctx) . is_ok( ) ) ;
1706
+ assert ! ( network_graph. read_only( ) . channels( ) . get( & short_channel_id) . is_some( ) ) ;
1667
1707
1668
1708
let unsigned_channel_update = UnsignedChannelUpdate {
1669
1709
chain_hash,
@@ -1683,44 +1723,65 @@ mod tests {
1683
1723
contents : unsigned_channel_update. clone ( )
1684
1724
} ;
1685
1725
1686
- match net_graph_msg_handler. handle_channel_update ( & valid_channel_update) {
1687
- Ok ( res) => assert ! ( res) ,
1688
- _ => panic ! ( )
1689
- } ;
1726
+ assert ! ( network_graph. read_only( ) . channels( ) . get( & short_channel_id) . unwrap( ) . one_to_two. is_none( ) ) ;
1727
+
1728
+ net_graph_msg_handler. handle_event ( & Event :: PaymentFailed {
1729
+ payment_hash : PaymentHash ( [ 0 ; 32 ] ) ,
1730
+ rejected_by_dest : false ,
1731
+ network_update : Some ( NetworkUpdate :: ChannelUpdateMessage {
1732
+ msg : valid_channel_update,
1733
+ } ) ,
1734
+ error_code : None ,
1735
+ error_data : None ,
1736
+ } ) ;
1737
+
1738
+ assert ! ( network_graph. read_only( ) . channels( ) . get( & short_channel_id) . unwrap( ) . one_to_two. is_some( ) ) ;
1690
1739
}
1691
1740
1692
1741
// Non-permanent closing just disables a channel
1693
1742
{
1694
- let network = & net_graph_msg_handler. network_graph ;
1695
- match network. read_only ( ) . channels ( ) . get ( & short_channel_id) {
1743
+ match network_graph. read_only ( ) . channels ( ) . get ( & short_channel_id) {
1696
1744
None => panic ! ( ) ,
1697
1745
Some ( channel_info) => {
1698
- assert ! ( channel_info. one_to_two. is_some ( ) ) ;
1746
+ assert ! ( channel_info. one_to_two. as_ref ( ) . unwrap ( ) . enabled ) ;
1699
1747
}
1700
1748
} ;
1701
- }
1702
1749
1703
- net_graph_msg_handler. network_graph . close_channel_from_update ( short_channel_id, false ) ;
1750
+ net_graph_msg_handler. handle_event ( & Event :: PaymentFailed {
1751
+ payment_hash : PaymentHash ( [ 0 ; 32 ] ) ,
1752
+ rejected_by_dest : false ,
1753
+ network_update : Some ( NetworkUpdate :: ChannelClosed {
1754
+ short_channel_id,
1755
+ is_permanent : false ,
1756
+ } ) ,
1757
+ error_code : None ,
1758
+ error_data : None ,
1759
+ } ) ;
1704
1760
1705
- // Non-permanent closing just disables a channel
1706
- {
1707
- let network = & net_graph_msg_handler. network_graph ;
1708
- match network. read_only ( ) . channels ( ) . get ( & short_channel_id) {
1761
+ match network_graph. read_only ( ) . channels ( ) . get ( & short_channel_id) {
1709
1762
None => panic ! ( ) ,
1710
1763
Some ( channel_info) => {
1711
1764
assert ! ( !channel_info. one_to_two. as_ref( ) . unwrap( ) . enabled) ;
1712
1765
}
1713
1766
} ;
1714
1767
}
1715
1768
1716
- net_graph_msg_handler. network_graph . close_channel_from_update ( short_channel_id, true ) ;
1717
-
1718
1769
// Permanent closing deletes a channel
1719
1770
{
1720
- let network = & net_graph_msg_handler. network_graph ;
1721
- assert_eq ! ( network. read_only( ) . channels( ) . len( ) , 0 ) ;
1771
+ net_graph_msg_handler. handle_event ( & Event :: PaymentFailed {
1772
+ payment_hash : PaymentHash ( [ 0 ; 32 ] ) ,
1773
+ rejected_by_dest : false ,
1774
+ network_update : Some ( NetworkUpdate :: ChannelClosed {
1775
+ short_channel_id,
1776
+ is_permanent : true ,
1777
+ } ) ,
1778
+ error_code : None ,
1779
+ error_data : None ,
1780
+ } ) ;
1781
+
1782
+ assert_eq ! ( network_graph. read_only( ) . channels( ) . len( ) , 0 ) ;
1722
1783
// Nodes are also deleted because there are no associated channels anymore
1723
- assert_eq ! ( network . read_only( ) . nodes( ) . len( ) , 0 ) ;
1784
+ assert_eq ! ( network_graph . read_only( ) . nodes( ) . len( ) , 0 ) ;
1724
1785
}
1725
1786
// TODO: Test NetworkUpdate::NodeFailure, which is not implemented yet.
1726
1787
}
0 commit comments