@@ -35,6 +35,7 @@ use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
35
35
use chain:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdate , ChannelMonitorUpdateStep , LATENCY_GRACE_PERIOD_BLOCKS } ;
36
36
use chain:: transaction:: { OutPoint , TransactionData } ;
37
37
use chain:: keysinterface:: { Sign , KeysInterface } ;
38
+ use util:: events:: ClosureReason ;
38
39
use util:: ser:: { Readable , ReadableArgs , Writeable , Writer , VecWriter } ;
39
40
use util:: logger:: Logger ;
40
41
use util:: errors:: APIError ;
@@ -378,6 +379,11 @@ pub const FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE: u64 = 2;
378
379
#[ cfg( not( fuzzing) ) ]
379
380
const FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE : u64 = 2 ;
380
381
382
+ /// If we fail to see a funding transaction confirmed on-chain within this many blocks after the
383
+ /// channel creation on an inbound channel, we simply force-close and move on.
384
+ /// This constant is the one suggested in BOLT 2.
385
+ pub ( crate ) const FUNDING_CONF_DEADLINE_BLOCKS : u32 = 2016 ;
386
+
381
387
// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
382
388
// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
383
389
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
@@ -475,6 +481,10 @@ pub(super) struct Channel<Signer: Sign> {
475
481
funding_tx_confirmed_in : Option < BlockHash > ,
476
482
funding_tx_confirmation_height : u32 ,
477
483
short_channel_id : Option < u64 > ,
484
+ /// Either the height at which this channel was created or the height at which it was last
485
+ /// serialized if it was serialized by versions prior to 0.0.103.
486
+ /// We use this to close if funding is never broadcasted.
487
+ channel_creation_height : u32 ,
478
488
479
489
counterparty_dust_limit_satoshis : u64 ,
480
490
#[ cfg( test) ]
@@ -646,7 +656,10 @@ impl<Signer: Sign> Channel<Signer> {
646
656
}
647
657
648
658
// Constructors:
649
- pub fn new_outbound < K : Deref , F : Deref > ( fee_estimator : & F , keys_provider : & K , counterparty_node_id : PublicKey , their_features : & InitFeatures , channel_value_satoshis : u64 , push_msat : u64 , user_id : u64 , config : & UserConfig ) -> Result < Channel < Signer > , APIError >
659
+ pub fn new_outbound < K : Deref , F : Deref > (
660
+ fee_estimator : & F , keys_provider : & K , counterparty_node_id : PublicKey , their_features : & InitFeatures ,
661
+ channel_value_satoshis : u64 , push_msat : u64 , user_id : u64 , config : & UserConfig , current_chain_height : u32
662
+ ) -> Result < Channel < Signer > , APIError >
650
663
where K :: Target : KeysInterface < Signer = Signer > ,
651
664
F :: Target : FeeEstimator ,
652
665
{
@@ -734,6 +747,7 @@ impl<Signer: Sign> Channel<Signer> {
734
747
funding_tx_confirmed_in : None ,
735
748
funding_tx_confirmation_height : 0 ,
736
749
short_channel_id : None ,
750
+ channel_creation_height : current_chain_height,
737
751
738
752
feerate_per_kw : feerate,
739
753
counterparty_dust_limit_satoshis : 0 ,
@@ -807,7 +821,10 @@ impl<Signer: Sign> Channel<Signer> {
807
821
808
822
/// Creates a new channel from a remote sides' request for one.
809
823
/// Assumes chain_hash has already been checked and corresponds with what we expect!
810
- pub fn new_from_req < K : Deref , F : Deref > ( fee_estimator : & F , keys_provider : & K , counterparty_node_id : PublicKey , their_features : & InitFeatures , msg : & msgs:: OpenChannel , user_id : u64 , config : & UserConfig ) -> Result < Channel < Signer > , ChannelError >
824
+ pub fn new_from_req < K : Deref , F : Deref > (
825
+ fee_estimator : & F , keys_provider : & K , counterparty_node_id : PublicKey , their_features : & InitFeatures ,
826
+ msg : & msgs:: OpenChannel , user_id : u64 , config : & UserConfig , current_chain_height : u32
827
+ ) -> Result < Channel < Signer > , ChannelError >
811
828
where K :: Target : KeysInterface < Signer = Signer > ,
812
829
F :: Target : FeeEstimator
813
830
{
@@ -1020,6 +1037,7 @@ impl<Signer: Sign> Channel<Signer> {
1020
1037
funding_tx_confirmed_in : None ,
1021
1038
funding_tx_confirmation_height : 0 ,
1022
1039
short_channel_id : None ,
1040
+ channel_creation_height : current_chain_height,
1023
1041
1024
1042
feerate_per_kw : msg. feerate_per_kw ,
1025
1043
channel_value_satoshis : msg. funding_satoshis ,
@@ -4117,7 +4135,7 @@ impl<Signer: Sign> Channel<Signer> {
4117
4135
/// In the first case, we store the confirmation height and calculating the short channel id.
4118
4136
/// In the second, we simply return an Err indicating we need to be force-closed now.
4119
4137
pub fn transactions_confirmed < L : Deref > ( & mut self , block_hash : & BlockHash , height : u32 , txdata : & TransactionData , logger : & L )
4120
- -> Result < Option < msgs:: FundingLocked > , msgs :: ErrorMessage > where L :: Target : Logger {
4138
+ -> Result < Option < msgs:: FundingLocked > , ClosureReason > where L :: Target : Logger {
4121
4139
let non_shutdown_state = self . channel_state & ( !MULTI_STATE_FLAGS ) ;
4122
4140
for & ( index_in_block, tx) in txdata. iter ( ) {
4123
4141
if let Some ( funding_txo) = self . get_funding_txo ( ) {
@@ -4138,10 +4156,8 @@ impl<Signer: Sign> Channel<Signer> {
4138
4156
panic ! ( "Client called ChannelManager::funding_transaction_generated with bogus transaction!" ) ;
4139
4157
}
4140
4158
self . update_time_counter += 1 ;
4141
- return Err ( msgs:: ErrorMessage {
4142
- channel_id : self . channel_id ( ) ,
4143
- data : "funding tx had wrong script/value or output index" . to_owned ( )
4144
- } ) ;
4159
+ let err_reason = "funding tx had wrong script/value or output index" ;
4160
+ return Err ( ClosureReason :: ProcessingError { err : err_reason. to_owned ( ) } ) ;
4145
4161
} else {
4146
4162
if self . is_outbound ( ) {
4147
4163
for input in tx. input . iter ( ) {
@@ -4172,10 +4188,7 @@ impl<Signer: Sign> Channel<Signer> {
4172
4188
for inp in tx. input . iter ( ) {
4173
4189
if inp. previous_output == funding_txo. into_bitcoin_outpoint ( ) {
4174
4190
log_info ! ( logger, "Detected channel-closing tx {} spending {}:{}, closing channel {}" , tx. txid( ) , inp. previous_output. txid, inp. previous_output. vout, log_bytes!( self . channel_id( ) ) ) ;
4175
- return Err ( msgs:: ErrorMessage {
4176
- channel_id : self . channel_id ( ) ,
4177
- data : "Commitment or closing transaction was confirmed on chain." . to_owned ( )
4178
- } ) ;
4191
+ return Err ( ClosureReason :: CommitmentTxConfirmed ) ;
4179
4192
}
4180
4193
}
4181
4194
}
@@ -4195,7 +4208,7 @@ impl<Signer: Sign> Channel<Signer> {
4195
4208
/// May return some HTLCs (and their payment_hash) which have timed out and should be failed
4196
4209
/// back.
4197
4210
pub fn best_block_updated < L : Deref > ( & mut self , height : u32 , highest_header_time : u32 , logger : & L )
4198
- -> Result < ( Option < msgs:: FundingLocked > , Vec < ( HTLCSource , PaymentHash ) > ) , msgs :: ErrorMessage > where L :: Target : Logger {
4211
+ -> Result < ( Option < msgs:: FundingLocked > , Vec < ( HTLCSource , PaymentHash ) > ) , ClosureReason > where L :: Target : Logger {
4199
4212
let mut timed_out_htlcs = Vec :: new ( ) ;
4200
4213
// This mirrors the check in ChannelManager::decode_update_add_htlc_onion, refusing to
4201
4214
// forward an HTLC when our counterparty should almost certainly just fail it for expiring
@@ -4236,11 +4249,17 @@ impl<Signer: Sign> Channel<Signer> {
4236
4249
// close the channel and hope we can get the latest state on chain (because presumably
4237
4250
// the funding transaction is at least still in the mempool of most nodes).
4238
4251
if funding_tx_confirmations < self . minimum_depth . unwrap ( ) as i64 / 2 {
4239
- return Err ( msgs:: ErrorMessage {
4240
- channel_id : self . channel_id ( ) ,
4241
- data : format ! ( "Funding transaction was un-confirmed. Locked at {} confs, now have {} confs." , self . minimum_depth. unwrap( ) , funding_tx_confirmations) ,
4242
- } ) ;
4252
+ let err_reason = format ! ( "Funding transaction was un-confirmed. Locked at {} confs, now have {} confs." ,
4253
+ self . minimum_depth. unwrap( ) , funding_tx_confirmations) ;
4254
+ return Err ( ClosureReason :: ProcessingError { err : err_reason } ) ;
4243
4255
}
4256
+ } else if !self . is_outbound ( ) && self . funding_tx_confirmed_in . is_none ( ) &&
4257
+ height >= self . channel_creation_height + FUNDING_CONF_DEADLINE_BLOCKS {
4258
+ log_info ! ( logger, "Closing channel {} due to funding timeout" , log_bytes!( self . channel_id) ) ;
4259
+ // If funding_tx_confirmed_in is unset, the channel must not be active
4260
+ assert ! ( non_shutdown_state <= ChannelState :: ChannelFunded as u32 ) ;
4261
+ assert_eq ! ( non_shutdown_state & ChannelState :: OurFundingLocked as u32 , 0 ) ;
4262
+ return Err ( ClosureReason :: FundingTimedOut ) ;
4244
4263
}
4245
4264
4246
4265
Ok ( ( None , timed_out_htlcs) )
@@ -4249,7 +4268,7 @@ impl<Signer: Sign> Channel<Signer> {
4249
4268
/// Indicates the funding transaction is no longer confirmed in the main chain. This may
4250
4269
/// force-close the channel, but may also indicate a harmless reorganization of a block or two
4251
4270
/// before the channel has reached funding_locked and we can just wait for more blocks.
4252
- pub fn funding_transaction_unconfirmed < L : Deref > ( & mut self , logger : & L ) -> Result < ( ) , msgs :: ErrorMessage > where L :: Target : Logger {
4271
+ pub fn funding_transaction_unconfirmed < L : Deref > ( & mut self , logger : & L ) -> Result < ( ) , ClosureReason > where L :: Target : Logger {
4253
4272
if self . funding_tx_confirmation_height != 0 {
4254
4273
// We handle the funding disconnection by calling best_block_updated with a height one
4255
4274
// below where our funding was connected, implying a reorg back to conf_height - 1.
@@ -5279,16 +5298,18 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
5279
5298
( 7 , self . shutdown_scriptpubkey, option) ,
5280
5299
( 9 , self . target_closing_feerate_sats_per_kw, option) ,
5281
5300
( 11 , self . monitor_pending_finalized_fulfills, vec_type) ,
5301
+ ( 13 , self . channel_creation_height, required) ,
5282
5302
} ) ;
5283
5303
5284
5304
Ok ( ( ) )
5285
5305
}
5286
5306
}
5287
5307
5288
5308
const MAX_ALLOC_SIZE : usize = 64 * 1024 ;
5289
- impl < ' a , Signer : Sign , K : Deref > ReadableArgs < & ' a K > for Channel < Signer >
5309
+ impl < ' a , Signer : Sign , K : Deref > ReadableArgs < ( & ' a K , u32 ) > for Channel < Signer >
5290
5310
where K :: Target : KeysInterface < Signer = Signer > {
5291
- fn read < R : io:: Read > ( reader : & mut R , keys_source : & ' a K ) -> Result < Self , DecodeError > {
5311
+ fn read < R : io:: Read > ( reader : & mut R , args : ( & ' a K , u32 ) ) -> Result < Self , DecodeError > {
5312
+ let ( keys_source, serialized_height) = args;
5292
5313
let ver = read_ver_prefix ! ( reader, SERIALIZATION_VERSION ) ;
5293
5314
5294
5315
let user_id = Readable :: read ( reader) ?;
@@ -5516,6 +5537,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5516
5537
// Prior to supporting channel type negotiation, all of our channels were static_remotekey
5517
5538
// only, so we default to that if none was written.
5518
5539
let mut channel_type = Some ( ChannelTypeFeatures :: only_static_remote_key ( ) ) ;
5540
+ let mut channel_creation_height = Some ( serialized_height) ;
5519
5541
read_tlv_fields ! ( reader, {
5520
5542
( 0 , announcement_sigs, option) ,
5521
5543
( 1 , minimum_depth, option) ,
@@ -5525,6 +5547,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5525
5547
( 7 , shutdown_scriptpubkey, option) ,
5526
5548
( 9 , target_closing_feerate_sats_per_kw, option) ,
5527
5549
( 11 , monitor_pending_finalized_fulfills, vec_type) ,
5550
+ ( 13 , channel_creation_height, option) ,
5528
5551
} ) ;
5529
5552
5530
5553
let chan_features = channel_type. as_ref ( ) . unwrap ( ) ;
@@ -5589,6 +5612,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5589
5612
funding_tx_confirmed_in,
5590
5613
funding_tx_confirmation_height,
5591
5614
short_channel_id,
5615
+ channel_creation_height : channel_creation_height. unwrap ( ) ,
5592
5616
5593
5617
counterparty_dust_limit_satoshis,
5594
5618
holder_dust_limit_satoshis,
@@ -5737,7 +5761,7 @@ mod tests {
5737
5761
let secp_ctx = Secp256k1 :: new ( ) ;
5738
5762
let node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
5739
5763
let config = UserConfig :: default ( ) ;
5740
- match Channel :: < EnforcingSigner > :: new_outbound ( & & fee_estimator, & & keys_provider, node_id, & features, 10000000 , 100000 , 42 , & config) {
5764
+ match Channel :: < EnforcingSigner > :: new_outbound ( & & fee_estimator, & & keys_provider, node_id, & features, 10000000 , 100000 , 42 , & config, 0 ) {
5741
5765
Err ( APIError :: IncompatibleShutdownScript { script } ) => {
5742
5766
assert_eq ! ( script. into_inner( ) , non_v0_segwit_shutdown_script. into_inner( ) ) ;
5743
5767
} ,
@@ -5759,7 +5783,7 @@ mod tests {
5759
5783
5760
5784
let node_a_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
5761
5785
let config = UserConfig :: default ( ) ;
5762
- let node_a_chan = Channel :: < EnforcingSigner > :: new_outbound ( & & fee_est, & & keys_provider, node_a_node_id, & InitFeatures :: known ( ) , 10000000 , 100000 , 42 , & config) . unwrap ( ) ;
5786
+ let node_a_chan = Channel :: < EnforcingSigner > :: new_outbound ( & & fee_est, & & keys_provider, node_a_node_id, & InitFeatures :: known ( ) , 10000000 , 100000 , 42 , & config, 0 ) . unwrap ( ) ;
5763
5787
5764
5788
// Now change the fee so we can check that the fee in the open_channel message is the
5765
5789
// same as the old fee.
@@ -5784,13 +5808,13 @@ mod tests {
5784
5808
// Create Node A's channel pointing to Node B's pubkey
5785
5809
let node_b_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
5786
5810
let config = UserConfig :: default ( ) ;
5787
- let mut node_a_chan = Channel :: < EnforcingSigner > :: new_outbound ( & & feeest, & & keys_provider, node_b_node_id, & InitFeatures :: known ( ) , 10000000 , 100000 , 42 , & config) . unwrap ( ) ;
5811
+ let mut node_a_chan = Channel :: < EnforcingSigner > :: new_outbound ( & & feeest, & & keys_provider, node_b_node_id, & InitFeatures :: known ( ) , 10000000 , 100000 , 42 , & config, 0 ) . unwrap ( ) ;
5788
5812
5789
5813
// Create Node B's channel by receiving Node A's open_channel message
5790
5814
// Make sure A's dust limit is as we expect.
5791
5815
let open_channel_msg = node_a_chan. get_open_channel ( genesis_block ( network) . header . block_hash ( ) ) ;
5792
5816
let node_b_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 7 ; 32 ] ) . unwrap ( ) ) ;
5793
- let node_b_chan = Channel :: < EnforcingSigner > :: new_from_req ( & & feeest, & & keys_provider, node_b_node_id, & InitFeatures :: known ( ) , & open_channel_msg, 7 , & config) . unwrap ( ) ;
5817
+ let node_b_chan = Channel :: < EnforcingSigner > :: new_from_req ( & & feeest, & & keys_provider, node_b_node_id, & InitFeatures :: known ( ) , & open_channel_msg, 7 , & config, 0 ) . unwrap ( ) ;
5794
5818
5795
5819
// Node B --> Node A: accept channel, explicitly setting B's dust limit.
5796
5820
let mut accept_channel_msg = node_b_chan. get_accept_channel ( ) ;
@@ -5854,7 +5878,7 @@ mod tests {
5854
5878
5855
5879
let node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
5856
5880
let config = UserConfig :: default ( ) ;
5857
- let mut chan = Channel :: < EnforcingSigner > :: new_outbound ( & & fee_est, & & keys_provider, node_id, & InitFeatures :: known ( ) , 10000000 , 100000 , 42 , & config) . unwrap ( ) ;
5881
+ let mut chan = Channel :: < EnforcingSigner > :: new_outbound ( & & fee_est, & & keys_provider, node_id, & InitFeatures :: known ( ) , 10000000 , 100000 , 42 , & config, 0 ) . unwrap ( ) ;
5858
5882
5859
5883
let commitment_tx_fee_0_htlcs = chan. commit_tx_fee_msat ( 0 ) ;
5860
5884
let commitment_tx_fee_1_htlc = chan. commit_tx_fee_msat ( 1 ) ;
@@ -5903,12 +5927,12 @@ mod tests {
5903
5927
// Create Node A's channel pointing to Node B's pubkey
5904
5928
let node_b_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
5905
5929
let config = UserConfig :: default ( ) ;
5906
- let mut node_a_chan = Channel :: < EnforcingSigner > :: new_outbound ( & & feeest, & & keys_provider, node_b_node_id, & InitFeatures :: known ( ) , 10000000 , 100000 , 42 , & config) . unwrap ( ) ;
5930
+ let mut node_a_chan = Channel :: < EnforcingSigner > :: new_outbound ( & & feeest, & & keys_provider, node_b_node_id, & InitFeatures :: known ( ) , 10000000 , 100000 , 42 , & config, 0 ) . unwrap ( ) ;
5907
5931
5908
5932
// Create Node B's channel by receiving Node A's open_channel message
5909
5933
let open_channel_msg = node_a_chan. get_open_channel ( chain_hash) ;
5910
5934
let node_b_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 7 ; 32 ] ) . unwrap ( ) ) ;
5911
- let mut node_b_chan = Channel :: < EnforcingSigner > :: new_from_req ( & & feeest, & & keys_provider, node_b_node_id, & InitFeatures :: known ( ) , & open_channel_msg, 7 , & config) . unwrap ( ) ;
5935
+ let mut node_b_chan = Channel :: < EnforcingSigner > :: new_from_req ( & & feeest, & & keys_provider, node_b_node_id, & InitFeatures :: known ( ) , & open_channel_msg, 7 , & config, 0 ) . unwrap ( ) ;
5912
5936
5913
5937
// Node B --> Node A: accept channel
5914
5938
let accept_channel_msg = node_b_chan. get_accept_channel ( ) ;
@@ -5965,7 +5989,7 @@ mod tests {
5965
5989
// Create a channel.
5966
5990
let node_b_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
5967
5991
let config = UserConfig :: default ( ) ;
5968
- let mut node_a_chan = Channel :: < EnforcingSigner > :: new_outbound ( & & feeest, & & keys_provider, node_b_node_id, & InitFeatures :: known ( ) , 10000000 , 100000 , 42 , & config) . unwrap ( ) ;
5992
+ let mut node_a_chan = Channel :: < EnforcingSigner > :: new_outbound ( & & feeest, & & keys_provider, node_b_node_id, & InitFeatures :: known ( ) , 10000000 , 100000 , 42 , & config, 0 ) . unwrap ( ) ;
5969
5993
assert ! ( node_a_chan. counterparty_forwarding_info. is_none( ) ) ;
5970
5994
assert_eq ! ( node_a_chan. holder_htlc_minimum_msat, 1 ) ; // the default
5971
5995
assert ! ( node_a_chan. counterparty_forwarding_info( ) . is_none( ) ) ;
@@ -6029,7 +6053,7 @@ mod tests {
6029
6053
let counterparty_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
6030
6054
let mut config = UserConfig :: default ( ) ;
6031
6055
config. channel_options . announced_channel = false ;
6032
- let mut chan = Channel :: < InMemorySigner > :: new_outbound ( & & feeest, & & keys_provider, counterparty_node_id, & InitFeatures :: known ( ) , 10_000_000 , 100000 , 42 , & config) . unwrap ( ) ; // Nothing uses their network key in this test
6056
+ let mut chan = Channel :: < InMemorySigner > :: new_outbound ( & & feeest, & & keys_provider, counterparty_node_id, & InitFeatures :: known ( ) , 10_000_000 , 100000 , 42 , & config, 0 ) . unwrap ( ) ; // Nothing uses their network key in this test
6033
6057
chan. holder_dust_limit_satoshis = 546 ;
6034
6058
chan. counterparty_selected_channel_reserve_satoshis = Some ( 0 ) ; // Filled in in accept_channel
6035
6059
0 commit comments