From 83fa93cda34a4d687be489195b0fb82319f295db Mon Sep 17 00:00:00 2001 From: benthecarman Date: Fri, 2 Jun 2023 11:27:51 -0500 Subject: [PATCH 1/3] Impl clone for ChannelMonitor This gives people more freedom with the channel monitors. For Mutiny this would be nice for us to be able to create copies of them and pass aorund in memory without having to serialize until we actually want to. --- lightning/src/chain/channelmonitor.rs | 17 ++++++++++++----- lightning/src/chain/onchaintx.rs | 7 +++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index a48f169a4d5..9a6fb7e6d16 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -284,7 +284,7 @@ impl HolderSignedTx { /// We use this to track static counterparty commitment transaction data and to generate any /// justice or 2nd-stage preimage/timeout transactions. -#[derive(PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq)] struct CounterpartyCommitmentParameters { counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, @@ -338,7 +338,7 @@ impl Readable for CounterpartyCommitmentParameters { /// observed, as well as the transaction causing it. /// /// Used to determine when the on-chain event can be considered safe from a chain reorganization. -#[derive(PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq)] struct OnchainEventEntry { txid: Txid, height: u32, @@ -381,7 +381,7 @@ type CommitmentTxCounterpartyOutputInfo = Option<(u32, u64)>; /// Upon discovering of some classes of onchain tx by ChannelMonitor, we may have to take actions on it /// once they mature to enough confirmations (ANTI_REORG_DELAY) -#[derive(PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq)] enum OnchainEvent { /// An outbound HTLC failing after a transaction is confirmed. Used /// * when an outbound HTLC output is spent by us after the HTLC timed out @@ -652,7 +652,7 @@ pub enum Balance { } /// An HTLC which has been irrevocably resolved on-chain, and has reached ANTI_REORG_DELAY. -#[derive(PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq)] struct IrrevocablyResolvedHTLC { commitment_tx_output_idx: Option, /// The txid of the transaction which resolved the HTLC, this may be a commitment (if the HTLC @@ -725,7 +725,14 @@ pub struct ChannelMonitor { inner: Mutex>, } -#[derive(PartialEq)] +impl Clone for ChannelMonitor where Signer: Clone { + fn clone(&self) -> Self { + let inner = self.inner.lock().unwrap().clone(); + ChannelMonitor { inner: Mutex::new(inner) } + } +} + +#[derive(Clone, PartialEq)] pub(crate) struct ChannelMonitorImpl { latest_update_id: u64, commitment_transaction_number_obscure_factor: u64, diff --git a/lightning/src/chain/onchaintx.rs b/lightning/src/chain/onchaintx.rs index 45968c57e53..3bce559b473 100644 --- a/lightning/src/chain/onchaintx.rs +++ b/lightning/src/chain/onchaintx.rs @@ -56,7 +56,7 @@ const MAX_ALLOC_SIZE: usize = 64*1024; /// transaction causing it. /// /// Used to determine when the on-chain event can be considered safe from a chain reorganization. -#[derive(PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq)] struct OnchainEventEntry { txid: Txid, height: u32, @@ -76,7 +76,7 @@ impl OnchainEventEntry { /// Events for claims the [`OnchainTxHandler`] has generated. Once the events are considered safe /// from a chain reorg, the [`OnchainTxHandler`] will act accordingly. -#[derive(PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq)] enum OnchainEvent { /// A pending request has been claimed by a transaction spending the exact same set of outpoints /// as the request. This claim can either be ours or from the counterparty. Once the claiming @@ -179,6 +179,7 @@ impl Writeable for Option>> { #[cfg(anchors)] /// The claim commonly referred to as the pre-signed second-stage HTLC transaction. +#[derive(Clone, PartialEq, Eq)] pub(crate) struct ExternalHTLCClaim { pub(crate) commitment_txid: Txid, pub(crate) per_commitment_number: u64, @@ -190,6 +191,7 @@ pub(crate) struct ExternalHTLCClaim { // Represents the different types of claims for which events are yielded externally to satisfy said // claims. #[cfg(anchors)] +#[derive(Clone, PartialEq, Eq)] pub(crate) enum ClaimEvent { /// Event yielded to signal that the commitment transaction fee must be bumped to claim any /// encumbered funds and proceed to HTLC resolution, if any HTLCs exist. @@ -223,6 +225,7 @@ type PackageID = [u8; 32]; /// OnchainTxHandler receives claiming requests, aggregates them if it's sound, broadcast and /// do RBF bumping if possible. +#[derive(Clone)] pub struct OnchainTxHandler { destination_script: Script, holder_commitment: HolderCommitmentTransaction, From 44bda615244ae390e7c92d8bb45a359b44d5bca9 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Sun, 4 Jun 2023 15:08:22 -0500 Subject: [PATCH 2/3] Impl PartialOrd for ChannelMonitor This should let you tell which is the more recent channel monitor easily. If they are for a different funding txo, then they will not be compared. --- lightning/src/chain/channelmonitor.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index 9a6fb7e6d16..a395795fbad 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -886,6 +886,18 @@ impl PartialEq for ChannelMonitor w } } +impl PartialOrd for ChannelMonitor where Signer: PartialEq { + fn partial_cmp(&self, other: &Self) -> Option { + let inner = self.inner.lock().unwrap(); + let other_inner = other.inner.lock().unwrap(); + if inner.get_funding_txo() != other_inner.get_funding_txo() { + return None; + } + + inner.latest_update_id.partial_cmp(&other_inner.latest_update_id) + } +} + impl Writeable for ChannelMonitor { fn write(&self, writer: &mut W) -> Result<(), Error> { self.inner.lock().unwrap().write(writer) From 7eb9aa8e8905d6d845fab855707e7c578b668080 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Fri, 2 Jun 2023 12:01:59 -0500 Subject: [PATCH 3/3] Add get_monitors for ChainMontior This allows you to get access to all the channel monitors without locking on each look-up. --- lightning/src/chain/chainmonitor.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lightning/src/chain/chainmonitor.rs b/lightning/src/chain/chainmonitor.rs index 261e5593b5b..2c23924ae9b 100644 --- a/lightning/src/chain/chainmonitor.rs +++ b/lightning/src/chain/chainmonitor.rs @@ -556,6 +556,25 @@ where C::Target: chain::Filter, } } +impl ChainMonitor + where C::Target: chain::Filter, + T::Target: BroadcasterInterface, + F::Target: FeeEstimator, + L::Target: Logger, + P::Target: Persist, + ChannelSigner: Clone +{ + /// Gets all the funding outpoints with each [`ChannelMonitor`] being monitored. + /// + /// Note that [`ChannelMonitor`]s are not removed when a channel is closed as they are always + /// monitoring for on-chain state resolutions. + pub fn get_monitors(&self) -> HashMap> { + self.monitors.read().unwrap().iter().map(|(outpoint, holder)| { + (*outpoint, holder.monitor.clone()) + }).collect() + } +} + impl chain::Listen for ChainMonitor where