Skip to content

Commit c39c398

Browse files
committed
ChannelManager initialization docs with example
1 parent 19e50eb commit c39c398

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,77 @@ where
11511151
/// setups. For instance, on-chain enforcement could be moved to a separate host or have added
11521152
/// redundancy, possibly as a watchtower. See [`chain::Watch`] for the relevant interface.
11531153
///
1154+
/// # Initialization
1155+
///
1156+
/// Use [`ChannelManager::new`] with the most recent [`BlockHash`] when creating a fresh instance.
1157+
/// Otherwise, if restarting, construct [`ChannelManagerReadArgs`] with the necessary parameters and
1158+
/// references to any deserialized [`ChannelMonitor`]s that were previously persisted. Use this to
1159+
/// deserialize the [`ChannelManager`] and feed it any new chain data since it was last online, as
1160+
/// detailed in the [`ChannelManagerReadArgs`] documentation.
1161+
///
1162+
/// ```
1163+
/// use bitcoin::BlockHash;
1164+
/// use bitcoin::network::constants::Network;
1165+
/// use lightning::chain::BestBlock;
1166+
/// # use lightning::chain::channelmonitor::ChannelMonitor;
1167+
/// use lightning::ln::channelmanager::{ChainParameters, ChannelManager, ChannelManagerReadArgs};
1168+
/// # use lightning::routing::gossip::NetworkGraph;
1169+
/// use lightning::util::config::UserConfig;
1170+
/// use lightning::util::ser::ReadableArgs;
1171+
///
1172+
/// # fn read_channel_monitors() -> Vec<ChannelMonitor<lightning::sign::InMemorySigner>> { vec![] }
1173+
/// # fn example<
1174+
/// # 'a,
1175+
/// # L: lightning::util::logger::Logger,
1176+
/// # ES: lightning::sign::EntropySource,
1177+
/// # S: for <'b> lightning::routing::scoring::LockableScore<'b, ScoreLookUp = SL>,
1178+
/// # SL: lightning::routing::scoring::ScoreLookUp<ScoreParams = SP>,
1179+
/// # SP: Sized,
1180+
/// # R: lightning::io::Read,
1181+
/// # >(
1182+
/// # fee_estimator: &dyn lightning::chain::chaininterface::FeeEstimator,
1183+
/// # chain_monitor: &dyn lightning::chain::Watch<lightning::sign::InMemorySigner>,
1184+
/// # tx_broadcaster: &dyn lightning::chain::chaininterface::BroadcasterInterface,
1185+
/// # router: &lightning::routing::router::DefaultRouter<&NetworkGraph<&'a L>, &'a L, &ES, &S, SP, SL>,
1186+
/// # logger: &L,
1187+
/// # entropy_source: &ES,
1188+
/// # node_signer: &dyn lightning::sign::NodeSigner,
1189+
/// # signer_provider: &lightning::sign::DynSignerProvider,
1190+
/// # best_block: lightning::chain::BestBlock,
1191+
/// # current_timestamp: u32,
1192+
/// # mut reader: R,
1193+
/// # ) -> Result<(), lightning::ln::msgs::DecodeError> {
1194+
/// // Fresh start with no channels
1195+
/// let params = ChainParameters {
1196+
/// network: Network::Bitcoin,
1197+
/// best_block,
1198+
/// };
1199+
/// let default_config = UserConfig::default();
1200+
/// let channel_manager = ChannelManager::new(
1201+
/// fee_estimator, chain_monitor, tx_broadcaster, router, logger, entropy_source, node_signer,
1202+
/// signer_provider, default_config, params, current_timestamp
1203+
/// );
1204+
///
1205+
/// // Restart from deserialized data
1206+
/// let mut channel_monitors = read_channel_monitors();
1207+
/// let args = ChannelManagerReadArgs::new(
1208+
/// entropy_source, node_signer, signer_provider, fee_estimator, chain_monitor, tx_broadcaster,
1209+
/// router, logger, default_config, channel_monitors.iter_mut().collect()
1210+
/// );
1211+
/// let (block_hash, channel_manager) =
1212+
/// <(BlockHash, ChannelManager<_, _, _, _, _, _, _, _>)>::read(&mut reader, args)?;
1213+
///
1214+
/// // Update the ChannelManager and ChannelMonitors with the latest chain data
1215+
/// // ...
1216+
///
1217+
/// // Move the monitors to the ChannelManager's chain::Watch parameter
1218+
/// for monitor in channel_monitors {
1219+
/// chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
1220+
/// }
1221+
/// # Ok(())
1222+
/// # }
1223+
/// ```
1224+
///
11541225
/// # Persistence
11551226
///
11561227
/// Implements [`Writeable`] to write out all channel state to disk. Implies [`peer_disconnected`] for

lightning/src/sign/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,19 @@ pub trait NodeSigner {
736736
fn sign_gossip_message(&self, msg: UnsignedGossipMessage) -> Result<Signature, ()>;
737737
}
738738

739+
// Primarily needed in doctests because of https://github.com/rust-lang/rust/issues/67295
740+
/// A dynamic [`SignerProvider`] temporarily needed for doc tests.
741+
#[cfg(taproot)]
742+
#[doc(hidden)]
743+
#[deprecated(note = "Remove once taproot cfg is removed")]
744+
pub type DynSignerProvider = dyn SignerProvider<EcdsaSigner = InMemorySigner, TaprootSigner = InMemorySigner>;
745+
746+
/// A dynamic [`SignerProvider`] temporarily needed for doc tests.
747+
#[cfg(not(taproot))]
748+
#[doc(hidden)]
749+
#[deprecated(note = "Remove once taproot cfg is removed")]
750+
pub type DynSignerProvider = dyn SignerProvider<EcdsaSigner = InMemorySigner>;
751+
739752
/// A trait that can return signer instances for individual channels.
740753
pub trait SignerProvider {
741754
/// A type which implements [`WriteableEcdsaChannelSigner`] which will be returned by [`Self::derive_channel_signer`].

0 commit comments

Comments
 (0)