Skip to content

Commit 40283e7

Browse files
authored
Merge pull request #3151 from alecchendev/2024-06-async-test-util-followup
Async signing test util follow ups
2 parents bfc20f8 + 1ce5bbd commit 40283e7

File tree

3 files changed

+56
-31
lines changed

3 files changed

+56
-31
lines changed

lightning/src/ln/functional_test_utils.rs

+16
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,22 @@ impl<'a, 'b, 'c> Node<'a, 'b, 'c> {
486486
self.blocks.lock().unwrap()[height as usize].0.header
487487
}
488488

489+
/// Executes `enable_channel_signer_op` for every single signer operation for this channel.
490+
#[cfg(test)]
491+
pub fn enable_all_channel_signer_ops(&self, peer_id: &PublicKey, chan_id: &ChannelId) {
492+
for signer_op in SignerOp::all() {
493+
self.enable_channel_signer_op(peer_id, chan_id, signer_op);
494+
}
495+
}
496+
497+
/// Executes `disable_channel_signer_op` for every single signer operation for this channel.
498+
#[cfg(test)]
499+
pub fn disable_all_channel_signer_ops(&self, peer_id: &PublicKey, chan_id: &ChannelId) {
500+
for signer_op in SignerOp::all() {
501+
self.disable_channel_signer_op(peer_id, chan_id, signer_op);
502+
}
503+
}
504+
489505
/// Toggles this node's signer to be available for the given signer operation.
490506
/// This is useful for testing behavior for restoring an async signer that previously
491507
/// could not return a signature immediately.

lightning/src/util/test_channel_signer.rs

+38-28
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::sign::ecdsa::EcdsaChannelSigner;
1818
#[allow(unused_imports)]
1919
use crate::prelude::*;
2020

21-
use core::{cmp, fmt};
21+
use core::cmp;
2222
use crate::sync::{Mutex, Arc};
2323
#[cfg(test)] use crate::sync::MutexGuard;
2424

@@ -71,9 +71,6 @@ pub struct TestChannelSigner {
7171
/// Channel state used for policy enforcement
7272
pub state: Arc<Mutex<EnforcementState>>,
7373
pub disable_revocation_policy_check: bool,
74-
/// Set of signer operations that are disabled. If an operation is disabled,
75-
/// the signer will return `Err` when the corresponding method is called.
76-
pub disabled_signer_ops: Arc<Mutex<HashSet<SignerOp>>>,
7774
}
7875

7976
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -93,23 +90,23 @@ pub enum SignerOp {
9390
SignChannelAnnouncementWithFundingKey,
9491
}
9592

96-
impl fmt::Display for SignerOp {
97-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
98-
match self {
99-
SignerOp::GetPerCommitmentPoint => write!(f, "get_per_commitment_point"),
100-
SignerOp::ReleaseCommitmentSecret => write!(f, "release_commitment_secret"),
101-
SignerOp::ValidateHolderCommitment => write!(f, "validate_holder_commitment"),
102-
SignerOp::SignCounterpartyCommitment => write!(f, "sign_counterparty_commitment"),
103-
SignerOp::ValidateCounterpartyRevocation => write!(f, "validate_counterparty_revocation"),
104-
SignerOp::SignHolderCommitment => write!(f, "sign_holder_commitment"),
105-
SignerOp::SignJusticeRevokedOutput => write!(f, "sign_justice_revoked_output"),
106-
SignerOp::SignJusticeRevokedHtlc => write!(f, "sign_justice_revoked_htlc"),
107-
SignerOp::SignHolderHtlcTransaction => write!(f, "sign_holder_htlc_transaction"),
108-
SignerOp::SignCounterpartyHtlcTransaction => write!(f, "sign_counterparty_htlc_transaction"),
109-
SignerOp::SignClosingTransaction => write!(f, "sign_closing_transaction"),
110-
SignerOp::SignHolderAnchorInput => write!(f, "sign_holder_anchor_input"),
111-
SignerOp::SignChannelAnnouncementWithFundingKey => write!(f, "sign_channel_announcement_with_funding_key"),
112-
}
93+
impl SignerOp {
94+
pub fn all() -> Vec<Self> {
95+
vec![
96+
SignerOp::GetPerCommitmentPoint,
97+
SignerOp::ReleaseCommitmentSecret,
98+
SignerOp::ValidateHolderCommitment,
99+
SignerOp::SignCounterpartyCommitment,
100+
SignerOp::ValidateCounterpartyRevocation,
101+
SignerOp::SignHolderCommitment,
102+
SignerOp::SignJusticeRevokedOutput,
103+
SignerOp::SignJusticeRevokedHtlc,
104+
SignerOp::SignHolderHtlcTransaction,
105+
SignerOp::SignCounterpartyHtlcTransaction,
106+
SignerOp::SignClosingTransaction,
107+
SignerOp::SignHolderAnchorInput,
108+
SignerOp::SignChannelAnnouncementWithFundingKey,
109+
]
113110
}
114111
}
115112

@@ -127,7 +124,6 @@ impl TestChannelSigner {
127124
inner,
128125
state,
129126
disable_revocation_policy_check: false,
130-
disabled_signer_ops: Arc::new(Mutex::new(new_hash_set())),
131127
}
132128
}
133129

@@ -141,7 +137,6 @@ impl TestChannelSigner {
141137
inner,
142138
state,
143139
disable_revocation_policy_check,
144-
disabled_signer_ops: Arc::new(Mutex::new(new_hash_set())),
145140
}
146141
}
147142

@@ -152,16 +147,19 @@ impl TestChannelSigner {
152147
self.state.lock().unwrap()
153148
}
154149

155-
pub fn enable_op(&mut self, signer_op: SignerOp) {
156-
self.disabled_signer_ops.lock().unwrap().remove(&signer_op);
150+
#[cfg(test)]
151+
pub fn enable_op(&self, signer_op: SignerOp) {
152+
self.get_enforcement_state().disabled_signer_ops.remove(&signer_op);
157153
}
158154

159-
pub fn disable_op(&mut self, signer_op: SignerOp) {
160-
self.disabled_signer_ops.lock().unwrap().insert(signer_op);
155+
#[cfg(test)]
156+
pub fn disable_op(&self, signer_op: SignerOp) {
157+
self.get_enforcement_state().disabled_signer_ops.insert(signer_op);
161158
}
162159

160+
#[cfg(test)]
163161
fn is_signer_available(&self, signer_op: SignerOp) -> bool {
164-
!self.disabled_signer_ops.lock().unwrap().contains(&signer_op)
162+
!self.get_enforcement_state().disabled_signer_ops.contains(&signer_op)
165163
}
166164
}
167165

@@ -189,6 +187,7 @@ impl ChannelSigner for TestChannelSigner {
189187
}
190188

191189
fn validate_counterparty_revocation(&self, idx: u64, _secret: &SecretKey) -> Result<(), ()> {
190+
#[cfg(test)]
192191
if !self.is_signer_available(SignerOp::ValidateCounterpartyRevocation) {
193192
return Err(());
194193
}
@@ -212,6 +211,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
212211
self.verify_counterparty_commitment_tx(commitment_tx, secp_ctx);
213212

214213
{
214+
#[cfg(test)]
215215
if !self.is_signer_available(SignerOp::SignCounterpartyCommitment) {
216216
return Err(());
217217
}
@@ -231,6 +231,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
231231
}
232232

233233
fn sign_holder_commitment(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
234+
#[cfg(test)]
234235
if !self.is_signer_available(SignerOp::SignHolderCommitment) {
235236
return Err(());
236237
}
@@ -252,13 +253,15 @@ impl EcdsaChannelSigner for TestChannelSigner {
252253
}
253254

254255
fn sign_justice_revoked_output(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
256+
#[cfg(test)]
255257
if !self.is_signer_available(SignerOp::SignJusticeRevokedOutput) {
256258
return Err(());
257259
}
258260
Ok(EcdsaChannelSigner::sign_justice_revoked_output(&self.inner, justice_tx, input, amount, per_commitment_key, secp_ctx).unwrap())
259261
}
260262

261263
fn sign_justice_revoked_htlc(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
264+
#[cfg(test)]
262265
if !self.is_signer_available(SignerOp::SignJusticeRevokedHtlc) {
263266
return Err(());
264267
}
@@ -269,6 +272,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
269272
&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor,
270273
secp_ctx: &Secp256k1<secp256k1::All>
271274
) -> Result<Signature, ()> {
275+
#[cfg(test)]
272276
if !self.is_signer_available(SignerOp::SignHolderHtlcTransaction) {
273277
return Err(());
274278
}
@@ -305,6 +309,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
305309
}
306310

307311
fn sign_counterparty_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, amount: u64, per_commitment_point: &PublicKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
312+
#[cfg(test)]
308313
if !self.is_signer_available(SignerOp::SignCounterpartyHtlcTransaction) {
309314
return Err(());
310315
}
@@ -324,6 +329,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
324329
// As long as our minimum dust limit is enforced and is greater than our anchor output
325330
// value, an anchor output can only have an index within [0, 1].
326331
assert!(anchor_tx.input[input].previous_output.vout == 0 || anchor_tx.input[input].previous_output.vout == 1);
332+
#[cfg(test)]
327333
if !self.is_signer_available(SignerOp::SignHolderAnchorInput) {
328334
return Err(());
329335
}
@@ -417,6 +423,9 @@ pub struct EnforcementState {
417423
pub last_holder_revoked_commitment: u64,
418424
/// The last validated holder commitment number, backwards counting
419425
pub last_holder_commitment: u64,
426+
/// Set of signer operations that are disabled. If an operation is disabled,
427+
/// the signer will return `Err` when the corresponding method is called.
428+
pub disabled_signer_ops: HashSet<SignerOp>,
420429
}
421430

422431
impl EnforcementState {
@@ -427,6 +436,7 @@ impl EnforcementState {
427436
last_counterparty_revoked_commitment: INITIAL_REVOKED_COMMITMENT_NUMBER,
428437
last_holder_revoked_commitment: INITIAL_REVOKED_COMMITMENT_NUMBER,
429438
last_holder_commitment: INITIAL_REVOKED_COMMITMENT_NUMBER,
439+
disabled_signer_ops: new_hash_set(),
430440
}
431441
}
432442
}

lightning/src/util/test_utils.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,6 @@ pub struct TestKeysInterface {
12351235
pub disable_revocation_policy_check: bool,
12361236
enforcement_states: Mutex<HashMap<[u8;32], Arc<Mutex<EnforcementState>>>>,
12371237
expectations: Mutex<Option<VecDeque<OnGetShutdownScriptpubkey>>>,
1238-
pub unavailable_signers: Mutex<HashSet<[u8; 32]>>,
12391238
pub unavailable_signers_ops: Mutex<HashMap<[u8; 32], HashSet<SignerOp>>>,
12401239
}
12411240

@@ -1295,7 +1294,8 @@ impl SignerProvider for TestKeysInterface {
12951294
fn derive_channel_signer(&self, channel_value_satoshis: u64, channel_keys_id: [u8; 32]) -> TestChannelSigner {
12961295
let keys = self.backing.derive_channel_signer(channel_value_satoshis, channel_keys_id);
12971296
let state = self.make_enforcement_state_cell(keys.commitment_seed);
1298-
let mut signer = TestChannelSigner::new_with_revoked(keys, state, self.disable_revocation_policy_check);
1297+
let signer = TestChannelSigner::new_with_revoked(keys, state, self.disable_revocation_policy_check);
1298+
#[cfg(test)]
12991299
if let Some(ops) = self.unavailable_signers_ops.lock().unwrap().get(&channel_keys_id) {
13001300
for &op in ops {
13011301
signer.disable_op(op);
@@ -1339,7 +1339,6 @@ impl TestKeysInterface {
13391339
disable_revocation_policy_check: false,
13401340
enforcement_states: Mutex::new(new_hash_map()),
13411341
expectations: Mutex::new(None),
1342-
unavailable_signers: Mutex::new(new_hash_set()),
13431342
unavailable_signers_ops: Mutex::new(new_hash_map()),
13441343
}
13451344
}

0 commit comments

Comments
 (0)