Skip to content

Commit

Permalink
Add implicit sort by level for transactions and originations
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Feb 21, 2024
1 parent befb3c9 commit c0d71f8
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 40 deletions.
8 changes: 4 additions & 4 deletions Tzkt.Api/Controllers/OperationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2018,7 +2018,7 @@ public async Task<ActionResult<IEnumerable<OriginationOperation>>> GetOriginatio
Int32Parameter typeHash,
Int32Parameter codeHash,
Int32Parameter level,
DateTimeParameter timestamp,
TimestampParameter timestamp,
Int32Parameter senderCodeHash,
Int32Parameter anyCodeHash,
OperationStatusParameter status,
Expand Down Expand Up @@ -2201,7 +2201,7 @@ public async Task<ActionResult<IEnumerable<OriginationOperation>>> GetOriginatio
[HttpGet("originations/count")]
public async Task<ActionResult<int>> GetOriginationsCount(
Int32Parameter level,
DateTimeParameter timestamp)
TimestampParameter timestamp)
{
if (level == null && timestamp == null)
return Ok(State.Current.OriginationOpsCount);
Expand Down Expand Up @@ -2260,7 +2260,7 @@ public async Task<ActionResult<IEnumerable<TransactionOperation>>> GetTransactio
Int64Parameter amount,
Int64Parameter id,
Int32Parameter level,
DateTimeParameter timestamp,
TimestampParameter timestamp,
Int32Parameter senderCodeHash,
Int32Parameter targetCodeHash,
Int32Parameter codeHash,
Expand Down Expand Up @@ -2501,7 +2501,7 @@ public async Task<ActionResult<int>> GetTransactionsCount(
AccountParameter target,
Int64Parameter amount,
Int32Parameter level,
DateTimeParameter timestamp,
TimestampParameter timestamp,
StringParameter entrypoint,
JsonParameter parameter,
OperationStatusParameter status)
Expand Down
36 changes: 26 additions & 10 deletions Tzkt.Api/Repositories/AccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,22 @@ public async Task<IEnumerable<Operation>> GetOperations(
limit = limit
};

TimestampParameter _timestamp = null;
if (timestamp != null)
{
_timestamp = new TimestampParameter
{
Eq = timestamp.Eq == null ? null : Time.FindLevel((DateTime)timestamp.Eq, SearchMode.Exact),
Ne = timestamp.Ne == null ? null : Time.FindLevel((DateTime)timestamp.Ne, SearchMode.Exact),
Gt = timestamp.Gt == null ? null : Time.FindLevel((DateTime)timestamp.Gt, SearchMode.ExactOrLower),
Ge = timestamp.Ge == null ? null : Time.FindLevel((DateTime)timestamp.Ge, SearchMode.ExactOrHigher),
Lt = timestamp.Lt == null ? null : Time.FindLevel((DateTime)timestamp.Lt, SearchMode.ExactOrHigher),
Le = timestamp.Le == null ? null : Time.FindLevel((DateTime)timestamp.Le, SearchMode.ExactOrLower),
In = timestamp.In?.Select(x => Time.FindLevel(x, SearchMode.Exact)).ToList(),
Ni = timestamp.Ni?.Select(x => Time.FindLevel(x, SearchMode.Exact)).ToList(),
};
}

switch (account)
{
case RawDelegate delegat:
Expand Down Expand Up @@ -2208,11 +2224,11 @@ public async Task<IEnumerable<Operation>> GetOperations(
: Task.FromResult(Enumerable.Empty<DelegationOperation>());

var originations = delegat.OriginationsCount > 0 && types.Contains(OpTypes.Origination)
? Operations.GetOriginations(new AnyOfParameter { Fields = new[] { "initiator", "sender", "contractManager", "contractDelegate", "originatedContract" }, Eq = delegat.Id }, initiator, sender, contractManager, contractDelegate, originatedContract, null, null, null, level, timestamp, null, null, status, sort, offset, limit, format, quote)
? Operations.GetOriginations(new AnyOfParameter { Fields = new[] { "initiator", "sender", "contractManager", "contractDelegate", "originatedContract" }, Eq = delegat.Id }, initiator, sender, contractManager, contractDelegate, originatedContract, null, null, null, level, _timestamp, null, null, status, sort, offset, limit, format, quote)
: Task.FromResult(Enumerable.Empty<OriginationOperation>());

var transactions = delegat.TransactionsCount > 0 && types.Contains(OpTypes.Transaction)
? Operations.GetTransactions(new AnyOfParameter { Fields = new[] { "initiator", "sender", "target" }, Eq = delegat.Id }, initiator, sender, target, null, null, level, timestamp, null, null, null, entrypoint, parameter, hasInternals, status, sort, offset, limit, format, quote)
? Operations.GetTransactions(new AnyOfParameter { Fields = new[] { "initiator", "sender", "target" }, Eq = delegat.Id }, initiator, sender, target, null, null, level, _timestamp, null, null, null, entrypoint, parameter, hasInternals, status, sort, offset, limit, format, quote)
: Task.FromResult(Enumerable.Empty<TransactionOperation>());

var reveals = delegat.RevealsCount > 0 && types.Contains(OpTypes.Reveal)
Expand Down Expand Up @@ -2425,11 +2441,11 @@ await Task.WhenAll(
: Task.FromResult(Enumerable.Empty<DelegationOperation>());

var userOriginations = user.OriginationsCount > 0 && types.Contains(OpTypes.Origination)
? Operations.GetOriginations(new AnyOfParameter { Fields = new[] { "initiator", "sender", "contractManager", "contractDelegate", "originatedContract" }, Eq = user.Id }, initiator, sender, contractManager, contractDelegate, originatedContract, null, null, null, level, timestamp, null, null, status, sort, offset, limit, format, quote)
? Operations.GetOriginations(new AnyOfParameter { Fields = new[] { "initiator", "sender", "contractManager", "contractDelegate", "originatedContract" }, Eq = user.Id }, initiator, sender, contractManager, contractDelegate, originatedContract, null, null, null, level, _timestamp, null, null, status, sort, offset, limit, format, quote)
: Task.FromResult(Enumerable.Empty<OriginationOperation>());

var userTransactions = user.TransactionsCount > 0 && types.Contains(OpTypes.Transaction)
? Operations.GetTransactions(new AnyOfParameter { Fields = new[] { "initiator", "sender", "target" }, Eq = user.Id }, initiator, sender, target, null, null, level, timestamp, null, null, null, entrypoint, parameter, hasInternals, status, sort, offset, limit, format, quote)
? Operations.GetTransactions(new AnyOfParameter { Fields = new[] { "initiator", "sender", "target" }, Eq = user.Id }, initiator, sender, target, null, null, level, _timestamp, null, null, null, entrypoint, parameter, hasInternals, status, sort, offset, limit, format, quote)
: Task.FromResult(Enumerable.Empty<TransactionOperation>());

var userReveals = user.RevealsCount > 0 && types.Contains(OpTypes.Reveal)
Expand Down Expand Up @@ -2590,19 +2606,19 @@ await Task.WhenAll(
: Task.FromResult(Enumerable.Empty<DelegationOperation>());

var contractOriginations = contract.OriginationsCount > 0 && types.Contains(OpTypes.Origination)
? Operations.GetOriginations(new AnyOfParameter { Fields = new[] { "initiator", "sender", "contractManager", "contractDelegate", "originatedContract" }, Eq = contract.Id }, initiator, sender, contractManager, contractDelegate, originatedContract, null, null, null, level, timestamp, null, null, status, sort, offset, limit, format, quote)
? Operations.GetOriginations(new AnyOfParameter { Fields = new[] { "initiator", "sender", "contractManager", "contractDelegate", "originatedContract" }, Eq = contract.Id }, initiator, sender, contractManager, contractDelegate, originatedContract, null, null, null, level, _timestamp, null, null, status, sort, offset, limit, format, quote)
: Task.FromResult(Enumerable.Empty<OriginationOperation>());

var contractTransactions1 = contract.TransactionsCount > 0 && types.Contains(OpTypes.Transaction) && contract.Kind == 0
? Operations.GetTransactions(new AnyOfParameter { Fields = new[] { "initiator" }, Eq = contract.Id }, initiator, sender, target, null, null, level, timestamp, null, null, null, entrypoint, parameter, hasInternals, status, sort, offset, limit, format, quote)
? Operations.GetTransactions(new AnyOfParameter { Fields = new[] { "initiator" }, Eq = contract.Id }, initiator, sender, target, null, null, level, _timestamp, null, null, null, entrypoint, parameter, hasInternals, status, sort, offset, limit, format, quote)
: Task.FromResult(Enumerable.Empty<TransactionOperation>());

var contractTransactions2 = contract.TransactionsCount > 0 && types.Contains(OpTypes.Transaction)
? Operations.GetTransactions(new AnyOfParameter { Fields = new[] { "sender" }, Eq = contract.Id }, initiator, sender, target, null, null, level, timestamp, null, null, null, entrypoint, parameter, hasInternals, status, sort, offset, limit, format, quote)
? Operations.GetTransactions(new AnyOfParameter { Fields = new[] { "sender" }, Eq = contract.Id }, initiator, sender, target, null, null, level, _timestamp, null, null, null, entrypoint, parameter, hasInternals, status, sort, offset, limit, format, quote)
: Task.FromResult(Enumerable.Empty<TransactionOperation>());

var contractTransactions3 = contract.TransactionsCount > 0 && types.Contains(OpTypes.Transaction)
? Operations.GetTransactions(new AnyOfParameter { Fields = new[] { "target" }, Eq = contract.Id }, initiator, sender, target, null, null, level, timestamp, null, null, null, entrypoint, parameter, hasInternals, status, sort, offset, limit, format, quote)
? Operations.GetTransactions(new AnyOfParameter { Fields = new[] { "target" }, Eq = contract.Id }, initiator, sender, target, null, null, level, _timestamp, null, null, null, entrypoint, parameter, hasInternals, status, sort, offset, limit, format, quote)
: Task.FromResult(Enumerable.Empty<TransactionOperation>());

var contractReveals = contract.RevealsCount > 0 && types.Contains(OpTypes.Reveal)
Expand Down Expand Up @@ -2646,7 +2662,7 @@ await Task.WhenAll(
var _rollup = new AccountParameter { Eq = rollup.Id };

var rollupTransactionOps = rollup.TransactionsCount> 0 && types.Contains(OpTypes.Transaction)
? Operations.GetTransactions(null, null, null, _rollup, null, null, level, timestamp, null, null, null, entrypoint, parameter, hasInternals, status, sort, offset, limit, format, quote)
? Operations.GetTransactions(null, null, null, _rollup, null, null, level, _timestamp, null, null, null, entrypoint, parameter, hasInternals, status, sort, offset, limit, format, quote)
: Task.FromResult(Enumerable.Empty<TransactionOperation>());

var rollupTxRollupCommitOps = rollup.TxRollupCommitCount > 0 && types.Contains(OpTypes.TxRollupCommit)
Expand Down Expand Up @@ -2708,7 +2724,7 @@ await Task.WhenAll(
var _smartRollup = new SmartRollupParameter { Eq = smartRollup.Id };

var smartRollupTransactionOps = smartRollup.TransactionsCount > 0 && types.Contains(OpTypes.Transaction)
? Operations.GetTransactions(new() { Fields = new[] { "sender", "target" }, Eq = smartRollup.Id }, null, null, null, null, null, level, timestamp, null, null, null, entrypoint, parameter, hasInternals, status, sort, offset, limit, format, quote)
? Operations.GetTransactions(new() { Fields = new[] { "sender", "target" }, Eq = smartRollup.Id }, null, null, null, null, null, level, _timestamp, null, null, null, entrypoint, parameter, hasInternals, status, sort, offset, limit, format, quote)
: Task.FromResult(Enumerable.Empty<TransactionOperation>());

var smartRollupSrCementOps = smartRollup.SmartRollupCementCount > 0 && types.Contains(OpTypes.SmartRollupCement)
Expand Down
61 changes: 48 additions & 13 deletions Tzkt.Api/Repositories/OperationRepository.Originations.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using Dapper;
using Netezos.Encoding;
using Tzkt.Api.Models;
using Tzkt.Api.Services.Cache;
Expand All @@ -20,11 +16,11 @@ public partial class OperationRepository : DbConnection

public async Task<int> GetOriginationsCount(
Int32Parameter level,
DateTimeParameter timestamp)
TimestampParameter timestamp)
{
var sql = new SqlBuilder(@"SELECT COUNT(*) FROM ""OriginationOps""")
.Filter("Level", level)
.Filter("Timestamp", timestamp);
.Filter("Level", timestamp);

using var db = GetConnection();
return await db.QueryFirstAsync<int>(sql.Query, sql.Params);
Expand Down Expand Up @@ -370,7 +366,7 @@ public async Task<IEnumerable<OriginationOperation>> GetOriginations(
Int32Parameter typeHash,
Int32Parameter codeHash,
Int32Parameter level,
DateTimeParameter timestamp,
TimestampParameter timestamp,
Int32Parameter anyCodeHash,
Int32Parameter senderCodeHash,
OperationStatusParameter status,
Expand All @@ -382,6 +378,19 @@ public async Task<IEnumerable<OriginationOperation>> GetOriginations(
bool includeStorage = false,
bool includeBigmaps = false)
{
#region opts
if (ImplicitSortByLevel)
{
if ((level != null || timestamp != null) && offset?.Cr == null)
{
if (sort == null || sort.Asc == "id")
sort = new() { Asc = "level" };
else if (sort.Desc == "id")
sort = new() { Desc = "level" };
}
}
#endregion

var sql = new SqlBuilder($@"
SELECT o.*, b.""Hash""
FROM ""OriginationOps"" AS o
Expand All @@ -405,7 +414,7 @@ INNER JOIN ""Blocks"" as b
.FilterA(@"c.""TypeHash""", typeHash)
.FilterA(@"o.""ContractCodeHash""", codeHash)
.FilterA(@"o.""Level""", level)
.FilterA(@"o.""Timestamp""", timestamp)
.FilterA(@"o.""Level""", timestamp)
.FilterA(@"o.""SenderCodeHash""", senderCodeHash)
.Filter("Status", status)
.FilterOrA(new[] { @"o.""SenderCodeHash""", @"o.""ContractCodeHash""" }, anyCodeHash)
Expand Down Expand Up @@ -502,7 +511,7 @@ public async Task<object[][]> GetOriginations(
Int32Parameter typeHash,
Int32Parameter codeHash,
Int32Parameter level,
DateTimeParameter timestamp,
TimestampParameter timestamp,
Int32Parameter anyCodeHash,
Int32Parameter senderCodeHash,
OperationStatusParameter status,
Expand Down Expand Up @@ -569,6 +578,19 @@ public async Task<object[][]> GetOriginations(
if (typeHash != null || codeHash != null)
joins.Add(@"LEFT JOIN ""Accounts"" as c ON c.""Id"" = o.""ContractId""");

#region opts
if (ImplicitSortByLevel)
{
if ((level != null || timestamp != null) && offset?.Cr == null)
{
if (sort == null || sort.Asc == "id")
sort = new() { Asc = "level" };
else if (sort.Desc == "id")
sort = new() { Desc = "level" };
}
}
#endregion

var sql = new SqlBuilder($@"SELECT {string.Join(',', columns)} FROM ""OriginationOps"" as o {string.Join(' ', joins)}")
.Filter(anyof, x => x switch
{
Expand All @@ -587,7 +609,7 @@ public async Task<object[][]> GetOriginations(
.FilterA(@"c.""TypeHash""", typeHash)
.FilterA(@"o.""ContractCodeHash""", codeHash)
.FilterA(@"o.""Level""", level)
.FilterA(@"o.""Timestamp""", timestamp)
.FilterA(@"o.""Level""", timestamp)
.FilterA(@"o.""SenderCodeHash""", senderCodeHash)
.Filter("Status", status)
.FilterOrA(new[] { @"o.""SenderCodeHash""", @"o.""ContractCodeHash""" }, anyCodeHash)
Expand Down Expand Up @@ -779,7 +801,7 @@ public async Task<object[]> GetOriginations(
Int32Parameter typeHash,
Int32Parameter codeHash,
Int32Parameter level,
DateTimeParameter timestamp,
TimestampParameter timestamp,
Int32Parameter anyCodeHash,
Int32Parameter senderCodeHash,
OperationStatusParameter status,
Expand Down Expand Up @@ -843,6 +865,19 @@ public async Task<object[]> GetOriginations(
if (typeHash != null || codeHash != null)
joins.Add(@"LEFT JOIN ""Accounts"" as c ON c.""Id"" = o.""ContractId""");

#region opts
if (ImplicitSortByLevel)
{
if ((level != null || timestamp != null) && offset?.Cr == null)
{
if (sort == null || sort.Asc == "id")
sort = new() { Asc = "level" };
else if (sort.Desc == "id")
sort = new() { Desc = "level" };
}
}
#endregion

var sql = new SqlBuilder($@"SELECT {string.Join(',', columns)} FROM ""OriginationOps"" as o {string.Join(' ', joins)}")
.Filter(anyof, x => x switch
{
Expand All @@ -861,7 +896,7 @@ public async Task<object[]> GetOriginations(
.FilterA(@"c.""TypeHash""", typeHash)
.FilterA(@"o.""ContractCodeHash""", codeHash)
.FilterA(@"o.""Level""", level)
.FilterA(@"o.""Timestamp""", timestamp)
.FilterA(@"o.""Level""", timestamp)
.FilterA(@"o.""SenderCodeHash""", senderCodeHash)
.Filter("Status", status)
.FilterOrA(new[] { @"o.""SenderCodeHash""", @"o.""ContractCodeHash""" }, anyCodeHash)
Expand Down
Loading

0 comments on commit c0d71f8

Please sign in to comment.