Skip to content

Commit 33540ca

Browse files
committed
ChannelManager initialization docs with example
1 parent 8997d94 commit 33540ca

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,76 @@ 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+
/// # S: for <'b> lightning::routing::scoring::LockableScore<'b, ScoreLookUp = SL>,
1177+
/// # SL: lightning::routing::scoring::ScoreLookUp<ScoreParams = SP>,
1178+
/// # SP: Sized,
1179+
/// # R: lightning::io::Read,
1180+
/// # >(
1181+
/// # fee_estimator: &dyn lightning::chain::chaininterface::FeeEstimator,
1182+
/// # chain_monitor: &dyn lightning::chain::Watch<lightning::sign::InMemorySigner>,
1183+
/// # tx_broadcaster: &dyn lightning::chain::chaininterface::BroadcasterInterface,
1184+
/// # router: &lightning::routing::router::DefaultRouter<&NetworkGraph<&'a L>, &'a L, &S, SP, SL>,
1185+
/// # logger: &L,
1186+
/// # entropy_source: &dyn lightning::sign::EntropySource,
1187+
/// # node_signer: &dyn lightning::sign::NodeSigner,
1188+
/// # signer_provider: &dyn lightning::sign::SignerProvider<EcdsaSigner = lightning::sign::InMemorySigner>,
1189+
/// # best_block: lightning::chain::BestBlock,
1190+
/// # current_timestamp: u32,
1191+
/// # mut reader: R,
1192+
/// # ) -> Result<(), lightning::ln::msgs::DecodeError> {
1193+
/// // Fresh start with no channels
1194+
/// let params = ChainParameters {
1195+
/// network: Network::Bitcoin,
1196+
/// best_block,
1197+
/// };
1198+
/// let default_config = UserConfig::default();
1199+
/// let channel_manager = ChannelManager::new(
1200+
/// fee_estimator, chain_monitor, tx_broadcaster, router, logger, entropy_source, node_signer,
1201+
/// signer_provider, default_config, params, current_timestamp
1202+
/// );
1203+
///
1204+
/// // Restart from deserialized data
1205+
/// let mut channel_monitors = read_channel_monitors();
1206+
/// let args = ChannelManagerReadArgs::new(
1207+
/// entropy_source, node_signer, signer_provider, fee_estimator, chain_monitor, tx_broadcaster,
1208+
/// router, logger, default_config, channel_monitors.iter_mut().collect()
1209+
/// );
1210+
/// let (block_hash, channel_manager) =
1211+
/// <(BlockHash, ChannelManager<_, _, _, _, _, _, _, _>)>::read(&mut reader, args)?;
1212+
///
1213+
/// // Update the ChannelManager and ChannelMonitors with the latest chain data
1214+
/// // ...
1215+
///
1216+
/// // Move the monitors to the ChannelManager's chain::Watch parameter
1217+
/// for monitor in channel_monitors {
1218+
/// chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
1219+
/// }
1220+
/// # Ok(())
1221+
/// # }
1222+
/// ```
1223+
///
11541224
/// # Persistence
11551225
///
11561226
/// Implements [`Writeable`] to write out all channel state to disk. Implies [`peer_disconnected`] for

0 commit comments

Comments
 (0)