Skip to content

Commit 491b694

Browse files
committed
Enforce that ChanelSigner::pubkeys is only called once
In the previous commit we partially reverted 9d291e0 renaming `ChannelSigner::new_pubkeys` to `pubkeys` again, but we still don't want to go back to requiring that `pubkeys` return the same contents on each call. Thus, here, we add test logic to check that `pubkeys` isn't called more than once.
1 parent e95ebf8 commit 491b694

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

lightning/src/util/test_channel_signer.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ use crate::prelude::*;
2424
#[cfg(any(test, feature = "_test_utils"))]
2525
use crate::sync::MutexGuard;
2626
use crate::sync::{Arc, Mutex};
27+
2728
use core::cmp;
29+
use core::sync::atomic::{AtomicBool, Ordering};
2830

2931
use bitcoin::hashes::Hash;
3032
use bitcoin::sighash;
@@ -68,13 +70,31 @@ pub const INITIAL_REVOKED_COMMITMENT_NUMBER: u64 = 1 << 48;
6870
///
6971
/// Note that before we do so we should ensure its serialization format has backwards- and
7072
/// forwards-compatibility prefix/suffixes!
71-
#[derive(Clone)]
7273
pub struct TestChannelSigner {
7374
pub inner: DynSigner,
7475
/// Channel state used for policy enforcement
7576
pub state: Arc<Mutex<EnforcementState>>,
7677
pub disable_revocation_policy_check: bool,
7778
pub disable_all_state_policy_checks: bool,
79+
have_fetched_pubkeys: AtomicBool,
80+
}
81+
82+
impl Clone for TestChannelSigner {
83+
fn clone(&self) -> Self {
84+
// Generally, a signer should only ever be cloned when a ChannelMonitor is cloned (which
85+
// doesn't fetch the pubkeys at all). This isn't really a critical test, but if it
86+
// it ever does fail we should make sure the clone is hapening in a sensible place.
87+
assert!(!self.have_fetched_pubkeys.load(Ordering::Acquire));
88+
Self {
89+
inner: self.inner.clone(),
90+
state: Arc::clone(&self.state),
91+
disable_revocation_policy_check: self.disable_revocation_policy_check,
92+
disable_all_state_policy_checks: self.disable_all_state_policy_checks,
93+
// In some tests we clone a `ChannelMonitor` multiple times, so have to initialize with
94+
// `!have_fetched_pubkeys` to ensure the above assertion passes.
95+
have_fetched_pubkeys: AtomicBool::new(false),
96+
}
97+
}
7898
}
7999

80100
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -129,6 +149,7 @@ impl TestChannelSigner {
129149
state,
130150
disable_revocation_policy_check: false,
131151
disable_all_state_policy_checks: false,
152+
have_fetched_pubkeys: AtomicBool::new(false),
132153
}
133154
}
134155

@@ -141,7 +162,13 @@ impl TestChannelSigner {
141162
inner: DynSigner, state: Arc<Mutex<EnforcementState>>,
142163
disable_revocation_policy_check: bool, disable_all_state_policy_checks: bool,
143164
) -> Self {
144-
Self { inner, state, disable_revocation_policy_check, disable_all_state_policy_checks }
165+
Self {
166+
inner,
167+
state,
168+
disable_revocation_policy_check,
169+
disable_all_state_policy_checks,
170+
have_fetched_pubkeys: AtomicBool::new(false),
171+
}
145172
}
146173

147174
#[cfg(any(test, feature = "_test_utils"))]
@@ -222,6 +249,7 @@ impl ChannelSigner for TestChannelSigner {
222249
}
223250

224251
fn pubkeys(&self, secp_ctx: &Secp256k1<secp256k1::All>) -> ChannelPublicKeys {
252+
assert!(!self.have_fetched_pubkeys.swap(true, Ordering::AcqRel));
225253
self.inner.pubkeys(secp_ctx)
226254
}
227255

0 commit comments

Comments
 (0)