Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed protocol constants in indexer without tzkt boost #510

Merged
merged 1 commit into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 13 additions & 28 deletions cmd/indexer/indexer/boost.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{&currentProtocol}); err != nil {
return err
}
}

bi.currentProtocol = currentProtocol
logger.WithNetwork(bi.Network).Infof("Current network protocol: %s", currentProtocol.Hash)
return nil
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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{}{
Expand Down
43 changes: 43 additions & 0 deletions cmd/indexer/indexer/protocol.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 4 additions & 2 deletions internal/elastic/protocol/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand Down Expand Up @@ -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
}
Expand Down