Skip to content
This repository has been archived by the owner on Apr 11, 2021. It is now read-only.

Commit

Permalink
Extract isCallTo into its own function
Browse files Browse the repository at this point in the history
* Check that `len(args)` is greater than 4
  • Loading branch information
masonforest committed Jan 27, 2020
1 parent c52fa1a commit a61a624
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
15 changes: 11 additions & 4 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (
tt255 = math.BigPow(2, 255)
OvmSLOADMethodId = crypto.Keccak256([]byte("ovmSLOAD()"))[0:4]
OvmSSTOREMethodId = crypto.Keccak256([]byte("ovmSSTORE()"))[0:4]
OvmContractAddress = common.FromHex(os.Getenv("EXECUTION_MANAGER_ADDRESS"))
OvmContractAddress = common.HexToAddress(os.Getenv("EXECUTION_MANAGER_ADDRESS"))
errWriteProtection = errors.New("evm: write protection")
errReturnDataOutOfBounds = errors.New("evm: return data out of bounds")
errExecutionReverted = errors.New("evm: execution reverted")
Expand Down Expand Up @@ -762,16 +762,15 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory
value = math.U256(value)
// Get the arguments from the memory.
args := memory.GetPtr(inOffset.Int64(), inSize.Int64())
methodId := args[0:4]

if bytes.Equal(toAddr.Bytes(), OvmContractAddress) && bytes.Equal(methodId, OvmSLOADMethodId) {
if isCallTo(toAddr, args, OvmContractAddress, OvmSLOADMethodId) {
storageSlot := new(big.Int).SetBytes(args[4:36])
stack.push(storageSlot)
opSload(pc, interpreter, contract, memory, stack)
storageValue := stack.peek()
memory.Set(retOffset.Uint64(), retSize.Uint64(), storageValue.Bytes())
return storageValue.Bytes(), nil
} else if bytes.Equal(toAddr.Bytes(), OvmContractAddress) && bytes.Equal(methodId, OvmSSTOREMethodId) {
} else if isCallTo(toAddr, args, OvmContractAddress, OvmSSTOREMethodId) {
storageSlot := new(big.Int).SetBytes(args[4:36])
storageValue := new(big.Int).SetBytes(args[36:68])
stack.push(storageValue)
Expand Down Expand Up @@ -799,6 +798,14 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory
}
}

func isCallTo(addr common.Address, args []byte, testAddr common.Address, testMethodId []byte) bool {
if len(args) < 4 {
return false
}
methodId := args[0:4]
return addr == testAddr && bytes.Equal(methodId, testMethodId)
}

func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
interpreter.intPool.put(stack.pop())
Expand Down
6 changes: 2 additions & 4 deletions tests/ovm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tests

import (
"bytes"
"fmt"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -27,7 +26,7 @@ func mstoreBytes(bytes []byte, offset int) []byte {
return output
}

func call(addr []byte, value uint, inOffset uint, inSize uint, retOffset uint, retSize uint) []byte {
func call(addr common.Address, value uint, inOffset uint, inSize uint, retOffset uint, retSize uint) []byte {
output := []byte{
byte(vm.PUSH1), 0,
byte(vm.PUSH1), 0,
Expand All @@ -39,7 +38,7 @@ func call(addr []byte, value uint, inOffset uint, inSize uint, retOffset uint, r
}
output = append(output, []byte{
byte(vm.PUSH20)}...)
output = append(output, addr...)
output = append(output, addr.Bytes()...)
output = append(output, []byte{
byte(vm.GAS),
byte(vm.CALL),
Expand All @@ -48,7 +47,6 @@ func call(addr []byte, value uint, inOffset uint, inSize uint, retOffset uint, r
}

func TestOvm(t *testing.T) {
fmt.Printf("%x", vm.OvmSSTOREMethodId)
db := state.NewDatabase(rawdb.NewMemoryDatabase())
state, _ := state.New(common.Hash{}, db)
address := common.HexToAddress("0x0a")
Expand Down

0 comments on commit a61a624

Please sign in to comment.