Skip to content

Commit

Permalink
Fix: finding global constants (#942)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Sep 6, 2022
1 parent 5c724a4 commit 59dd61d
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 4 deletions.
14 changes: 11 additions & 3 deletions internal/bcd/contract/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,18 @@ func FindConstants(tree ast.UntypedAST) (types.Set, error) {

constants := make(types.Set)
for i := range tree {
if constant := parseConstants(tree[i]); constant != "" {
constants.Add(constant)
}
findConstants(tree[i], constants)
}

return constants, nil
}

func findConstants(node *base.Node, constants types.Set) {
if constant := parseConstants(node); constant != "" {
constants.Add(constant)
}

for i := range node.Args {
findConstants(node.Args[i], constants)
}
}
2 changes: 1 addition & 1 deletion internal/postgres/contract/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (storage *Storage) Script(address string, symLink string) (contract.Script,
// GetScripts -
func (storage *Storage) GetScripts(limit, offset int) (scripts []contract.Script, err error) {
err = storage.DB.Model(&scripts).
ColumnExpr("tags, hash, project_id, fail_strings, annotations, entrypoints").
ColumnExpr("id, tags, hash, fail_strings, annotations, entrypoints").
Limit(limit).Offset(offset).Order("id asc").Select()
return
}
Expand Down
1 change: 1 addition & 0 deletions scripts/migration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var migrationsList = []migrations.Migration{
&migrations.DefaultEntrypoint{},
&migrations.FindLostContracts{},
&migrations.BigMapStateCount{},
&migrations.GlobalConstantsRelations{},
}

func main() {
Expand Down
116 changes: 116 additions & 0 deletions scripts/migration/migrations/global_constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package migrations

import (
"context"
"encoding/json"
"strings"

"github.com/baking-bad/bcdhub/internal/bcd/ast"
astContract "github.com/baking-bad/bcdhub/internal/bcd/contract"
"github.com/baking-bad/bcdhub/internal/config"
"github.com/baking-bad/bcdhub/internal/logger"
"github.com/baking-bad/bcdhub/internal/models/account"
"github.com/baking-bad/bcdhub/internal/models/contract"
)

// GlobalConstantsRelations -
type GlobalConstantsRelations struct{}

// Key -
func (m *GlobalConstantsRelations) Key() string {
return "recover_global_constants"
}

// Description -
func (m *GlobalConstantsRelations) Description() string {
return "recover `global_constants` relation"
}

// Do - migrate function
func (m *GlobalConstantsRelations) Do(ctx *config.Context) error {
var offset int
var end bool
for !end {
scripts, err := ctx.Scripts.GetScripts(10, offset)
if err != nil {
if strings.Contains(err.Error(), "no rows in result set") {
end = true
continue
}
return err
}

for i := range scripts {
var accountID int64
if err := ctx.StorageDB.DB.Model(&contract.Contract{}).
Column("account_id").
Where("jakarta_id = ?", scripts[i].ID).
WhereOr("babylon_id = ?", scripts[i].ID).
OrderExpr("id ASC").Limit(1).
Select(&accountID); err != nil {
if strings.Contains(err.Error(), "no rows in result set") {
continue
}
return err
}

var address string
if err := ctx.StorageDB.DB.Model(&account.Account{}).
Column("address").
Where("id = ?", accountID).
OrderExpr("id ASC").Limit(1).
Select(&address); err != nil {
if strings.Contains(err.Error(), "no rows in result set") {
continue
}
return err
}

logger.Info().Str("address", address).Msg("finding constants...")

data, err := ctx.RPC.GetRawScript(context.Background(), address, 0)
if err != nil {
return err
}

var cd astContract.ContractData
if err := json.Unmarshal(data, &cd); err != nil {
return err
}

var tree ast.UntypedAST
if err := json.Unmarshal(cd.Code, &tree); err != nil {
return err
}

constants, err := astContract.FindConstants(tree)
if err != nil {
return err
}

if len(constants) == 0 {
continue
}
logger.Info().Str("address", address).Int("constants_count", len(constants)).Msg("found constants")

for key := range constants {
gc, err := ctx.GlobalConstants.Get(key)
if err != nil {
return err
}
relation := contract.ScriptConstants{
GlobalConstantId: gc.ID,
ScriptId: scripts[i].ID,
}

if _, err := ctx.StorageDB.DB.Model(&relation).Insert(); err != nil {
return err
}
}
}

offset += len(scripts)
end = len(scripts) == 0
}
return nil
}

0 comments on commit 59dd61d

Please sign in to comment.