Skip to content

Commit

Permalink
Add inbox message index
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed May 31, 2024
1 parent 3d65672 commit c00a973
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Tzkt.Data/Migrations/20240424125408_Initial.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Tzkt.Data/Migrations/20240424125408_Initial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Level = table.Column<int>(type: "integer", nullable: false),
Index = table.Column<int>(type: "integer", nullable: false),
Type = table.Column<int>(type: "integer", nullable: false),
PredecessorLevel = table.Column<int>(type: "integer", nullable: true),
OperationId = table.Column<long>(type: "bigint", nullable: true),
Expand Down
3 changes: 3 additions & 0 deletions Tzkt.Data/Migrations/20240501115741_Triggers.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Tzkt.Data/Migrations/20240501115809_TokensValue.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Tzkt.Data/Migrations/TzktContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2007,6 +2007,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<byte[]>("Payload")
.HasColumnType("bytea");

b.Property<int>("Index")
.HasColumnType("integer");

b.Property<int?>("PredecessorLevel")
.HasColumnType("integer");

Expand Down
1 change: 1 addition & 0 deletions Tzkt.Data/Models/SmartRollups/InboxMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class InboxMessage
{
public int Id { get; set; }
public int Level { get; set; }
public int Index { get; set; }
public InboxMessageType Type { get; set; }
public int? PredecessorLevel { get; set; } // only for LevelInfo
public long? OperationId { get; set; } // only for Internal and External
Expand Down
23 changes: 17 additions & 6 deletions Tzkt.Sync/Protocols/Handlers/Proto16/Commits/InboxCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ public void Init(Block block)
{
var conn = Db.Database.GetDbConnection() as NpgsqlConnection;
using var writer = conn.BeginBinaryImport("""
COPY "InboxMessages" ("Id", "Level", "Type", "PredecessorLevel", "OperationId", "Payload", "Protocol")
COPY "InboxMessages" ("Id", "Level", "Index", "Type", "PredecessorLevel", "OperationId", "Payload", "Protocol")
FROM STDIN (FORMAT BINARY)
""");

var index = 0;

writer.StartRow();
writer.Write(Cache.AppState.NextInboxMessageId(), NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(block.Level, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(index++, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write((int)InboxMessageType.LevelStart, NpgsqlTypes.NpgsqlDbType.Integer);
writer.WriteNull();
writer.WriteNull();
Expand All @@ -28,17 +31,19 @@ FROM STDIN (FORMAT BINARY)
writer.StartRow();
writer.Write(Cache.AppState.NextInboxMessageId(), NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(block.Level, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(index++, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write((int)InboxMessageType.LevelInfo, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(block.Level - 1, NpgsqlTypes.NpgsqlDbType.Integer);
writer.WriteNull();
writer.WriteNull();
writer.WriteNull();

WriteMigrationMessage(writer, block);
WriteMigrationMessage(writer, block, ref index);

writer.StartRow();
writer.Write(Cache.AppState.NextInboxMessageId(), NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(block.Level, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(index, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write((int)InboxMessageType.LevelEnd, NpgsqlTypes.NpgsqlDbType.Integer);
writer.WriteNull();
writer.WriteNull();
Expand All @@ -52,13 +57,16 @@ public void Apply(Block block)
{
var conn = Db.Database.GetDbConnection() as NpgsqlConnection;
using var writer = conn.BeginBinaryImport("""
COPY "InboxMessages" ("Id", "Level", "Type", "PredecessorLevel", "OperationId", "Payload", "Protocol")
COPY "InboxMessages" ("Id", "Level", "Index", "Type", "PredecessorLevel", "OperationId", "Payload", "Protocol")
FROM STDIN (FORMAT BINARY)
""");


var index = 0;

writer.StartRow();
writer.Write(Cache.AppState.NextInboxMessageId(), NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(block.Level, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(index++, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write((int)InboxMessageType.LevelStart, NpgsqlTypes.NpgsqlDbType.Integer);
writer.WriteNull();
writer.WriteNull();
Expand All @@ -68,20 +76,22 @@ FROM STDIN (FORMAT BINARY)
writer.StartRow();
writer.Write(Cache.AppState.NextInboxMessageId(), NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(block.Level, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(index++, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write((int)InboxMessageType.LevelInfo, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(block.Level - 1, NpgsqlTypes.NpgsqlDbType.Integer);
writer.WriteNull();
writer.WriteNull();
writer.WriteNull();

if (block.Events.HasFlag(BlockEvents.ProtocolBegin))
WriteMigrationMessage(writer, block);
WriteMigrationMessage(writer, block, ref index);

foreach (var (operationId, payload) in Proto.Inbox.Messages)
{
writer.StartRow();
writer.Write(Cache.AppState.NextInboxMessageId(), NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(block.Level, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(index++, NpgsqlTypes.NpgsqlDbType.Integer);
if (payload == null)
{
writer.Write((int)InboxMessageType.Transfer, NpgsqlTypes.NpgsqlDbType.Integer);
Expand All @@ -102,6 +112,7 @@ FROM STDIN (FORMAT BINARY)
writer.StartRow();
writer.Write(Cache.AppState.NextInboxMessageId(), NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(block.Level, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(index, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write((int)InboxMessageType.LevelEnd, NpgsqlTypes.NpgsqlDbType.Integer);
writer.WriteNull();
writer.WriteNull();
Expand All @@ -121,7 +132,7 @@ DELETE FROM "InboxMessages"
Cache.AppState.ReleaseInboxMessageId(cnt);
}

protected virtual void WriteMigrationMessage(NpgsqlBinaryImporter writer, Block block)
protected virtual void WriteMigrationMessage(NpgsqlBinaryImporter writer, Block block, ref int index)
{
// migration messages were added in Proto17
}
Expand Down
3 changes: 2 additions & 1 deletion Tzkt.Sync/Protocols/Handlers/Proto17/Commits/InboxCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ public class InboxCommit : Proto16.InboxCommit
{
public InboxCommit(ProtocolHandler protocol) : base(protocol) { }

protected override void WriteMigrationMessage(NpgsqlBinaryImporter writer, Block block)
protected override void WriteMigrationMessage(NpgsqlBinaryImporter writer, Block block, ref int index)
{
writer.StartRow();
writer.Write(Cache.AppState.NextInboxMessageId(), NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(block.Level, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write(index++, NpgsqlTypes.NpgsqlDbType.Integer);
writer.Write((int)InboxMessageType.Migration, NpgsqlTypes.NpgsqlDbType.Integer);
writer.WriteNull();
writer.WriteNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ protected override void UpgradeParameters(Protocol protocol, Protocol prev)
protocol.BlocksPerCommitment = protocol.BlocksPerCommitment * 3 / 2;
protocol.BlocksPerSnapshot = protocol.BlocksPerCycle;
protocol.BlocksPerVoting = protocol.BlocksPerVoting * 3 / 2;
protocol.TimeBetweenBlocks = protocol.TimeBetweenBlocks * 3 / 2;
protocol.HardBlockGasLimit = prev.HardBlockGasLimit * 3 / 2;
protocol.TimeBetweenBlocks = protocol.TimeBetweenBlocks * 2 / 3;
protocol.HardBlockGasLimit = prev.HardBlockGasLimit * 2 / 3;
}

protected override async Task MigrateContext(AppState state)
Expand Down

0 comments on commit c00a973

Please sign in to comment.