From db0287137fa327ec2be9f1b2fcb9287d76e2958b Mon Sep 17 00:00:00 2001 From: Devrandom Date: Thu, 1 Apr 2021 12:24:21 +0200 Subject: [PATCH] Separate Clone from Sign Clone requires Sized, which prevents Sign from being a dyn object. --- lightning-persister/src/lib.rs | 2 +- lightning/src/chain/keysinterface.rs | 14 ++++++++++++-- lightning/src/ln/channel.rs | 4 ++-- lightning/src/ln/functional_tests.rs | 2 +- lightning/src/util/enforcing_trait_impls.rs | 5 +++-- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lightning-persister/src/lib.rs b/lightning-persister/src/lib.rs index af11dfd16..47789f2ef 100644 --- a/lightning-persister/src/lib.rs +++ b/lightning-persister/src/lib.rs @@ -143,7 +143,7 @@ impl FilesystemPersister { } } -impl channelmonitor::Persist for FilesystemPersister { +impl channelmonitor::Persist for FilesystemPersister { fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr> { let filename = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index); util::write_to_file(self.path_to_monitor_data(), filename, monitor) diff --git a/lightning/src/chain/keysinterface.rs b/lightning/src/chain/keysinterface.rs index f12df3a80..31b309652 100644 --- a/lightning/src/chain/keysinterface.rs +++ b/lightning/src/chain/keysinterface.rs @@ -226,7 +226,7 @@ impl Readable for SpendableOutputDescriptor { /// of LN security model, orthogonal of key management issues. // TODO: We should remove Clone by instead requesting a new Sign copy when we create // ChannelMonitors instead of expecting to clone the one out of the Channel into the monitors. -pub trait Sign : Send+Clone + Writeable { +pub trait BaseSign : Send + Writeable { /// Gets the per-commitment point for a specific commitment number /// /// Note that the commitment number starts at (1 << 48) - 1 and counts backwards. @@ -344,6 +344,14 @@ pub trait Sign : Send+Clone + Writeable { fn ready_channel(&mut self, channel_parameters: &ChannelTransactionParameters); } +/// A cloneable signer. +/// +/// Although we require signers to be cloneable, it may be useful for developers to be able to use +/// signers in an un-sized way, for example as `dyn BaseSign`. Therefore we separate the Clone trait, +/// which implies Sized, into this derived trait. +pub trait Sign: BaseSign + Clone { +} + /// A trait to describe an object which can get user secrets and key material. pub trait KeysInterface: Send + Sync { /// A type which implements Sign which will be returned by get_channel_signer. @@ -549,7 +557,7 @@ impl InMemorySigner { } } -impl Sign for InMemorySigner { +impl BaseSign for InMemorySigner { fn get_per_commitment_point(&self, idx: u64, secp_ctx: &Secp256k1) -> PublicKey { let commitment_secret = SecretKey::from_slice(&chan_utils::build_commitment_secret(&self.commitment_seed, idx)).unwrap(); PublicKey::from_secret_key(secp_ctx, &commitment_secret) @@ -682,6 +690,8 @@ impl Sign for InMemorySigner { } } +impl Sign for InMemorySigner {} + impl Writeable for InMemorySigner { fn write(&self, writer: &mut W) -> Result<(), Error> { self.funding_key.write(writer)?; diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 174eed9b9..a54b9cb19 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -4792,14 +4792,14 @@ mod tests { use bitcoin::hashes::hex::FromHex; use hex; use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash}; - use ln::channel::{Channel,Sign,InboundHTLCOutput,OutboundHTLCOutput,InboundHTLCState,OutboundHTLCState,HTLCOutputInCommitment,HTLCCandidate,HTLCInitiator,TxCreationKeys}; + use ln::channel::{Channel,InboundHTLCOutput,OutboundHTLCOutput,InboundHTLCState,OutboundHTLCState,HTLCOutputInCommitment,HTLCCandidate,HTLCInitiator,TxCreationKeys}; use ln::channel::MAX_FUNDING_SATOSHIS; use ln::features::InitFeatures; use ln::msgs::{ChannelUpdate, DataLossProtect, DecodeError, OptionalField, UnsignedChannelUpdate}; use ln::chan_utils; use ln::chan_utils::{ChannelPublicKeys, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT}; use chain::chaininterface::{FeeEstimator,ConfirmationTarget}; - use chain::keysinterface::{InMemorySigner, KeysInterface}; + use chain::keysinterface::{InMemorySigner, KeysInterface, BaseSign}; use chain::transaction::OutPoint; use util::config::UserConfig; use util::enforcing_trait_impls::EnforcingSigner; diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 293ccf9f3..0d039bc56 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -16,7 +16,7 @@ use chain::Watch; use chain::channelmonitor; use chain::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY}; use chain::transaction::OutPoint; -use chain::keysinterface::{Sign, KeysInterface}; +use chain::keysinterface::{KeysInterface, BaseSign}; use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC}; use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentPreimage, PaymentHash, PaymentSecret, PaymentSendFailure, BREAKDOWN_TIMEOUT}; use ln::channel::{Channel, ChannelError}; diff --git a/lightning/src/util/enforcing_trait_impls.rs b/lightning/src/util/enforcing_trait_impls.rs index a7036a9ac..8b0152d83 100644 --- a/lightning/src/util/enforcing_trait_impls.rs +++ b/lightning/src/util/enforcing_trait_impls.rs @@ -9,7 +9,7 @@ use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitmentTransaction, CommitmentTransaction, ChannelTransactionParameters, TrustedCommitmentTransaction}; use ln::{chan_utils, msgs}; -use chain::keysinterface::{Sign, InMemorySigner}; +use chain::keysinterface::{Sign, InMemorySigner, BaseSign}; use std::cmp; use std::sync::{Mutex, Arc}; @@ -74,7 +74,7 @@ impl EnforcingSigner { } } -impl Sign for EnforcingSigner { +impl BaseSign for EnforcingSigner { fn get_per_commitment_point(&self, idx: u64, secp_ctx: &Secp256k1) -> PublicKey { self.inner.get_per_commitment_point(idx, secp_ctx) } @@ -161,6 +161,7 @@ impl Sign for EnforcingSigner { } } +impl Sign for EnforcingSigner {} impl Writeable for EnforcingSigner { fn write(&self, writer: &mut W) -> Result<(), Error> { -- 2.39.5