Skip to content
This repository was archived by the owner on Mar 11, 2024. It is now read-only.

Commit df8f38a

Browse files
author
kaifei Hu
authored
Merge pull request #95 from irisnet/develop
R4R: Use blockResult get events instead of concurrency call queryTx
2 parents 4844060 + 8e484f4 commit df8f38a

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

block/parse_tx.go

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,26 @@ func ParseBlock(b int64, client *pool.Client) (*model.Block, []*model.Tx, []mode
132132
Height: b,
133133
CreateTime: time.Now().Unix(),
134134
}
135+
136+
blockResults, err := client.BlockResults(ctx, &b)
137+
138+
if err != nil {
139+
time.Sleep(1 * time.Second)
140+
blockResults, err = client.BlockResults(ctx, &b)
141+
if err != nil {
142+
return &blockDoc, nil, nil, utils.ConvertErr(b, "", "ParseBlockResult", err)
143+
}
144+
}
145+
146+
if len(resblock.Block.Txs) != len(blockResults.TxsResults) {
147+
return nil, nil, nil, utils.ConvertErr(b, "", "block.Txs length not equal blockResult", nil)
148+
}
149+
135150
txs := make([]*model.Tx, 0, len(resblock.Block.Txs))
136151
var docMsgs []model.TxMsg
137-
for _, tx := range resblock.Block.Txs {
138-
tx, msgs, err := ParseTx(tx, resblock.Block, client)
152+
for index, tx := range resblock.Block.Txs {
153+
txResult := blockResults.TxsResults[index]
154+
tx, msgs, err := ParseTx(tx, txResult, resblock.Block, index)
139155
if err != nil {
140156
return &blockDoc, txs, docMsgs, err
141157
}
@@ -148,7 +164,7 @@ func ParseBlock(b int64, client *pool.Client) (*model.Block, []*model.Tx, []mode
148164
}
149165

150166
// parse iris tx from iris block result tx
151-
func ParseTx(txBytes types.Tx, block *types.Block, client *pool.Client) (model.Tx, []model.TxMsg, error) {
167+
func ParseTx(txBytes types.Tx, txResult *aTypes.ResponseDeliverTx, block *types.Block, index int) (model.Tx, []model.TxMsg, error) {
152168

153169
var (
154170
docMsgs []model.TxMsg
@@ -168,18 +184,6 @@ func ParseTx(txBytes types.Tx, block *types.Block, client *pool.Client) (model.T
168184
}
169185
fee := msgsdktypes.BuildFee(authTx.GetFee(), authTx.GetGas())
170186
memo := authTx.GetMemo()
171-
ctx := context.Background()
172-
res, err := client.Tx(ctx, txBytes.Hash(), false)
173-
if err != nil {
174-
time.Sleep(1 * time.Second)
175-
var err1 error
176-
client2 := pool.GetClient()
177-
res, err1 = client2.Tx(ctx, txBytes.Hash(), false)
178-
client2.Release()
179-
if err1 != nil {
180-
return docTx, docMsgs, utils.ConvertErr(block.Height, txHash, "TxResult", err1)
181-
}
182-
}
183187

184188
if len(fee.Amount) > 0 {
185189
actualFee = fee.Amount[0]
@@ -192,19 +196,19 @@ func ParseTx(txBytes types.Tx, block *types.Block, client *pool.Client) (model.T
192196
Fee: fee,
193197
ActualFee: actualFee,
194198
Memo: memo,
195-
TxIndex: res.Index,
196-
TxId: buildTxId(height, res.Index),
199+
TxIndex: uint32(index),
200+
TxId: buildTxId(height, uint32(index)),
197201
}
198202
docTx.Status = utils.TxStatusSuccess
199-
if res.TxResult.Code != 0 {
203+
if txResult.Code != 0 {
200204
docTx.Status = utils.TxStatusFail
201-
docTx.Log = res.TxResult.Log
205+
docTx.Log = txResult.Log
202206

203207
}
204-
docTx.Events = parseEvents(res.TxResult.Events)
208+
docTx.Events = parseEvents(txResult.Events)
205209
eventsIndexMap := make(map[int]model.MsgEvent)
206-
if res.TxResult.Code == 0 {
207-
eventsIndexMap = splitEvents(res.TxResult.Log)
210+
if txResult.Code == 0 {
211+
eventsIndexMap = splitEvents(txResult.Log)
208212
}
209213

210214
msgs := authTx.GetMsgs()
@@ -245,12 +249,12 @@ func ParseTx(txBytes types.Tx, block *types.Block, client *pool.Client) (model.T
245249
TxHash: docTx.TxHash,
246250
Type: msgDocInfo.DocTxMsg.Type,
247251
MsgIndex: i,
248-
TxIndex: res.Index,
252+
TxIndex: uint32(index),
249253
TxStatus: docTx.Status,
250254
TxMemo: memo,
251255
TxLog: docTx.Log,
252-
GasUsed: res.TxResult.GasUsed,
253-
GasWanted: res.TxResult.GasWanted,
256+
GasUsed: txResult.GasUsed,
257+
GasWanted: txResult.GasWanted,
254258
}
255259
docMsg.Msg = msgDocInfo.DocTxMsg
256260
if val, ok := eventsIndexMap[i]; ok {

0 commit comments

Comments
 (0)