From e9c494cf71a18c3e241c3f6ff502b76d01a5c21d Mon Sep 17 00:00:00 2001 From: noot <36753753+noot@users.noreply.github.com> Date: Fri, 26 Jun 2020 14:15:54 -0400 Subject: [PATCH] rpc: return 0 nonce if account doesn't exist (#345) * add EnsureExists check to GetTransactionCount * cleanup, return 0 as nonce if account doesn't exist * update changelog * update changelog --- CHANGELOG.md | 3 ++- rpc/eth_api.go | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d091783f8..e6af17504 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* (rpc) [\#305](https://github.com/ChainSafe/ethermint/issues/305) Update eth_getTransactionCount to check for account existence before getting sequence and return 0 as the nonce if it doesn't exist. * (`x/evm`) [\#319](https://github.com/ChainSafe/ethermint/pull/319) Fix `SetBlockHash` that was setting the incorrect height during `BeginBlock`. * (x/evm) [\#176](https://github.com/ChainSafe/ethermint/issues/176) Updated Web3 transaction hash from using RLP hash. Now all transaction hashes exposed are amino hashes. - * Removes `Hash()` (RLP) function from `MsgEthereumTx` to avoid confusion or misuse in future. + * Removes `Hash()` (RLP) function from `MsgEthereumTx` to avoid confusion or misuse in future. \ No newline at end of file diff --git a/rpc/eth_api.go b/rpc/eth_api.go index 3266cc07a..62073cdb3 100644 --- a/rpc/eth_api.go +++ b/rpc/eth_api.go @@ -197,6 +197,13 @@ func (e *PublicEthAPI) GetTransactionCount(address common.Address, blockNum Bloc authclient.Codec = codec.NewAppCodec(ctx.Codec) accRet := authtypes.NewAccountRetriever(authclient.Codec, ctx) + err := accRet.EnsureExists(from) + if err != nil { + // account doesn't exist yet, return 0 + n := hexutil.Uint64(0) + return &n, nil + } + _, nonce, err := accRet.GetAccountNumberSequence(from) if err != nil { return nil, err @@ -415,7 +422,6 @@ type account struct { func (e *PublicEthAPI) doCall( args CallArgs, blockNr rpc.BlockNumber, globalGasCap *big.Int, ) (*sdk.SimulationResponse, error) { - // Set height for historical queries ctx := e.cliCtx @@ -900,6 +906,12 @@ func (e *PublicEthAPI) generateFromArgs(args params.SendTxArgs) (*types.MsgEther authclient.Codec = codec.NewAppCodec(e.cliCtx.Codec) accRet := authtypes.NewAccountRetriever(authclient.Codec, e.cliCtx) + err = accRet.EnsureExists(from) + if err != nil { + // account doesn't exist + return nil, fmt.Errorf("nonexistent account %s: %s", args.From.Hex(), err) + } + _, nonce, err = accRet.GetAccountNumberSequence(from) if err != nil { return nil, err