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
7 changes: 7 additions & 0 deletions src/Data/Models/Rebalance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ public class Rebalance : Entity
/// </summary>
public string? TargetPubkey { get; set; }

/// <summary>
/// Payment hash of the self-invoice. Persisted as soon as the invoice is created so the
/// reconciliation job can call <c>Router.TrackPaymentV2</c> after a crash / cancellation
/// and resolve the true outcome against LND.
/// </summary>
public string? PaymentHashHex { get; set; }

/// <summary>
/// Persisted for forensic / proof-of-payment lookup; intentionally not exposed via gRPC.
/// </summary>
Expand Down
11 changes: 8 additions & 3 deletions src/Data/Repositories/Interfaces/IRebalanceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ public interface IRebalanceRepository
DateTimeOffset? toDate = null);

/// <summary>
/// Get all rebalances currently in flight (Pending/Probing/InFlight). Used by the
/// monitor job for stale-state cleanup after process restart.
/// Returns rebalances the monitor job should reconcile against LND:
/// non-terminal rows (Pending/Probing/InFlight) plus recently-marked terminal failures
/// within <paramref name="recentTerminalWindow"/>. Only rows with a stored payment hash
/// are returned — without a hash there is nothing to look up in LND.
/// The recent-terminal sweep exists because the catch-all in RebalanceService can mark
/// a row Failed on transient errors (cancellation, RPC timeout) while LND has actually
/// settled the payment. Includes Node so the caller can call LND directly.
/// </summary>
Task<List<Rebalance>> GetAllInFlight();
Task<List<Rebalance>> GetReconcilable(TimeSpan recentTerminalWindow);

Task<(bool, string?)> AddAsync(Rebalance rebalance);

Expand Down
17 changes: 13 additions & 4 deletions src/Data/Repositories/RebalanceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,23 @@ public RebalanceRepository(IRepository<Rebalance> repository, IDbContextFactory<
return (rebalances, totalCount);
}

public async Task<List<Rebalance>> GetAllInFlight()
public async Task<List<Rebalance>> GetReconcilable(TimeSpan recentTerminalWindow)
{
await using var context = await _dbContextFactory.CreateDbContextAsync();

var terminalCutoff = DateTimeOffset.UtcNow - recentTerminalWindow;

return await context.Rebalances
.Where(r => r.Status == RebalanceStatus.Pending
|| r.Status == RebalanceStatus.Probing
|| r.Status == RebalanceStatus.InFlight)
.Include(r => r.Node)
.Where(r => r.PaymentHashHex != null
&& (r.Status == RebalanceStatus.Pending
|| r.Status == RebalanceStatus.Probing
|| r.Status == RebalanceStatus.InFlight
|| ((r.Status == RebalanceStatus.Failed
|| r.Status == RebalanceStatus.Timeout
|| r.Status == RebalanceStatus.NoRoute
|| r.Status == RebalanceStatus.InsufficientBalance)
&& r.UpdateDatetime >= terminalCutoff)))
.ToListAsync();
}

Expand Down
Loading
Loading