From: Devrandom Date: Tue, 17 Aug 2021 11:04:29 +0000 (+0200) Subject: Always use KeysInterface.read_chan_signer for de-serializing EnforcingSigner in tests X-Git-Tag: v0.0.101~28^2~2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=rust-lightning;a=commitdiff_plain;h=181d3196308ea0bd1962712857581133b5b8e6d8 Always use KeysInterface.read_chan_signer for de-serializing EnforcingSigner in tests --- diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index 510966f0..28592ffd 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -42,7 +42,7 @@ use lightning::routing::network_graph::NetGraphMsgHandler; use lightning::util::config::UserConfig; use lightning::util::errors::APIError; use lightning::util::events::Event; -use lightning::util::enforcing_trait_impls::EnforcingSigner; +use lightning::util::enforcing_trait_impls::{EnforcingSigner, EnforcementState}; use lightning::util::logger::Logger; use lightning::util::ser::Readable; @@ -315,8 +315,15 @@ impl KeysInterface for KeyProvider { (ctr >> 8*7) as u8, (ctr >> 8*6) as u8, (ctr >> 8*5) as u8, (ctr >> 8*4) as u8, (ctr >> 8*3) as u8, (ctr >> 8*2) as u8, (ctr >> 8*1) as u8, 14, (ctr >> 8*0) as u8] } - fn read_chan_signer(&self, data: &[u8]) -> Result { - EnforcingSigner::read(&mut std::io::Cursor::new(data)) + fn read_chan_signer(&self, mut data: &[u8]) -> Result { + let inner: InMemorySigner = Readable::read(&mut data)?; + let state = Arc::new(Mutex::new(EnforcementState::new())); + + Ok(EnforcingSigner::new_with_revoked( + inner, + state, + false + )) } fn sign_invoice(&self, _invoice_preimage: Vec) -> Result { diff --git a/lightning/src/util/enforcing_trait_impls.rs b/lightning/src/util/enforcing_trait_impls.rs index 19408e98..7daacf36 100644 --- a/lightning/src/util/enforcing_trait_impls.rs +++ b/lightning/src/util/enforcing_trait_impls.rs @@ -11,7 +11,6 @@ use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitment use ln::{chan_utils, msgs}; use chain::keysinterface::{Sign, InMemorySigner, BaseSign}; -use io; use prelude::*; use core::cmp; use sync::{Mutex, Arc}; @@ -23,9 +22,8 @@ use bitcoin::util::bip143; use bitcoin::secp256k1; use bitcoin::secp256k1::key::{SecretKey, PublicKey}; use bitcoin::secp256k1::{Secp256k1, Signature}; -use util::ser::{Writeable, Writer, Readable}; +use util::ser::{Writeable, Writer}; use io::Error; -use ln::msgs::DecodeError; /// Initial value for revoked commitment downward counter pub const INITIAL_REVOKED_COMMITMENT_NUMBER: u64 = 1 << 48; @@ -199,24 +197,15 @@ impl Sign for EnforcingSigner {} impl Writeable for EnforcingSigner { fn write(&self, writer: &mut W) -> Result<(), Error> { + // EnforcingSigner has two fields - `inner` ([`InMemorySigner`]) and `state` + // ([`EnforcementState`]). `inner` is serialized here and deserialized by + // [`KeysInterface::read_chan_signer`]. `state` is managed by [`KeysInterface`] + // and will be serialized as needed by the implementation of that trait. self.inner.write(writer)?; - // NOTE - the commitment state is maintained by KeysInterface, so we don't persist it Ok(()) } } -impl Readable for EnforcingSigner { - fn read(reader: &mut R) -> Result { - let inner = Readable::read(reader)?; - let state = Arc::new(Mutex::new(EnforcementState::new())); - Ok(EnforcingSigner { - inner, - state, - disable_revocation_policy_check: false, - }) - } -} - impl EnforcingSigner { fn verify_counterparty_commitment_tx<'a, T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &'a CommitmentTransaction, secp_ctx: &Secp256k1) -> TrustedCommitmentTransaction<'a> { commitment_tx.verify(&self.inner.get_channel_parameters().as_counterparty_broadcastable(), diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index fed16768..64b88acb 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -76,8 +76,15 @@ impl keysinterface::KeysInterface for OnlyReadsKeysInterface { fn get_channel_signer(&self, _inbound: bool, _channel_value_satoshis: u64) -> EnforcingSigner { unreachable!(); } fn get_secure_random_bytes(&self) -> [u8; 32] { [0; 32] } - fn read_chan_signer(&self, reader: &[u8]) -> Result { - EnforcingSigner::read(&mut io::Cursor::new(reader)) + fn read_chan_signer(&self, mut reader: &[u8]) -> Result { + let inner: InMemorySigner = Readable::read(&mut reader)?; + let state = Arc::new(Mutex::new(EnforcementState::new())); + + Ok(EnforcingSigner::new_with_revoked( + inner, + state, + false + )) } fn sign_invoice(&self, _invoice_preimage: Vec) -> Result { unreachable!(); } } @@ -499,11 +506,11 @@ impl keysinterface::KeysInterface for TestKeysInterface { let inner: InMemorySigner = Readable::read(&mut reader)?; let state = self.make_enforcement_state_cell(inner.commitment_seed); - Ok(EnforcingSigner { + Ok(EnforcingSigner::new_with_revoked( inner, state, - disable_revocation_policy_check: self.disable_revocation_policy_check, - }) + self.disable_revocation_policy_check + )) } fn sign_invoice(&self, invoice_preimage: Vec) -> Result {