-
Notifications
You must be signed in to change notification settings - Fork 12
/
block_result.go
194 lines (185 loc) · 5.51 KB
/
block_result.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package cli
import (
"context"
"encoding/hex"
"encoding/json"
"strconv"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/gogo/protobuf/proto"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/abci/types"
coretypes "github.com/tendermint/tendermint/rpc/core/types"
)
func QueryBlockResultsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "block-results <height>",
Short: "Query for a transaction by hash in a committed block",
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
var height int64
if len(args) > 0 {
height, err = strconv.ParseInt(args[0], 10, 64)
if err != nil {
blockHeight, err := hexutil.DecodeUint64(args[0])
if err != nil {
return err
}
height = int64(blockHeight)
}
} else {
status, err := clientCtx.Client.Status(context.Background())
if err != nil {
return err
}
height = status.SyncInfo.LatestBlockHeight
}
blockResults, err := clientCtx.Client.BlockResults(context.Background(), &height)
if err != nil {
return err
}
output, err := ParseBlockResults(clientCtx.Codec, blockResults)
if err != nil {
return err
}
return PrintOutput(clientCtx, output)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
func ParseBlockResults(cdc codec.JSONCodec, blockResults *coretypes.ResultBlockResults) (interface{}, error) {
consensusParamUpdates, err := cdc.MarshalJSON(blockResults.ConsensusParamUpdates)
if err != nil {
return nil, err
}
var beginBlockEvents []map[string]interface{}
for _, event := range blockResults.BeginBlockEvents {
beginBlockEvents = append(beginBlockEvents, map[string]interface{}{
"type": event.Type,
"attributes": AttributesToMap(event.Attributes),
})
}
var endBlockEvents []map[string]interface{}
for _, event := range blockResults.EndBlockEvents {
endBlockEvents = append(endBlockEvents, map[string]interface{}{
"type": event.Type,
"attributes": AttributesToMap(event.Attributes),
})
}
var txsResults []map[string]interface{}
for _, txResult := range blockResults.TxsResults {
txsResults = append(txsResults, TxResultToMap(cdc, txResult))
}
var validatorUpdates []json.RawMessage
for _, valUp := range blockResults.ValidatorUpdates {
valUpData, err := cdc.MarshalJSON(&valUp)
if err != nil {
return nil, err
}
validatorUpdates = append(validatorUpdates, valUpData)
}
return map[string]interface{}{
"height": blockResults.Height,
"txs_results": txsResults,
"begin_block_events": beginBlockEvents,
"end_block_events": endBlockEvents,
"validator_updates": validatorUpdates,
"consensus_param_updates": json.RawMessage(consensusParamUpdates),
}, nil
}
func TxResponseToMap(cdc codec.JSONCodec, txResponse *sdk.TxResponse) map[string]interface{} {
if txResponse == nil {
return map[string]interface{}{}
}
var txResultEvents []map[string]interface{}
for _, event := range txResponse.Events {
txResultEvents = append(txResultEvents, map[string]interface{}{
"type": event.Type,
"attributes": AttributesToMap(event.Attributes),
})
}
tx, err := cdc.MarshalJSON(txResponse.Tx)
if err != nil {
return nil
}
txData, err := hex.DecodeString(txResponse.Data)
if err != nil {
return nil
}
txMsgData := sdk.TxMsgData{
Data: make([]*sdk.MsgData, 0), // nolint:staticcheck
}
if err := proto.Unmarshal(txData, &txMsgData); err != nil {
return nil
}
data, err := cdc.MarshalJSON(&txMsgData)
if err != nil {
return nil
}
return map[string]interface{}{
"height": txResponse.Height,
"txhash": txResponse.TxHash,
"codespace": txResponse.Codespace,
"code": txResponse.Code,
"data": json.RawMessage(data),
"raw_log": txResponse.RawLog,
"logs": txResponse.Logs,
"info": txResponse.Info,
"gas_wanted": txResponse.GasWanted,
"gas_used": txResponse.GasUsed,
"tx": json.RawMessage(tx),
"timestamp": txResponse.Timestamp,
"events": txResultEvents,
}
}
func TxResultToMap(cdc codec.JSONCodec, txResult *types.ResponseDeliverTx) map[string]interface{} {
if txResult == nil {
return map[string]interface{}{}
}
var txResultEvents []map[string]interface{}
for _, event := range txResult.Events {
txResultEvents = append(txResultEvents, map[string]interface{}{
"type": event.Type,
"attributes": AttributesToMap(event.Attributes),
})
}
txMsgData := sdk.TxMsgData{
Data: make([]*sdk.MsgData, 0), // nolint:staticcheck
}
if err := proto.Unmarshal(txResult.Data, &txMsgData); err != nil {
return nil
}
data, err := cdc.MarshalJSON(&txMsgData)
if err != nil {
return nil
}
return map[string]interface{}{
"code": txResult.Code,
"data": json.RawMessage(data),
"log": txResult.Log,
"info": txResult.Info,
"gas_wanted": txResult.GasWanted,
"gas_used": txResult.GasUsed,
"events": txResultEvents,
"codespace": txResult.Codespace,
}
}
func AttributesToMap(attrs []types.EventAttribute) []map[string]interface{} {
var attributes []map[string]interface{}
for _, attribute := range attrs {
attributes = append(attributes, map[string]interface{}{
"index": attribute.Index,
"key": string(attribute.Key),
"value": string(attribute.Value),
})
}
return attributes
}