Skip to content

Commit cf6532e

Browse files
authored
Merge pull request #97 from TheBlueMatt/2018-07-no-useless-preimages
Stop adding remote's payment_preimages to our channel monitor
2 parents e571c7a + 1051e53 commit cf6532e

File tree

2 files changed

+33
-51
lines changed

2 files changed

+33
-51
lines changed

src/ln/channel.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ impl Channel {
985985
}
986986

987987
let htlc_id = {
988-
let mut htlc = &mut self.pending_htlcs[pending_idx];
988+
let htlc = &mut self.pending_htlcs[pending_idx];
989989
if htlc.state == HTLCState::Committed {
990990
htlc.state = HTLCState::LocalRemoved;
991991
htlc.local_removed_fulfilled = true;
@@ -1356,7 +1356,7 @@ impl Channel {
13561356
Err(HandleError{err: "Remote tried to fulfill/fail an HTLC we couldn't find", action: None})
13571357
}
13581358

1359-
pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<ChannelMonitor, HandleError> {
1359+
pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<(), HandleError> {
13601360
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
13611361
return Err(HandleError{err: "Got add HTLC message when channel was not in an operational state", action: None});
13621362
}
@@ -1366,9 +1366,8 @@ impl Channel {
13661366
let mut payment_hash = [0; 32];
13671367
sha.result(&mut payment_hash);
13681368

1369-
self.channel_monitor.provide_payment_preimage(&payment_hash, &msg.payment_preimage);
13701369
self.mark_outbound_htlc_removed(msg.htlc_id, Some(payment_hash), None)?;
1371-
Ok(self.channel_monitor.clone())
1370+
Ok(())
13721371
}
13731372

13741373
pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<[u8; 32], HandleError> {

src/ln/channelmanager.rs

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,15 @@ impl ChannelHolder {
137137
by_id: &mut self.by_id,
138138
short_to_id: &mut self.short_to_id,
139139
next_forward: &mut self.next_forward,
140-
/// short channel id -> forward infos. Key of 0 means payments received
141140
forward_htlcs: &mut self.forward_htlcs,
142141
claimable_htlcs: &mut self.claimable_htlcs,
143142
}
144143
}
145144
}
146145

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+
147149
/// Manager which keeps track of a number of channels and sends messages to the appropriate
148150
/// channel, also tracking HTLC preimages and forwarding onion packets appropriately.
149151
/// Implements ChannelMessageHandler, handling the multi-channel parts and passing things through
@@ -157,7 +159,7 @@ pub struct ChannelManager {
157159

158160
announce_channels_publicly: bool,
159161
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,
161163
secp_ctx: Secp256k1,
162164

163165
channel_state: Mutex<ChannelHolder>,
@@ -388,7 +390,7 @@ impl ChannelManager {
388390
let mut chan = {
389391
let mut channel_state_lock = self.channel_state.lock().unwrap();
390392
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) {
392394
if let Some(short_id) = chan.get_short_channel_id() {
393395
channel_state.short_to_id.remove(&short_id);
394396
}
@@ -1135,9 +1137,9 @@ impl ChainListener for ChannelManager {
11351137
let mut new_events = Vec::new();
11361138
let mut failed_channels = Vec::new();
11371139
{
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;
11411143
channel_state.by_id.retain(|_, channel| {
11421144
if let Some(funding_locked) = channel.block_connected(header, height, txn_matched, indexes_of_txn_matched) {
11431145
let announcement_sigs = match self.get_announcement_sigs(channel) {
@@ -1152,14 +1154,14 @@ impl ChainListener for ChannelManager {
11521154
msg: funding_locked,
11531155
announcement_sigs: announcement_sigs
11541156
});
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());
11561158
}
11571159
if let Some(funding_txo) = channel.get_funding_txo() {
11581160
for tx in txn_matched {
11591161
for inp in tx.input.iter() {
11601162
if inp.prev_hash == funding_txo.txid && inp.prev_index == funding_txo.index as u32 {
11611163
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);
11631165
}
11641166
// It looks like our counterparty went on-chain. We go ahead and
11651167
// broadcast our latest local state as well here, just in case its
@@ -1177,7 +1179,7 @@ impl ChainListener for ChannelManager {
11771179
}
11781180
if channel.channel_monitor().would_broadcast_at_height(height) {
11791181
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);
11811183
}
11821184
failed_channels.push(channel.force_shutdown());
11831185
// If would_broadcast_at_height() is true, the channel_monitor will broadcast
@@ -1193,12 +1195,6 @@ impl ChainListener for ChannelManager {
11931195
}
11941196
true
11951197
});
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-
}
12021198
}
12031199
for failure in failed_channels.drain(..) {
12041200
self.finish_force_close_channel(failure);
@@ -1628,20 +1624,14 @@ impl ChannelMessageHandler for ChannelManager {
16281624
// destination. That's OK since those nodes are probably busted or trying to do network
16291625
// mapping through repeated loops. In either case, we want them to stop talking to us, so
16301626
// 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+
}
16451635
}
16461636

16471637
let (source_short_channel_id, res) = match channel_state.by_id.get_mut(&msg.channel_id) {
@@ -1692,22 +1682,16 @@ impl ChannelMessageHandler for ChannelManager {
16921682
// is broken, we may have enough info to get our own money!
16931683
self.claim_funds_internal(msg.payment_preimage.clone(), false);
16941684

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})
17091694
}
1710-
Ok(())
17111695
}
17121696

17131697
fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) -> Result<Option<msgs::HTLCFailChannelUpdate>, HandleError> {
@@ -2502,10 +2486,9 @@ mod tests {
25022486
{
25032487
let mut added_monitors = $node.chan_monitor.added_monitors.lock().unwrap();
25042488
if $last_node {
2505-
assert_eq!(added_monitors.len(), 1);
2489+
assert_eq!(added_monitors.len(), 0);
25062490
} 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);
25092492
}
25102493
added_monitors.clear();
25112494
}

0 commit comments

Comments
 (0)