Skip to content

Commit

Permalink
Merge PR: add explains of index for dropped txs (#953)
Browse files Browse the repository at this point in the history
* add defer unlock

* fix defer unlock

* fix defer unlock

* fix out of range[]

* add judge of i

* add explains of index for dropped txs

* del unusable judge i

* add func dropTxs

* add func dropTxs

* add func dropTxs

Co-authored-by: MengXiangJian <[email protected]>
  • Loading branch information
chengzhinei and xiangjianmeng authored Aug 10, 2021
1 parent 35f7b8f commit 653ca25
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions app/rpc/namespaces/eth/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ var broadcastErrors = map[uint32]*sdkerrors.Error{
sdkerrors.ErrTxTooLarge.ABCICode(): sdkerrors.ErrTxTooLarge,
}

var txPoolSliceMaxLen uint64


type TxPool struct {
addressTxsPool map[common.Address][]*evmtypes.MsgEthereumTx // All currently processable transactions
clientCtx clientcontext.CLIContext
Expand All @@ -49,6 +52,8 @@ func NewTxPool(clientCtx clientcontext.CLIContext, api *PublicEthereumAPI) *TxPo
panic(err)
}

txPoolSliceMaxLen = viper.GetUint64(TxPoolSliceMaxLen)

pool := &TxPool{
addressTxsPool: make(map[common.Address][]*evmtypes.MsgEthereumTx),
clientCtx: clientCtx,
Expand Down Expand Up @@ -146,7 +151,7 @@ func (pool *TxPool) CacheAndBroadcastTx(api *PublicEthereumAPI, address common.A
return fmt.Errorf("AccountNonce of tx is less than currentNonce in memPool: AccountNonce[%d], currentNonce[%d]", tx.Data.AccountNonce, currentNonce)
}

if tx.Data.AccountNonce > currentNonce+viper.GetUint64(TxPoolSliceMaxLen) {
if tx.Data.AccountNonce > currentNonce + txPoolSliceMaxLen {
return fmt.Errorf("AccountNonce of tx is bigger than txPool capacity, please try later: AccountNonce[%d]", tx.Data.AccountNonce)
}

Expand Down Expand Up @@ -182,7 +187,7 @@ func (pool *TxPool) update(index int, address common.Address, tx *evmtypes.MsgEt
func (pool *TxPool) insertTx(address common.Address, tx *evmtypes.MsgEthereumTx) error {
// if this is the first time to insertTx, make the cap of txPool be TxPoolSliceMaxLen
if _, ok := pool.addressTxsPool[address]; !ok {
pool.addressTxsPool[address] = make([]*evmtypes.MsgEthereumTx, 0, viper.GetUint64(TxPoolSliceMaxLen))
pool.addressTxsPool[address] = make([]*evmtypes.MsgEthereumTx, 0, txPoolSliceMaxLen)
}
index := 0
for index < len(pool.addressTxsPool[address]) {
Expand Down Expand Up @@ -221,36 +226,30 @@ func (pool *TxPool) continueBroadcast(api *PublicEthereumAPI, currentNonce uint6
break
}
}
// i is the start index of txs that don't need to be dropped
if err != nil {
if !strings.Contains(err.Error(), sdkerrors.ErrMempoolIsFull.Error()) {
if i >= txsLen {
return fmt.Errorf("index out of range")
}
// tx has err, and err is not mempoolfull, the tx should be dropped
err = fmt.Errorf("%s, nonce %d of tx has been dropped, please send again",
err.Error(), pool.addressTxsPool[address][i].Data.AccountNonce)
i++
pool.dropTxs(i + 1, address)
} else {
if i >= txsLen {
return fmt.Errorf("index out of range")
}
err = fmt.Errorf("%s, nonce %d :", err.Error(), pool.addressTxsPool[address][i].Data.AccountNonce)
pool.dropTxs(i, address)
}
api.logger.Error(err.Error())
}

// update txPool
if i != 0 {
if i > txsLen {
return fmt.Errorf("index out of range")
}
tmp := make([]*evmtypes.MsgEthereumTx, len(pool.addressTxsPool[address][i:]), viper.GetUint64(TxPoolSliceMaxLen))
copy(tmp, pool.addressTxsPool[address][i:])
pool.addressTxsPool[address] = tmp
}

return err
}

// drop [0:index) txs in txpool
func (pool *TxPool)dropTxs(index int, address common.Address) {
tmp := make([]*evmtypes.MsgEthereumTx, len(pool.addressTxsPool[address][index:]), txPoolSliceMaxLen)
copy(tmp, pool.addressTxsPool[address][index:])
pool.addressTxsPool[address] = tmp
}

func (pool *TxPool) broadcast(tx *evmtypes.MsgEthereumTx) error {
txEncoder := authclient.GetTxEncoder(pool.clientCtx.Codec)
txBytes, err := txEncoder(tx)
Expand Down

0 comments on commit 653ca25

Please sign in to comment.