Skip to content

Commit 2018782

Browse files
authored
Merge pull request #16 from TheBlueMatt/2018-03-fixups
Assorted fixes/cleanups
2 parents 511c531 + 93f47f5 commit 2018782

File tree

5 files changed

+473
-261
lines changed

5 files changed

+473
-261
lines changed

fuzz/fuzz_targets/channel_target.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ pub fn do_test(data: &[u8]) {
114114
msgs::DecodeError::UnknownRealmByte => return,
115115
msgs::DecodeError::BadPublicKey => return,
116116
msgs::DecodeError::BadSignature => return,
117+
msgs::DecodeError::ExtraAddressesPerType => return,
117118
msgs::DecodeError::WrongLength => panic!("We picked the length..."),
118119
}
119120
}
@@ -133,6 +134,7 @@ pub fn do_test(data: &[u8]) {
133134
msgs::DecodeError::UnknownRealmByte => return,
134135
msgs::DecodeError::BadPublicKey => return,
135136
msgs::DecodeError::BadSignature => return,
137+
msgs::DecodeError::ExtraAddressesPerType => return,
136138
msgs::DecodeError::WrongLength => panic!("We picked the length..."),
137139
}
138140
}

src/ln/channel.rs

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ pub struct Channel {
211211
channel_monitor: ChannelMonitor,
212212
}
213213

214-
const OUR_MAX_HTLCS: u16 = 1; //TODO
214+
const OUR_MAX_HTLCS: u16 = 5; //TODO
215215
const CONF_TARGET: u32 = 12; //TODO: Should be much higher
216216
/// Confirmation count threshold at which we close a channel. Ideally we'd keep the channel around
217217
/// on ice until the funding transaction gets more confirmations, but the LN protocol doesn't
@@ -765,6 +765,7 @@ impl Channel {
765765

766766
let mut htlc_id = 0;
767767
let mut htlc_amount_msat = 0;
768+
//TODO: swap_remove since we dont need to maintain ordering here
768769
self.pending_htlcs.retain(|ref htlc| {
769770
if !htlc.outbound && htlc.payment_hash == payment_hash {
770771
if htlc_id != 0 {
@@ -796,6 +797,7 @@ impl Channel {
796797

797798
let mut htlc_id = 0;
798799
let mut htlc_amount_msat = 0;
800+
//TODO: swap_remove since we dont need to maintain ordering here
799801
self.pending_htlcs.retain(|ref htlc| {
800802
if !htlc.outbound && htlc.payment_hash == *payment_hash {
801803
if htlc_id != 0 {
@@ -1103,23 +1105,7 @@ impl Channel {
11031105
}
11041106
}
11051107

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> {
11231109
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
11241110
return Err(HandleError{err: "Got add HTLC message when channel was not in an operational state", msg: None});
11251111
}
@@ -1137,11 +1123,10 @@ impl Channel {
11371123
self.value_to_self_msat -= htlc.amount_msat;
11381124
}
11391125
}
1140-
1141-
self.check_and_free_holding_cell_htlcs()
1126+
Ok(())
11421127
}
11431128

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> {
11451130
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
11461131
return Err(HandleError{err: "Got add HTLC message when channel was not in an operational state", msg: None});
11471132
}
@@ -1154,12 +1139,10 @@ impl Channel {
11541139
htlc.payment_hash
11551140
}
11561141
};
1157-
1158-
let holding_cell_freedom = self.check_and_free_holding_cell_htlcs()?;
1159-
Ok((payment_hash, holding_cell_freedom))
1142+
Ok(payment_hash)
11601143
}
11611144

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> {
11631146
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
11641147
return Err(HandleError{err: "Got add HTLC message when channel was not in an operational state", msg: None});
11651148
}
@@ -1172,9 +1155,7 @@ impl Channel {
11721155
htlc.payment_hash
11731156
}
11741157
};
1175-
1176-
let holding_cell_freedom = self.check_and_free_holding_cell_htlcs()?;
1177-
Ok((payment_hash, holding_cell_freedom))
1158+
Ok(payment_hash)
11781159
}
11791160

11801161
pub fn commitment_signed(&mut self, msg: &msgs::CommitmentSigned) -> Result<(msgs::RevokeAndACK, Vec<PendingForwardHTLCInfo>), HandleError> {
@@ -1258,7 +1239,7 @@ impl Channel {
12581239
if self.channel_outbound {
12591240
return Err(HandleError{err: "Non-funding remote tried to update channel fee", msg: None});
12601241
}
1261-
Channel::check_remote_fee(fee_estimator, msg.feerate_per_kw).unwrap();
1242+
Channel::check_remote_fee(fee_estimator, msg.feerate_per_kw)?;
12621243
self.feerate_per_kw = msg.feerate_per_kw as u64;
12631244
Ok(())
12641245
}
@@ -1640,6 +1621,19 @@ impl Channel {
16401621
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
16411622
return Err(HandleError{err: "Cannot create commitment tx until channel is fully established", msg: None});
16421623
}
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+
}
16431637

16441638
let funding_script = self.get_funding_redeemscript();
16451639

0 commit comments

Comments
 (0)