Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Helpers/LightningHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Google.Protobuf;
using NBitcoin;
using NBXplorer;
using NBXplorer.DerivationStrategy;
using NBXplorer.Models;

namespace NodeGuard.Helpers
Expand Down Expand Up @@ -220,6 +221,30 @@ public static async Task<List<ICoin>> SelectCoins(Wallet wallet, List<UTXO> sele
.ToList();
}

/// <summary>
/// Biggest channel the coins can fund through a normal open: every coin in, the funding output
/// and a change output out, with the change kept at dust so the tx still relays.
/// </summary>
public static long MaxFundableChannelSats(DerivationStrategyBase derivationStrategy, List<UTXO> utxos, decimal feeRateSatPerVb)
{
if (utxos.Count == 0)
{
return 0;
}

// Output values don't change the size, so the amounts here are placeholders
var builder = CurrentNetworkHelper.GetCurrentNetwork().CreateTransactionBuilder();
var tx = builder.AddCoins(utxos.Select(u => u.AsCoin(derivationStrategy)))
.SendAll(new WitScriptId(uint256.Zero).ScriptPubKey)
.BuildTransaction(false);
tx.Outputs.Add(Money.Zero, utxos[0].ScriptPubKey);
Comment thread
markettes marked this conversation as resolved.
Resolved

var totalSats = utxos.Sum(u => ((Money)u.Value).Satoshi);
var feeSats = builder.EstimateFees(tx, new FeeRate(feeRateSatPerVb)).Satoshi;

return Math.Max(0, totalSats - feeSats - Constants.MINIMUM_UTXO_VALUE_SATS);
}

/// <summary>
/// Returns the fee rate (sat/vb) for a tx, 4 is the value in regtest
/// </summary>
Expand Down
367 changes: 367 additions & 0 deletions src/Jobs/AutoChannelOpenJob.cs

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public static async Task Main(string[] args)
builder.Services.AddTransient<IAuditLogRepository, AuditLogRepository>();
builder.Services.AddTransient<IForwardingHtlcEventRepository, ForwardingHtlcEventRepository>();
builder.Services.AddTransient<IChannelRoutingStateRepository, ChannelRoutingStateRepository>();
builder.Services.AddTransient<IChannelOpenRecommendationRepository, ChannelOpenRecommendationRepository>();
builder.Services.AddTransient<IChannelFeeStateRepository, ChannelFeeStateRepository>();
builder.Services.AddTransient<ISparkSignerIdentityRepository, SparkSignerIdentityRepository>();
builder.Services.AddTransient<ICoinSelectionService, CoinSelectionService>();
Expand All @@ -148,6 +149,7 @@ public static async Task Main(string[] args)
builder.Services.AddTransient<ClipboardService>();
builder.Services.AddTransient<ILocalStorageService, LocalStorageService>();
builder.Services.AddTransient<ILightningService, LightningService>();
builder.Services.AddTransient<IChannelOpenPromotionService, ChannelOpenPromotionService>();
builder.Services.AddTransient<IBitcoinService, BitcoinService>();
builder.Services.AddTransient<IWithdrawalRequestService, WithdrawalRequestService>();
builder.Services.AddTransient<NotificationService, NotificationService>();
Expand Down Expand Up @@ -376,6 +378,23 @@ void ScheduleRoutingJob(SimpleScheduleBuilder sb, int minutes)
opts.StartAt(DateBuilder.FutureDate(Constants.ROUTING_ENGINE_ACTUATOR_OFFSET_MINUTES, IntervalUnit.Minute));
});

//Auto Channel Open Job (demand-driven capacity, own cadence)
q.AddJob<AutoChannelOpenJob>(opts =>
{
opts.DisallowConcurrentExecution();
opts.WithIdentity(nameof(AutoChannelOpenJob));
});

q.AddTrigger(opts =>
{
// No offset: unlike the fee and rebalance actuators this job reads HTLC events and
// live LND state, never the routing state TargetRatioReevaluationJob writes.
opts.ForJob(nameof(AutoChannelOpenJob))
.WithIdentity($"{nameof(AutoChannelOpenJob)}Trigger")
.StartNow()
.WithSimpleSchedule(sb => ScheduleRoutingJob(sb, Constants.AUTO_CHANNEL_OPEN_JOB_INTERVAL_MINUTES));
});

//Monitor Withdrawals Job
q.AddJob<MonitorWithdrawalsJob>(opts =>
{
Expand Down
163 changes: 163 additions & 0 deletions src/Services/ChannelOpenInitiatorService.cs
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;
Comment thread
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;
}
}
Loading
Loading