From: Jeffrey Czyz Date: Mon, 1 Mar 2021 06:39:01 +0000 (-0800) Subject: Implement chain::Listen without using RefCell X-Git-Tag: v0.0.13~16^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=e8ea0d9f041651f6cfc45e54c02e8c132c0fdeaf;p=rust-lightning Implement chain::Listen without using RefCell The implementation of chain::Listen for ChannelMonitor required using a RefCell since its block_connected method required a mutable borrow. This is no longer the case since ChannelMonitor now uses interior mutability via a Mutex. So the RefCell is no longer needed. --- diff --git a/lightning-block-sync/src/init.rs b/lightning-block-sync/src/init.rs index da9895ae7..24080b15a 100644 --- a/lightning-block-sync/src/init.rs +++ b/lightning-block-sync/src/init.rs @@ -38,7 +38,6 @@ use lightning::chain; /// /// use lightning_block_sync::*; /// -/// use std::cell::RefCell; /// use std::io::Cursor; /// /// async fn init_sync< @@ -83,7 +82,7 @@ use lightning::chain; /// /// // Synchronize any channel monitors and the channel manager to be on the best block. /// let mut cache = UnboundedCache::new(); -/// let mut monitor_listener = (RefCell::new(monitor), &*tx_broadcaster, &*fee_estimator, &*logger); +/// let mut monitor_listener = (monitor, &*tx_broadcaster, &*fee_estimator, &*logger); /// let listeners = vec![ /// (monitor_block_hash, &mut monitor_listener as &mut dyn chain::Listen), /// (manager_block_hash, &mut manager as &mut dyn chain::Listen), @@ -92,7 +91,7 @@ use lightning::chain; /// block_source, Network::Bitcoin, &mut cache, listeners).await.unwrap(); /// /// // Allow the chain monitor to watch any channels. -/// let monitor = monitor_listener.0.into_inner(); +/// let monitor = monitor_listener.0; /// chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor); /// /// // Create an SPV client to notify the chain monitor and channel manager of block events. diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index a99769472..034854810 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -50,7 +50,6 @@ use util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, U48}; use util::byte_utils; use util::events::Event; -use std::cell::RefCell; use std::collections::{HashMap, HashSet, hash_map}; use std::{cmp, mem}; use std::io::Error; @@ -2474,7 +2473,7 @@ pub trait Persist: Send + Sync { fn update_persisted_channel(&self, id: OutPoint, update: &ChannelMonitorUpdate, data: &ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr>; } -impl chain::Listen for (RefCell>, T, F, L) +impl chain::Listen for (ChannelMonitor, T, F, L) where T::Target: BroadcasterInterface, F::Target: FeeEstimator, @@ -2482,11 +2481,11 @@ where { fn block_connected(&self, block: &Block, height: u32) { let txdata: Vec<_> = block.txdata.iter().enumerate().collect(); - self.0.borrow_mut().block_connected(&block.header, &txdata, height, &*self.1, &*self.2, &*self.3); + self.0.block_connected(&block.header, &txdata, height, &*self.1, &*self.2, &*self.3); } fn block_disconnected(&self, header: &BlockHeader, height: u32) { - self.0.borrow_mut().block_disconnected(header, height, &*self.1, &*self.2, &*self.3); + self.0.block_disconnected(header, height, &*self.1, &*self.2, &*self.3); } }