Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify migration runtime, no need for interface and environment #5651

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 18 additions & 220 deletions cmd/util/ledger/migrations/cadence_value_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ package migrations
import (
"fmt"
"strings"
"time"

"github.com/onflow/atree"
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/attribute"

"github.com/onflow/flow-go/cmd/util/ledger/util"
"github.com/onflow/flow-go/ledger"
Expand Down Expand Up @@ -99,7 +95,12 @@ func validateStorageDomain(

newValue := newStorageMap.ReadValue(nil, mapKey)

err := cadenceValueEqual(oldRuntime.Interpreter, oldValue, newRuntime.Interpreter, newValue)
err := cadenceValueEqual(
oldRuntime.Interpreter,
oldValue,
newRuntime.Interpreter,
newValue,
)
if err != nil {
if verboseLogging {
log.Info().
Expand All @@ -112,7 +113,13 @@ func validateStorageDomain(
Msgf("failed to validate value")
}

return fmt.Errorf("failed to validate value for address %s, domain %s, key %s: %s", address.Hex(), domain, key, err.Error())
return fmt.Errorf(
"failed to validate value for address %s, domain %s, key %s: %s",
address.Hex(),
domain,
key,
err.Error(),
)
}
}

Expand Down Expand Up @@ -380,22 +387,13 @@ func newReadonlyStorageRuntime(payloads []*ledger.Payload) (

storage := runtime.NewStorage(readonlyLedger, nil)

env := runtime.NewBaseInterpreterEnvironment(runtime.Config{
AccountLinkingEnabled: true,
// Attachments are enabled everywhere except for Mainnet
AttachmentsEnabled: true,
// Capability Controllers are enabled everywhere except for Mainnet
CapabilityControllersEnabled: true,
})

env.Configure(
&NoopRuntimeInterface{},
runtime.NewCodesAndPrograms(),
storage,
inter, err := interpreter.NewInterpreter(
nil,
nil,
&interpreter.Config{
Storage: storage,
},
)

inter, err := interpreter.NewInterpreter(nil, nil, env.InterpreterConfig)
if err != nil {
return nil, err
}
Expand All @@ -405,203 +403,3 @@ func newReadonlyStorageRuntime(payloads []*ledger.Payload) (
Storage: storage,
}, nil
}

// NoopRuntimeInterface is a runtime interface that can be used in migrations.
type NoopRuntimeInterface struct {
}

func (NoopRuntimeInterface) ResolveLocation(_ []runtime.Identifier, _ runtime.Location) ([]runtime.ResolvedLocation, error) {
panic("unexpected ResolveLocation call")
}

func (NoopRuntimeInterface) GetCode(_ runtime.Location) ([]byte, error) {
panic("unexpected GetCode call")
}

func (NoopRuntimeInterface) GetAccountContractCode(_ common.AddressLocation) ([]byte, error) {
panic("unexpected GetAccountContractCode call")
}

func (NoopRuntimeInterface) GetOrLoadProgram(_ runtime.Location, _ func() (*interpreter.Program, error)) (*interpreter.Program, error) {
panic("unexpected GetOrLoadProgram call")
}

func (NoopRuntimeInterface) MeterMemory(_ common.MemoryUsage) error {
return nil
}

func (NoopRuntimeInterface) MeterComputation(_ common.ComputationKind, _ uint) error {
return nil
}

func (NoopRuntimeInterface) GetValue(_, _ []byte) (value []byte, err error) {
panic("unexpected GetValue call")
}

func (NoopRuntimeInterface) SetValue(_, _, _ []byte) (err error) {
panic("unexpected SetValue call")
}

func (NoopRuntimeInterface) CreateAccount(_ runtime.Address) (address runtime.Address, err error) {
panic("unexpected CreateAccount call")
}

func (NoopRuntimeInterface) AddEncodedAccountKey(_ runtime.Address, _ []byte) error {
panic("unexpected AddEncodedAccountKey call")
}

func (NoopRuntimeInterface) RevokeEncodedAccountKey(_ runtime.Address, _ int) (publicKey []byte, err error) {
panic("unexpected RevokeEncodedAccountKey call")
}

func (NoopRuntimeInterface) AddAccountKey(_ runtime.Address, _ *runtime.PublicKey, _ runtime.HashAlgorithm, _ int) (*runtime.AccountKey, error) {
panic("unexpected AddAccountKey call")
}

func (NoopRuntimeInterface) GetAccountKey(_ runtime.Address, _ int) (*runtime.AccountKey, error) {
panic("unexpected GetAccountKey call")
}

func (NoopRuntimeInterface) RevokeAccountKey(_ runtime.Address, _ int) (*runtime.AccountKey, error) {
panic("unexpected RevokeAccountKey call")
}

func (NoopRuntimeInterface) UpdateAccountContractCode(_ common.AddressLocation, _ []byte) (err error) {
panic("unexpected UpdateAccountContractCode call")
}

func (NoopRuntimeInterface) RemoveAccountContractCode(common.AddressLocation) (err error) {
panic("unexpected RemoveAccountContractCode call")
}

func (NoopRuntimeInterface) GetSigningAccounts() ([]runtime.Address, error) {
panic("unexpected GetSigningAccounts call")
}

func (NoopRuntimeInterface) ProgramLog(_ string) error {
panic("unexpected ProgramLog call")
}

func (NoopRuntimeInterface) EmitEvent(_ cadence.Event) error {
panic("unexpected EmitEvent call")
}

func (NoopRuntimeInterface) ValueExists(_, _ []byte) (exists bool, err error) {
panic("unexpected ValueExists call")
}

func (NoopRuntimeInterface) GenerateUUID() (uint64, error) {
panic("unexpected GenerateUUID call")
}

func (NoopRuntimeInterface) GetComputationLimit() uint64 {
panic("unexpected GetComputationLimit call")
}

func (NoopRuntimeInterface) SetComputationUsed(_ uint64) error {
panic("unexpected SetComputationUsed call")
}

func (NoopRuntimeInterface) DecodeArgument(_ []byte, _ cadence.Type) (cadence.Value, error) {
panic("unexpected DecodeArgument call")
}

func (NoopRuntimeInterface) GetCurrentBlockHeight() (uint64, error) {
panic("unexpected GetCurrentBlockHeight call")
}

func (NoopRuntimeInterface) GetBlockAtHeight(_ uint64) (block runtime.Block, exists bool, err error) {
panic("unexpected GetBlockAtHeight call")
}

func (NoopRuntimeInterface) ReadRandom([]byte) error {
panic("unexpected ReadRandom call")
}

func (NoopRuntimeInterface) VerifySignature(_ []byte, _ string, _ []byte, _ []byte, _ runtime.SignatureAlgorithm, _ runtime.HashAlgorithm) (bool, error) {
panic("unexpected VerifySignature call")
}

func (NoopRuntimeInterface) Hash(_ []byte, _ string, _ runtime.HashAlgorithm) ([]byte, error) {
panic("unexpected Hash call")
}

func (NoopRuntimeInterface) GetAccountBalance(_ common.Address) (value uint64, err error) {
panic("unexpected GetAccountBalance call")
}

func (NoopRuntimeInterface) GetAccountAvailableBalance(_ common.Address) (value uint64, err error) {
panic("unexpected GetAccountAvailableBalance call")
}

func (NoopRuntimeInterface) GetStorageUsed(_ runtime.Address) (value uint64, err error) {
panic("unexpected GetStorageUsed call")
}

func (NoopRuntimeInterface) GetStorageCapacity(_ runtime.Address) (value uint64, err error) {
panic("unexpected GetStorageCapacity call")
}

func (NoopRuntimeInterface) ImplementationDebugLog(_ string) error {
panic("unexpected ImplementationDebugLog call")
}

func (NoopRuntimeInterface) ValidatePublicKey(_ *runtime.PublicKey) error {
panic("unexpected ValidatePublicKey call")
}

func (NoopRuntimeInterface) GetAccountContractNames(_ runtime.Address) ([]string, error) {
panic("unexpected GetAccountContractNames call")
}

func (NoopRuntimeInterface) AllocateStorageIndex(_ []byte) (atree.StorageIndex, error) {
panic("unexpected AllocateStorageIndex call")
}

func (NoopRuntimeInterface) ComputationUsed() (uint64, error) {
panic("unexpected ComputationUsed call")
}

func (NoopRuntimeInterface) MemoryUsed() (uint64, error) {
panic("unexpected MemoryUsed call")
}

func (NoopRuntimeInterface) InteractionUsed() (uint64, error) {
panic("unexpected InteractionUsed call")
}

func (NoopRuntimeInterface) SetInterpreterSharedState(_ *interpreter.SharedState) {
panic("unexpected SetInterpreterSharedState call")
}

func (NoopRuntimeInterface) GetInterpreterSharedState() *interpreter.SharedState {
panic("unexpected GetInterpreterSharedState call")
}

func (NoopRuntimeInterface) AccountKeysCount(_ runtime.Address) (uint64, error) {
panic("unexpected AccountKeysCount call")
}

func (NoopRuntimeInterface) BLSVerifyPOP(_ *runtime.PublicKey, _ []byte) (bool, error) {
panic("unexpected BLSVerifyPOP call")
}

func (NoopRuntimeInterface) BLSAggregateSignatures(_ [][]byte) ([]byte, error) {
panic("unexpected BLSAggregateSignatures call")
}

func (NoopRuntimeInterface) BLSAggregatePublicKeys(_ []*runtime.PublicKey) (*runtime.PublicKey, error) {
panic("unexpected BLSAggregatePublicKeys call")
}

func (NoopRuntimeInterface) ResourceOwnerChanged(_ *interpreter.Interpreter, _ *interpreter.CompositeValue, _ common.Address, _ common.Address) {
panic("unexpected ResourceOwnerChanged call")
}

func (NoopRuntimeInterface) GenerateAccountID(_ common.Address) (uint64, error) {
panic("unexpected GenerateAccountID call")
}

func (NoopRuntimeInterface) RecordTrace(_ string, _ runtime.Location, _ time.Duration, _ []attribute.KeyValue) {
panic("unexpected RecordTrace call")
}
28 changes: 4 additions & 24 deletions cmd/util/ledger/migrations/migrator_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,13 @@ func newMigratorRuntime(
accountsAtreeLedger := util.NewAccountsAtreeLedger(accounts)
storage := runtime.NewStorage(accountsAtreeLedger, nil)

ri := &util.MigrationRuntimeInterface{
Accounts: accounts,
}

env := runtime.NewBaseInterpreterEnvironment(runtime.Config{
AccountLinkingEnabled: true,
// Attachments are enabled everywhere except for Mainnet
AttachmentsEnabled: true,
// Capability Controllers are enabled everywhere except for Mainnet
CapabilityControllersEnabled: true,
})

env.Configure(
ri,
runtime.NewCodesAndPrograms(),
storage,
runtime.NewCoverageReport(),
)

inter, err := interpreter.NewInterpreter(
nil,
nil,
env.InterpreterConfig)
&interpreter.Config{
Storage: storage,
},
)
if err != nil {
return nil, err
}
Expand All @@ -78,7 +62,3 @@ type migratorRuntime struct {
Address common.Address
Accounts *util.AccountsAtreeLedger
}

func (mr *migratorRuntime) GetReadOnlyStorage() *runtime.Storage {
return runtime.NewStorage(util.NewPayloadsReadonlyLedger(mr.Snapshot), nil)
}
Loading
Loading