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

Commit

Permalink
fix: contract address in contract creation receipt (#851)
Browse files Browse the repository at this point in the history
* Problem: contract address in contract creation receipt is wrong

Closes: #850
- decrease nonce before evm.create
- add unit tests and rpc tests

* add changelog
  • Loading branch information
yihuang authored Dec 28, 2021
1 parent 8bc3cc4 commit d822fee
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (feemarket) [tharsis#822](https://github.com/tharsis/ethermint/pull/822) Update EIP1559 base fee in `BeginBlock`.
- (evm) [tharsis#817](https://github.com/tharsis/ethermint/pull/817) Use `effectiveGasPrice` in ante handler, add `effectiveGasPrice` to tx receipt.
- (evm) [tharsis#808](https://github.com/tharsis/ethermint/issues/808) increase nonce in ante handler for contract creation transaction.
- (evm) [tharsis#851](https://github.com/tharsis/ethermint/pull/851) fix contract address used in EVM, this issue is caused by [tharsis#808](https://github.com/tharsis/ethermint/issues/808).
- (evm) [tharsis#N/A]() reject invalid `MsgEthereumTx` wrapping tx
- (evm) [tharsis#N/A]() Fix SelfDestruct opcode by deleting account code and state

Expand Down
22 changes: 13 additions & 9 deletions tests/rpc/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)

const (
Expand Down Expand Up @@ -634,18 +635,21 @@ func deployTestContract(t *testing.T) (hexutil.Bytes, map[string]interface{}) {
}

func TestEth_GetTransactionReceipt_ContractDeployment(t *testing.T) {
hash, _ := deployTestContract(t)

param := []string{hash.String()}
rpcRes := call(t, "eth_getTransactionReceipt", param)
nonce := getNonce(t)
_, receipt := deployTestContract(t)

receipt := make(map[string]interface{})
err := json.Unmarshal(rpcRes.Result, &receipt)
addrBz, err := hexutil.Decode(receipt["contractAddress"].(string))
require.NoError(t, err)
require.Equal(t, "0x1", receipt["status"].(string))

require.NotEqual(t, common.Address{}.String(), receipt["contractAddress"].(string))
require.NotNil(t, receipt["logs"])
addr := common.BytesToAddress(addrBz)
require.Equal(t, crypto.CreateAddress(common.BytesToAddress(from), uint64(nonce)), addr)
require.Greater(t, len(receipt["logs"].([]interface{})), 0)

rpcRes := call(t, "eth_getCode", []string{addr.Hex(), "latest"})
var code hexutil.Bytes
err = code.UnmarshalJSON(rpcRes.Result)
require.NoError(t, err)
require.NotEmpty(t, code)
}

func getTransactionReceipt(t *testing.T, hash hexutil.Bytes) map[string]interface{} {
Expand Down
7 changes: 5 additions & 2 deletions x/evm/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,12 +665,15 @@ func (suite *EvmTestSuite) TestContractDeploymentRevert() {
)
suite.SignTx(tx)

// simulate nonce increment in ante handler
k.SetNonce(suite.from, nonce+1)

rsp, err := k.EthereumTx(sdk.WrapSDKContext(suite.ctx), tx)
suite.Require().NoError(err)
suite.Require().True(rsp.Failed())

// nonce don't increase, it's increased in ante handler.
suite.Require().Equal(nonce, k.GetNonce(suite.from))
// nonce don't change
suite.Require().Equal(nonce+1, k.GetNonce(suite.from))
})
}
}
Expand Down
9 changes: 5 additions & 4 deletions x/evm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,13 @@ func (suite *KeeperTestSuite) DeployTestContract(t require.TestingT, owner commo
ctorArgs, err := types.ERC20Contract.ABI.Pack("", owner, supply)
require.NoError(t, err)

nonce := suite.app.EvmKeeper.GetNonce(suite.address)

data := append(types.ERC20Contract.Bin, ctorArgs...)
args, err := json.Marshal(&types.TransactionArgs{
From: &suite.address,
Data: (*hexutil.Bytes)(&data),
From: &suite.address,
Data: (*hexutil.Bytes)(&data),
Nonce: (*hexutil.Uint64)(&nonce),
})
require.NoError(t, err)

Expand All @@ -221,8 +224,6 @@ func (suite *KeeperTestSuite) DeployTestContract(t require.TestingT, owner commo
})
require.NoError(t, err)

nonce := suite.app.EvmKeeper.GetNonce(suite.address)

var erc20DeployTx *types.MsgEthereumTx
if suite.dynamicTxFee {
erc20DeployTx = types.NewTxContract(
Expand Down
8 changes: 5 additions & 3 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,12 @@ func (k *Keeper) ApplyMessageWithConfig(msg core.Message, tracer vm.Tracer, comm
}

if contractCreation {
nonce := k.GetNonce(sender.Address())
// take over the nonce management from evm:
// - reset sender's nonce to msg.Nonce() before calling evm.
// - increase sender's nonce by one no matter the result.
k.SetNonce(sender.Address(), msg.Nonce())
ret, _, leftoverGas, vmErr = evm.Create(sender, msg.Data(), leftoverGas, msg.Value())
// revert nonce increment, because it's increased in ante handler
k.SetNonce(sender.Address(), nonce)
k.SetNonce(sender.Address(), msg.Nonce()+1)
} else {
ret, leftoverGas, vmErr = evm.Call(sender, *msg.To(), msg.Data(), leftoverGas, msg.Value())
}
Expand Down
6 changes: 6 additions & 0 deletions x/evm/keeper/state_transition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,9 @@ func (suite *KeeperTestSuite) TestEVMConfig() {
suite.Require().Equal(suite.address, cfg.CoinBase)
suite.Require().Equal(types.DefaultParams().ChainConfig.EthereumConfig(big.NewInt(9000)), cfg.ChainConfig)
}

func (suite *KeeperTestSuite) TestContractDeployment() {
suite.SetupTest()
contractAddress := suite.DeployTestContract(suite.T(), suite.address, big.NewInt(10000000000000))
suite.Require().Greater(suite.app.EvmKeeper.GetCodeSize(contractAddress), 0)
}
8 changes: 7 additions & 1 deletion x/evm/types/tx_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,13 @@ func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (e
if args.AccessList != nil {
accessList = *args.AccessList
}
msg := ethtypes.NewMessage(addr, args.To, 0, value, gas, gasPrice, gasFeeCap, gasTipCap, data, accessList, true)

nonce := uint64(0)
if args.Nonce != nil {
nonce = uint64(*args.Nonce)
}

msg := ethtypes.NewMessage(addr, args.To, nonce, value, gas, gasPrice, gasFeeCap, gasTipCap, data, accessList, true)
return msg, nil
}

Expand Down

0 comments on commit d822fee

Please sign in to comment.