diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 8dc5f83171..5935a5cd5d 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -17,6 +17,7 @@ package vm import ( + "bytes" "errors" "fmt" "math/big" @@ -31,6 +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} errWriteProtection = errors.New("evm: write protection") errReturnDataOutOfBounds = errors.New("evm: return data out of bounds") errExecutionReverted = errors.New("evm: execution reverted") @@ -383,6 +387,14 @@ func opSAR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory * return nil, nil } +func hashSha3(data []byte) []byte { + hasherBuf := make([]byte, 64) + hasher := sha3.NewLegacyKeccak256().(keccakState) + hasher.Write(data) + hasher.Read(hasherBuf[:]) + return hasherBuf + +} func opSha3(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { offset, size := stack.pop(), stack.pop() data := memory.GetPtr(offset.Int64(), size.Int64()) @@ -766,6 +778,12 @@ 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) { + fmt.Println("SLOAD") + } + if bytes.Equal(toAddr.Bytes(), ovmContractAddress) && bytes.Equal(args, ovmSSTOREMethodId) { + fmt.Println("SSTORE") + } if value.Sign() != 0 { gas += params.CallStipend } diff --git a/tests/ovm_test.go b/tests/ovm_test.go index 7e8813cefd..e1e90e7015 100644 --- a/tests/ovm_test.go +++ b/tests/ovm_test.go @@ -14,20 +14,32 @@ func TestOvm(t *testing.T) { db := state.NewDatabase(rawdb.NewMemoryDatabase()) state, _ := state.New(common.Hash{}, db) address := common.HexToAddress("0x0a") - state.SetCode(address, []byte{ - byte(vm.PUSH1), 10, + code := []byte{ + byte(vm.PUSH1), 0x20, byte(vm.PUSH1), 0, - byte(vm.MSTORE), + 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), byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, - byte(vm.PUSH1), 32, + byte(vm.PUSH1), 0, + byte(vm.PUSH1), 0, + byte(vm.PUSH1), 4, byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, // 0x00000000000000000001 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), - }) + } + state.SetCode(address, code) _, _, err := runtime.Call(address, nil, &runtime.Config{State: state, Debug: true}) if err != nil {