@@ -1511,6 +1511,84 @@ impl ChannelManager {
1511
1511
Ok ( funding_msg)
1512
1512
}
1513
1513
1514
+ fn internal_funding_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: FundingSigned ) -> Result < ( ) , MsgHandleErrInternal > {
1515
+ let ( funding_txo, user_id, monitor) = {
1516
+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1517
+ match channel_state. by_id . get_mut ( & msg. channel_id ) {
1518
+ Some ( chan) => {
1519
+ if chan. get_their_node_id ( ) != * their_node_id {
1520
+ //TODO: here and below MsgHandleErrInternal, #153 case
1521
+ return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
1522
+ }
1523
+ let chan_monitor = chan. funding_signed ( & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?;
1524
+ ( chan. get_funding_txo ( ) . unwrap ( ) , chan. get_user_id ( ) , chan_monitor)
1525
+ } ,
1526
+ None => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
1527
+ }
1528
+ } ;
1529
+ if let Err ( _e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) {
1530
+ unimplemented ! ( ) ;
1531
+ }
1532
+ let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
1533
+ pending_events. push ( events:: Event :: FundingBroadcastSafe {
1534
+ funding_txo : funding_txo,
1535
+ user_channel_id : user_id,
1536
+ } ) ;
1537
+ Ok ( ( ) )
1538
+ }
1539
+
1540
+ fn internal_funding_locked ( & self , their_node_id : & PublicKey , msg : & msgs:: FundingLocked ) -> Result < Option < msgs:: AnnouncementSignatures > , MsgHandleErrInternal > {
1541
+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1542
+ match channel_state. by_id . get_mut ( & msg. channel_id ) {
1543
+ Some ( chan) => {
1544
+ if chan. get_their_node_id ( ) != * their_node_id {
1545
+ //TODO: here and below MsgHandleErrInternal, #153 case
1546
+ return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
1547
+ }
1548
+ chan. funding_locked ( & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?;
1549
+ return Ok ( self . get_announcement_sigs ( chan) ) ;
1550
+ } ,
1551
+ None => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
1552
+ } ;
1553
+ }
1554
+
1555
+ fn internal_shutdown ( & self , their_node_id : & PublicKey , msg : & msgs:: Shutdown ) -> Result < ( Option < msgs:: Shutdown > , Option < msgs:: ClosingSigned > ) , MsgHandleErrInternal > {
1556
+ let ( res, chan_option) = {
1557
+ let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1558
+ let channel_state = channel_state_lock. borrow_parts ( ) ;
1559
+
1560
+ match channel_state. by_id . entry ( msg. channel_id . clone ( ) ) {
1561
+ hash_map:: Entry :: Occupied ( mut chan_entry) => {
1562
+ if chan_entry. get ( ) . get_their_node_id ( ) != * their_node_id {
1563
+ //TODO: here and below MsgHandleErrInternal, #153 case
1564
+ return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
1565
+ }
1566
+ let res = chan_entry. get_mut ( ) . shutdown ( & * self . fee_estimator , & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?;
1567
+ if chan_entry. get ( ) . is_shutdown ( ) {
1568
+ if let Some ( short_id) = chan_entry. get ( ) . get_short_channel_id ( ) {
1569
+ channel_state. short_to_id . remove ( & short_id) ;
1570
+ }
1571
+ ( res, Some ( chan_entry. remove_entry ( ) . 1 ) )
1572
+ } else { ( res, None ) }
1573
+ } ,
1574
+ hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
1575
+ }
1576
+ } ;
1577
+ for payment_hash in res. 2 {
1578
+ // unknown_next_peer...I dunno who that is anymore....
1579
+ self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , & payment_hash, HTLCFailReason :: Reason { failure_code : 0x4000 | 10 , data : Vec :: new ( ) } ) ;
1580
+ }
1581
+ if let Some ( chan) = chan_option {
1582
+ if let Ok ( update) = self . get_channel_update ( & chan) {
1583
+ let mut events = self . pending_events . lock ( ) . unwrap ( ) ;
1584
+ events. push ( events:: Event :: BroadcastChannelUpdate {
1585
+ msg : update
1586
+ } ) ;
1587
+ }
1588
+ }
1589
+ Ok ( ( res. 0 , res. 1 ) )
1590
+ }
1591
+
1514
1592
fn internal_announcement_signatures ( & self , their_node_id : & PublicKey , msg : & msgs:: AnnouncementSignatures ) -> Result < ( ) , MsgHandleErrInternal > {
1515
1593
let ( chan_announcement, chan_update) = {
1516
1594
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
@@ -1725,78 +1803,15 @@ impl ChannelMessageHandler for ChannelManager {
1725
1803
}
1726
1804
1727
1805
fn handle_funding_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: FundingSigned ) -> Result < ( ) , HandleError > {
1728
- let ( funding_txo, user_id, monitor) = {
1729
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1730
- match channel_state. by_id . get_mut ( & msg. channel_id ) {
1731
- Some ( chan) => {
1732
- if chan. get_their_node_id ( ) != * their_node_id {
1733
- return Err ( HandleError { err : "Got a message for a channel from the wrong node!" , action : None } )
1734
- }
1735
- let chan_monitor = chan. funding_signed ( & msg) ?;
1736
- ( chan. get_funding_txo ( ) . unwrap ( ) , chan. get_user_id ( ) , chan_monitor)
1737
- } ,
1738
- None => return Err ( HandleError { err : "Failed to find corresponding channel" , action : None } )
1739
- }
1740
- } ;
1741
- if let Err ( _e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) {
1742
- unimplemented ! ( ) ;
1743
- }
1744
- let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
1745
- pending_events. push ( events:: Event :: FundingBroadcastSafe {
1746
- funding_txo : funding_txo,
1747
- user_channel_id : user_id,
1748
- } ) ;
1749
- Ok ( ( ) )
1806
+ handle_error ! ( self , self . internal_funding_signed( their_node_id, msg) , their_node_id)
1750
1807
}
1751
1808
1752
1809
fn handle_funding_locked ( & self , their_node_id : & PublicKey , msg : & msgs:: FundingLocked ) -> Result < Option < msgs:: AnnouncementSignatures > , HandleError > {
1753
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1754
- match channel_state. by_id . get_mut ( & msg. channel_id ) {
1755
- Some ( chan) => {
1756
- if chan. get_their_node_id ( ) != * their_node_id {
1757
- return Err ( HandleError { err : "Got a message for a channel from the wrong node!" , action : None } )
1758
- }
1759
- chan. funding_locked ( & msg) ?;
1760
- return Ok ( self . get_announcement_sigs ( chan) ) ;
1761
- } ,
1762
- None => return Err ( HandleError { err : "Failed to find corresponding channel" , action : None } )
1763
- } ;
1810
+ handle_error ! ( self , self . internal_funding_locked( their_node_id, msg) , their_node_id)
1764
1811
}
1765
1812
1766
1813
fn handle_shutdown ( & self , their_node_id : & PublicKey , msg : & msgs:: Shutdown ) -> Result < ( Option < msgs:: Shutdown > , Option < msgs:: ClosingSigned > ) , HandleError > {
1767
- let ( res, chan_option) = {
1768
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1769
- let channel_state = channel_state_lock. borrow_parts ( ) ;
1770
-
1771
- match channel_state. by_id . entry ( msg. channel_id . clone ( ) ) {
1772
- hash_map:: Entry :: Occupied ( mut chan_entry) => {
1773
- if chan_entry. get ( ) . get_their_node_id ( ) != * their_node_id {
1774
- return Err ( HandleError { err : "Got a message for a channel from the wrong node!" , action : None } )
1775
- }
1776
- let res = chan_entry. get_mut ( ) . shutdown ( & * self . fee_estimator , & msg) ?;
1777
- if chan_entry. get ( ) . is_shutdown ( ) {
1778
- if let Some ( short_id) = chan_entry. get ( ) . get_short_channel_id ( ) {
1779
- channel_state. short_to_id . remove ( & short_id) ;
1780
- }
1781
- ( res, Some ( chan_entry. remove_entry ( ) . 1 ) )
1782
- } else { ( res, None ) }
1783
- } ,
1784
- hash_map:: Entry :: Vacant ( _) => return Err ( HandleError { err : "Failed to find corresponding channel" , action : None } )
1785
- }
1786
- } ;
1787
- for payment_hash in res. 2 {
1788
- // unknown_next_peer...I dunno who that is anymore....
1789
- self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , & payment_hash, HTLCFailReason :: Reason { failure_code : 0x4000 | 10 , data : Vec :: new ( ) } ) ;
1790
- }
1791
- if let Some ( chan) = chan_option {
1792
- if let Ok ( update) = self . get_channel_update ( & chan) {
1793
- let mut events = self . pending_events . lock ( ) . unwrap ( ) ;
1794
- events. push ( events:: Event :: BroadcastChannelUpdate {
1795
- msg : update
1796
- } ) ;
1797
- }
1798
- }
1799
- Ok ( ( res. 0 , res. 1 ) )
1814
+ handle_error ! ( self , self . internal_shutdown( their_node_id, msg) , their_node_id)
1800
1815
}
1801
1816
1802
1817
fn handle_closing_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: ClosingSigned ) -> Result < Option < msgs:: ClosingSigned > , HandleError > {
0 commit comments