-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
multi: use more efficient key to avoid hashing the blob. #9231
Open
ziggie1984
wants to merge
2
commits into
lightningnetwork:master
Choose a base branch
from
ziggie1984:active-htlc-fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2472,15 +2472,18 @@ func (c *OpenChannel) ActiveHtlcs() []HTLC { | |
|
||
// We'll only return HTLC's that are locked into *both* commitment | ||
// transactions. So we'll iterate through their set of HTLC's to note | ||
// which ones are present on their commitment. | ||
remoteHtlcs := make(map[[32]byte]struct{}) | ||
// which ones are present on their commitment. We combine the RHash and | ||
// the HTLC index to create a unique key. | ||
remoteHtlcs := make(map[[32 + 8]byte]struct{}) | ||
var htlcKey [32 + 8]byte | ||
for _, htlc := range c.RemoteCommitment.Htlcs { | ||
log.Tracef("RemoteCommitment has htlc: id=%v, update=%v "+ | ||
"incoming=%v", htlc.HtlcIndex, htlc.LogIndex, | ||
htlc.Incoming) | ||
|
||
onionHash := sha256.Sum256(htlc.OnionBlob[:]) | ||
remoteHtlcs[onionHash] = struct{}{} | ||
copy(htlcKey[:32], htlc.RHash[:]) | ||
binary.BigEndian.PutUint64(htlcKey[32:], htlc.HtlcIndex) | ||
remoteHtlcs[htlcKey] = struct{}{} | ||
} | ||
|
||
// Now that we know which HTLC's they have, we'll only mark the HTLC's | ||
|
@@ -2491,9 +2494,10 @@ func (c *OpenChannel) ActiveHtlcs() []HTLC { | |
"incoming=%v", htlc.HtlcIndex, htlc.LogIndex, | ||
htlc.Incoming) | ||
|
||
onionHash := sha256.Sum256(htlc.OnionBlob[:]) | ||
if _, ok := remoteHtlcs[onionHash]; !ok { | ||
log.Tracef("Skipped htlc due to onion mismatched: "+ | ||
copy(htlcKey[:32], htlc.RHash[:]) | ||
binary.BigEndian.PutUint64(htlcKey[32:], htlc.HtlcIndex) | ||
if _, ok := remoteHtlcs[htlcKey]; !ok { | ||
log.Tracef("Skipped htlc due to htlcKey mismatch: "+ | ||
"id=%v, update=%v incoming=%v", | ||
htlc.HtlcIndex, htlc.LogIndex, htlc.Incoming) | ||
|
||
|
@@ -2506,6 +2510,52 @@ func (c *OpenChannel) ActiveHtlcs() []HTLC { | |
return activeHtlcs | ||
} | ||
|
||
// LocalAndRemoteHtlcs returns a slice of HTLC's which are currently active on | ||
// both or on either commitment transactions. | ||
func (c *OpenChannel) LocalAndRemoteHtlcs() []HTLC { | ||
c.RLock() | ||
defer c.RUnlock() | ||
|
||
activeHtlcs := make([]HTLC, 0, len(c.RemoteCommitment.Htlcs)) | ||
|
||
// We'll only return HTLC's that are locked into *both* commitment | ||
// transactions. So we'll iterate through their set of HTLC's to note | ||
// which ones are present on their commitment. We combine the RHash and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // We'll return all HTLCs that are locked on remote or local commitment |
||
// the HTLC index to create a unique key. | ||
remoteHtlcs := make(map[[32 + 8]byte]struct{}) | ||
var htlcKey [32 + 8]byte | ||
for _, htlc := range c.RemoteCommitment.Htlcs { | ||
log.Tracef("RemoteCommitment has htlc: id=%v, update=%v "+ | ||
"incoming=%v", htlc.HtlcIndex, htlc.LogIndex, | ||
htlc.Incoming) | ||
|
||
copy(htlcKey[:32], htlc.RHash[:]) | ||
binary.BigEndian.PutUint64(htlcKey[32:], htlc.HtlcIndex) | ||
remoteHtlcs[htlcKey] = struct{}{} | ||
activeHtlcs = append(activeHtlcs, htlc) | ||
} | ||
|
||
// Now that we know which HTLC's they have, we'll only mark the HTLC's | ||
// as active if *we* know them as well. | ||
for _, htlc := range c.LocalCommitment.Htlcs { | ||
log.Tracef("LocalCommitment has htlc: id=%v, update=%v "+ | ||
"incoming=%v", htlc.HtlcIndex, htlc.LogIndex, | ||
htlc.Incoming) | ||
|
||
copy(htlcKey[:32], htlc.RHash[:]) | ||
binary.BigEndian.PutUint64(htlcKey[32:], htlc.HtlcIndex) | ||
// We skip the htlcs which are already part of the remote set to | ||
// not have duplicates. | ||
if _, ok := remoteHtlcs[htlcKey]; ok { | ||
continue | ||
} | ||
|
||
activeHtlcs = append(activeHtlcs, htlc) | ||
} | ||
|
||
return activeHtlcs | ||
} | ||
|
||
// HTLC is the on-disk representation of a hash time-locked contract. HTLCs are | ||
// contained within ChannelDeltas which encode the current state of the | ||
// commitment between state updates. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe even just the htlc index is enough probably, because it's unique and always increasing ?