get account sequence in GetTransactionCount (#303)
* get account sequence if querying latest in GetTransactionCount * use ctx WithHeight in GetTransactionCount; remove nonce query from querier
This commit is contained in:
parent
1f63ddfe96
commit
846f48a572
@ -181,14 +181,19 @@ func (e *PublicEthAPI) GetStorageAt(address common.Address, key string, blockNum
|
||||
// GetTransactionCount returns the number of transactions at the given address up to the given block number.
|
||||
func (e *PublicEthAPI) GetTransactionCount(address common.Address, blockNum BlockNumber) (*hexutil.Uint64, error) {
|
||||
ctx := e.cliCtx.WithHeight(blockNum.Int64())
|
||||
res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/nonce/%s", types.ModuleName, address.Hex()), nil)
|
||||
|
||||
// Get nonce (sequence) from account
|
||||
from := sdk.AccAddress(address.Bytes())
|
||||
authclient.Codec = codec.NewAppCodec(ctx.Codec)
|
||||
accRet := authtypes.NewAccountRetriever(authclient.Codec, ctx)
|
||||
|
||||
_, nonce, err := accRet.GetAccountNumberSequence(from)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var out types.QueryResNonce
|
||||
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
|
||||
return (*hexutil.Uint64)(&out.Nonce), nil
|
||||
n := hexutil.Uint64(nonce)
|
||||
return &n, nil
|
||||
}
|
||||
|
||||
// GetBlockTransactionCountByHash returns the number of transactions in the block identified by hash.
|
||||
|
@ -637,3 +637,21 @@ func TestBlockBloom_Hash(t *testing.T) {
|
||||
lb := hexToBigInt(t, block["logsBloom"].(string))
|
||||
require.NotEqual(t, big.NewInt(0), lb)
|
||||
}
|
||||
|
||||
func getNonce(t *testing.T) hexutil.Uint64 {
|
||||
from := getAddress(t)
|
||||
param := []interface{}{hexutil.Bytes(from), "latest"}
|
||||
rpcRes := call(t, "eth_getTransactionCount", param)
|
||||
|
||||
var nonce hexutil.Uint64
|
||||
err := json.Unmarshal(rpcRes.Result, &nonce)
|
||||
require.NoError(t, err)
|
||||
return nonce
|
||||
}
|
||||
|
||||
func TestEth_GetTransactionCount(t *testing.T) {
|
||||
prev := getNonce(t)
|
||||
sendTestTransaction(t)
|
||||
post := getNonce(t)
|
||||
require.Equal(t, prev, post-1)
|
||||
}
|
||||
|
@ -32,8 +32,6 @@ func NewQuerier(keeper Keeper) sdk.Querier {
|
||||
return queryStorage(ctx, path, keeper)
|
||||
case types.QueryCode:
|
||||
return queryCode(ctx, path, keeper)
|
||||
case types.QueryNonce:
|
||||
return queryNonce(ctx, path, keeper)
|
||||
case types.QueryHashToHeight:
|
||||
return queryHashToHeight(ctx, path, keeper)
|
||||
case types.QueryTransactionLogs:
|
||||
@ -113,18 +111,6 @@ func queryCode(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
|
||||
return bz, nil
|
||||
}
|
||||
|
||||
func queryNonce(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
|
||||
addr := ethcmn.HexToAddress(path[1])
|
||||
nonce := keeper.GetNonce(ctx, addr)
|
||||
nRes := types.QueryResNonce{Nonce: nonce}
|
||||
bz, err := codec.MarshalJSONIndent(keeper.cdc, nRes)
|
||||
if err != nil {
|
||||
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
||||
}
|
||||
|
||||
return bz, nil
|
||||
}
|
||||
|
||||
func queryHashToHeight(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
|
||||
blockHash := ethcmn.FromHex(path[1])
|
||||
blockNumber, err := keeper.GetBlockHashMapping(ctx, blockHash)
|
||||
|
@ -24,7 +24,6 @@ func (suite *KeeperTestSuite) TestQuerier() {
|
||||
{"block number", []string{types.QueryBlockNumber, "0x0"}, func() {}, true},
|
||||
{"storage", []string{types.QueryStorage, "0x0", "0x0"}, func() {}, true},
|
||||
{"code", []string{types.QueryCode, "0x0"}, func() {}, true},
|
||||
{"nonce", []string{types.QueryNonce, "0x0"}, func() {}, true},
|
||||
// {"hash to height", []string{types.QueryHashToHeight, "0x0"}, func() {}, true},
|
||||
{"tx logs", []string{types.QueryTransactionLogs, "0x0"}, func() {}, true},
|
||||
// {"logs bloom", []string{types.QueryLogsBloom, "0x0"}, func() {}, true},
|
||||
|
Loading…
Reference in New Issue
Block a user