Skip to content

Commit 2f5675b

Browse files
committed
fuzz: Add upgrade/downgrade simulation to chanmon_consistency and fix chacha20 build
1 parent cb951b4 commit 2f5675b

2 files changed

Lines changed: 82 additions & 16 deletions

File tree

fuzz/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ lightning-invoice = { path = "../lightning-invoice" }
2323
lightning-liquidity = { path = "../lightning-liquidity" }
2424
lightning-rapid-gossip-sync = { path = "../lightning-rapid-gossip-sync" }
2525
lightning-persister = { path = "../lightning-persister", features = ["tokio"]}
26+
lightning_0_2 = { package = "lightning", version = "0.2.0", features = ["_test_utils"] }
2627
bech32 = "0.11.0"
2728
bitcoin = { version = "0.32.4", features = ["secp-lowmemory"] }
2829
tokio = { version = "~1.35", default-features = false, features = ["rt-multi-thread"] }

fuzz/src/chanmon_consistency.rs

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,28 @@ impl FeeEstimator for FuzzEstimator {
130130
}
131131
}
132132

133+
impl lightning_0_2::chain::chaininterface::FeeEstimator for FuzzEstimator {
134+
fn get_est_sat_per_1000_weight(
135+
&self, conf_target: lightning_0_2::chain::chaininterface::ConfirmationTarget,
136+
) -> u32 {
137+
match conf_target {
138+
lightning_0_2::chain::chaininterface::ConfirmationTarget::MaximumFeeEstimate
139+
| lightning_0_2::chain::chaininterface::ConfirmationTarget::UrgentOnChainSweep => {
140+
MAX_FEE
141+
},
142+
lightning_0_2::chain::chaininterface::ConfirmationTarget::ChannelCloseMinimum
143+
| lightning_0_2::chain::chaininterface::ConfirmationTarget::AnchorChannelFee
144+
| lightning_0_2::chain::chaininterface::ConfirmationTarget::MinAllowedAnchorChannelRemoteFee
145+
| lightning_0_2::chain::chaininterface::ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee
146+
| lightning_0_2::chain::chaininterface::ConfirmationTarget::OutputSpendingFee => 253,
147+
lightning_0_2::chain::chaininterface::ConfirmationTarget::NonAnchorChannelFee => {
148+
let val = self.ret_val.load(atomic::Ordering::Relaxed);
149+
cmp::min(val, MAX_FEE)
150+
},
151+
}
152+
}
153+
}
154+
133155
impl FuzzEstimator {
134156
fn feerate_sat_per_kw(&self) -> FeeRate {
135157
let feerate = self.ret_val.load(atomic::Ordering::Acquire);
@@ -182,6 +204,14 @@ impl BroadcasterInterface for TestBroadcaster {
182204
}
183205
}
184206

207+
impl lightning_0_2::chain::chaininterface::BroadcasterInterface for TestBroadcaster {
208+
fn broadcast_transactions(&self, txs: &[&bitcoin::Transaction]) {
209+
for tx in txs {
210+
self.txn_broadcasted.borrow_mut().push((*tx).clone());
211+
}
212+
}
213+
}
214+
185215
struct ChainState {
186216
blocks: Vec<(Header, Vec<Transaction>)>,
187217
confirmed_txids: HashSet<Txid>,
@@ -290,6 +320,20 @@ impl TestChainMonitor {
290320
latest_monitors: Mutex::new(new_hash_map()),
291321
}
292322
}
323+
fn do_watch_channel_bytes(
324+
&self, channel_id_bytes: [u8; 32], monitor_id: u64, serialized_monitor: Vec<u8>,
325+
) {
326+
let channel_id = ChannelId(channel_id_bytes);
327+
let state = LatestMonitorState {
328+
persisted_monitor_id: monitor_id,
329+
persisted_monitor: serialized_monitor,
330+
pending_monitors: Vec::new(),
331+
};
332+
let mut latest_monitors = self.latest_monitors.lock().unwrap();
333+
if latest_monitors.insert(channel_id, state).is_some() {
334+
panic!("Already had monitor pre-watch_channel");
335+
}
336+
}
293337
}
294338
impl chain::Watch<TestChannelSigner> for TestChainMonitor {
295339
fn watch_channel(
@@ -299,22 +343,8 @@ impl chain::Watch<TestChannelSigner> for TestChainMonitor {
299343
monitor.write(&mut ser).unwrap();
300344
let monitor_id = monitor.get_latest_update_id();
301345
let res = self.chain_monitor.watch_channel(channel_id, monitor);
302-
let state = match res {
303-
Ok(chain::ChannelMonitorUpdateStatus::Completed) => LatestMonitorState {
304-
persisted_monitor_id: monitor_id,
305-
persisted_monitor: ser.0,
306-
pending_monitors: Vec::new(),
307-
},
308-
Ok(chain::ChannelMonitorUpdateStatus::InProgress) => LatestMonitorState {
309-
persisted_monitor_id: monitor_id,
310-
persisted_monitor: Vec::new(),
311-
pending_monitors: vec![(monitor_id, ser.0)],
312-
},
313-
Ok(chain::ChannelMonitorUpdateStatus::UnrecoverableError) => panic!(),
314-
Err(()) => panic!(),
315-
};
316-
if self.latest_monitors.lock().unwrap().insert(channel_id, state).is_some() {
317-
panic!("Already had monitor pre-watch_channel");
346+
if res == Ok(chain::ChannelMonitorUpdateStatus::Completed) {
347+
self.do_watch_channel_bytes(channel_id.0, monitor_id, ser.0);
318348
}
319349
res
320350
}
@@ -368,6 +398,41 @@ impl chain::Watch<TestChannelSigner> for TestChainMonitor {
368398
}
369399
}
370400

401+
impl lightning_0_2::chain::Watch<lightning_0_2::util::test_channel_signer::TestChannelSigner>
402+
for TestChainMonitor
403+
{
404+
fn watch_channel(
405+
&self, channel_id: lightning_0_2::ln::types::ChannelId,
406+
monitor: lightning_0_2::chain::channelmonitor::ChannelMonitor<
407+
lightning_0_2::util::test_channel_signer::TestChannelSigner,
408+
>,
409+
) -> Result<lightning_0_2::chain::ChannelMonitorUpdateStatus, ()> {
410+
let mut ser = Vec::new();
411+
lightning_0_2::util::ser::Writeable::write(&monitor, &mut ser).unwrap();
412+
let monitor_id = monitor.get_latest_update_id();
413+
self.do_watch_channel_bytes(channel_id.0, monitor_id, ser);
414+
415+
Ok(lightning_0_2::chain::ChannelMonitorUpdateStatus::Completed)
416+
}
417+
fn update_channel(
418+
&self, _channel_id: lightning_0_2::ln::types::ChannelId,
419+
_update: &lightning_0_2::chain::channelmonitor::ChannelMonitorUpdate,
420+
) -> lightning_0_2::chain::ChannelMonitorUpdateStatus {
421+
lightning_0_2::chain::ChannelMonitorUpdateStatus::Completed
422+
}
423+
424+
fn release_pending_monitor_events(
425+
&self,
426+
) -> Vec<(
427+
lightning_0_2::chain::transaction::OutPoint,
428+
lightning_0_2::ln::types::ChannelId,
429+
Vec<lightning_0_2::chain::channelmonitor::MonitorEvent>,
430+
PublicKey,
431+
)> {
432+
Vec::new()
433+
}
434+
}
435+
371436
struct KeyProvider {
372437
node_secret: SecretKey,
373438
rand_bytes_id: atomic::AtomicU32,

0 commit comments

Comments
 (0)