From b1c50ff7f1c19fd7c752dfa71d3aaeb32e2a8b72 Mon Sep 17 00:00:00 2001 From: YuanXingqiang Date: Wed, 12 Jan 2022 18:38:32 +0800 Subject: [PATCH] Merge PR: fix txpool before venus height (#1421) * fix txpool before venus height * fix CLIContext height when checking tendermint error * fix ut --- app/rpc/namespaces/eth/tx_pool.go | 14 +++++++++++++- libs/cosmos-sdk/client/context/broadcast.go | 16 ++++++++++++---- libs/cosmos-sdk/client/context/broadcast_test.go | 6 ++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/app/rpc/namespaces/eth/tx_pool.go b/app/rpc/namespaces/eth/tx_pool.go index 56a532e32d..4ff50eee8f 100644 --- a/app/rpc/namespaces/eth/tx_pool.go +++ b/app/rpc/namespaces/eth/tx_pool.go @@ -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 @@ -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 diff --git a/libs/cosmos-sdk/client/context/broadcast.go b/libs/cosmos-sdk/client/context/broadcast.go index b13cbacf07..9c23c1378c 100644 --- a/libs/cosmos-sdk/client/context/broadcast.go +++ b/libs/cosmos-sdk/client/context/broadcast.go @@ -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)) @@ -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 } @@ -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 } @@ -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 } diff --git a/libs/cosmos-sdk/client/context/broadcast_test.go b/libs/cosmos-sdk/client/context/broadcast_test.go index 6fd1b92fbe..4dd90f10e1 100644 --- a/libs/cosmos-sdk/client/context/broadcast_test.go +++ b/libs/cosmos-sdk/client/context/broadcast_test.go @@ -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},