Skip to content

Commit

Permalink
[DAL] compute dal shards distribution (baking-bad#19)
Browse files Browse the repository at this point in the history
* [data] add the field `DalShards` to the table `BakingRights`

In order to keep track of the DAL shards distribution

* [dal-shards] compute and index DAL shards

Shards are the `number_of_shards` first slots

* [api] Expose `DalShards` in `/v1/rights` endpoint's response

* [api] allow to filter rights by `dalShards`

For the `/v1/rights/count` and `/v1/rights` entrypoints
  • Loading branch information
spalmer25 authored Aug 13, 2024
1 parent 3733997 commit 107f27b
Show file tree
Hide file tree
Showing 12 changed files with 7,115 additions and 21 deletions.
16 changes: 10 additions & 6 deletions Tzkt.Api/Controllers/RightsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public RightsController(BakingRightsRepository bakingRights)
/// <param name="cycle">Filters rights by cycle</param>
/// <param name="level">Filters rights by level</param>
/// <param name="slots">Filters rights by slots</param>
/// <param name="dalShards">Filters rights by DAL shards</param>
/// <param name="round">Filters rights by round</param>
/// <param name="priority">[DEPRECATED]</param>
/// <param name="status">Filters rights by status (`future`, `realized`, `missed`)</param>
Expand All @@ -38,11 +39,12 @@ public Task<int> GetCount(
Int32Parameter cycle,
Int32Parameter level,
Int32NullParameter slots,
Int32NullParameter dalShards,
Int32NullParameter round,
Int32NullParameter priority,
BakingRightStatusParameter status)
{
return BakingRights.GetCount(type, baker, cycle, level, slots, round ?? priority, status);
return BakingRights.GetCount(type, baker, cycle, level, slots, dalShards, round ?? priority, status);
}

/// <summary>
Expand All @@ -56,6 +58,7 @@ public Task<int> GetCount(
/// <param name="cycle">Filters rights by cycle</param>
/// <param name="level">Filters rights by level</param>
/// <param name="slots">Filters rights by slots</param>
/// <param name="dalShards">Filters rights by DAL shards</param>
/// <param name="round">Filters rights by round</param>
/// <param name="priority">[DEPRECATED]</param>
/// <param name="status">Filters rights by status (`future`, `realized`, `missed`)</param>
Expand All @@ -71,6 +74,7 @@ public async Task<ActionResult<IEnumerable<BakingRight>>> Get(
Int32Parameter cycle,
Int32Parameter level,
Int32NullParameter slots,
Int32NullParameter dalShards,
Int32NullParameter round,
Int32NullParameter priority,
BakingRightStatusParameter status,
Expand All @@ -85,25 +89,25 @@ public async Task<ActionResult<IEnumerable<BakingRight>>> Get(
#endregion

if (select == null)
return Ok(await BakingRights.Get(type, baker, cycle, level, slots, round ?? priority, status, sort, offset, limit));
return Ok(await BakingRights.Get(type, baker, cycle, level, slots, dalShards, round ?? priority, status, sort, offset, limit));

if (select.Values != null)
{
if (select.Values.Length == 1)
return Ok(await BakingRights.Get(type, baker, cycle, level, slots, round ?? priority, status, sort, offset, limit, select.Values[0]));
return Ok(await BakingRights.Get(type, baker, cycle, level, slots, dalShards, round ?? priority, status, sort, offset, limit, select.Values[0]));
else
return Ok(await BakingRights.Get(type, baker, cycle, level, slots, round ?? priority, status, sort, offset, limit, select.Values));
return Ok(await BakingRights.Get(type, baker, cycle, level, slots, dalShards, round ?? priority, status, sort, offset, limit, select.Values));
}
else
{
if (select.Fields.Length == 1)
return Ok(await BakingRights.Get(type, baker, cycle, level, slots, round ?? priority, status, sort, offset, limit, select.Fields[0]));
return Ok(await BakingRights.Get(type, baker, cycle, level, slots, dalShards, round ?? priority, status, sort, offset, limit, select.Fields[0]));
else
{
return Ok(new SelectionResponse
{
Cols = select.Fields,
Rows = await BakingRights.Get(type, baker, cycle, level, slots, round ?? priority, status, sort, offset, limit, select.Fields)
Rows = await BakingRights.Get(type, baker, cycle, level, slots, dalShards, round ?? priority, status, sort, offset, limit, select.Fields)
});
}
}
Expand Down
5 changes: 5 additions & 0 deletions Tzkt.Api/Models/Baking/BakingRight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public class BakingRight
/// </summary>
public int? Slots { get; set; }

/// <summary>
/// Number of DAL shards to be attested. For `baking` rights this field is always `null`.
/// </summary>
public int? DalShards { get; set; }

/// <summary>
/// Baker to which baking or endorsing right has been given.
/// </summary>
Expand Down
21 changes: 20 additions & 1 deletion Tzkt.Api/Repositories/BakingRightsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public async Task<int> GetCount(
Int32Parameter cycle,
Int32Parameter level,
Int32NullParameter slots,
Int32NullParameter dalShards,
Int32NullParameter round,
BakingRightStatusParameter status)
{
Expand All @@ -38,7 +39,8 @@ public async Task<int> GetCount(
.Filter("Type", type)
.Filter("Status", status)
.Filter("Round", round)
.Filter("Slots", slots);
.Filter("Slots", slots)
.Filter("DalShards", dalShards);

await using var db = await DataSource.OpenConnectionAsync();
return await db.QueryFirstAsync<int>(sql.Query, sql.Params);
Expand All @@ -50,6 +52,7 @@ public async Task<IEnumerable<BakingRight>> Get(
Int32Parameter cycle,
Int32Parameter level,
Int32NullParameter slots,
Int32NullParameter dalShards,
Int32NullParameter round,
BakingRightStatusParameter status,
SortParameter sort,
Expand All @@ -64,6 +67,7 @@ public async Task<IEnumerable<BakingRight>> Get(
.Filter("Status", status)
.Filter("Round", round)
.Filter("Slots", slots)
.Filter("DalShards", dalShards)
.Take(sort ?? new SortParameter { Asc = "level" }, offset, limit, x => ("Level", "Level"));

await using var db = await DataSource.OpenConnectionAsync();
Expand All @@ -78,6 +82,7 @@ public async Task<IEnumerable<BakingRight>> Get(
Baker = Accounts.GetAlias(row.BakerId),
Round = row.Round,
Slots = row.Slots,
DalShards = row.DalShards,
Status = BakingRightStatuses.ToString(row.Status)
});
}
Expand All @@ -88,6 +93,7 @@ public async Task<object[][]> Get(
Int32Parameter cycle,
Int32Parameter level,
Int32NullParameter slots,
Int32NullParameter dalShards,
Int32NullParameter round,
BakingRightStatusParameter status,
SortParameter sort,
Expand All @@ -107,6 +113,7 @@ public async Task<object[][]> Get(
case "baker": columns.Add(@"""BakerId"""); break;
case "round": columns.Add(@"""Round"""); break;
case "slots": columns.Add(@"""Slots"""); break;
case "dalShards": columns.Add(@"""DalShards"""); break;
case "status": columns.Add(@"""Status"""); break;
}
}
Expand All @@ -122,6 +129,7 @@ public async Task<object[][]> Get(
.Filter("Status", status)
.Filter("Round", round)
.Filter("Slots", slots)
.Filter("DalShards", dalShards)
.Take(sort ?? new SortParameter { Asc = "level" }, offset, limit, x => ("Level", "Level"));

await using var db = await DataSource.OpenConnectionAsync();
Expand Down Expand Up @@ -163,6 +171,10 @@ public async Task<object[][]> Get(
foreach (var row in rows)
result[j++][i] = row.Slots;
break;
case "dalShards":
foreach (var row in rows)
result[j++][i] = row.DalShards;
break;
case "status":
foreach (var row in rows)
result[j++][i] = BakingRightStatuses.ToString(row.Status);
Expand All @@ -179,6 +191,7 @@ public async Task<object[]> Get(
Int32Parameter cycle,
Int32Parameter level,
Int32NullParameter slots,
Int32NullParameter dalShards,
Int32NullParameter round,
BakingRightStatusParameter status,
SortParameter sort,
Expand All @@ -196,6 +209,7 @@ public async Task<object[]> Get(
case "baker": columns.Add(@"""BakerId"""); break;
case "round": columns.Add(@"""Round"""); break;
case "slots": columns.Add(@"""Slots"""); break;
case "dalShards": columns.Add(@"""DalShards"""); break;
case "status": columns.Add(@"""Status"""); break;
}

Expand All @@ -210,6 +224,7 @@ public async Task<object[]> Get(
.Filter("Status", status)
.Filter("Round", round)
.Filter("Slots", slots)
.Filter("DalShards", dalShards)
.Take(sort ?? new SortParameter { Asc = "level" }, offset, limit, x => ("Level", "Level"));

await using var db = await DataSource.OpenConnectionAsync();
Expand Down Expand Up @@ -249,6 +264,10 @@ public async Task<object[]> Get(
foreach (var row in rows)
result[j++] = row.Slots;
break;
case "dalShards":
foreach (var row in rows)
result[j++] = row.DalShards;
break;
case "status":
foreach (var row in rows)
result[j++] = BakingRightStatuses.ToString(row.Status);
Expand Down
2 changes: 1 addition & 1 deletion Tzkt.Api/Services/Home/HomeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ async Task<object[][]> GetBlocks()
new BakingRightTypeParameter { Eq = 0 },
null, null,
new Int32Parameter { In = Enumerable.Range(level + 1, 3).ToList() },
null,
null, null,
new Int32NullParameter { Eq = 0 },
null,
new SortParameter { Desc = "level" },
Expand Down
Loading

0 comments on commit 107f27b

Please sign in to comment.