Add GetCodeContractHash

This commit is contained in:
Mason Fischer 2020-05-13 17:25:27 -04:00
parent cf7efffe92
commit d0b7a97e4d
2 changed files with 25 additions and 0 deletions

View file

@ -21,6 +21,7 @@ var funcs = map[string]stateManagerFunction{
"deployContract(address,bytes,bool,address)": deployContract, "deployContract(address,bytes,bool,address)": deployContract,
"getOvmContractNonce(address)": getOvmContractNonce, "getOvmContractNonce(address)": getOvmContractNonce,
"getCodeContractBytecode(address)": getCodeContractBytecode, "getCodeContractBytecode(address)": getCodeContractBytecode,
"getCodeContractHash(address)": getCodeContractHash,
"incrementOvmContractNonce(address)": incrementOvmContractNonce, "incrementOvmContractNonce(address)": incrementOvmContractNonce,
} }
var methodIds map[[4]byte]stateManagerFunction var methodIds map[[4]byte]stateManagerFunction
@ -67,6 +68,12 @@ func getCodeContractBytecode(evm *EVM, contract *Contract, input []byte) (ret []
return code, nil 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) { func getOvmContractNonce(evm *EVM, contract *Contract, input []byte) (ret []byte, err error) {
address := common.BytesToAddress(input[4:36]) address := common.BytesToAddress(input[4:36])
b := make([]byte, 8) b := make([]byte, 8)

View file

@ -15,6 +15,7 @@ import (
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/core/vm/runtime" "github.com/ethereum/go-ethereum/core/vm/runtime"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params" "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 { func makeUint256WithUint64(num uint64) []byte {
b := make([]byte, 8) b := make([]byte, 8)
binary.BigEndian.PutUint64(b, num) binary.BigEndian.PutUint64(b, num)