@@ -137,13 +137,15 @@ impl ChannelHolder {
137
137
by_id : & mut self . by_id ,
138
138
short_to_id : & mut self . short_to_id ,
139
139
next_forward : & mut self . next_forward ,
140
- /// short channel id -> forward infos. Key of 0 means payments received
141
140
forward_htlcs : & mut self . forward_htlcs ,
142
141
claimable_htlcs : & mut self . claimable_htlcs ,
143
142
}
144
143
}
145
144
}
146
145
146
+ #[ cfg( not( any( target_pointer_width = "32" , target_pointer_width = "64" ) ) ) ]
147
+ const ERR : ( ) = "You need at least 32 bit pointers (well, usize, but we'll assume they're the same) for ChannelManager::latest_block_height" ;
148
+
147
149
/// Manager which keeps track of a number of channels and sends messages to the appropriate
148
150
/// channel, also tracking HTLC preimages and forwarding onion packets appropriately.
149
151
/// Implements ChannelMessageHandler, handling the multi-channel parts and passing things through
@@ -157,7 +159,7 @@ pub struct ChannelManager {
157
159
158
160
announce_channels_publicly : bool ,
159
161
fee_proportional_millionths : u32 ,
160
- latest_block_height : AtomicUsize , //TODO: Compile-time assert this is at least 32-bits long
162
+ latest_block_height : AtomicUsize ,
161
163
secp_ctx : Secp256k1 ,
162
164
163
165
channel_state : Mutex < ChannelHolder > ,
@@ -388,7 +390,7 @@ impl ChannelManager {
388
390
let mut chan = {
389
391
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
390
392
let channel_state = channel_state_lock. borrow_parts ( ) ;
391
- if let Some ( mut chan) = channel_state. by_id . remove ( channel_id) {
393
+ if let Some ( chan) = channel_state. by_id . remove ( channel_id) {
392
394
if let Some ( short_id) = chan. get_short_channel_id ( ) {
393
395
channel_state. short_to_id . remove ( & short_id) ;
394
396
}
@@ -1135,9 +1137,9 @@ impl ChainListener for ChannelManager {
1135
1137
let mut new_events = Vec :: new ( ) ;
1136
1138
let mut failed_channels = Vec :: new ( ) ;
1137
1139
{
1138
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1139
- let mut short_to_ids_to_insert = Vec :: new ( ) ;
1140
- let mut short_to_ids_to_remove = Vec :: new ( ) ;
1140
+ let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1141
+ let channel_state = channel_lock . borrow_parts ( ) ;
1142
+ let short_to_id = channel_state . short_to_id ;
1141
1143
channel_state. by_id . retain ( |_, channel| {
1142
1144
if let Some ( funding_locked) = channel. block_connected ( header, height, txn_matched, indexes_of_txn_matched) {
1143
1145
let announcement_sigs = match self . get_announcement_sigs ( channel) {
@@ -1152,14 +1154,14 @@ impl ChainListener for ChannelManager {
1152
1154
msg : funding_locked,
1153
1155
announcement_sigs : announcement_sigs
1154
1156
} ) ;
1155
- short_to_ids_to_insert . push ( ( channel. get_short_channel_id ( ) . unwrap ( ) , channel. channel_id ( ) ) ) ;
1157
+ short_to_id . insert ( channel. get_short_channel_id ( ) . unwrap ( ) , channel. channel_id ( ) ) ;
1156
1158
}
1157
1159
if let Some ( funding_txo) = channel. get_funding_txo ( ) {
1158
1160
for tx in txn_matched {
1159
1161
for inp in tx. input . iter ( ) {
1160
1162
if inp. prev_hash == funding_txo. txid && inp. prev_index == funding_txo. index as u32 {
1161
1163
if let Some ( short_id) = channel. get_short_channel_id ( ) {
1162
- short_to_ids_to_remove . push ( short_id) ;
1164
+ short_to_id . remove ( & short_id) ;
1163
1165
}
1164
1166
// It looks like our counterparty went on-chain. We go ahead and
1165
1167
// broadcast our latest local state as well here, just in case its
@@ -1177,7 +1179,7 @@ impl ChainListener for ChannelManager {
1177
1179
}
1178
1180
if channel. channel_monitor ( ) . would_broadcast_at_height ( height) {
1179
1181
if let Some ( short_id) = channel. get_short_channel_id ( ) {
1180
- short_to_ids_to_remove . push ( short_id) ;
1182
+ short_to_id . remove ( & short_id) ;
1181
1183
}
1182
1184
failed_channels. push ( channel. force_shutdown ( ) ) ;
1183
1185
// If would_broadcast_at_height() is true, the channel_monitor will broadcast
@@ -1193,12 +1195,6 @@ impl ChainListener for ChannelManager {
1193
1195
}
1194
1196
true
1195
1197
} ) ;
1196
- for to_remove in short_to_ids_to_remove {
1197
- channel_state. short_to_id . remove ( & to_remove) ;
1198
- }
1199
- for to_insert in short_to_ids_to_insert {
1200
- channel_state. short_to_id . insert ( to_insert. 0 , to_insert. 1 ) ;
1201
- }
1202
1198
}
1203
1199
for failure in failed_channels. drain ( ..) {
1204
1200
self . finish_force_close_channel ( failure) ;
@@ -1628,20 +1624,14 @@ impl ChannelMessageHandler for ChannelManager {
1628
1624
// destination. That's OK since those nodes are probably busted or trying to do network
1629
1625
// mapping through repeated loops. In either case, we want them to stop talking to us, so
1630
1626
// we send permanent_node_failure.
1631
- match & claimable_htlcs_entry {
1632
- & hash_map:: Entry :: Occupied ( ref e) => {
1633
- let mut acceptable_cycle = false ;
1634
- match e. get ( ) {
1635
- & PendingOutboundHTLC :: OutboundRoute { .. } => {
1636
- acceptable_cycle = pending_forward_info. short_channel_id == 0 ;
1637
- } ,
1638
- _ => { } ,
1639
- }
1640
- if !acceptable_cycle {
1641
- return_err ! ( "Payment looped through us twice" , 0x4000 | 0x2000 | 2 , & [ 0 ; 0 ] ) ;
1642
- }
1643
- } ,
1644
- _ => { } ,
1627
+ if let & hash_map:: Entry :: Occupied ( ref e) = & claimable_htlcs_entry {
1628
+ let mut acceptable_cycle = false ;
1629
+ if let & PendingOutboundHTLC :: OutboundRoute { .. } = e. get ( ) {
1630
+ acceptable_cycle = pending_forward_info. short_channel_id == 0 ;
1631
+ }
1632
+ if !acceptable_cycle {
1633
+ return_err ! ( "Payment looped through us twice" , 0x4000 | 0x2000 | 2 , & [ 0 ; 0 ] ) ;
1634
+ }
1645
1635
}
1646
1636
1647
1637
let ( source_short_channel_id, res) = match channel_state. by_id . get_mut ( & msg. channel_id ) {
@@ -1692,22 +1682,16 @@ impl ChannelMessageHandler for ChannelManager {
1692
1682
// is broken, we may have enough info to get our own money!
1693
1683
self . claim_funds_internal ( msg. payment_preimage . clone ( ) , false ) ;
1694
1684
1695
- let monitor = {
1696
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1697
- match channel_state. by_id . get_mut ( & msg. channel_id ) {
1698
- Some ( chan) => {
1699
- if chan. get_their_node_id ( ) != * their_node_id {
1700
- return Err ( HandleError { err : "Got a message for a channel from the wrong node!" , action : None } )
1701
- }
1702
- chan. update_fulfill_htlc ( & msg) ?
1703
- } ,
1704
- None => return Err ( HandleError { err : "Failed to find corresponding channel" , action : None } )
1705
- }
1706
- } ;
1707
- if let Err ( _e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) {
1708
- unimplemented ! ( ) ;
1685
+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1686
+ match channel_state. by_id . get_mut ( & msg. channel_id ) {
1687
+ Some ( chan) => {
1688
+ if chan. get_their_node_id ( ) != * their_node_id {
1689
+ return Err ( HandleError { err : "Got a message for a channel from the wrong node!" , action : None } )
1690
+ }
1691
+ chan. update_fulfill_htlc ( & msg)
1692
+ } ,
1693
+ None => return Err ( HandleError { err : "Failed to find corresponding channel" , action : None } )
1709
1694
}
1710
- Ok ( ( ) )
1711
1695
}
1712
1696
1713
1697
fn handle_update_fail_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFailHTLC ) -> Result < Option < msgs:: HTLCFailChannelUpdate > , HandleError > {
@@ -2502,10 +2486,9 @@ mod tests {
2502
2486
{
2503
2487
let mut added_monitors = $node. chan_monitor. added_monitors. lock( ) . unwrap( ) ;
2504
2488
if $last_node {
2505
- assert_eq!( added_monitors. len( ) , 1 ) ;
2489
+ assert_eq!( added_monitors. len( ) , 0 ) ;
2506
2490
} else {
2507
- assert_eq!( added_monitors. len( ) , 2 ) ;
2508
- assert!( added_monitors[ 0 ] . 0 != added_monitors[ 1 ] . 0 ) ;
2491
+ assert_eq!( added_monitors. len( ) , 1 ) ;
2509
2492
}
2510
2493
added_monitors. clear( ) ;
2511
2494
}
0 commit comments