|
13 | 13 |
|
14 | 14 | use bitcoin::hashes::sha256::Hash as Sha256;
|
15 | 15 | use bitcoin::hashes::{Hash, HashEngine, Hmac, HmacEngine};
|
| 16 | +use bitcoin::secp256k1::PublicKey; |
16 | 17 |
|
| 18 | +use crate::io; |
| 19 | +use crate::ln::msgs::DecodeError; |
| 20 | +use crate::ln::types::ChannelId; |
17 | 21 | use crate::sign::PeerStorageKey;
|
| 22 | +use crate::util::ser::{Readable, Writeable, Writer}; |
18 | 23 |
|
19 | 24 | use crate::crypto::chacha20poly1305rfc::ChaCha20Poly1305RFC;
|
20 | 25 | use crate::prelude::*;
|
@@ -146,6 +151,77 @@ fn derive_nonce(key: &PeerStorageKey, random_bytes: &[u8]) -> [u8; 12] {
|
146 | 151 | nonce
|
147 | 152 | }
|
148 | 153 |
|
| 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 | + |
149 | 225 | #[cfg(test)]
|
150 | 226 | mod tests {
|
151 | 227 | use crate::ln::our_peer_storage::{derive_nonce, DecryptedOurPeerStorage};
|
|
0 commit comments