Skip to content
Closed
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
11 changes: 8 additions & 3 deletions docker/e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,14 @@ tests (`DustUtxoWithdrawalE2ETests`, `WithdrawalRbfBumpE2ETests`, `GetNewWalletA
those two in sync between the `nodeguard` and `e2e-runner` services**, or the test asserts against numbers
NodeGuard didn't plan with.
- **RBF bump scenario** (`WithdrawalRbfBumpE2ETests`): `RequestWithdrawal` on the hot wallet, then `BumpWithdrawal`
over gRPC, asserting bitcoind evicts the original for the higher-fee replacement, NodeGuard marks the original
`WITHDRAWAL_BUMPED`, and the replacement settles once mined. It relies on `MONITOR_WITHDRAWALS_CRON` being fast on
the `nodeguard` service (10s here; prod keeps the every-5-minutes default) and never mines between the two calls.
over gRPC, asserting bitcoind evicts the original for the higher-fee replacement, NodeGuard reports the original
`WITHDRAWAL_BUMPED` while the replacement is in the mempool (its stored status stays pending confirmation), and once
the replacement is mined it settles and the original is stored as `WITHDRAWAL_BUMPED`. It relies on
`MONITOR_WITHDRAWALS_CRON` being fast on the `nodeguard` service (10s here; prod keeps the every-5-minutes default)
and never mines between the two calls. The mined-original theory mines the ORIGINAL with `generateblock` (raw tx,
bypassing the mempool) after a bump or cancel, and asserts NodeGuard settles the original and fails the replacement.
The bump-of-a-bump test does the same with three transactions (A bumped to B, B to C, A mined): A settles, B and C
fail. The by-reference lookup returns the whole chain, each request with its own status.
- **Adding an e2e test**: put it in `[Collection("E2E")]` (so it serialises with the others on the one
regtest chain — without it the class runs in parallel and they interfere), have it provision the
resources it needs and reset its own state, and gate it with `[E2EFact]`.
5 changes: 4 additions & 1 deletion src/Automapper/MapperProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ public MapperProfile()
.ForMember(x => x.Wallet, opt => opt.Ignore())
.ForMember(x => x.WalletWithdrawalRequestPSBTs, opt => opt.Ignore())
.ForMember(x => x.UserRequestor, opt => opt.Ignore())
.ForMember(x => x.UTXOs, opt => opt.Ignore());
.ForMember(x => x.UTXOs, opt => opt.Ignore())
// Update attaches the whole mapped graph as Modified: an RBF neighbour must never ride along.
.ForMember(x => x.BumpingWalletWithdrawalRequest, opt => opt.Ignore())
.ForMember(x => x.Replacements, opt => opt.Ignore());

CreateMap<UTXO, FMUTXO>()
.ForMember(x => x.TxId, opt => opt.MapFrom(x => x.Outpoint.Hash.ToString()))
Expand Down
15 changes: 15 additions & 0 deletions src/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.IsUnique()
.HasFilter("\"IsTemplatePSBT\"");

// RBF: a withdrawal may be replaced several times over its life (each replacement abandoned before the
// next), but at most one replacement may be live, or two NodeGuard transactions would compete for the
// same inputs. Partial unique index over the live statuses: Pending, PSBTSignaturesPending,
// OnChainConfirmationPending, FinalizingPSBT. Same named-overload and plain-FK-index rules as above.
modelBuilder.Entity<WalletWithdrawalRequest>()
.HasOne(x => x.BumpingWalletWithdrawalRequest)
.WithMany(x => x.Replacements)
.HasForeignKey(x => x.BumpingWalletWithdrawalRequestId);
modelBuilder.Entity<WalletWithdrawalRequest>().HasIndex(x => x.BumpingWalletWithdrawalRequestId);
var liveStatuses = string.Join(", ", WalletWithdrawalRequest.LiveStatuses.Cast<int>().OrderBy(s => s));
modelBuilder.Entity<WalletWithdrawalRequest>()
.HasIndex(x => x.BumpingWalletWithdrawalRequestId, WalletWithdrawalRequest.LiveReplacementIndex)
.IsUnique()
.HasFilter($"\"BumpingWalletWithdrawalRequestId\" IS NOT NULL AND \"Status\" IN ({liveStatuses})");

modelBuilder.Entity<ApplicationUser>().HasIndex(x => x.NormalizedUserName).IsUnique();


Expand Down
86 changes: 84 additions & 2 deletions src/Data/Models/WalletWithdrawalRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public enum WalletWithdrawalRequestStatus
FinalizingPSBT = 7,

/// <summary>
/// The tx was bumped by Replace by Fee (RBF) method
/// Superseded: an RBF replacement of this request (or of one of its replacements) confirmed. Terminal, written
/// only by the monitor. While a replacement is merely in flight the request keeps its own status.
/// </summary>
Bumped = 8
}
Expand Down Expand Up @@ -224,10 +225,91 @@ public bool Equals(WalletWithdrawalRequest? other)

/// <summary>
/// True when this request replaces <see cref="BumpingWalletWithdrawalRequest"/> to CANCEL it (RBF): same inputs,
/// a single output back to the wallet, and the replaced request ends up Cancelled instead of Bumped.
/// a single output back to the wallet, and once this one confirms the replaced request ends up Cancelled
/// instead of Bumped.
/// </summary>
public bool IsRbfCancellation { get; set; }

/// <summary>
/// Requests that replace this one through RBF (fee bumps and cancellations). Which of them is live or confirmed
/// is what makes this request "replaced": its own status only ever describes its own transaction.
/// </summary>
public List<WalletWithdrawalRequest> Replacements { get; set; } = new();

/// <summary>Partial unique index: at most one replacement of a request in a live status.</summary>
public const string LiveReplacementIndex = "IX_WalletWithdrawalRequests_LiveReplacement";

/// <summary>
/// Statuses in which a request can still put (or has put) a transaction in the mempool. A replacement in any of
/// them blocks a second replacement of the same request (<see cref="LiveReplacementIndex"/>).
/// </summary>
public static readonly WalletWithdrawalRequestStatus[] LiveStatuses =
{
WalletWithdrawalRequestStatus.Pending,
WalletWithdrawalRequestStatus.PSBTSignaturesPending,
WalletWithdrawalRequestStatus.FinalizingPSBT,
WalletWithdrawalRequestStatus.OnChainConfirmationPending,
};

[NotMapped]
public bool IsLive => LiveStatuses.Contains(Status);

[NotMapped]
public bool IsAbandoned => Status is WalletWithdrawalRequestStatus.Failed
or WalletWithdrawalRequestStatus.Cancelled
or WalletWithdrawalRequestStatus.Rejected;

/// <summary>The replacement still in flight, if any. Needs <see cref="Replacements"/> loaded.</summary>
[NotMapped]
public WalletWithdrawalRequest? LiveReplacement => Replacements.FirstOrDefault(r => r.IsLive);

/// <summary>The replacement whose transaction competes with this one's in the mempool, if any.</summary>
[NotMapped]
public WalletWithdrawalRequest? BroadcastReplacement =>
Replacements.FirstOrDefault(r => r.Status == WalletWithdrawalRequestStatus.OnChainConfirmationPending);

/// <summary>The newest replacement that was not abandoned: the next hop when following a chain by id.</summary>
[NotMapped]
public WalletWithdrawalRequest? NewestReplacement => Replacements.Where(r => !r.IsAbandoned).MaxBy(r => r.Id);

public static string RbfCancellationReason(int replacementId, string? txId) =>
WithTx($"Cancelled by RBF replacement request {replacementId}", txId);

public static string ReplacedReason(int replacementId, string? txId) =>
WithTx($"Replaced by request {replacementId}", txId);

public static string MinedInsteadReason(int originalId, string? txId) =>
WithTx($"Withdrawal request {originalId} was mined instead", txId);

private static string WithTx(string reason, string? txId) =>
string.IsNullOrEmpty(txId) ? reason : $"{reason} (tx {txId})";

/// <summary>
/// What a request superseded by <paramref name="replacement"/> becomes: Cancelled when a cancellation sits
/// on the way to it, otherwise Bumped.
/// </summary>
public static (WalletWithdrawalRequestStatus Status, string Reason) SupersededBy(WalletWithdrawalRequest replacement, bool cancellation) =>
cancellation
? (WalletWithdrawalRequestStatus.Cancelled, RbfCancellationReason(replacement.Id, replacement.TxId))
: (WalletWithdrawalRequestStatus.Bumped, ReplacedReason(replacement.Id, replacement.TxId));

/// <summary>
/// The status the API reports. The stored one describes this request's own transaction only: while a
/// replacement's transaction is in the mempool and this one has no confirmations, the request is reported
/// with the status it will be stored with once that replacement settles.
/// </summary>
public (WalletWithdrawalRequestStatus Status, string? Reason, int? ReplacedById) EffectiveStatus(ulong ownConfirmations)
{
if (Status == WalletWithdrawalRequestStatus.OnChainConfirmationPending && ownConfirmations == 0
&& BroadcastReplacement is { } broadcast)
{
var (status, reason) = SupersededBy(broadcast, broadcast.IsRbfCancellation);
return (status, reason, broadcast.Id);
}

return (Status, RejectCancelDescription, NewestReplacement?.Id);
}

public List<WalletWithdrawalRequestPSBT> WalletWithdrawalRequestPSBTs { get; set; }

public List<FMUTXO> UTXOs { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,13 @@ public interface IWalletWithdrawalRequestRepository : IBitcoinRequestRepository
Task<List<WalletWithdrawalRequest>> GetPendingRequests();

Task<List<WalletWithdrawalRequest>> GetOnChainPendingWithdrawals();

/// <summary>
/// Every request connected to this one through RBF links, in both directions, the request itself included.
/// Untracked, no navigations loaded.
/// </summary>
Task<List<WalletWithdrawalRequest>> GetRbfChainAsync(int requestId);

/// <summary>Ids of the live requests that replace another request or are replaced by one.</summary>
Task<List<int>> GetLiveRbfLinkedRequestIdsAsync();
}
2 changes: 2 additions & 0 deletions src/Data/Repositories/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public async Task<List<T>> GetAll(ApplicationDbContext applicationDbContext)
catch (Exception e)
{
_logger.LogError(e, "Error on repository");
// The innermost message is the database's own (e.g. the constraint a unique index rejected).
return (false, e.GetBaseException().Message);
}

return (rowsChanged, null);
Expand Down
73 changes: 64 additions & 9 deletions src/Data/Repositories/WalletWithdrawalRequestRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ INBXplorerService nBXplorerService
.Include(x => x.WalletWithdrawalRequestPSBTs)
.Include(x => x.WalletWithdrawalRequestDestinations)
.Include(x => x.UTXOs)
.Include(x => x.Replacements)
.SingleOrDefaultAsync(x => x.Id == id);

return request;
Expand All @@ -75,30 +76,31 @@ public async Task<List<WalletWithdrawalRequest>> GetByIds(List<int> ids)
{
await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync();

return await applicationDbContext.WalletWithdrawalRequests.Where(wr => ids.Contains(wr.Id)).ToListAsync();
return await applicationDbContext.WalletWithdrawalRequests
.Include(x => x.Replacements)
.Where(wr => ids.Contains(wr.Id))
.ToListAsync();
}

public async Task<List<WalletWithdrawalRequest>> GetByReferenceIds(List<string> referenceIds)
{
await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync();

// RBF replacements inherit the reference id of the request they replace, so a reference id can match a whole
// chain of requests. Only the latest one of each chain reflects the current status of the withdrawal.
var requests = await applicationDbContext.WalletWithdrawalRequests
// RBF replacements inherit the reference id of the request they replace, so a reference id matches a whole
// chain. Every request of it is returned, oldest first, each reporting its own (derived) status.
return await applicationDbContext.WalletWithdrawalRequests
.Include(x => x.Replacements)
.Where(wr => !string.IsNullOrEmpty(wr.ReferenceId) && referenceIds.Contains(wr.ReferenceId))
.OrderBy(wr => wr.Id)
.ToListAsync();

return requests
.GroupBy(wr => wr.ReferenceId!)
.Select(g => g.OrderByDescending(wr => wr.Id).First())
.ToList();
}

public async Task<WalletWithdrawalRequest?> GetByTxHash(string txHash)
{
await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync();

return await applicationDbContext.WalletWithdrawalRequests
.Include(x => x.Replacements)
.FirstOrDefaultAsync(wr => wr.TxId == txHash);
}

Expand Down Expand Up @@ -145,6 +147,7 @@ public async Task<List<WalletWithdrawalRequest>> GetAll()
.Include(x => x.WalletWithdrawalRequestPSBTs)
.Include(x => x.WalletWithdrawalRequestDestinations)
.Include(x => x.UTXOs)
.Include(x => x.Replacements)
.AsSplitQuery()
.AsQueryable();

Expand Down Expand Up @@ -382,6 +385,58 @@ public async Task<List<WalletWithdrawalRequest>> GetPendingRequests()
return walletWithdrawalRequests;
}

public async Task<List<WalletWithdrawalRequest>> GetRbfChainAsync(int requestId)
{
await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync();
var requests = applicationDbContext.WalletWithdrawalRequests.AsNoTracking();

// Up to the first request of the chain, then down through every replacement, level by level. One query
// per hop; chains are a handful of rows.
var root = await requests.SingleOrDefaultAsync(x => x.Id == requestId);
if (root == null) return new List<WalletWithdrawalRequest>();

var chain = new Dictionary<int, WalletWithdrawalRequest> { [root.Id] = root };
while (root.BumpingWalletWithdrawalRequestId is { } replacedId && !chain.ContainsKey(replacedId))
{
var replaced = await requests.SingleOrDefaultAsync(x => x.Id == replacedId);
if (replaced == null) break;

chain[replaced.Id] = replaced;
root = replaced;
}

var expanded = new HashSet<int> { root.Id };
var frontier = new List<int> { root.Id };
while (frontier.Count > 0)
{
var parents = frontier;
var children = await requests
.Where(x => x.BumpingWalletWithdrawalRequestId != null && parents.Contains(x.BumpingWalletWithdrawalRequestId.Value))
.ToListAsync();

frontier = new List<int>();
foreach (var child in children)
{
chain.TryAdd(child.Id, child);
if (expanded.Add(child.Id)) frontier.Add(child.Id);
}
}

return chain.Values.OrderBy(x => x.Id).ToList();
}

public async Task<List<int>> GetLiveRbfLinkedRequestIdsAsync()
{
await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync();

var live = WalletWithdrawalRequest.LiveStatuses;
return await applicationDbContext.WalletWithdrawalRequests
.Where(x => live.Contains(x.Status)
&& (x.BumpingWalletWithdrawalRequestId != null || x.Replacements.Any()))
.Select(x => x.Id)
.ToListAsync();
}

public async Task<List<WalletWithdrawalRequest>> GetOnChainPendingWithdrawals()
{
await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync();
Expand Down
3 changes: 2 additions & 1 deletion src/Helpers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,9 @@ static Constants()
var environmentVariableMax = Environment.GetEnvironmentVariable("MAXIMUM_WITHDRAWAL_BTC_AMOUNT");
if (environmentVariableMax != null) MAXIMUM_WITHDRAWAL_BTC_AMOUNT = decimal.Parse(environmentVariableMax, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture);

// At least one: a transaction evicted from the mempool sits at 0 confirmations and must never count as mined.
var transactionConfBlocks = GetEnvironmentalVariableOrThrowIfNotTesting("TRANSACTION_CONFIRMATION_MINIMUM_BLOCKS");
if (transactionConfBlocks != null) TRANSACTION_CONFIRMATION_MINIMUM_BLOCKS = int.Parse(transactionConfBlocks);
TRANSACTION_CONFIRMATION_MINIMUM_BLOCKS = Math.Max(1, transactionConfBlocks != null ? int.Parse(transactionConfBlocks) : 1);

var defaultChannelFeePolicyTimelockDeltaBlocks = Environment.GetEnvironmentVariable("DEFAULT_CHANNEL_FEE_POLICY_TIMELOCK_DELTA_BLOCKS");
if (defaultChannelFeePolicyTimelockDeltaBlocks != null) DEFAULT_CHANNEL_FEE_POLICY_TIMELOCK_DELTA_BLOCKS = int.Parse(defaultChannelFeePolicyTimelockDeltaBlocks);
Expand Down
Loading
Loading