Separate Clone from Sign
authorDevrandom <c1.devrandom@niftybox.net>
Thu, 1 Apr 2021 10:24:21 +0000 (12:24 +0200)
committerDevrandom <c1.devrandom@niftybox.net>
Fri, 9 Apr 2021 09:19:22 +0000 (11:19 +0200)
Clone requires Sized, which prevents Sign from being a dyn object.

lightning-persister/src/lib.rs
lightning/src/chain/keysinterface.rs
lightning/src/ln/channel.rs
lightning/src/ln/functional_tests.rs
lightning/src/util/enforcing_trait_impls.rs

index af11dfd167b4d51f1c6a1bde4ea7ab5eb42308a9..47789f2ef2ca6250aeea05679316ce07f900a449 100644 (file)
@@ -143,7 +143,7 @@ impl FilesystemPersister {
                }
 }
 
-impl<ChannelSigner: Sign + Send + Sync> channelmonitor::Persist<ChannelSigner> for FilesystemPersister {
+impl<ChannelSigner: Sign> channelmonitor::Persist<ChannelSigner> for FilesystemPersister {
        fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr> {
                let filename = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
                util::write_to_file(self.path_to_monitor_data(), filename, monitor)
index f12df3a806fe0aae9a89caf32c3a361ca0d76cb9..31b3096520888bfe40079cb949f019d6da9a31aa 100644 (file)
@@ -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<secp256k1::All>) -> 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<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
                self.funding_key.write(writer)?;
index 174eed9b9185669dcc704f81d5b849d62837d641..a54b9cb19386231a36bd460f66092dea51d74276 100644 (file)
@@ -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;
index 293ccf9f3ea949940525c11aaa18375c6a7e6c43..0d039bc56c70304155e9d5668dd4baf641b11c92 100644 (file)
@@ -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};
index a7036a9acd5d2b3de0754b6ad31dcb9758ef1916..8b0152d834e44972995805f1b6f79aff36d8d07c 100644 (file)
@@ -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<secp256k1::All>) -> 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<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {