Complete an ovmSSTORE and ovmSLOAD roundtrip test

This commit is contained in:
Mason Fischer 2020-01-22 16:30:47 -05:00
parent ccb684b93f
commit 30b157a126
2 changed files with 47 additions and 30 deletions

View file

@ -19,7 +19,6 @@ package vm
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -768,43 +767,38 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory
toAddr := common.BigToAddress(addr) toAddr := common.BigToAddress(addr)
value = math.U256(value) 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. // Get the arguments from the memory.
args := memory.GetPtr(inOffset.Int64(), inSize.Int64()) 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) { if bytes.Equal(toAddr.Bytes(), OvmContractAddress) && bytes.Equal(args[0:4], OvmSLOADMethodId) {
fmt.Println("SLOAD") loc := common.BytesToHash(args[4:35])
} val := interpreter.evm.StateDB.GetState(contract.Address(), loc)
if bytes.Equal(toAddr.Bytes(), OvmContractAddress) && bytes.Equal(args[0:4], OvmSSTOREMethodId) { stack.push(val.Big())
fmt.Printf("SSTORE %v %v\n", args[4:35], args[36:68]) return val.Bytes(), nil
} else if bytes.Equal(toAddr.Bytes(), OvmContractAddress) && bytes.Equal(args[0:4], OvmSSTOREMethodId) {
loc := common.BytesToHash(args[4:35]) loc := common.BytesToHash(args[4:35])
val := common.BytesToHash(args[36:68]) val := common.BytesToHash(args[36:68])
interpreter.evm.StateDB.SetState(contract.Address(), loc, val) interpreter.evm.StateDB.SetState(contract.Address(), loc, val)
interpreter.intPool.put(val.Big()) interpreter.intPool.put(val.Big())
} return nil, nil
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 { } else {
stack.push(interpreter.intPool.get().SetUint64(1)) if value.Sign() != 0 {
} gas += params.CallStipend
if err == nil || err == errExecutionReverted { }
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) ret, returnGas, err := interpreter.evm.Call(contract, toAddr, args, gas, value)
} if err != nil {
contract.Gas += returnGas 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) interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
return ret, nil return ret, nil
}
} }
func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {

View file

@ -1,6 +1,7 @@
package tests package tests
import ( import (
"bytes"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -11,7 +12,7 @@ import (
) )
var KEY = common.FromHex("0102030000000000000000000000000000000000000000000000000000000000") var KEY = common.FromHex("0102030000000000000000000000000000000000000000000000000000000000")
var VALUE = common.FromHex("0405060000000000000000000000000000000000000000000000000000000001") var VALUE = common.FromHex("0405060000000000000000000000000000000000000000000000000000000000")
func mstoreBytes(bytes []byte, offset int) []byte { func mstoreBytes(bytes []byte, offset int) []byte {
output := make([]byte, len(bytes)*5) output := make([]byte, len(bytes)*5)
@ -60,11 +61,33 @@ func TestOvm(t *testing.T) {
68, 68,
0, 0,
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) 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 { if err != nil {
t.Fatal("didn't expect error", err) t.Fatal("didn't expect error", err)
} }
if !bytes.Equal(returnValue, VALUE) {
t.Errorf("Expected %020x; got %020x", returnValue, VALUE)
}
} }