Skip to content

Commit 80497c4

Browse files
committed
fix RGB LN balance update
1 parent 4b21877 commit 80497c4

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

lightning/src/ln/channel.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::chain::BestBlock;
3838
use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, LowerBoundedFeeEstimator};
3939
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS, CLOSED_CHANNEL_UPDATE_ID};
4040
use crate::chain::transaction::{OutPoint, TransactionData};
41-
use crate::rgb_utils::{color_closing, color_commitment, color_htlc, get_rgb_channel_info, rename_rgb_files, update_rgb_channel_amount};
41+
use crate::rgb_utils::{color_closing, color_commitment, color_htlc, get_rgb_channel_info_pending, rename_rgb_files, update_rgb_channel_amount_pending};
4242
use crate::sign::{EcdsaChannelSigner, WriteableEcdsaChannelSigner, EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient};
4343
use crate::events::ClosureReason;
4444
use crate::routing::gossip::NodeId;
@@ -3543,7 +3543,7 @@ impl<SP: Deref> Channel<SP> where
35433543
}
35443544
self.context.value_to_self_msat = (self.context.value_to_self_msat as i64 + value_to_self_msat_diff) as u64;
35453545
if self.context.is_colored() {
3546-
update_rgb_channel_amount(&self.context.channel_id, rgb_offered_htlc, rgb_received_htlc, &self.context.ldk_data_dir);
3546+
update_rgb_channel_amount_pending(&self.context.channel_id, rgb_offered_htlc, rgb_received_htlc, &self.context.ldk_data_dir);
35473547
}
35483548

35493549
if let Some((feerate, update_state)) = self.context.pending_update_fee {
@@ -5117,7 +5117,7 @@ impl<SP: Deref> Channel<SP> where
51175117
let were_node_one = node_id.as_slice() < counterparty_node_id.as_slice();
51185118

51195119
let contract_id = if self.context.is_colored() {
5120-
let (rgb_info, _) = get_rgb_channel_info(&self.context.channel_id, &self.context.ldk_data_dir);
5120+
let (rgb_info, _) = get_rgb_channel_info_pending(&self.context.channel_id, &self.context.ldk_data_dir);
51215121
Some(rgb_info.contract_id)
51225122
} else {
51235123
None
@@ -5495,7 +5495,7 @@ impl<SP: Deref> Channel<SP> where
54955495
}
54965496
}
54975497
if self.context.is_colored() {
5498-
update_rgb_channel_amount(&self.context.channel_id, 0, rgb_received_htlc, &self.context.ldk_data_dir);
5498+
update_rgb_channel_amount_pending(&self.context.channel_id, 0, rgb_received_htlc, &self.context.ldk_data_dir);
54995499
}
55005500
if let Some((feerate, update_state)) = self.context.pending_update_fee {
55015501
if update_state == FeeUpdateState::AwaitingRemoteRevokeToAnnounce {

lightning/src/rgb_utils/mod.rs

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub(crate) fn color_commitment<SP: Deref>(channel_context: &ChannelContext<SP>,
152152

153153
let mut runtime = get_rgb_runtime(ldk_data_dir);
154154

155-
let (rgb_info, _) = get_rgb_channel_info(channel_id, ldk_data_dir);
155+
let (rgb_info, _) = get_rgb_channel_info_pending(channel_id, ldk_data_dir);
156156

157157
let chan_id = channel_id.to_hex();
158158
let mut beneficiaries = vec![];
@@ -419,7 +419,7 @@ pub(crate) fn color_closing(channel_id: &ChannelId, funding_outpoint: &OutPoint,
419419

420420
let mut runtime = get_rgb_runtime(ldk_data_dir);
421421

422-
let (rgb_info, _) = get_rgb_channel_info(channel_id, ldk_data_dir);
422+
let (rgb_info, _) = get_rgb_channel_info_pending(channel_id, ldk_data_dir);
423423

424424
let holder_vout = transaction.output.iter().position(|o| o.script_pubkey == closing_tx.to_holder_script).unwrap();
425425
let counterparty_vout = holder_vout ^ 1;
@@ -509,29 +509,42 @@ pub fn get_rgb_payment_info_path(payment_hash: &PaymentHash, ldk_data_dir: &Path
509509
ldk_data_dir.join(hex::encode(payment_hash.0))
510510
}
511511

512-
/// Get RgbPaymentInfo file
513-
pub fn get_rgb_payment_info(payment_hash: &PaymentHash, ldk_data_dir: &Path) -> RgbPaymentInfo {
514-
let rgb_payment_info_path = get_rgb_payment_info_path(payment_hash, ldk_data_dir);
515-
parse_rgb_payment_info(&rgb_payment_info_path)
516-
}
517-
518512
/// Parse RgbPaymentInfo
519513
pub fn parse_rgb_payment_info(rgb_payment_info_path: &PathBuf) -> RgbPaymentInfo {
520514
let serialized_info = fs::read_to_string(rgb_payment_info_path).expect("valid rgb payment info");
521515
serde_json::from_str(&serialized_info).expect("valid rgb info file")
522516
}
523517

518+
/// Get RgbInfo file path
519+
pub fn get_rgb_channel_info_path(channel_id: &str, ldk_data_dir: &Path, pending: bool) -> PathBuf {
520+
let mut info_file_path = ldk_data_dir.join(channel_id);
521+
if pending {
522+
info_file_path.set_extension("pending");
523+
}
524+
info_file_path
525+
}
526+
524527
/// Get RgbInfo file
525-
pub fn get_rgb_channel_info(channel_id: &ChannelId, ldk_data_dir: &Path) -> (RgbInfo, PathBuf) {
526-
let info_file_path = ldk_data_dir.join(channel_id.to_hex());
527-
let serialized_info = fs::read_to_string(&info_file_path).expect("valid rgb info file");
528-
let info: RgbInfo = serde_json::from_str(&serialized_info).expect("valid rgb info file");
528+
pub(crate) fn get_rgb_channel_info(channel_id: &str, ldk_data_dir: &Path, pending: bool) -> (RgbInfo, PathBuf) {
529+
let info_file_path = get_rgb_channel_info_path(channel_id, ldk_data_dir, pending);
530+
let info = parse_rgb_channel_info(&info_file_path);
529531
(info, info_file_path)
530532
}
531533

534+
/// Get pending RgbInfo file
535+
pub fn get_rgb_channel_info_pending(channel_id: &ChannelId, ldk_data_dir: &Path) -> (RgbInfo, PathBuf) {
536+
get_rgb_channel_info(&channel_id.to_hex(), ldk_data_dir, true)
537+
}
538+
539+
/// Parse RgbInfo
540+
pub fn parse_rgb_channel_info(rgb_channel_info_path: &PathBuf) -> RgbInfo {
541+
let serialized_info = fs::read_to_string(&rgb_channel_info_path).expect("valid rgb info file");
542+
serde_json::from_str(&serialized_info).expect("valid rgb info file")
543+
}
544+
532545
/// Whether the channel data for a channel exist
533546
pub fn is_channel_rgb(channel_id: &ChannelId, ldk_data_dir: &Path) -> bool {
534-
ldk_data_dir.join(channel_id.to_hex()).exists()
547+
get_rgb_channel_info_path(&channel_id.to_hex(), ldk_data_dir, false).exists()
535548
}
536549

537550
/// Write RgbInfo file
@@ -559,9 +572,14 @@ pub(crate) fn rename_rgb_files(channel_id: &ChannelId, temporary_channel_id: &Ch
559572
let temp_chan_id = temporary_channel_id.to_hex();
560573
let chan_id = channel_id.to_hex();
561574

562-
let temporary_channel_id_path = ldk_data_dir.join(&temp_chan_id);
563-
let channel_id_path = ldk_data_dir.join(&chan_id);
564-
fs::rename(temporary_channel_id_path, channel_id_path).expect("rename ok");
575+
fs::rename(
576+
get_rgb_channel_info_path(&temp_chan_id, ldk_data_dir, false),
577+
get_rgb_channel_info_path(&chan_id, ldk_data_dir, false),
578+
).expect("rename ok");
579+
fs::rename(
580+
get_rgb_channel_info_path(&temp_chan_id, ldk_data_dir, true),
581+
get_rgb_channel_info_path(&chan_id, ldk_data_dir, true),
582+
).expect("rename ok");
565583

566584
let funding_consignment_tmp = ldk_data_dir.join(format!("consignment_{}", temp_chan_id));
567585
if funding_consignment_tmp.exists() {
@@ -652,20 +670,21 @@ pub(crate) fn handle_funding(temporary_channel_id: &ChannelId, funding_txid: Str
652670
};
653671
let _status = runtime.runtime.accept_transfer(validated_transfer, &mut resolver, true).expect("valid transfer");
654672

655-
let rgb_info_path = ldk_data_dir.join(temporary_channel_id.to_hex());
656673
let rgb_info = RgbInfo {
657674
contract_id,
658675
local_rgb_amount: 0,
659676
remote_rgb_amount,
660677
};
661-
write_rgb_channel_info(&rgb_info_path, &rgb_info);
678+
let temporary_channel_id_str = temporary_channel_id.to_hex();
679+
write_rgb_channel_info(&get_rgb_channel_info_path(&temporary_channel_id_str, &ldk_data_dir, true), &rgb_info);
680+
write_rgb_channel_info(&get_rgb_channel_info_path(&temporary_channel_id_str, &ldk_data_dir, false), &rgb_info);
662681

663682
Ok(())
664683
}
665684

666685
/// Update RGB channel amount
667-
pub(crate) fn update_rgb_channel_amount(channel_id: &ChannelId, rgb_offered_htlc: u64, rgb_received_htlc: u64, ldk_data_dir: &Path) {
668-
let (mut rgb_info, info_file_path) = get_rgb_channel_info(channel_id, ldk_data_dir);
686+
pub fn update_rgb_channel_amount(channel_id: &str, rgb_offered_htlc: u64, rgb_received_htlc: u64, ldk_data_dir: &Path, pending: bool) {
687+
let (mut rgb_info, info_file_path) = get_rgb_channel_info(channel_id, ldk_data_dir, pending);
669688

670689
if rgb_offered_htlc > rgb_received_htlc {
671690
let spent = rgb_offered_htlc - rgb_received_htlc;
@@ -680,6 +699,11 @@ pub(crate) fn update_rgb_channel_amount(channel_id: &ChannelId, rgb_offered_htlc
680699
write_rgb_channel_info(&info_file_path, &rgb_info)
681700
}
682701

702+
/// Update pending RGB channel amount
703+
pub(crate) fn update_rgb_channel_amount_pending(channel_id: &ChannelId, rgb_offered_htlc: u64, rgb_received_htlc: u64, ldk_data_dir: &Path) {
704+
update_rgb_channel_amount(&channel_id.to_hex(), rgb_offered_htlc, rgb_received_htlc, ldk_data_dir, true)
705+
}
706+
683707
/// Whether the payment is colored
684708
pub(crate) fn is_payment_rgb(ldk_data_dir: &Path, payment_hash: &PaymentHash) -> bool {
685709
ldk_data_dir.join(hex::encode(payment_hash.0)).exists()

0 commit comments

Comments
 (0)