From bf447f0149f75cd23d5945d0f8086dc74220473e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Mon, 6 Nov 2023 15:04:55 -0800 Subject: [PATCH 1/2] fix tests: use proper environment kind for transactions and scripts --- fvm/evm/stdlib/contract_test.go | 173 +++++++++++++++++++++++--------- 1 file changed, 127 insertions(+), 46 deletions(-) diff --git a/fvm/evm/stdlib/contract_test.go b/fvm/evm/stdlib/contract_test.go index e5fb109a9a9..48be7814d88 100644 --- a/fvm/evm/stdlib/contract_test.go +++ b/fvm/evm/stdlib/contract_test.go @@ -128,7 +128,7 @@ func deployContracts( rt runtime.Runtime, contractsAddress flow.Address, runtimeInterface *TestRuntimeInterface, - env runtime.Environment, + transactionEnvironment runtime.Environment, nextTransactionLocation func() common.TransactionLocation, ) { @@ -203,7 +203,7 @@ func deployContracts( }, runtime.Context{ Interface: runtimeInterface, - Environment: env, + Environment: transactionEnvironment, Location: nextTransactionLocation(), }, ) @@ -212,17 +212,40 @@ func deployContracts( } +func newEVMTransactionEnvironment(handler types.ContractHandler, service flow.Address) runtime.Environment { + transactionEnvironment := runtime.NewBaseInterpreterEnvironment(runtime.Config{}) + + stdlib.SetupEnvironment( + transactionEnvironment, + handler, + service, + ) + + return transactionEnvironment +} + +func newEVMScriptEnvironment(handler types.ContractHandler, service flow.Address) runtime.Environment { + scriptEnvironment := runtime.NewScriptInterpreterEnvironment(runtime.Config{}) + + stdlib.SetupEnvironment( + scriptEnvironment, + handler, + service, + ) + + return scriptEnvironment +} + func TestEVMAddressConstructionAndReturn(t *testing.T) { t.Parallel() handler := &testContractHandler{} - env := runtime.NewBaseInterpreterEnvironment(runtime.Config{}) - contractsAddress := flow.BytesToAddress([]byte{0x1}) - stdlib.SetupEnvironment(env, handler, contractsAddress) + transactionEnvironment := newEVMTransactionEnvironment(handler, contractsAddress) + scriptEnvironment := newEVMScriptEnvironment(handler, contractsAddress) rt := runtime.NewInterpreterRuntime(runtime.Config{}) @@ -279,7 +302,14 @@ func TestEVMAddressConstructionAndReturn(t *testing.T) { // Deploy contracts - deployContracts(t, rt, contractsAddress, runtimeInterface, env, nextTransactionLocation) + deployContracts( + t, + rt, + contractsAddress, + runtimeInterface, + transactionEnvironment, + nextTransactionLocation, + ) // Run script @@ -292,7 +322,7 @@ func TestEVMAddressConstructionAndReturn(t *testing.T) { }, runtime.Context{ Interface: runtimeInterface, - Environment: env, + Environment: scriptEnvironment, Location: nextScriptLocation(), }, ) @@ -317,11 +347,10 @@ func TestBalanceConstructionAndReturn(t *testing.T) { handler := &testContractHandler{} - env := runtime.NewBaseInterpreterEnvironment(runtime.Config{}) - contractsAddress := flow.BytesToAddress([]byte{0x1}) - stdlib.SetupEnvironment(env, handler, contractsAddress) + transactionEnvironment := newEVMTransactionEnvironment(handler, contractsAddress) + scriptEnvironment := newEVMScriptEnvironment(handler, contractsAddress) rt := runtime.NewInterpreterRuntime(runtime.Config{}) @@ -365,7 +394,14 @@ func TestBalanceConstructionAndReturn(t *testing.T) { // Deploy contracts - deployContracts(t, rt, contractsAddress, runtimeInterface, env, nextTransactionLocation) + deployContracts( + t, + rt, + contractsAddress, + runtimeInterface, + transactionEnvironment, + nextTransactionLocation, + ) // Run script @@ -381,7 +417,7 @@ func TestBalanceConstructionAndReturn(t *testing.T) { }, runtime.Context{ Interface: runtimeInterface, - Environment: env, + Environment: scriptEnvironment, Location: nextScriptLocation(), }, ) @@ -441,11 +477,10 @@ func TestEVMRun(t *testing.T) { }, } - env := runtime.NewBaseInterpreterEnvironment(runtime.Config{}) - contractsAddress := flow.BytesToAddress([]byte{0x1}) - stdlib.SetupEnvironment(env, handler, contractsAddress) + transactionEnvironment := newEVMTransactionEnvironment(handler, contractsAddress) + scriptEnvironment := newEVMScriptEnvironment(handler, contractsAddress) rt := runtime.NewInterpreterRuntime(runtime.Config{}) @@ -490,7 +525,14 @@ func TestEVMRun(t *testing.T) { // Deploy contracts - deployContracts(t, rt, contractsAddress, runtimeInterface, env, nextTransactionLocation) + deployContracts( + t, + rt, + contractsAddress, + runtimeInterface, + transactionEnvironment, + nextTransactionLocation, + ) // Run script @@ -501,7 +543,7 @@ func TestEVMRun(t *testing.T) { }, runtime.Context{ Interface: runtimeInterface, - Environment: env, + Environment: scriptEnvironment, Location: nextScriptLocation(), }, ) @@ -517,11 +559,10 @@ func TestEVMCreateBridgedAccount(t *testing.T) { handler := &testContractHandler{} - env := runtime.NewBaseInterpreterEnvironment(runtime.Config{}) - contractsAddress := flow.BytesToAddress([]byte{0x1}) - stdlib.SetupEnvironment(env, handler, contractsAddress) + transactionEnvironment := newEVMTransactionEnvironment(handler, contractsAddress) + scriptEnvironment := newEVMScriptEnvironment(handler, contractsAddress) rt := runtime.NewInterpreterRuntime(runtime.Config{}) @@ -572,7 +613,14 @@ func TestEVMCreateBridgedAccount(t *testing.T) { // Deploy contracts - deployContracts(t, rt, contractsAddress, runtimeInterface, env, nextTransactionLocation) + deployContracts( + t, + rt, + contractsAddress, + runtimeInterface, + transactionEnvironment, + nextTransactionLocation, + ) // Run script @@ -582,7 +630,7 @@ func TestEVMCreateBridgedAccount(t *testing.T) { }, runtime.Context{ Interface: runtimeInterface, - Environment: env, + Environment: scriptEnvironment, Location: nextScriptLocation(), }, ) @@ -638,11 +686,10 @@ func TestBridgedAccountCall(t *testing.T) { }, } - env := runtime.NewBaseInterpreterEnvironment(runtime.Config{}) - contractsAddress := flow.BytesToAddress([]byte{0x1}) - stdlib.SetupEnvironment(env, handler, contractsAddress) + transactionEnvironment := newEVMTransactionEnvironment(handler, contractsAddress) + scriptEnvironment := newEVMScriptEnvironment(handler, contractsAddress) rt := runtime.NewInterpreterRuntime(runtime.Config{}) @@ -696,7 +743,14 @@ func TestBridgedAccountCall(t *testing.T) { // Deploy contracts - deployContracts(t, rt, contractsAddress, runtimeInterface, env, nextTransactionLocation) + deployContracts( + t, + rt, + contractsAddress, + runtimeInterface, + transactionEnvironment, + nextTransactionLocation, + ) // Run script @@ -706,7 +760,7 @@ func TestBridgedAccountCall(t *testing.T) { }, runtime.Context{ Interface: runtimeInterface, - Environment: env, + Environment: scriptEnvironment, Location: nextScriptLocation(), }, ) @@ -721,11 +775,12 @@ func TestBridgedAccountCall(t *testing.T) { require.Equal(t, expected, actual) } -// TODO: deposit non-zero amount func TestEVMAddressDeposit(t *testing.T) { t.Parallel() + expectedBalance, err := cadence.NewUFix64FromParts(1, 23000000) + var deposited bool handler := &testContractHandler{ @@ -738,17 +793,20 @@ func TestEVMAddressDeposit(t *testing.T) { address: fromAddress, deposit: func(vault *types.FLOWTokenVault) { deposited = true - assert.Equal(t, types.Balance(0), vault.Balance()) + assert.Equal( + t, + types.Balance(expectedBalance), + vault.Balance(), + ) }, } }, } - env := runtime.NewBaseInterpreterEnvironment(runtime.Config{}) - contractsAddress := flow.BytesToAddress([]byte{0x1}) - stdlib.SetupEnvironment(env, handler, contractsAddress) + transactionEnvironment := newEVMTransactionEnvironment(handler, contractsAddress) + scriptEnvironment := newEVMScriptEnvironment(handler, contractsAddress) rt := runtime.NewInterpreterRuntime(runtime.Config{}) @@ -758,11 +816,15 @@ func TestEVMAddressDeposit(t *testing.T) { access(all) fun main() { + let admin = getAuthAccount(0x1) + .borrow<&FlowToken.Administrator>(from: /storage/flowTokenAdmin)! + let minter <- admin.createNewMinter(allowedAmount: 1.23) + let vault <- minter.mintTokens(amount: 1.23) let address = EVM.EVMAddress( bytes: [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ) - let vault <- FlowToken.createEmptyVault() as! @FlowToken.Vault address.deposit(from: <-vault) + destroy minter } `) @@ -797,17 +859,24 @@ func TestEVMAddressDeposit(t *testing.T) { // Deploy contracts - deployContracts(t, rt, contractsAddress, runtimeInterface, env, nextTransactionLocation) + deployContracts( + t, + rt, + contractsAddress, + runtimeInterface, + transactionEnvironment, + nextTransactionLocation, + ) // Run script - _, err := rt.ExecuteScript( + _, err = rt.ExecuteScript( runtime.Script{ Source: script, }, runtime.Context{ Interface: runtimeInterface, - Environment: env, + Environment: scriptEnvironment, Location: nextScriptLocation(), }, ) @@ -841,9 +910,8 @@ func TestBridgedAccountWithdraw(t *testing.T) { }, } - env := runtime.NewBaseInterpreterEnvironment(runtime.Config{}) - - stdlib.SetupEnvironment(env, handler, contractsAddress) + transactionEnvironment := newEVMTransactionEnvironment(handler, contractsAddress) + scriptEnvironment := newEVMScriptEnvironment(handler, contractsAddress) rt := runtime.NewInterpreterRuntime(runtime.Config{}) @@ -893,7 +961,14 @@ func TestBridgedAccountWithdraw(t *testing.T) { // Deploy contracts - deployContracts(t, rt, contractsAddress, runtimeInterface, env, nextTransactionLocation) + deployContracts( + t, + rt, + contractsAddress, + runtimeInterface, + transactionEnvironment, + nextTransactionLocation, + ) // Run script @@ -903,7 +978,7 @@ func TestBridgedAccountWithdraw(t *testing.T) { }, runtime.Context{ Interface: runtimeInterface, - Environment: env, + Environment: scriptEnvironment, Location: nextScriptLocation(), }, ) @@ -946,9 +1021,8 @@ func TestBridgedAccountDeploy(t *testing.T) { }, } - env := runtime.NewBaseInterpreterEnvironment(runtime.Config{}) - - stdlib.SetupEnvironment(env, handler, contractsAddress) + transactionEnvironment := newEVMTransactionEnvironment(handler, contractsAddress) + scriptEnvironment := newEVMScriptEnvironment(handler, contractsAddress) rt := runtime.NewInterpreterRuntime(runtime.Config{}) @@ -1000,7 +1074,14 @@ func TestBridgedAccountDeploy(t *testing.T) { // Deploy contracts - deployContracts(t, rt, contractsAddress, runtimeInterface, env, nextTransactionLocation) + deployContracts( + t, + rt, + contractsAddress, + runtimeInterface, + transactionEnvironment, + nextTransactionLocation, + ) // Run script @@ -1010,7 +1091,7 @@ func TestBridgedAccountDeploy(t *testing.T) { }, runtime.Context{ Interface: runtimeInterface, - Environment: env, + Environment: scriptEnvironment, Location: nextScriptLocation(), }, ) From 7682f9c75d33b6e3398efeab5e70d743b22ad8b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Tue, 14 Nov 2023 17:27:51 -0800 Subject: [PATCH 2/2] check error --- fvm/evm/stdlib/contract_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/fvm/evm/stdlib/contract_test.go b/fvm/evm/stdlib/contract_test.go index 13b3430d0d3..996f12cd08f 100644 --- a/fvm/evm/stdlib/contract_test.go +++ b/fvm/evm/stdlib/contract_test.go @@ -778,6 +778,7 @@ func TestEVMAddressDeposit(t *testing.T) { t.Parallel() expectedBalance, err := cadence.NewUFix64FromParts(1, 23000000) + require.NoError(t, err) var deposited bool