@@ -42,6 +42,7 @@ use sync::{RwLock, RwLockReadGuard};
42
42
use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
43
43
use sync:: Mutex ;
44
44
use core:: ops:: Deref ;
45
+ use std:: cmp:: max;
45
46
use bitcoin:: hashes:: hex:: ToHex ;
46
47
47
48
#[ cfg( feature = "std" ) ]
@@ -1201,62 +1202,70 @@ impl NetworkGraph {
1201
1202
///
1202
1203
/// `mutations: Vec<ChannelMutationProperty>`: Array of properties to mutate
1203
1204
pub fn override_directional_channel_properties ( & self , short_channel_id : u64 , flags : u8 , mutations : Vec < ChannelMutationProperty > ) -> Result < ( ) , LightningError > {
1204
- let mut channels = self . channels . write ( ) . unwrap ( ) ;
1205
- let direction = flags & 1u8 ;
1206
- let channel_update = match channels. get ( & short_channel_id) {
1207
- None => return Err ( LightningError { err : "Couldn't find channel for update" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ,
1208
- Some ( channel) => {
1209
- let directional_info = if direction == 0 {
1210
- channel. one_to_two . as_ref ( )
1211
- } else {
1212
- channel. two_to_one . as_ref ( )
1213
- } ;
1214
- if let Some ( directional_info) = directional_info {
1215
- let mut synthetic_update = msgs:: UnsignedChannelUpdate :: new ( ) ;
1216
- synthetic_update. chain_hash = Default :: default ( ) ;
1217
- synthetic_update. short_channel_id = short_channel_id;
1218
- synthetic_update. timestamp = directional_info. last_update ;
1219
- synthetic_update. flags = flags;
1220
- synthetic_update. cltv_expiry_delta = directional_info. cltv_expiry_delta ;
1221
- synthetic_update. htlc_minimum_msat = directional_info. htlc_minimum_msat ;
1222
- synthetic_update. htlc_maximum_msat = directional_info. htlc_maximum_msat . into ( ) ;
1223
- synthetic_update. fee_base_msat = directional_info. fees . base_msat ;
1224
- synthetic_update. fee_proportional_millionths = directional_info. fees . proportional_millionths ;
1225
- for current_mutation in mutations {
1226
- match current_mutation {
1227
- ChannelMutationProperty :: CltvExpiryDelta ( cltv_expiry_delta) => {
1228
- synthetic_update. cltv_expiry_delta = cltv_expiry_delta;
1229
- }
1230
- ChannelMutationProperty :: HtlcMinimumMsat ( htlc_minimum_msat) => {
1231
- synthetic_update. htlc_minimum_msat = htlc_minimum_msat;
1232
- }
1233
- ChannelMutationProperty :: FeeBaseMsat ( fee_base_msat) => {
1234
- synthetic_update. fee_base_msat = fee_base_msat;
1235
- }
1236
- ChannelMutationProperty :: FeeProportionalMillionths ( fee_proportional_millionths) => {
1237
- synthetic_update. fee_proportional_millionths = fee_proportional_millionths;
1238
- }
1239
- ChannelMutationProperty :: HtlcMaximumMsat ( htlc_maximum_msat) => {
1240
- // repeat the same HTLC max checks from update_channel_intern
1241
- if let Some ( htlc_maximum_msat) = htlc_maximum_msat {
1242
- if htlc_maximum_msat > MAX_VALUE_MSAT {
1243
- return Err ( LightningError { err : "htlc_maximum_msat is larger than maximum possible msats" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ;
1244
- }
1245
- if let Some ( capacity_sats) = channel. capacity_sats {
1246
- // It's possible channel capacity is available now, although it wasn't available at announcement (so the field is None).
1247
- // Don't query UTXO set here to reduce DoS risks.
1248
- if capacity_sats > MAX_VALUE_MSAT / 1000 || htlc_maximum_msat > capacity_sats * 1000 {
1249
- return Err ( LightningError { err : "htlc_maximum_msat is larger than channel capacity or capacity is bogus" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ;
1205
+ let channel_update = {
1206
+ let mut channels = self . channels . read ( ) . unwrap ( ) ;
1207
+ let direction = flags & 1u8 ;
1208
+ match channels. get ( & short_channel_id) {
1209
+ None => return Err ( LightningError { err : "Couldn't find channel for update" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ,
1210
+ Some ( channel) => {
1211
+ let directional_info = if direction == 0 {
1212
+ channel. one_to_two . as_ref ( )
1213
+ } else {
1214
+ channel. two_to_one . as_ref ( )
1215
+ } ;
1216
+ if let Some ( directional_info) = directional_info {
1217
+ let current_timestamp = SystemTime :: now ( )
1218
+ . duration_since ( UNIX_EPOCH )
1219
+ . unwrap ( )
1220
+ . as_secs ( ) as u32 ;
1221
+ let mut synthetic_update = msgs:: UnsignedChannelUpdate :: new ( ) ;
1222
+ // TODO: get active chain hash
1223
+ synthetic_update. chain_hash = Default :: default ( ) ;
1224
+ synthetic_update. short_channel_id = short_channel_id;
1225
+ // TODO: broadcast timestamps
1226
+ synthetic_update. timestamp = max ( current_timestamp, directional_info. last_update + 1 ) ;
1227
+ synthetic_update. flags = flags;
1228
+ synthetic_update. cltv_expiry_delta = directional_info. cltv_expiry_delta ;
1229
+ synthetic_update. htlc_minimum_msat = directional_info. htlc_minimum_msat ;
1230
+ synthetic_update. htlc_maximum_msat = directional_info. htlc_maximum_msat . into ( ) ;
1231
+ synthetic_update. fee_base_msat = directional_info. fees . base_msat ;
1232
+ synthetic_update. fee_proportional_millionths = directional_info. fees . proportional_millionths ;
1233
+ for current_mutation in mutations {
1234
+ match current_mutation {
1235
+ ChannelMutationProperty :: CltvExpiryDelta ( cltv_expiry_delta) => {
1236
+ synthetic_update. cltv_expiry_delta = cltv_expiry_delta;
1237
+ }
1238
+ ChannelMutationProperty :: HtlcMinimumMsat ( htlc_minimum_msat) => {
1239
+ synthetic_update. htlc_minimum_msat = htlc_minimum_msat;
1240
+ }
1241
+ ChannelMutationProperty :: FeeBaseMsat ( fee_base_msat) => {
1242
+ synthetic_update. fee_base_msat = fee_base_msat;
1243
+ }
1244
+ ChannelMutationProperty :: FeeProportionalMillionths ( fee_proportional_millionths) => {
1245
+ synthetic_update. fee_proportional_millionths = fee_proportional_millionths;
1246
+ }
1247
+ ChannelMutationProperty :: HtlcMaximumMsat ( htlc_maximum_msat) => {
1248
+ // repeat the same HTLC max checks from update_channel_intern
1249
+ if let Some ( htlc_maximum_msat) = htlc_maximum_msat {
1250
+ if htlc_maximum_msat > MAX_VALUE_MSAT {
1251
+ return Err ( LightningError { err : "htlc_maximum_msat is larger than maximum possible msats" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ;
1252
+ }
1253
+ if let Some ( capacity_sats) = channel. capacity_sats {
1254
+ // It's possible channel capacity is available now, although it wasn't available at announcement (so the field is None).
1255
+ // Don't query UTXO set here to reduce DoS risks.
1256
+ if capacity_sats > MAX_VALUE_MSAT / 1000 || htlc_maximum_msat > capacity_sats * 1000 {
1257
+ return Err ( LightningError { err : "htlc_maximum_msat is larger than channel capacity or capacity is bogus" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ;
1258
+ }
1250
1259
}
1251
1260
}
1261
+ synthetic_update. htlc_maximum_msat = htlc_maximum_msat. into ( ) ;
1252
1262
}
1253
- synthetic_update. htlc_maximum_msat = htlc_maximum_msat. into ( ) ;
1254
1263
}
1255
1264
}
1265
+ synthetic_update
1266
+ } else {
1267
+ return Err ( LightningError { err : "Couldn't find previous directional data for update" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ;
1256
1268
}
1257
- synthetic_update
1258
- } else {
1259
- return Err ( LightningError { err : "Couldn't find previous directional data for update" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ;
1260
1269
}
1261
1270
}
1262
1271
} ;
0 commit comments