Skip to content

Commit 3baad70

Browse files
committed
Review comments
1 parent 1762459 commit 3baad70

File tree

4 files changed

+119
-162
lines changed

4 files changed

+119
-162
lines changed

lightning/src/ln/channel.rs

Lines changed: 28 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ 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)]
259258
enum ChannelState {
260259
/// Implies we have (or are prepared to) send our open_channel/accept_channel message
261260
OurInitSent = 1 << 0,
@@ -319,54 +318,6 @@ const STATE_FLAGS: u32 =
319318
ChannelState::OurChannelReady as u32 |
320319
ChannelState::AwaitingRemoteRevoke as u32 |
321320
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-
);
370321

371322
pub const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
372323

@@ -854,6 +805,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
854805
pub(crate) channel_transaction_parameters: ChannelTransactionParameters,
855806
funding_transaction: Option<Transaction>,
856807
funding_txid: Option<Txid>,
808+
is_batch_funding: Option<bool>,
857809

858810
counterparty_cur_commitment_point: Option<PublicKey>,
859811
counterparty_prev_commitment_point: Option<PublicKey>,
@@ -1990,7 +1942,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
19901942
fn if_unbroadcasted_funding<F, O>(&self, f: F) -> Option<O>
19911943
where F: Fn() -> Option<O> {
19921944
if self.channel_state & ChannelState::FundingCreated as u32 != 0 ||
1993-
self.channel_state & ChannelState::WaitingForBatch as u32 != 0 {
1945+
self.channel_state & ChannelState::WaitingForBatch as u32 != 0 {
19941946
f()
19951947
} else {
19961948
None
@@ -2009,6 +1961,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
20091961
self.if_unbroadcasted_funding(|| self.funding_txid.clone())
20101962
}
20111963

1964+
/// Returns whether the channel is funded in a batch.
1965+
pub fn is_batch_funding(&self) -> bool {
1966+
self.is_batch_funding.unwrap_or(false)
1967+
}
1968+
20121969
/// Gets the latest commitment transaction and any dependent transactions for relay (forcing
20131970
/// shutdown of this channel - no more calls into this Channel may be made afterwards except
20141971
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
@@ -2552,7 +2509,7 @@ impl<SP: Deref> Channel<SP> where
25522509
/// Handles a funding_signed message from the remote end.
25532510
/// If this call is successful, broadcast the funding transaction (and not before!)
25542511
pub fn funding_signed<L: Deref>(
2555-
&mut self, msg: &msgs::FundingSigned, best_block: BestBlock, signer_provider: &SP, is_batch_funding: bool, logger: &L
2512+
&mut self, msg: &msgs::FundingSigned, best_block: BestBlock, signer_provider: &SP, logger: &L
25562513
) -> Result<ChannelMonitor<<SP::Target as SignerProvider>::Signer>, ChannelError>
25572514
where
25582515
L::Target: Logger
@@ -2627,7 +2584,7 @@ impl<SP: Deref> Channel<SP> where
26272584
counterparty_initial_commitment_tx.to_countersignatory_value_sat(), logger);
26282585

26292586
assert_eq!(self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32), 0); // We have no had any monitor(s) yet to fail update!
2630-
if is_batch_funding {
2587+
if self.context.is_batch_funding() {
26312588
self.context.channel_state = ChannelState::FundingSent as u32 | ChannelState::WaitingForBatch as u32;
26322589
} else {
26332590
self.context.channel_state = ChannelState::FundingSent as u32;
@@ -5830,6 +5787,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
58305787
},
58315788
funding_transaction: None,
58325789
funding_txid: None,
5790+
is_batch_funding: None,
58335791

58345792
counterparty_cur_commitment_point: None,
58355793
counterparty_prev_commitment_point: None,
@@ -5890,7 +5848,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
58905848
/// Note that channel_id changes during this call!
58915849
/// Do NOT broadcast the funding transaction until after a successful funding_signed call!
58925850
/// If an Err is returned, it is a ChannelError::Close.
5893-
pub fn get_funding_created<L: Deref>(mut self, funding_transaction: Transaction, funding_txo: OutPoint, logger: &L)
5851+
pub fn get_funding_created<L: Deref>(mut self, funding_transaction: Transaction, funding_txo: OutPoint, is_batch_funding: bool, logger: &L)
58945852
-> Result<(Channel<SP>, msgs::FundingCreated), (Self, ChannelError)> where L::Target: Logger {
58955853
if !self.context.is_outbound() {
58965854
panic!("Tried to create outbound funding_created message on an inbound channel!");
@@ -5924,6 +5882,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
59245882
self.context.channel_id = funding_txo.to_channel_id();
59255883
self.context.funding_transaction = Some(funding_transaction);
59265884
self.context.funding_txid = self.context.funding_transaction.as_ref().map(|tx| tx.txid());
5885+
self.context.is_batch_funding = Some(is_batch_funding);
59275886

59285887
let channel = Channel {
59295888
context: self.context,
@@ -6474,6 +6433,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
64746433
},
64756434
funding_transaction: None,
64766435
funding_txid: None,
6436+
is_batch_funding: None,
64776437

64786438
counterparty_cur_commitment_point: Some(msg.first_per_commitment_point),
64796439
counterparty_prev_commitment_point: None,
@@ -7090,9 +7050,9 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
70907050
(35, pending_outbound_skimmed_fees, optional_vec),
70917051
(37, holding_cell_skimmed_fees, optional_vec),
70927052
(
7093-
if self.context.channel_state & ChannelState::WaitingForBatch as u32 != 0 { 38 } else { 39 },
7094-
ChannelStateInt(self.context.channel_state).into_iter().collect::<Vec<ChannelState>>(),
7095-
optional_vec
7053+
if self.context.is_batch_funding() { 38 } else { 39 },
7054+
self.context.is_batch_funding,
7055+
option
70967056
),
70977057
});
70987058

@@ -7378,8 +7338,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
73787338
let mut pending_outbound_skimmed_fees_opt: Option<Vec<Option<u64>>> = None;
73797339
let mut holding_cell_skimmed_fees_opt: Option<Vec<Option<u64>>> = None;
73807340

7381-
let mut _channel_states_even: Option<Vec<ChannelState>>;
7382-
let mut _channel_states_uneven: Option<Vec<ChannelState>>;
7341+
let mut is_batch_funding_even: Option<bool> = None;
7342+
let mut is_batch_funding_uneven: Option<bool> = None;
73837343

73847344
read_tlv_fields!(reader, {
73857345
(0, announcement_sigs, option),
@@ -7406,8 +7366,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
74067366
(31, channel_pending_event_emitted, option),
74077367
(35, pending_outbound_skimmed_fees_opt, optional_vec),
74087368
(37, holding_cell_skimmed_fees_opt, optional_vec),
7409-
(38, _channel_states_even, optional_vec),
7410-
(39, _channel_states_uneven, optional_vec),
7369+
(38, is_batch_funding_even, option),
7370+
(39, is_batch_funding_uneven, option),
74117371
});
74127372

74137373
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
@@ -7566,6 +7526,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
75667526
channel_transaction_parameters: channel_parameters,
75677527
funding_transaction,
75687528
funding_txid,
7529+
is_batch_funding: is_batch_funding_even.or(is_batch_funding_uneven),
75697530

75707531
counterparty_cur_commitment_point,
75717532
counterparty_prev_commitment_point,
@@ -7798,11 +7759,11 @@ mod tests {
77987759
value: 10000000, script_pubkey: output_script.clone(),
77997760
}]};
78007761
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
7801-
let (mut node_a_chan, funding_created_msg) = node_a_chan.get_funding_created(tx.clone(), funding_outpoint, &&logger).map_err(|_| ()).unwrap();
7762+
let (mut node_a_chan, funding_created_msg) = node_a_chan.get_funding_created(tx.clone(), funding_outpoint, false, &&logger).map_err(|_| ()).unwrap();
78027763
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
78037764

78047765
// Node B --> Node A: funding signed
7805-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, false, &&logger).unwrap();
7766+
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
78067767

78077768
// Put some inbound and outbound HTLCs in A's channel.
78087769
let htlc_amount_msat = 11_092_000; // put an amount below A's effective dust limit but above B's.
@@ -7925,11 +7886,11 @@ mod tests {
79257886
value: 10000000, script_pubkey: output_script.clone(),
79267887
}]};
79277888
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
7928-
let (mut node_a_chan, funding_created_msg) = node_a_chan.get_funding_created(tx.clone(), funding_outpoint, &&logger).map_err(|_| ()).unwrap();
7889+
let (mut node_a_chan, funding_created_msg) = node_a_chan.get_funding_created(tx.clone(), funding_outpoint, false, &&logger).map_err(|_| ()).unwrap();
79297890
let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
79307891

79317892
// Node B --> Node A: funding signed
7932-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, false, &&logger).unwrap();
7893+
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
79337894

79347895
// Now disconnect the two nodes and check that the commitment point in
79357896
// Node B's channel_reestablish message is sane.
@@ -8113,11 +8074,11 @@ mod tests {
81138074
value: 10000000, script_pubkey: output_script.clone(),
81148075
}]};
81158076
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
8116-
let (mut node_a_chan, funding_created_msg) = node_a_chan.get_funding_created(tx.clone(), funding_outpoint, &&logger).map_err(|_| ()).unwrap();
8077+
let (mut node_a_chan, funding_created_msg) = node_a_chan.get_funding_created(tx.clone(), funding_outpoint, false, &&logger).map_err(|_| ()).unwrap();
81178078
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
81188079

81198080
// Node B --> Node A: funding signed
8120-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, false, &&logger).unwrap();
8081+
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
81218082

81228083
// Make sure that receiving a channel update will update the Channel as expected.
81238084
let update = ChannelUpdate {
@@ -9168,6 +9129,7 @@ mod tests {
91689129
let (mut node_a_chan, funding_created_msg) = node_a_chan.get_funding_created(
91699130
tx.clone(),
91709131
funding_outpoint,
9132+
true,
91719133
&&logger,
91729134
).map_err(|_| ()).unwrap();
91739135
let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(
@@ -9190,7 +9152,6 @@ mod tests {
91909152
&funding_signed_msg,
91919153
best_block,
91929154
&&keys_provider,
9193-
true,
91949155
&&logger,
91959156
).unwrap();
91969157
let node_a_updates = node_a_chan.monitor_updating_restored(

0 commit comments

Comments
 (0)