use crate::util::errors::APIError;
use crate::util::config::{UserConfig, MaxDustHTLCExposure};
use crate::util::ser::{ReadableArgs, Writeable};
+#[cfg(test)]
+use crate::util::logger::Logger;
use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::transaction::{Transaction, TxOut};
pub fn get_block_header(&self, height: u32) -> BlockHeader {
self.blocks.lock().unwrap()[height as usize].0.header
}
+ /// Changes the channel signer's availability for the specified peer and channel.
+ ///
+ /// When `available` is set to `true`, the channel signer will behave normally. When set to
+ /// `false`, the channel signer will act like an off-line remote signer and will return `Err` for
+ /// several of the signing methods. Currently, only `get_per_commitment_point` and
+ /// `release_commitment_secret` are affected by this setting.
+ #[cfg(test)]
+ pub fn set_channel_signer_available(&self, peer_id: &PublicKey, chan_id: &ChannelId, available: bool) {
+ let per_peer_state = self.node.per_peer_state.read().unwrap();
+ let chan_lock = per_peer_state.get(peer_id).unwrap().lock().unwrap();
+ let signer = (|| {
+ match chan_lock.channel_by_id.get(chan_id) {
+ Some(phase) => phase.context().get_signer(),
+ None => panic!("Couldn't find a channel with id {}", chan_id),
+ }
+ })();
+ log_debug!(self.logger, "Setting channel signer for {} as available={}", chan_id, available);
+ signer.as_ecdsa().unwrap().set_available(available);
+ }
}
/// If we need an unsafe pointer to a `Node` (ie to reference it in a thread
/// Channel state used for policy enforcement
pub state: Arc<Mutex<EnforcementState>>,
pub disable_revocation_policy_check: bool,
+ /// When `true` (the default), the signer will respond immediately with signatures. When `false`,
+ /// the signer will return an error indicating that it is unavailable.
+ pub available: Arc<Mutex<bool>>,
}
impl PartialEq for TestChannelSigner {
Self {
inner,
state,
- disable_revocation_policy_check: false
+ disable_revocation_policy_check: false,
+ available: Arc::new(Mutex::new(true)),
}
}
Self {
inner,
state,
- disable_revocation_policy_check
+ disable_revocation_policy_check,
+ available: Arc::new(Mutex::new(true)),
}
}
pub fn get_enforcement_state(&self) -> MutexGuard<EnforcementState> {
self.state.lock().unwrap()
}
+
+ /// Marks the signer's availability.
+ ///
+ /// When `true`, methods are forwarded to the underlying signer as normal. When `false`, some
+ /// methods will return `Err` indicating that the signer is unavailable. Intended to be used for
+ /// testing asynchronous signing.
+ #[cfg(test)]
+ pub fn set_available(&self, available: bool) {
+ *self.available.lock().unwrap() = available;
+ }
}
impl ChannelSigner for TestChannelSigner {
self.verify_counterparty_commitment_tx(commitment_tx, secp_ctx);
{
+ if !*self.available.lock().unwrap() {
+ return Err(());
+ }
let mut state = self.state.lock().unwrap();
let actual_commitment_number = commitment_tx.commitment_number();
let last_commitment_number = state.last_counterparty_commitment;
}
fn validate_counterparty_revocation(&self, idx: u64, _secret: &SecretKey) -> Result<(), ()> {
+ if !*self.available.lock().unwrap() {
+ return Err(());
+ }
let mut state = self.state.lock().unwrap();
assert!(idx == state.last_counterparty_revoked_commitment || idx == state.last_counterparty_revoked_commitment - 1, "expecting to validate the current or next counterparty revocation - trying {}, current {}", idx, state.last_counterparty_revoked_commitment);
state.last_counterparty_revoked_commitment = idx;
}
fn sign_holder_commitment(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
+ if !*self.available.lock().unwrap() {
+ return Err(());
+ }
let trusted_tx = self.verify_holder_commitment_tx(commitment_tx, secp_ctx);
let state = self.state.lock().unwrap();
let commitment_number = trusted_tx.commitment_number();