diff --git a/cmd/util/ledger/util/migration_runtime_interface.go b/cmd/util/ledger/util/migration_runtime_interface.go index 64c4e5f6689..8cb809bf684 100644 --- a/cmd/util/ledger/util/migration_runtime_interface.go +++ b/cmd/util/ledger/util/migration_runtime_interface.go @@ -3,12 +3,7 @@ package util import ( "errors" "fmt" - "time" - "go.opentelemetry.io/otel/attribute" - - "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" @@ -39,12 +34,15 @@ type GerOrLoadProgramListenerFunc func( // MigrationRuntimeInterface is a runtime interface that can be used in migrations. // It only allows parsing and checking of contracts. type MigrationRuntimeInterface struct { + runtime.EmptyRuntimeInterface GetContractCodeFunc GetContractCodeFunc GetContractNamesFunc GetContractNamesFunc GetOrLoadProgramFunc GetOrLoadProgramFunc GetOrLoadProgramListenerFunc GerOrLoadProgramListenerFunc } +var _ runtime.Interface = &MigrationRuntimeInterface{} + func NewMigrationRuntimeInterface( getCodeFunc GetContractCodeFunc, getContractNamesFunc GetContractNamesFunc, @@ -167,203 +165,6 @@ func (m *MigrationRuntimeInterface) GetOrLoadProgram( return getOrLoadProgram(location, load) } -func (m *MigrationRuntimeInterface) MeterMemory(_ common.MemoryUsage) error { - return nil -} - -func (m *MigrationRuntimeInterface) MeterComputation(_ common.ComputationKind, _ uint) error { - return nil -} - -func (m *MigrationRuntimeInterface) GetValue(_, _ []byte) (value []byte, err error) { - panic("unexpected GetValue call") -} - -func (m *MigrationRuntimeInterface) SetValue(_, _, _ []byte) (err error) { - panic("unexpected SetValue call") -} - -func (m *MigrationRuntimeInterface) CreateAccount(_ runtime.Address) (address runtime.Address, err error) { - panic("unexpected CreateAccount call") -} - -func (m *MigrationRuntimeInterface) AddEncodedAccountKey(_ runtime.Address, _ []byte) error { - panic("unexpected AddEncodedAccountKey call") -} - -func (m *MigrationRuntimeInterface) RevokeEncodedAccountKey(_ runtime.Address, _ int) (publicKey []byte, err error) { - panic("unexpected RevokeEncodedAccountKey call") -} - -func (m *MigrationRuntimeInterface) AddAccountKey( - _ runtime.Address, - _ *runtime.PublicKey, - _ runtime.HashAlgorithm, - _ int, -) (*runtime.AccountKey, error) { - panic("unexpected AddAccountKey call") -} - -func (m *MigrationRuntimeInterface) GetAccountKey(_ runtime.Address, _ uint32) (*runtime.AccountKey, error) { - panic("unexpected GetAccountKey call") -} - -func (m *MigrationRuntimeInterface) RevokeAccountKey(_ runtime.Address, _ uint32) (*runtime.AccountKey, error) { - panic("unexpected RevokeAccountKey call") -} - -func (m *MigrationRuntimeInterface) UpdateAccountContractCode(_ common.AddressLocation, _ []byte) (err error) { - panic("unexpected UpdateAccountContractCode call") -} - -func (m *MigrationRuntimeInterface) RemoveAccountContractCode(common.AddressLocation) (err error) { - panic("unexpected RemoveAccountContractCode call") -} - -func (m *MigrationRuntimeInterface) GetSigningAccounts() ([]runtime.Address, error) { - panic("unexpected GetSigningAccounts call") -} - -func (m *MigrationRuntimeInterface) ProgramLog(_ string) error { - panic("unexpected ProgramLog call") -} - -func (m *MigrationRuntimeInterface) EmitEvent(_ cadence.Event) error { - panic("unexpected EmitEvent call") -} - -func (m *MigrationRuntimeInterface) ValueExists(_, _ []byte) (exists bool, err error) { - panic("unexpected ValueExists call") -} - -func (m *MigrationRuntimeInterface) GenerateUUID() (uint64, error) { - panic("unexpected GenerateUUID call") -} - -func (m *MigrationRuntimeInterface) GetComputationLimit() uint64 { - panic("unexpected GetComputationLimit call") -} - -func (m *MigrationRuntimeInterface) SetComputationUsed(_ uint64) error { - panic("unexpected SetComputationUsed call") -} - -func (m *MigrationRuntimeInterface) DecodeArgument(_ []byte, _ cadence.Type) (cadence.Value, error) { - panic("unexpected DecodeArgument call") -} - -func (m *MigrationRuntimeInterface) GetCurrentBlockHeight() (uint64, error) { - panic("unexpected GetCurrentBlockHeight call") -} - -func (m *MigrationRuntimeInterface) GetBlockAtHeight(_ uint64) (block runtime.Block, exists bool, err error) { - panic("unexpected GetBlockAtHeight call") -} - -func (m *MigrationRuntimeInterface) ReadRandom([]byte) error { - panic("unexpected ReadRandom call") -} - -func (m *MigrationRuntimeInterface) VerifySignature( - _ []byte, - _ string, - _ []byte, - _ []byte, - _ runtime.SignatureAlgorithm, - _ runtime.HashAlgorithm, -) (bool, error) { - panic("unexpected VerifySignature call") -} - -func (m *MigrationRuntimeInterface) Hash(_ []byte, _ string, _ runtime.HashAlgorithm) ([]byte, error) { - panic("unexpected Hash call") -} - -func (m *MigrationRuntimeInterface) GetAccountBalance(_ common.Address) (value uint64, err error) { - panic("unexpected GetAccountBalance call") -} - -func (m *MigrationRuntimeInterface) GetAccountAvailableBalance(_ common.Address) (value uint64, err error) { - panic("unexpected GetAccountAvailableBalance call") -} - -func (m *MigrationRuntimeInterface) GetStorageUsed(_ runtime.Address) (value uint64, err error) { - panic("unexpected GetStorageUsed call") -} - -func (m *MigrationRuntimeInterface) GetStorageCapacity(_ runtime.Address) (value uint64, err error) { - panic("unexpected GetStorageCapacity call") -} - -func (m *MigrationRuntimeInterface) ImplementationDebugLog(_ string) error { - panic("unexpected ImplementationDebugLog call") -} - -func (m *MigrationRuntimeInterface) ValidatePublicKey(_ *runtime.PublicKey) error { - panic("unexpected ValidatePublicKey call") -} - -func (m *MigrationRuntimeInterface) GetAccountContractNames(_ runtime.Address) ([]string, error) { - panic("unexpected GetAccountContractNames call") -} - -func (m *MigrationRuntimeInterface) AllocateStorageIndex(_ []byte) (atree.StorageIndex, error) { - panic("unexpected AllocateStorageIndex call") -} - -func (m *MigrationRuntimeInterface) ComputationUsed() (uint64, error) { - panic("unexpected ComputationUsed call") -} - -func (m *MigrationRuntimeInterface) MemoryUsed() (uint64, error) { - panic("unexpected MemoryUsed call") -} - -func (m *MigrationRuntimeInterface) InteractionUsed() (uint64, error) { - panic("unexpected InteractionUsed call") -} - -func (m *MigrationRuntimeInterface) SetInterpreterSharedState(_ *interpreter.SharedState) { - panic("unexpected SetInterpreterSharedState call") -} - -func (m *MigrationRuntimeInterface) GetInterpreterSharedState() *interpreter.SharedState { - panic("unexpected GetInterpreterSharedState call") -} - -func (m *MigrationRuntimeInterface) AccountKeysCount(_ runtime.Address) (uint32, error) { - panic("unexpected AccountKeysCount call") -} - -func (m *MigrationRuntimeInterface) BLSVerifyPOP(_ *runtime.PublicKey, _ []byte) (bool, error) { - panic("unexpected BLSVerifyPOP call") -} - -func (m *MigrationRuntimeInterface) BLSAggregateSignatures(_ [][]byte) ([]byte, error) { - panic("unexpected BLSAggregateSignatures call") -} - -func (m *MigrationRuntimeInterface) BLSAggregatePublicKeys(_ []*runtime.PublicKey) (*runtime.PublicKey, error) { - panic("unexpected BLSAggregatePublicKeys call") -} - -func (m *MigrationRuntimeInterface) ResourceOwnerChanged( - _ *interpreter.Interpreter, - _ *interpreter.CompositeValue, - _ common.Address, - _ common.Address, -) { - panic("unexpected ResourceOwnerChanged call") -} - -func (m *MigrationRuntimeInterface) GenerateAccountID(_ common.Address) (uint64, error) { - panic("unexpected GenerateAccountID call") -} - -func (m *MigrationRuntimeInterface) RecordTrace(_ string, _ runtime.Location, _ time.Duration, _ []attribute.KeyValue) { - panic("unexpected RecordTrace call") -} - type migrationTransactionPreparer struct { state.NestedTransactionPreparer derived.DerivedTransactionPreparer diff --git a/fvm/evm/stdlib/checking.go b/fvm/evm/stdlib/checking.go index b9ae9683c71..d4862cb56c6 100644 --- a/fvm/evm/stdlib/checking.go +++ b/fvm/evm/stdlib/checking.go @@ -2,20 +2,17 @@ package stdlib import ( "fmt" - "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" - "go.opentelemetry.io/otel/attribute" ) // checkingInterface is a runtime.Interface implementation // that can be used for ParseAndCheckProgram. // It is not suitable for execution. type checkingInterface struct { + runtime.EmptyRuntimeInterface SystemContractCodes map[common.AddressLocation][]byte Programs map[runtime.Location]*interpreter.Program } @@ -95,189 +92,3 @@ func (r *checkingInterface) GetOrLoadProgram( func (r *checkingInterface) GetAccountContractCode(location common.AddressLocation) (code []byte, err error) { return r.SystemContractCodes[location], nil } - -func (*checkingInterface) MeterMemory(_ common.MemoryUsage) error { - // NO-OP - return nil -} - -func (*checkingInterface) MeterComputation(_ common.ComputationKind, _ uint) error { - panic("unexpected call to MeterComputation") -} - -func (*checkingInterface) ComputationUsed() (uint64, error) { - panic("unexpected call to ComputationUsed") -} - -func (*checkingInterface) MemoryUsed() (uint64, error) { - panic("unexpected call to MemoryUsed") -} - -func (*checkingInterface) InteractionUsed() (uint64, error) { - panic("unexpected call to InteractionUsed") -} - -func (*checkingInterface) GetCode(_ runtime.Location) ([]byte, error) { - panic("unexpected call to GetCode") -} - -func (*checkingInterface) SetInterpreterSharedState(_ *interpreter.SharedState) { - panic("unexpected call to SetInterpreterSharedState") -} - -func (*checkingInterface) GetInterpreterSharedState() *interpreter.SharedState { - panic("unexpected call to GetInterpreterSharedState") -} - -func (*checkingInterface) GetValue(_, _ []byte) (value []byte, err error) { - panic("unexpected call to GetValue") -} - -func (*checkingInterface) SetValue(_, _, _ []byte) (err error) { - panic("unexpected call to SetValue") -} - -func (*checkingInterface) ValueExists(_, _ []byte) (exists bool, err error) { - panic("unexpected call to ValueExists") -} - -func (*checkingInterface) AllocateStorageIndex(_ []byte) (atree.StorageIndex, error) { - panic("unexpected call to AllocateStorageIndex") -} - -func (*checkingInterface) CreateAccount(_ runtime.Address) (address runtime.Address, err error) { - panic("unexpected call to CreateAccount") -} - -func (*checkingInterface) AddAccountKey( - _ runtime.Address, - _ *runtime.PublicKey, - _ runtime.HashAlgorithm, - _ int, -) (*runtime.AccountKey, error) { - panic("unexpected call to AddAccountKey") -} - -func (*checkingInterface) GetAccountKey(_ runtime.Address, _ uint32) (*runtime.AccountKey, error) { - panic("unexpected call to GetAccountKey") -} - -func (*checkingInterface) AccountKeysCount(_ runtime.Address) (uint32, error) { - panic("unexpected call to AccountKeysCount") -} - -func (*checkingInterface) RevokeAccountKey(_ runtime.Address, _ uint32) (*runtime.AccountKey, error) { - panic("unexpected call to RevokeAccountKey") -} - -func (*checkingInterface) UpdateAccountContractCode(_ common.AddressLocation, _ []byte) (err error) { - panic("unexpected call to UpdateAccountContractCode") -} - -func (*checkingInterface) RemoveAccountContractCode(_ common.AddressLocation) (err error) { - panic("unexpected call to RemoveAccountContractCode") -} - -func (*checkingInterface) GetSigningAccounts() ([]runtime.Address, error) { - panic("unexpected call to GetSigningAccounts") -} - -func (*checkingInterface) ProgramLog(_ string) error { - panic("unexpected call to ProgramLog") -} - -func (*checkingInterface) EmitEvent(_ cadence.Event) error { - panic("unexpected call to EmitEvent") -} - -func (*checkingInterface) GenerateUUID() (uint64, error) { - panic("unexpected call to GenerateUUID") -} - -func (*checkingInterface) DecodeArgument(_ []byte, _ cadence.Type) (cadence.Value, error) { - panic("unexpected call to DecodeArgument") -} - -func (*checkingInterface) GetCurrentBlockHeight() (uint64, error) { - panic("unexpected call to GetCurrentBlockHeight") -} - -func (*checkingInterface) GetBlockAtHeight(_ uint64) (block runtime.Block, exists bool, err error) { - panic("unexpected call to GetBlockAtHeight") -} - -func (*checkingInterface) ReadRandom(_ []byte) error { - panic("unexpected call to ReadRandom") -} - -func (*checkingInterface) VerifySignature( - _ []byte, - _ string, - _ []byte, - _ []byte, - _ runtime.SignatureAlgorithm, - _ runtime.HashAlgorithm, -) (bool, error) { - panic("unexpected call to VerifySignature") -} - -func (*checkingInterface) Hash(_ []byte, _ string, _ runtime.HashAlgorithm) ([]byte, error) { - panic("unexpected call to Hash") -} - -func (*checkingInterface) GetAccountBalance(_ common.Address) (value uint64, err error) { - panic("unexpected call to GetAccountBalance") -} - -func (*checkingInterface) GetAccountAvailableBalance(_ common.Address) (value uint64, err error) { - panic("unexpected call to GetAccountAvailableBalance") -} - -func (*checkingInterface) GetStorageUsed(_ runtime.Address) (value uint64, err error) { - panic("unexpected call to GetStorageUsed") -} - -func (*checkingInterface) GetStorageCapacity(_ runtime.Address) (value uint64, err error) { - panic("unexpected call to GetStorageCapacity") -} - -func (*checkingInterface) ImplementationDebugLog(_ string) error { - panic("unexpected call to ImplementationDebugLog") -} - -func (*checkingInterface) ValidatePublicKey(_ *runtime.PublicKey) error { - panic("unexpected call to ValidatePublicKey") -} - -func (*checkingInterface) GetAccountContractNames(_ runtime.Address) ([]string, error) { - panic("unexpected call to GetAccountContractNames") -} - -func (*checkingInterface) RecordTrace(_ string, _ runtime.Location, _ time.Duration, _ []attribute.KeyValue) { - panic("unexpected call to RecordTrace") -} - -func (*checkingInterface) BLSVerifyPOP(_ *runtime.PublicKey, _ []byte) (bool, error) { - panic("unexpected call to BLSVerifyPOP") -} - -func (*checkingInterface) BLSAggregateSignatures(_ [][]byte) ([]byte, error) { - panic("unexpected call to BLSAggregateSignatures") -} - -func (*checkingInterface) BLSAggregatePublicKeys(_ []*runtime.PublicKey) (*runtime.PublicKey, error) { - panic("unexpected call to BLSAggregatePublicKeys") -} - -func (*checkingInterface) ResourceOwnerChanged( - _ *interpreter.Interpreter, - _ *interpreter.CompositeValue, - _ common.Address, - _ common.Address, -) { - panic("unexpected call to ResourceOwnerChanged") -} - -func (*checkingInterface) GenerateAccountID(_ common.Address) (uint64, error) { - panic("unexpected call to GenerateAccountID") -} diff --git a/fvm/evm/stdlib/contract_test.go b/fvm/evm/stdlib/contract_test.go index 19f7b40e797..ee7851d01ae 100644 --- a/fvm/evm/stdlib/contract_test.go +++ b/fvm/evm/stdlib/contract_test.go @@ -13,6 +13,7 @@ import ( "github.com/onflow/cadence/runtime/common" "github.com/onflow/cadence/runtime/sema" cadenceStdlib "github.com/onflow/cadence/runtime/stdlib" + . "github.com/onflow/cadence/runtime/tests/runtime_utils" "github.com/onflow/cadence/runtime/tests/utils" coreContracts "github.com/onflow/flow-core-contracts/lib/go/contracts" coreContractstemplates "github.com/onflow/flow-core-contracts/lib/go/templates" diff --git a/fvm/evm/testutils/cadence.go b/fvm/evm/testutils/cadence.go index 3ff2da39848..1f607427c22 100644 --- a/fvm/evm/testutils/cadence.go +++ b/fvm/evm/testutils/cadence.go @@ -1,28 +1,16 @@ package testutils import ( - "bytes" - "encoding/binary" - "encoding/hex" - "errors" "fmt" - "strconv" - "strings" - "sync/atomic" "testing" - "time" - "github.com/onflow/atree" "github.com/onflow/cadence" "github.com/onflow/cadence/encoding/json" "github.com/onflow/cadence/runtime" "github.com/onflow/cadence/runtime/ast" "github.com/onflow/cadence/runtime/common" - "github.com/onflow/cadence/runtime/interpreter" "github.com/onflow/cadence/runtime/sema" - cadenceStdlib "github.com/onflow/cadence/runtime/stdlib" "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" ) // LocationResolver is a location Cadence runtime interface location resolver @@ -75,26 +63,6 @@ func LocationResolver( return resolvedLocations, nil } -// TODO: replace with Cadence runtime testing utils once available https://github.com/onflow/cadence/pull/2800 - -func newLocationGenerator[T ~[32]byte]() func() T { - var count uint64 - return func() T { - t := T{} - newCount := atomic.AddUint64(&count, 1) - binary.LittleEndian.PutUint64(t[:], newCount) - return t - } -} - -func NewTransactionLocationGenerator() func() common.TransactionLocation { - return newLocationGenerator[common.TransactionLocation]() -} - -func NewScriptLocationGenerator() func() common.ScriptLocation { - return newLocationGenerator[common.ScriptLocation]() -} - func EncodeArgs(argValues []cadence.Value) [][]byte { args := make([][]byte, len(argValues)) for i, arg := range argValues { @@ -107,617 +75,6 @@ func EncodeArgs(argValues []cadence.Value) [][]byte { return args } -type TestLedger struct { - StoredValues map[string][]byte - OnValueExists func(owner, key []byte) (exists bool, err error) - OnGetValue func(owner, key []byte) (value []byte, err error) - OnSetValue func(owner, key, value []byte) (err error) - OnAllocateStorageIndex func(owner []byte) (atree.StorageIndex, error) -} - -var _ atree.Ledger = TestLedger{} - -func (s TestLedger) GetValue(owner, key []byte) (value []byte, err error) { - return s.OnGetValue(owner, key) -} - -func (s TestLedger) SetValue(owner, key, value []byte) (err error) { - return s.OnSetValue(owner, key, value) -} - -func (s TestLedger) ValueExists(owner, key []byte) (exists bool, err error) { - return s.OnValueExists(owner, key) -} - -func (s TestLedger) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) { - return s.OnAllocateStorageIndex(owner) -} - -func (s TestLedger) Dump() { - // Only used for testing/debugging purposes - for key, data := range s.StoredValues { //nolint:maprange - fmt.Printf("%s:\n", strconv.Quote(key)) - fmt.Printf("%s\n", hex.Dump(data)) - println() - } -} - -func NewTestLedger( - onRead func(owner, key, value []byte), - onWrite func(owner, key, value []byte), -) TestLedger { - - storageKey := func(owner, key string) string { - return strings.Join([]string{owner, key}, "|") - } - - storedValues := map[string][]byte{} - - storageIndices := map[string]uint64{} - - return TestLedger{ - StoredValues: storedValues, - OnValueExists: func(owner, key []byte) (bool, error) { - value := storedValues[storageKey(string(owner), string(key))] - return len(value) > 0, nil - }, - OnGetValue: func(owner, key []byte) (value []byte, err error) { - value = storedValues[storageKey(string(owner), string(key))] - if onRead != nil { - onRead(owner, key, value) - } - return value, nil - }, - OnSetValue: func(owner, key, value []byte) (err error) { - storedValues[storageKey(string(owner), string(key))] = value - if onWrite != nil { - onWrite(owner, key, value) - } - return nil - }, - OnAllocateStorageIndex: func(owner []byte) (result atree.StorageIndex, err error) { - index := storageIndices[string(owner)] + 1 - storageIndices[string(owner)] = index - binary.BigEndian.PutUint64(result[:], index) - return - }, - } -} - -type TestRuntimeInterface struct { - Storage atree.Ledger - - OnResolveLocation func( - identifiers []runtime.Identifier, - location runtime.Location, - ) ( - []runtime.ResolvedLocation, - error, - ) - OnGetCode func(_ runtime.Location) ([]byte, error) - OnGetAndSetProgram func( - location runtime.Location, - load func() (*interpreter.Program, error), - ) (*interpreter.Program, error) - OnSetInterpreterSharedState func(state *interpreter.SharedState) - OnGetInterpreterSharedState func() *interpreter.SharedState - OnCreateAccount func(payer runtime.Address) (address runtime.Address, err error) - OnAddEncodedAccountKey func(address runtime.Address, publicKey []byte) error - OnRemoveEncodedAccountKey func(address runtime.Address, index uint32) (publicKey []byte, err error) - OnAddAccountKey func( - address runtime.Address, - publicKey *cadenceStdlib.PublicKey, - hashAlgo runtime.HashAlgorithm, - weight int, - ) (*cadenceStdlib.AccountKey, error) - OnGetAccountKey func(address runtime.Address, index uint32) (*cadenceStdlib.AccountKey, error) - OnRemoveAccountKey func(address runtime.Address, index uint32) (*cadenceStdlib.AccountKey, error) - OnAccountKeysCount func(address runtime.Address) (uint32, error) - OnUpdateAccountContractCode func(location common.AddressLocation, code []byte) error - OnGetAccountContractCode func(location common.AddressLocation) (code []byte, err error) - OnRemoveAccountContractCode func(location common.AddressLocation) (err error) - OnGetSigningAccounts func() ([]runtime.Address, error) - OnProgramLog func(string) - OnEmitEvent func(cadence.Event) error - OnResourceOwnerChanged func( - interpreter *interpreter.Interpreter, - resource *interpreter.CompositeValue, - oldAddress common.Address, - newAddress common.Address, - ) - OnGenerateUUID func() (uint64, error) - OnMeterComputation func(compKind common.ComputationKind, intensity uint) error - OnDecodeArgument func(b []byte, t cadence.Type) (cadence.Value, error) - OnProgramParsed func(location runtime.Location, duration time.Duration) - OnProgramChecked func(location runtime.Location, duration time.Duration) - OnProgramInterpreted func(location runtime.Location, duration time.Duration) - OnReadRandom func([]byte) error - OnVerifySignature func( - signature []byte, - tag string, - signedData []byte, - publicKey []byte, - signatureAlgorithm runtime.SignatureAlgorithm, - hashAlgorithm runtime.HashAlgorithm, - ) (bool, error) - OnHash func( - data []byte, - tag string, - hashAlgorithm runtime.HashAlgorithm, - ) ([]byte, error) - OnSetCadenceValue func(owner runtime.Address, key string, value cadence.Value) (err error) - OnGetAccountBalance func(_ runtime.Address) (uint64, error) - OnGetAccountAvailableBalance func(_ runtime.Address) (uint64, error) - OnGetStorageUsed func(_ runtime.Address) (uint64, error) - OnGetStorageCapacity func(_ runtime.Address) (uint64, error) - Programs map[runtime.Location]*interpreter.Program - OnImplementationDebugLog func(message string) error - OnValidatePublicKey func(publicKey *cadenceStdlib.PublicKey) error - OnBLSVerifyPOP func(pk *cadenceStdlib.PublicKey, s []byte) (bool, error) - OnBLSAggregateSignatures func(sigs [][]byte) ([]byte, error) - OnBLSAggregatePublicKeys func(keys []*cadenceStdlib.PublicKey) (*cadenceStdlib.PublicKey, error) - OnGetAccountContractNames func(address runtime.Address) ([]string, error) - OnRecordTrace func( - operation string, - location runtime.Location, - duration time.Duration, - attrs []attribute.KeyValue, - ) - OnMeterMemory func(usage common.MemoryUsage) error - OnComputationUsed func() (uint64, error) - OnMemoryUsed func() (uint64, error) - OnInteractionUsed func() (uint64, error) - OnGenerateAccountID func(address common.Address) (uint64, error) - - lastUUID uint64 - accountIDs map[common.Address]uint64 - updatedContractCode bool -} - -// TestRuntimeInterface should implement Interface -var _ runtime.Interface = &TestRuntimeInterface{} - -func (i *TestRuntimeInterface) ResolveLocation( - identifiers []runtime.Identifier, - location runtime.Location, -) ([]runtime.ResolvedLocation, error) { - if i.OnResolveLocation == nil { - return []runtime.ResolvedLocation{ - { - Location: location, - Identifiers: identifiers, - }, - }, nil - } - return i.OnResolveLocation(identifiers, location) -} - -func (i *TestRuntimeInterface) GetCode(location runtime.Location) ([]byte, error) { - if i.OnGetCode == nil { - return nil, nil - } - return i.OnGetCode(location) -} - -func (i *TestRuntimeInterface) GetOrLoadProgram( - location runtime.Location, - load func() (*interpreter.Program, error), -) ( - program *interpreter.Program, - err error, -) { - if i.OnGetAndSetProgram == nil { - if i.Programs == nil { - i.Programs = map[runtime.Location]*interpreter.Program{} - } - - var ok bool - program, ok = i.Programs[location] - if ok { - return - } - - program, err = load() - - // NOTE: important: still set empty program, - // even if error occurred - - i.Programs[location] = program - - return - } - - return i.OnGetAndSetProgram(location, load) -} - -func (i *TestRuntimeInterface) SetInterpreterSharedState(state *interpreter.SharedState) { - if i.OnSetInterpreterSharedState == nil { - return - } - - i.OnSetInterpreterSharedState(state) -} - -func (i *TestRuntimeInterface) GetInterpreterSharedState() *interpreter.SharedState { - if i.OnGetInterpreterSharedState == nil { - return nil - } - - return i.OnGetInterpreterSharedState() -} - -func (i *TestRuntimeInterface) ValueExists(owner, key []byte) (exists bool, err error) { - return i.Storage.ValueExists(owner, key) -} - -func (i *TestRuntimeInterface) GetValue(owner, key []byte) (value []byte, err error) { - return i.Storage.GetValue(owner, key) -} - -func (i *TestRuntimeInterface) SetValue(owner, key, value []byte) (err error) { - return i.Storage.SetValue(owner, key, value) -} - -func (i *TestRuntimeInterface) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) { - return i.Storage.AllocateStorageIndex(owner) -} - -func (i *TestRuntimeInterface) CreateAccount(payer runtime.Address) (address runtime.Address, err error) { - if i.OnCreateAccount == nil { - panic("must specify TestRuntimeInterface.OnCreateAccount") - } - return i.OnCreateAccount(payer) -} - -func (i *TestRuntimeInterface) AddEncodedAccountKey(address runtime.Address, publicKey []byte) error { - if i.OnAddEncodedAccountKey == nil { - panic("must specify TestRuntimeInterface.OnAddEncodedAccountKey") - } - return i.OnAddEncodedAccountKey(address, publicKey) -} - -func (i *TestRuntimeInterface) RevokeEncodedAccountKey(address runtime.Address, index uint32) ([]byte, error) { - if i.OnRemoveEncodedAccountKey == nil { - panic("must specify TestRuntimeInterface.OnRemoveEncodedAccountKey") - } - return i.OnRemoveEncodedAccountKey(address, index) -} - -func (i *TestRuntimeInterface) AddAccountKey( - address runtime.Address, - publicKey *cadenceStdlib.PublicKey, - hashAlgo runtime.HashAlgorithm, - weight int, -) (*cadenceStdlib.AccountKey, error) { - if i.OnAddAccountKey == nil { - panic("must specify TestRuntimeInterface.OnAddAccountKey") - } - return i.OnAddAccountKey(address, publicKey, hashAlgo, weight) -} - -func (i *TestRuntimeInterface) GetAccountKey(address runtime.Address, index uint32) (*cadenceStdlib.AccountKey, error) { - if i.OnGetAccountKey == nil { - panic("must specify TestRuntimeInterface.OnGetAccountKey") - } - return i.OnGetAccountKey(address, index) -} - -func (i *TestRuntimeInterface) AccountKeysCount(address runtime.Address) (uint32, error) { - if i.OnAccountKeysCount == nil { - panic("must specify TestRuntimeInterface.OnAccountKeysCount") - } - return i.OnAccountKeysCount(address) -} - -func (i *TestRuntimeInterface) RevokeAccountKey(address runtime.Address, index uint32) (*cadenceStdlib.AccountKey, error) { - if i.OnRemoveAccountKey == nil { - panic("must specify TestRuntimeInterface.OnRemoveAccountKey") - } - return i.OnRemoveAccountKey(address, index) -} - -func (i *TestRuntimeInterface) UpdateAccountContractCode(location common.AddressLocation, code []byte) (err error) { - if i.OnUpdateAccountContractCode == nil { - panic("must specify TestRuntimeInterface.OnUpdateAccountContractCode") - } - - err = i.OnUpdateAccountContractCode(location, code) - if err != nil { - return err - } - - i.updatedContractCode = true - - return nil -} - -func (i *TestRuntimeInterface) GetAccountContractCode(location common.AddressLocation) (code []byte, err error) { - if i.OnGetAccountContractCode == nil { - panic("must specify TestRuntimeInterface.OnGetAccountContractCode") - } - return i.OnGetAccountContractCode(location) -} - -func (i *TestRuntimeInterface) RemoveAccountContractCode(location common.AddressLocation) (err error) { - if i.OnRemoveAccountContractCode == nil { - panic("must specify TestRuntimeInterface.OnRemoveAccountContractCode") - } - return i.OnRemoveAccountContractCode(location) -} - -func (i *TestRuntimeInterface) GetSigningAccounts() ([]runtime.Address, error) { - if i.OnGetSigningAccounts == nil { - return nil, nil - } - return i.OnGetSigningAccounts() -} - -func (i *TestRuntimeInterface) ProgramLog(message string) error { - i.OnProgramLog(message) - return nil -} - -func (i *TestRuntimeInterface) EmitEvent(event cadence.Event) error { - return i.OnEmitEvent(event) -} - -func (i *TestRuntimeInterface) ResourceOwnerChanged( - interpreter *interpreter.Interpreter, - resource *interpreter.CompositeValue, - oldOwner common.Address, - newOwner common.Address, -) { - if i.OnResourceOwnerChanged != nil { - i.OnResourceOwnerChanged( - interpreter, - resource, - oldOwner, - newOwner, - ) - } -} - -func (i *TestRuntimeInterface) GenerateUUID() (uint64, error) { - if i.OnGenerateUUID == nil { - i.lastUUID++ - return i.lastUUID, nil - } - return i.OnGenerateUUID() -} - -func (i *TestRuntimeInterface) MeterComputation(compKind common.ComputationKind, intensity uint) error { - if i.OnMeterComputation == nil { - return nil - } - return i.OnMeterComputation(compKind, intensity) -} - -func (i *TestRuntimeInterface) DecodeArgument(b []byte, t cadence.Type) (cadence.Value, error) { - if i.OnDecodeArgument == nil { - panic("must specify TestRuntimeInterface.OnDecodeArgument") - } - return i.OnDecodeArgument(b, t) -} - -func (i *TestRuntimeInterface) ProgramParsed(location runtime.Location, duration time.Duration) { - if i.OnProgramParsed == nil { - return - } - i.OnProgramParsed(location, duration) -} - -func (i *TestRuntimeInterface) ProgramChecked(location runtime.Location, duration time.Duration) { - if i.OnProgramChecked == nil { - return - } - i.OnProgramChecked(location, duration) -} - -func (i *TestRuntimeInterface) ProgramInterpreted(location runtime.Location, duration time.Duration) { - if i.OnProgramInterpreted == nil { - return - } - i.OnProgramInterpreted(location, duration) -} - -func (i *TestRuntimeInterface) GetCurrentBlockHeight() (uint64, error) { - return 1, nil -} - -func (i *TestRuntimeInterface) GetBlockAtHeight(height uint64) (block cadenceStdlib.Block, exists bool, err error) { - - buf := new(bytes.Buffer) - err = binary.Write(buf, binary.BigEndian, height) - if err != nil { - panic(err) - } - - encoded := buf.Bytes() - var hash cadenceStdlib.BlockHash - copy(hash[sema.BlockTypeIdFieldType.Size-int64(len(encoded)):], encoded) - - block = cadenceStdlib.Block{ - Height: height, - View: height, - Hash: hash, - Timestamp: time.Unix(int64(height), 0).UnixNano(), - } - return block, true, nil -} - -func (i *TestRuntimeInterface) ReadRandom(buffer []byte) error { - if i.OnReadRandom == nil { - return nil - } - return i.OnReadRandom(buffer) -} - -func (i *TestRuntimeInterface) VerifySignature( - signature []byte, - tag string, - signedData []byte, - publicKey []byte, - signatureAlgorithm runtime.SignatureAlgorithm, - hashAlgorithm runtime.HashAlgorithm, -) (bool, error) { - if i.OnVerifySignature == nil { - return false, nil - } - return i.OnVerifySignature( - signature, - tag, - signedData, - publicKey, - signatureAlgorithm, - hashAlgorithm, - ) -} - -func (i *TestRuntimeInterface) Hash(data []byte, tag string, hashAlgorithm runtime.HashAlgorithm) ([]byte, error) { - if i.OnHash == nil { - return nil, nil - } - return i.OnHash(data, tag, hashAlgorithm) -} - -func (i *TestRuntimeInterface) SetCadenceValue(owner common.Address, key string, value cadence.Value) (err error) { - if i.OnSetCadenceValue == nil { - panic("must specify TestRuntimeInterface.OnSetCadenceValue") - } - return i.OnSetCadenceValue(owner, key, value) -} - -func (i *TestRuntimeInterface) GetAccountBalance(address runtime.Address) (uint64, error) { - if i.OnGetAccountBalance == nil { - panic("must specify TestRuntimeInterface.OnGetAccountBalance") - } - return i.OnGetAccountBalance(address) -} - -func (i *TestRuntimeInterface) GetAccountAvailableBalance(address runtime.Address) (uint64, error) { - if i.OnGetAccountAvailableBalance == nil { - panic("must specify TestRuntimeInterface.OnGetAccountAvailableBalance") - } - return i.OnGetAccountAvailableBalance(address) -} - -func (i *TestRuntimeInterface) GetStorageUsed(address runtime.Address) (uint64, error) { - if i.OnGetStorageUsed == nil { - panic("must specify TestRuntimeInterface.OnGetStorageUsed") - } - return i.OnGetStorageUsed(address) -} - -func (i *TestRuntimeInterface) GetStorageCapacity(address runtime.Address) (uint64, error) { - if i.OnGetStorageCapacity == nil { - panic("must specify TestRuntimeInterface.OnGetStorageCapacity") - } - return i.OnGetStorageCapacity(address) -} - -func (i *TestRuntimeInterface) ImplementationDebugLog(message string) error { - if i.OnImplementationDebugLog == nil { - return nil - } - return i.OnImplementationDebugLog(message) -} - -func (i *TestRuntimeInterface) ValidatePublicKey(key *cadenceStdlib.PublicKey) error { - if i.OnValidatePublicKey == nil { - return errors.New("mock defaults to public key validation failure") - } - - return i.OnValidatePublicKey(key) -} - -func (i *TestRuntimeInterface) BLSVerifyPOP(key *cadenceStdlib.PublicKey, s []byte) (bool, error) { - if i.OnBLSVerifyPOP == nil { - return false, nil - } - - return i.OnBLSVerifyPOP(key, s) -} - -func (i *TestRuntimeInterface) BLSAggregateSignatures(sigs [][]byte) ([]byte, error) { - if i.OnBLSAggregateSignatures == nil { - return []byte{}, nil - } - - return i.OnBLSAggregateSignatures(sigs) -} - -func (i *TestRuntimeInterface) BLSAggregatePublicKeys(keys []*cadenceStdlib.PublicKey) (*cadenceStdlib.PublicKey, error) { - if i.OnBLSAggregatePublicKeys == nil { - return nil, nil - } - - return i.OnBLSAggregatePublicKeys(keys) -} - -func (i *TestRuntimeInterface) GetAccountContractNames(address runtime.Address) ([]string, error) { - if i.OnGetAccountContractNames == nil { - return []string{}, nil - } - - return i.OnGetAccountContractNames(address) -} - -func (i *TestRuntimeInterface) GenerateAccountID(address common.Address) (uint64, error) { - if i.OnGenerateAccountID == nil { - if i.accountIDs == nil { - i.accountIDs = map[common.Address]uint64{} - } - i.accountIDs[address]++ - return i.accountIDs[address], nil - } - - return i.OnGenerateAccountID(address) -} - -func (i *TestRuntimeInterface) RecordTrace( - operation string, - location runtime.Location, - duration time.Duration, - attrs []attribute.KeyValue, -) { - if i.OnRecordTrace == nil { - return - } - i.OnRecordTrace(operation, location, duration, attrs) -} - -func (i *TestRuntimeInterface) MeterMemory(usage common.MemoryUsage) error { - if i.OnMeterMemory == nil { - return nil - } - - return i.OnMeterMemory(usage) -} - -func (i *TestRuntimeInterface) ComputationUsed() (uint64, error) { - if i.OnComputationUsed == nil { - return 0, nil - } - - return i.OnComputationUsed() -} - -func (i *TestRuntimeInterface) MemoryUsed() (uint64, error) { - if i.OnMemoryUsed == nil { - return 0, nil - } - - return i.OnMemoryUsed() -} - -func (i *TestRuntimeInterface) InteractionUsed() (uint64, error) { - if i.OnInteractionUsed == nil { - return 0, nil - } - - return i.OnInteractionUsed() -} - func CheckCadenceEventTypes(t testing.TB, events []cadence.Event, expectedTypes []string) { require.Equal(t, len(events), len(expectedTypes)) for i, ev := range events {