Skip to content

Commit 5a19c53

Browse files
committed
ChannelState serialization
1 parent 8f1af36 commit 5a19c53

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

lightning/src/ln/channel.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ enum HTLCUpdateAwaitingACK {
255255
/// Note that `PeerDisconnected` can be set on both `ChannelReady` and `FundingSent`.
256256
/// `ChannelReady` can then get all remaining flags set on it, until we finish shutdown, then we
257257
/// move on to `ShutdownComplete`, at which point most calls into this channel are disallowed.
258+
#[derive(Clone, Copy)]
258259
enum ChannelState {
259260
/// Implies we have (or are prepared to) send our open_channel/accept_channel message
260261
OurInitSent = 1 << 0,
@@ -318,6 +319,54 @@ const STATE_FLAGS: u32 =
318319
ChannelState::OurChannelReady as u32 |
319320
ChannelState::AwaitingRemoteRevoke as u32 |
320321
ChannelState::WaitingForBatch as u32;
322+
const CHANNEL_STATE_ENUMS: [ChannelState; 14] = [
323+
ChannelState::OurInitSent,
324+
ChannelState::TheirInitSent,
325+
ChannelState::FundingCreated,
326+
ChannelState::FundingSent,
327+
ChannelState::TheirChannelReady,
328+
ChannelState::OurChannelReady,
329+
ChannelState::ChannelReady,
330+
ChannelState::PeerDisconnected,
331+
ChannelState::MonitorUpdateInProgress,
332+
ChannelState::AwaitingRemoteRevoke,
333+
ChannelState::RemoteShutdownSent,
334+
ChannelState::LocalShutdownSent,
335+
ChannelState::ShutdownComplete,
336+
ChannelState::WaitingForBatch,
337+
];
338+
339+
struct ChannelStateInt(u32);
340+
341+
impl IntoIterator for ChannelStateInt {
342+
type Item = ChannelState;
343+
type IntoIter = Box<dyn Iterator<Item = ChannelState>>;
344+
345+
fn into_iter(self) -> Self::IntoIter {
346+
Box::new(
347+
CHANNEL_STATE_ENUMS.iter()
348+
.filter(move |&channel_state| self.0 & *channel_state as u32 != 0)
349+
.map(|channel_state| channel_state.clone())
350+
)
351+
}
352+
}
353+
354+
impl_writeable_tlv_based_enum_upgradable!(ChannelState,
355+
(0, OurInitSent) => {},
356+
(2, TheirInitSent) => {},
357+
(4, FundingCreated) => {},
358+
(6, FundingSent) => {},
359+
(8, TheirChannelReady) => {},
360+
(10, OurChannelReady) => {},
361+
(12, ChannelReady) => {},
362+
(14, PeerDisconnected) => {},
363+
(16, MonitorUpdateInProgress) => {},
364+
(18, AwaitingRemoteRevoke) => {},
365+
(20, RemoteShutdownSent) => {},
366+
(22, LocalShutdownSent) => {},
367+
(24, ShutdownComplete) => {},
368+
(26, WaitingForBatch) => {},
369+
);
321370

322371
pub const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
323372

@@ -7036,6 +7085,11 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
70367085
(31, channel_pending_event_emitted, option),
70377086
(35, pending_outbound_skimmed_fees, optional_vec),
70387087
(37, holding_cell_skimmed_fees, optional_vec),
7088+
(
7089+
if self.context.channel_state & ChannelState::WaitingForBatch as u32 != 0 { 38 } else { 39 },
7090+
ChannelStateInt(self.context.channel_state).into_iter().collect::<Vec<ChannelState>>(),
7091+
optional_vec
7092+
),
70397093
});
70407094

70417095
Ok(())
@@ -7320,6 +7374,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
73207374
let mut pending_outbound_skimmed_fees_opt: Option<Vec<Option<u64>>> = None;
73217375
let mut holding_cell_skimmed_fees_opt: Option<Vec<Option<u64>>> = None;
73227376

7377+
let mut _channel_states_even: Option<Vec<ChannelState>>;
7378+
let mut _channel_states_uneven: Option<Vec<ChannelState>>;
7379+
73237380
read_tlv_fields!(reader, {
73247381
(0, announcement_sigs, option),
73257382
(1, minimum_depth, option),
@@ -7345,6 +7402,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
73457402
(31, channel_pending_event_emitted, option),
73467403
(35, pending_outbound_skimmed_fees_opt, optional_vec),
73477404
(37, holding_cell_skimmed_fees_opt, optional_vec),
7405+
(38, _channel_states_even, optional_vec),
7406+
(39, _channel_states_uneven, optional_vec),
73487407
});
73497408

73507409
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {

0 commit comments

Comments
 (0)