@@ -13,7 +13,7 @@ use secp256k1;
13
13
use crypto:: digest:: Digest ;
14
14
15
15
use ln:: msgs;
16
- use ln:: msgs:: { DecodeError , ErrorAction , HandleError } ;
16
+ use ln:: msgs:: DecodeError ;
17
17
use ln:: channelmonitor:: ChannelMonitor ;
18
18
use ln:: channelmanager:: { PendingHTLCStatus , HTLCSource , HTLCFailReason , HTLCFailureMsg , PendingForwardHTLCInfo , RAACommitmentOrder } ;
19
19
use ln:: chan_utils:: { TxCreationKeys , HTLCOutputInCommitment , HTLC_SUCCESS_TX_WEIGHT , HTLC_TIMEOUT_TX_WEIGHT } ;
@@ -373,15 +373,6 @@ pub(super) enum ChannelError {
373
373
Close ( & ' static str ) ,
374
374
}
375
375
376
- macro_rules! secp_call {
377
- ( $res: expr, $err: expr, $chan_id: expr ) => {
378
- match $res {
379
- Ok ( key) => key,
380
- Err ( _) => return Err ( HandleError { err: $err, action: Some ( msgs:: ErrorAction :: SendErrorMessage { msg: msgs:: ErrorMessage { channel_id: $chan_id, data: $err. to_string( ) } } ) } )
381
- }
382
- } ;
383
- }
384
-
385
376
macro_rules! secp_check {
386
377
( $res: expr, $err: expr) => {
387
378
match $res {
@@ -1013,6 +1004,7 @@ impl Channel {
1013
1004
#[ inline]
1014
1005
/// Creates a set of keys for build_commitment_transaction to generate a transaction which we
1015
1006
/// will sign and send to our counterparty.
1007
+ /// If an Err is returned, it is a ChannelError::Close (for get_outbound_funding_created)
1016
1008
fn build_remote_transaction_keys ( & self ) -> Result < TxCreationKeys , ChannelError > {
1017
1009
//TODO: Ensure that the payment_key derived here ends up in the library users' wallet as we
1018
1010
//may see payments to it!
@@ -1528,40 +1520,40 @@ impl Channel {
1528
1520
( self . pending_outbound_htlcs . len ( ) as u32 , htlc_outbound_value_msat)
1529
1521
}
1530
1522
1531
- pub fn update_add_htlc ( & mut self , msg : & msgs:: UpdateAddHTLC , pending_forward_state : PendingHTLCStatus ) -> Result < ( ) , HandleError > {
1523
+ pub fn update_add_htlc ( & mut self , msg : & msgs:: UpdateAddHTLC , pending_forward_state : PendingHTLCStatus ) -> Result < ( ) , ChannelError > {
1532
1524
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 | ChannelState :: RemoteShutdownSent as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1533
- return Err ( HandleError { err : "Got add HTLC message when channel was not in an operational state" , action : None } ) ;
1525
+ return Err ( ChannelError :: Close ( "Got add HTLC message when channel was not in an operational state" ) ) ;
1534
1526
}
1535
1527
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1536
- return Err ( HandleError { err : "Peer sent update_add_htlc when we needed a channel_reestablish" , action : Some ( msgs :: ErrorAction :: SendErrorMessage { msg : msgs :: ErrorMessage { data : "Peer sent update_add_htlc when we needed a channel_reestablish" . to_string ( ) , channel_id : msg . channel_id } } ) } ) ;
1528
+ return Err ( ChannelError :: Close ( "Peer sent update_add_htlc when we needed a channel_reestablish" ) ) ;
1537
1529
}
1538
1530
if msg. amount_msat > self . channel_value_satoshis * 1000 {
1539
- return Err ( HandleError { err : "Remote side tried to send more than the total value of the channel" , action : None } ) ;
1531
+ return Err ( ChannelError :: Close ( "Remote side tried to send more than the total value of the channel" ) ) ;
1540
1532
}
1541
1533
if msg. amount_msat < self . our_htlc_minimum_msat {
1542
- return Err ( HandleError { err : "Remote side tried to send less than our minimum HTLC value" , action : None } ) ;
1534
+ return Err ( ChannelError :: Close ( "Remote side tried to send less than our minimum HTLC value" ) ) ;
1543
1535
}
1544
1536
1545
1537
let ( inbound_htlc_count, htlc_inbound_value_msat) = self . get_inbound_pending_htlc_stats ( ) ;
1546
1538
if inbound_htlc_count + 1 > OUR_MAX_HTLCS as u32 {
1547
- return Err ( HandleError { err : "Remote tried to push more than our max accepted HTLCs" , action : None } ) ;
1539
+ return Err ( ChannelError :: Close ( "Remote tried to push more than our max accepted HTLCs" ) ) ;
1548
1540
}
1549
1541
//TODO: Spec is unclear if this is per-direction or in total (I assume per direction):
1550
1542
// Check our_max_htlc_value_in_flight_msat
1551
1543
if htlc_inbound_value_msat + msg. amount_msat > Channel :: get_our_max_htlc_value_in_flight_msat ( self . channel_value_satoshis ) {
1552
- return Err ( HandleError { err : "Remote HTLC add would put them over their max HTLC value in flight" , action : None } ) ;
1544
+ return Err ( ChannelError :: Close ( "Remote HTLC add would put them over their max HTLC value in flight" ) ) ;
1553
1545
}
1554
1546
// Check our_channel_reserve_satoshis (we're getting paid, so they have to at least meet
1555
1547
// the reserve_satoshis we told them to always have as direct payment so that they lose
1556
1548
// something if we punish them for broadcasting an old state).
1557
1549
if htlc_inbound_value_msat + msg. amount_msat + self . value_to_self_msat > ( self . channel_value_satoshis - Channel :: get_our_channel_reserve_satoshis ( self . channel_value_satoshis ) ) * 1000 {
1558
- return Err ( HandleError { err : "Remote HTLC add would put them over their reserve value" , action : None } ) ;
1550
+ return Err ( ChannelError :: Close ( "Remote HTLC add would put them over their reserve value" ) ) ;
1559
1551
}
1560
1552
if self . next_remote_htlc_id != msg. htlc_id {
1561
- return Err ( HandleError { err : "Remote skipped HTLC ID" , action : None } ) ;
1553
+ return Err ( ChannelError :: Close ( "Remote skipped HTLC ID" ) ) ;
1562
1554
}
1563
1555
if msg. cltv_expiry >= 500000000 {
1564
- return Err ( HandleError { err : "Remote provided CLTV expiry in seconds instead of block height" , action : None } ) ;
1556
+ return Err ( ChannelError :: Close ( "Remote provided CLTV expiry in seconds instead of block height" ) ) ;
1565
1557
}
1566
1558
1567
1559
//TODO: Check msg.cltv_expiry further? Do this in channel manager?
@@ -1613,7 +1605,7 @@ impl Channel {
1613
1605
Err ( ChannelError :: Close ( "Remote tried to fulfill/fail an HTLC we couldn't find" ) )
1614
1606
}
1615
1607
1616
- pub fn update_fulfill_htlc ( & mut self , msg : & msgs:: UpdateFulfillHTLC ) -> Result < & HTLCSource , ChannelError > {
1608
+ pub fn update_fulfill_htlc ( & mut self , msg : & msgs:: UpdateFulfillHTLC ) -> Result < HTLCSource , ChannelError > {
1617
1609
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1618
1610
return Err ( ChannelError :: Close ( "Got fulfill HTLC message when channel was not in an operational state" ) ) ;
1619
1611
}
@@ -1626,29 +1618,31 @@ impl Channel {
1626
1618
let mut payment_hash = [ 0 ; 32 ] ;
1627
1619
sha. result ( & mut payment_hash) ;
1628
1620
1629
- self . mark_outbound_htlc_removed ( msg. htlc_id , Some ( payment_hash) , None )
1621
+ self . mark_outbound_htlc_removed ( msg. htlc_id , Some ( payment_hash) , None ) . map ( |source| source . clone ( ) )
1630
1622
}
1631
1623
1632
- pub fn update_fail_htlc ( & mut self , msg : & msgs:: UpdateFailHTLC , fail_reason : HTLCFailReason ) -> Result < & HTLCSource , ChannelError > {
1624
+ pub fn update_fail_htlc ( & mut self , msg : & msgs:: UpdateFailHTLC , fail_reason : HTLCFailReason ) -> Result < ( ) , ChannelError > {
1633
1625
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1634
1626
return Err ( ChannelError :: Close ( "Got fail HTLC message when channel was not in an operational state" ) ) ;
1635
1627
}
1636
1628
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1637
1629
return Err ( ChannelError :: Close ( "Peer sent update_fail_htlc when we needed a channel_reestablish" ) ) ;
1638
1630
}
1639
1631
1640
- self . mark_outbound_htlc_removed ( msg. htlc_id , None , Some ( fail_reason) )
1632
+ self . mark_outbound_htlc_removed ( msg. htlc_id , None , Some ( fail_reason) ) ?;
1633
+ Ok ( ( ) )
1641
1634
}
1642
1635
1643
- pub fn update_fail_malformed_htlc < ' a > ( & mut self , msg : & msgs:: UpdateFailMalformedHTLC , fail_reason : HTLCFailReason ) -> Result < & HTLCSource , ChannelError > {
1636
+ pub fn update_fail_malformed_htlc < ' a > ( & mut self , msg : & msgs:: UpdateFailMalformedHTLC , fail_reason : HTLCFailReason ) -> Result < ( ) , ChannelError > {
1644
1637
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1645
1638
return Err ( ChannelError :: Close ( "Got fail malformed HTLC message when channel was not in an operational state" ) ) ;
1646
1639
}
1647
1640
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
1648
1641
return Err ( ChannelError :: Close ( "Peer sent update_fail_malformed_htlc when we needed a channel_reestablish" ) ) ;
1649
1642
}
1650
1643
1651
- self . mark_outbound_htlc_removed ( msg. htlc_id , None , Some ( fail_reason) )
1644
+ self . mark_outbound_htlc_removed ( msg. htlc_id , None , Some ( fail_reason) ) ?;
1645
+ Ok ( ( ) )
1652
1646
}
1653
1647
1654
1648
pub fn commitment_signed ( & mut self , msg : & msgs:: CommitmentSigned , fee_estimator : & FeeEstimator ) -> Result < ( msgs:: RevokeAndACK , Option < msgs:: CommitmentSigned > , Option < msgs:: ClosingSigned > , ChannelMonitor ) , ChannelError > {
@@ -2508,24 +2502,24 @@ impl Channel {
2508
2502
Ok ( ( our_shutdown, self . maybe_propose_first_closing_signed ( fee_estimator) , dropped_outbound_htlcs) )
2509
2503
}
2510
2504
2511
- pub fn closing_signed ( & mut self , fee_estimator : & FeeEstimator , msg : & msgs:: ClosingSigned ) -> Result < ( Option < msgs:: ClosingSigned > , Option < Transaction > ) , HandleError > {
2505
+ pub fn closing_signed ( & mut self , fee_estimator : & FeeEstimator , msg : & msgs:: ClosingSigned ) -> Result < ( Option < msgs:: ClosingSigned > , Option < Transaction > ) , ChannelError > {
2512
2506
if self . channel_state & BOTH_SIDES_SHUTDOWN_MASK != BOTH_SIDES_SHUTDOWN_MASK {
2513
- return Err ( HandleError { err : "Remote end sent us a closing_signed before both sides provided a shutdown" , action : None } ) ;
2507
+ return Err ( ChannelError :: Close ( "Remote end sent us a closing_signed before both sides provided a shutdown" ) ) ;
2514
2508
}
2515
2509
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
2516
- return Err ( HandleError { err : "Peer sent closing_signed when we needed a channel_reestablish" , action : Some ( msgs :: ErrorAction :: SendErrorMessage { msg : msgs :: ErrorMessage { data : "Peer sent closing_signed when we needed a channel_reestablish" . to_string ( ) , channel_id : msg . channel_id } } ) } ) ;
2510
+ return Err ( ChannelError :: Close ( "Peer sent closing_signed when we needed a channel_reestablish" ) ) ;
2517
2511
}
2518
2512
if !self . pending_inbound_htlcs . is_empty ( ) || !self . pending_outbound_htlcs . is_empty ( ) {
2519
- return Err ( HandleError { err : "Remote end sent us a closing_signed while there were still pending HTLCs" , action : None } ) ;
2513
+ return Err ( ChannelError :: Close ( "Remote end sent us a closing_signed while there were still pending HTLCs" ) ) ;
2520
2514
}
2521
2515
if msg. fee_satoshis > 21000000 * 10000000 { //this is required to stop potential overflow in build_closing_transaction
2522
- return Err ( HandleError { err : "Remote tried to send us a closing tx with > 21 million BTC fee" , action : None } ) ;
2516
+ return Err ( ChannelError :: Close ( "Remote tried to send us a closing tx with > 21 million BTC fee" ) ) ;
2523
2517
}
2524
2518
2525
2519
let funding_redeemscript = self . get_funding_redeemscript ( ) ;
2526
2520
let ( mut closing_tx, used_total_fee) = self . build_closing_transaction ( msg. fee_satoshis , false ) ;
2527
2521
if used_total_fee != msg. fee_satoshis {
2528
- return Err ( HandleError { err : "Remote sent us a closing_signed with a fee greater than the value they can claim" , action : None } ) ;
2522
+ return Err ( ChannelError :: Close ( "Remote sent us a closing_signed with a fee greater than the value they can claim" ) ) ;
2529
2523
}
2530
2524
let mut sighash = Message :: from_slice ( & bip143:: SighashComponents :: new ( & closing_tx) . sighash_all ( & closing_tx. input [ 0 ] , & funding_redeemscript, self . channel_value_satoshis ) [ ..] ) . unwrap ( ) ;
2531
2525
@@ -2536,7 +2530,7 @@ impl Channel {
2536
2530
// limits, so check for that case by re-checking the signature here.
2537
2531
closing_tx = self . build_closing_transaction ( msg. fee_satoshis , true ) . 0 ;
2538
2532
sighash = Message :: from_slice ( & bip143:: SighashComponents :: new ( & closing_tx) . sighash_all ( & closing_tx. input [ 0 ] , & funding_redeemscript, self . channel_value_satoshis ) [ ..] ) . unwrap ( ) ;
2539
- secp_call ! ( self . secp_ctx. verify( & sighash, & msg. signature, & self . their_funding_pubkey. unwrap( ) ) , "Invalid closing tx signature from peer" , self . channel_id ( ) ) ;
2533
+ secp_check ! ( self . secp_ctx. verify( & sighash, & msg. signature, & self . their_funding_pubkey. unwrap( ) ) , "Invalid closing tx signature from peer" ) ;
2540
2534
} ,
2541
2535
} ;
2542
2536
@@ -2570,7 +2564,7 @@ impl Channel {
2570
2564
if proposed_sat_per_kw > our_max_feerate {
2571
2565
if let Some ( ( last_feerate, _) ) = self . last_sent_closing_fee {
2572
2566
if our_max_feerate <= last_feerate {
2573
- return Err ( HandleError { err : "Unable to come to consensus about closing feerate, remote wanted something higher than our Normal feerate" , action : None } ) ;
2567
+ return Err ( ChannelError :: Close ( "Unable to come to consensus about closing feerate, remote wanted something higher than our Normal feerate" ) ) ;
2574
2568
}
2575
2569
}
2576
2570
propose_new_feerate ! ( our_max_feerate) ;
@@ -2580,7 +2574,7 @@ impl Channel {
2580
2574
if proposed_sat_per_kw < our_min_feerate {
2581
2575
if let Some ( ( last_feerate, _) ) = self . last_sent_closing_fee {
2582
2576
if our_min_feerate >= last_feerate {
2583
- return Err ( HandleError { err : "Unable to come to consensus about closing feerate, remote wanted something lower than our Background feerate" , action : None } ) ;
2577
+ return Err ( ChannelError :: Close ( "Unable to come to consensus about closing feerate, remote wanted something lower than our Background feerate" ) ) ;
2584
2578
}
2585
2579
}
2586
2580
propose_new_feerate ! ( our_min_feerate) ;
@@ -2780,7 +2774,7 @@ impl Channel {
2780
2774
/// In case of Err, the channel may have been closed, at which point the standard requirements
2781
2775
/// apply - no calls may be made except those explicitly stated to be allowed post-shutdown.
2782
2776
/// Only returns an ErrorAction of DisconnectPeer, if Err.
2783
- pub fn block_connected ( & mut self , header : & BlockHeader , height : u32 , txn_matched : & [ & Transaction ] , indexes_of_txn_matched : & [ u32 ] ) -> Result < Option < msgs:: FundingLocked > , HandleError > {
2777
+ pub fn block_connected ( & mut self , header : & BlockHeader , height : u32 , txn_matched : & [ & Transaction ] , indexes_of_txn_matched : & [ u32 ] ) -> Result < Option < msgs:: FundingLocked > , msgs :: ErrorMessage > {
2784
2778
let non_shutdown_state = self . channel_state & ( !MULTI_STATE_FLAGS ) ;
2785
2779
if header. bitcoin_hash ( ) != self . last_block_connected {
2786
2780
self . last_block_connected = header. bitcoin_hash ( ) ;
@@ -2840,7 +2834,10 @@ impl Channel {
2840
2834
}
2841
2835
self . channel_state = ChannelState :: ShutdownComplete as u32 ;
2842
2836
self . channel_update_count += 1 ;
2843
- return Err ( HandleError { err : "funding tx had wrong script/value" , action : Some ( ErrorAction :: DisconnectPeer { msg : None } ) } ) ;
2837
+ return Err ( msgs:: ErrorMessage {
2838
+ channel_id : self . channel_id ( ) ,
2839
+ data : "funding tx had wrong script/value" . to_owned ( )
2840
+ } ) ;
2844
2841
} else {
2845
2842
if self . channel_outbound {
2846
2843
for input in tx. input . iter ( ) {
@@ -2953,6 +2950,7 @@ impl Channel {
2953
2950
}
2954
2951
}
2955
2952
2953
+ /// If an Err is returned, it is a ChannelError::Close (for get_outbound_funding_created)
2956
2954
fn get_outbound_funding_created_signature ( & mut self ) -> Result < ( Signature , Transaction ) , ChannelError > {
2957
2955
let funding_script = self . get_funding_redeemscript ( ) ;
2958
2956
@@ -2970,6 +2968,7 @@ impl Channel {
2970
2968
/// or if called on an inbound channel.
2971
2969
/// Note that channel_id changes during this call!
2972
2970
/// Do NOT broadcast the funding transaction until after a successful funding_signed call!
2971
+ /// If an Err is returned, it is a ChannelError::Close.
2973
2972
pub fn get_outbound_funding_created ( & mut self , funding_txo : OutPoint ) -> Result < ( msgs:: FundingCreated , ChannelMonitor ) , ChannelError > {
2974
2973
if !self . channel_outbound {
2975
2974
panic ! ( "Tried to create outbound funding_created message on an inbound channel!" ) ;
@@ -3160,7 +3159,7 @@ impl Channel {
3160
3159
}
3161
3160
3162
3161
/// Creates a signed commitment transaction to send to the remote peer.
3163
- /// Always returns a Channel-failing HandleError::action if an immediately-preceding (read: the
3162
+ /// Always returns a ChannelError::Close if an immediately-preceding (read: the
3164
3163
/// last call to this Channel) send_htlc returned Ok(Some(_)) and there is an Err.
3165
3164
/// May panic if called except immediately after a successful, Ok(Some(_))-returning send_htlc.
3166
3165
pub fn send_commitment ( & mut self ) -> Result < ( msgs:: CommitmentSigned , ChannelMonitor ) , ChannelError > {
0 commit comments