@@ -78,9 +78,8 @@ use crate::ln::types::ChannelId;
7878use crate::ln::LN_MAX_MSG_LEN;
7979use crate::offers::static_invoice::StaticInvoice;
8080use crate::rgb_utils::{
81- color_closing, color_commitment, color_htlc, get_rgb_channel_info_path,
82- get_rgb_channel_info_pending, parse_rgb_channel_info, rename_rgb_files,
83- update_rgb_channel_amount_pending,
81+ color_closing, color_commitment, color_htlc, get_rgb_channel_info_pending, rename_rgb_files,
82+ update_rgb_channel_amount_pending, RgbKvStoreExt,
8483};
8584use crate::routing::gossip::NodeId;
8685use crate::sign::ecdsa::EcdsaChannelSigner;
@@ -104,6 +103,8 @@ use crate::prelude::*;
104103use crate::sign::type_resolver::ChannelSignerType;
105104#[cfg(any(test, fuzzing, debug_assertions))]
106105use crate::sync::Mutex;
106+ use crate::sync::Arc;
107+ use crate::util::persist::KVStoreSync;
107108use core::ops::Deref;
108109use core::time::Duration;
109110use core::{cmp, fmt, mem};
@@ -3147,6 +3148,9 @@ where
31473148 pub(super) is_colored: bool,
31483149
31493150 pub(crate) ldk_data_dir: PathBuf,
3151+
3152+ /// KVStore for RGB data persistence
3153+ pub(crate) rgb_kv_store: Arc<dyn KVStoreSync + Send + Sync>,
31503154}
31513155
31523156/// A channel struct implementing this trait can receive an initial counterparty commitment
@@ -3246,8 +3250,8 @@ where
32463250 let context = self.context_mut();
32473251 let temporary_channel_id = context.channel_id;
32483252 context.channel_id = channel_id;
3249- if context.is_colored() {
3250- rename_rgb_files(&context.channel_id, &temporary_channel_id, & context.ldk_data_dir );
3253+ if context.is_colored() && temporary_channel_id != channel_id {
3254+ rename_rgb_files(&context.channel_id, &temporary_channel_id, context.rgb_kv_store.as_ref() );
32513255 }
32523256
32533257 assert!(!context.channel_state.is_monitor_update_in_progress()); // We have not had any monitor(s) yet to fail update!
@@ -3417,6 +3421,7 @@ where
34173421 open_channel_fields: msgs::CommonOpenChannelFields,
34183422 push_asset_amount: Option<u64>,
34193423 ldk_data_dir: PathBuf,
3424+ rgb_kv_store: Arc<dyn KVStoreSync + Send + Sync>,
34203425 ) -> Result<(FundingScope, ChannelContext<SP>), ChannelError>
34213426 where
34223427 ES::Target: EntropySource,
@@ -3741,6 +3746,7 @@ where
37413746
37423747 is_colored: funding.consignment_endpoint.is_some(),
37433748 ldk_data_dir,
3749+ rgb_kv_store,
37443750 };
37453751
37463752 Ok((funding, channel_context))
@@ -3767,6 +3773,7 @@ where
37673773 consignment_endpoint: Option<RgbTransport>,
37683774 ldk_data_dir: PathBuf,
37693775 push_asset_amount: Option<u64>,
3776+ rgb_kv_store: Arc<dyn KVStoreSync + Send + Sync>,
37703777 ) -> Result<(FundingScope, ChannelContext<SP>), APIError>
37713778 where
37723779 ES::Target: EntropySource,
@@ -3987,6 +3994,7 @@ where
39873994
39883995 is_colored: funding.consignment_endpoint.is_some(),
39893996 ldk_data_dir,
3997+ rgb_kv_store,
39903998 };
39913999
39924000 Ok((funding, channel_context))
@@ -4365,13 +4373,8 @@ where
43654373
43664374 /// Get the channel local RGB amount
43674375 pub fn get_local_rgb_amount(&self) -> u64 {
4368- let info_file_path = get_rgb_channel_info_path(
4369- &self.channel_id.0.as_hex().to_string(),
4370- &self.ldk_data_dir,
4371- false,
4372- );
4373- if info_file_path.exists() {
4374- let rgb_info = parse_rgb_channel_info(&info_file_path);
4376+ let channel_id_str = self.channel_id.0.as_hex().to_string();
4377+ if let Ok(rgb_info) = self.rgb_kv_store.read_rgb_channel_info(&channel_id_str, false) {
43754378 rgb_info.local_rgb_amount
43764379 } else {
43774380 0
@@ -4380,13 +4383,8 @@ where
43804383
43814384 /// Get the channel remote RGB amount
43824385 pub fn get_remote_rgb_amount(&self) -> u64 {
4383- let info_file_path = get_rgb_channel_info_path(
4384- &self.channel_id.0.as_hex().to_string(),
4385- &self.ldk_data_dir,
4386- false,
4387- );
4388- if info_file_path.exists() {
4389- let rgb_info = parse_rgb_channel_info(&info_file_path);
4386+ let channel_id_str = self.channel_id.0.as_hex().to_string();
4387+ if let Ok(rgb_info) = self.rgb_kv_store.read_rgb_channel_info(&channel_id_str, false) {
43904388 rgb_info.remote_rgb_amount
43914389 } else {
43924390 0
@@ -5101,7 +5099,7 @@ where
51015099 &holder_keys.revocation_key,
51025100 );
51035101 if self.is_colored() {
5104- color_htlc(&mut htlc_tx, htlc, &self.ldk_data_dir)
5102+ color_htlc(&mut htlc_tx, htlc, &self.ldk_data_dir, self.rgb_kv_store.as_ref() )
51055103 .expect("successful htlc coloring");
51065104 }
51075105
@@ -7345,6 +7343,7 @@ where
73457343 &self.context.channel_id,
73467344 &mut closing_transaction,
73477345 &self.context.ldk_data_dir,
7346+ self.context.rgb_kv_store.as_ref(),
73487347 )
73497348 .expect("successful closing TX coloring");
73507349 }
@@ -8935,7 +8934,7 @@ where
89358934 &self.context.channel_id,
89368935 rgb_offered_htlc,
89378936 rgb_received_htlc,
8938- & self.context.ldk_data_dir ,
8937+ self.context.rgb_kv_store.as_ref() ,
89398938 );
89408939 }
89418940
@@ -11749,7 +11748,7 @@ where
1174911748 let were_node_one = node_id.as_slice() < counterparty_node_id.as_slice();
1175011749
1175111750 let contract_id = if self.context.is_colored() {
11752- let ( rgb_info, _) = get_rgb_channel_info_pending(&self.context.channel_id, & self.context.ldk_data_dir );
11751+ let rgb_info = get_rgb_channel_info_pending(&self.context.channel_id, self.context.rgb_kv_store.as_ref() );
1175311752 Some(rgb_info.contract_id)
1175411753 } else {
1175511754 None
@@ -12903,7 +12902,7 @@ where
1290312902 }
1290412903 }
1290512904 if self.context.is_colored() && rgb_received_htlc > 0 {
12906- update_rgb_channel_amount_pending(&self.context.channel_id, 0, rgb_received_htlc, & self.context.ldk_data_dir );
12905+ update_rgb_channel_amount_pending(&self.context.channel_id, 0, rgb_received_htlc, self.context.rgb_kv_store.as_ref() );
1290712906 }
1290812907 if let Some((feerate, update_state)) = self.context.pending_update_fee {
1290912908 if update_state == FeeUpdateState::AwaitingRemoteRevokeToAnnounce {
@@ -13570,6 +13569,7 @@ where
1357013569 fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP, counterparty_node_id: PublicKey, their_features: &InitFeatures,
1357113570 channel_value_satoshis: u64, push_msat: u64, user_id: u128, config: &UserConfig, current_chain_height: u32,
1357213571 outbound_scid_alias: u64, temporary_channel_id: Option<ChannelId>, logger: L, consignment_endpoint: Option<RgbTransport>, ldk_data_dir: PathBuf, push_asset_amount: Option<u64>,
13572+ rgb_kv_store: Arc<dyn KVStoreSync + Send + Sync>,
1357313573 ) -> Result<OutboundV1Channel<SP>, APIError>
1357413574 where ES::Target: EntropySource,
1357513575 F::Target: FeeEstimator,
@@ -13610,6 +13610,7 @@ where
1361013610 consignment_endpoint,
1361113611 ldk_data_dir,
1361213612 push_asset_amount,
13613+ rgb_kv_store,
1361313614 )?;
1361413615 let unfunded_context = UnfundedChannelContext {
1361513616 unfunded_channel_age_ticks: 0,
@@ -13693,7 +13694,7 @@ where
1369313694 let temporary_channel_id = self.context.channel_id;
1369413695 self.context.channel_id = ChannelId::v1_from_funding_outpoint(funding_txo);
1369513696 if self.context.is_colored() {
13696- rename_rgb_files(&self.context.channel_id, &temporary_channel_id, & self.context.ldk_data_dir );
13697+ rename_rgb_files(&self.context.channel_id, &temporary_channel_id, self.context.rgb_kv_store.as_ref() );
1369713698 }
1369813699
1369913700 // If the funding transaction is a coinbase transaction, we need to set the minimum depth to 100.
@@ -13947,7 +13948,8 @@ where
1394713948 fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
1394813949 counterparty_node_id: PublicKey, our_supported_features: &ChannelTypeFeatures,
1394913950 their_features: &InitFeatures, msg: &msgs::OpenChannel, user_id: u128, config: &UserConfig,
13950- current_chain_height: u32, logger: &L, is_0conf: bool, ldk_data_dir: PathBuf
13951+ current_chain_height: u32, logger: &L, is_0conf: bool, ldk_data_dir: PathBuf,
13952+ rgb_kv_store: Arc<dyn KVStoreSync + Send + Sync>,
1395113953 ) -> Result<InboundV1Channel<SP>, ChannelError>
1395213954 where ES::Target: EntropySource,
1395313955 F::Target: FeeEstimator,
@@ -13989,6 +13991,7 @@ where
1398913991 msg.common_fields.clone(),
1399013992 msg.push_asset_amount,
1399113993 ldk_data_dir,
13994+ rgb_kv_store,
1399213995 )?;
1399313996 let unfunded_context = UnfundedChannelContext {
1399413997 unfunded_channel_age_ticks: 0,
@@ -14189,7 +14192,7 @@ where
1418914192 counterparty_node_id: PublicKey, their_features: &InitFeatures, funding_satoshis: u64,
1419014193 funding_inputs: Vec<FundingTxInput>, user_id: u128, config: &UserConfig,
1419114194 current_chain_height: u32, outbound_scid_alias: u64, funding_confirmation_target: ConfirmationTarget,
14192- logger: L, ldk_data_dir: PathBuf,
14195+ logger: L, ldk_data_dir: PathBuf, rgb_kv_store: Arc<dyn KVStoreSync + Send + Sync>,
1419314196 ) -> Result<Self, APIError>
1419414197 where ES::Target: EntropySource,
1419514198 F::Target: FeeEstimator,
@@ -14233,6 +14236,7 @@ where
1423314236 None,
1423414237 ldk_data_dir,
1423514238 None,
14239+ rgb_kv_store,
1423614240 )?;
1423714241 let unfunded_context = UnfundedChannelContext {
1423814242 unfunded_channel_age_ticks: 0,
@@ -14343,7 +14347,7 @@ where
1434314347 holder_node_id: PublicKey, counterparty_node_id: PublicKey, our_supported_features: &ChannelTypeFeatures,
1434414348 their_features: &InitFeatures, msg: &msgs::OpenChannelV2,
1434514349 user_id: u128, config: &UserConfig, current_chain_height: u32, logger: &L,
14346- ldk_data_dir: PathBuf,
14350+ ldk_data_dir: PathBuf, rgb_kv_store: Arc<dyn KVStoreSync + Send + Sync>,
1434714351 ) -> Result<Self, ChannelError>
1434814352 where ES::Target: EntropySource,
1434914353 F::Target: FeeEstimator,
@@ -14390,6 +14394,7 @@ where
1439014394 msg.common_fields.clone(),
1439114395 None,
1439214396 ldk_data_dir,
14397+ rgb_kv_store,
1439314398 )?;
1439414399 let channel_id = ChannelId::v2_from_revocation_basepoints(
1439514400 &funding.get_holder_pubkeys().revocation_basepoint,
@@ -15080,16 +15085,16 @@ where
1508015085 }
1508115086}
1508215087
15083- impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c ChannelTypeFeatures, PathBuf)>
15088+ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c ChannelTypeFeatures, PathBuf, Arc<dyn KVStoreSync + Send + Sync> )>
1508415089 for FundedChannel<SP>
1508515090where
1508615091 ES::Target: EntropySource,
1508715092 SP::Target: SignerProvider,
1508815093{
1508915094 fn read<R: io::Read>(
15090- reader: &mut R, args: (&'a ES, &'b SP, &'c ChannelTypeFeatures, PathBuf),
15095+ reader: &mut R, args: (&'a ES, &'b SP, &'c ChannelTypeFeatures, PathBuf, Arc<dyn KVStoreSync + Send + Sync> ),
1509115096 ) -> Result<Self, DecodeError> {
15092- let (entropy_source, signer_provider, our_supported_features, ldk_data_dir) = args;
15097+ let (entropy_source, signer_provider, our_supported_features, ldk_data_dir, rgb_kv_store ) = args;
1509315098 let ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
1509415099 if ver <= 2 {
1509515100 return Err(DecodeError::UnknownVersion);
@@ -15888,6 +15893,7 @@ where
1588815893 interactive_tx_signing_session,
1588915894 is_colored: consignment_endpoint.is_some(),
1589015895 ldk_data_dir,
15896+ rgb_kv_store,
1589115897 },
1589215898 holder_commitment_point,
1589315899 pending_splice,
0 commit comments