Skip to content

Commit

Permalink
Add filter by id to /revelation_penalties and /endorsing_rewards endp…
Browse files Browse the repository at this point in the history
…oints
  • Loading branch information
Groxan committed Nov 22, 2023
1 parent b0bca66 commit 3f36cd2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 18 deletions.
28 changes: 16 additions & 12 deletions Tzkt.Api/Controllers/OperationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5182,6 +5182,7 @@ public async Task<ActionResult<int>> GetMigrationsCount(
/// <remarks>
/// Returns a list of revelation penalty operations (synthetic type).
/// </remarks>
/// <param name="id">Filters operations by internal TzKT id.</param>
/// <param name="baker">Filters revelation penalty operations by baker. Allowed fields for `.eqx` mode: none.</param>
/// <param name="level">Filters revelation penalty operations by level.</param>
/// <param name="timestamp">Filters revelation penalty operations by timestamp.</param>
Expand All @@ -5193,6 +5194,7 @@ public async Task<ActionResult<int>> GetMigrationsCount(
/// <returns></returns>
[HttpGet("revelation_penalties")]
public async Task<ActionResult<IEnumerable<RevelationPenaltyOperation>>> GetRevelationPenalties(
Int64Parameter id,
AccountParameter baker,
Int32Parameter level,
DateTimeParameter timestamp,
Expand Down Expand Up @@ -5220,7 +5222,7 @@ public async Task<ActionResult<IEnumerable<RevelationPenaltyOperation>>> GetReve
#endregion

var query = ResponseCacheService.BuildKey(Request.Path.Value,
("baker", baker), ("level", level), ("timestamp", timestamp),
("id", id), ("baker", baker), ("level", level), ("timestamp", timestamp),
("select", select), ("sort", sort), ("offset", offset), ("limit", limit), ("quote", quote));

if (ResponseCache.TryGet(query, out var cached))
Expand All @@ -5229,25 +5231,25 @@ public async Task<ActionResult<IEnumerable<RevelationPenaltyOperation>>> GetReve
object res;
if (select == null)
{
res = await Operations.GetRevelationPenalties(baker, level, timestamp, sort, offset, limit, quote);
res = await Operations.GetRevelationPenalties(id, baker, level, timestamp, sort, offset, limit, quote);
}
else if (select.Values != null)
{
if (select.Values.Length == 1)
res = await Operations.GetRevelationPenalties(baker, level, timestamp, sort, offset, limit, select.Values[0], quote);
res = await Operations.GetRevelationPenalties(id, baker, level, timestamp, sort, offset, limit, select.Values[0], quote);
else
res = await Operations.GetRevelationPenalties(baker, level, timestamp, sort, offset, limit, select.Values, quote);
res = await Operations.GetRevelationPenalties(id, baker, level, timestamp, sort, offset, limit, select.Values, quote);
}
else
{
if (select.Fields.Length == 1)
res = await Operations.GetRevelationPenalties(baker, level, timestamp, sort, offset, limit, select.Fields[0], quote);
res = await Operations.GetRevelationPenalties(id, baker, level, timestamp, sort, offset, limit, select.Fields[0], quote);
else
{
res = new SelectionResponse
{
Cols = select.Fields,
Rows = await Operations.GetRevelationPenalties(baker, level, timestamp, sort, offset, limit, select.Fields, quote)
Rows = await Operations.GetRevelationPenalties(id, baker, level, timestamp, sort, offset, limit, select.Fields, quote)
};
}
}
Expand Down Expand Up @@ -5481,6 +5483,7 @@ public async Task<ActionResult<int>> GetBakingCount(
/// <remarks>
/// Returns a list of endorsing reward operations (synthetic type).
/// </remarks>
/// <param name="id">Filters operations by internal TzKT id.</param>
/// <param name="baker">Filters by baker. Allowed fields for `.eqx` mode: none.</param>
/// <param name="level">Filters by level.</param>
/// <param name="timestamp">Filters by timestamp.</param>
Expand All @@ -5492,6 +5495,7 @@ public async Task<ActionResult<int>> GetBakingCount(
/// <returns></returns>
[HttpGet("endorsing_rewards")]
public async Task<ActionResult<IEnumerable<EndorsingRewardOperation>>> GetEndorsingRewards(
Int64Parameter id,
AccountParameter baker,
Int32Parameter level,
DateTimeParameter timestamp,
Expand Down Expand Up @@ -5519,7 +5523,7 @@ public async Task<ActionResult<IEnumerable<EndorsingRewardOperation>>> GetEndors
#endregion

var query = ResponseCacheService.BuildKey(Request.Path.Value,
("baker", baker), ("level", level), ("timestamp", timestamp),
("id", id), ("baker", baker), ("level", level), ("timestamp", timestamp),
("select", select), ("sort", sort), ("offset", offset), ("limit", limit), ("quote", quote));

if (ResponseCache.TryGet(query, out var cached))
Expand All @@ -5528,25 +5532,25 @@ public async Task<ActionResult<IEnumerable<EndorsingRewardOperation>>> GetEndors
object res;
if (select == null)
{
res = await Operations.GetEndorsingRewards(baker, level, timestamp, sort, offset, limit, quote);
res = await Operations.GetEndorsingRewards(id, baker, level, timestamp, sort, offset, limit, quote);
}
else if (select.Values != null)
{
if (select.Values.Length == 1)
res = await Operations.GetEndorsingRewards(baker, level, timestamp, sort, offset, limit, select.Values[0], quote);
res = await Operations.GetEndorsingRewards(id, baker, level, timestamp, sort, offset, limit, select.Values[0], quote);
else
res = await Operations.GetEndorsingRewards(baker, level, timestamp, sort, offset, limit, select.Values, quote);
res = await Operations.GetEndorsingRewards(id, baker, level, timestamp, sort, offset, limit, select.Values, quote);
}
else
{
if (select.Fields.Length == 1)
res = await Operations.GetEndorsingRewards(baker, level, timestamp, sort, offset, limit, select.Fields[0], quote);
res = await Operations.GetEndorsingRewards(id, baker, level, timestamp, sort, offset, limit, select.Fields[0], quote);
else
{
res = new SelectionResponse
{
Cols = select.Fields,
Rows = await Operations.GetEndorsingRewards(baker, level, timestamp, sort, offset, limit, select.Fields, quote)
Rows = await Operations.GetEndorsingRewards(id, baker, level, timestamp, sort, offset, limit, select.Fields, quote)
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions Tzkt.Api/Repositories/AccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2130,15 +2130,15 @@ public async Task<IEnumerable<Operation>> GetOperations(
: Task.FromResult(Enumerable.Empty<MigrationOperation>());

var revelationPenalties = delegat.RevelationPenaltiesCount > 0 && types.Contains(OpTypes.RevelationPenalty)
? Operations.GetRevelationPenalties(_delegat, level, timestamp, sort, offset, limit, quote)
? Operations.GetRevelationPenalties(null, _delegat, level, timestamp, sort, offset, limit, quote)
: Task.FromResult(Enumerable.Empty<RevelationPenaltyOperation>());

var bakingOps = delegat.BlocksCount > 0 && types.Contains(OpTypes.Baking)
? Operations.GetBakings(new AnyOfParameter { Fields = new[] { "proposer", "producer" }, Eq = delegat.Id }, null, null, level, timestamp, sort, offset, limit, quote)
: Task.FromResult(Enumerable.Empty<BakingOperation>());

var endorsingRewards = delegat.EndorsingRewardsCount > 0 && types.Contains(OpTypes.EndorsingReward)
? Operations.GetEndorsingRewards(_delegat, level, timestamp, sort, offset, limit, quote)
? Operations.GetEndorsingRewards(null, _delegat, level, timestamp, sort, offset, limit, quote)
: Task.FromResult(Enumerable.Empty<EndorsingRewardOperation>());

await Task.WhenAll(
Expand Down
4 changes: 2 additions & 2 deletions Tzkt.Api/Repositories/BlockRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -636,11 +636,11 @@ async Task LoadOperations(Block block, Data.Models.Operations operations, Michel
: Task.FromResult(Enumerable.Empty<MigrationOperation>());

var penalties = operations.HasFlag(Data.Models.Operations.RevelationPenalty)
? Operations.GetRevelationPenalties(null, new Int32Parameter { Eq = block.Level }, null, null, null, 10_000, quote)
? Operations.GetRevelationPenalties(null, null, new Int32Parameter { Eq = block.Level }, null, null, null, 10_000, quote)
: Task.FromResult(Enumerable.Empty<RevelationPenaltyOperation>());

var endorsingRewards = operations.HasFlag(Data.Models.Operations.EndorsingRewards)
? Operations.GetEndorsingRewards(null, new Int32Parameter { Eq = block.Level }, null, null, null, 10_000, quote)
? Operations.GetEndorsingRewards(null, null, new Int32Parameter { Eq = block.Level }, null, null, null, 10_000, quote)
: Task.FromResult(Enumerable.Empty<EndorsingRewardOperation>());

await Task.WhenAll(
Expand Down
6 changes: 6 additions & 0 deletions Tzkt.Api/Repositories/OperationRepository.EndorsingRewards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ INNER JOIN ""Blocks"" as b
}

public async Task<IEnumerable<EndorsingRewardOperation>> GetEndorsingRewards(
Int64Parameter id,
AccountParameter baker,
Int32Parameter level,
DateTimeParameter timestamp,
Expand All @@ -58,6 +59,7 @@ public async Task<IEnumerable<EndorsingRewardOperation>> GetEndorsingRewards(
Symbols quote)
{
var sql = new SqlBuilder(@"SELECT o.*, b.""Hash"" FROM ""EndorsingRewardOps"" AS o INNER JOIN ""Blocks"" as b ON b.""Level"" = o.""Level""")
.FilterA(@"o.""Id""", id)
.FilterA(@"o.""BakerId""", baker)
.FilterA(@"o.""Level""", level)
.FilterA(@"o.""Timestamp""", timestamp)
Expand All @@ -80,6 +82,7 @@ public async Task<IEnumerable<EndorsingRewardOperation>> GetEndorsingRewards(
}

public async Task<object[][]> GetEndorsingRewards(
Int64Parameter id,
AccountParameter baker,
Int32Parameter level,
DateTimeParameter timestamp,
Expand Down Expand Up @@ -114,6 +117,7 @@ public async Task<object[][]> GetEndorsingRewards(
return Array.Empty<object[]>();

var sql = new SqlBuilder($@"SELECT {string.Join(',', columns)} FROM ""EndorsingRewardOps"" as o {string.Join(' ', joins)}")
.FilterA(@"o.""Id""", id)
.FilterA(@"o.""BakerId""", baker)
.FilterA(@"o.""Level""", level)
.FilterA(@"o.""Timestamp""", timestamp)
Expand Down Expand Up @@ -169,6 +173,7 @@ public async Task<object[][]> GetEndorsingRewards(
}

public async Task<object[]> GetEndorsingRewards(
Int64Parameter id,
AccountParameter baker,
Int32Parameter level,
DateTimeParameter timestamp,
Expand Down Expand Up @@ -200,6 +205,7 @@ public async Task<object[]> GetEndorsingRewards(
return Array.Empty<object>();

var sql = new SqlBuilder($@"SELECT {string.Join(',', columns)} FROM ""EndorsingRewardOps"" as o {string.Join(' ', joins)}")
.FilterA(@"o.""Id""", id)
.FilterA(@"o.""BakerId""", baker)
.FilterA(@"o.""Level""", level)
.FilterA(@"o.""Timestamp""", timestamp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ INNER JOIN ""Blocks"" as b
}

public async Task<IEnumerable<RevelationPenaltyOperation>> GetRevelationPenalties(
Int64Parameter id,
AccountParameter baker,
Int32Parameter level,
DateTimeParameter timestamp,
Expand All @@ -58,6 +59,7 @@ public async Task<IEnumerable<RevelationPenaltyOperation>> GetRevelationPenaltie
Symbols quote)
{
var sql = new SqlBuilder(@"SELECT o.*, b.""Hash"" FROM ""RevelationPenaltyOps"" AS o INNER JOIN ""Blocks"" as b ON b.""Level"" = o.""Level""")
.FilterA(@"o.""Id""", id)
.FilterA(@"o.""BakerId""", baker)
.FilterA(@"o.""Level""", level)
.FilterA(@"o.""Timestamp""", timestamp)
Expand All @@ -80,6 +82,7 @@ public async Task<IEnumerable<RevelationPenaltyOperation>> GetRevelationPenaltie
}

public async Task<object[][]> GetRevelationPenalties(
Int64Parameter id,
AccountParameter baker,
Int32Parameter level,
DateTimeParameter timestamp,
Expand Down Expand Up @@ -114,6 +117,7 @@ public async Task<object[][]> GetRevelationPenalties(
return Array.Empty<object[]>();

var sql = new SqlBuilder($@"SELECT {string.Join(',', columns)} FROM ""RevelationPenaltyOps"" as o {string.Join(' ', joins)}")
.FilterA(@"o.""Id""", id)
.FilterA(@"o.""BakerId""", baker)
.FilterA(@"o.""Level""", level)
.FilterA(@"o.""Timestamp""", timestamp)
Expand Down Expand Up @@ -169,6 +173,7 @@ public async Task<object[][]> GetRevelationPenalties(
}

public async Task<object[]> GetRevelationPenalties(
Int64Parameter id,
AccountParameter baker,
Int32Parameter level,
DateTimeParameter timestamp,
Expand Down Expand Up @@ -200,6 +205,7 @@ public async Task<object[]> GetRevelationPenalties(
return Array.Empty<object>();

var sql = new SqlBuilder($@"SELECT {string.Join(',', columns)} FROM ""RevelationPenaltyOps"" as o {string.Join(' ', joins)}")
.FilterA(@"o.""Id""", id)
.FilterA(@"o.""BakerId""", baker)
.FilterA(@"o.""Level""", level)
.FilterA(@"o.""Timestamp""", timestamp)
Expand Down
4 changes: 2 additions & 2 deletions Tzkt.Api/Websocket/Processors/OperationsProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,15 @@ public async Task OnStateChanged()
: Task.FromResult(Enumerable.Empty<Models.MigrationOperation>());

var penalties = TypeSubs.TryGetValue(Operations.RevelationPenalty, out var penaltiesSub)
? Repo.GetRevelationPenalties(null, level, null, null, null, limit, symbols)
? Repo.GetRevelationPenalties(null, null, level, null, null, null, limit, symbols)
: Task.FromResult(Enumerable.Empty<Models.RevelationPenaltyOperation>());

var baking = TypeSubs.TryGetValue(Operations.Baking, out var bakingSub)
? Repo.GetBakings(null, null, null, level, null, null, null, limit, symbols)
: Task.FromResult(Enumerable.Empty<Models.BakingOperation>());

var endorsingRewards = TypeSubs.TryGetValue(Operations.EndorsingRewards, out var endorsingRewardsSub)
? Repo.GetEndorsingRewards(null, level, null, null, null, limit, symbols)
? Repo.GetEndorsingRewards(null, null, level, null, null, null, limit, symbols)
: Task.FromResult(Enumerable.Empty<Models.EndorsingRewardOperation>());

await Task.WhenAll(
Expand Down

0 comments on commit 3f36cd2

Please sign in to comment.