+ disable_revocation_policy_check: false,
+ enforcement_states: Mutex::new(HashMap::new()),
+ expectations: Mutex::new(None),
+ }
+ }
+
+ /// Sets an expectation that [`keysinterface::KeysInterface::get_shutdown_scriptpubkey`] is
+ /// called.
+ pub fn expect(&self, expectation: OnGetShutdownScriptpubkey) -> &Self {
+ self.expectations.lock().unwrap()
+ .get_or_insert_with(|| VecDeque::new())
+ .push_back(expectation);
+ self
+ }
+
+ pub fn derive_channel_keys(&self, channel_value_satoshis: u64, id: &[u8; 32]) -> EnforcingSigner {
+ let keys = self.backing.derive_channel_keys(channel_value_satoshis, id);
+ let state = self.make_enforcement_state_cell(keys.commitment_seed);
+ EnforcingSigner::new_with_revoked(keys, state, self.disable_revocation_policy_check)
+ }
+
+ fn make_enforcement_state_cell(&self, commitment_seed: [u8; 32]) -> Arc<Mutex<EnforcementState>> {
+ let mut states = self.enforcement_states.lock().unwrap();
+ if !states.contains_key(&commitment_seed) {
+ let state = EnforcementState::new();
+ states.insert(commitment_seed, Arc::new(Mutex::new(state)));
+ }
+ let cell = states.get(&commitment_seed).unwrap();
+ Arc::clone(cell)
+ }
+}
+
+impl Drop for TestKeysInterface {
+ fn drop(&mut self) {
+ if std::thread::panicking() {
+ return;
+ }
+
+ if let Some(expectations) = &*self.expectations.lock().unwrap() {
+ if !expectations.is_empty() {
+ panic!("Unsatisfied expectations: {:?}", expectations);
+ }
+ }
+ }
+}
+
+/// An expectation that [`keysinterface::KeysInterface::get_shutdown_scriptpubkey`] was called and
+/// returns a [`ShutdownScript`].
+pub struct OnGetShutdownScriptpubkey {
+ /// A shutdown script used to close a channel.
+ pub returns: ShutdownScript,
+}
+
+impl core::fmt::Debug for OnGetShutdownScriptpubkey {
+ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+ f.debug_struct("OnGetShutdownScriptpubkey").finish()
+ }
+}
+
+pub struct TestChainSource {
+ pub genesis_hash: BlockHash,
+ pub utxo_ret: Mutex<Result<TxOut, chain::AccessError>>,
+ pub watched_txn: Mutex<HashSet<(Txid, Script)>>,
+ pub watched_outputs: Mutex<HashSet<(OutPoint, Script)>>,
+ expectations: Mutex<Option<VecDeque<OnRegisterOutput>>>,
+}
+
+impl TestChainSource {
+ pub fn new(network: Network) -> Self {
+ let script_pubkey = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
+ Self {
+ genesis_hash: genesis_block(network).block_hash(),
+ utxo_ret: Mutex::new(Ok(TxOut { value: u64::max_value(), script_pubkey })),
+ watched_txn: Mutex::new(HashSet::new()),
+ watched_outputs: Mutex::new(HashSet::new()),
+ expectations: Mutex::new(None),
+ }
+ }
+
+ /// Sets an expectation that [`chain::Filter::register_output`] is called.
+ pub fn expect(&self, expectation: OnRegisterOutput) -> &Self {
+ self.expectations.lock().unwrap()
+ .get_or_insert_with(|| VecDeque::new())
+ .push_back(expectation);
+ self
+ }
+}
+
+impl chain::Access for TestChainSource {
+ fn get_utxo(&self, genesis_hash: &BlockHash, _short_channel_id: u64) -> Result<TxOut, chain::AccessError> {
+ if self.genesis_hash != *genesis_hash {
+ return Err(chain::AccessError::UnknownChain);
+ }
+
+ self.utxo_ret.lock().unwrap().clone()
+ }
+}
+
+impl chain::Filter for TestChainSource {
+ fn register_tx(&self, txid: &Txid, script_pubkey: &Script) {
+ self.watched_txn.lock().unwrap().insert((*txid, script_pubkey.clone()));
+ }
+
+ fn register_output(&self, output: WatchedOutput) -> Option<(usize, Transaction)> {
+ let dependent_tx = match &mut *self.expectations.lock().unwrap() {
+ None => None,
+ Some(expectations) => match expectations.pop_front() {
+ None => {
+ panic!("Unexpected register_output: {:?}",
+ (output.outpoint, output.script_pubkey));
+ },
+ Some(expectation) => {
+ assert_eq!(output.outpoint, expectation.outpoint());
+ assert_eq!(&output.script_pubkey, expectation.script_pubkey());
+ expectation.returns
+ },
+ },
+ };
+
+ self.watched_outputs.lock().unwrap().insert((output.outpoint, output.script_pubkey));
+ dependent_tx
+ }
+}
+
+impl Drop for TestChainSource {
+ fn drop(&mut self) {
+ if std::thread::panicking() {
+ return;