@@ -211,7 +211,7 @@ pub struct Channel {
211
211
channel_monitor : ChannelMonitor ,
212
212
}
213
213
214
- const OUR_MAX_HTLCS : u16 = 1 ; //TODO
214
+ const OUR_MAX_HTLCS : u16 = 5 ; //TODO
215
215
const CONF_TARGET : u32 = 12 ; //TODO: Should be much higher
216
216
/// Confirmation count threshold at which we close a channel. Ideally we'd keep the channel around
217
217
/// on ice until the funding transaction gets more confirmations, but the LN protocol doesn't
@@ -765,6 +765,7 @@ impl Channel {
765
765
766
766
let mut htlc_id = 0 ;
767
767
let mut htlc_amount_msat = 0 ;
768
+ //TODO: swap_remove since we dont need to maintain ordering here
768
769
self . pending_htlcs . retain ( |ref htlc| {
769
770
if !htlc. outbound && htlc. payment_hash == payment_hash {
770
771
if htlc_id != 0 {
@@ -796,6 +797,7 @@ impl Channel {
796
797
797
798
let mut htlc_id = 0 ;
798
799
let mut htlc_amount_msat = 0 ;
800
+ //TODO: swap_remove since we dont need to maintain ordering here
799
801
self . pending_htlcs . retain ( |ref htlc| {
800
802
if !htlc. outbound && htlc. payment_hash == * payment_hash {
801
803
if htlc_id != 0 {
@@ -1103,23 +1105,7 @@ impl Channel {
1103
1105
}
1104
1106
}
1105
1107
1106
- /// Checks if there are any LocalAnnounced HTLCs remaining and sets
1107
- /// ChannelState::AwaitingRemoteRevoke accordingly, possibly calling free_holding_cell_htlcs.
1108
- fn check_and_free_holding_cell_htlcs ( & mut self ) -> Result < Option < ( Vec < msgs:: UpdateAddHTLC > , msgs:: CommitmentSigned ) > , HandleError > {
1109
- if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == ( ChannelState :: AwaitingRemoteRevoke as u32 ) {
1110
- for htlc in self . pending_htlcs . iter ( ) {
1111
- if htlc. state == HTLCState :: LocalAnnounced {
1112
- return Ok ( None ) ;
1113
- }
1114
- }
1115
- self . channel_state &= !( ChannelState :: AwaitingRemoteRevoke as u32 ) ;
1116
- self . free_holding_cell_htlcs ( )
1117
- } else {
1118
- Ok ( None )
1119
- }
1120
- }
1121
-
1122
- pub fn update_fulfill_htlc ( & mut self , msg : & msgs:: UpdateFulfillHTLC ) -> Result < Option < ( Vec < msgs:: UpdateAddHTLC > , msgs:: CommitmentSigned ) > , HandleError > {
1108
+ pub fn update_fulfill_htlc ( & mut self , msg : & msgs:: UpdateFulfillHTLC ) -> Result < ( ) , HandleError > {
1123
1109
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1124
1110
return Err ( HandleError { err : "Got add HTLC message when channel was not in an operational state" , msg : None } ) ;
1125
1111
}
@@ -1137,11 +1123,10 @@ impl Channel {
1137
1123
self . value_to_self_msat -= htlc. amount_msat ;
1138
1124
}
1139
1125
}
1140
-
1141
- self . check_and_free_holding_cell_htlcs ( )
1126
+ Ok ( ( ) )
1142
1127
}
1143
1128
1144
- pub fn update_fail_htlc ( & mut self , msg : & msgs:: UpdateFailHTLC ) -> Result < ( [ u8 ; 32 ] , Option < ( Vec < msgs :: UpdateAddHTLC > , msgs :: CommitmentSigned ) > ) , HandleError > {
1129
+ pub fn update_fail_htlc ( & mut self , msg : & msgs:: UpdateFailHTLC ) -> Result < [ u8 ; 32 ] , HandleError > {
1145
1130
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1146
1131
return Err ( HandleError { err : "Got add HTLC message when channel was not in an operational state" , msg : None } ) ;
1147
1132
}
@@ -1154,12 +1139,10 @@ impl Channel {
1154
1139
htlc. payment_hash
1155
1140
}
1156
1141
} ;
1157
-
1158
- let holding_cell_freedom = self . check_and_free_holding_cell_htlcs ( ) ?;
1159
- Ok ( ( payment_hash, holding_cell_freedom) )
1142
+ Ok ( payment_hash)
1160
1143
}
1161
1144
1162
- pub fn update_fail_malformed_htlc ( & mut self , msg : & msgs:: UpdateFailMalformedHTLC ) -> Result < ( [ u8 ; 32 ] , Option < ( Vec < msgs :: UpdateAddHTLC > , msgs :: CommitmentSigned ) > ) , HandleError > {
1145
+ pub fn update_fail_malformed_htlc ( & mut self , msg : & msgs:: UpdateFailMalformedHTLC ) -> Result < [ u8 ; 32 ] , HandleError > {
1163
1146
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1164
1147
return Err ( HandleError { err : "Got add HTLC message when channel was not in an operational state" , msg : None } ) ;
1165
1148
}
@@ -1172,9 +1155,7 @@ impl Channel {
1172
1155
htlc. payment_hash
1173
1156
}
1174
1157
} ;
1175
-
1176
- let holding_cell_freedom = self . check_and_free_holding_cell_htlcs ( ) ?;
1177
- Ok ( ( payment_hash, holding_cell_freedom) )
1158
+ Ok ( payment_hash)
1178
1159
}
1179
1160
1180
1161
pub fn commitment_signed ( & mut self , msg : & msgs:: CommitmentSigned ) -> Result < ( msgs:: RevokeAndACK , Vec < PendingForwardHTLCInfo > ) , HandleError > {
@@ -1258,7 +1239,7 @@ impl Channel {
1258
1239
if self . channel_outbound {
1259
1240
return Err ( HandleError { err : "Non-funding remote tried to update channel fee" , msg : None } ) ;
1260
1241
}
1261
- Channel :: check_remote_fee ( fee_estimator, msg. feerate_per_kw ) . unwrap ( ) ;
1242
+ Channel :: check_remote_fee ( fee_estimator, msg. feerate_per_kw ) ? ;
1262
1243
self . feerate_per_kw = msg. feerate_per_kw as u64 ;
1263
1244
Ok ( ( ) )
1264
1245
}
@@ -1640,6 +1621,19 @@ impl Channel {
1640
1621
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1641
1622
return Err ( HandleError { err : "Cannot create commitment tx until channel is fully established" , msg : None } ) ;
1642
1623
}
1624
+ if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == ( ChannelState :: AwaitingRemoteRevoke as u32 ) {
1625
+ return Err ( HandleError { err : "Cannot create commitment tx until remote revokes their previous commitment" , msg : None } ) ;
1626
+ }
1627
+ let mut have_updates = false ; // TODO initialize with "have we sent a fee update?"
1628
+ for htlc in self . pending_htlcs . iter ( ) {
1629
+ if htlc. state == HTLCState :: LocalAnnounced {
1630
+ have_updates = true ;
1631
+ }
1632
+ if have_updates { break ; }
1633
+ }
1634
+ if !have_updates {
1635
+ return Err ( HandleError { err : "Cannot create commitment tx until we have some updates to send" , msg : None } ) ;
1636
+ }
1643
1637
1644
1638
let funding_script = self . get_funding_redeemscript ( ) ;
1645
1639
0 commit comments