/
block_result.go
147 lines (141 loc) · 4.21 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
package cli
import (
"context"
"encoding/json"
"strconv"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
)
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 {
return err
}
} 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
}
consensusParamUpdates, err := clientCtx.JSONMarshaler.MarshalJSON(blockResults.ConsensusParamUpdates)
if err != nil {
return err
}
var beginBlockEvents []map[string]interface{}
for _, event := range blockResults.BeginBlockEvents {
var attributes []map[string]interface{}
for _, attribute := range event.Attributes {
attributes = append(attributes, map[string]interface{}{
"Index": attribute.Index,
"Key": string(attribute.Key),
"Value": string(attribute.Value),
})
}
beginBlockEvents = append(beginBlockEvents, map[string]interface{}{
"type": event.Type,
"attributes": attributes,
})
}
var endBlockEvents []map[string]interface{}
for _, event := range blockResults.EndBlockEvents {
var attributes []map[string]interface{}
for _, attribute := range event.Attributes {
attributes = append(attributes, map[string]interface{}{
"Index": attribute.Index,
"Key": string(attribute.Key),
"Value": string(attribute.Value),
})
}
endBlockEvents = append(endBlockEvents, map[string]interface{}{
"type": event.Type,
"attributes": attributes,
})
}
var txsResults []map[string]interface{}
for _, txResult := range blockResults.TxsResults {
var txResultEvents []map[string]interface{}
for _, event := range txResult.Events {
var attributes []map[string]interface{}
for _, attribute := range event.Attributes {
attributes = append(attributes, map[string]interface{}{
"Index": attribute.Index,
"Key": string(attribute.Key),
"Value": string(attribute.Value),
})
}
txResultEvents = append(txResultEvents, map[string]interface{}{
"type": event.Type,
"attributes": attributes,
})
}
txsResults = append(txsResults, map[string]interface{}{
"code": txResult.Code,
"data": string(txResult.Data),
"log": txResult.Log,
"info": txResult.Info,
"gas_wanted": txResult.GasWanted,
"gas_used": txResult.GasUsed,
"events": txResultEvents,
"codespace": txResult.Codespace,
})
}
var validatorUpdates []json.RawMessage
for _, valUp := range blockResults.ValidatorUpdates {
valUpData, err := clientCtx.JSONMarshaler.MarshalJSON(&valUp)
if err != nil {
return err
}
validatorUpdates = append(validatorUpdates, valUpData)
}
output, err := json.Marshal(map[string]interface{}{
"Height": blockResults.Height,
"ConsensusParamUpdates": json.RawMessage(consensusParamUpdates),
"BeginBlockEvents": beginBlockEvents,
"EndBlockEvents": endBlockEvents,
"TxsResults": txsResults,
"ValidatorUpdates": validatorUpdates,
})
if err != nil {
return err
}
return PrintOutput(clientCtx, output)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
func PrintOutput(ctx client.Context, out []byte) error {
if ctx.OutputFormat == "text" {
// handle text format by decoding and re-encoding JSON as YAML
var j interface{}
err := json.Unmarshal(out, &j)
if err != nil {
return err
}
out, err = yaml.Marshal(j)
if err != nil {
return err
}
return ctx.PrintBytes(out)
} else {
return ctx.PrintBytes(out)
}
}