Skip to content

Commit

Permalink
Merge PR: optimize pending pool nonce validation (#1968)
Browse files Browse the repository at this point in the history
* optimize pending pool nonce validation

* fix zero nonce

Co-authored-by: xiangjianmeng <[email protected]>

Co-authored-by: xiangjianmeng <[email protected]>
  • Loading branch information
ilovers and xiangjianmeng authored Apr 29, 2022
1 parent 5244c0e commit 9fd260d
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 25 deletions.
12 changes: 6 additions & 6 deletions app/rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Backend interface {
UserPendingTransactionsCnt(address string) (int, error)
UserPendingTransactions(address string, limit int) ([]*watcher.Transaction, error)
PendingAddressList() ([]string, error)
GetPendingNonce(address string) (uint64, error)
GetPendingNonce(address string) (uint64, bool)

// Used by log filter
GetTransactionLogs(txHash common.Hash) ([]*ethtypes.Log, error)
Expand Down Expand Up @@ -311,12 +311,12 @@ func (b *EthermintBackend) UserPendingTransactionsCnt(address string) (int, erro
return result.Count, nil
}

func (b *EthermintBackend) GetPendingNonce(address string) (uint64, error) {
result, err := b.clientCtx.Client.GetPendingNonce(address)
if err != nil {
return 0, err
func (b *EthermintBackend) GetPendingNonce(address string) (uint64, bool) {
result, ok := b.clientCtx.Client.GetPendingNonce(address)
if !ok {
return 0, false
}
return result.Nonce, nil
return result.Nonce, true
}

func (b *EthermintBackend) UserPendingTransactions(address string, limit int) ([]*watcher.Transaction, error) {
Expand Down
4 changes: 2 additions & 2 deletions app/rpc/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1643,8 +1643,8 @@ func (api *PublicEthereumAPI) accountNonce(
) (uint64, error) {
if pending {
// nonce is continuous in mempool txs
pendingNonce, err := api.backend.GetPendingNonce(address.String())
if err == nil && pendingNonce > 0 {
pendingNonce, ok := api.backend.GetPendingNonce(address.String())
if ok {
return pendingNonce + 1, nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion libs/tendermint/lite2/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (c *Client) GetAddressList() (*ctypes.ResultUnconfirmedAddresses, error) {
return c.next.GetAddressList()
}

func (c *Client) GetPendingNonce(address string) (*ctypes.ResultPendingNonce, error) {
func (c *Client) GetPendingNonce(address string) (*ctypes.ResultPendingNonce, bool) {
return c.next.GetPendingNonce(address)
}

Expand Down
6 changes: 3 additions & 3 deletions libs/tendermint/mempool/address_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ func (ar *AddressRecord) GetAddressTxsCnt(address string) int {
return len(am.items)
}

func (ar *AddressRecord) GetAddressNonce(address string) uint64 {
func (ar *AddressRecord) GetAddressNonce(address string) (uint64, bool) {
v, ok := ar.addrTxs.Load(address)
if !ok {
return 0
return 0, false
}
am := v.(*addrMap)
am.RLock()
Expand All @@ -171,7 +171,7 @@ func (ar *AddressRecord) GetAddressNonce(address string) uint64 {
nonce = e.Nonce
}
}
return nonce
return nonce, true
}

func (ar *AddressRecord) GetAddressTxs(address string, txCount int, max int) types.Txs {
Expand Down
10 changes: 7 additions & 3 deletions libs/tendermint/mempool/clist_mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,12 @@ func (mem *CListMempool) isFull(txSize int) error {

func (mem *CListMempool) addPendingTx(memTx *mempoolTx) error {
// nonce is continuous
pendingCnt := mem.GetUserPendingTxsCnt(memTx.from)
if memTx.realTx.GetNonce() == memTx.senderNonce+uint64(pendingCnt) {
expectedNonce := memTx.senderNonce
pendingNonce, ok := mem.GetPendingNonce(memTx.from)
if ok {
expectedNonce = pendingNonce + 1
}
if memTx.realTx.GetNonce() == expectedNonce {
err := mem.addTx(memTx)
if err == nil {
go mem.consumePendingTx(memTx.from, memTx.realTx.GetNonce()+1)
Expand Down Expand Up @@ -827,7 +831,7 @@ func (mem *CListMempool) GetAddressList() []string {
return mem.addressRecord.GetAddressList()
}

func (mem *CListMempool) GetPendingNonce(address string) uint64 {
func (mem *CListMempool) GetPendingNonce(address string) (uint64, bool) {
return mem.addressRecord.GetAddressNonce(address)
}

Expand Down
2 changes: 1 addition & 1 deletion libs/tendermint/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Mempool interface {
GetUserPendingTxsCnt(address string) int

ReapUserTxs(address string, max int) types.Txs
GetPendingNonce(address string) uint64
GetPendingNonce(address string) (uint64, bool)

// Lock locks the mempool. The consensus must be able to hold lock to safely update.
Lock()
Expand Down
2 changes: 1 addition & 1 deletion libs/tendermint/mock/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (Mempool) ReapMaxTxs(n int) types.Txs { return types.Txs
func (Mempool) ReapUserTxsCnt(address string) int { return 0 }
func (Mempool) GetUserPendingTxsCnt(address string) int { return 0 }
func (Mempool) ReapUserTxs(address string, max int) types.Txs { return types.Txs{} }
func (Mempool) GetPendingNonce(address string) uint64 { return 0 }
func (Mempool) GetPendingNonce(address string) (uint64, bool) { return 0, true }
func (Mempool) Update(
_ int64,
txs types.Txs,
Expand Down
6 changes: 3 additions & 3 deletions libs/tendermint/rpc/client/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,13 @@ func (c *baseRPCClient) GetAddressList() (*ctypes.ResultUnconfirmedAddresses, er
return result, nil
}

func (c *baseRPCClient) GetPendingNonce(address string) (*ctypes.ResultPendingNonce, error) {
func (c *baseRPCClient) GetPendingNonce(address string) (*ctypes.ResultPendingNonce, bool) {
result := new(ctypes.ResultPendingNonce)
_, err := c.caller.Call("get_pending_nonce", map[string]interface{}{"address": address}, result)
if err != nil {
return nil, errors.Wrap(err, "get_pending_nonce")
return nil, false
}
return result, nil
return result, true
}

func (c *baseRPCClient) NetInfo() (*ctypes.ResultNetInfo, error) {
Expand Down
2 changes: 1 addition & 1 deletion libs/tendermint/rpc/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type MempoolClient interface {
UserNumUnconfirmedTxs(address string) (*ctypes.ResultUserUnconfirmedTxs, error)
GetUnconfirmedTxByHash(hash [sha256.Size]byte) (types.Tx, error)
GetAddressList() (*ctypes.ResultUnconfirmedAddresses, error)
GetPendingNonce(address string) (*ctypes.ResultPendingNonce, error)
GetPendingNonce(address string) (*ctypes.ResultPendingNonce, bool)
}

// EvidenceClient is used for submitting an evidence of the malicious
Expand Down
2 changes: 1 addition & 1 deletion libs/tendermint/rpc/client/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (c *Local) GetAddressList() (*ctypes.ResultUnconfirmedAddresses, error) {
return core.GetAddressList()
}

func (c *Local) GetPendingNonce(address string) (*ctypes.ResultPendingNonce, error) {
func (c *Local) GetPendingNonce(address string) (*ctypes.ResultPendingNonce, bool) {
return core.GetPendingNonce(address)
}

Expand Down
9 changes: 6 additions & 3 deletions libs/tendermint/rpc/core/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,12 @@ func GetAddressList() (*ctypes.ResultUnconfirmedAddresses, error) {
}, nil
}

func GetPendingNonce(address string) (*ctypes.ResultPendingNonce, error) {
nonce := env.Mempool.GetPendingNonce(address)
func GetPendingNonce(address string) (*ctypes.ResultPendingNonce, bool) {
nonce, ok := env.Mempool.GetPendingNonce(address)
if !ok {
return nil, false
}
return &ctypes.ResultPendingNonce{
Nonce: nonce,
}, nil
}, true
}

0 comments on commit 9fd260d

Please sign in to comment.