From b7b78c00586e5564e8927c43d8bd6a56d66b43d8 Mon Sep 17 00:00:00 2001 From: Yondon Fu Date: Tue, 9 Jan 2018 12:42:33 -0500 Subject: [PATCH] internal/ethapi: add eth_nextNonce RPC method --- eth/bind.go | 2 +- ethclient/ethclient.go | 2 +- internal/ethapi/api.go | 11 +++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/eth/bind.go b/eth/bind.go index 769a6c741c..6e044716d6 100644 --- a/eth/bind.go +++ b/eth/bind.go @@ -104,7 +104,7 @@ func toBlockNumber(num *big.Int) rpc.BlockNumber { // PendingAccountNonce implements bind.ContractTransactor retrieving the current // pending nonce associated with an account. func (b *ContractBackend) PendingNonceAt(ctx context.Context, account common.Address) (nonce uint64, err error) { - out, err := b.txapi.GetTransactionCount(ctx, account, rpc.PendingBlockNumber) + out, err := b.txapi.NextNonce(ctx, account) if out != nil { nonce = uint64(*out) } diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 87a912901a..75db592b2c 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -400,7 +400,7 @@ func (ec *Client) PendingCodeAt(ctx context.Context, account common.Address) ([] // This is the nonce that should be used for the next transaction. func (ec *Client) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) { var result hexutil.Uint64 - err := ec.c.CallContext(ctx, &result, "eth_getTransactionCount", account, "pending") + err := ec.c.CallContext(ctx, &result, "eth_nextNonce", account) return uint64(result), err } diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 55bd5aa1ba..575528ec25 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -971,6 +971,17 @@ func (s *PublicTransactionPoolAPI) GetTransactionCount(ctx context.Context, addr return (*hexutil.Uint64)(&nonce), state.Error() } +// NextNonce returns the next nonce for the given address taking into account any pending transactions the address has +func (s *PublicTransactionPoolAPI) NextNonce(ctx context.Context, address common.Address) (*hexutil.Uint64, error) { + // Ask transaction pool for the nonce which includes pending transactions + nonce, err := s.b.GetPoolNonce(ctx, address) + if err != nil { + return nil, err + } + + return (*hexutil.Uint64)(&nonce), nil +} + // GetTransactionByHash returns the transaction for the given hash func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, hash common.Hash) *RPCTransaction { // Try to return an already finalized transaction