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

[test] Properly setup EVM environment to allow usage of EVM types in test scripts #338

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 35 additions & 0 deletions test/test_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"bytes"
"errors"
"fmt"
"os"
"testing"
"time"

"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -5932,3 +5934,36 @@ func TestGetTests(t *testing.T) {

assert.ElementsMatch(t, []string{"test1", "test2", "test3"}, tests)
}

func TestEVMContract(t *testing.T) {
t.Parallel()

const testCode = `
import Test
import "EVM"

access(all)
fun testCOACreation() {
let coa <- EVM.createCadenceOwnedAccount()
Test.assertEqual(UInt(0), coa.address().balance().inAttoFLOW())

destroy <- coa
}
`

importResolver := func(location common.Location) (string, error) {
return "", fmt.Errorf("cannot find import location: %s", location)
}

output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
log := zerolog.New(output).With().Timestamp().Logger()
runner := NewTestRunner().
WithImportResolver(importResolver).
WithLogger(log)

results, err := runner.RunTests(testCode)
require.NoError(t, err)
for _, result := range results {
require.NoError(t, result.Error)
}
}
30 changes: 29 additions & 1 deletion test/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
"github.com/rs/zerolog"

"github.com/onflow/atree"
"github.com/onflow/flow-go/fvm/environment"
"github.com/onflow/flow-go/fvm/evm"
"github.com/onflow/flow-go/fvm/systemcontracts"
"github.com/onflow/flow-go/model/flow"

"github.com/onflow/cadence/runtime"
Expand Down Expand Up @@ -474,8 +477,9 @@ func (r *TestRunner) initializeEnvironment() (
backend.contracts = r.contracts
r.backend = backend

fvmEnv := r.backend.blockchain.NewScriptEnvironment()
ctx := runtime.Context{
Interface: r.backend.blockchain.NewScriptEnvironment(),
Interface: fvmEnv,
Location: testScriptLocation,
Environment: env,
}
Expand Down Expand Up @@ -506,6 +510,11 @@ func (r *TestRunner) initializeEnvironment() (
r.coverageReport,
)

err := setupEVMEnvironment(fvmEnv, env)
if err != nil {
panic(err)
}

return env, ctx
}

Expand Down Expand Up @@ -786,6 +795,11 @@ func (r *TestRunner) parseAndCheckImport(

env.CheckerConfig.ContractValueHandler = contractValueHandler

err = setupEVMEnvironment(r.backend.blockchain.NewScriptEnvironment(), env)
turbolent marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
panic(err)
}

code = r.replaceImports(code)
program, err := r.testRuntime.ParseAndCheckProgram([]byte(code), ctx)

Expand All @@ -796,6 +810,20 @@ func (r *TestRunner) parseAndCheckImport(
return program.Program, program.Elaboration, nil
}

func setupEVMEnvironment(
fvmEnv environment.Environment,
runtimeEnv runtime.Environment,
) error {
sc := systemcontracts.SystemContractsForChain(chain.ChainID())
return evm.SetupEnvironment(
chain.ChainID(),
fvmEnv,
runtimeEnv,
chain.ServiceAddress(),
sc.FlowToken.Address,
)
}

func baseContracts() map[string]common.Address {
contracts := make(map[string]common.Address, 0)
serviceAddress := common.Address(chain.ServiceAddress())
Expand Down
Loading