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
2 changes: 2 additions & 0 deletions src/Automapper/MapperProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public MapperProfile()
.ForMember(x => x.ChannelOperationRequestsAsDestination, opt => opt.Ignore())
.ForMember(x => x.ChannelOperationRequestsAsSource, opt => opt.Ignore())
.ForMember(x => x.FundsDestinationWallet, opt => opt.Ignore())
.ForMember(x => x.AutoChannelOpenWallet, opt => opt.Ignore())
.ForMember(x => x.ChannelOpenRecommendations, opt => opt.Ignore())
.ForMember(x => x.Users, opt => opt.Ignore());

CreateMap<Channel, Channel>()
Expand Down
19 changes: 19 additions & 0 deletions src/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,23 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<SparkSignerIdentity>()
.HasIndex(x => new { x.SeedFingerprint, x.Account }).IsUnique();

modelBuilder.Entity<ChannelOpenRecommendation>()
.HasOne(x => x.Node)
.WithMany(x => x.ChannelOpenRecommendations)
.HasForeignKey(x => x.NodeId)
.OnDelete(DeleteBehavior.Cascade);

modelBuilder.Entity<ChannelOpenRecommendation>()
.HasOne(x => x.ChannelOperationRequest)
.WithMany()
.HasForeignKey(x => x.ChannelOperationRequestId)
.OnDelete(DeleteBehavior.SetNull);

// One recommendation per peer per node — the job upserts on this. Keyed by peer rather
// than channel because non-strict forwarding splits one signal across a peer's channels.
modelBuilder.Entity<ChannelOpenRecommendation>()
.HasIndex(x => new { x.NodeId, x.PeerPubKey }).IsUnique();

// These default ON: existing rows must be backfilled true (the C# initializer
// only affects new in-code instances, not the DB column default / migration backfill).
modelBuilder.Entity<Channel>().Property(x => x.IsDynamicFeeEnabled).HasDefaultValue(false);
Expand Down Expand Up @@ -220,5 +237,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
public DbSet<ChannelFeeState> ChannelFeeStates { get; set; }

public DbSet<SparkSignerIdentity> SparkSignerIdentities { get; set; }

public DbSet<ChannelOpenRecommendation> ChannelOpenRecommendations { get; set; }
}
}
3 changes: 2 additions & 1 deletion src/Data/Models/AuditLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,6 @@ public enum AuditObjectType
UTXO,
InternalWallet,
Session,
Rebalance
Rebalance,
ChannelOpenRecommendation
}
101 changes: 101 additions & 0 deletions src/Data/Models/ChannelOpenRecommendation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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 System.ComponentModel.DataAnnotations;

namespace NodeGuard.Data.Models;

public enum AutoChannelOpenMode
{
/// Write the recommendation and stop. A human promotes it.
Recommendation = 0,

/// Write the recommendation and promote it at the node's fixed size, under budget.
Auto = 1
}

public enum ChannelOpenRecommendationStatus
{
/// Awaiting a decision; refreshed in place while the peer still qualifies.
Open = 0,

/// Turned into a <see cref="ChannelOperationRequest"/>.
Promoted = 1,

/// Rejected by an operator; suppresses the peer for the cooldown.
Dismissed = 2,

/// Not reproduced by the latest run. Re-opened in place if the peer qualifies again.
Expired = 3,

/// Promoted, but the channel never opened — the request failed or was cancelled. Unlike a
/// dismissal this says nothing about the peer, so it leaves no cooldown behind.
Failed = 4
}

/// Kept distinct from <see cref="ChannelOperationRequest"/>: a request is a funding instruction
/// awaiting treasury, a recommendation is evidence an operator may promote or dismiss.
/// <para>
/// One row per (<see cref="NodeId"/>, <see cref="PeerPubKey"/>) — keyed by peer, not channel.
/// </para>
public class ChannelOpenRecommendation : Entity
{
public int NodeId { get; set; }
public Node? Node { get; set; }

[MaxLength(66)]
public string PeerPubKey { get; set; } = string.Empty;

#region Evidence
/// Fees earned on settled forwards out to the peer over the lookback window.
public long EarnedFeeSats { get; set; }

/// Settled volume out to the peer over the lookback window. Displayed, never gated on.
public long SettledOutSats { get; set; }

/// Forwards refused for lack of outbound to the peer over the shortage window.
public int Refusals { get; set; }

/// Usable outbound to the peer when the run saw it, reserve excluded.
public long UsableLocalSats { get; set; }

/// Total capacity of our open channels to the peer.
public long PeerCapacitySats { get; set; }

/// The node's fixed size at the time of the run. An operator may override it before promoting.
public long SuggestedCapacitySats { get; set; }

/// The policy the channel opens at: the highest-rate one among our channels to the peer.
public long? InheritedFeeRatePpm { get; set; }

public long? InheritedBaseFeeMsat { get; set; }
#endregion Evidence

#region Lifecycle
public ChannelOpenRecommendationStatus Status { get; set; }

public DateTimeOffset LastEvidenceAt { get; set; }

public int? ChannelOperationRequestId { get; set; }
public ChannelOperationRequest? ChannelOperationRequest { get; set; }

[MaxLength(512)]
public string? DismissReason { get; set; }
#endregion Lifecycle
}
6 changes: 6 additions & 0 deletions src/Data/Models/ChannelOperationRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ public decimal Amount

public ChannelOperationRequestStatus Status { get; set; }

[NotMapped]
public bool IsFinalized => Status is ChannelOperationRequestStatus.OnChainConfirmed
or ChannelOperationRequestStatus.Cancelled
or ChannelOperationRequestStatus.Rejected
or ChannelOperationRequestStatus.Failed;

public OperationRequestType RequestType { get; set; }

/// <summary>
Expand Down
27 changes: 27 additions & 0 deletions src/Data/Models/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,37 @@ public class Node : Entity
/// </summary>
public double? MaxRebalanceCostToEarnRatio { get; set; }

/// <summary>
/// Gate for automatic channel opening.
/// </summary>
public bool AutoChannelOpenEnabled { get; set; } = false;

public AutoChannelOpenMode AutoChannelOpenMode { get; set; } = AutoChannelOpenMode.Recommendation;

/// <summary>
/// Wallet the opened channels are funded from. Without it no plan can be promoted.
/// </summary>
public int? AutoChannelOpenWalletId { get; set; }

public Wallet? AutoChannelOpenWallet { get; set; }

/// <summary>
/// Every automatic open uses this size; a wallet or budget that can't cover it skips the peer.
/// </summary>
public long? AutoChannelOpenSizeSats { get; set; }

public long? AutoChannelOpenBudgetSats { get; set; }

public TimeSpan? AutoChannelOpenBudgetRefreshInterval { get; set; }

public DateTimeOffset? AutoChannelOpenBudgetStartDatetime { get; set; }

#endregion Routing Engine

#region Relationships

public ICollection<ChannelOpenRecommendation> ChannelOpenRecommendations { get; set; }

public ICollection<ChannelOperationRequest> ChannelOperationRequestsAsSource { get; set; }
public ICollection<ChannelOperationRequest> ChannelOperationRequestsAsDestination { get; set; }

Expand Down
Loading
Loading