-
Notifications
You must be signed in to change notification settings - Fork 2.3k
routing: include htlc amount in bandwidth hint queries #5512
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
Merged
Roasbeef
merged 3 commits into
lightningnetwork:master
from
carlaKC:5468-amountawarehint
Oct 19, 2021
Merged
Changes from all commits
Commits
Show all changes
3 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -5038,19 +5038,29 @@ func (lc *LightningChannel) GetDustSum(remote bool) lnwire.MilliSatoshi { | |
| } | ||
|
|
||
| // MayAddOutgoingHtlc validates whether we can add an outgoing htlc to this | ||
| // channel. We don't have a value or circuit for this htlc, because we just | ||
| // want to test that we have slots for a potential htlc so we use a "mock" | ||
| // htlc to validate a potential commitment state with one more outgoing htlc. | ||
| func (lc *LightningChannel) MayAddOutgoingHtlc() error { | ||
| // channel. We don't have a circuit for this htlc, because we just want to test | ||
| // that we have slots for a potential htlc so we use a "mock" htlc to validate | ||
| // a potential commitment state with one more outgoing htlc. If a zero htlc | ||
| // amount is provided, we'll attempt to add the smallest possible htlc to the | ||
| // channel (either the minimum htlc, or 1 sat). | ||
| func (lc *LightningChannel) MayAddOutgoingHtlc(amt lnwire.MilliSatoshi) error { | ||
| lc.Lock() | ||
| defer lc.Unlock() | ||
|
|
||
| // As this is a mock HTLC, we'll attempt to add the smallest possible | ||
| // HTLC permitted in the channel. However certain implementations may | ||
| // set this value to zero, so we'll catch that and increment things so | ||
| // we always use a non-zero value. | ||
| mockHtlcAmt := lc.channelState.LocalChanCfg.MinHTLC | ||
| if mockHtlcAmt == 0 { | ||
| var mockHtlcAmt lnwire.MilliSatoshi | ||
| switch { | ||
| // If the caller specifically set an amount, we use it. | ||
| case amt != 0: | ||
| mockHtlcAmt = amt | ||
|
|
||
| // In absence of a specific amount, we want to use minimum htlc value | ||
| // for the channel. However certain implementations may set this value | ||
| // to zero, so we only use this value if it is non-zero. | ||
| case lc.channelState.LocalChanCfg.MinHTLC != 0: | ||
| mockHtlcAmt = lc.channelState.LocalChanCfg.MinHTLC | ||
|
|
||
| // As a last resort, we just add a non-zero amount. | ||
|
Member
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. 👍 |
||
| default: | ||
| mockHtlcAmt++ | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package routing | ||
|
carlaKC marked this conversation as resolved.
Outdated
|
||
|
|
||
| import ( | ||
| "github.com/lightningnetwork/lnd/channeldb" | ||
| "github.com/lightningnetwork/lnd/htlcswitch" | ||
| "github.com/lightningnetwork/lnd/lnwire" | ||
| "github.com/lightningnetwork/lnd/routing/route" | ||
| ) | ||
|
|
||
| // bandwidthHints provides hints about the currently available balance in our | ||
| // channels. | ||
| type bandwidthHints interface { | ||
| // availableChanBandwidth returns the total available bandwidth for a | ||
| // channel and a bool indicating whether the channel hint was found. | ||
| // The amount parameter is used to validate the outgoing htlc amount | ||
| // that we wish to add to the channel against its flow restrictions. If | ||
| // a zero amount is provided, the minimum htlc value for the channel | ||
| // will be used. If the channel is unavailable, a zero amount is | ||
| // returned. | ||
| availableChanBandwidth(channelID uint64, | ||
| amount lnwire.MilliSatoshi) (lnwire.MilliSatoshi, bool) | ||
| } | ||
|
|
||
| // getLinkQuery is the function signature used to lookup a link. | ||
| type getLinkQuery func(lnwire.ShortChannelID) ( | ||
| htlcswitch.ChannelLink, error) | ||
|
|
||
| // bandwidthManager is an implementation of the bandwidthHints interface which | ||
| // uses the link lookup provided to query the link for our latest local channel | ||
| // balances. | ||
| type bandwidthManager struct { | ||
| getLink getLinkQuery | ||
| localChans map[lnwire.ShortChannelID]struct{} | ||
| } | ||
|
|
||
| // newBandwidthManager creates a bandwidth manager for the source node provided | ||
| // which is used to obtain hints from the lower layer w.r.t the available | ||
| // bandwidth of edges on the network. Currently, we'll only obtain bandwidth | ||
| // hints for the edges we directly have open ourselves. Obtaining these hints | ||
| // allows us to reduce the number of extraneous attempts as we can skip channels | ||
| // that are inactive, or just don't have enough bandwidth to carry the payment. | ||
| func newBandwidthManager(graph routingGraph, sourceNode route.Vertex, | ||
| linkQuery getLinkQuery) (*bandwidthManager, error) { | ||
|
|
||
| manager := &bandwidthManager{ | ||
| getLink: linkQuery, | ||
| localChans: make(map[lnwire.ShortChannelID]struct{}), | ||
| } | ||
|
|
||
| // First, we'll collect the set of outbound edges from the target | ||
| // source node and add them to our bandwidth manager's map of channels. | ||
| err := graph.forEachNodeChannel(sourceNode, | ||
| func(channel *channeldb.DirectedChannel) error { | ||
| shortID := lnwire.NewShortChanIDFromInt( | ||
| channel.ChannelID, | ||
| ) | ||
| manager.localChans[shortID] = struct{}{} | ||
|
|
||
| return nil | ||
| }) | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return manager, nil | ||
| } | ||
|
|
||
| // getBandwidth queries the current state of a link and gets its currently | ||
| // available bandwidth. Note that this function assumes that the channel being | ||
| // queried is one of our local channels, so any failure to retrieve the link | ||
| // is interpreted as the link being offline. | ||
| func (b *bandwidthManager) getBandwidth(cid lnwire.ShortChannelID, | ||
| amount lnwire.MilliSatoshi) lnwire.MilliSatoshi { | ||
| link, err := b.getLink(cid) | ||
| if err != nil { | ||
| // If the link isn't online, then we'll report that it has | ||
| // zero bandwidth. | ||
| return 0 | ||
| } | ||
|
|
||
| // If the link is found within the switch, but it isn't yet eligible | ||
| // to forward any HTLCs, then we'll treat it as if it isn't online in | ||
| // the first place. | ||
| if !link.EligibleToForward() { | ||
| return 0 | ||
| } | ||
|
|
||
| // If our link isn't currently in a state where it can add another | ||
| // outgoing htlc, treat the link as unusable. | ||
| if err := link.MayAddOutgoingHtlc(amount); err != nil { | ||
| return 0 | ||
| } | ||
|
|
||
| // Otherwise, we'll return the current best estimate for the available | ||
| // bandwidth for the link. | ||
| return link.Bandwidth() | ||
| } | ||
|
|
||
| // availableChanBandwidth returns the total available bandwidth for a channel | ||
| // and a bool indicating whether the channel hint was found. If the channel is | ||
| // unavailable, a zero amount is returned. | ||
| func (b *bandwidthManager) availableChanBandwidth(channelID uint64, | ||
| amount lnwire.MilliSatoshi) (lnwire.MilliSatoshi, bool) { | ||
|
|
||
| shortID := lnwire.NewShortChanIDFromInt(channelID) | ||
| _, ok := b.localChans[shortID] | ||
| if !ok { | ||
| return 0, false | ||
| } | ||
|
|
||
| return b.getBandwidth(shortID, amount), true | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.