Skip to content

Commit 8766784

Browse files
committed
fixup: Serialise ChannelMonitors and send them over inside Peer Storage
1 parent 03bb2f0 commit 8766784

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use bitcoin::hash_types::{BlockHash, Txid};
2929
use crate::chain;
3030
use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
3131
use crate::chain::channelmonitor::{
32-
write_util_internal, Balance, ChannelMonitor, ChannelMonitorUpdate, MonitorEvent, TransactionOutputs,
32+
write_chanmon_internal, Balance, ChannelMonitor, ChannelMonitorUpdate, MonitorEvent, TransactionOutputs,
3333
WithChannelMonitor,
3434
};
3535
use crate::chain::transaction::{OutPoint, TransactionData};
@@ -814,8 +814,9 @@ where
814814

815815
fn send_peer_storage(&self, their_node_id: PublicKey) {
816816
const MAX_PEER_STORAGE_SIZE: usize = 65531;
817+
const USIZE_LEN: usize = core::mem::size_of::<usize>();
817818
let random_bytes = self.entropy_source.get_secure_random_bytes();
818-
let random_usize = usize::from_le_bytes(random_bytes[0..core::mem::size_of::<usize>()].try_into().unwrap());
819+
let random_usize = usize::from_le_bytes(random_bytes[0..USIZE_LEN].try_into().unwrap());
819820

820821
let monitors = self.monitors.read().unwrap();
821822
let mut monitors_list = PeerStorageMonitorHolderList { monitors: Vec::new() };
@@ -835,31 +836,22 @@ where
835836
let counterparty_node_id = mon.monitor.get_counterparty_node_id();
836837
let chan_mon = mon.monitor.inner.lock().unwrap();
837838

838-
match write_util_internal(&chan_mon, true, &mut ser_chan) {
839-
Ok(_) => {
840-
// Adding size of peer_storage_monitor.
841-
curr_size += ser_chan.0.serialized_length()
842-
+ min_seen_secret.serialized_length()
843-
+ chan_id.serialized_length()
844-
+ counterparty_node_id.serialized_length();
839+
write_chanmon_internal(&chan_mon, true, &mut ser_chan).expect("can not write Channel Monitor for peer storage message");
845840

846-
if curr_size > MAX_PEER_STORAGE_SIZE {
847-
break;
848-
}
841+
let peer_storage_monitor = PeerStorageMonitorHolder {
842+
channel_id: *chan_id,
843+
min_seen_secret,
844+
counterparty_node_id,
845+
monitor_bytes: ser_chan.0,
846+
};
849847

850-
let peer_storage_monitor = PeerStorageMonitorHolder {
851-
channel_id: *chan_id,
852-
min_seen_secret,
853-
counterparty_node_id,
854-
monitor_bytes: ser_chan.0,
855-
};
848+
// Adding size of peer_storage_monitor.
849+
curr_size += peer_storage_monitor.serialized_length();
856850

857-
monitors_list.monitors.push(peer_storage_monitor);
858-
},
859-
Err(_) => {
860-
panic!("Can not write monitor for {}", mon.monitor.channel_id())
861-
},
851+
if curr_size > MAX_PEER_STORAGE_SIZE {
852+
break;
862853
}
854+
monitors_list.monitors.push(peer_storage_monitor);
863855
}
864856

865857
let serialised_channels = monitors_list.encode();

lightning/src/chain/channelmonitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ const MIN_SERIALIZATION_VERSION: u8 = 1;
13501350
/// NOTE: `is_stub` is true only when we are using this to serialise for Peer Storage.
13511351
///
13521352
/// [`ChainMonitor`]: crate::chain::chainmonitor::ChainMonitor
1353-
pub(crate) fn write_util_internal<Signer: EcdsaChannelSigner, W: Writer>(
1353+
pub(crate) fn write_chanmon_internal<Signer: EcdsaChannelSigner, W: Writer>(
13541354
channel_monitor: &ChannelMonitorImpl<Signer>, is_stub: bool, writer: &mut W,
13551355
) -> Result<(), Error> {
13561356
write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
@@ -1592,7 +1592,7 @@ pub(crate) fn write_util_internal<Signer: EcdsaChannelSigner, W: Writer>(
15921592

15931593
impl<Signer: EcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signer> {
15941594
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
1595-
write_util_internal(self, false, writer)
1595+
write_chanmon_internal(self, false, writer)
15961596
}
15971597
}
15981598

0 commit comments

Comments
 (0)