From 53d137237775d7b9115cbf5c1c7f037c3369babf Mon Sep 17 00:00:00 2001 From: Roman Serikov Date: Wed, 24 Feb 2021 18:35:32 +0300 Subject: [PATCH] Fixed protocol constants in indexer without tzkt boost --- cmd/indexer/indexer/boost.go | 41 +++++++++----------------- cmd/indexer/indexer/protocol.go | 43 ++++++++++++++++++++++++++++ internal/elastic/protocol/storage.go | 6 ++-- 3 files changed, 60 insertions(+), 30 deletions(-) create mode 100644 cmd/indexer/indexer/protocol.go diff --git a/cmd/indexer/indexer/boost.go b/cmd/indexer/indexer/boost.go index 8f5a72fc2..eed56f34b 100644 --- a/cmd/indexer/indexer/boost.go +++ b/cmd/indexer/indexer/boost.go @@ -126,18 +126,9 @@ func (bi *BoostIndexer) fetchExternalProtocols() error { Network: bi.Network, } - protocolConstants := protocol.Constants{} - if newProtocol.StartLevel != newProtocol.EndLevel || newProtocol.EndLevel != 0 { - constants, err := bi.rpc.GetNetworkConstants(extProtocols[i].StartLevel) - if err != nil { - return err - } - protocolConstants.CostPerByte = constants.CostPerByte - protocolConstants.HardGasLimitPerOperation = constants.HardGasLimitPerOperation - protocolConstants.HardStorageLimitPerOperation = constants.HardStorageLimitPerOperation - protocolConstants.TimeBetweenBlocks = constants.TimeBetweenBlocks[0] + if err := setProtocolConstants(bi.rpc, newProtocol); err != nil { + return err } - newProtocol.Constants = protocolConstants protocols = append(protocols, newProtocol) logger.WithNetwork(bi.Network).Infof("Fetched %s", alias) @@ -218,15 +209,24 @@ func (bi *BoostIndexer) init() error { currentProtocol, err := bi.Protocols.GetProtocol(bi.Network, "", currentState.Level) if err != nil { + if !bi.Storage.IsRecordNotFound(err) { + return err + } + header, err := bi.rpc.GetHeader(helpers.MaxInt64(1, currentState.Level)) if err != nil { return err } - currentProtocol, err = createProtocol(bi.Network, header.Protocol, 0) + currentProtocol, err = createProtocol(bi.rpc, bi.Network, header.Protocol, header.Level) if err != nil { return err } + + if err := bi.Storage.BulkInsert([]models.Model{¤tProtocol}); err != nil { + return err + } } + bi.currentProtocol = currentProtocol logger.WithNetwork(bi.Network).Infof("Current network protocol: %s", currentProtocol.Hash) return nil @@ -560,7 +560,7 @@ func (bi *BoostIndexer) migrate(head noderpc.Header) ([]models.Model, error) { newProtocol, err := bi.Protocols.GetProtocol(bi.Network, head.Protocol, head.Level) if err != nil { logger.Warning("%s", err) - newProtocol, err = createProtocol(bi.Network, head.Protocol, head.Level) + newProtocol, err = createProtocol(bi.rpc, bi.Network, head.Protocol, head.Level) if err != nil { return nil, err } @@ -603,21 +603,6 @@ func (bi *BoostIndexer) migrate(head noderpc.Header) ([]models.Model, error) { return newModels, nil } -func createProtocol(network, hash string, level int64) (protocol protocol.Protocol, err error) { - logger.WithNetwork(network).Infof("Creating new protocol %s starting at %d", hash, level) - protocol.SymLink, err = meta.GetProtoSymLink(hash) - if err != nil { - return - } - - protocol.Alias = hash[:8] - protocol.Network = network - protocol.Hash = hash - protocol.StartLevel = level - protocol.ID = helpers.GenerateID() - return -} - func (bi *BoostIndexer) standartMigration(newProtocol protocol.Protocol, head noderpc.Header) ([]models.Model, []models.Model, error) { logger.WithNetwork(bi.Network).Info("Try to find migrations...") contracts, err := bi.Contracts.GetMany(map[string]interface{}{ diff --git a/cmd/indexer/indexer/protocol.go b/cmd/indexer/indexer/protocol.go new file mode 100644 index 000000000..a5e6d121b --- /dev/null +++ b/cmd/indexer/indexer/protocol.go @@ -0,0 +1,43 @@ +package indexer + +import ( + "github.com/baking-bad/bcdhub/internal/contractparser/meta" + "github.com/baking-bad/bcdhub/internal/helpers" + "github.com/baking-bad/bcdhub/internal/logger" + "github.com/baking-bad/bcdhub/internal/models/protocol" + "github.com/baking-bad/bcdhub/internal/noderpc" +) + +func createProtocol(rpc noderpc.INode, network, hash string, level int64) (protocol protocol.Protocol, err error) { + logger.WithNetwork(network).Infof("Creating new protocol %s starting at %d", hash, level) + protocol.SymLink, err = meta.GetProtoSymLink(hash) + if err != nil { + return + } + + protocol.Alias = hash[:8] + protocol.Network = network + protocol.Hash = hash + protocol.StartLevel = level + protocol.ID = helpers.GenerateID() + + err = setProtocolConstants(rpc, &protocol) + + return +} + +func setProtocolConstants(rpc noderpc.INode, proto *protocol.Protocol) error { + if proto.StartLevel > 0 { + resp, err := rpc.GetNetworkConstants(proto.StartLevel) + if err != nil { + return err + } + + proto.Constants.CostPerByte = resp.CostPerByte + proto.Constants.HardGasLimitPerOperation = resp.HardGasLimitPerOperation + proto.Constants.HardStorageLimitPerOperation = resp.HardStorageLimitPerOperation + proto.Constants.TimeBetweenBlocks = resp.TimeBetweenBlocks[0] + } + + return nil +} diff --git a/internal/elastic/protocol/storage.go b/internal/elastic/protocol/storage.go index e4b0bf458..265b2801a 100644 --- a/internal/elastic/protocol/storage.go +++ b/internal/elastic/protocol/storage.go @@ -6,7 +6,6 @@ import ( "github.com/baking-bad/bcdhub/internal/elastic/core" "github.com/baking-bad/bcdhub/internal/models" "github.com/baking-bad/bcdhub/internal/models/protocol" - "github.com/pkg/errors" ) // Storage - @@ -48,9 +47,12 @@ func (storage *Storage) GetProtocol(network, hash string, level int64) (p protoc return } if response.Hits.Total.Value == 0 { - err = errors.Errorf("Couldn't find a protocol for %s (hash = %s) at level %d", network, hash, level) + err = core.NewRecordNotFoundError(models.DocProtocol, "") return } + + p.ID = response.Hits.Hits[0].ID + err = json.Unmarshal(response.Hits.Hits[0].Source, &p) return }