Skip to content

Commit 1b7c7cd

Browse files
committed
ChannelManager initialization docs with example
1 parent cd2367f commit 1b7c7cd

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,78 @@ where
11061106
/// This division of off-chain management and on-chain enforcement is what allows for interesting
11071107
/// node setups. See the [`chain`] module for further details.
11081108
///
1109+
/// # Initialization
1110+
///
1111+
/// Use [`ChannelManager::new`] with the most recent [`BlockHash`] when creating a fresh instance.
1112+
/// Otherwise, if restarting, construct [`ChannelManagerReadArgs`] with the necessary parameters and
1113+
/// references to any deserialized [`ChannelMonitor`]s that were previously persisted. Use this to
1114+
/// deserialize the [`ChannelManager`] and feed it any new chain data since it was last online, as
1115+
/// detailed in the [`ChannelManagerReadArgs`] documentation.
1116+
///
1117+
/// ```
1118+
/// use bitcoin::BlockHash;
1119+
/// use bitcoin::network::constants::Network;
1120+
/// use lightning::chain::BestBlock;
1121+
/// # use lightning::chain::channelmonitor::ChannelMonitor;
1122+
/// use lightning::ln::channelmanager::{ChainParameters, ChannelManager, ChannelManagerReadArgs};
1123+
/// # use lightning::routing::gossip::NetworkGraph;
1124+
/// use lightning::util::config::UserConfig;
1125+
/// use lightning::util::ser::ReadableArgs;
1126+
///
1127+
/// # fn read_channel_monitors() -> Vec<ChannelMonitor<lightning::sign::InMemorySigner>> { vec![] }
1128+
/// # fn example<
1129+
/// # 'a,
1130+
/// # L: lightning::util::logger::Logger,
1131+
/// # S: for <'b> lightning::routing::scoring::LockableScore<'b, ScoreLookUp = SL>,
1132+
/// # SL: lightning::routing::scoring::ScoreLookUp<ScoreParams = SP>,
1133+
/// # SP: Sized,
1134+
/// # R: lightning::io::Read,
1135+
/// # >(
1136+
/// # fee_estimator: &dyn lightning::chain::chaininterface::FeeEstimator,
1137+
/// # chain_monitor: &dyn lightning::chain::Watch<lightning::sign::InMemorySigner>,
1138+
/// # tx_broadcaster: &dyn lightning::chain::chaininterface::BroadcasterInterface,
1139+
/// # router: &lightning::routing::router::DefaultRouter<&NetworkGraph<&'a L>, &'a L, &S, SP, SL>,
1140+
/// # logger: &L,
1141+
/// # entropy_source: &dyn lightning::sign::EntropySource,
1142+
/// # node_signer: &dyn lightning::sign::NodeSigner,
1143+
/// # signer_provider: &dyn lightning::sign::SignerProvider<EcdsaSigner = lightning::sign::InMemorySigner>,
1144+
/// # best_block: lightning::chain::BestBlock,
1145+
/// # current_timestamp: u32,
1146+
/// # mut reader: R,
1147+
/// # ) -> Result<(), lightning::ln::msgs::DecodeError> {
1148+
/// // Fresh start with no channels
1149+
/// let params = ChainParameters {
1150+
/// network: Network::Bitcoin,
1151+
/// best_block,
1152+
/// };
1153+
/// let default_config = UserConfig::default();
1154+
/// let channel_manager = ChannelManager::new(
1155+
/// fee_estimator, chain_monitor, tx_broadcaster, router, logger, entropy_source, node_signer,
1156+
/// signer_provider, default_config, params, current_timestamp
1157+
/// );
1158+
///
1159+
/// // Restart from deserialized data
1160+
/// let mut channel_monitors = read_channel_monitors();
1161+
/// let args = ChannelManagerReadArgs::new(
1162+
/// entropy_source, node_signer, signer_provider, fee_estimator, chain_monitor, tx_broadcaster,
1163+
/// router, logger, default_config, channel_monitors.iter_mut().collect()
1164+
/// );
1165+
/// let (block_hash, channel_manager) =
1166+
/// <(BlockHash, ChannelManager<_, _, _, _, _, _, _, _>)>::read(&mut reader, args)?;
1167+
///
1168+
/// // Update the ChannelManager and ChannelMonitors with the latest chain data
1169+
/// // ...
1170+
///
1171+
/// // Move the monitors to the ChannelManager's chain::Watch parameter
1172+
/// for monitor in channel_monitors {
1173+
/// chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
1174+
/// }
1175+
/// # Ok(())
1176+
/// # }
1177+
/// ```
1178+
///
1179+
/// TODO: Consider re-writing ChannelManagerReadArgs docs and moving here.
1180+
///
11091181
/// # Persistence
11101182
///
11111183
/// Implements [`Writeable`] to write out all channel state to disk. Implies [`peer_disconnected`] for

0 commit comments

Comments
 (0)