Skip to content

Commit 0448305

Browse files
Aditya SharmaAditya Sharma
Aditya Sharma
authored and
Aditya Sharma
committed
lightning: Add struct for building OurPeerStorage, which would be encrypted and sent to our peers.
1 parent 0368e9a commit 0448305

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

lightning/src/ln/channel.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2101,6 +2101,14 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
21012101
self.update_time_counter
21022102
}
21032103

2104+
pub fn get_commitment_secret(&self) -> CounterpartyCommitmentSecrets {
2105+
self.commitment_secrets.clone()
2106+
}
2107+
2108+
pub fn get_channel_keys_id(&self) -> [u8;32] {
2109+
self.channel_keys_id
2110+
}
2111+
21042112
pub fn increment_and_fetch_monitor_update_id(&mut self) -> u64 {
21052113
self.latest_monitor_update_id +=1;
21062114
self.latest_monitor_update_id

lightning/src/ln/channelmanager.rs

+77
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,69 @@ impl From<&ClaimableHTLC> for events::ClaimedHTLC {
390390
}
391391
}
392392

393+
#[derive(Clone, PartialEq, Eq)]
394+
pub struct StubChannel {
395+
pub channel_id: ChannelId,
396+
pub funding_outpoint: OutPoint,
397+
pub channel_value_stoshis: u64,
398+
pub channel_keys_id: [u8;32],
399+
pub commitment_secrets: CounterpartyCommitmentSecrets,
400+
pub counterparty_node_id: PublicKey,
401+
}
402+
403+
impl StubChannel {
404+
pub fn new(channel_id: ChannelId, funding_outpoint: OutPoint, channel_value_stoshis: u64, channel_keys_id: [u8; 32], commitment_secrets: CounterpartyCommitmentSecrets, counterparty_node_id: PublicKey) -> Self {
405+
StubChannel {
406+
channel_id,
407+
funding_outpoint,
408+
channel_value_stoshis,
409+
channel_keys_id,
410+
commitment_secrets,
411+
counterparty_node_id,
412+
}
413+
}
414+
}
415+
416+
impl_writeable_tlv_based!(StubChannel, {
417+
(0, channel_id, required),
418+
(2, channel_keys_id, required),
419+
(4, channel_value_stoshis, required),
420+
(6, funding_outpoint, required),
421+
(8, commitment_secrets, required),
422+
(10, counterparty_node_id, required),
423+
});
424+
425+
#[derive(Clone, PartialEq, Eq)]
426+
pub struct OurPeerStorage {
427+
pub version: u32,
428+
pub timestamp: u32,
429+
pub channels: Vec<StubChannel>,
430+
}
431+
432+
impl OurPeerStorage {
433+
pub fn new() -> Self {
434+
let duration_since_epoch = std::time::SystemTime::now()
435+
.duration_since(std::time::SystemTime::UNIX_EPOCH)
436+
.expect("Time must be > 1970");
437+
438+
Self {
439+
version: 1,
440+
timestamp: duration_since_epoch.as_secs() as u32,
441+
channels: Vec::new(),
442+
}
443+
}
444+
445+
pub fn stub_channel(&mut self, chan: StubChannel) {
446+
self.channels.push(chan);
447+
}
448+
}
449+
450+
impl_writeable_tlv_based!(OurPeerStorage, {
451+
(0, version, (default_value, 1)),
452+
(2, timestamp, required),
453+
(4, channels, optional_vec),
454+
});
455+
393456
/// A user-provided identifier in [`ChannelManager::send_payment`] used to uniquely identify
394457
/// a payment and ensure idempotency in LDK.
395458
///
@@ -1432,6 +1495,7 @@ where
14321495
entropy_source: ES,
14331496
node_signer: NS,
14341497
signer_provider: SP,
1498+
our_peer_storage: FairRwLock<OurPeerStorage>,
14351499

14361500
logger: L,
14371501
}
@@ -2527,6 +2591,7 @@ where
25272591
entropy_source,
25282592
node_signer,
25292593
signer_provider,
2594+
our_peer_storage: FairRwLock::new(OurPeerStorage::new()),
25302595
logger,
25312596
}
25322597
}
@@ -10844,6 +10909,8 @@ where
1084410909
let mut close_background_events = Vec::new();
1084510910
let mut funding_txo_to_channel_id = hash_map_with_capacity(channel_count as usize);
1084610911
let mut peer_storage_dir: HashMap<PublicKey, Vec<u8>> = HashMap::new();
10912+
let mut our_peer_storage: OurPeerStorage = OurPeerStorage::new();
10913+
1084710914
for _ in 0..channel_count {
1084810915
let mut channel: Channel<SP> = Channel::read(reader, (
1084910916
&args.entropy_source, &args.signer_provider, best_block_height, &provided_channel_type_features(&args.default_config)
@@ -10852,6 +10919,15 @@ where
1085210919
let funding_txo = channel.context.get_funding_txo().ok_or(DecodeError::InvalidValue)?;
1085310920
funding_txo_to_channel_id.insert(funding_txo, channel.context.channel_id());
1085410921
funding_txo_set.insert(funding_txo.clone());
10922+
let stub_chan = StubChannel::new(
10923+
channel.context.channel_id(),
10924+
funding_txo,
10925+
channel.context.get_value_satoshis(),
10926+
channel.context.get_channel_keys_id(),
10927+
channel.context.get_commitment_secret(),
10928+
channel.context.get_counterparty_node_id(),
10929+
);
10930+
our_peer_storage.stub_channel(stub_chan);
1085510931
if let Some(ref mut monitor) = args.channel_monitors.get_mut(&funding_txo) {
1085610932
// Load Peer_storage from ChannelMonitor to memory.
1085710933
peer_storage_dir.insert(channel.context.get_counterparty_node_id(), monitor.get_peer_storage());
@@ -11682,6 +11758,7 @@ where
1168211758
entropy_source: args.entropy_source,
1168311759
node_signer: args.node_signer,
1168411760
signer_provider: args.signer_provider,
11761+
our_peer_storage: FairRwLock::new(our_peer_storage),
1168511762
logger: args.logger,
1168611763
default_configuration: args.default_config,
1168711764
};

0 commit comments

Comments
 (0)