@@ -1539,50 +1539,65 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
15391539
15401540 /// Handles a funding_signed message from the remote end.
15411541 /// 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 > {
15431543 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?" ) ) ;
15451545 }
15461546 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!" ) ) ;
15481548 }
15491549 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 ||
15511551 self . cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
15521552 panic ! ( "Should not have advanced channel commitment tx numbers prior to funding_created" ) ;
15531553 }
15541554
15551555 let funding_script = self . get_funding_redeemscript ( ) ;
15561556
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 ) ?;
15581561 let local_initial_commitment_tx = self . build_commitment_transaction ( self . cur_local_commitment_transaction_number , & local_keys, true , false , self . feerate_per_kw ) . 0 ;
15591562 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) [ ..] ) ;
15601563
15611564 let their_funding_pubkey = & self . their_pubkeys . as_ref ( ) . unwrap ( ) . funding_pubkey ;
15621565
15631566 // They sign the "local" commitment transaction, allowing us to broadcast the tx if we wish.
15641567 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" ) ) ;
15661569 }
15671570
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( ) ) ;
15791584
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+ } }
15851590 }
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)
15861601 }
15871602
15881603 pub fn funding_locked ( & mut self , msg : & msgs:: FundingLocked ) -> Result < ( ) , ChannelError > {
@@ -2951,7 +2966,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
29512966
29522967 /// May only be called after funding has been initiated (ie is_funding_initiated() is true)
29532968 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 {
29552970 panic ! ( "Can't get a channel monitor until funding has been created" ) ;
29562971 }
29572972 self . channel_monitor . as_mut ( ) . unwrap ( )
@@ -3105,7 +3120,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
31053120
31063121 /// Returns true if funding_created was sent/received.
31073122 pub fn is_funding_initiated ( & self ) -> bool {
3108- self . channel_state >= ChannelState :: FundingCreated as u32
3123+ self . channel_state >= ChannelState :: FundingSent as u32
31093124 }
31103125
31113126 /// 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> {
33373352 }
33383353
33393354 /// 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 > {
33413356 let remote_keys = self . build_remote_transaction_keys ( ) ?;
33423357 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 )
33453360 }
33463361
33473362 /// Updates channel state with knowledge of the funding transaction's txid/index, and generates
@@ -3351,7 +3366,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
33513366 /// Note that channel_id changes during this call!
33523367 /// Do NOT broadcast the funding transaction until after a successful funding_signed call!
33533368 /// 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 > {
33553370 if !self . channel_outbound {
33563371 panic ! ( "Tried to create outbound funding_created message on an inbound channel!" ) ;
33573372 }
@@ -3365,7 +3380,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
33653380 }
33663381
33673382 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 ( ) {
33693384 Ok ( res) => res,
33703385 Err ( e) => {
33713386 log_error ! ( self , "Got bad signatures: {:?}!" , e) ;
@@ -3378,37 +3393,15 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
33783393
33793394 // Now that we're past error-generating stuff, update our local state:
33803395
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-
34023396 self . channel_state = ChannelState :: FundingCreated as u32 ;
34033397 self . channel_id = funding_txo. to_channel_id ( ) ;
3404- self . cur_remote_commitment_transaction_number -= 1 ;
34053398
3406- Ok ( ( msgs:: FundingCreated {
3407- temporary_channel_id : temporary_channel_id ,
3399+ Ok ( msgs:: FundingCreated {
3400+ temporary_channel_id,
34083401 funding_txid : funding_txo. txid ,
34093402 funding_output_index : funding_txo. index ,
34103403 signature : our_signature
3411- } , channel_monitor ) )
3404+ } )
34123405 }
34133406
34143407 /// Gets an UnsignedChannelAnnouncement, as well as a signature covering it using our
@@ -4385,7 +4378,7 @@ mod tests {
43854378 value: 10000000 , script_pubkey: output_script. clone( ) ,
43864379 } ] } ;
43874380 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 ( ) ;
43894382 let ( funding_signed_msg, _) = node_b_chan. funding_created ( & funding_created_msg) . unwrap ( ) ;
43904383
43914384 // Node B --> Node A: funding signed
0 commit comments