From 4ee791650b19fabfa4de4f00489c07b3672ec418 Mon Sep 17 00:00:00 2001 From: zhyj Date: Fri, 8 Jul 2022 17:55:26 +0800 Subject: [PATCH 1/2] Use blockResult get events instead of concurrency call queryTx --- block/parse_tx.go | 55 ++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/block/parse_tx.go b/block/parse_tx.go index 90c2d8e..ea93be3 100644 --- a/block/parse_tx.go +++ b/block/parse_tx.go @@ -13,6 +13,7 @@ import ( "github.com/kaifei-bianjie/msg-parser/modules/ibc" msgsdktypes "github.com/kaifei-bianjie/msg-parser/types" aTypes "github.com/tendermint/tendermint/abci/types" + types2 "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/types" "golang.org/x/net/context" "gopkg.in/mgo.v2/bson" @@ -132,10 +133,26 @@ func ParseBlock(b int64, client *pool.Client) (*model.Block, []*model.Tx, []mode Height: b, CreateTime: time.Now().Unix(), } + + blockResults, err := client.BlockResults(ctx, &b) + + if err != nil { + time.Sleep(1 * time.Second) + blockResults, err = client.BlockResults(ctx, &b) + if err != nil { + return &blockDoc, nil, nil, utils.ConvertErr(b, "", "ParseBlockResult", err) + } + } + + if len(resblock.Block.Txs) != len(blockResults.TxsResults) { + return nil, nil, nil, utils.ConvertErr(b, "", "block.Txs length not equal blockResult", nil) + } + txs := make([]*model.Tx, 0, len(resblock.Block.Txs)) var docMsgs []model.TxMsg - for _, tx := range resblock.Block.Txs { - tx, msgs, err := ParseTx(tx, resblock.Block, client) + for index, tx := range resblock.Block.Txs { + txResult := blockResults.TxsResults[index] + tx, msgs, err := ParseTx(tx, txResult, resblock.Block, index) if err != nil { return &blockDoc, txs, docMsgs, err } @@ -148,7 +165,7 @@ func ParseBlock(b int64, client *pool.Client) (*model.Block, []*model.Tx, []mode } // parse iris tx from iris block result tx -func ParseTx(txBytes types.Tx, block *types.Block, client *pool.Client) (model.Tx, []model.TxMsg, error) { +func ParseTx(txBytes types.Tx, txResult *types2.ResponseDeliverTx, block *types.Block, index int) (model.Tx, []model.TxMsg, error) { var ( docMsgs []model.TxMsg @@ -168,18 +185,6 @@ func ParseTx(txBytes types.Tx, block *types.Block, client *pool.Client) (model.T } fee := msgsdktypes.BuildFee(authTx.GetFee(), authTx.GetGas()) memo := authTx.GetMemo() - ctx := context.Background() - res, err := client.Tx(ctx, txBytes.Hash(), false) - if err != nil { - time.Sleep(1 * time.Second) - var err1 error - client2 := pool.GetClient() - res, err1 = client2.Tx(ctx, txBytes.Hash(), false) - client2.Release() - if err1 != nil { - return docTx, docMsgs, utils.ConvertErr(block.Height, txHash, "TxResult", err1) - } - } if len(fee.Amount) > 0 { actualFee = fee.Amount[0] @@ -192,19 +197,19 @@ func ParseTx(txBytes types.Tx, block *types.Block, client *pool.Client) (model.T Fee: fee, ActualFee: actualFee, Memo: memo, - TxIndex: res.Index, - TxId: buildTxId(height, res.Index), + TxIndex: uint32(index), + TxId: buildTxId(height, uint32(index)), } docTx.Status = utils.TxStatusSuccess - if res.TxResult.Code != 0 { + if txResult.Code != 0 { docTx.Status = utils.TxStatusFail - docTx.Log = res.TxResult.Log + docTx.Log = txResult.Log } - docTx.Events = parseEvents(res.TxResult.Events) + docTx.Events = parseEvents(txResult.Events) eventsIndexMap := make(map[int]model.MsgEvent) - if res.TxResult.Code == 0 { - eventsIndexMap = splitEvents(res.TxResult.Log) + if txResult.Code == 0 { + eventsIndexMap = splitEvents(txResult.Log) } msgs := authTx.GetMsgs() @@ -245,12 +250,12 @@ func ParseTx(txBytes types.Tx, block *types.Block, client *pool.Client) (model.T TxHash: docTx.TxHash, Type: msgDocInfo.DocTxMsg.Type, MsgIndex: i, - TxIndex: res.Index, + TxIndex: uint32(index), TxStatus: docTx.Status, TxMemo: memo, TxLog: docTx.Log, - GasUsed: res.TxResult.GasUsed, - GasWanted: res.TxResult.GasWanted, + GasUsed: txResult.GasUsed, + GasWanted: txResult.GasWanted, } docMsg.Msg = msgDocInfo.DocTxMsg if val, ok := eventsIndexMap[i]; ok { From 0e67d9d00e7e8cb986e652147d575f40940aba81 Mon Sep 17 00:00:00 2001 From: zhyj Date: Mon, 11 Jul 2022 09:28:10 +0800 Subject: [PATCH 2/2] delete unused package --- block/parse_tx.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/block/parse_tx.go b/block/parse_tx.go index ea93be3..5770c80 100644 --- a/block/parse_tx.go +++ b/block/parse_tx.go @@ -13,7 +13,6 @@ import ( "github.com/kaifei-bianjie/msg-parser/modules/ibc" msgsdktypes "github.com/kaifei-bianjie/msg-parser/types" aTypes "github.com/tendermint/tendermint/abci/types" - types2 "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/types" "golang.org/x/net/context" "gopkg.in/mgo.v2/bson" @@ -165,7 +164,7 @@ func ParseBlock(b int64, client *pool.Client) (*model.Block, []*model.Tx, []mode } // parse iris tx from iris block result tx -func ParseTx(txBytes types.Tx, txResult *types2.ResponseDeliverTx, block *types.Block, index int) (model.Tx, []model.TxMsg, error) { +func ParseTx(txBytes types.Tx, txResult *aTypes.ResponseDeliverTx, block *types.Block, index int) (model.Tx, []model.TxMsg, error) { var ( docMsgs []model.TxMsg