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