Skip to content

Commit e26ba06

Browse files
committed
Review comments
1 parent e67e8b8 commit e26ba06

File tree

3 files changed

+258
-58
lines changed

3 files changed

+258
-58
lines changed

lightning/src/ln/channel.rs

Lines changed: 172 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@ pub(super) struct ChannelContext<Signer: ChannelSigner> {
804804

805805
pub(crate) channel_transaction_parameters: ChannelTransactionParameters,
806806
funding_transaction: Option<Transaction>,
807+
funding_txid: Option<Txid>,
807808

808809
counterparty_cur_commitment_point: Option<PublicKey>,
809810
counterparty_prev_commitment_point: Option<PublicKey>,
@@ -1944,16 +1945,28 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
19441945
res
19451946
}
19461947

1947-
/// Returns transaction if there is pending funding transaction that is yet to broadcast
1948-
pub fn unbroadcasted_funding(&self) -> Option<Transaction> {
1948+
fn if_unbroadcasted_funding<F, O>(&self, f: F) -> Option<O>
1949+
where F: Fn() -> Option<O> {
19491950
if self.channel_state & ChannelState::FundingCreated as u32 != 0 ||
19501951
self.channel_state & ChannelState::WaitingForBatch as u32 != 0 {
1951-
self.funding_transaction.clone()
1952+
f()
19521953
} else {
19531954
None
19541955
}
19551956
}
19561957

1958+
/// Returns the transaction if there is a pending funding transaction that is yet to be
1959+
/// broadcast.
1960+
pub fn unbroadcasted_funding(&self) -> Option<Transaction> {
1961+
self.if_unbroadcasted_funding(|| self.funding_transaction.clone())
1962+
}
1963+
1964+
/// Returns the transaction ID if there is a pending funding transaction that is yet to be
1965+
/// broadcast.
1966+
pub fn unbroadcasted_funding_txid(&self) -> Option<Txid> {
1967+
self.if_unbroadcasted_funding(|| self.funding_txid.clone())
1968+
}
1969+
19571970
/// Gets the latest commitment transaction and any dependent transactions for relay (forcing
19581971
/// shutdown of this channel - no more calls into this Channel may be made afterwards except
19591972
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
@@ -2613,7 +2626,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
26132626

26142627
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
26152628

2616-
if non_shutdown_state == ChannelState::FundingSent as u32 {
2629+
if non_shutdown_state & !(ChannelState::WaitingForBatch as u32) == ChannelState::FundingSent as u32 {
26172630
self.context.channel_state |= ChannelState::TheirChannelReady as u32;
26182631
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
26192632
self.context.channel_state = ChannelState::ChannelReady as u32 | (self.context.channel_state & MULTI_STATE_FLAGS);
@@ -3676,6 +3689,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
36763689
// first received the funding_signed.
36773690
let mut funding_broadcastable =
36783691
if self.context.is_outbound() && self.context.channel_state & !MULTI_STATE_FLAGS >= ChannelState::FundingSent as u32 && self.context.channel_state & ChannelState::WaitingForBatch as u32 == 0 {
3692+
self.context.funding_txid.take();
36793693
self.context.funding_transaction.take()
36803694
} else { None };
36813695
// That said, if the funding transaction is already confirmed (ie we're active with a
@@ -4565,7 +4579,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
45654579
pub fn is_awaiting_initial_mon_persist(&self) -> bool {
45664580
if !self.is_awaiting_monitor_update() { return false; }
45674581
if self.context.channel_state &
4568-
!(ChannelState::TheirChannelReady as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)
4582+
!(ChannelState::TheirChannelReady as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32 | ChannelState::WaitingForBatch as u32)
45694583
== ChannelState::FundingSent as u32 {
45704584
// If we're not a 0conf channel, we'll be waiting on a monitor update with only
45714585
// FundingSent set, though our peer could have sent their channel_ready.
@@ -4645,6 +4659,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
46454659
return None;
46464660
}
46474661

4662+
// Note that we don't include ChannelState::WaitingForBatch as we don't want to send
4663+
// channel_ready until the entire batch is ready.
46484664
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
46494665
let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
46504666
self.context.channel_state |= ChannelState::OurChannelReady as u32;
@@ -5699,6 +5715,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
56995715
channel_type_features: channel_type.clone()
57005716
},
57015717
funding_transaction: None,
5718+
funding_txid: None,
57025719

57035720
counterparty_cur_commitment_point: None,
57045721
counterparty_prev_commitment_point: None,
@@ -5787,6 +5804,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
57875804
self.context.channel_state = ChannelState::FundingCreated as u32;
57885805
self.context.channel_id = funding_txo.to_channel_id();
57895806
self.context.funding_transaction = Some(funding_transaction);
5807+
self.context.funding_txid = self.context.funding_transaction.as_ref().map(|tx| tx.txid());
57905808

57915809
let channel = Channel {
57925810
context: self.context,
@@ -6333,6 +6351,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
63336351
channel_type_features: channel_type.clone()
63346352
},
63356353
funding_transaction: None,
6354+
funding_txid: None,
63366355

63376356
counterparty_cur_commitment_point: Some(msg.first_per_commitment_point),
63386357
counterparty_prev_commitment_point: None,
@@ -7179,7 +7198,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
71797198
};
71807199

71817200
let mut channel_parameters: ChannelTransactionParameters = Readable::read(reader)?;
7182-
let funding_transaction = Readable::read(reader)?;
7201+
let funding_transaction: Option<Transaction> = Readable::read(reader)?;
7202+
let funding_txid = funding_transaction.as_ref().map(|tx| tx.txid());
71837203

71847204
let counterparty_cur_commitment_point = Readable::read(reader)?;
71857205

@@ -7424,6 +7444,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
74247444

74257445
channel_transaction_parameters: channel_parameters,
74267446
funding_transaction,
7447+
funding_txid,
74277448

74287449
counterparty_cur_commitment_point,
74297450
counterparty_prev_commitment_point,
@@ -7477,7 +7498,7 @@ mod tests {
74777498
use crate::ln::PaymentHash;
74787499
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
74797500
use crate::ln::channel::InitFeatures;
7480-
use crate::ln::channel::{Channel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, commit_tx_fee_msat};
7501+
use crate::ln::channel::{Channel, ChannelState, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, commit_tx_fee_msat};
74817502
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
74827503
use crate::ln::features::ChannelTypeFeatures;
74837504
use crate::ln::msgs::{ChannelUpdate, DecodeError, UnsignedChannelUpdate, MAX_VALUE_MSAT};
@@ -8952,4 +8973,148 @@ mod tests {
89528973
);
89538974
assert!(res.is_err());
89548975
}
8976+
8977+
#[test]
8978+
fn test_waiting_for_batch() {
8979+
let feeest = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
8980+
let logger = test_utils::TestLogger::new();
8981+
let secp_ctx = Secp256k1::new();
8982+
let seed = [42; 32];
8983+
let network = Network::Testnet;
8984+
let best_block = BestBlock::from_network(network);
8985+
let chain_hash = genesis_block(network).header.block_hash();
8986+
let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
8987+
8988+
let mut config = UserConfig::default();
8989+
// Set trust_own_funding_0conf while ensuring we don't send channel_ready for a
8990+
// channel in a batch before all channels are ready.
8991+
config.channel_handshake_limits.trust_own_funding_0conf = true;
8992+
8993+
// Create a channel from node a to node b that will be part of batch funding.
8994+
let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
8995+
let mut node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(
8996+
&feeest,
8997+
&&keys_provider,
8998+
&&keys_provider,
8999+
node_b_node_id,
9000+
&channelmanager::provided_init_features(&config),
9001+
10000000,
9002+
100000,
9003+
42,
9004+
&config,
9005+
0,
9006+
42,
9007+
).unwrap();
9008+
9009+
let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
9010+
let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
9011+
let mut node_b_chan = InboundV1Channel::<EnforcingSigner>::new(
9012+
&feeest,
9013+
&&keys_provider,
9014+
&&keys_provider,
9015+
node_b_node_id,
9016+
&channelmanager::provided_channel_type_features(&config),
9017+
&channelmanager::provided_init_features(&config),
9018+
&open_channel_msg,
9019+
7,
9020+
&config,
9021+
0,
9022+
&&logger,
9023+
42,
9024+
).unwrap();
9025+
9026+
// Allow node b to send a 0conf channel_ready.
9027+
node_b_chan.set_0conf();
9028+
9029+
let accept_channel_msg = node_b_chan.accept_inbound_channel(0);
9030+
node_a_chan.accept_channel(
9031+
&accept_channel_msg,
9032+
&config.channel_handshake_limits,
9033+
&channelmanager::provided_init_features(&config),
9034+
).unwrap();
9035+
9036+
// Fund the channel with a batch funding transaction.
9037+
let output_script = node_a_chan.context.get_funding_redeemscript();
9038+
let tx = Transaction {
9039+
version: 1,
9040+
lock_time: PackedLockTime::ZERO,
9041+
input: Vec::new(),
9042+
output: vec![
9043+
TxOut {
9044+
value: 10000000, script_pubkey: output_script.clone(),
9045+
},
9046+
TxOut {
9047+
value: 10000000, script_pubkey: Builder::new().into_script(),
9048+
},
9049+
]};
9050+
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
9051+
let (mut node_a_chan, funding_created_msg) = node_a_chan.get_funding_created(
9052+
tx.clone(),
9053+
funding_outpoint,
9054+
&&logger,
9055+
).map_err(|_| ()).unwrap();
9056+
let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(
9057+
&funding_created_msg,
9058+
best_block,
9059+
&&keys_provider,
9060+
&&logger,
9061+
).map_err(|_| ()).unwrap();
9062+
let node_b_updates = node_b_chan.monitor_updating_restored(
9063+
&&logger,
9064+
&&keys_provider,
9065+
chain_hash,
9066+
&config,
9067+
0,
9068+
);
9069+
9070+
// Receive funding_signed, but the channel will be configured to hold sending channel_ready and
9071+
// broadcasting the funding transaction until the batch is ready.
9072+
let _ = node_a_chan.funding_signed(
9073+
&funding_signed_msg,
9074+
best_block,
9075+
&&keys_provider,
9076+
true,
9077+
&&logger,
9078+
).unwrap();
9079+
let node_a_updates = node_a_chan.monitor_updating_restored(
9080+
&&logger,
9081+
&&keys_provider,
9082+
chain_hash,
9083+
&config,
9084+
0,
9085+
);
9086+
// Our channel_ready shouldn't be sent yet, even with trust_own_funding_0conf set,
9087+
// as the funding transaction depends on all channels in the batch becoming ready.
9088+
assert!(node_a_updates.channel_ready.is_none());
9089+
assert!(node_a_updates.funding_broadcastable.is_none());
9090+
assert_eq!(
9091+
node_a_chan.context.channel_state,
9092+
ChannelState::FundingSent as u32 |
9093+
ChannelState::WaitingForBatch as u32,
9094+
);
9095+
9096+
// It is possible to receive a 0conf channel_ready from the remote node.
9097+
node_a_chan.channel_ready(
9098+
&node_b_updates.channel_ready.unwrap(),
9099+
&&keys_provider,
9100+
chain_hash,
9101+
&config,
9102+
&best_block,
9103+
&&logger,
9104+
).unwrap();
9105+
assert_eq!(
9106+
node_a_chan.context.channel_state,
9107+
ChannelState::FundingSent as u32 |
9108+
ChannelState::WaitingForBatch as u32 |
9109+
ChannelState::TheirChannelReady as u32,
9110+
);
9111+
9112+
// Clear the ChannelState::WaitingForBatch only when called by ChannelManager.
9113+
node_a_chan.set_batch_ready();
9114+
assert_eq!(
9115+
node_a_chan.context.channel_state,
9116+
ChannelState::FundingSent as u32 |
9117+
ChannelState::TheirChannelReady as u32,
9118+
);
9119+
}
89559120
}

0 commit comments

Comments
 (0)