testutils

package
v0.33.10-script-instru... Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2024 License: AGPL-3.0 Imports: 37 Imported by: 0

Documentation

Index

Constants

View Source
const EOATestAccount1KeyHex = "9c647b8b7c4e7c3490668fb6c11473619db80c93704c70893d3813af4090c39c"

address: 658bdf435d810c91414ec09147daa6db62406379

Variables

View Source
var TestComputationLimit = uint(math.MaxUint64 - 1)
View Source
var TestFlowEVMRootAddress = flow.BytesToAddress([]byte("FlowEVM"))

Functions

func ConvertToCadence

func ConvertToCadence(data []byte) []cadence.Value

func DeployContract

func DeployContract(t testing.TB, tc *TestContract, led atree.Ledger, flowEVMRootAddress flow.Address)

func EncodeArgs

func EncodeArgs(argValues []cadence.Value) [][]byte

func GetRandomLogFixture

func GetRandomLogFixture(t testing.TB) *gethTypes.Log

func NewScriptLocationGenerator

func NewScriptLocationGenerator() func() common.ScriptLocation

func NewTransactionLocationGenerator

func NewTransactionLocationGenerator() func() common.TransactionLocation

func RandomAddress

func RandomAddress(t testing.TB) types.Address

func RandomBigInt

func RandomBigInt(limit int64) *big.Int

func RandomCommonAddress

func RandomCommonAddress(t testing.TB) gethCommon.Address

func RandomCommonHash

func RandomCommonHash(t testing.TB) gethCommon.Hash

func RandomData

func RandomData(t testing.TB) []byte

func RandomGas

func RandomGas(limit int64) uint64

func RunWithDeployedContract

func RunWithDeployedContract(t testing.TB, tc *TestContract, led atree.Ledger, flowEVMRootAddress flow.Address, f func(*TestContract))

func RunWithEOATestAccount

func RunWithEOATestAccount(t testing.TB, led atree.Ledger, flowEVMRootAddress flow.Address, f func(*EOATestAccount))

func RunWithTestBackend

func RunWithTestBackend(t testing.TB, f func(*TestBackend))

func RunWithTestFlowEVMRootAddress

func RunWithTestFlowEVMRootAddress(t testing.TB, backend atree.Ledger, f func(flow.Address))

func SingleIdentifierLocationResolver

func SingleIdentifierLocationResolver(t testing.TB) func(
	identifiers []runtime.Identifier,
	location runtime.Location,
) (
	[]runtime.ResolvedLocation,
	error,
)

Types

type EOATestAccount

type EOATestAccount struct {
	// contains filtered or unexported fields
}

func FundAndGetEOATestAccount

func FundAndGetEOATestAccount(t testing.TB, led atree.Ledger, flowEVMRootAddress flow.Address) *EOATestAccount

func GetTestEOAAccount

func GetTestEOAAccount(t testing.TB, keyHex string) *EOATestAccount

func (*EOATestAccount) Address

func (a *EOATestAccount) Address() types.Address

func (*EOATestAccount) PrepareAndSignTx

func (a *EOATestAccount) PrepareAndSignTx(
	t testing.TB,
	to gethCommon.Address,
	data []byte,
	amount *big.Int,
	gasLimit uint64,
	gasPrice *big.Int,
) *gethTypes.Transaction

func (*EOATestAccount) PrepareSignAndEncodeTx

func (a *EOATestAccount) PrepareSignAndEncodeTx(
	t testing.TB,
	to gethCommon.Address,
	data []byte,
	amount *big.Int,
	gasLimit uint64,
	gasPrice *big.Int,
) []byte

func (*EOATestAccount) SetNonce

func (a *EOATestAccount) SetNonce(nonce uint64)

func (*EOATestAccount) SignTx

type TestBackend

type TestBackend struct {
	*TestValueStore
	// contains filtered or unexported fields
}

func (TestBackend) ComputationAvailable

func (m TestBackend) ComputationAvailable(
	kind common.ComputationKind,
	intensity uint,
) bool

func (TestBackend) ComputationIntensities

func (m TestBackend) ComputationIntensities() meter.MeteredComputationIntensities

func (TestBackend) ComputationUsed

func (m TestBackend) ComputationUsed() (uint64, error)

func (TestBackend) ConvertedServiceEvents

func (vs TestBackend) ConvertedServiceEvents() flow.ServiceEventList

func (*TestBackend) DropEvents

func (tb *TestBackend) DropEvents()

func (TestBackend) EmitEvent

func (vs TestBackend) EmitEvent(event cadence.Event) error

func (TestBackend) Events

func (vs TestBackend) Events() flow.EventsList

func (TestBackend) InteractionUsed

func (m TestBackend) InteractionUsed() (uint64, error)

func (TestBackend) MemoryUsed

func (m TestBackend) MemoryUsed() (uint64, error)

func (TestBackend) MeterComputation

func (m TestBackend) MeterComputation(
	kind common.ComputationKind,
	intensity uint,
) error

func (TestBackend) MeterEmittedEvent

func (m TestBackend) MeterEmittedEvent(byteSize uint64) error

func (TestBackend) MeterMemory

func (m TestBackend) MeterMemory(usage common.MemoryUsage) error

func (TestBackend) Reset

func (vs TestBackend) Reset()

func (TestBackend) ServiceEvents

func (vs TestBackend) ServiceEvents() flow.EventsList

func (TestBackend) TotalEmittedEventBytes

func (m TestBackend) TotalEmittedEventBytes() uint64

func (*TestBackend) TotalStorageSize

func (tb *TestBackend) TotalStorageSize() int

type TestContract

type TestContract struct {
	Code       string
	ABI        string
	ByteCode   []byte
	DeployedAt types.Address
}

func GetDummyKittyTestContract

func GetDummyKittyTestContract(t testing.TB) *TestContract

func GetStorageTestContract

func GetStorageTestContract(tb testing.TB) *TestContract

func (*TestContract) MakeCallData

func (tc *TestContract) MakeCallData(t testing.TB, name string, args ...interface{}) []byte

func (*TestContract) SetDeployedAt

func (tc *TestContract) SetDeployedAt(deployedAt types.Address)

type TestEmulator

type TestEmulator struct {
	BalanceOfFunc      func(address types.Address) (*big.Int, error)
	NonceOfFunc        func(address types.Address) (uint64, error)
	CodeOfFunc         func(address types.Address) (types.Code, error)
	DirectCallFunc     func(call *types.DirectCall) (*types.Result, error)
	RunTransactionFunc func(tx *gethTypes.Transaction) (*types.Result, error)
}

func (*TestEmulator) BalanceOf

func (em *TestEmulator) BalanceOf(address types.Address) (*big.Int, error)

BalanceOf returns the balance of this address

func (*TestEmulator) CodeOf

func (em *TestEmulator) CodeOf(address types.Address) (types.Code, error)

CodeOf returns the code for this address (if smart contract is deployed at this address)

func (*TestEmulator) DirectCall

func (em *TestEmulator) DirectCall(call *types.DirectCall) (*types.Result, error)

DirectCall executes a direct call

func (*TestEmulator) NewBlockView

func (em *TestEmulator) NewBlockView(_ types.BlockContext) (types.BlockView, error)

NewBlock returns a new block

func (*TestEmulator) NewReadOnlyBlockView

func (em *TestEmulator) NewReadOnlyBlockView(_ types.BlockContext) (types.ReadOnlyBlockView, error)

NewBlock returns a new block view

func (*TestEmulator) NonceOf

func (em *TestEmulator) NonceOf(address types.Address) (uint64, error)

NonceOfFunc returns the nonce for this address

func (*TestEmulator) RunTransaction

func (em *TestEmulator) RunTransaction(tx *gethTypes.Transaction) (*types.Result, error)

RunTransaction runs a transaction and collect gas fees to the coinbase account

type TestLedger

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)
}

func NewTestLedger

func NewTestLedger(
	onRead func(owner, key, value []byte),
	onWrite func(owner, key, value []byte),
) TestLedger

func (TestLedger) AllocateStorageIndex

func (s TestLedger) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error)

func (TestLedger) Dump

func (s TestLedger) Dump()

func (TestLedger) GetValue

func (s TestLedger) GetValue(owner, key []byte) (value []byte, err error)

func (TestLedger) SetValue

func (s TestLedger) SetValue(owner, key, value []byte) (err error)

func (TestLedger) ValueExists

func (s TestLedger) ValueExists(owner, key []byte) (exists bool, err error)

type TestRuntimeInterface

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 int) (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 int) (*cadenceStdlib.AccountKey, error)
	OnRemoveAccountKey          func(address runtime.Address, index int) (*cadenceStdlib.AccountKey, error)
	OnAccountKeysCount          func(address runtime.Address) (uint64, 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)
	// contains filtered or unexported fields
}

func (*TestRuntimeInterface) AccountKeysCount

func (i *TestRuntimeInterface) AccountKeysCount(address runtime.Address) (uint64, error)

func (*TestRuntimeInterface) AddAccountKey

func (i *TestRuntimeInterface) AddAccountKey(
	address runtime.Address,
	publicKey *cadenceStdlib.PublicKey,
	hashAlgo runtime.HashAlgorithm,
	weight int,
) (*cadenceStdlib.AccountKey, error)

func (*TestRuntimeInterface) AddEncodedAccountKey

func (i *TestRuntimeInterface) AddEncodedAccountKey(address runtime.Address, publicKey []byte) error

func (*TestRuntimeInterface) AllocateStorageIndex

func (i *TestRuntimeInterface) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error)

func (*TestRuntimeInterface) BLSAggregatePublicKeys

func (i *TestRuntimeInterface) BLSAggregatePublicKeys(keys []*cadenceStdlib.PublicKey) (*cadenceStdlib.PublicKey, error)

func (*TestRuntimeInterface) BLSAggregateSignatures

func (i *TestRuntimeInterface) BLSAggregateSignatures(sigs [][]byte) ([]byte, error)

func (*TestRuntimeInterface) BLSVerifyPOP

func (i *TestRuntimeInterface) BLSVerifyPOP(key *cadenceStdlib.PublicKey, s []byte) (bool, error)

func (*TestRuntimeInterface) ComputationUsed

func (i *TestRuntimeInterface) ComputationUsed() (uint64, error)

func (*TestRuntimeInterface) CreateAccount

func (i *TestRuntimeInterface) CreateAccount(payer runtime.Address) (address runtime.Address, err error)

func (*TestRuntimeInterface) DecodeArgument

func (i *TestRuntimeInterface) DecodeArgument(b []byte, t cadence.Type) (cadence.Value, error)

func (*TestRuntimeInterface) EmitEvent

func (i *TestRuntimeInterface) EmitEvent(event cadence.Event) error

func (*TestRuntimeInterface) GenerateAccountID

func (i *TestRuntimeInterface) GenerateAccountID(address common.Address) (uint64, error)

func (*TestRuntimeInterface) GenerateUUID

func (i *TestRuntimeInterface) GenerateUUID() (uint64, error)

func (*TestRuntimeInterface) GetAccountAvailableBalance

func (i *TestRuntimeInterface) GetAccountAvailableBalance(address runtime.Address) (uint64, error)

func (*TestRuntimeInterface) GetAccountBalance

func (i *TestRuntimeInterface) GetAccountBalance(address runtime.Address) (uint64, error)

func (*TestRuntimeInterface) GetAccountContractCode

func (i *TestRuntimeInterface) GetAccountContractCode(location common.AddressLocation) (code []byte, err error)

func (*TestRuntimeInterface) GetAccountContractNames

func (i *TestRuntimeInterface) GetAccountContractNames(address runtime.Address) ([]string, error)

func (*TestRuntimeInterface) GetAccountKey

func (i *TestRuntimeInterface) GetAccountKey(address runtime.Address, index int) (*cadenceStdlib.AccountKey, error)

func (*TestRuntimeInterface) GetBlockAtHeight

func (i *TestRuntimeInterface) GetBlockAtHeight(height uint64) (block cadenceStdlib.Block, exists bool, err error)

func (*TestRuntimeInterface) GetCode

func (i *TestRuntimeInterface) GetCode(location runtime.Location) ([]byte, error)

func (*TestRuntimeInterface) GetCurrentBlockHeight

func (i *TestRuntimeInterface) GetCurrentBlockHeight() (uint64, error)

func (*TestRuntimeInterface) GetInterpreterSharedState

func (i *TestRuntimeInterface) GetInterpreterSharedState() *interpreter.SharedState

func (*TestRuntimeInterface) GetOrLoadProgram

func (i *TestRuntimeInterface) GetOrLoadProgram(
	location runtime.Location,
	load func() (*interpreter.Program, error),
) (
	program *interpreter.Program,
	err error,
)

func (*TestRuntimeInterface) GetSigningAccounts

func (i *TestRuntimeInterface) GetSigningAccounts() ([]runtime.Address, error)

func (*TestRuntimeInterface) GetStorageCapacity

func (i *TestRuntimeInterface) GetStorageCapacity(address runtime.Address) (uint64, error)

func (*TestRuntimeInterface) GetStorageUsed

func (i *TestRuntimeInterface) GetStorageUsed(address runtime.Address) (uint64, error)

func (*TestRuntimeInterface) GetValue

func (i *TestRuntimeInterface) GetValue(owner, key []byte) (value []byte, err error)

func (*TestRuntimeInterface) Hash

func (i *TestRuntimeInterface) Hash(data []byte, tag string, hashAlgorithm runtime.HashAlgorithm) ([]byte, error)

func (*TestRuntimeInterface) ImplementationDebugLog

func (i *TestRuntimeInterface) ImplementationDebugLog(message string) error

func (*TestRuntimeInterface) InteractionUsed

func (i *TestRuntimeInterface) InteractionUsed() (uint64, error)

func (*TestRuntimeInterface) MemoryUsed

func (i *TestRuntimeInterface) MemoryUsed() (uint64, error)

func (*TestRuntimeInterface) MeterComputation

func (i *TestRuntimeInterface) MeterComputation(compKind common.ComputationKind, intensity uint) error

func (*TestRuntimeInterface) MeterMemory

func (i *TestRuntimeInterface) MeterMemory(usage common.MemoryUsage) error

func (*TestRuntimeInterface) ProgramChecked

func (i *TestRuntimeInterface) ProgramChecked(location runtime.Location, duration time.Duration)

func (*TestRuntimeInterface) ProgramInterpreted

func (i *TestRuntimeInterface) ProgramInterpreted(location runtime.Location, duration time.Duration)

func (*TestRuntimeInterface) ProgramLog

func (i *TestRuntimeInterface) ProgramLog(message string) error

func (*TestRuntimeInterface) ProgramParsed

func (i *TestRuntimeInterface) ProgramParsed(location runtime.Location, duration time.Duration)

func (*TestRuntimeInterface) ReadRandom

func (i *TestRuntimeInterface) ReadRandom(buffer []byte) error

func (*TestRuntimeInterface) RecordTrace

func (i *TestRuntimeInterface) RecordTrace(
	operation string,
	location runtime.Location,
	duration time.Duration,
	attrs []attribute.KeyValue,
)

func (*TestRuntimeInterface) RemoveAccountContractCode

func (i *TestRuntimeInterface) RemoveAccountContractCode(location common.AddressLocation) (err error)

func (*TestRuntimeInterface) ResolveLocation

func (i *TestRuntimeInterface) ResolveLocation(
	identifiers []runtime.Identifier,
	location runtime.Location,
) ([]runtime.ResolvedLocation, error)

func (*TestRuntimeInterface) ResourceOwnerChanged

func (i *TestRuntimeInterface) ResourceOwnerChanged(
	interpreter *interpreter.Interpreter,
	resource *interpreter.CompositeValue,
	oldOwner common.Address,
	newOwner common.Address,
)

func (*TestRuntimeInterface) RevokeAccountKey

func (i *TestRuntimeInterface) RevokeAccountKey(address runtime.Address, index int) (*cadenceStdlib.AccountKey, error)

func (*TestRuntimeInterface) RevokeEncodedAccountKey

func (i *TestRuntimeInterface) RevokeEncodedAccountKey(address runtime.Address, index int) ([]byte, error)

func (*TestRuntimeInterface) SetCadenceValue

func (i *TestRuntimeInterface) SetCadenceValue(owner common.Address, key string, value cadence.Value) (err error)

func (*TestRuntimeInterface) SetInterpreterSharedState

func (i *TestRuntimeInterface) SetInterpreterSharedState(state *interpreter.SharedState)

func (*TestRuntimeInterface) SetValue

func (i *TestRuntimeInterface) SetValue(owner, key, value []byte) (err error)

func (*TestRuntimeInterface) UpdateAccountContractCode

func (i *TestRuntimeInterface) UpdateAccountContractCode(location common.AddressLocation, code []byte) (err error)

func (*TestRuntimeInterface) ValidatePublicKey

func (i *TestRuntimeInterface) ValidatePublicKey(key *cadenceStdlib.PublicKey) error

func (*TestRuntimeInterface) ValueExists

func (i *TestRuntimeInterface) ValueExists(owner, key []byte) (exists bool, err error)

func (*TestRuntimeInterface) VerifySignature

func (i *TestRuntimeInterface) VerifySignature(
	signature []byte,
	tag string,
	signedData []byte,
	publicKey []byte,
	signatureAlgorithm runtime.SignatureAlgorithm,
	hashAlgorithm runtime.HashAlgorithm,
) (bool, error)

type TestValueStore

type TestValueStore struct {
	GetValueFunc             func(owner, key []byte) ([]byte, error)
	SetValueFunc             func(owner, key, value []byte) error
	ValueExistsFunc          func(owner, key []byte) (bool, error)
	AllocateStorageIndexFunc func(owner []byte) (atree.StorageIndex, error)
	TotalStorageSizeFunc     func() int
	TotalBytesReadFunc       func() int
	TotalBytesWrittenFunc    func() int
	TotalStorageItemsFunc    func() int
	ResetStatsFunc           func()
}

func GetSimpleValueStore

func GetSimpleValueStore() *TestValueStore

func (*TestValueStore) AllocateStorageIndex

func (vs *TestValueStore) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error)

func (*TestValueStore) GetValue

func (vs *TestValueStore) GetValue(owner, key []byte) ([]byte, error)

func (*TestValueStore) ResetStats

func (vs *TestValueStore) ResetStats()

func (*TestValueStore) SetValue

func (vs *TestValueStore) SetValue(owner, key, value []byte) error

func (*TestValueStore) TotalBytesRead

func (vs *TestValueStore) TotalBytesRead() int

func (*TestValueStore) TotalBytesWritten

func (vs *TestValueStore) TotalBytesWritten() int

func (*TestValueStore) TotalStorageItems

func (vs *TestValueStore) TotalStorageItems() int

func (*TestValueStore) TotalStorageSize

func (vs *TestValueStore) TotalStorageSize() int

func (*TestValueStore) ValueExists

func (vs *TestValueStore) ValueExists(owner, key []byte) (bool, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL