Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add timestamp limits to getTransactions #473

Closed
wants to merge 5 commits into from
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
21 changes: 19 additions & 2 deletions NBXplorer/Backend/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -742,16 +742,33 @@ public async Task<SavedTransaction[]> GetSavedTransactions(uint256 txid)
}};
}

public async Task<TrackedTransaction[]> GetTransactions(TrackedSource trackedSource, uint256 txId = null, bool includeTransactions = true, CancellationToken cancellation = default)
public async Task<TrackedTransaction[]> GetTransactions(
TrackedSource trackedSource,
uint256 txId = null,
bool includeTransactions = true,
ulong? fromSeen = null,
ulong? toSeen = null,
CancellationToken cancellation = default)
{
await using var connection = await connectionFactory.CreateConnectionHelper(Network);
var tip = await connection.GetTip();
var txIdCond = txId is null ? string.Empty : " AND tx_id=@tx_id";

var fromSeenCond = fromSeen is null ? string.Empty : " AND seen_at>=@fromSeen";
var toSeenCond = toSeen is null ? string.Empty : " AND seen_at<=@toSeen";

var utxos = await
connection.Connection.QueryAsync<(string tx_id, long idx, string blk_id, long? blk_height, int? blk_idx, bool is_out, string spent_tx_id, long spent_idx, string script, string addr, long value, string asset_id, bool immature, string keypath, DateTime seen_at)>(
"SELECT tx_id, idx, blk_id, blk_height, blk_idx, is_out, spent_tx_id, spent_idx, script, s.addr, value, asset_id, immature, keypath, seen_at " +
"FROM nbxv1_tracked_txs LEFT JOIN scripts s USING (code, script) " +
$"WHERE code=@code AND wallet_id=@walletId{txIdCond}", new { code = Network.CryptoCode, walletId = GetWalletKey(trackedSource).wid, tx_id = txId?.ToString() });
$"WHERE code=@code AND wallet_id=@walletId{txIdCond}{fromSeenCond}{toSeenCond}", new {
code = Network.CryptoCode,
walletId = GetWalletKey(trackedSource).wid,
tx_id = txId?.ToString(),
fromSeen = NBitcoin.Utils.UnixTimeToDateTime(fromSeen ?? 0),
toSeen = NBitcoin.Utils.UnixTimeToDateTime(toSeen ?? 0)
}
);
utxos.TryGetNonEnumeratedCount(out int c);
var trackedById = new Dictionary<string, TrackedTransaction>(c);
foreach (var utxo in utxos)
Expand Down
18 changes: 14 additions & 4 deletions NBXplorer/Controllers/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,10 @@ public async Task<IActionResult> GetTransactions(
TrackedSourceContext trackedSourceContext,
[ModelBinder(BinderType = typeof(UInt256ModelBinding))]
uint256 txId = null,
bool includeTransaction = true)
bool includeTransaction = true,
ulong? fromSeen = null,
ulong? toSeen = null
)
{
TransactionInformation fetchedTransactionInfo = null;
var network = trackedSourceContext.Network;
Expand All @@ -533,7 +536,7 @@ public async Task<IActionResult> GetTransactions(
var response = new GetTransactionsResponse();
int currentHeight = (await repo.GetTip()).Height;
response.Height = currentHeight;
var txs = await GetAnnotatedTransactions(repo, trackedSource, includeTransaction, txId);
var txs = await GetAnnotatedTransactions(repo, trackedSource, includeTransaction, txId, fromSeen, toSeen);
foreach (var item in new[]
{
new
Expand Down Expand Up @@ -717,9 +720,16 @@ public async Task<IActionResult> Rescan(TrackedSourceContext trackedSourceContex
}
}

internal async Task<AnnotatedTransactionCollection> GetAnnotatedTransactions(Repository repo, TrackedSource trackedSource, bool includeTransaction, uint256 txId = null)
internal async Task<AnnotatedTransactionCollection> GetAnnotatedTransactions(
Repository repo,
TrackedSource trackedSource,
bool includeTransaction,
uint256 txId = null,
ulong? fromSeen = null,
ulong? toSeen = null
)
{
var transactions = await repo.GetTransactions(trackedSource, txId, includeTransaction, this.HttpContext?.RequestAborted ?? default);
var transactions = await repo.GetTransactions(trackedSource, txId, includeTransaction, fromSeen, toSeen, this.HttpContext?.RequestAborted ?? default);

// If the called is interested by only a single txId, we need to fetch the parents as well
if (txId != null)
Expand Down
8 changes: 8 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ Optional Parameters:

* `includeTransaction` includes the hex of the transaction, not only information (default: true)

* `fromSeen` indicates the earliest date from which transactions should be retrieved, (default: null, unix time)

* `toSeen` indicates the latest date up to which transactions should be retrieved, (default: null, unix time)

Returns:

```json
Expand Down Expand Up @@ -376,6 +380,10 @@ Optional Parameters:

* `includeTransaction` includes the hex of the transaction, not only information (default: true)

* `fromSeen` indicates the earliest date from which transactions should be retrieved, (default: null, unix time)

* `toSeen` indicates the latest date up to which transactions should be retrieved, (default: null, unix time)

Error codes:

* HTTP 404: Transaction not found
Expand Down