Skip to content

Commit ca9f813

Browse files
committed
Write structs to serialise-deserialise Channels inside Peer-storage
'PeerStorageMonitorHolder' is used to wrap a single ChannelMonitor, here we are adding some fields separetly so that we do not need to read the whole ChannelMonitor to identify if we have lost some states. `PeerStorageMonitorHolderList` is used to keep the list of all the channels which would be sent over the wire inside Peer Storage.
1 parent 3b4c8a8 commit ca9f813

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

lightning/src/ln/our_peer_storage.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313
1414
use bitcoin::hashes::sha256::Hash as Sha256;
1515
use bitcoin::hashes::{Hash, HashEngine, Hmac, HmacEngine};
16+
use bitcoin::secp256k1::PublicKey;
1617

18+
use crate::io;
19+
use crate::ln::msgs::DecodeError;
20+
use crate::ln::types::ChannelId;
1721
use crate::sign::PeerStorageKey;
22+
use crate::util::ser::{Readable, Writeable, Writer};
1823

1924
use crate::crypto::chacha20poly1305rfc::ChaCha20Poly1305RFC;
2025
use crate::prelude::*;
@@ -146,6 +151,77 @@ fn derive_nonce(key: &PeerStorageKey, random_bytes: &[u8]) -> [u8; 12] {
146151
nonce
147152
}
148153

154+
/// [`PeerStorageMonitorHolder`] represents a single channel sent over the wire.
155+
/// This would be used inside [`ChannelManager`] to determine
156+
/// if the user has lost channel states so that we can do something about it.
157+
///
158+
/// The main idea here is to just enable node to figure out that it has lost some data
159+
/// using peer storage backups.
160+
///
161+
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
162+
///
163+
/// TODO(aditya): Write FundRecoverer to use `monitor_bytes` to drop onchain.
164+
pub(crate) struct PeerStorageMonitorHolder {
165+
/// Channel Id of the channel.
166+
pub(crate) channel_id: ChannelId,
167+
/// Node Id of the channel partner.
168+
pub(crate) counterparty_node_id: PublicKey,
169+
/// Minimum seen secret to determine if we have lost state.
170+
pub(crate) min_seen_secret: u64,
171+
/// Whole serialised ChannelMonitor to recover funds.
172+
pub(crate) monitor_bytes: Vec<u8>,
173+
}
174+
175+
impl Writeable for PeerStorageMonitorHolder {
176+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
177+
self.channel_id.write(w)?;
178+
self.counterparty_node_id.write(w)?;
179+
self.min_seen_secret.write(w)?;
180+
self.monitor_bytes.write(w)
181+
}
182+
}
183+
184+
impl Readable for PeerStorageMonitorHolder {
185+
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
186+
let channel_id = Readable::read(r)?;
187+
let counterparty_node_id = Readable::read(r)?;
188+
let min_seen_secret = Readable::read(r)?;
189+
let monitor_bytes = Readable::read(r)?;
190+
191+
Ok(PeerStorageMonitorHolder {
192+
channel_id,
193+
counterparty_node_id,
194+
min_seen_secret,
195+
monitor_bytes,
196+
})
197+
}
198+
}
199+
200+
/// [`PeerStorageMonitorHolderList`] is used to serialise all the channels and send it over wire
201+
/// wrapped inside [`PeerStorage`].
202+
///
203+
/// [`PeerStorage`]: crate::ln::msgs::PeerStorage
204+
pub(crate) struct PeerStorageMonitorHolderList {
205+
/// List of all the channels to be sent over the wire.
206+
pub(crate) monitors: Vec<PeerStorageMonitorHolder>,
207+
}
208+
209+
impl Writeable for PeerStorageMonitorHolderList {
210+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
211+
encode_tlv_stream!(w, { (1, &self.monitors, required_vec) });
212+
Ok(())
213+
}
214+
}
215+
216+
impl Readable for PeerStorageMonitorHolderList {
217+
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
218+
let mut monitors: Option<Vec<PeerStorageMonitorHolder>> = None;
219+
decode_tlv_stream!(r, { (1, monitors, optional_vec) });
220+
221+
Ok(PeerStorageMonitorHolderList { monitors: monitors.ok_or(DecodeError::InvalidValue)? })
222+
}
223+
}
224+
149225
#[cfg(test)]
150226
mod tests {
151227
use crate::ln::our_peer_storage::{derive_nonce, DecryptedOurPeerStorage};

0 commit comments

Comments
 (0)