Skip to content

Commit

Permalink
Protocol migration
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored and m-kus committed Sep 7, 2020
1 parent 6b75998 commit df717b6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
30 changes: 17 additions & 13 deletions cmd/indexer/indexer/boost.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,30 @@ 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,
StartLevel: extProtocols[i].StartLevel,
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)
}

Expand Down
1 change: 1 addition & 0 deletions scripts/migration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var migrationsList = []migrations.Migration{
&migrations.DropMichelson{},
&migrations.SetOperationTags{},
&migrations.CreateTransfersTags{},
&migrations.SetProtocolConstants{},
}

func main() {
Expand Down
54 changes: 54 additions & 0 deletions scripts/migration/migrations/set_protocol_constants.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit df717b6

Please sign in to comment.