Skip to content

Commit

Permalink
update to new static types migration
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Jan 24, 2024
1 parent 59def00 commit 934b155
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 33 deletions.
2 changes: 2 additions & 0 deletions cmd/util/cmd/execution-state-extract/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ func run(*cobra.Command, []string) {
flagOutputDir,
log.Logger,
flagNWorker,
nil,
nil,
)

if err != nil {
Expand Down
41 changes: 28 additions & 13 deletions cmd/util/cmd/execution-state-extract/execution_state_extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/rs/zerolog"
"go.uber.org/atomic"

migrators "github.com/onflow/flow-go/cmd/util/ledger/migrations"
"github.com/onflow/flow-go/cmd/util/ledger/migrations"
"github.com/onflow/flow-go/cmd/util/ledger/reporters"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/ledger/common/hash"
Expand All @@ -34,7 +34,9 @@ func extractExecutionState(
targetHash flow.StateCommitment,
outputDir string,
log zerolog.Logger,
nWorker int, // number of concurrent worker to migation payloads
nWorker int, // number of concurrent worker to migration payloads
compositeTypeRules migrations.StaticTypeMigrationRules,
interfaceTypeRules migrations.StaticTypeMigrationRules,
) error {

log.Info().Msg("init WAL")
Expand Down Expand Up @@ -71,7 +73,15 @@ func extractExecutionState(

log.Info().Msg("init compactor")

compactor, err := complete.NewCompactor(led, diskWal, log, complete.DefaultCacheSize, checkpointDistance, checkpointsToKeep, atomic.NewBool(false))
compactor, err := complete.NewCompactor(
led,
diskWal,
log,
complete.DefaultCacheSize,
checkpointDistance,
checkpointsToKeep,
atomic.NewBool(false),
)
if err != nil {
return fmt.Errorf("cannot create compactor: %w", err)
}
Expand All @@ -89,16 +99,21 @@ func extractExecutionState(

capabilityIDs := map[interpreter.AddressPath]interpreter.UInt64Value{}

var migrations = []ledger.Migration{
migrators.CreateAccountBasedMigration(
migrations := []ledger.Migration{
migrations.CreateAccountBasedMigration(
log,
nWorker,
[]migrators.AccountBasedMigration{
[]migrations.AccountBasedMigration{
// do account usage migration before and after as a sanity check.
&migrators.AccountUsageMigrator{},
migrators.NewCadenceLinkValueMigrator(rwf, capabilityIDs),
migrators.NewCadenceValueMigrator(rwf, capabilityIDs),
&migrators.AccountUsageMigrator{},
&migrations.AccountUsageMigrator{},
migrations.NewCadenceLinkValueMigrator(rwf, capabilityIDs),
migrations.NewCadenceValueMigrator(
rwf,
capabilityIDs,
migrations.NewStaticTypeMigrator[*interpreter.CompositeStaticType](compositeTypeRules),
migrations.NewStaticTypeMigrator[*interpreter.InterfaceStaticType](interfaceTypeRules),
),
&migrations.AccountUsageMigrator{},
}),
}
newState := ledger.State(targetHash)
Expand Down Expand Up @@ -151,9 +166,9 @@ func createCheckpoint(
outputDir,
outputFile string,
) (ledger.State, error) {
statecommitment := ledger.State(newTrie.RootHash())
stateCommitment := ledger.State(newTrie.RootHash())

log.Info().Msgf("successfully built new trie. NEW ROOT STATECOMMIEMENT: %v", statecommitment.String())
log.Info().Msgf("successfully built new trie. NEW ROOT STATECOMMIEMENT: %v", stateCommitment.String())

err := os.MkdirAll(outputDir, os.ModePerm)
if err != nil {
Expand All @@ -174,7 +189,7 @@ func createCheckpoint(
}

log.Info().Msgf("checkpoint file successfully stored at: %v %v", outputDir, outputFile)
return statecommitment, nil
return stateCommitment, nil
}

func writeStatusFile(fileName string, e error) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func TestExtractExecutionState(t *testing.T) {
outdir,
zerolog.Nop(),
10,
nil,
nil,
)
require.Error(t, err)
})
Expand Down
31 changes: 19 additions & 12 deletions cmd/util/ledger/migrations/cadence_values_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"

"github.com/onflow/cadence/migrations/statictypes"
"github.com/rs/zerolog"

"github.com/onflow/flow-go/cmd/util/ledger/reporters"
Expand All @@ -16,11 +17,9 @@ import (
"github.com/onflow/flow-go/model/flow"

"github.com/onflow/cadence/migrations"
"github.com/onflow/cadence/migrations/account_type"
"github.com/onflow/cadence/migrations/capcons"
"github.com/onflow/cadence/migrations/entitlements"
"github.com/onflow/cadence/migrations/string_normalization"
"github.com/onflow/cadence/migrations/type_value"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
)
Expand Down Expand Up @@ -140,6 +139,8 @@ func (m *CadenceBaseMigrator) mergeRegisterChanges(
func NewCadenceValueMigrator(
rwf reporters.ReportWriterFactory,
capabilityIDs map[interpreter.AddressPath]interpreter.UInt64Value,
compositeTypeConverter statictypes.CompositeTypeConverterFunc,
interfaceTypeConverter statictypes.InterfaceTypeConverterFunc,
) *CadenceBaseMigrator {
return &CadenceBaseMigrator{
name: "cadence-value-migration",
Expand All @@ -157,8 +158,9 @@ func NewCadenceValueMigrator(
},
entitlements.NewEntitlementsMigration(inter),
string_normalization.NewStringNormalizingMigration(),
account_type.NewAccountTypeMigration(),
type_value.NewTypeValueMigration(),
statictypes.NewStaticTypeMigration().
WithCompositeTypeConverter(compositeTypeConverter).
WithInterfaceTypeConverter(interfaceTypeConverter),
}
},
}
Expand Down Expand Up @@ -209,24 +211,28 @@ func newValueMigrationReporter(rw reporters.ReportWriter, log zerolog.Logger) *c
}

func (t *cadenceValueMigrationReporter) Migrated(
addressPath interpreter.AddressPath,
storageKey interpreter.StorageKey,
storageMapKey interpreter.StorageMapKey,
migration string,
) {
t.rw.Write(cadenceValueMigrationReportEntry{
Address: addressPath,
Migration: migration,
StorageKey: storageKey,
StorageMapKey: storageMapKey,
Migration: migration,
})
}

func (t *cadenceValueMigrationReporter) Error(
addressPath interpreter.AddressPath,
storageKey interpreter.StorageKey,
storageMapKey interpreter.StorageMapKey,
migration string,
err error,
) {
t.log.Error().Msgf(
"failed to run %s for path %s: %s",
"failed to run %s for %s, %s: %s",
migration,
addressPath,
storageKey,
storageMapKey,
err,
)
}
Expand Down Expand Up @@ -274,8 +280,9 @@ func (t *cadenceValueMigrationReporter) MissingTarget(accountAddressPath interpr
}

type cadenceValueMigrationReportEntry struct {
Address interpreter.AddressPath `json:"address"`
Migration string `json:"migration"`
StorageKey interpreter.StorageKey `json:"storageKey"`
StorageMapKey interpreter.StorageMapKey `json:"StorageMapKey"`
Migration string `json:"migration"`
}

type capConsLinkMigration struct {
Expand Down
25 changes: 17 additions & 8 deletions cmd/util/ledger/migrations/cadence_values_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package migrations

import (
"fmt"
"io"
"os"
"testing"

"github.com/onflow/cadence/migrations"
"github.com/onflow/cadence/migrations/account_type"
"github.com/onflow/cadence/migrations/capcons"
"github.com/onflow/cadence/migrations/entitlements"
"github.com/onflow/cadence/migrations/statictypes"
"github.com/onflow/cadence/migrations/string_normalization"
"github.com/onflow/cadence/migrations/type_value"

"github.com/onflow/flow-go/fvm/environment"
"io"
"os"
"testing"

"github.com/rs/zerolog"

Expand Down Expand Up @@ -72,7 +73,16 @@ func TestCadenceValuesMigration(t *testing.T) {
payloads = runLinkMigration(t, address, payloads, capabilityIDs, rwf)

// Run remaining migrations
valueMigration := NewCadenceValueMigrator(rwf, capabilityIDs)
valueMigration := NewCadenceValueMigrator(
rwf,
capabilityIDs,
func(staticType *interpreter.CompositeStaticType) interpreter.StaticType {
return staticType
},
func(staticType *interpreter.InterfaceStaticType) interpreter.StaticType {
return staticType
},
)

logWriter := &writer{}
logger := zerolog.New(logWriter).Level(zerolog.ErrorLevel)
Expand Down Expand Up @@ -134,8 +144,7 @@ func TestCadenceValuesMigrationWithSwappedOrder(t *testing.T) {
Reporter: reporter,
},
string_normalization.NewStringNormalizingMigration(),
account_type.NewAccountTypeMigration(),
type_value.NewTypeValueMigration(),
statictypes.NewStaticTypeMigration(),

// Run this at the end
entitlements.NewEntitlementsMigration(inter),
Expand Down
24 changes: 24 additions & 0 deletions cmd/util/ledger/migrations/static_type_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package migrations

import (
"github.com/onflow/cadence/runtime/interpreter"
)

type StaticTypeMigrationRules map[interpreter.StaticType]interpreter.StaticType

func NewStaticTypeMigrator[T interpreter.StaticType](
rules StaticTypeMigrationRules,
) func(staticType T) interpreter.StaticType {
if rules == nil {
return func(original T) interpreter.StaticType {
return original
}
}

return func(original T) interpreter.StaticType {
if replacement, ok := rules[original]; ok {
return replacement
}
return original
}
}

0 comments on commit 934b155

Please sign in to comment.