diff --git a/cmd/indexer/indexer/boost.go b/cmd/indexer/indexer/boost.go index e97d26f6f..11c3636dc 100644 --- a/cmd/indexer/indexer/boost.go +++ b/cmd/indexer/indexer/boost.go @@ -75,12 +75,7 @@ func (bi *BoostIndexer) fetchExternalProtocols() error { alias = extProtocols[i].Hash[:8] } - constants, err := bi.rpc.GetNetworkConstants(extProtocols[i].StartLevel) - if err != nil { - return err - } - - protocols = append(protocols, &models.Protocol{ + newProtocol := &models.Protocol{ ID: helpers.GenerateID(), Hash: extProtocols[i].Hash, Alias: alias, @@ -88,13 +83,22 @@ func (bi *BoostIndexer) fetchExternalProtocols() error { EndLevel: extProtocols[i].LastLevel, SymLink: symLink, Network: bi.Network, - Constants: models.Constants{ - CostPerByte: constants.Get("cost_per_byte").Int(), - HardGasLimitPerOperation: constants.Get("hard_gas_limit_per_operation").Int(), - HardStorageLimitPerOperation: constants.Get("hard_storage_limit_per_operation").Int(), - TimeBetweenBlocks: constants.Get("time_between_blocks.0").Int(), - }, - }) + } + + protocolConstants := models.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.Get("cost_per_byte").Int() + protocolConstants.HardGasLimitPerOperation = constants.Get("hard_gas_limit_per_operation").Int() + protocolConstants.HardStorageLimitPerOperation = constants.Get("hard_storage_limit_per_operation").Int() + protocolConstants.TimeBetweenBlocks = constants.Get("time_between_blocks.0").Int() + } + newProtocol.Constants = protocolConstants + + protocols = append(protocols, newProtocol) logger.Info("[%s] Fetched %s", bi.Network, alias) } diff --git a/scripts/migration/main.go b/scripts/migration/main.go index ebb8bd91e..38942c5de 100644 --- a/scripts/migration/main.go +++ b/scripts/migration/main.go @@ -40,6 +40,7 @@ var migrationsList = []migrations.Migration{ &migrations.DropMichelson{}, &migrations.SetOperationTags{}, &migrations.CreateTransfersTags{}, + &migrations.SetProtocolConstants{}, } func main() { diff --git a/scripts/migration/migrations/set_protocol_constants.go b/scripts/migration/migrations/set_protocol_constants.go new file mode 100644 index 000000000..cdfc5b67b --- /dev/null +++ b/scripts/migration/migrations/set_protocol_constants.go @@ -0,0 +1,54 @@ +package migrations + +import ( + "github.com/baking-bad/bcdhub/internal/config" + "github.com/baking-bad/bcdhub/internal/elastic" + "github.com/baking-bad/bcdhub/internal/models" +) + +// SetProtocolConstants - migration that set constants for protocol +type SetProtocolConstants struct{} + +// Key - +func (m *SetProtocolConstants) Key() string { + return "protocol_constants" +} + +// Description - +func (m *SetProtocolConstants) Description() string { + return "set constants for protocol" +} + +// Do - migrate function +func (m *SetProtocolConstants) Do(ctx *config.Context) error { + protocols := make([]models.Protocol, 0) + if err := ctx.ES.GetAll(&protocols); err != nil { + return err + } + + updatedModels := make([]elastic.Model, len(protocols)) + for i := range protocols { + if protocols[i].StartLevel == protocols[i].EndLevel && protocols[i].EndLevel == 0 { + protocols[i].Constants = models.Constants{} + updatedModels[i] = &protocols[i] + continue + } + + rpc, err := ctx.GetRPC(protocols[i].Network) + if err != nil { + return err + } + constants, err := rpc.GetNetworkConstants(protocols[i].EndLevel) + if err != nil { + return err + } + protocols[i].Constants = models.Constants{ + CostPerByte: constants.Get("cost_per_byte").Int(), + HardGasLimitPerOperation: constants.Get("hard_gas_limit_per_operation").Int(), + HardStorageLimitPerOperation: constants.Get("hard_storage_limit_per_operation").Int(), + TimeBetweenBlocks: constants.Get("time_between_blocks.0").Int(), + } + updatedModels[i] = &protocols[i] + } + return ctx.ES.BulkUpdate(updatedModels) +}