|
1 | 1 | //! The top-level channel management and payment tracking stuff lives here. |
| 2 | +//! |
2 | 3 | //! The ChannelManager is the main chunk of logic implementing the lightning protocol and is |
3 | 4 | //! responsible for tracking which channels are open, HTLCs are in flight and reestablishing those |
4 | 5 | //! upon reconnect to the relevant peer(s). |
| 6 | +//! |
5 | 7 | //! It does not manage routing logic (see ln::router for that) nor does it manage constructing |
6 | 8 | //! on-chain transactions (it only monitors the chain to watch for any force-closes that might |
7 | 9 | //! imply it needs to fail HTLCs/payments/channels it manages). |
@@ -223,6 +225,7 @@ const ERR: () = "You need at least 32 bit pointers (well, usize, but we'll assum |
223 | 225 |
|
224 | 226 | /// Manager which keeps track of a number of channels and sends messages to the appropriate |
225 | 227 | /// channel, also tracking HTLC preimages and forwarding onion packets appropriately. |
| 228 | +/// |
226 | 229 | /// Implements ChannelMessageHandler, handling the multi-channel parts and passing things through |
227 | 230 | /// to individual Channels. |
228 | 231 | pub struct ChannelManager { |
@@ -285,10 +288,14 @@ pub struct ChannelDetails { |
285 | 288 | } |
286 | 289 |
|
287 | 290 | impl ChannelManager { |
288 | | - /// Constructs a new ChannelManager to hold several channels and route between them. This is |
289 | | - /// the main "logic hub" for all channel-related actions, and implements ChannelMessageHandler. |
| 291 | + /// Constructs a new ChannelManager to hold several channels and route between them. |
| 292 | + /// |
| 293 | + /// This is the main "logic hub" for all channel-related actions, and implements |
| 294 | + /// ChannelMessageHandler. |
| 295 | + /// |
290 | 296 | /// fee_proportional_millionths is an optional fee to charge any payments routed through us. |
291 | 297 | /// Non-proportional fees are fixed according to our risk using the provided fee estimator. |
| 298 | + /// |
292 | 299 | /// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`! |
293 | 300 | pub fn new(our_network_key: SecretKey, fee_proportional_millionths: u32, announce_channels_publicly: bool, network: Network, feeest: Arc<FeeEstimator>, monitor: Arc<ManyChannelMonitor>, chain_monitor: Arc<ChainWatchInterface>, tx_broadcaster: Arc<BroadcasterInterface>, logger: Arc<Logger>) -> Result<Arc<ChannelManager>, secp256k1::Error> { |
294 | 301 | let secp_ctx = Secp256k1::new(); |
@@ -324,12 +331,15 @@ impl ChannelManager { |
324 | 331 | } |
325 | 332 |
|
326 | 333 | /// Creates a new outbound channel to the given remote node and with the given value. |
| 334 | + /// |
327 | 335 | /// user_id will be provided back as user_channel_id in FundingGenerationReady and |
328 | 336 | /// FundingBroadcastSafe events to allow tracking of which events correspond with which |
329 | 337 | /// create_channel call. Note that user_channel_id defaults to 0 for inbound channels, so you |
330 | 338 | /// may wish to avoid using 0 for user_id here. |
| 339 | + /// |
331 | 340 | /// If successful, will generate a SendOpenChannel event, so you should probably poll |
332 | 341 | /// PeerManager::process_events afterwards. |
| 342 | + /// |
333 | 343 | /// Raises APIError::APIMisuseError when channel_value_satoshis > 2**24 or push_msat being greater than channel_value_satoshis * 1k |
334 | 344 | pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_id: u64) -> Result<(), APIError> { |
335 | 345 | let chan_keys = if cfg!(feature = "fuzztarget") { |
@@ -407,6 +417,7 @@ impl ChannelManager { |
407 | 417 | /// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs |
408 | 418 | /// will be accepted on the given channel, and after additional timeout/the closing of all |
409 | 419 | /// pending HTLCs, the channel will be closed on chain. |
| 420 | + /// |
410 | 421 | /// May generate a SendShutdown event on success, which should be relayed. |
411 | 422 | pub fn close_channel(&self, channel_id: &[u8; 32]) -> Result<(), HandleError> { |
412 | 423 | let (mut res, node_id, chan_option) = { |
@@ -947,16 +958,19 @@ impl ChannelManager { |
947 | 958 | } |
948 | 959 |
|
949 | 960 | /// Sends a payment along a given route. |
| 961 | + /// |
950 | 962 | /// Value parameters are provided via the last hop in route, see documentation for RouteHop |
951 | 963 | /// fields for more info. |
| 964 | + /// |
952 | 965 | /// Note that if the payment_hash already exists elsewhere (eg you're sending a duplicative |
953 | 966 | /// payment), we don't do anything to stop you! We always try to ensure that if the provided |
954 | 967 | /// next hop knows the preimage to payment_hash they can claim an additional amount as |
955 | 968 | /// specified in the last hop in the route! Thus, you should probably do your own |
956 | 969 | /// payment_preimage tracking (which you should already be doing as they represent "proof of |
957 | 970 | /// payment") and prevent double-sends yourself. |
958 | | - /// See-also docs on Channel::send_htlc_and_commit. |
| 971 | + /// |
959 | 972 | /// May generate a SendHTLCs event on success, which should be relayed. |
| 973 | + /// |
960 | 974 | /// Raises APIError::RoutError when invalid route or forward parameter |
961 | 975 | /// (cltv_delta, fee, node public key) is specified |
962 | 976 | pub fn send_payment(&self, route: Route, payment_hash: [u8; 32]) -> Result<(), APIError> { |
@@ -1033,7 +1047,9 @@ impl ChannelManager { |
1033 | 1047 | } |
1034 | 1048 |
|
1035 | 1049 | /// Call this upon creation of a funding transaction for the given channel. |
| 1050 | + /// |
1036 | 1051 | /// Panics if a funding transaction has already been provided for this channel. |
| 1052 | + /// |
1037 | 1053 | /// May panic if the funding_txo is duplicative with some other channel (note that this should |
1038 | 1054 | /// be trivially prevented by using unique funding transaction keys per-channel). |
1039 | 1055 | pub fn funding_transaction_generated(&self, temporary_channel_id: &[u8; 32], funding_txo: OutPoint) { |
@@ -1107,6 +1123,7 @@ impl ChannelManager { |
1107 | 1123 | } |
1108 | 1124 |
|
1109 | 1125 | /// Processes HTLCs which are pending waiting on random forward delay. |
| 1126 | + /// |
1110 | 1127 | /// Should only really ever be called in response to an PendingHTLCsForwardable event. |
1111 | 1128 | /// Will likely generate further events. |
1112 | 1129 | pub fn process_pending_htlc_forwards(&self) { |
@@ -1323,6 +1340,7 @@ impl ChannelManager { |
1323 | 1340 | /// Provides a payment preimage in response to a PaymentReceived event, returning true and |
1324 | 1341 | /// generating message events for the net layer to claim the payment, if possible. Thus, you |
1325 | 1342 | /// should probably kick the net layer to go send messages if this returns true! |
| 1343 | + /// |
1326 | 1344 | /// May panic if called except in response to a PaymentReceived event. |
1327 | 1345 | pub fn claim_funds(&self, payment_preimage: [u8; 32]) -> bool { |
1328 | 1346 | let mut sha = Sha256::new(); |
|
0 commit comments