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 (
"bytes"
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
@ -768,27 +767,21 @@ 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())
}
return nil, nil
} else {
if value.Sign() != 0 {
gas += params.CallStipend
}
@ -806,6 +799,7 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory
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) {
// Pop gas. The actual gas is in interpreter.evm.callGasTemp.

View file

@ -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)
}
}