Skip to content

Commit 73cd366

Browse files
committed
fix deadlock
1 parent feac3c5 commit 73cd366

File tree

2 files changed

+64
-50
lines changed

2 files changed

+64
-50
lines changed

lightning-graph-sync/src/processing.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ pub(crate) fn read_network_graph<R: Read>(
9595
read_cursor = restricted_cursor.into_inner();
9696

9797
let mut synthetic_update = UnsignedChannelUpdate::new();
98+
// TODO: get active chain hash
9899
synthetic_update.chain_hash = Default::default();
99100
synthetic_update.short_channel_id = short_channel_id;
101+
// TODO: broadcast timestamps
100102
synthetic_update.timestamp = current_timestamp;
101103
synthetic_update.flags = real_channel_flags;
102104
synthetic_update.cltv_expiry_delta = cltv_expiry_delta;
@@ -462,6 +464,9 @@ mod tests {
462464
&network_graph,
463465
&single_direction_incremental_update_input[..],
464466
);
467+
if update_result.is_err() {
468+
println!("here");
469+
}
465470
assert!(update_result.is_ok());
466471

467472
let after = network_graph.to_string();

lightning/src/routing/network_graph.rs

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use sync::{RwLock, RwLockReadGuard};
4242
use core::sync::atomic::{AtomicUsize, Ordering};
4343
use sync::Mutex;
4444
use core::ops::Deref;
45+
use std::cmp::max;
4546
use bitcoin::hashes::hex::ToHex;
4647

4748
#[cfg(feature = "std")]
@@ -1201,62 +1202,70 @@ impl NetworkGraph {
12011202
///
12021203
/// `mutations: Vec<ChannelMutationProperty>`: Array of properties to mutate
12031204
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+
}
12501259
}
12511260
}
1261+
synthetic_update.htlc_maximum_msat = htlc_maximum_msat.into();
12521262
}
1253-
synthetic_update.htlc_maximum_msat = htlc_maximum_msat.into();
12541263
}
12551264
}
1265+
synthetic_update
1266+
}else{
1267+
return Err(LightningError{err: "Couldn't find previous directional data for update".to_owned(), action: ErrorAction::IgnoreError});
12561268
}
1257-
synthetic_update
1258-
}else{
1259-
return Err(LightningError{err: "Couldn't find previous directional data for update".to_owned(), action: ErrorAction::IgnoreError});
12601269
}
12611270
}
12621271
};

0 commit comments

Comments
 (0)