@@ -11,7 +11,7 @@ use bitcoin::blockdata::opcodes;
11
11
12
12
use chain:: chaininterface:: { ChainError , ChainWatchInterface } ;
13
13
use ln:: features:: { ChannelFeatures , NodeFeatures } ;
14
- use ln:: msgs:: { DecodeError , ErrorAction , LightningError , RoutingMessageHandler , NetAddress , OptionalField } ;
14
+ use ln:: msgs:: { DecodeError , ErrorAction , LightningError , RoutingMessageHandler , NetAddress , OptionalField , MAX_VALUE_MSAT } ;
15
15
use ln:: msgs;
16
16
use util:: ser:: { Writeable , Readable , Writer } ;
17
17
use util:: logger:: Logger ;
@@ -666,6 +666,19 @@ impl NetworkGraph {
666
666
match self . channels . get_mut ( & msg. contents . short_channel_id ) {
667
667
None => return Err ( LightningError { err : "Couldn't find channel for update" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ,
668
668
Some ( channel) => {
669
+ if let OptionalField :: Present ( htlc_maximum_msat) = msg. contents . htlc_maximum_msat {
670
+ if htlc_maximum_msat > MAX_VALUE_MSAT {
671
+ return Err ( LightningError { err : "htlc_maximum_msat is larger than maximum possible msats" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ;
672
+ }
673
+
674
+ if let Some ( capacity_sats) = channel. capacity_sats {
675
+ // It's possible channel capacity is available now, although it wasn't available at announcement (so the field is None).
676
+ // Don't query UTXO set here to reduce DoS risks.
677
+ if htlc_maximum_msat > capacity_sats * 1000 {
678
+ return Err ( LightningError { err : "htlc_maximum_msat is larger than channel capacity" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ;
679
+ }
680
+ }
681
+ }
669
682
macro_rules! maybe_update_channel_info {
670
683
( $target: expr, $src_node: expr) => {
671
684
if let Some ( existing_chan_info) = $target. as_ref( ) {
@@ -783,7 +796,8 @@ mod tests {
783
796
use ln:: features:: { ChannelFeatures , NodeFeatures } ;
784
797
use routing:: network_graph:: { NetGraphMsgHandler , NetworkGraph } ;
785
798
use ln:: msgs:: { OptionalField , RoutingMessageHandler , UnsignedNodeAnnouncement , NodeAnnouncement ,
786
- UnsignedChannelAnnouncement , ChannelAnnouncement , UnsignedChannelUpdate , ChannelUpdate , HTLCFailChannelUpdate } ;
799
+ UnsignedChannelAnnouncement , ChannelAnnouncement , UnsignedChannelUpdate , ChannelUpdate , HTLCFailChannelUpdate ,
800
+ MAX_VALUE_MSAT } ;
787
801
use util:: test_utils;
788
802
use util:: logger:: Logger ;
789
803
use util:: ser:: { Readable , Writeable } ;
@@ -1118,7 +1132,11 @@ mod tests {
1118
1132
1119
1133
#[ test]
1120
1134
fn handling_channel_update ( ) {
1121
- let ( secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler ( ) ;
1135
+ let secp_ctx = Secp256k1 :: new ( ) ;
1136
+ let logger: Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
1137
+ let chain_monitor = Arc :: new ( test_utils:: TestChainWatcher :: new ( ) ) ;
1138
+ let net_graph_msg_handler = NetGraphMsgHandler :: new ( chain_monitor. clone ( ) , Arc :: clone ( & logger) ) ;
1139
+
1122
1140
let node_1_privkey = & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ;
1123
1141
let node_2_privkey = & SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ;
1124
1142
let node_id_1 = PublicKey :: from_secret_key ( & secp_ctx, node_1_privkey) ;
@@ -1129,8 +1147,16 @@ mod tests {
1129
1147
let zero_hash = Sha256dHash :: hash ( & [ 0 ; 32 ] ) ;
1130
1148
let short_channel_id = 0 ;
1131
1149
let chain_hash = genesis_block ( Network :: Testnet ) . header . bitcoin_hash ( ) ;
1150
+ let amount_sats = 1000_000 ;
1151
+
1132
1152
{
1133
1153
// Announce a channel we will update
1154
+ let good_script = Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_PUSHNUM_2 )
1155
+ . push_slice ( & PublicKey :: from_secret_key ( & secp_ctx, node_1_btckey) . serialize ( ) )
1156
+ . push_slice ( & PublicKey :: from_secret_key ( & secp_ctx, node_2_btckey) . serialize ( ) )
1157
+ . push_opcode ( opcodes:: all:: OP_PUSHNUM_2 )
1158
+ . push_opcode ( opcodes:: all:: OP_CHECKMULTISIG ) . into_script ( ) . to_v0_p2wsh ( ) ;
1159
+ * chain_monitor. utxo_ret . lock ( ) . unwrap ( ) = Ok ( ( good_script. clone ( ) , amount_sats) ) ;
1134
1160
let unsigned_announcement = UnsignedChannelAnnouncement {
1135
1161
features : ChannelFeatures :: empty ( ) ,
1136
1162
chain_hash,
@@ -1218,6 +1244,31 @@ mod tests {
1218
1244
} ;
1219
1245
unsigned_channel_update. short_channel_id = short_channel_id;
1220
1246
1247
+ unsigned_channel_update. htlc_maximum_msat = OptionalField :: Present ( MAX_VALUE_MSAT + 1 ) ;
1248
+ let msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_channel_update. encode( ) [ ..] ) [ ..] ) ;
1249
+ let valid_channel_update = ChannelUpdate {
1250
+ signature : secp_ctx. sign ( & msghash, node_1_privkey) ,
1251
+ contents : unsigned_channel_update. clone ( )
1252
+ } ;
1253
+
1254
+ match net_graph_msg_handler. handle_channel_update ( & valid_channel_update) {
1255
+ Ok ( _) => panic ! ( ) ,
1256
+ Err ( e) => assert_eq ! ( e. err, "htlc_maximum_msat is larger than maximum possible msats" )
1257
+ } ;
1258
+ unsigned_channel_update. htlc_maximum_msat = OptionalField :: Absent ;
1259
+
1260
+ unsigned_channel_update. htlc_maximum_msat = OptionalField :: Present ( amount_sats * 1000 + 1 ) ;
1261
+ let msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_channel_update. encode( ) [ ..] ) [ ..] ) ;
1262
+ let valid_channel_update = ChannelUpdate {
1263
+ signature : secp_ctx. sign ( & msghash, node_1_privkey) ,
1264
+ contents : unsigned_channel_update. clone ( )
1265
+ } ;
1266
+
1267
+ match net_graph_msg_handler. handle_channel_update ( & valid_channel_update) {
1268
+ Ok ( _) => panic ! ( ) ,
1269
+ Err ( e) => assert_eq ! ( e. err, "htlc_maximum_msat is larger than channel capacity" )
1270
+ } ;
1271
+ unsigned_channel_update. htlc_maximum_msat = OptionalField :: Absent ;
1221
1272
1222
1273
// Even though previous update was not relayed further, we still accepted it,
1223
1274
// so we now won't accept update before the previous one.
0 commit comments