From 608d3355d50500ff6b04b6d995fc49e966cceced Mon Sep 17 00:00:00 2001 From: Mason Fischer Date: Wed, 22 Jan 2020 14:55:39 -0500 Subject: [PATCH] Add mstoreBytes --- core/vm/instructions.go | 10 +++++----- tests/ovm_test.go | 33 +++++++++++++++++++-------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 5935a5cd5d..61c719f617 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -32,9 +32,9 @@ import ( var ( bigZero = new(big.Int) tt255 = math.BigPow(2, 255) - ovmSLOADMethodId = hashSha3([]byte("ovmSLOAD()"))[0:4] - ovmSSTOREMethodId = hashSha3([]byte("ovmSStore()"))[0:4] - ovmContractAddress = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} + OvmSLOADMethodId = hashSha3([]byte("ovmSLOAD()"))[0:4] + OvmSSTOREMethodId = hashSha3([]byte("ovmSStore()"))[0:4] + OvmContractAddress = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} errWriteProtection = errors.New("evm: write protection") errReturnDataOutOfBounds = errors.New("evm: return data out of bounds") errExecutionReverted = errors.New("evm: execution reverted") @@ -778,10 +778,10 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory args := memory.GetPtr(inOffset.Int64(), inSize.Int64()) fmt.Printf("args 0%x\n", args) - if bytes.Equal(toAddr.Bytes(), ovmContractAddress) && bytes.Equal(args, ovmSLOADMethodId) { + if bytes.Equal(toAddr.Bytes(), OvmContractAddress) && bytes.Equal(args, OvmSLOADMethodId) { fmt.Println("SLOAD") } - if bytes.Equal(toAddr.Bytes(), ovmContractAddress) && bytes.Equal(args, ovmSSTOREMethodId) { + if bytes.Equal(toAddr.Bytes(), OvmContractAddress) && bytes.Equal(args, OvmSSTOREMethodId) { fmt.Println("SSTORE") } if value.Sign() != 0 { diff --git a/tests/ovm_test.go b/tests/ovm_test.go index e1e90e7015..9b7d9e8576 100644 --- a/tests/ovm_test.go +++ b/tests/ovm_test.go @@ -1,6 +1,7 @@ package tests import ( + "fmt" "testing" "github.com/ethereum/go-ethereum/common" @@ -10,23 +11,25 @@ import ( "github.com/ethereum/go-ethereum/core/vm/runtime" ) +func mstoreBytes(bytes []byte) []byte { + output := make([]byte, len(bytes)*5) + for i, b := range bytes { + output[i*5] = byte(vm.PUSH1) + output[i*5+1] = b + output[i*5+2] = byte(vm.PUSH1) + output[i*5+3] = byte(i) + output[i*5+4] = byte(vm.MSTORE8) + } + return output +} + func TestOvm(t *testing.T) { db := state.NewDatabase(rawdb.NewMemoryDatabase()) state, _ := state.New(common.Hash{}, db) address := common.HexToAddress("0x0a") - code := []byte{ - byte(vm.PUSH1), 0x20, - byte(vm.PUSH1), 0, - byte(vm.MSTORE8), - byte(vm.PUSH1), 0x96, - byte(vm.PUSH1), 1, - byte(vm.MSTORE8), - byte(vm.PUSH1), 0x62, - byte(vm.PUSH1), 2, - byte(vm.MSTORE8), - byte(vm.PUSH1), 0x08, - byte(vm.PUSH1), 3, - byte(vm.MSTORE8), + fmt.Printf("%x\n", mstoreBytes(vm.OvmSLOADMethodId)) + code := mstoreBytes(vm.OvmSLOADMethodId) + code = append(code, []byte{ byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, @@ -38,7 +41,9 @@ func TestOvm(t *testing.T) { byte(vm.PUSH20), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, byte(vm.GAS), byte(vm.CALL), - } + }...) + + fmt.Printf("%x", code) state.SetCode(address, code) _, _, err := runtime.Call(address, nil, &runtime.Config{State: state, Debug: true})