From d0b7a97e4da2271c18253f997e634ef8efac856c Mon Sep 17 00:00:00 2001 From: Mason Fischer Date: Wed, 13 May 2020 17:25:27 -0400 Subject: [PATCH] Add GetCodeContractHash --- core/vm/state_manager.go | 7 +++++++ tests/ovm_test.go | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/core/vm/state_manager.go b/core/vm/state_manager.go index ee6d7e680a..aa2b40200a 100644 --- a/core/vm/state_manager.go +++ b/core/vm/state_manager.go @@ -21,6 +21,7 @@ var funcs = map[string]stateManagerFunction{ "deployContract(address,bytes,bool,address)": deployContract, "getOvmContractNonce(address)": getOvmContractNonce, "getCodeContractBytecode(address)": getCodeContractBytecode, + "getCodeContractHash(address)": getCodeContractHash, "incrementOvmContractNonce(address)": incrementOvmContractNonce, } var methodIds map[[4]byte]stateManagerFunction @@ -67,6 +68,12 @@ func getCodeContractBytecode(evm *EVM, contract *Contract, input []byte) (ret [] return code, nil } +func getCodeContractHash(evm *EVM, contract *Contract, input []byte) (ret []byte, err error) { + address := common.BytesToAddress(input[4:36]) + codeHash := evm.StateDB.GetCodeHash(address) + return codeHash.Bytes(), nil +} + func getOvmContractNonce(evm *EVM, contract *Contract, input []byte) (ret []byte, err error) { address := common.BytesToAddress(input[4:36]) b := make([]byte, 8) diff --git a/tests/ovm_test.go b/tests/ovm_test.go index f3ed05029b..9100851f0a 100644 --- a/tests/ovm_test.go +++ b/tests/ovm_test.go @@ -15,6 +15,7 @@ import ( "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm/runtime" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/params" ) @@ -115,6 +116,23 @@ func TestGetCodeContractBytecode(t *testing.T) { } } +func TestGetCodeContractHash(t *testing.T) { + state := newState() + initCode, _ := hex.DecodeString("6080604052348015600f57600080fd5b5060b28061001e6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80639b0b0fda14602d575b600080fd5b606060048036036040811015604157600080fd5b8101908080359060200190929190803590602001909291905050506062565b005b8060008084815260200190815260200160002081905550505056fea265627a7a7231582053ac32a8b70d1cf87fb4ebf5a538ea9d9e773351e6c8afbc4bf6a6c273187f4a64736f6c63430005110032") + rawStateManagerAbi, _ := ioutil.ReadFile("./StateManagerABI.json") + stateManagerAbi, _ := abi.JSON(strings.NewReader(string(rawStateManagerAbi))) + address := common.HexToAddress("9999999999999999999999999999999999999999") + callerAddress := common.HexToAddress("42") + deployContractCalldata, _ := stateManagerAbi.Pack("deployContract", address, initCode, true, callerAddress) + call(t, state, vm.StateManagerAddress, deployContractCalldata) + getCodeContractBytecodeCalldata, _ := stateManagerAbi.Pack("getCodeContractHash", address) + getCodeContractBytecodeReturnValue, _ := call(t, state, vm.StateManagerAddress, getCodeContractBytecodeCalldata) + expectedCreatedCodeHash := crypto.Keccak256(common.FromHex("6080604052348015600f57600080fd5b506004361060285760003560e01c80639b0b0fda14602d575b600080fd5b606060048036036040811015604157600080fd5b8101908080359060200190929190803590602001909291905050506062565b005b8060008084815260200190815260200160002081905550505056fea265627a7a7231582053ac32a8b70d1cf87fb4ebf5a538ea9d9e773351e6c8afbc4bf6a6c273187f4a64736f6c63430005110032")) + if !bytes.Equal(getCodeContractBytecodeReturnValue, expectedCreatedCodeHash) { + t.Errorf("Expected %020x; got %020x", getCodeContractBytecodeReturnValue, expectedCreatedCodeHash) + } +} + func makeUint256WithUint64(num uint64) []byte { b := make([]byte, 8) binary.BigEndian.PutUint64(b, num)