diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 5a2e397a064..8b3577f949a 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -23,6 +23,7 @@ lightning-invoice = { path = "../lightning-invoice" } lightning-liquidity = { path = "../lightning-liquidity" } lightning-rapid-gossip-sync = { path = "../lightning-rapid-gossip-sync" } lightning-persister = { path = "../lightning-persister", features = ["tokio"]} +lightning_0_2 = { package = "lightning", version = "0.2.0", features = ["_test_utils"] } bech32 = "0.11.0" bitcoin = { version = "0.32.4", features = ["secp-lowmemory"] } tokio = { version = "~1.35", default-features = false, features = ["rt-multi-thread"] } diff --git a/fuzz/src/chanmon_consistency.rs b/fuzz/src/chanmon_consistency.rs index 96104162db7..3572f4e8987 100644 --- a/fuzz/src/chanmon_consistency.rs +++ b/fuzz/src/chanmon_consistency.rs @@ -130,6 +130,28 @@ impl FeeEstimator for FuzzEstimator { } } +impl lightning_0_2::chain::chaininterface::FeeEstimator for FuzzEstimator { + fn get_est_sat_per_1000_weight( + &self, conf_target: lightning_0_2::chain::chaininterface::ConfirmationTarget, + ) -> u32 { + match conf_target { + lightning_0_2::chain::chaininterface::ConfirmationTarget::MaximumFeeEstimate + | lightning_0_2::chain::chaininterface::ConfirmationTarget::UrgentOnChainSweep => { + MAX_FEE + }, + lightning_0_2::chain::chaininterface::ConfirmationTarget::ChannelCloseMinimum + | lightning_0_2::chain::chaininterface::ConfirmationTarget::AnchorChannelFee + | lightning_0_2::chain::chaininterface::ConfirmationTarget::MinAllowedAnchorChannelRemoteFee + | lightning_0_2::chain::chaininterface::ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee + | lightning_0_2::chain::chaininterface::ConfirmationTarget::OutputSpendingFee => 253, + lightning_0_2::chain::chaininterface::ConfirmationTarget::NonAnchorChannelFee => { + let val = self.ret_val.load(atomic::Ordering::Relaxed); + cmp::min(val, MAX_FEE) + }, + } + } +} + impl FuzzEstimator { fn feerate_sat_per_kw(&self) -> FeeRate { let feerate = self.ret_val.load(atomic::Ordering::Acquire); @@ -182,6 +204,14 @@ impl BroadcasterInterface for TestBroadcaster { } } +impl lightning_0_2::chain::chaininterface::BroadcasterInterface for TestBroadcaster { + fn broadcast_transactions(&self, txs: &[&bitcoin::Transaction]) { + for tx in txs { + self.txn_broadcasted.borrow_mut().push((*tx).clone()); + } + } +} + struct ChainState { blocks: Vec<(Header, Vec)>, confirmed_txids: HashSet, @@ -290,6 +320,20 @@ impl TestChainMonitor { latest_monitors: Mutex::new(new_hash_map()), } } + fn do_watch_channel_bytes( + &self, channel_id_bytes: [u8; 32], monitor_id: u64, serialized_monitor: Vec, + ) { + let channel_id = ChannelId(channel_id_bytes); + let state = LatestMonitorState { + persisted_monitor_id: monitor_id, + persisted_monitor: serialized_monitor, + pending_monitors: Vec::new(), + }; + let mut latest_monitors = self.latest_monitors.lock().unwrap(); + if latest_monitors.insert(channel_id, state).is_some() { + panic!("Already had monitor pre-watch_channel"); + } + } } impl chain::Watch for TestChainMonitor { fn watch_channel( @@ -299,22 +343,8 @@ impl chain::Watch for TestChainMonitor { monitor.write(&mut ser).unwrap(); let monitor_id = monitor.get_latest_update_id(); let res = self.chain_monitor.watch_channel(channel_id, monitor); - let state = match res { - Ok(chain::ChannelMonitorUpdateStatus::Completed) => LatestMonitorState { - persisted_monitor_id: monitor_id, - persisted_monitor: ser.0, - pending_monitors: Vec::new(), - }, - Ok(chain::ChannelMonitorUpdateStatus::InProgress) => LatestMonitorState { - persisted_monitor_id: monitor_id, - persisted_monitor: Vec::new(), - pending_monitors: vec![(monitor_id, ser.0)], - }, - Ok(chain::ChannelMonitorUpdateStatus::UnrecoverableError) => panic!(), - Err(()) => panic!(), - }; - if self.latest_monitors.lock().unwrap().insert(channel_id, state).is_some() { - panic!("Already had monitor pre-watch_channel"); + if res == Ok(chain::ChannelMonitorUpdateStatus::Completed) { + self.do_watch_channel_bytes(channel_id.0, monitor_id, ser.0); } res } @@ -368,6 +398,41 @@ impl chain::Watch for TestChainMonitor { } } +impl lightning_0_2::chain::Watch + for TestChainMonitor +{ + fn watch_channel( + &self, channel_id: lightning_0_2::ln::types::ChannelId, + monitor: lightning_0_2::chain::channelmonitor::ChannelMonitor< + lightning_0_2::util::test_channel_signer::TestChannelSigner, + >, + ) -> Result { + let mut ser = Vec::new(); + lightning_0_2::util::ser::Writeable::write(&monitor, &mut ser).unwrap(); + let monitor_id = monitor.get_latest_update_id(); + self.do_watch_channel_bytes(channel_id.0, monitor_id, ser); + + Ok(lightning_0_2::chain::ChannelMonitorUpdateStatus::Completed) + } + fn update_channel( + &self, _channel_id: lightning_0_2::ln::types::ChannelId, + _update: &lightning_0_2::chain::channelmonitor::ChannelMonitorUpdate, + ) -> lightning_0_2::chain::ChannelMonitorUpdateStatus { + lightning_0_2::chain::ChannelMonitorUpdateStatus::Completed + } + + fn release_pending_monitor_events( + &self, + ) -> Vec<( + lightning_0_2::chain::transaction::OutPoint, + lightning_0_2::ln::types::ChannelId, + Vec, + PublicKey, + )> { + Vec::new() + } +} + struct KeyProvider { node_secret: SecretKey, rand_bytes_id: atomic::AtomicU32,