|
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,55 @@ pub enum PaymentPurpose {
|
68 | 69 | SpontaneousPayment(PaymentPreimage),
|
69 | 70 | }
|
70 | 71 |
|
| 72 | +#[derive(Clone, Debug, PartialEq)] |
| 73 | +/// The reason which 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 | + HolderForceClosed, |
| 89 | + /// The channel was closed after negotiating a cooperative close and we've now broadcasted |
| 90 | + /// the cooperative close transaction. Note the shutdown may have been initiated by us. |
| 91 | + //TODO: split between CounterpartyInitiated/LocallyInitiated |
| 92 | + CooperativeClosure, |
| 93 | + /// A commitment transaction was confirmed on chain, closing the channel. Most likely this |
| 94 | + /// commitment transaction came from our counterparty, but it may also have come from |
| 95 | + /// a copy of our own `ChannelMonitor`. |
| 96 | + CommitmentTxBroadcasted, |
| 97 | + /// Closure generated from processing an event, likely a HTLC forward/relay/reception. |
| 98 | + ProcessingError { |
| 99 | + /// A developer-readable error message which we generated. |
| 100 | + err: String, |
| 101 | + }, |
| 102 | + /// The `PeerManager` informed us that we've disconnected from the peer and that it is |
| 103 | + /// unlikely we'll be able to connect to the peer again in the future. This may be |
| 104 | + /// caused by incompatible features which our counterparty, or we, require. |
| 105 | + DisconnectedPeer, |
| 106 | + /// Closure generated from [`ChannelManager::read`] if the ChannelMonitor is newer than |
| 107 | + /// the ChannelManager deserialized. |
| 108 | + OutdatedChanMan |
| 109 | +} |
| 110 | + |
| 111 | +impl_writeable_tlv_based_enum_upgradable!(ClosureReason, |
| 112 | + (0, CounterpartyForceClosed) => { (1, peer_msg, required) }, |
| 113 | + (2, HolderForceClosed) => {}, |
| 114 | + (6, CommitmentTxBroadcasted) => {}, |
| 115 | + (4, CooperativeClosure) => {}, |
| 116 | + (8, ProcessingError) => { (1, err, required) }, |
| 117 | + (10, DisconnectedPeer) => {}, |
| 118 | + (12, OutdatedChanMan) => {}, |
| 119 | +); |
| 120 | + |
71 | 121 | /// An Event which you should probably take some action in response to.
|
72 | 122 | ///
|
73 | 123 | /// Note that while Writeable and Readable are implemented for Event, you probably shouldn't use
|
@@ -189,6 +239,14 @@ pub enum Event {
|
189 | 239 | /// transaction.
|
190 | 240 | claim_from_onchain_tx: bool,
|
191 | 241 | },
|
| 242 | + /// Used to indicate that a channel with the given `channel_id` is in the process of closure. |
| 243 | + ChannelClosed { |
| 244 | + /// The channel_id of the channel which has been closed. Note that on-chain transactions |
| 245 | + /// resolving the channel are likely still awaiting confirmation. |
| 246 | + channel_id: [u8; 32], |
| 247 | + /// The reason the channel was closed. |
| 248 | + reason: ClosureReason |
| 249 | + } |
192 | 250 | }
|
193 | 251 |
|
194 | 252 | impl Writeable for Event {
|
@@ -265,6 +323,13 @@ impl Writeable for Event {
|
265 | 323 | (2, claim_from_onchain_tx, required),
|
266 | 324 | });
|
267 | 325 | },
|
| 326 | + &Event::ChannelClosed { ref channel_id, ref reason } => { |
| 327 | + 9u8.write(writer)?; |
| 328 | + write_tlv_fields!(writer, { |
| 329 | + (0, channel_id, required), |
| 330 | + (2, reason, required) |
| 331 | + }); |
| 332 | + }, |
268 | 333 | }
|
269 | 334 | Ok(())
|
270 | 335 | }
|
@@ -378,6 +443,16 @@ impl MaybeReadable for Event {
|
378 | 443 | };
|
379 | 444 | f()
|
380 | 445 | },
|
| 446 | + 9u8 => { |
| 447 | + let mut channel_id = [0; 32]; |
| 448 | + let mut reason = None; |
| 449 | + read_tlv_fields!(reader, { |
| 450 | + (0, channel_id, required), |
| 451 | + (2, reason, ignorable), |
| 452 | + }); |
| 453 | + if reason.is_none() { return Ok(None); } |
| 454 | + Ok(Some(Event::ChannelClosed { channel_id, reason: reason.unwrap() })) |
| 455 | + }, |
381 | 456 | // Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
|
382 | 457 | x if x % 2 == 1 => Ok(None),
|
383 | 458 | _ => Err(msgs::DecodeError::InvalidValue)
|
|
0 commit comments