Skip to content

Commit

Permalink
Auto detect chain id and chain name
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Aug 2, 2021
1 parent c086167 commit ab51034
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 23 deletions.
13 changes: 10 additions & 3 deletions Tzkt.Api/Models/State.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Tzkt.Api.Models
{
public class State
{
/// <summary>
/// Alias name of the chain (or "private" if it's not on the list of known chains)
/// </summary>
public string Chain { get; set; }

/// <summary>
/// Unique identificator of the chain
/// </summary>
public string ChainId { get; set; }

/// <summary>
/// Current cycle
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Tzkt.Api/Repositories/StateRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public State Get()
var appState = State.Current;
return new State
{
Chain = appState.Chain,
ChainId = appState.ChainId,
KnownLevel = appState.KnownHead,
LastSync = appState.LastSync,
Hash = appState.Hash,
Expand Down
4 changes: 4 additions & 0 deletions Tzkt.Api/Services/Cache/State/RawModels/RawState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ namespace Tzkt.Api.Services.Cache
{
public class RawState
{
public string Chain { get; set; }

public string ChainId { get; set; }

public int KnownHead { get; set; }

public DateTime LastSync { get; set; }
Expand Down
6 changes: 6 additions & 0 deletions Tzkt.Data/Migrations/20210714124853_Initial.Designer.cs

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

6 changes: 4 additions & 2 deletions Tzkt.Data/Migrations/20210714124853_Initial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ protected override void Up(MigrationBuilder migrationBuilder)
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Chain = table.Column<string>(type: "text", nullable: true),
ChainId = table.Column<string>(type: "text", nullable: true),
KnownHead = table.Column<int>(type: "integer", nullable: false),
LastSync = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
Cycle = table.Column<int>(type: "integer", nullable: false),
Expand Down Expand Up @@ -1205,8 +1207,8 @@ protected override void Up(MigrationBuilder migrationBuilder)

migrationBuilder.InsertData(
table: "AppState",
columns: new[] { "Id", "AccountCounter", "AccountsCount", "ActivationOpsCount", "BallotOpsCount", "BigMapCounter", "BigMapKeyCounter", "BigMapUpdateCounter", "BlocksCount", "CommitmentsCount", "Cycle", "CyclesCount", "DelegationOpsCount", "DoubleBakingOpsCount", "DoubleEndorsingOpsCount", "EndorsementOpsCount", "Hash", "KnownHead", "LastSync", "Level", "ManagerCounter", "MigrationOpsCount", "NextProtocol", "NonceRevelationOpsCount", "OperationCounter", "OriginationOpsCount", "ProposalOpsCount", "ProposalsCount", "Protocol", "ProtocolsCount", "QuoteBtc", "QuoteCny", "QuoteEth", "QuoteEur", "QuoteJpy", "QuoteKrw", "QuoteLevel", "QuoteUsd", "RevealOpsCount", "RevelationPenaltyOpsCount", "ScriptCounter", "StorageCounter", "Timestamp", "TransactionOpsCount", "VotingEpoch", "VotingPeriod" },
values: new object[] { -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, "", 0, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), -1, 0, 0, "", 0, 0, 0, 0, 0, "", 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1, 0.0, 0, 0, 0, 0, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), 0, -1, -1 });
columns: new[] { "Id", "AccountCounter", "AccountsCount", "ActivationOpsCount", "BallotOpsCount", "BigMapCounter", "BigMapKeyCounter", "BigMapUpdateCounter", "BlocksCount", "Chain", "ChainId", "CommitmentsCount", "Cycle", "CyclesCount", "DelegationOpsCount", "DoubleBakingOpsCount", "DoubleEndorsingOpsCount", "EndorsementOpsCount", "Hash", "KnownHead", "LastSync", "Level", "ManagerCounter", "MigrationOpsCount", "NextProtocol", "NonceRevelationOpsCount", "OperationCounter", "OriginationOpsCount", "ProposalOpsCount", "ProposalsCount", "Protocol", "ProtocolsCount", "QuoteBtc", "QuoteCny", "QuoteEth", "QuoteEur", "QuoteJpy", "QuoteKrw", "QuoteLevel", "QuoteUsd", "RevealOpsCount", "RevelationPenaltyOpsCount", "ScriptCounter", "StorageCounter", "Timestamp", "TransactionOpsCount", "VotingEpoch", "VotingPeriod" },
values: new object[] { -1, 0, 0, 0, 0, 0, 0, 0, 0, null, null, 0, -1, 0, 0, 0, 0, 0, "", 0, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), -1, 0, 0, "", 0, 0, 0, 0, 0, "", 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1, 0.0, 0, 0, 0, 0, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), 0, -1, -1 });

migrationBuilder.CreateIndex(
name: "IX_Accounts_Address",
Expand Down
6 changes: 6 additions & 0 deletions Tzkt.Data/Migrations/TzktContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<int>("BlocksCount")
.HasColumnType("integer");

b.Property<string>("Chain")
.HasColumnType("text");

b.Property<string>("ChainId")
.HasColumnType("text");

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

Expand Down
8 changes: 6 additions & 2 deletions Tzkt.Data/Models/AppState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@

namespace Tzkt.Data.Models
{
//TODO: add chain id
public class AppState
{
public int Id { get; set; }
public string Chain { get; set; }
public string ChainId { get; set; }
public int KnownHead { get; set; }
public DateTime LastSync { get; set; }

#region head
public int Cycle { get; set; }
public int Level { get; set; }
public DateTime Timestamp { get; set; }
public string Protocol { get; set; }
public string NextProtocol { get; set; }
public string Hash { get; set; }

public int VotingEpoch { get; set; }
public int VotingPeriod { get; set; }
#endregion

#region counters
public int AccountCounter { get; set; }
public int OperationCounter { get; set; }
public int ManagerCounter { get; set; }
Expand All @@ -28,6 +31,7 @@ public class AppState
public int BigMapUpdateCounter { get; set; }
public int StorageCounter { get; set; }
public int ScriptCounter { get; set; }
#endregion

#region entities count
public int CommitmentsCount { get; set; }
Expand Down
4 changes: 4 additions & 0 deletions Tzkt.Sync/Protocols/Handlers/Genesis/GenesisHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public override Task Commit(JsonElement rawBlock)

#region update state
var state = Cache.AppState.Get();
state.ChainId = rawBlock.RequiredString("chain_id");
state.Chain = Chains.GetName(state.ChainId);
state.Cycle = -1;
state.Level = block.Level;
state.Timestamp = block.Timestamp;
Expand All @@ -93,6 +95,8 @@ await Db.Database.ExecuteSqlRawAsync(@"

#region update state
var state = Cache.AppState.Get();
state.ChainId = null;
state.Chain = null;
state.Cycle = -1;
state.Level = -1;
state.Timestamp = DateTime.MinValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public Validator(ProtocolHandler protocol)

public Task ValidateBlock(JsonElement block)
{
if (block.RequiredString("chain_id") != Cache.AppState.GetChainId())
throw new ValidationException("invalid chain");

if (block.Required("header").RequiredInt32("level") != Cache.AppState.GetNextLevel())
throw new ValidationException("invalid block level", true);

Expand Down
3 changes: 3 additions & 0 deletions Tzkt.Sync/Protocols/Handlers/Proto1/Validation/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Validator : IValidator

public virtual async Task ValidateBlock(JsonElement block)
{
if (block.RequiredString("chain_id") != Cache.AppState.GetChainId())
throw new ValidationException("invalid chain");

var protocol = block.RequiredString("protocol");
Protocol = await Cache.Protocols.GetAsync(protocol);

Expand Down
14 changes: 14 additions & 0 deletions Tzkt.Sync/Protocols/Helpers/Chains.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Tzkt.Sync.Protocols
{
static class Chains
{
public static string GetName(string chainId) => chainId switch
{
"NetXdQprcVkpaWU" => "mainnet",
"NetXSgo1ZT2DRUG" => "edo2net",
"NetXxkAx4woPLyu" => "florencenet",
"NetXz969SFaFn8k" => "granadanet",
_ => "private"
};
}
}
5 changes: 5 additions & 0 deletions Tzkt.Sync/Services/Cache/AppStateCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public AppState Get()
return AppState;
}

public string GetChainId()
{
return AppState.ChainId;
}

public int GetLevel()
{
return AppState.Level;
Expand Down
16 changes: 2 additions & 14 deletions Tzkt.Sync/Services/TezosNode/TezosNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -10,7 +8,6 @@ namespace Tzkt.Sync.Services
{
public sealed class TezosNode : IDisposable
{
readonly string ChainId;
readonly TzktClient Rpc;

Header Header;
Expand All @@ -20,7 +17,6 @@ public sealed class TezosNode : IDisposable
public TezosNode(IConfiguration config)
{
var nodeConf = config.GetTezosNodeConfig();
ChainId = nodeConf.ChainId;
Rpc = new TzktClient(nodeConf.Endpoint, nodeConf.Timeout);
}

Expand All @@ -36,9 +32,6 @@ public async Task<Header> GetHeaderAsync()
{
var header = await Rpc.GetObjectAsync<Header>("chains/main/blocks/head/header");

if (header.ChainId != ChainId)
throw new Exception("Invalid chain");

if (header.Protocol != Header?.Protocol)
Constants = await Rpc.GetObjectAsync<Constants>("chains/main/blocks/head/context/constants");

Expand All @@ -52,14 +45,9 @@ public async Task<Header> GetHeaderAsync()
return Header;
}

public async Task<Header> GetHeaderAsync(int level)
public Task<Header> GetHeaderAsync(int level)
{
var header = await Rpc.GetObjectAsync<Header>($"chains/main/blocks/{level}/header");

if (header.ChainId != ChainId)
throw new Exception("Invalid chain");

return header;
return Rpc.GetObjectAsync<Header>($"chains/main/blocks/{level}/header");
}

public async Task<bool> HasUpdatesAsync(int level)
Expand Down
1 change: 0 additions & 1 deletion Tzkt.Sync/Services/TezosNode/TezosNodeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Tzkt.Sync.Services
{
public class TezosNodeConfig
{
public string ChainId { get; set; }
public string Endpoint { get; set; }
public int Timeout { get; set; }
}
Expand Down
1 change: 0 additions & 1 deletion Tzkt.Sync/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"Validation": true
},
"TezosNode": {
"ChainId": "NetXdQprcVkpaWU",
"Endpoint": "https://mainnet-tezos.giganode.io/",
"Timeout": 60
},
Expand Down

0 comments on commit ab51034

Please sign in to comment.