@@ -1058,7 +1058,7 @@ mod tests {
1058
1058
use ln:: router:: { Router , NodeInfo , NetworkMap , ChannelInfo , DirectionalChannelInfo , RouteHint } ;
1059
1059
use ln:: features:: { ChannelFeatures , InitFeatures , NodeFeatures } ;
1060
1060
use ln:: msgs:: { ErrorAction , LightningError , RoutingMessageHandler , UnsignedNodeAnnouncement , NodeAnnouncement ,
1061
- UnsignedChannelAnnouncement , ChannelAnnouncement } ;
1061
+ UnsignedChannelAnnouncement , ChannelAnnouncement , UnsignedChannelUpdate , ChannelUpdate } ;
1062
1062
use util:: test_utils;
1063
1063
use util:: test_utils:: TestVecWriter ;
1064
1064
use util:: logger:: Logger ;
@@ -2079,4 +2079,154 @@ mod tests {
2079
2079
Err ( e) => assert_eq ! ( e. err, "Channel announcement node had a channel with itself" )
2080
2080
} ;
2081
2081
}
2082
+
2083
+ #[ test]
2084
+ fn handling_channel_update ( ) {
2085
+ let ( secp_ctx, _, router) = create_router ( ) ;
2086
+ let secret_key = & SecretKey :: from_slice ( & hex:: decode ( "0202020202020202020202020202020202020202020202020202020202020202" ) . unwrap ( ) [ ..] ) . unwrap ( ) ;
2087
+ let public_key = PublicKey :: from_secret_key ( & secp_ctx, secret_key) ;
2088
+
2089
+ let zero_hash = Sha256dHash :: hash ( & [ 0 ; 32 ] ) ;
2090
+ let channel_id = 0 ;
2091
+ let channel_time = 100 ;
2092
+ let channel_key = NetworkMap :: get_key ( channel_id, genesis_block ( Network :: Testnet ) . header . bitcoin_hash ( ) ) ;
2093
+
2094
+ {
2095
+ let mut network = router. network_map . write ( ) . unwrap ( ) ;
2096
+ network. channels . insert ( channel_key, ChannelInfo {
2097
+ features : ChannelFeatures :: empty ( ) ,
2098
+ one_to_two : DirectionalChannelInfo {
2099
+ src_node_id : public_key. clone ( ) ,
2100
+ last_update : channel_time,
2101
+ enabled : false ,
2102
+ cltv_expiry_delta : u16:: max_value ( ) ,
2103
+ htlc_minimum_msat : 0 ,
2104
+ fee_base_msat : u32:: max_value ( ) ,
2105
+ fee_proportional_millionths : u32:: max_value ( ) ,
2106
+ last_update_message : None ,
2107
+ } , two_to_one : DirectionalChannelInfo {
2108
+ src_node_id : public_key. clone ( ) ,
2109
+ last_update : channel_time,
2110
+ enabled : true ,
2111
+ cltv_expiry_delta : 0 ,
2112
+ htlc_minimum_msat : 0 ,
2113
+ fee_base_msat : u32:: max_value ( ) ,
2114
+ fee_proportional_millionths : 0 ,
2115
+ last_update_message : None ,
2116
+ } ,
2117
+ announcement_message : None ,
2118
+ } ) ;
2119
+ }
2120
+
2121
+ let mut unsigned_channel_update = UnsignedChannelUpdate {
2122
+ chain_hash : genesis_block ( Network :: Testnet ) . header . bitcoin_hash ( ) ,
2123
+ short_channel_id : channel_id,
2124
+ timestamp : channel_time + 100 ,
2125
+ flags : 0 ,
2126
+ cltv_expiry_delta : 144 ,
2127
+ htlc_minimum_msat : 1000000 ,
2128
+ fee_base_msat : 10000 ,
2129
+ fee_proportional_millionths : 20 ,
2130
+ excess_data : Vec :: new ( )
2131
+ } ;
2132
+ let mut msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_channel_update. encode( ) [ ..] ) [ ..] ) ;
2133
+ let valid_channel_update = ChannelUpdate {
2134
+ signature : secp_ctx. sign ( & msghash, secret_key) ,
2135
+ contents : unsigned_channel_update. clone ( )
2136
+ } ;
2137
+
2138
+ // TODO: local node (not test) causes panic because remote node does not exist.
2139
+ // Should this be handled?
2140
+ // match router.handle_channel_update(&valid_channel_update) {
2141
+ // Ok(res) => assert!(res),
2142
+ // Err(e) => {
2143
+ // println!("{}", e.err);
2144
+ // panic!();
2145
+ // }
2146
+ // };
2147
+
2148
+ {
2149
+ let mut network = router. network_map . write ( ) . unwrap ( ) ;
2150
+ network. nodes . insert ( public_key. clone ( ) , NodeInfo {
2151
+ channels : vec ! ( channel_key) ,
2152
+ lowest_inbound_channel_fee_base_msat : u32:: max_value ( ) ,
2153
+ lowest_inbound_channel_fee_proportional_millionths : 0 ,
2154
+ features : NodeFeatures :: empty ( ) ,
2155
+ last_update : Some ( 1 ) ,
2156
+ rgb : [ 0 ; 3 ] ,
2157
+ alias : [ 0 ; 32 ] ,
2158
+ addresses : Vec :: new ( ) ,
2159
+ announcement_message : None ,
2160
+ } ) ;
2161
+ }
2162
+
2163
+ match router. handle_channel_update ( & valid_channel_update) {
2164
+ Ok ( res) => assert ! ( res) ,
2165
+ _ => panic ! ( )
2166
+ } ;
2167
+
2168
+ {
2169
+ let mut network = router. network_map . write ( ) . unwrap ( ) ;
2170
+ match network. channels . get_mut ( & channel_key) {
2171
+ None => panic ! ( ) ,
2172
+ Some ( channel_info) => {
2173
+ assert_eq ! ( channel_info. one_to_two. cltv_expiry_delta, 144 ) ;
2174
+ assert_eq ! ( channel_info. two_to_one. cltv_expiry_delta, 0 ) ;
2175
+ }
2176
+ }
2177
+ let node = network. nodes . get_mut ( & public_key) . unwrap ( ) ;
2178
+ assert_eq ! ( node. lowest_inbound_channel_fee_base_msat, 10000 ) ;
2179
+ }
2180
+
2181
+ unsigned_channel_update. timestamp += 100 ;
2182
+ unsigned_channel_update. excess_data . push ( 1 ) ;
2183
+ msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_channel_update. encode( ) [ ..] ) [ ..] ) ;
2184
+ let valid_channel_update = ChannelUpdate {
2185
+ signature : secp_ctx. sign ( & msghash, secret_key) ,
2186
+ contents : unsigned_channel_update. clone ( )
2187
+ } ;
2188
+ // Return false because contains excess data
2189
+ match router. handle_channel_update ( & valid_channel_update) {
2190
+ Ok ( res) => assert ! ( !res) ,
2191
+ _ => panic ! ( )
2192
+ } ;
2193
+
2194
+ unsigned_channel_update. short_channel_id += 1 ;
2195
+ msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_channel_update. encode( ) [ ..] ) [ ..] ) ;
2196
+ let valid_channel_update = ChannelUpdate {
2197
+ signature : secp_ctx. sign ( & msghash, secret_key) ,
2198
+ contents : unsigned_channel_update. clone ( )
2199
+ } ;
2200
+
2201
+ match router. handle_channel_update ( & valid_channel_update) {
2202
+ Ok ( _) => panic ! ( ) ,
2203
+ Err ( e) => assert_eq ! ( e. err, "Couldn't find channel for update" )
2204
+ } ;
2205
+ unsigned_channel_update. short_channel_id = channel_id;
2206
+
2207
+ unsigned_channel_update. timestamp = 0 ;
2208
+ msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_channel_update. encode( ) [ ..] ) [ ..] ) ;
2209
+ let valid_channel_update = ChannelUpdate {
2210
+ signature : secp_ctx. sign ( & msghash, secret_key) ,
2211
+ contents : unsigned_channel_update. clone ( )
2212
+ } ;
2213
+
2214
+ match router. handle_channel_update ( & valid_channel_update) {
2215
+ Ok ( _) => panic ! ( ) ,
2216
+ Err ( e) => assert_eq ! ( e. err, "Update older than last processed update" )
2217
+ } ;
2218
+ unsigned_channel_update. timestamp = channel_time;
2219
+
2220
+ let fake_msghash = hash_to_message ! ( & zero_hash) ;
2221
+ let invalid_sig_channel_update = ChannelUpdate {
2222
+ signature : secp_ctx. sign ( & fake_msghash, secret_key) ,
2223
+ contents : unsigned_channel_update. clone ( )
2224
+ } ;
2225
+
2226
+ match router. handle_channel_update ( & invalid_sig_channel_update) {
2227
+ Ok ( _) => panic ! ( ) ,
2228
+ Err ( e) => assert_eq ! ( e. err, "Invalid signature from remote node" )
2229
+ } ;
2230
+
2231
+ }
2082
2232
}
0 commit comments