Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

fix: reverse order of contracts creation in Create #63

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions fhevm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,26 +191,36 @@ func padArrayTo32Multiple(input []byte) []byte {
func Create(evm EVMEnvironment, caller common.Address, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
contractAddr = crypto.CreateAddress(caller, evm.GetNonce(caller))
protectedStorageAddr := fhevm_crypto.CreateProtectedStorageContractAddress(contractAddr)
ret, contractAddr, leftOverGas, err = evm.CreateContract(caller, code, leftOverGas, value, contractAddr)
if err != nil {
ret = nil
contractAddr = common.Address{}
return
}
_, _, leftOverGas, err = evm.CreateContract(caller, nil, gas, big.NewInt(0), protectedStorageAddr)
if err != nil {
ret = nil
contractAddr = common.Address{}
return
}
// TODO: consider reverting changes to `protectedStorageAddr` if actual contract creation fails.
return evm.CreateContract(caller, code, leftOverGas, value, contractAddr)
return
}

func Create2(evm EVMEnvironment, caller common.Address, code []byte, gas uint64, endowment *big.Int, salt *uint256.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
codeHash := crypto.Keccak256Hash(code)
contractAddr = crypto.CreateAddress2(caller, salt.Bytes32(), codeHash.Bytes())
protectedStorageAddr := fhevm_crypto.CreateProtectedStorageContractAddress(contractAddr)
_, _, leftOverGas, err = evm.CreateContract2(caller, nil, common.Hash{}, gas, big.NewInt(0), protectedStorageAddr)
ret, contractAddr, leftOverGas, err = evm.CreateContract2(caller, code, codeHash, gas, endowment, contractAddr)
if err != nil {
ret = nil
contractAddr = common.Address{}
return
}
// TODO: consider reverting changes to `protectedStorageAddr` if actual contract creation fails.
return evm.CreateContract2(caller, code, codeHash, leftOverGas, endowment, contractAddr)
_, _, leftOverGas, err = evm.CreateContract2(caller, nil, common.Hash{}, leftOverGas, big.NewInt(0), protectedStorageAddr)
if err != nil {
ret = nil
contractAddr = common.Address{}
return
}
return
}