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.

Originally by benthecarman <benthecarman@live.com>
Small bugfix from Matt Corallo <git@bluematt.me>
This commit is contained in:
benthecarman 2023-06-02 11:27:51 -05:00 committed by Matt Corallo
parent e13ff10c63
commit d026259d3e
2 changed files with 17 additions and 7 deletions

View file

@ -280,7 +280,7 @@ impl HolderSignedTx {
/// We use this to track static counterparty commitment transaction data and to generate any /// We use this to track static counterparty commitment transaction data and to generate any
/// justice or 2nd-stage preimage/timeout transactions. /// justice or 2nd-stage preimage/timeout transactions.
#[derive(PartialEq, Eq)] #[derive(Clone, PartialEq, Eq)]
struct CounterpartyCommitmentParameters { struct CounterpartyCommitmentParameters {
counterparty_delayed_payment_base_key: PublicKey, counterparty_delayed_payment_base_key: PublicKey,
counterparty_htlc_base_key: PublicKey, counterparty_htlc_base_key: PublicKey,
@ -334,7 +334,7 @@ impl Readable for CounterpartyCommitmentParameters {
/// observed, as well as the transaction causing it. /// observed, as well as the transaction causing it.
/// ///
/// Used to determine when the on-chain event can be considered safe from a chain reorganization. /// 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 { struct OnchainEventEntry {
txid: Txid, txid: Txid,
height: u32, height: u32,
@ -377,7 +377,7 @@ type CommitmentTxCounterpartyOutputInfo = Option<(u32, u64)>;
/// Upon discovering of some classes of onchain tx by ChannelMonitor, we may have to take actions on it /// 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) /// once they mature to enough confirmations (ANTI_REORG_DELAY)
#[derive(PartialEq, Eq)] #[derive(Clone, PartialEq, Eq)]
enum OnchainEvent { enum OnchainEvent {
/// An outbound HTLC failing after a transaction is confirmed. Used /// An outbound HTLC failing after a transaction is confirmed. Used
/// * when an outbound HTLC output is spent by us after the HTLC timed out /// * when an outbound HTLC output is spent by us after the HTLC timed out
@ -682,7 +682,7 @@ impl Balance {
} }
/// An HTLC which has been irrevocably resolved on-chain, and has reached ANTI_REORG_DELAY. /// An HTLC which has been irrevocably resolved on-chain, and has reached ANTI_REORG_DELAY.
#[derive(PartialEq, Eq)] #[derive(Clone, PartialEq, Eq)]
struct IrrevocablyResolvedHTLC { struct IrrevocablyResolvedHTLC {
commitment_tx_output_idx: Option<u32>, commitment_tx_output_idx: Option<u32>,
/// The txid of the transaction which resolved the HTLC, this may be a commitment (if the HTLC /// The txid of the transaction which resolved the HTLC, this may be a commitment (if the HTLC
@ -750,7 +750,14 @@ pub struct ChannelMonitor<Signer: WriteableEcdsaChannelSigner> {
pub(super) inner: Mutex<ChannelMonitorImpl<Signer>>, pub(super) inner: Mutex<ChannelMonitorImpl<Signer>>,
} }
#[derive(PartialEq)] impl<Signer: WriteableEcdsaChannelSigner> Clone for ChannelMonitor<Signer> where Signer: Clone {
fn clone(&self) -> Self {
let inner = self.inner.lock().unwrap().clone();
ChannelMonitor::from_impl(inner)
}
}
#[derive(Clone, PartialEq)]
pub(crate) struct ChannelMonitorImpl<Signer: WriteableEcdsaChannelSigner> { pub(crate) struct ChannelMonitorImpl<Signer: WriteableEcdsaChannelSigner> {
latest_update_id: u64, latest_update_id: u64,
commitment_transaction_number_obscure_factor: u64, commitment_transaction_number_obscure_factor: u64,

View file

@ -50,7 +50,7 @@ const MAX_ALLOC_SIZE: usize = 64*1024;
/// transaction causing it. /// transaction causing it.
/// ///
/// Used to determine when the on-chain event can be considered safe from a chain reorganization. /// 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 { struct OnchainEventEntry {
txid: Txid, txid: Txid,
height: u32, height: u32,
@ -70,7 +70,7 @@ impl OnchainEventEntry {
/// Events for claims the [`OnchainTxHandler`] has generated. Once the events are considered safe /// Events for claims the [`OnchainTxHandler`] has generated. Once the events are considered safe
/// from a chain reorg, the [`OnchainTxHandler`] will act accordingly. /// from a chain reorg, the [`OnchainTxHandler`] will act accordingly.
#[derive(PartialEq, Eq)] #[derive(Clone, PartialEq, Eq)]
enum OnchainEvent { enum OnchainEvent {
/// A pending request has been claimed by a transaction spending the exact same set of outpoints /// 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 /// as the request. This claim can either be ours or from the counterparty. Once the claiming
@ -172,6 +172,7 @@ impl Writeable for Option<Vec<Option<(usize, Signature)>>> {
} }
/// The claim commonly referred to as the pre-signed second-stage HTLC transaction. /// The claim commonly referred to as the pre-signed second-stage HTLC transaction.
#[derive(Clone, PartialEq, Eq)]
pub(crate) struct ExternalHTLCClaim { pub(crate) struct ExternalHTLCClaim {
pub(crate) commitment_txid: Txid, pub(crate) commitment_txid: Txid,
pub(crate) per_commitment_number: u64, pub(crate) per_commitment_number: u64,
@ -182,6 +183,7 @@ pub(crate) struct ExternalHTLCClaim {
// Represents the different types of claims for which events are yielded externally to satisfy said // Represents the different types of claims for which events are yielded externally to satisfy said
// claims. // claims.
#[derive(Clone, PartialEq, Eq)]
pub(crate) enum ClaimEvent { pub(crate) enum ClaimEvent {
/// Event yielded to signal that the commitment transaction fee must be bumped to claim any /// 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. /// encumbered funds and proceed to HTLC resolution, if any HTLCs exist.
@ -211,6 +213,7 @@ pub(crate) enum OnchainClaim {
/// OnchainTxHandler receives claiming requests, aggregates them if it's sound, broadcast and /// OnchainTxHandler receives claiming requests, aggregates them if it's sound, broadcast and
/// do RBF bumping if possible. /// do RBF bumping if possible.
#[derive(Clone)]
pub struct OnchainTxHandler<ChannelSigner: WriteableEcdsaChannelSigner> { pub struct OnchainTxHandler<ChannelSigner: WriteableEcdsaChannelSigner> {
destination_script: Script, destination_script: Script,
holder_commitment: HolderCommitmentTransaction, holder_commitment: HolderCommitmentTransaction,