|
16 | 16 |
|
17 | 17 | use chain::keysinterface::SpendableOutputDescriptor;
|
18 | 18 | use ln::msgs;
|
| 19 | +use ln::msgs::DecodeError; |
19 | 20 | use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
|
20 | 21 | use routing::network_graph::NetworkUpdate;
|
21 | 22 | use util::ser::{Writeable, Writer, MaybeReadable, Readable, VecReadWrapper, VecWriteWrapper};
|
@@ -68,6 +69,60 @@ pub enum PaymentPurpose {
|
68 | 69 | SpontaneousPayment(PaymentPreimage),
|
69 | 70 | }
|
70 | 71 |
|
| 72 | +#[derive(Clone, Debug, PartialEq)] |
| 73 | +/// The reason the channel was closed. See individual variants more details. |
| 74 | +pub enum ClosureReason { |
| 75 | + /// Closure generated from receiving a peer error message. |
| 76 | + /// |
| 77 | + /// Our counterparty may have broadcasted their latest commitment state, and we have |
| 78 | + /// as well. |
| 79 | + CounterpartyForceClosed { |
| 80 | + /// The error which the peer sent us. |
| 81 | + /// |
| 82 | + /// The string should be sanitized before it is used (e.g emitted to logs |
| 83 | + /// or printed to stdout). Otherwise, a well crafted error message may exploit |
| 84 | + /// a security vulnerability in the terminal emulator or the logging subsystem. |
| 85 | + peer_msg: String, |
| 86 | + }, |
| 87 | + /// Closure generated from [`ChannelManager::force_close_channel`], called by the user. |
| 88 | + /// |
| 89 | + /// [`ChannelManager::force_close_channel`]: ln::channelmanager::ChannelManager::force_close_channel. |
| 90 | + HolderForceClosed, |
| 91 | + /// The channel was closed after negotiating a cooperative close and we've now broadcasted |
| 92 | + /// the cooperative close transaction. Note the shutdown may have been initiated by us. |
| 93 | + //TODO: split between CounterpartyInitiated/LocallyInitiated |
| 94 | + CooperativeClosure, |
| 95 | + /// A commitment transaction was confirmed on chain, closing the channel. Most likely this |
| 96 | + /// commitment transaction came from our counterparty, but it may also have come from |
| 97 | + /// a copy of our own `ChannelMonitor`. |
| 98 | + CommitmentTxConfirmed, |
| 99 | + /// Closure generated from processing an event, likely a HTLC forward/relay/reception. |
| 100 | + ProcessingError { |
| 101 | + /// A developer-readable error message which we generated. |
| 102 | + err: String, |
| 103 | + }, |
| 104 | + /// The `PeerManager` informed us that we've disconnected from the peer. We close channels |
| 105 | + /// if the `PeerManager` informed us that it is unlikely we'll be able to connect to the |
| 106 | + /// peer again in the future or if the peer disconnected before we finished negotiating |
| 107 | + /// the channel open. The first case may be caused by incompatible features which our |
| 108 | + /// counterparty, or we, require. |
| 109 | + //TODO: split between PeerUnconnectable/PeerDisconnected ? |
| 110 | + DisconnectedPeer, |
| 111 | + /// Closure generated from [`ChannelManager::read`] if the ChannelMonitor is newer than |
| 112 | + /// the ChannelManager deserialized. |
| 113 | + OutdatedChannelManager |
| 114 | +} |
| 115 | + |
| 116 | +impl_writeable_tlv_based_enum_upgradable!(ClosureReason, |
| 117 | + (0, CounterpartyForceClosed) => { (1, peer_msg, required) }, |
| 118 | + (2, HolderForceClosed) => {}, |
| 119 | + (6, CommitmentTxConfirmed) => {}, |
| 120 | + (4, CooperativeClosure) => {}, |
| 121 | + (8, ProcessingError) => { (1, err, required) }, |
| 122 | + (10, DisconnectedPeer) => {}, |
| 123 | + (12, OutdatedChannelManager) => {}, |
| 124 | +); |
| 125 | + |
71 | 126 | /// An Event which you should probably take some action in response to.
|
72 | 127 | ///
|
73 | 128 | /// Note that while Writeable and Readable are implemented for Event, you probably shouldn't use
|
@@ -189,6 +244,14 @@ pub enum Event {
|
189 | 244 | /// transaction.
|
190 | 245 | claim_from_onchain_tx: bool,
|
191 | 246 | },
|
| 247 | + /// Used to indicate that a channel with the given `channel_id` is in the process of closure. |
| 248 | + ChannelClosed { |
| 249 | + /// The channel_id of the channel which has been closed. Note that on-chain transactions |
| 250 | + /// resolving the channel are likely still awaiting confirmation. |
| 251 | + channel_id: [u8; 32], |
| 252 | + /// The reason the channel was closed. |
| 253 | + reason: ClosureReason |
| 254 | + } |
192 | 255 | }
|
193 | 256 |
|
194 | 257 | impl Writeable for Event {
|
@@ -265,6 +328,13 @@ impl Writeable for Event {
|
265 | 328 | (2, claim_from_onchain_tx, required),
|
266 | 329 | });
|
267 | 330 | },
|
| 331 | + &Event::ChannelClosed { ref channel_id, ref reason } => { |
| 332 | + 9u8.write(writer)?; |
| 333 | + write_tlv_fields!(writer, { |
| 334 | + (0, channel_id, required), |
| 335 | + (2, reason, required) |
| 336 | + }); |
| 337 | + }, |
268 | 338 | }
|
269 | 339 | Ok(())
|
270 | 340 | }
|
@@ -378,6 +448,16 @@ impl MaybeReadable for Event {
|
378 | 448 | };
|
379 | 449 | f()
|
380 | 450 | },
|
| 451 | + 9u8 => { |
| 452 | + let mut channel_id = [0; 32]; |
| 453 | + let mut reason = None; |
| 454 | + read_tlv_fields!(reader, { |
| 455 | + (0, channel_id, required), |
| 456 | + (2, reason, ignorable), |
| 457 | + }); |
| 458 | + if reason.is_none() { return Ok(None); } |
| 459 | + Ok(Some(Event::ChannelClosed { channel_id, reason: reason.unwrap() })) |
| 460 | + }, |
381 | 461 | // Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
|
382 | 462 | x if x % 2 == 1 => Ok(None),
|
383 | 463 | _ => Err(msgs::DecodeError::InvalidValue)
|
|
0 commit comments