From 7cf428df5247f3a69abd5b3aaae02752528de2e9 Mon Sep 17 00:00:00 2001 From: 257Byte <257byte@gmail.com> Date: Mon, 22 Aug 2022 19:15:41 +0300 Subject: [PATCH] Add contract code hash to events --- .../Migrations/20220819172957_Kathmandu.Designer.cs | 7 +++++++ Tzkt.Data/Migrations/20220819172957_Kathmandu.cs | 11 +++++++++++ Tzkt.Data/Migrations/TzktContextModelSnapshot.cs | 7 +++++++ Tzkt.Data/Models/Scripts/ContractEvent.cs | 7 +++++++ .../Handlers/Proto14/Commits/ContractEventCommit.cs | 1 + 5 files changed, 33 insertions(+) diff --git a/Tzkt.Data/Migrations/20220819172957_Kathmandu.Designer.cs b/Tzkt.Data/Migrations/20220819172957_Kathmandu.Designer.cs index a3a7e0491..73c35d191 100644 --- a/Tzkt.Data/Migrations/20220819172957_Kathmandu.Designer.cs +++ b/Tzkt.Data/Migrations/20220819172957_Kathmandu.Designer.cs @@ -1016,6 +1016,9 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .HasColumnType("integer") .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + b.Property("ContractCodeHash") + .HasColumnType("integer"); + b.Property("ContractId") .HasColumnType("integer"); @@ -1039,6 +1042,8 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasKey("Id"); + b.HasIndex("ContractCodeHash"); + b.HasIndex("ContractId"); b.HasIndex("Id") @@ -1054,6 +1059,8 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasIndex("TransactionId"); + b.HasIndex("ContractCodeHash", "Tag"); + b.HasIndex("ContractId", "Tag"); b.ToTable("Events"); diff --git a/Tzkt.Data/Migrations/20220819172957_Kathmandu.cs b/Tzkt.Data/Migrations/20220819172957_Kathmandu.cs index 9e77de5a9..4ce85ccbc 100644 --- a/Tzkt.Data/Migrations/20220819172957_Kathmandu.cs +++ b/Tzkt.Data/Migrations/20220819172957_Kathmandu.cs @@ -62,6 +62,7 @@ protected override void Up(MigrationBuilder migrationBuilder) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Level = table.Column(type: "integer", nullable: false), ContractId = table.Column(type: "integer", nullable: false), + ContractCodeHash = table.Column(type: "integer", nullable: false), TransactionId = table.Column(type: "integer", nullable: false), Tag = table.Column(type: "text", nullable: true), Type = table.Column(type: "bytea", nullable: true), @@ -145,6 +146,16 @@ protected override void Up(MigrationBuilder migrationBuilder) onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateIndex( + name: "IX_Events_ContractCodeHash", + table: "Events", + column: "ContractCodeHash"); + + migrationBuilder.CreateIndex( + name: "IX_Events_ContractCodeHash_Tag", + table: "Events", + columns: new[] { "ContractCodeHash", "Tag" }); + migrationBuilder.CreateIndex( name: "IX_Events_ContractId", table: "Events", diff --git a/Tzkt.Data/Migrations/TzktContextModelSnapshot.cs b/Tzkt.Data/Migrations/TzktContextModelSnapshot.cs index 9a41ab575..cd4fe284d 100644 --- a/Tzkt.Data/Migrations/TzktContextModelSnapshot.cs +++ b/Tzkt.Data/Migrations/TzktContextModelSnapshot.cs @@ -1014,6 +1014,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnType("integer") .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + b.Property("ContractCodeHash") + .HasColumnType("integer"); + b.Property("ContractId") .HasColumnType("integer"); @@ -1037,6 +1040,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("Id"); + b.HasIndex("ContractCodeHash"); + b.HasIndex("ContractId"); b.HasIndex("Id") @@ -1052,6 +1057,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("TransactionId"); + b.HasIndex("ContractCodeHash", "Tag"); + b.HasIndex("ContractId", "Tag"); b.ToTable("Events"); diff --git a/Tzkt.Data/Models/Scripts/ContractEvent.cs b/Tzkt.Data/Models/Scripts/ContractEvent.cs index 386b8a340..e8c83af12 100644 --- a/Tzkt.Data/Models/Scripts/ContractEvent.cs +++ b/Tzkt.Data/Models/Scripts/ContractEvent.cs @@ -7,6 +7,7 @@ public class ContractEvent public int Id { get; set; } public int Level { get; set; } public int ContractId { get; set; } + public int ContractCodeHash { get; set; } public int TransactionId { get; set; } public string Tag { get; set; } @@ -41,6 +42,9 @@ public static void BuildContractEventModel(this ModelBuilder modelBuilder) modelBuilder.Entity() .HasIndex(x => x.ContractId); + modelBuilder.Entity() + .HasIndex(x => x.ContractCodeHash); + modelBuilder.Entity() .HasIndex(x => x.TransactionId); @@ -50,6 +54,9 @@ public static void BuildContractEventModel(this ModelBuilder modelBuilder) modelBuilder.Entity() .HasIndex(x => new { x.ContractId, x.Tag }); + modelBuilder.Entity() + .HasIndex(x => new { x.ContractCodeHash, x.Tag }); + modelBuilder.Entity() .HasIndex(x => x.JsonPayload) .HasMethod("gin") diff --git a/Tzkt.Sync/Protocols/Handlers/Proto14/Commits/ContractEventCommit.cs b/Tzkt.Sync/Protocols/Handlers/Proto14/Commits/ContractEventCommit.cs index 59283da6c..c48195149 100644 --- a/Tzkt.Sync/Protocols/Handlers/Proto14/Commits/ContractEventCommit.cs +++ b/Tzkt.Sync/Protocols/Handlers/Proto14/Commits/ContractEventCommit.cs @@ -33,6 +33,7 @@ public virtual async Task Apply(Block block, JsonElement content) Id = Cache.AppState.NextOperationId(), Level = block.Level, ContractId = contract.Id, + ContractCodeHash = contract.CodeHash, TransactionId = parentTx.Id, Tag = content.RequiredString("tag") };