@@ -1539,50 +1539,65 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1539
1539
1540
1540
/// Handles a funding_signed message from the remote end.
1541
1541
/// If this call is successful, broadcast the funding transaction (and not before!)
1542
- pub fn funding_signed ( & mut self , msg : & msgs:: FundingSigned ) -> Result < ChannelMonitorUpdate , ( Option < ChannelMonitorUpdate > , ChannelError ) > {
1542
+ pub fn funding_signed ( & mut self , msg : & msgs:: FundingSigned ) -> Result < ChannelMonitor < ChanSigner > , ChannelError > {
1543
1543
if !self . channel_outbound {
1544
- return Err ( ( None , ChannelError :: Close ( "Received funding_signed for an inbound channel?" ) ) ) ;
1544
+ return Err ( ChannelError :: Close ( "Received funding_signed for an inbound channel?" ) ) ;
1545
1545
}
1546
1546
if self . channel_state & !( ChannelState :: MonitorUpdateFailed as u32 ) != ChannelState :: FundingCreated as u32 {
1547
- return Err ( ( None , ChannelError :: Close ( "Received funding_signed in strange state!" ) ) ) ;
1547
+ return Err ( ChannelError :: Close ( "Received funding_signed in strange state!" ) ) ;
1548
1548
}
1549
1549
if self . commitment_secrets . get_min_seen_secret ( ) != ( 1 << 48 ) ||
1550
- self . cur_remote_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER - 1 ||
1550
+ self . cur_remote_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
1551
1551
self . cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
1552
1552
panic ! ( "Should not have advanced channel commitment tx numbers prior to funding_created" ) ;
1553
1553
}
1554
1554
1555
1555
let funding_script = self . get_funding_redeemscript ( ) ;
1556
1556
1557
- let local_keys = self . build_local_transaction_keys ( self . cur_local_commitment_transaction_number ) . map_err ( |e| ( None , e) ) ?;
1557
+ let remote_keys = self . build_remote_transaction_keys ( ) ?;
1558
+ let remote_initial_commitment_tx = self . build_commitment_transaction ( self . cur_remote_commitment_transaction_number , & remote_keys, false , false , self . feerate_per_kw ) . 0 ;
1559
+
1560
+ let local_keys = self . build_local_transaction_keys ( self . cur_local_commitment_transaction_number ) ?;
1558
1561
let local_initial_commitment_tx = self . build_commitment_transaction ( self . cur_local_commitment_transaction_number , & local_keys, true , false , self . feerate_per_kw ) . 0 ;
1559
1562
let local_sighash = hash_to_message ! ( & bip143:: SighashComponents :: new( & local_initial_commitment_tx) . sighash_all( & local_initial_commitment_tx. input[ 0 ] , & funding_script, self . channel_value_satoshis) [ ..] ) ;
1560
1563
1561
1564
let their_funding_pubkey = & self . their_pubkeys . as_ref ( ) . unwrap ( ) . funding_pubkey ;
1562
1565
1563
1566
// They sign the "local" commitment transaction, allowing us to broadcast the tx if we wish.
1564
1567
if let Err ( _) = self . secp_ctx . verify ( & local_sighash, & msg. signature , their_funding_pubkey) {
1565
- return Err ( ( None , ChannelError :: Close ( "Invalid funding_signed signature from peer" ) ) ) ;
1568
+ return Err ( ChannelError :: Close ( "Invalid funding_signed signature from peer" ) ) ;
1566
1569
}
1567
1570
1568
- self . latest_monitor_update_id += 1 ;
1569
- let monitor_update = ChannelMonitorUpdate {
1570
- update_id : self . latest_monitor_update_id ,
1571
- updates : vec ! [ ChannelMonitorUpdateStep :: LatestLocalCommitmentTXInfo {
1572
- commitment_tx: LocalCommitmentTransaction :: new_missing_local_sig( local_initial_commitment_tx, & msg. signature, & PublicKey :: from_secret_key( & self . secp_ctx, self . local_keys. funding_key( ) ) , their_funding_pubkey, local_keys, self . feerate_per_kw, Vec :: new( ) ) ,
1573
- htlc_outputs: Vec :: new( ) ,
1574
- } ]
1575
- } ;
1576
- self . channel_monitor . as_mut ( ) . unwrap ( ) . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
1577
- self . channel_state = ChannelState :: FundingSent as u32 | ( self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) ) ;
1578
- self . cur_local_commitment_transaction_number -= 1 ;
1571
+ let their_pubkeys = self . their_pubkeys . as_ref ( ) . unwrap ( ) ;
1572
+ let funding_redeemscript = self . get_funding_redeemscript ( ) ;
1573
+ let funding_txo = self . funding_txo . as_ref ( ) . unwrap ( ) ;
1574
+ let funding_txo_script = funding_redeemscript. to_v0_p2wsh ( ) ;
1575
+ macro_rules! create_monitor {
1576
+ ( ) => { {
1577
+ let mut channel_monitor = ChannelMonitor :: new( self . local_keys. clone( ) ,
1578
+ & self . shutdown_pubkey, self . our_to_self_delay,
1579
+ & self . destination_script, ( funding_txo. clone( ) , funding_txo_script. clone( ) ) ,
1580
+ & their_pubkeys. htlc_basepoint, & their_pubkeys. delayed_payment_basepoint,
1581
+ self . their_to_self_delay, funding_redeemscript. clone( ) , self . channel_value_satoshis,
1582
+ self . get_commitment_transaction_number_obscure_factor( ) ,
1583
+ self . logger. clone( ) ) ;
1579
1584
1580
- if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) == 0 {
1581
- Ok ( monitor_update )
1582
- } else {
1583
- Err ( ( Some ( monitor_update ) ,
1584
- ChannelError :: Ignore ( "Previous monitor update failure prevented funding_signed from allowing funding broadcast" ) ) )
1585
+ channel_monitor . provide_latest_remote_commitment_tx_info ( & remote_initial_commitment_tx , Vec :: new ( ) , self . cur_remote_commitment_transaction_number , self . their_cur_commitment_point . unwrap ( ) ) ;
1586
+ channel_monitor . provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: new_missing_local_sig ( local_initial_commitment_tx . clone ( ) , & msg . signature , & PublicKey :: from_secret_key ( & self . secp_ctx , self . local_keys . funding_key ( ) ) , their_funding_pubkey , local_keys . clone ( ) , self . feerate_per_kw , Vec :: new ( ) ) , Vec :: new ( ) ) . unwrap ( ) ;
1587
+
1588
+ channel_monitor
1589
+ } }
1585
1590
}
1591
+
1592
+ self . channel_monitor = Some ( create_monitor ! ( ) ) ;
1593
+ let channel_monitor = create_monitor ! ( ) ;
1594
+
1595
+ assert_eq ! ( self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) , 0 ) ; // We have no had any monitor(s) yet to fail update!
1596
+ self . channel_state = ChannelState :: FundingSent as u32 ;
1597
+ self . cur_local_commitment_transaction_number -= 1 ;
1598
+ self . cur_remote_commitment_transaction_number -= 1 ;
1599
+
1600
+ Ok ( channel_monitor)
1586
1601
}
1587
1602
1588
1603
pub fn funding_locked ( & mut self , msg : & msgs:: FundingLocked ) -> Result < ( ) , ChannelError > {
@@ -2951,7 +2966,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2951
2966
2952
2967
/// May only be called after funding has been initiated (ie is_funding_initiated() is true)
2953
2968
pub fn channel_monitor ( & mut self ) -> & mut ChannelMonitor < ChanSigner > {
2954
- if self . channel_state < ChannelState :: FundingCreated as u32 {
2969
+ if self . channel_state < ChannelState :: FundingSent as u32 {
2955
2970
panic ! ( "Can't get a channel monitor until funding has been created" ) ;
2956
2971
}
2957
2972
self . channel_monitor . as_mut ( ) . unwrap ( )
@@ -3105,7 +3120,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3105
3120
3106
3121
/// Returns true if funding_created was sent/received.
3107
3122
pub fn is_funding_initiated ( & self ) -> bool {
3108
- self . channel_state >= ChannelState :: FundingCreated as u32
3123
+ self . channel_state >= ChannelState :: FundingSent as u32
3109
3124
}
3110
3125
3111
3126
/// Returns true if this channel is fully shut down. True here implies that no further actions
@@ -3337,11 +3352,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3337
3352
}
3338
3353
3339
3354
/// If an Err is returned, it is a ChannelError::Close (for get_outbound_funding_created)
3340
- fn get_outbound_funding_created_signature ( & mut self ) -> Result < ( Signature , Transaction ) , ChannelError > {
3355
+ fn get_outbound_funding_created_signature ( & mut self ) -> Result < Signature , ChannelError > {
3341
3356
let remote_keys = self . build_remote_transaction_keys ( ) ?;
3342
3357
let remote_initial_commitment_tx = self . build_commitment_transaction ( self . cur_remote_commitment_transaction_number , & remote_keys, false , false , self . feerate_per_kw ) . 0 ;
3343
- Ok ( ( self . local_keys . sign_remote_commitment ( self . feerate_per_kw , & remote_initial_commitment_tx, & remote_keys, & Vec :: new ( ) , self . our_to_self_delay , & self . secp_ctx )
3344
- . map_err ( |_| ChannelError :: Close ( "Failed to get signatures for new commitment_signed" ) ) ?. 0 , remote_initial_commitment_tx ) )
3358
+ Ok ( self . local_keys . sign_remote_commitment ( self . feerate_per_kw , & remote_initial_commitment_tx, & remote_keys, & Vec :: new ( ) , self . our_to_self_delay , & self . secp_ctx )
3359
+ . map_err ( |_| ChannelError :: Close ( "Failed to get signatures for new commitment_signed" ) ) ?. 0 )
3345
3360
}
3346
3361
3347
3362
/// Updates channel state with knowledge of the funding transaction's txid/index, and generates
@@ -3351,7 +3366,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3351
3366
/// Note that channel_id changes during this call!
3352
3367
/// Do NOT broadcast the funding transaction until after a successful funding_signed call!
3353
3368
/// If an Err is returned, it is a ChannelError::Close.
3354
- pub fn get_outbound_funding_created ( & mut self , funding_txo : OutPoint ) -> Result < ( msgs:: FundingCreated , ChannelMonitor < ChanSigner > ) , ChannelError > {
3369
+ pub fn get_outbound_funding_created ( & mut self , funding_txo : OutPoint ) -> Result < msgs:: FundingCreated , ChannelError > {
3355
3370
if !self . channel_outbound {
3356
3371
panic ! ( "Tried to create outbound funding_created message on an inbound channel!" ) ;
3357
3372
}
@@ -3365,7 +3380,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3365
3380
}
3366
3381
3367
3382
self . funding_txo = Some ( funding_txo. clone ( ) ) ;
3368
- let ( our_signature, commitment_tx ) = match self . get_outbound_funding_created_signature ( ) {
3383
+ let our_signature = match self . get_outbound_funding_created_signature ( ) {
3369
3384
Ok ( res) => res,
3370
3385
Err ( e) => {
3371
3386
log_error ! ( self , "Got bad signatures: {:?}!" , e) ;
@@ -3378,37 +3393,15 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3378
3393
3379
3394
// Now that we're past error-generating stuff, update our local state:
3380
3395
3381
- let their_pubkeys = self . their_pubkeys . as_ref ( ) . unwrap ( ) ;
3382
- let funding_redeemscript = self . get_funding_redeemscript ( ) ;
3383
- let funding_txo_script = funding_redeemscript. to_v0_p2wsh ( ) ;
3384
- macro_rules! create_monitor {
3385
- ( ) => { {
3386
- let mut channel_monitor = ChannelMonitor :: new( self . local_keys. clone( ) ,
3387
- & self . shutdown_pubkey, self . our_to_self_delay,
3388
- & self . destination_script, ( funding_txo, funding_txo_script. clone( ) ) ,
3389
- & their_pubkeys. htlc_basepoint, & their_pubkeys. delayed_payment_basepoint,
3390
- self . their_to_self_delay, funding_redeemscript. clone( ) , self . channel_value_satoshis,
3391
- self . get_commitment_transaction_number_obscure_factor( ) ,
3392
- self . logger. clone( ) ) ;
3393
-
3394
- channel_monitor. provide_latest_remote_commitment_tx_info( & commitment_tx, Vec :: new( ) , self . cur_remote_commitment_transaction_number, self . their_cur_commitment_point. unwrap( ) ) ;
3395
- channel_monitor
3396
- } }
3397
- }
3398
-
3399
- self . channel_monitor = Some ( create_monitor ! ( ) ) ;
3400
- let channel_monitor = create_monitor ! ( ) ;
3401
-
3402
3396
self . channel_state = ChannelState :: FundingCreated as u32 ;
3403
3397
self . channel_id = funding_txo. to_channel_id ( ) ;
3404
- self . cur_remote_commitment_transaction_number -= 1 ;
3405
3398
3406
- Ok ( ( msgs:: FundingCreated {
3407
- temporary_channel_id : temporary_channel_id ,
3399
+ Ok ( msgs:: FundingCreated {
3400
+ temporary_channel_id,
3408
3401
funding_txid : funding_txo. txid ,
3409
3402
funding_output_index : funding_txo. index ,
3410
3403
signature : our_signature
3411
- } , channel_monitor ) )
3404
+ } )
3412
3405
}
3413
3406
3414
3407
/// Gets an UnsignedChannelAnnouncement, as well as a signature covering it using our
@@ -4385,7 +4378,7 @@ mod tests {
4385
4378
value: 10000000 , script_pubkey: output_script. clone( ) ,
4386
4379
} ] } ;
4387
4380
let funding_outpoint = OutPoint :: new ( tx. txid ( ) , 0 ) ;
4388
- let ( funding_created_msg, _ ) = node_a_chan. get_outbound_funding_created ( funding_outpoint) . unwrap ( ) ;
4381
+ let funding_created_msg = node_a_chan. get_outbound_funding_created ( funding_outpoint) . unwrap ( ) ;
4389
4382
let ( funding_signed_msg, _) = node_b_chan. funding_created ( & funding_created_msg) . unwrap ( ) ;
4390
4383
4391
4384
// Node B --> Node A: funding signed
0 commit comments