Skip to content

Commit

Permalink
Import types we use in test_utils
Browse files Browse the repository at this point in the history
`test_utils` is ancient and didn't import common types like
`ChannelMonitor` for some reason. Here we do that, cleaning up
some code.
  • Loading branch information
TheBlueMatt committed Jan 20, 2025
1 parent 86308e1 commit 8e9ad55
Showing 1 changed file with 27 additions and 36 deletions.
63 changes: 27 additions & 36 deletions lightning/src/util/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ use crate::chain::chaininterface;
use crate::chain::chaininterface::ConfirmationTarget;
#[cfg(test)]
use crate::chain::chaininterface::FEERATE_FLOOR_SATS_PER_KW;
use crate::chain::chainmonitor;
use crate::chain::channelmonitor;
use crate::chain::channelmonitor::MonitorEvent;
use crate::chain::chainmonitor::{ChainMonitor, Persist};
use crate::chain::channelmonitor::{
ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, MonitorEvent,
};
use crate::chain::transaction::OutPoint;
use crate::chain::WatchedOutput;
use crate::events;
Expand Down Expand Up @@ -386,16 +387,16 @@ impl SignerProvider for OnlyReadsKeysInterface {
}

pub struct TestChainMonitor<'a> {
pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor<TestChannelSigner>)>>,
pub monitor_updates: Mutex<HashMap<ChannelId, Vec<channelmonitor::ChannelMonitorUpdate>>>,
pub added_monitors: Mutex<Vec<(OutPoint, ChannelMonitor<TestChannelSigner>)>>,
pub monitor_updates: Mutex<HashMap<ChannelId, Vec<ChannelMonitorUpdate>>>,
pub latest_monitor_update_id: Mutex<HashMap<ChannelId, (OutPoint, u64, u64)>>,
pub chain_monitor: chainmonitor::ChainMonitor<
pub chain_monitor: ChainMonitor<
TestChannelSigner,
&'a TestChainSource,
&'a dyn chaininterface::BroadcasterInterface,
&'a TestFeeEstimator,
&'a TestLogger,
&'a dyn chainmonitor::Persist<TestChannelSigner>,
&'a dyn Persist<TestChannelSigner>,
>,
pub keys_manager: &'a TestKeysInterface,
/// If this is set to Some(), the next update_channel call (not watch_channel) must be a
Expand All @@ -410,8 +411,7 @@ impl<'a> TestChainMonitor<'a> {
pub fn new(
chain_source: Option<&'a TestChainSource>,
broadcaster: &'a dyn chaininterface::BroadcasterInterface, logger: &'a TestLogger,
fee_estimator: &'a TestFeeEstimator,
persister: &'a dyn chainmonitor::Persist<TestChannelSigner>,
fee_estimator: &'a TestFeeEstimator, persister: &'a dyn Persist<TestChannelSigner>,
keys_manager: &'a TestKeysInterface,
) -> Self {
let added_monitors = Mutex::new(Vec::new());
Expand All @@ -423,7 +423,7 @@ impl<'a> TestChainMonitor<'a> {
added_monitors,
monitor_updates,
latest_monitor_update_id,
chain_monitor: chainmonitor::ChainMonitor::new(
chain_monitor: ChainMonitor::new(
chain_source,
broadcaster,
logger,
Expand All @@ -444,13 +444,13 @@ impl<'a> TestChainMonitor<'a> {
}
impl<'a> chain::Watch<TestChannelSigner> for TestChainMonitor<'a> {
fn watch_channel(
&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<TestChannelSigner>,
&self, funding_txo: OutPoint, monitor: ChannelMonitor<TestChannelSigner>,
) -> Result<chain::ChannelMonitorUpdateStatus, ()> {
// At every point where we get a monitor update, we should be able to send a useful monitor
// to a watchtower and disk...
let mut w = TestVecWriter(Vec::new());
monitor.write(&mut w).unwrap();
let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::read(
let new_monitor = <(BlockHash, ChannelMonitor<TestChannelSigner>)>::read(
&mut io::Cursor::new(&w.0),
(self.keys_manager, self.keys_manager),
)
Expand All @@ -466,15 +466,12 @@ impl<'a> chain::Watch<TestChannelSigner> for TestChainMonitor<'a> {
}

fn update_channel(
&self, funding_txo: OutPoint, update: &channelmonitor::ChannelMonitorUpdate,
&self, funding_txo: OutPoint, update: &ChannelMonitorUpdate,
) -> chain::ChannelMonitorUpdateStatus {
// Every monitor update should survive roundtrip
let mut w = TestVecWriter(Vec::new());
update.write(&mut w).unwrap();
assert!(
channelmonitor::ChannelMonitorUpdate::read(&mut io::Cursor::new(&w.0)).unwrap()
== *update
);
assert_eq!(ChannelMonitorUpdate::read(&mut &w.0[..]).unwrap(), *update);
let channel_id =
update.channel_id.unwrap_or(ChannelId::v1_from_funding_outpoint(funding_txo));

Expand All @@ -488,11 +485,9 @@ impl<'a> chain::Watch<TestChannelSigner> for TestChainMonitor<'a> {
if let Some(exp) = self.expect_channel_force_closed.lock().unwrap().take() {
assert_eq!(channel_id, exp.0);
assert_eq!(update.updates.len(), 1);
if let channelmonitor::ChannelMonitorUpdateStep::ChannelForceClosed {
should_broadcast,
} = update.updates[0]
{
assert_eq!(should_broadcast, exp.1);
let update = &update.updates[0];
if let ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast } = update {
assert_eq!(*should_broadcast, exp.1);
} else {
panic!();
}
Expand All @@ -508,7 +503,7 @@ impl<'a> chain::Watch<TestChannelSigner> for TestChainMonitor<'a> {
let monitor = self.chain_monitor.get_monitor(funding_txo).unwrap();
w.0.clear();
monitor.write(&mut w).unwrap();
let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::read(
let new_monitor = <(BlockHash, ChannelMonitor<TestChannelSigner>)>::read(
&mut io::Cursor::new(&w.0),
(self.keys_manager, self.keys_manager),
)
Expand Down Expand Up @@ -598,11 +593,9 @@ impl WatchtowerPersister {
}

#[cfg(test)]
impl<Signer: sign::ecdsa::EcdsaChannelSigner> chainmonitor::Persist<Signer>
for WatchtowerPersister
{
impl<Signer: sign::ecdsa::EcdsaChannelSigner> Persist<Signer> for WatchtowerPersister {
fn persist_new_channel(
&self, funding_txo: OutPoint, data: &channelmonitor::ChannelMonitor<Signer>,
&self, funding_txo: OutPoint, data: &ChannelMonitor<Signer>,
) -> chain::ChannelMonitorUpdateStatus {
let res = self.persister.persist_new_channel(funding_txo, data);

Expand Down Expand Up @@ -635,8 +628,8 @@ impl<Signer: sign::ecdsa::EcdsaChannelSigner> chainmonitor::Persist<Signer>
}

fn update_persisted_channel(
&self, funding_txo: OutPoint, update: Option<&channelmonitor::ChannelMonitorUpdate>,
data: &channelmonitor::ChannelMonitor<Signer>,
&self, funding_txo: OutPoint, update: Option<&ChannelMonitorUpdate>,
data: &ChannelMonitor<Signer>,
) -> chain::ChannelMonitorUpdateStatus {
let res = self.persister.update_persisted_channel(funding_txo, update, data);

Expand Down Expand Up @@ -679,7 +672,7 @@ impl<Signer: sign::ecdsa::EcdsaChannelSigner> chainmonitor::Persist<Signer>
}

fn archive_persisted_channel(&self, funding_txo: OutPoint) {
<TestPersister as chainmonitor::Persist<TestChannelSigner>>::archive_persisted_channel(
<TestPersister as Persist<TestChannelSigner>>::archive_persisted_channel(
&self.persister,
funding_txo,
);
Expand All @@ -692,8 +685,6 @@ pub struct TestPersister {
pub update_rets: Mutex<VecDeque<chain::ChannelMonitorUpdateStatus>>,
/// When we get an update_persisted_channel call *with* a ChannelMonitorUpdate, we insert the
/// [`ChannelMonitor::get_latest_update_id`] here.
///
/// [`ChannelMonitor`]: channelmonitor::ChannelMonitor
pub offchain_monitor_updates: Mutex<HashMap<OutPoint, HashSet<u64>>>,
/// When we get an update_persisted_channel call with no ChannelMonitorUpdate, we insert the
/// monitor's funding outpoint here.
Expand All @@ -712,9 +703,9 @@ impl TestPersister {
self.update_rets.lock().unwrap().push_back(next_ret);
}
}
impl<Signer: sign::ecdsa::EcdsaChannelSigner> chainmonitor::Persist<Signer> for TestPersister {
impl<Signer: sign::ecdsa::EcdsaChannelSigner> Persist<Signer> for TestPersister {
fn persist_new_channel(
&self, _funding_txo: OutPoint, _data: &channelmonitor::ChannelMonitor<Signer>,
&self, _funding_txo: OutPoint, _data: &ChannelMonitor<Signer>,
) -> chain::ChannelMonitorUpdateStatus {
if let Some(update_ret) = self.update_rets.lock().unwrap().pop_front() {
return update_ret;
Expand All @@ -723,8 +714,8 @@ impl<Signer: sign::ecdsa::EcdsaChannelSigner> chainmonitor::Persist<Signer> for
}

fn update_persisted_channel(
&self, funding_txo: OutPoint, update: Option<&channelmonitor::ChannelMonitorUpdate>,
_data: &channelmonitor::ChannelMonitor<Signer>,
&self, funding_txo: OutPoint, update: Option<&ChannelMonitorUpdate>,
_data: &ChannelMonitor<Signer>,
) -> chain::ChannelMonitorUpdateStatus {
let mut ret = chain::ChannelMonitorUpdateStatus::Completed;
if let Some(update_ret) = self.update_rets.lock().unwrap().pop_front() {
Expand Down

0 comments on commit 8e9ad55

Please sign in to comment.