Skip to content

Commit

Permalink
Index refutation games
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Mar 14, 2023
1 parent 010980c commit a5d3daf
Show file tree
Hide file tree
Showing 18 changed files with 551 additions and 39 deletions.
2 changes: 2 additions & 0 deletions Tzkt.Data/Models/Accounts/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class Account
public int SmartRollupRefuteCount { get; set; }
public int SmartRollupTimeoutCount { get; set; }

public int RefutationGamesCount { get; set; }

public int? DelegateId { get; set; }
public int? DelegationLevel { get; set; }
public bool Staked { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions Tzkt.Data/Models/Accounts/SmartRollup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class SmartRollup : Account
public int CementedCommitments { get; set; }
public int RefutedCommitments { get; set; }
public int InboxLevel { get; set; }
public string Genesis { get; set; }
public string Commitment { get; set; }
public int ActiveGames { get; set; }
}

public enum PvmKind
Expand Down
1 change: 1 addition & 0 deletions Tzkt.Data/Models/AppState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class AppState
public int ScriptCounter { get; set; }
public int EventCounter { get; set; }
public int SmartRollupCommitmentCounter { get; set; }
public int RefutationGameCounter { get; set; }
#endregion

#region entities count
Expand Down
22 changes: 22 additions & 0 deletions Tzkt.Data/Models/Operations/SmartRollupRefuteOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ namespace Tzkt.Data.Models
public class SmartRollupRefuteOperation : ManagerOperation
{
public int? SmartRollupId { get; set; }
public int? GameId { get; set; }
public RefutationMove Move { get; set; }
public RefutationGameStatus GameStatus { get; set; }
}

public enum RefutationMove
{
Start,
Dissection,
Proof,
Timeout
}

public enum RefutationGameStatus
{
None,
Ongoing,
Loser,
Draw
}

public static class SmartRollupRefuteOperationModel
Expand Down Expand Up @@ -37,6 +56,9 @@ public static void BuildSmartRollupRefuteOperationModel(this ModelBuilder modelB

modelBuilder.Entity<SmartRollupRefuteOperation>()
.HasIndex(x => x.SmartRollupId);

modelBuilder.Entity<SmartRollupRefuteOperation>()
.HasIndex(x => x.GameId);
#endregion

#region relations
Expand Down
45 changes: 45 additions & 0 deletions Tzkt.Data/Models/SmartRollups/RefutationGame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.EntityFrameworkCore;

namespace Tzkt.Data.Models
{
public class RefutationGame
{
public int Id { get; set; }
public int SmartRollupId { get; set; }
public int InitiatorId { get; set; }
public int OpponentId { get; set; }
public int InitiatorCommitmentId { get; set; }
public int OpponentCommitmentId { get; set; }
public long LastMoveId { get; set; }

public int FirstLevel { get; set; }
public int LastLevel { get; set; }

public long? InitiatorReward { get; set; }
public long? InitiatorLoss { get; set; }
public long? OpponentReward { get; set; }
public long? OpponentLoss { get; set; }
}

public static class RefutationGameModel
{
public static void BuildRefutationGameModel(this ModelBuilder modelBuilder)
{
#region keys
modelBuilder.Entity<RefutationGame>()
.HasKey(x => x.Id);
#endregion

#region indexes
modelBuilder.Entity<RefutationGame>()
.HasIndex(x => x.SmartRollupId);

modelBuilder.Entity<RefutationGame>()
.HasIndex(x => x.FirstLevel);

modelBuilder.Entity<RefutationGame>()
.HasIndex(x => x.LastLevel);
#endregion
}
}
}
5 changes: 4 additions & 1 deletion Tzkt.Data/Models/SmartRollups/SmartRollupCommitment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace Tzkt.Data.Models
public class SmartRollupCommitment
{
public int Id { get; set; }
public int PredecessorId { get; set; }
public int SmartRollupId { get; set; }
public int InitiatorId { get; set; }
public int? PredecessorId { get; set; }

public int FirstLevel { get; set; }
public int LastLevel { get; set; }
Expand All @@ -19,6 +19,9 @@ public class SmartRollupCommitment

public int Publications { get; set; }
public int Successors { get; set; }
public int? ActiveGames { get; set; }
public int? LostGames { get; set; }
public int? WonGames { get; set; }
}

public static class SmartRollupCommitmentModel
Expand Down
2 changes: 2 additions & 0 deletions Tzkt.Data/TzktContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public class TzktContext : DbContext

#region rollups
public DbSet<SmartRollupCommitment> SmartRollupCommitments { get; set; }
public DbSet<RefutationGame> RefutationGames { get; set; }
#endregion

#region plugins
Expand Down Expand Up @@ -229,6 +230,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

#region rollups
modelBuilder.BuildSmartRollupCommitmentModel();
modelBuilder.BuildRefutationGameModel();
#endregion

#region plugins
Expand Down
3 changes: 3 additions & 0 deletions Tzkt.Sync.Tests/Database/AppStateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ await db.TransactionOps.CountAsync(x => x.InitiatorId == null) +

if (state.SmartRollupCommitmentCounter != await db.SmartRollupCommitments.CountAsync())
throw new Exception("Invalid AppState.SmartRollupCommitmentCounter");

if (state.RefutationGameCounter != await db.RefutationGames.CountAsync())
throw new Exception("Invalid AppState.RefutationGameCounter");
#endregion

#region counts
Expand Down
6 changes: 6 additions & 0 deletions Tzkt.Sync/Extensions/JsonElementExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public static JsonElement Required(this JsonElement el, string name)
: throw new SerializationException($"Missed required property {name}");
}

public static JsonElement? Optional(this JsonElement el, string name)
{
return el.TryGetProperty(name, out var res) ? res
: null;
}

public static int Count(this JsonElement el)
{
return el.EnumerateArray().Count();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,9 @@ public virtual async Task Apply(Block block, List<EndorsingRewardOperation> endo
throw new NotImplementedException();
}

if (block.SmartRollupRefuteOps != null)
{
throw new NotImplementedException();
}
//if (block.SmartRollupRefuteOps != null)
//{
//}

if (block.SmartRollupTimeoutOps != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public virtual async Task Apply(Block block, JsonElement op, JsonElement content
Staked = false,
Type = AccountType.SmartRollup,
PvmKind = pvmKind,
Genesis = result.RequiredString("genesis_commitment_hash"),
ActiveTokensCount = ghost.ActiveTokensCount,
TokenBalancesCount = ghost.TokenBalancesCount,
TokenTransfersCount = ghost.TokenTransfersCount
Expand All @@ -67,7 +68,8 @@ public virtual async Task Apply(Block block, JsonElement op, JsonElement content
CreatorId = sender.Id,
Staked = false,
Type = AccountType.SmartRollup,
PvmKind = pvmKind
PvmKind = pvmKind,
Genesis = result.RequiredString("genesis_commitment_hash")
};
Db.SmartRollups.Add(smartRollup);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public virtual async Task Apply(Block block, JsonElement op, JsonElement content
#region init
var sender = await Cache.Accounts.GetAsync(content.RequiredString("source"));
sender.Delegate ??= Cache.Accounts.GetDelegate(sender.DelegateId);
var smartRollup = await Cache.Accounts.GetAsync(content.RequiredString("rollup"));
var smartRollup = await Cache.Accounts.GetAsync(content.RequiredString("rollup")) as SmartRollup;

var result = content.Required("metadata").Required("operation_result");
var bond = result.OptionalArray("balance_updates")?.EnumerateArray()
Expand Down Expand Up @@ -89,25 +89,17 @@ public virtual async Task Apply(Block block, JsonElement op, JsonElement content
{
sender.SmartRollupBonds += operation.Bond;
smartRollup.SmartRollupBonds += operation.Bond;
(smartRollup as SmartRollup).PendingCommitments++;
smartRollup.PendingCommitments++;

var commitmentHash = result.RequiredString("staked_hash");
var commitment = await Cache.SmartRollupCommitments.GetOrDefaultAsync(commitmentHash, smartRollup.Id);
if (commitment == null)
{
var commitmentEl = content.Required("commitment");
var predecessorHash = commitmentEl.RequiredString("predecessor");

var predecessor = await Cache.SmartRollupCommitments.GetAsync(predecessorHash, smartRollup.Id);
Db.TryAttach(predecessor);
predecessor.Successors++;
predecessor.LastLevel = operation.Level;

commitment = new SmartRollupCommitment
{
Id = Cache.AppState.NextSmartRollupCommitmentId(),
SmartRollupId = smartRollup.Id,
PredecessorId = predecessor.Id,
InitiatorId = operation.SenderId,
FirstLevel = operation.Level,
LastLevel = operation.Level,
Expand All @@ -116,10 +108,24 @@ public virtual async Task Apply(Block block, JsonElement op, JsonElement content
Ticks = commitmentEl.RequiredInt64("number_of_ticks"),
Hash = commitmentHash,
Publications = 1,
Successors = 0
Successors = 0,
ActiveGames = null,
LostGames = null,
WonGames = null
};
Cache.SmartRollupCommitments.Add(commitment);
Db.SmartRollupCommitments.Add(commitment);

var predecessorHash = commitmentEl.RequiredString("predecessor");
if (predecessorHash != smartRollup.Genesis)
{
var predecessor = await Cache.SmartRollupCommitments.GetAsync(predecessorHash, smartRollup.Id);
Db.TryAttach(predecessor);
predecessor.Successors++;
predecessor.LastLevel = operation.Level;

commitment.PredecessorId = predecessor.Id;
}
}
else
{
Expand Down Expand Up @@ -173,10 +179,13 @@ public virtual async Task Revert(Block block, SmartRollupPublishOperation operat

if (commitment.Publications == 0)
{
var predecessor = await Cache.SmartRollupCommitments.GetAsync(commitment.PredecessorId);
Db.TryAttach(predecessor);
predecessor.Successors--;
// TODO: properly revert predecessor.LastLevel
if (commitment.PredecessorId != null)
{
var predecessor = await Cache.SmartRollupCommitments.GetAsync((int)commitment.PredecessorId);
Db.TryAttach(predecessor);
predecessor.Successors--;
// TODO: properly revert predecessor.LastLevel
}

Cache.AppState.ReleaseSmartRollupCommitmentId();
Cache.SmartRollupCommitments.Remove(commitment);
Expand Down
Loading

0 comments on commit a5d3daf

Please sign in to comment.