Skip to content

Commit 181d319

Browse files
committed
Always use KeysInterface.read_chan_signer for de-serializing EnforcingSigner in tests
1 parent 285b3fa commit 181d319

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

fuzz/src/full_stack.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use lightning::routing::network_graph::NetGraphMsgHandler;
4242
use lightning::util::config::UserConfig;
4343
use lightning::util::errors::APIError;
4444
use lightning::util::events::Event;
45-
use lightning::util::enforcing_trait_impls::EnforcingSigner;
45+
use lightning::util::enforcing_trait_impls::{EnforcingSigner, EnforcementState};
4646
use lightning::util::logger::Logger;
4747
use lightning::util::ser::Readable;
4848

@@ -315,8 +315,15 @@ impl KeysInterface for KeyProvider {
315315
(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]
316316
}
317317

318-
fn read_chan_signer(&self, data: &[u8]) -> Result<EnforcingSigner, DecodeError> {
319-
EnforcingSigner::read(&mut std::io::Cursor::new(data))
318+
fn read_chan_signer(&self, mut data: &[u8]) -> Result<EnforcingSigner, DecodeError> {
319+
let inner: InMemorySigner = Readable::read(&mut data)?;
320+
let state = Arc::new(Mutex::new(EnforcementState::new()));
321+
322+
Ok(EnforcingSigner::new_with_revoked(
323+
inner,
324+
state,
325+
false
326+
))
320327
}
321328

322329
fn sign_invoice(&self, _invoice_preimage: Vec<u8>) -> Result<RecoverableSignature, ()> {

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitment
1111
use ln::{chan_utils, msgs};
1212
use chain::keysinterface::{Sign, InMemorySigner, BaseSign};
1313

14-
use io;
1514
use prelude::*;
1615
use core::cmp;
1716
use sync::{Mutex, Arc};
@@ -23,9 +22,8 @@ use bitcoin::util::bip143;
2322
use bitcoin::secp256k1;
2423
use bitcoin::secp256k1::key::{SecretKey, PublicKey};
2524
use bitcoin::secp256k1::{Secp256k1, Signature};
26-
use util::ser::{Writeable, Writer, Readable};
25+
use util::ser::{Writeable, Writer};
2726
use io::Error;
28-
use ln::msgs::DecodeError;
2927

3028
/// Initial value for revoked commitment downward counter
3129
pub const INITIAL_REVOKED_COMMITMENT_NUMBER: u64 = 1 << 48;
@@ -199,24 +197,15 @@ impl Sign for EnforcingSigner {}
199197

200198
impl Writeable for EnforcingSigner {
201199
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
200+
// EnforcingSigner has two fields - `inner` ([`InMemorySigner`]) and `state`
201+
// ([`EnforcementState`]). `inner` is serialized here and deserialized by
202+
// [`KeysInterface::read_chan_signer`]. `state` is managed by [`KeysInterface`]
203+
// and will be serialized as needed by the implementation of that trait.
202204
self.inner.write(writer)?;
203-
// NOTE - the commitment state is maintained by KeysInterface, so we don't persist it
204205
Ok(())
205206
}
206207
}
207208

208-
impl Readable for EnforcingSigner {
209-
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
210-
let inner = Readable::read(reader)?;
211-
let state = Arc::new(Mutex::new(EnforcementState::new()));
212-
Ok(EnforcingSigner {
213-
inner,
214-
state,
215-
disable_revocation_policy_check: false,
216-
})
217-
}
218-
}
219-
220209
impl EnforcingSigner {
221210
fn verify_counterparty_commitment_tx<'a, T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &'a CommitmentTransaction, secp_ctx: &Secp256k1<T>) -> TrustedCommitmentTransaction<'a> {
222211
commitment_tx.verify(&self.inner.get_channel_parameters().as_counterparty_broadcastable(),

lightning/src/util/test_utils.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,15 @@ impl keysinterface::KeysInterface for OnlyReadsKeysInterface {
7676
fn get_channel_signer(&self, _inbound: bool, _channel_value_satoshis: u64) -> EnforcingSigner { unreachable!(); }
7777
fn get_secure_random_bytes(&self) -> [u8; 32] { [0; 32] }
7878

79-
fn read_chan_signer(&self, reader: &[u8]) -> Result<Self::Signer, msgs::DecodeError> {
80-
EnforcingSigner::read(&mut io::Cursor::new(reader))
79+
fn read_chan_signer(&self, mut reader: &[u8]) -> Result<Self::Signer, msgs::DecodeError> {
80+
let inner: InMemorySigner = Readable::read(&mut reader)?;
81+
let state = Arc::new(Mutex::new(EnforcementState::new()));
82+
83+
Ok(EnforcingSigner::new_with_revoked(
84+
inner,
85+
state,
86+
false
87+
))
8188
}
8289
fn sign_invoice(&self, _invoice_preimage: Vec<u8>) -> Result<RecoverableSignature, ()> { unreachable!(); }
8390
}
@@ -499,11 +506,11 @@ impl keysinterface::KeysInterface for TestKeysInterface {
499506
let inner: InMemorySigner = Readable::read(&mut reader)?;
500507
let state = self.make_enforcement_state_cell(inner.commitment_seed);
501508

502-
Ok(EnforcingSigner {
509+
Ok(EnforcingSigner::new_with_revoked(
503510
inner,
504511
state,
505-
disable_revocation_policy_check: self.disable_revocation_policy_check,
506-
})
512+
self.disable_revocation_policy_check
513+
))
507514
}
508515

509516
fn sign_invoice(&self, invoice_preimage: Vec<u8>) -> Result<RecoverableSignature, ()> {

0 commit comments

Comments
 (0)