fix: contract address in contract creation receipt (#851)

* Problem: contract address in contract creation receipt is wrong

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

* add changelog
This commit is contained in:
yihuang
2021-12-28 08:55:40 +01:00
committed by GitHub
parent 8bc3cc471b
commit d822fee5c1
7 changed files with 42 additions and 19 deletions
+5 -2
View File
@@ -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))
})
}
}
+5 -4
View File
@@ -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)
@@ -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(
+5 -3
View File
@@ -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())
}
+6
View File
@@ -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)
}
+7 -1
View File
@@ -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
}