Skip to content

Commit

Permalink
fix program loading
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Jan 23, 2024
1 parent 4de5312 commit 59def00
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions cmd/util/ledger/migrations/migrator_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@ import (

"github.com/onflow/flow-go/cmd/util/ledger/util"
"github.com/onflow/flow-go/fvm/environment"
"github.com/onflow/flow-go/fvm/storage"
"github.com/onflow/flow-go/fvm/storage/derived"
"github.com/onflow/flow-go/fvm/storage/state"
"github.com/onflow/flow-go/fvm/tracing"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/model/flow"
util2 "github.com/onflow/flow-go/module/util"
)

type migrationTransactionPreparer struct {
state.NestedTransactionPreparer
derived.DerivedTransactionPreparer
}

var _ storage.TransactionPreparer = migrationTransactionPreparer{}

// migratorRuntime is a runtime that can be used to run a migration on a single account
func newMigratorRuntime(
address common.Address,
Expand All @@ -31,14 +41,35 @@ func newMigratorRuntime(
if err != nil {
return nil, fmt.Errorf("failed to create payload snapshot: %w", err)
}

transactionState := state.NewTransactionState(snapshot, state.DefaultParameters())
accounts := environment.NewAccounts(transactionState)

accountsAtreeLedger := util.NewAccountsAtreeLedger(accounts)
storage := runtime.NewStorage(accountsAtreeLedger, util.NopMemoryGauge{})
runtimeStorage := runtime.NewStorage(accountsAtreeLedger, util.NopMemoryGauge{})

derivedChainData, err := derived.NewDerivedChainData(derived.DefaultDerivedDataCacheSize)
if err != nil {
return nil, fmt.Errorf("failed to create derived chain data: %w", err)
}

// TODO: how to get currentBlockId (flow.Identifier) for NewDerivedBlockDataForScript?
derivedTransactionData := derivedChainData.
NewDerivedBlockDataForScript(flow.Identifier{}).
NewSnapshotReadDerivedTransactionData()

ri := &util.MigrationRuntimeInterface{
Accounts: accounts,
Programs: environment.NewPrograms(
tracing.NewTracerSpan(),
util.NopMeter{},
environment.NoopMetricsReporter{},
migrationTransactionPreparer{
NestedTransactionPreparer: transactionState,
DerivedTransactionPreparer: derivedTransactionData,
},
accounts,
),
}

env := runtime.NewBaseInterpreterEnvironment(runtime.Config{
Expand All @@ -49,21 +80,15 @@ func newMigratorRuntime(
env.Configure(
ri,
runtime.NewCodesAndPrograms(),
storage,
runtimeStorage,
runtime.NewCoverageReport(),
)

ri.GetOrLoadProgramFunc = func(
location runtime.Location,
load func() (*interpreter.Program, error),
) (*interpreter.Program, error) {
return load()
}

inter, err := interpreter.NewInterpreter(
nil,
nil,
env.InterpreterConfig)
env.InterpreterConfig,
)
if err != nil {
return nil, err
}
Expand All @@ -74,7 +99,7 @@ func newMigratorRuntime(
Snapshot: snapshot,
TransactionState: transactionState,
Interpreter: inter,
Storage: storage,
Storage: runtimeStorage,
AccountsLedger: accountsAtreeLedger,
Accounts: accounts,
}, nil
Expand Down

0 comments on commit 59def00

Please sign in to comment.