Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
97 changes: 81 additions & 16 deletions fuzz/src/chanmon_consistency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<Transaction>)>,
confirmed_txids: HashSet<Txid>,
Expand Down Expand Up @@ -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<u8>,
) {
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<TestChannelSigner> for TestChainMonitor {
fn watch_channel(
Expand All @@ -299,22 +343,8 @@ impl chain::Watch<TestChannelSigner> 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
}
Expand Down Expand Up @@ -368,6 +398,41 @@ impl chain::Watch<TestChannelSigner> for TestChainMonitor {
}
}

impl lightning_0_2::chain::Watch<lightning_0_2::util::test_channel_signer::TestChannelSigner>
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<lightning_0_2::chain::ChannelMonitorUpdateStatus, ()> {
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<lightning_0_2::chain::channelmonitor::MonitorEvent>,
PublicKey,
)> {
Vec::new()
}
}

struct KeyProvider {
node_secret: SecretKey,
rand_bytes_id: atomic::AtomicU32,
Expand Down
Loading