Repository navigation
Move short_to_chan_info into standalone lock - #1639
TheBlueMatt merged 4 commits into
Conversation
| ($self: ident, $err: expr, $entry: expr, $action_type: path, $resend_channel_ready: expr, OPTIONALLY_RESEND_FUNDING_LOCKED) => { | ||
| handle_monitor_err!($self, $err, $entry, $action_type, false, false, $resend_channel_ready, Vec::new(), Vec::new(), Vec::new()) | ||
| }; | ||
| ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => { |
There was a problem hiding this comment.
Would have preferred to place this cleanup in the final commit, but keeping them in the first commit messed up the macro. I hope that's ok.
|
Realized I need to go through this closer another time, as the current consistency guarantees ensures that the channel is guaranteed to exist in the While open the pull request when I've checked this further. |
d9eb4c1 to
1da7a95
Compare
|
Added a fixup commit that ensures that the consistency guarantees that any Also rebased this on main, now that the blocking PR has been merged. |
| macro_rules! update_maps_on_chan_removal { | ||
| ($self: expr, $short_to_chan_info: expr, $channel: expr) => { | ||
| ($self: expr, $channel: expr) => { | ||
| // The lockorder for id_to_peer is prior to short_to_chan_info. |
There was a problem hiding this comment.
Sure, but there's also no reason to hold both locks at the same time here - just do self.id_to_peer.lock().unwrap().remove(&$channel.channel_id()); all in one statement at the top and then you don't have to worry about it :).
| /// SCIDs being added once the funding transaction is confirmed at the channel's required | ||
| /// confirmation depth. | ||
| /// | ||
| /// Consistancy guarantees with the `channel_state::by_id` map, guarantees that any |
There was a problem hiding this comment.
Hmm, I mean while this patch may be fine, I'd kinda prefer if we just not have guarantees to begin with - is there a reason we need to have these guarantees, or can we just not guarantee anything and handle the None case in the by_id lookups when we fetch from this?
There was a problem hiding this comment.
Ok sure I'll change to that instead :)! I did it this way mostly just to make this PR not change the current consistency guarantees, but if we're ok with changing that, I see no reason why we can't do what you suggest.
There was a problem hiding this comment.
Yea, I mean the nice thing about this PR is its relatively small-ish, and aside from argument changes, is quite reviewable, so its totally find to slip in a change like that here, as it can also make future things easier to review. Plus it seems like a more robust design anyway.
1da7a95 to
edbff4c
Compare
|
Thanks for the review @TheBlueMatt! I've addressed your feedback with the latest pushed commits. Unfortunately I had to rebase to drop the patch I made before, but I rebased on the same HEAD as before. |
| let forward_chan_id = match channel_state.short_to_chan_info.get(&short_chan_id) { | ||
| Some((_cp_id, chan_id)) => chan_id.clone(), | ||
| None => { | ||
| macro_rules! fail_pending_forwards { |
There was a problem hiding this comment.
Not super happy about this macro, and would prefer to break it out into a separate function due to the added complexity. But I figured process_pending_htlc_forwards was probably kept large with macros for efficacy reasons, so opted for the new macro instead. Let me know if my assumptions are wrong, and you'd prefer to break this out :)
There was a problem hiding this comment.
I'm not sure here, I just know that nested macros are not fun to debug 🙈
There was a problem hiding this comment.
Yeah definitely agree 😬...
| let mut valid_mpp = true; | ||
| for htlc in sources.iter() { | ||
| if let None = channel_state.as_ref().unwrap().short_to_chan_info.get(&htlc.prev_hop.short_channel_id) { | ||
| if let None = self.short_to_chan_info.read().unwrap().get(&htlc.prev_hop.short_channel_id) { |
There was a problem hiding this comment.
You need to check that the channel is available now, too. And while we're at it, let's check that the channel isn't shutting down.
| hash_map::Entry::Occupied(_) => continue, | ||
| hash_map::Entry::Vacant(_) => return scid_candidate | ||
| match short_to_chan_info.get(&scid_candidate) { | ||
| Some((_cp_id, _chan_id)) => continue, |
There was a problem hiding this comment.
| Some((_cp_id, _chan_id)) => continue, | |
| Some(_) => continue, |
| fn get_relevant_txids(&self) -> Vec<Txid> { | ||
| let channel_state = self.channel_state.lock().unwrap(); | ||
| let mut res = Vec::with_capacity(channel_state.short_to_chan_info.len()); | ||
| let mut res = Vec::with_capacity(self.short_to_chan_info.read().unwrap().len()); |
There was a problem hiding this comment.
lol let's not take a lock just for the capacity here, just use the by_id map instead, its close enough.
|
Thanks! Addressed the latest feedback with 1f01103 :) |
TheBlueMatt
left a comment
There was a problem hiding this comment.
Basically there, I think.
| let mut channel_state_lock = self.channel_state.lock().unwrap(); | ||
| let channel_state = &mut *channel_state_lock; | ||
| let chan_id = match channel_state.short_to_chan_info.get(&msg.contents.short_channel_id) { | ||
| let chan_id = match self.short_to_chan_info.read().unwrap().get(&msg.contents.short_channel_id) { |
There was a problem hiding this comment.
Given there's no consistency requirements anymore, you can move the channel_state lock down below the short_to_chan_info lock.
Codecov ReportBase: 90.79% // Head: 90.83% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #1639 +/- ##
==========================================
+ Coverage 90.79% 90.83% +0.04%
==========================================
Files 87 86 -1
Lines 47604 46422 -1182
Branches 47604 46422 -1182
==========================================
- Hits 43221 42168 -1053
+ Misses 4383 4254 -129
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
dunxen
left a comment
There was a problem hiding this comment.
Generally looks good to me. I'll try to give it another pass today, but not 100% confident with pending forwards yet :)
| let forward_chan_id = match channel_state.short_to_chan_info.get(&short_chan_id) { | ||
| Some((_cp_id, chan_id)) => chan_id.clone(), | ||
| None => { | ||
| macro_rules! fail_pending_forwards { |
There was a problem hiding this comment.
I'm not sure here, I just know that nested macros are not fun to debug 🙈
| } | ||
|
|
||
| let id = match channel_lock.short_to_chan_info.get(&path.first().unwrap().short_channel_id) { | ||
| let id = match self.short_to_chan_info.read().unwrap().get(&path.first().unwrap().short_channel_id) { |
There was a problem hiding this comment.
Looks like we should move this above the let payment_entry = ... line above, otherwise short_to_chan_info lock order also depends on pending_outbound_payments
There was a problem hiding this comment.
Oh yes, good catch! Thanks :)
Fixed with 569f172
| pub fn get_phantom_scid(&self) -> u64 { | ||
| let mut channel_state = self.channel_state.lock().unwrap(); | ||
| let short_to_chan_info = self.short_to_chan_info.read().unwrap(); | ||
| let best_block = self.best_block.read().unwrap(); |
There was a problem hiding this comment.
I think we should change this to just get the block height within a scope for use below. Otherwise a dependency between best_block and short_to_chan_info is introduced
| macro_rules! fail_pending_forwards { | ||
| () => { |
There was a problem hiding this comment.
Think you can get rid of the macro with something like:
| macro_rules! fail_pending_forwards { | |
| () => { | |
| let forward_chan_id_opt = match self.short_to_chan_info.read().unwrap().get(&short_chan_id) | |
| .map(|(_cp_id, chan_id)| chan_id.clone()); | |
| if forward_chan_id_opt.is_none() || !channel_state.by_id.contains_key(forward_chan_id_opt.unwrap()) { | |
but I haven't actually completed this patch myself
There was a problem hiding this comment.
Oh, thanks for the suggestion :)!
That change would indeed work when only taking this PR into account. Sadly though, that change would not help when taking #1507 into consideration as well, which I'd like this PR to prepare for. 1507 is quite huge, so I'd like to do as many changes as possible within this PR, to make 1507 as reviewable as possible.
In 1507, we will lock the new channel lock in this scope by using the info retrieved from short_to_chan_info (The counterparty_node_id and the channel_id). Therefore we wouldn't be able to check that both short_to_chan_info contained a value for the scid and check that the new channel lock contains the channel for the unwrapped short_to_chan_info info in the same if clause, unless we'd take the new channel lock temporarily just for the contains check. Though by taking the channel lock temporarily, we would still need the Vaccant check below for the channel lock , hence requiring the second call site for the code in fail_pending_forwards :/
There was a problem hiding this comment.
Ah okay, worth a shot!
|
I think the fixups may be out of order? Correct me if I'm wrong, but it helps me when a fixup is next to the commit it's fixing up |
41d275d to
913daf8
Compare
|
Thank you for the reviews @valentinewallace and @dunxen :)! Sorry for taking so long to address the feedback, I've been traveling.
Oh yes, you are correct. Thanks for informing me about that it helps in the review process to move up the fixup commits to the correct order. I was incorrectly under the impression that it was less confusing to not move them, but now I know better :). I'll move them up from now on. |
|
Looks like this needs rebase now :/ |
913daf8 to
3e3d954
Compare
|
Rebased on upstream without changes except fixing minor merge conflicts. |
| $short_to_chan_info.remove(&$channel.outbound_scid_alias()); | ||
| ($self: expr, $channel: expr) => { | ||
| { | ||
| let mut id_to_peer = $self.id_to_peer.lock().unwrap(); |
There was a problem hiding this comment.
nit: why not keep the line as-is previously vs assigning the mutexguard? Then you can drop the scope (as temporaries are drop'd automatically at the ;).
There was a problem hiding this comment.
Good question 😅 Fixed with: e47fc06
Hmm did this fix end up getting dropped?
There was a problem hiding this comment.
Yeah I moved this change to the follow-up PR #1795. It's getting a bit unnecessary with a separate PR for the follow-ups now that I'm already doing some small fixes in this PR hah, so I will close that PR and bring the fixups into this PR.
There was a problem hiding this comment.
Understood, since this is basically mergeable I support keeping this fix in followup! But up to you
There was a problem hiding this comment.
Sorry missed this until I had already pushed the followups with this PR yesterday. Hope that's ok :)
| if let Some((err, code, chan_update)) = loop { | ||
| let mut channel_state = self.channel_state.lock().unwrap(); | ||
| let id_option = channel_state.short_to_chan_info.get(&short_channel_id).cloned(); | ||
| let id_option = self.short_to_chan_info.read().unwrap().get(&short_channel_id).cloned(); |
There was a problem hiding this comment.
This should be above the channel_state lock, no?
There was a problem hiding this comment.
Yeah sure :)! Sadly we're already holding the channel_state when taking the short_to_chan_info lock in multiple other places, but might as well fix that here.
| let err: Result<(), _> = loop { | ||
| let mut channel_lock = self.channel_state.lock().unwrap(); | ||
|
|
||
| let id = match self.short_to_chan_info.read().unwrap().get(&path.first().unwrap().short_channel_id) { |
There was a problem hiding this comment.
Should short_to_chan_info comment be updated in consequence "Locked after channel_state" ?
There was a problem hiding this comment.
Unfortunately we can't remove the lock order requirement for short_to_chan_info to be locked after the channel_state lock with this PR, as it's still required in multiple other places in the code, but with #1507 we'll be able to remove that lock order however :).
I think that this order change here and above mainly a preparation for that PR, and is ok here since we only take the short_to_chan_info lock temporarily and drop it before taking the channel_state lock.
I agree though that this is a bit confusing, and if you'd like, I could remove this temporary order change.
There was a problem hiding this comment.
Even with the order, if this hunk were moved above the channel_lock it would be fine - because we take the read lock and then copy out of it the read lock is dropped at the semicolon after the match, which means no lock order would be implied. Not a big deal, though, its fine to leave it for now, just nice to avoid holding both locks at the same time if we can avoid it.
3e3d954 to
9e341c8
Compare
|
Addressed the latest feedback, and restricted the access for the |
| let err: Result<(), _> = loop { | ||
| let mut channel_lock = self.channel_state.lock().unwrap(); | ||
|
|
||
| let id = match self.short_to_chan_info.read().unwrap().get(&path.first().unwrap().short_channel_id) { |
There was a problem hiding this comment.
Should short_to_chan_info comment be updated in consequence "Locked after channel_state" ?
| let err: Result<(), _> = loop { | ||
| let mut channel_lock = self.channel_state.lock().unwrap(); | ||
| let id = match self.short_to_chan_info.read().unwrap().get(&path.first().unwrap().short_channel_id) { | ||
| None => return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()}), |
There was a problem hiding this comment.
Note, error message could be "No channel found with first-hop", historically ChannelUnavailable has been used to flag a channel !is.live(), e.g when the monitor is not ready. I think it's more accurate to indicate non-availability in a way different than non-presence.
There was a problem hiding this comment.
Ah ok, I didn't know this, so thanks for informing me! Is it mainly the error message that we'd like to change, or would we like to create a new APIError variant as well?
| } | ||
| } else { unreachable!(); } | ||
| } else { | ||
| return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()}); |
There was a problem hiding this comment.
Could recall the lack of consistency between short_to_chan_info and by_id, what we hit afaiu. Or move this indication as a general comment of short_to_chan_info.
There was a problem hiding this comment.
Hmm I'm not sure if you mean that we'd like to change the error message here to indicate to the user the lack between the short_to_chan_info and the by_id maps, or if you mean that we should just add a code comment here to indicate it. I don't think we should change the message to surface the reason for the problem to the user, as neither the short_to_chan_info nor the by_id map are public to the user, and it would therefore make no sense to refer to them in the message.
I'll add a code comment here to note why this was triggered :). See: a8b9fd0
| let forward_chan_id = match channel_state.short_to_chan_info.get(&short_chan_id) { | ||
| Some((_cp_id, chan_id)) => chan_id.clone(), | ||
| None => { | ||
| macro_rules! fail_pending_forwards { |
There was a problem hiding this comment.
Note, I think of the phantom invoices handling logic is actually in this branch (L3168-3193), so I'm not sure about qualifying it as "fail_pending_forwards" as in some case we might fetch the phantom payment.
Not necessarily in that PR, but I think it could be interesting to break up process_pending_htlc_forwards in few smaller helpers, especially as here we're starting to reuse some code paths.
There was a problem hiding this comment.
Ah good point! Perhaps we'd like to name the macro to something else than fail_pending_forwards then? I'll rename it to forwarding_channel_not_found temporarily, but if you have a better suggestion let me know :).
I also agree that process_pending_htlc_forwards is starting to get very long and complicated, and would be nice to brake up if possible. Adding this macro also makes everything more complicated and harder to debug.
Name changed with: c95961e
| // enforce option_scid_alias then), and if the funding tx is ever | ||
| // un-confirmed we force-close the channel, ensuring short_to_chan_info | ||
| // is always consistent. | ||
| let mut short_to_chan_info = self.short_to_chan_info.write().unwrap(); |
There was a problem hiding this comment.
Still confused here by the what is the correct lock order between short_to_chan_info and channel_state one L5895
TheBlueMatt
left a comment
There was a problem hiding this comment.
This LGTM. Please squash and lets land it!
| // Note that we may send a `channel_ready` multiple times for a channel if we reconnect, so | ||
| // we allow collisions, but we shouldn't ever be updating the channel ID pointed to. | ||
| let outbound_alias_insert = $short_to_chan_info.insert($channel.outbound_scid_alias(), ($channel.get_counterparty_node_id(), $channel.channel_id())); | ||
| let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap(); |
There was a problem hiding this comment.
nit: Can we scope this lock (or the macro, by adding extra {}s in the macro)? I'm not sure its required but I also don't really understand the rules for drop when leaving a macro.
There was a problem hiding this comment.
Fixed with the latest push. I didn't push in a separate fixup commit, hope that's ok!
| let err: Result<(), _> = loop { | ||
| let mut channel_lock = self.channel_state.lock().unwrap(); | ||
|
|
||
| let id = match self.short_to_chan_info.read().unwrap().get(&path.first().unwrap().short_channel_id) { |
There was a problem hiding this comment.
Even with the order, if this hunk were moved above the channel_lock it would be fine - because we take the read lock and then copy out of it the read lock is dropped at the semicolon after the match, which means no lock order would be implied. Not a big deal, though, its fine to leave it for now, just nice to avoid holding both locks at the same time if we can avoid it.
1ea75e3 to
590eae3
Compare
|
Squashed and addressed latest feedback :)! |
valentinewallace
left a comment
There was a problem hiding this comment.
LGTM, feel free to take or leave nits
| $short_to_chan_info.remove(&$channel.outbound_scid_alias()); | ||
| ($self: expr, $channel: expr) => { | ||
| { | ||
| let mut id_to_peer = $self.id_to_peer.lock().unwrap(); |
There was a problem hiding this comment.
Good question 😅 Fixed with: e47fc06
Hmm did this fix end up getting dropped?
|
Thanks @dunxen & @valentinewallace. |
a38c9c4
590eae3 to
a38c9c4
Compare
|
Addressed the latest feedback, and also brought in the followups from #1795 into this PR (ie. scope fix for I fixed up these changes into the corresponding commit already as they were minor. I hope that's ok :) |
|
LGTM after rebase |
a38c9c4 to
acf17e7
Compare
|
Rebased on main. The only changes were merge conflict fixes 🚀! |
|
Grrrrr, I'm sorry, this was dumb #1639 (comment) . We should not be checking I think what i was thinking is we should check if the peer is online before claiming, which, like, maybe, but its not critical, and we'd need to check specifically only for if they're connected. |
acf17e7 to
07664c2
Compare
|
Removed the |
As the `channel_state` (`ChannelHolder`) struct will be removed, this commit moves the `short_to_chan_info` map from that lock into a seperate lock.
As the `short_to_chan_info` has been moved out of the `channel_state` to a standalone lock, several macros no longer need the `channel_state` passed into the macro.
As the `short_to_chan_info` map has been removed from the `channel_state`, there is no longer any consistency guarantees between the `by_id` and `short_to_chan_info` maps. This commit ensures that we don't force unwrap channels where the channel_id has been queried from the `short_to_chan_info` map.
Refactor `process_pending_htlc_forwards` to ensure that both branches that fails `pending_forwards` are placed next to eachother for improved readability.
07664c2 to
b368941
Compare
|
Sorry, reverted once more to just remove the |
Based on #1638 and a prerequisite for #1507.
As the
channel_state(ChannelHolder) struct will be removed, this commit moves theshort_to_chan_infomap from that lock into a separate lock, and cleans up a few macros that can be cleaned up due to moving it into a separate lock.