Skip to content

Commit

Permalink
Fix timeout errors, Fix NBXplorer hanging when exiting (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier authored Nov 29, 2024
1 parent 1eeb0ff commit 24d24f2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
12 changes: 10 additions & 2 deletions NBXplorer/Backend/DbConnectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace NBXplorer.Backend
Expand Down Expand Up @@ -58,7 +59,7 @@ public static void Register(NpgsqlDataSourceBuilder dsBuilder)
dsBuilder.MapComposite<OutpointRaw>("outpoint");
dsBuilder.MapComposite<Repository.DescriptorScriptInsert>("nbxv1_ds");
}
public async Task<bool> FetchMatches(MatchQuery matchQuery)
public async Task<bool> FetchMatches(MatchQuery matchQuery, CancellationToken cancellationToken)
{
var outs = new List<NewOutRaw>(matchQuery.Outs.Count);
var ins = new List<NewInRaw>(matchQuery.Ins.Count);
Expand Down Expand Up @@ -93,7 +94,14 @@ public async Task<bool> FetchMatches(MatchQuery matchQuery)
parameters.Add("in_outs", outs);
parameters.Add("in_ins", ins);
parameters.Add("has_match", dbType: System.Data.DbType.Boolean, direction: ParameterDirection.InputOutput);
await Connection.QueryAsync<int>("fetch_matches", parameters, commandType: CommandType.StoredProcedure);
var command = new CommandDefinition(
commandText: "fetch_matches",
parameters: parameters,
commandType: CommandType.StoredProcedure,
commandTimeout: ((NpgsqlConnection)Connection).CommandTimeout * 3,
cancellationToken: cancellationToken
);
await Connection.QueryAsync<int>(command);
return parameters.Get<bool>("has_match");
}

Expand Down
4 changes: 2 additions & 2 deletions NBXplorer/Backend/Indexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ private async Task UpdateState(Node node)
private async Task<BlockLocator> GetDefaultCurrentLocation(CancellationToken token)
{
if (ChainConfiguration.StartHeight > BlockchainInfo.Headers)
throw new InvalidOperationException($"{Network.CryptoCode}: StartHeight should not be above the current tip");
throw new InvalidOperationException($"{Network.CryptoCode}: StartHeight ({ChainConfiguration.StartHeight}) should not be above the current tip ({BlockchainInfo.Headers})");
BlockLocator blockLocator = null;
if (ChainConfiguration.StartHeight == -1)
{
Expand Down Expand Up @@ -464,7 +464,7 @@ private async Task SaveMatches(DbConnectionHelper conn, List<Transaction> transa
{
await conn.NewBlock(slimChainedBlock);
}
var matches = await Repository.GetMatches(conn, transactions, slimChainedBlock, now, blockMatch: true, useCache: true);
var matches = await Repository.GetMatches(conn, transactions, slimChainedBlock, now, useCache: true, cancellationToken: cts.Token);
_ = AddressPoolService.GenerateAddresses(Network, matches);

long confirmations = 0;
Expand Down
13 changes: 6 additions & 7 deletions NBXplorer/Backend/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NBitcoin.Altcoins.Elements;
using NBXplorer.Altcoins.Liquid;
using NBXplorer.Client;
using NBitcoin.Scripting;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -522,7 +521,7 @@ public async Task Unblind(RPCClient rpc, DerivationStrategyBase ts, ElementsTran
}
record UpdateMatchesOuts(string tx_id, long idx, string asset_id, long value);

public async Task<TrackedTransaction[]> GetMatches(DbConnectionHelper connection, IList<Transaction> txs, SlimChainedBlock slimBlock, DateTimeOffset now, bool blockMatch, bool useCache)
public async Task<TrackedTransaction[]> GetMatches(DbConnectionHelper connection, IList<Transaction> txs, SlimChainedBlock slimBlock, DateTimeOffset now, bool useCache, CancellationToken cancellationToken = default)
{
List<SaveTransactionRecord> records = new(txs.Count);
foreach (var tx in txs)
Expand All @@ -532,7 +531,7 @@ public async Task<TrackedTransaction[]> GetMatches(DbConnectionHelper connection
int i = 0;
foreach (var tx in txs)
{
var record = SaveTransactionRecord.Create(tx, slimBlock: slimBlock, blockIndex: blockMatch ? i : null, seenAt: now);
var record = SaveTransactionRecord.Create(tx, slimBlock: slimBlock, blockIndex: i, seenAt: now);
if (!useCache || !noMatchCache.Contains(record.Id))
records.Add(record);
i++;
Expand All @@ -546,9 +545,9 @@ public async Task<TrackedTransaction[]> GetMatches(DbConnectionHelper connection
records.Add(record);
}
}
var query = MatchQuery.FromTransactions(records.Select(r => r.Transaction), MinUtxoValue);

var matches = await SaveMatches(connection, query, records);
var query = MatchQuery.FromTransactions(records.Select(r => r.Transaction), MinUtxoValue);
var matches = await SaveMatches(connection, query, records, cancellationToken);

// Let's remember unconfirmed that didn't match so the processing of a block is faster
if (useCache && slimBlock is null)
Expand All @@ -561,7 +560,7 @@ public async Task<TrackedTransaction[]> GetMatches(DbConnectionHelper connection
}
return matches.TrackedTransactions;
}
async Task<(TrackedTransaction[] TrackedTransactions, SaveTransactionRecord[] Saved)> SaveMatches(DbConnectionHelper connection, MatchQuery matchQuery, IList<SaveTransactionRecord> records)
async Task<(TrackedTransaction[] TrackedTransactions, SaveTransactionRecord[] Saved)> SaveMatches(DbConnectionHelper connection, MatchQuery matchQuery, IList<SaveTransactionRecord> records, CancellationToken cancellationToken = default)
{
HashSet<uint256> unconfTxs = await connection.GetUnconfirmedTxs();
Dictionary<uint256, SaveTransactionRecord> txs = new();
Expand All @@ -581,7 +580,7 @@ public async Task<TrackedTransaction[]> GetMatches(DbConnectionHelper connection

var elementContext = Network.IsElement ? new ElementMatchContext() : null;

if (!await connection.FetchMatches(matchQuery))
if (!await connection.FetchMatches(matchQuery, cancellationToken))
goto end;
using (var result = await connection.Connection.QueryMultipleAsync(
"SELECT * FROM matched_outs;" +
Expand Down
6 changes: 5 additions & 1 deletion NBXplorer/HostedServices/RefreshWalletHistoryPeriodicTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public RefreshWalletHistoryPeriodicTask(DbConnectionFactory connectionFactory)
public async Task Do(CancellationToken cancellationToken)
{
await using var conn = await _DS.ReliableOpenConnectionAsync();
await conn.ExecuteAsync("SELECT wallets_history_refresh();");
var command = new CommandDefinition(
commandText: "SELECT wallets_history_refresh();",
commandType: System.Data.CommandType.Text,
cancellationToken: cancellationToken);
await conn.ExecuteAsync(command);
}

public ValueTask DisposeAsync()
Expand Down

0 comments on commit 24d24f2

Please sign in to comment.