Skip to content

Commit dacd467

Browse files
committed
avoid deletion of rgb payment info file
1 parent 529b3e7 commit dacd467

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

lightning/src/rgb_utils/mod.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,22 @@ pub(crate) fn color_commitment<SP: Deref>(channel_context: &ChannelContext<SP>,
170170

171171
let htlc_payment_hash = hex::encode(htlc.payment_hash.0);
172172
let htlc_proxy_id = format!("{chan_id}{htlc_payment_hash}");
173-
let rgb_payment_info_path = ldk_data_dir.join(htlc_proxy_id);
173+
let rgb_payment_info_proxy_id_path = ldk_data_dir.join(htlc_proxy_id);
174174

175-
let rgb_payment_info_hash_path = ldk_data_dir.join(htlc_payment_hash);
176-
if rgb_payment_info_hash_path.exists() {
177-
let mut rgb_payment_info = parse_rgb_payment_info(&rgb_payment_info_hash_path);
175+
let rgb_payment_info_path = ldk_data_dir.join(htlc_payment_hash);
176+
let mut rgb_payment_info_tmp_path = rgb_payment_info_path.clone();
177+
rgb_payment_info_tmp_path.set_extension("pending");
178+
if rgb_payment_info_tmp_path.exists() {
179+
let mut rgb_payment_info = parse_rgb_payment_info(&rgb_payment_info_tmp_path);
178180
rgb_payment_info.local_rgb_amount = rgb_info.local_rgb_amount;
179181
rgb_payment_info.remote_rgb_amount = rgb_info.remote_rgb_amount;
180182
let serialized_info = serde_json::to_string(&rgb_payment_info).expect("valid rgb payment info");
181-
fs::write(&rgb_payment_info_path, serialized_info).expect("able to write rgb payment info file");
182-
fs::remove_file(rgb_payment_info_hash_path).expect("able to remove file");
183+
fs::write(&rgb_payment_info_proxy_id_path, serialized_info).expect("able to write rgb payment info file");
184+
fs::remove_file(rgb_payment_info_tmp_path).expect("able to remove file");
183185
}
184186

185-
let rgb_payment_info = if rgb_payment_info_path.exists() {
186-
parse_rgb_payment_info(&rgb_payment_info_path)
187+
let rgb_payment_info = if rgb_payment_info_proxy_id_path.exists() {
188+
parse_rgb_payment_info(&rgb_payment_info_proxy_id_path)
187189
} else {
188190
let rgb_payment_info = RgbPaymentInfo {
189191
contract_id: rgb_info.contract_id,
@@ -194,6 +196,7 @@ pub(crate) fn color_commitment<SP: Deref>(channel_context: &ChannelContext<SP>,
194196
inbound: htlc.offered == counterparty,
195197
};
196198
let serialized_info = serde_json::to_string(&rgb_payment_info).expect("valid rgb payment info");
199+
fs::write(rgb_payment_info_proxy_id_path, serialized_info.clone()).expect("able to write rgb payment info file");
197200
fs::write(rgb_payment_info_path, serialized_info).expect("able to write rgb payment info file");
198201
rgb_payment_info
199202
};
@@ -558,7 +561,9 @@ pub fn write_rgb_channel_info(path: &PathBuf, rgb_info: &RgbInfo) {
558561

559562
/// Write RGB payment info to file
560563
pub fn write_rgb_payment_info_file(ldk_data_dir: &Path, payment_hash: &PaymentHash, contract_id: ContractId, amount_rgb: u64, override_route_amount: bool, inbound: bool) {
561-
let rgb_payment_info_path = ldk_data_dir.join(hex::encode(payment_hash.0));
564+
let rgb_payment_info_path = get_rgb_payment_info_path(payment_hash, ldk_data_dir);
565+
let mut rgb_payment_info_tmp_path = rgb_payment_info_path.clone();
566+
rgb_payment_info_tmp_path.set_extension("pending");
562567
let rgb_payment_info = RgbPaymentInfo {
563568
contract_id,
564569
amount: amount_rgb,
@@ -568,7 +573,8 @@ pub fn write_rgb_payment_info_file(ldk_data_dir: &Path, payment_hash: &PaymentHa
568573
inbound,
569574
};
570575
let serialized_info = serde_json::to_string(&rgb_payment_info).expect("valid rgb payment info");
571-
std::fs::write(rgb_payment_info_path, serialized_info).expect("able to write rgb payment info file");
576+
std::fs::write(rgb_payment_info_path, serialized_info.clone()).expect("able to write rgb payment info file");
577+
std::fs::write(rgb_payment_info_tmp_path, serialized_info).expect("able to write rgb payment info tmp file");
572578
}
573579

574580
/// Rename RGB files from temporary to final channel ID

0 commit comments

Comments
 (0)