Skip to content

Commit

Permalink
Merge PR: fix txpool before venus height (#1421)
Browse files Browse the repository at this point in the history
* fix txpool before venus height

* fix CLIContext height when checking tendermint error

* fix ut
  • Loading branch information
yann-sjtu authored Jan 12, 2022
1 parent 64832fc commit b1c50ff
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
14 changes: 13 additions & 1 deletion app/rpc/namespaces/eth/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func (pool *TxPool) initDB(api *PublicEthereumAPI) error {
}

func broadcastTxByTxPool(api *PublicEthereumAPI, tx *evmtypes.MsgEthereumTx, txBytes []byte) (common.Hash, error) {
//TODO: to delete after venus height
info, err := api.clientCtx.Client.BlockchainInfo(0, 0)
if err != nil {
return common.Hash{}, err
Expand Down Expand Up @@ -263,7 +264,18 @@ func (pool *TxPool) dropTxs(index int, address common.Address) {
}

func (pool *TxPool) broadcast(tx *evmtypes.MsgEthereumTx) error {
txEncoder := authclient.GetTxEncoder(pool.clientCtx.Codec, authclient.WithEthereumTx())
// TODO: to delete after venus height
info, err := pool.clientCtx.Client.BlockchainInfo(0, 0)
if err != nil {
return err
}
var txEncoder sdk.TxEncoder
if types.HigherThanVenus(info.LastHeight) {
txEncoder = authclient.GetTxEncoder(nil, authclient.WithEthereumTx())
} else {
txEncoder = authclient.GetTxEncoder(pool.clientCtx.Codec)
}

txBytes, err := txEncoder(tx)
if err != nil {
return err
Expand Down
16 changes: 12 additions & 4 deletions libs/cosmos-sdk/client/context/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ func (ctx CLIContext) BroadcastTx(txBytes []byte) (res sdk.TxResponse, err error
// TODO: Avoid brittle string matching in favor of error matching. This requires
// a change to Tendermint's RPCError type to allow retrieval or matching against
// a concrete error type.
func CheckTendermintError(err error, txBytes []byte, height int64) *sdk.TxResponse {
func (ctx CLIContext) CheckTendermintError(err error, txBytes []byte) *sdk.TxResponse {
if err == nil {
return nil
}
var height int64
info, _ := ctx.Client.BlockchainInfo(0, 0)
if info != nil {
height = info.LastHeight
} else {
// default new tx hash
height = types.GetMilestoneVenusHeight()
}

errStr := strings.ToLower(err.Error())
txHash := fmt.Sprintf("%X", types.Tx(txBytes).Hash(height))
Expand Down Expand Up @@ -88,7 +96,7 @@ func (ctx CLIContext) BroadcastTxCommit(txBytes []byte) (sdk.TxResponse, error)

res, err := node.BroadcastTxCommit(txBytes)
if err != nil {
if errRes := CheckTendermintError(err, txBytes, ctx.Height); errRes != nil {
if errRes := ctx.CheckTendermintError(err, txBytes); errRes != nil {
return *errRes, nil
}

Expand All @@ -115,7 +123,7 @@ func (ctx CLIContext) BroadcastTxSync(txBytes []byte) (sdk.TxResponse, error) {
}

res, err := node.BroadcastTxSync(txBytes)
if errRes := CheckTendermintError(err, txBytes, ctx.Height); errRes != nil {
if errRes := ctx.CheckTendermintError(err, txBytes); errRes != nil {
return *errRes, nil
}

Expand All @@ -131,7 +139,7 @@ func (ctx CLIContext) BroadcastTxAsync(txBytes []byte) (sdk.TxResponse, error) {
}

res, err := node.BroadcastTxAsync(txBytes)
if errRes := CheckTendermintError(err, txBytes, ctx.Height); errRes != nil {
if errRes := ctx.CheckTendermintError(err, txBytes); errRes != nil {
return *errRes, nil
}

Expand Down
6 changes: 6 additions & 0 deletions libs/cosmos-sdk/client/context/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func (c MockClient) BroadcastTxSync(tx tmtypes.Tx) (*ctypes.ResultBroadcastTx, e
return nil, c.err
}

func (c MockClient) BlockchainInfo(minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
return &ctypes.ResultBlockchainInfo{
LastHeight: 0,
}, nil
}

func CreateContextWithErrorAndMode(err error, mode string) CLIContext {
return CLIContext{
Client: MockClient{err: err},
Expand Down

0 comments on commit b1c50ff

Please sign in to comment.