From 30b157a126a27bda91c915c9541c3b50141ec60a Mon Sep 17 00:00:00 2001 From: Mason Fischer Date: Wed, 22 Jan 2020 16:30:47 -0500 Subject: [PATCH] Complete an ovmSSTORE and ovmSLOAD roundtrip test --- core/vm/instructions.go | 50 ++++++++++++++++++----------------------- tests/ovm_test.go | 27 ++++++++++++++++++++-- 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 243ff9a484..956ecbd89c 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -19,7 +19,6 @@ package vm import ( "bytes" "errors" - "fmt" "math/big" "github.com/ethereum/go-ethereum/common" @@ -768,43 +767,38 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory toAddr := common.BigToAddress(addr) value = math.U256(value) - fmt.Printf("value %s\n", value) - fmt.Printf("inOffset %s\n", inOffset) - fmt.Printf("inSize %s\n", inSize) - fmt.Printf("retOffset %s\n", retOffset) - fmt.Printf("retSize %s\n", retSize) - fmt.Printf("address 0x%020x\n", addr) // Get the arguments from the memory. args := memory.GetPtr(inOffset.Int64(), inSize.Int64()) - fmt.Printf("args 0%x\n", args) if bytes.Equal(toAddr.Bytes(), OvmContractAddress) && bytes.Equal(args[0:4], OvmSLOADMethodId) { - fmt.Println("SLOAD") - } - if bytes.Equal(toAddr.Bytes(), OvmContractAddress) && bytes.Equal(args[0:4], OvmSSTOREMethodId) { - fmt.Printf("SSTORE %v %v\n", args[4:35], args[36:68]) + loc := common.BytesToHash(args[4:35]) + val := interpreter.evm.StateDB.GetState(contract.Address(), loc) + stack.push(val.Big()) + return val.Bytes(), nil + } else if bytes.Equal(toAddr.Bytes(), OvmContractAddress) && bytes.Equal(args[0:4], OvmSSTOREMethodId) { loc := common.BytesToHash(args[4:35]) val := common.BytesToHash(args[36:68]) interpreter.evm.StateDB.SetState(contract.Address(), loc, val) - interpreter.intPool.put(val.Big()) - } - if value.Sign() != 0 { - gas += params.CallStipend - } - ret, returnGas, err := interpreter.evm.Call(contract, toAddr, args, gas, value) - if err != nil { - stack.push(interpreter.intPool.getZero()) + return nil, nil } else { - stack.push(interpreter.intPool.get().SetUint64(1)) - } - if err == nil || err == errExecutionReverted { - memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) - } - contract.Gas += returnGas + if value.Sign() != 0 { + gas += params.CallStipend + } + ret, returnGas, err := interpreter.evm.Call(contract, toAddr, args, gas, value) + if err != nil { + stack.push(interpreter.intPool.getZero()) + } else { + stack.push(interpreter.intPool.get().SetUint64(1)) + } + if err == nil || err == errExecutionReverted { + memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) + } + contract.Gas += returnGas - interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize) - return ret, nil + interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize) + return ret, nil + } } func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { diff --git a/tests/ovm_test.go b/tests/ovm_test.go index 1d8ec9ebbe..df3bdfde37 100644 --- a/tests/ovm_test.go +++ b/tests/ovm_test.go @@ -1,6 +1,7 @@ package tests import ( + "bytes" "testing" "github.com/ethereum/go-ethereum/common" @@ -11,7 +12,7 @@ import ( ) var KEY = common.FromHex("0102030000000000000000000000000000000000000000000000000000000000") -var VALUE = common.FromHex("0405060000000000000000000000000000000000000000000000000000000001") +var VALUE = common.FromHex("0405060000000000000000000000000000000000000000000000000000000000") func mstoreBytes(bytes []byte, offset int) []byte { output := make([]byte, len(bytes)*5) @@ -60,11 +61,33 @@ func TestOvm(t *testing.T) { 68, 0, 0)...) + code = append(code, mstoreBytes(vm.OvmSLOADMethodId, 0)...) + code = append(code, mstoreBytes(KEY, 4)...) + code = append(code, + call( + vm.OvmContractAddress, + 0, + 0, + 36, + 0, + 0)...) + code = append(code, []byte{ + byte(vm.PUSH1), 0, + byte(vm.MSTORE), + byte(vm.PUSH1), 32, + byte(vm.PUSH1), 0, + byte(vm.RETURN), + }...) state.SetCode(address, code) - _, _, err := runtime.Call(address, nil, &runtime.Config{State: state, Debug: true}) + returnValue, _, err := runtime.Call(address, nil, &runtime.Config{State: state, Debug: true}) if err != nil { t.Fatal("didn't expect error", err) } + + if !bytes.Equal(returnValue, VALUE) { + t.Errorf("Expected %020x; got %020x", returnValue, VALUE) + } + }