Repository navigation
Auto channel open: recommend a fixed-size channel to earning peers that run out of outbound #660
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
edff77e
feat: job for recommending a fixed-size channel open when an earning …
markettes 4db2212
fix: fail a promoted channel open request when its dispatch throws
markettes 74ee134
fix: cancel the channel open request when its recommendation is no lo…
markettes d2c940d
fix: open at 0 ppm when that is the rate we charge the peer
markettes 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,163 @@ | ||
| /* | ||
| * NodeGuard | ||
| * Copyright (C) 2023 Elenpay | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see http://www.gnu.org/licenses/. | ||
| * | ||
| */ | ||
|
|
||
| using NodeGuard.Data.Models; | ||
| using NodeGuard.Helpers; | ||
|
|
||
| namespace NodeGuard.Services; | ||
|
|
||
| /// <summary> | ||
| /// Everything the planner knows about one peer, aggregated over all our open channels to it. LND | ||
| /// forwards non-strictly and reports whichever channel it tried last, so the peer is the identity. | ||
| /// </summary> | ||
| public record PeerSnapshot( | ||
| string PeerPubKey, | ||
| long EarnedFeeSats, | ||
| long SettledOutSats, | ||
| int Refusals, | ||
| long UsableLocalSats, | ||
| long CapacitySats, | ||
| LocalOutboundPolicy? OutboundPolicy, | ||
| bool OnCooldown); | ||
|
|
||
| public record ChannelOpenInitiatorTunables( | ||
| long MinEarnedFeeSats, | ||
| int MinRefusals, | ||
| int LowOutboundPct, | ||
| long SizeSats, | ||
| long WalletSpendableSats, | ||
| long RemainingBudgetSats, | ||
| int MaxPlansPerRun) | ||
| { | ||
| public static ChannelOpenInitiatorTunables FromConstants( | ||
| Node node, | ||
| long walletSpendableSats, | ||
| long remainingBudgetSats) => new( | ||
| MinEarnedFeeSats: Constants.AUTO_CHANNEL_OPEN_MIN_EARNED_FEE_SATS, | ||
| MinRefusals: Constants.AUTO_CHANNEL_OPEN_MIN_REFUSALS, | ||
| LowOutboundPct: Constants.AUTO_CHANNEL_OPEN_LOW_OUTBOUND_PCT, | ||
| SizeSats: node.AutoChannelOpenSizeSats ?? 0, | ||
| WalletSpendableSats: walletSpendableSats, | ||
| RemainingBudgetSats: remainingBudgetSats, | ||
| MaxPlansPerRun: Constants.AUTO_CHANNEL_OPEN_MAX_PLANS_PER_RUN); | ||
| } | ||
|
|
||
| public record ChannelOpenRejection(string PeerPubKey, string Reason); | ||
|
|
||
| public record ChannelOpenClassification( | ||
| IReadOnlyList<ChannelOpenRecommendation> Plans, | ||
| IReadOnlyList<ChannelOpenRejection> Rejected); | ||
|
|
||
| /// <summary> | ||
| /// Channel-open planner. Pure, no I/O. A peer gets a fixed-size channel when the route to it has | ||
| /// earned us fees, it refused forwards recently for lack of outbound, and it is still short now. | ||
| /// </summary> | ||
| public static class ChannelOpenInitiatorService | ||
| { | ||
| public static ChannelOpenClassification Evaluate( | ||
| IReadOnlyList<PeerSnapshot> peers, | ||
| ChannelOpenInitiatorTunables t) | ||
| { | ||
| var plans = new List<ChannelOpenRecommendation>(); | ||
| var rejected = new List<ChannelOpenRejection>(); | ||
| var now = DateTimeOffset.UtcNow; | ||
|
|
||
| foreach (var peer in peers.OrderByDescending(x => x.EarnedFeeSats)) | ||
| { | ||
| var reason = Reject(peer, t); | ||
| if (reason != null) | ||
| { | ||
| rejected.Add(new ChannelOpenRejection(peer.PeerPubKey, reason)); | ||
| continue; | ||
| } | ||
|
|
||
| if (plans.Count >= t.MaxPlansPerRun) | ||
| { | ||
| rejected.Add(new ChannelOpenRejection(peer.PeerPubKey, | ||
| $"qualifies, but the run already has {t.MaxPlansPerRun} plan(s)")); | ||
| continue; | ||
|
markettes marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| plans.Add(new ChannelOpenRecommendation | ||
| { | ||
| PeerPubKey = peer.PeerPubKey, | ||
| EarnedFeeSats = peer.EarnedFeeSats, | ||
| SettledOutSats = peer.SettledOutSats, | ||
| Refusals = peer.Refusals, | ||
| UsableLocalSats = peer.UsableLocalSats, | ||
| PeerCapacitySats = peer.CapacitySats, | ||
| SuggestedCapacitySats = t.SizeSats, | ||
| InheritedFeeRatePpm = peer.OutboundPolicy!.FeeRatePpm, | ||
| InheritedBaseFeeMsat = peer.OutboundPolicy.BaseFeeMsat, | ||
| Status = ChannelOpenRecommendationStatus.Open, | ||
| LastEvidenceAt = now | ||
| }); | ||
| } | ||
|
|
||
| return new ChannelOpenClassification(plans, rejected); | ||
| } | ||
|
|
||
| private static string? Reject(PeerSnapshot peer, ChannelOpenInitiatorTunables t) | ||
| { | ||
| if (peer.OnCooldown) | ||
| { | ||
| return "on cooldown after a recent promotion or dismissal"; | ||
| } | ||
|
|
||
| // Without it the channel can't be priced: at the global default it would undercut what we | ||
| // already charge this peer. | ||
| if (peer.OutboundPolicy == null) | ||
| { | ||
| return "no outbound fee policy reported for any channel to the peer"; | ||
| } | ||
|
|
||
| if (peer.EarnedFeeSats < t.MinEarnedFeeSats) | ||
| { | ||
| return $"earned {peer.EarnedFeeSats} sats, below the {t.MinEarnedFeeSats} sats minimum"; | ||
| } | ||
|
|
||
| if (peer.Refusals < t.MinRefusals) | ||
| { | ||
| return $"{peer.Refusals} refused forward(s), below the {t.MinRefusals} minimum"; | ||
| } | ||
|
|
||
| // The refusals are history; a rebalance or a settled HTLC may have refilled the peer since. | ||
| if (peer.UsableLocalSats * 100 >= peer.CapacitySats * t.LowOutboundPct) | ||
| { | ||
| return $"{peer.UsableLocalSats} of {peer.CapacitySats} sats usable outbound, not below {t.LowOutboundPct}%"; | ||
| } | ||
|
|
||
| if (t.SizeSats <= 0) | ||
| { | ||
| return "the node has no channel size configured"; | ||
| } | ||
|
|
||
| if (t.SizeSats > t.WalletSpendableSats) | ||
| { | ||
| return $"the funding wallet can fund at most {t.WalletSpendableSats} sats, below the {t.SizeSats} sats size"; | ||
| } | ||
|
|
||
| if (t.SizeSats > t.RemainingBudgetSats) | ||
| { | ||
| return $"{t.RemainingBudgetSats} sats of budget left, below the {t.SizeSats} sats size"; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.