-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
132 lines (113 loc) · 3.26 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package utils
import (
"encoding/hex"
"errors"
"fmt"
"github.com/ci123chain/ci123chain/pkg/abci/codec"
apptypes "github.com/ci123chain/ci123chain/pkg/app/types"
"github.com/ci123chain/ci123chain/pkg/client/context"
"github.com/ci123chain/ci123chain/pkg/transfer/types"
"time"
sdk "github.com/ci123chain/ci123chain/pkg/abci/types"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
)
func QueryTx(cliCtx context.Context, hashHexStr string) (sdk.TxResponse, error) {
hash, err := hex.DecodeString(hashHexStr)
if err != nil {
return sdk.TxResponse{}, types.ErrInvalidTxHash
}
node, err := cliCtx.GetNode()
if err != nil {
return sdk.TxResponse{}, types.ErrGetNodeFailed
}
resTx, err := node.Tx(cliCtx.Context(), hash, true)
if err != nil {
return sdk.TxResponse{}, types.ErrQueryTx
}
if resTx.TxResult.Code == 204 {
return sdk.TxResponse{
TxHash: hashHexStr,
Code: resTx.TxResult.Code,
Info: resTx.TxResult.Log,
}, nil
}
resBlocks, err := getBlocksForTxResults(cliCtx, []*ctypes.ResultTx{resTx})
if err != nil {
return sdk.TxResponse{}, types.ErrGetBlockFailed
}
out, err := formatTxResult(cliCtx.Cdc, resTx, resBlocks[resTx.Height])
if err != nil {
return out, types.ErrQueryTx
}
return out, nil
}
func QueryTxsWithHeight(cliCtx context.Context, heights []int64) ([]sdk.TxsResult, error) {
var results = make([]sdk.TxsResult, 0)
for _, v := range heights {
var result sdk.TxsResult
res, getErr := getTxsInfo(cliCtx, v)
if getErr != nil {
result.Txs = nil
result.Height = v
result.Error = getErr.Error()
}else {
result.Error = ""
result.Height = v
result.Txs = res
}
results = append(results, result)
}
return results, nil
}
func getBlocksForTxResults(cliCtx context.Context, resTxs []*ctypes.ResultTx) (map[int64]*ctypes.ResultBlock, error) {
node, err := cliCtx.GetNode()
if err != nil {
return nil, err
}
resBlocks := make(map[int64]*ctypes.ResultBlock)
for _, resTx := range resTxs {
if _, ok := resBlocks[resTx.Height]; !ok {
resBlock, err := node.Block(cliCtx.Context(), &resTx.Height)
if err != nil {
return nil, err
}
resBlocks[resTx.Height] = resBlock
}
}
return resBlocks, nil
}
func getTxsInfo(cliCtx context.Context, height int64) ([]sdk.TxInfo, error) {
node, err := cliCtx.GetNode()
if err != nil {
return nil, err
}
infoes := make([]sdk.TxInfo, 0)
block, err := node.Block(cliCtx.Context(), &height)
if err != nil {
return nil, err
}
if block == nil {
return nil, errors.New(fmt.Sprintf("the height %d, has no block", height))
}
for _, v := range block.Block.Data.Txs{
///hashes = append(hashes, hex.EncodeToString(v.Hash()))
resTx, _ := node.Tx(cliCtx.Context(), v.Hash(), true)
index := resTx.Index
info := sdk.TxInfo{
Hash: hex.EncodeToString(v.Hash()),
Index: index,
}
infoes = append(infoes, info)
}
return infoes, nil
}
func formatTxResult(cdc *codec.Codec, resTx *ctypes.ResultTx, resBlock *ctypes.ResultBlock) (sdk.TxResponse, error) {
tx, err := parseTx(cdc, resTx.Tx)
if err != nil {
return sdk.TxResponse{}, err
}
return sdk.NewResponseResultTx(resTx, tx, resBlock.Block.Time.Format(time.RFC3339)), nil
}
func parseTx(cdc *codec.Codec, txBytes []byte) (sdk.Tx, error) {
return apptypes.DefaultTxDecoder(cdc)(txBytes)
}