-
Notifications
You must be signed in to change notification settings - Fork 29
/
service.go
260 lines (231 loc) · 7.61 KB
/
service.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package tmservice
import (
"context"
"crypto/sha256"
gogogrpc "github.com/gogo/protobuf/grpc"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/Finschia/finschia-sdk/client"
"github.com/Finschia/finschia-sdk/client/rpc"
codectypes "github.com/Finschia/finschia-sdk/codec/types"
cryptotypes "github.com/Finschia/finschia-sdk/crypto/types"
qtypes "github.com/Finschia/finschia-sdk/types/query"
"github.com/Finschia/finschia-sdk/version"
)
// This is the struct that we will implement all the handlers on.
type queryServer struct {
clientCtx client.Context
interfaceRegistry codectypes.InterfaceRegistry
}
var (
_ ServiceServer = queryServer{}
_ codectypes.UnpackInterfacesMessage = &GetLatestValidatorSetResponse{}
)
// NewQueryServer creates a new tendermint query server.
func NewQueryServer(clientCtx client.Context, interfaceRegistry codectypes.InterfaceRegistry) ServiceServer {
return queryServer{
clientCtx: clientCtx,
interfaceRegistry: interfaceRegistry,
}
}
// GetSyncing implements ServiceServer.GetSyncing
func (s queryServer) GetSyncing(ctx context.Context, _ *GetSyncingRequest) (*GetSyncingResponse, error) {
status, err := getNodeStatus(ctx, s.clientCtx)
if err != nil {
return nil, err
}
return &GetSyncingResponse{
Syncing: status.SyncInfo.CatchingUp,
}, nil
}
// GetLatestBlock implements ServiceServer.GetLatestBlock
func (s queryServer) GetLatestBlock(ctx context.Context, _ *GetLatestBlockRequest) (*GetLatestBlockResponse, error) {
status, err := getBlock(ctx, s.clientCtx, nil)
if err != nil {
return nil, err
}
protoBlockID := status.BlockID.ToProto()
protoBlock, err := status.Block.ToProto()
if err != nil {
return nil, err
}
return &GetLatestBlockResponse{
BlockId: &protoBlockID,
Block: protoBlock,
}, nil
}
// GetBlockByHeight implements ServiceServer.GetBlockByHeight
func (s queryServer) GetBlockByHeight(ctx context.Context, req *GetBlockByHeightRequest) (*GetBlockByHeightResponse, error) {
chainHeight, err := rpc.GetChainHeight(s.clientCtx)
if err != nil {
return nil, err
}
if req.Height > chainHeight {
return nil, status.Error(codes.InvalidArgument, "requested block height is bigger then the chain length")
}
protoBlockID, protoBlock, err := GetProtoBlock(ctx, s.clientCtx, &req.Height)
if err != nil {
return nil, err
}
return &GetBlockByHeightResponse{
BlockId: &protoBlockID,
Block: protoBlock,
}, nil
}
// GetBlockByHash implements ServiceServer.GetBlockByHash
func (s queryServer) GetBlockByHash(_ context.Context, req *GetBlockByHashRequest) (*GetBlockByHashResponse, error) {
if n := len(req.Hash); n != sha256.Size {
if n == 0 {
return nil, status.Error(codes.InvalidArgument, "block hash cannot be empty")
}
return nil, status.Error(codes.InvalidArgument, "the length of block hash must be 32")
}
res, err := getBlockByHash(s.clientCtx, req.Hash)
if err != nil {
return nil, err
}
protoBlockID := res.BlockID.ToProto()
protoBlock, err := res.Block.ToProto()
if err != nil {
return nil, err
}
return &GetBlockByHashResponse{
BlockId: &protoBlockID,
Block: protoBlock,
}, nil
}
// GetBlockResultsByHeight implements ServiceServer.GetBlockResultsByHeight
func (s queryServer) GetBlockResultsByHeight(_ context.Context, req *GetBlockResultsByHeightRequest) (*GetBlockResultsByHeightResponse, error) {
res, err := getBlockResultsByHeight(s.clientCtx, &req.Height)
if err != nil {
return nil, err
}
return &GetBlockResultsByHeightResponse{
Height: res.Height,
TxsResults: res.TxsResults,
ResBeginBlock: &abci.ResponseBeginBlock{
Events: res.BeginBlockEvents,
},
ResEndBlock: &abci.ResponseEndBlock{
ValidatorUpdates: res.ValidatorUpdates,
ConsensusParamUpdates: res.ConsensusParamUpdates,
Events: res.EndBlockEvents,
},
}, nil
}
// GetLatestValidatorSet implements ServiceServer.GetLatestValidatorSet
func (s queryServer) GetLatestValidatorSet(ctx context.Context, req *GetLatestValidatorSetRequest) (*GetLatestValidatorSetResponse, error) {
page, limit, err := qtypes.ParsePagination(req.Pagination)
if err != nil {
return nil, err
}
return validatorsOutput(ctx, s.clientCtx, nil, page, limit)
}
func (m *GetLatestValidatorSetResponse) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var pubKey cryptotypes.PubKey
for _, val := range m.Validators {
err := unpacker.UnpackAny(val.PubKey, &pubKey)
if err != nil {
return err
}
}
return nil
}
// GetValidatorSetByHeight implements ServiceServer.GetValidatorSetByHeight
func (s queryServer) GetValidatorSetByHeight(ctx context.Context, req *GetValidatorSetByHeightRequest) (*GetValidatorSetByHeightResponse, error) {
page, limit, err := qtypes.ParsePagination(req.Pagination)
if err != nil {
return nil, err
}
chainHeight, err := rpc.GetChainHeight(s.clientCtx)
if err != nil {
return nil, status.Error(codes.Internal, "failed to parse chain height")
}
if req.Height > chainHeight {
return nil, status.Error(codes.InvalidArgument, "requested block height is bigger then the chain length")
}
r, err := validatorsOutput(ctx, s.clientCtx, &req.Height, page, limit)
if err != nil {
return nil, err
}
return &GetValidatorSetByHeightResponse{
BlockHeight: r.BlockHeight,
Validators: r.Validators,
Pagination: r.Pagination,
}, nil
}
func validatorsOutput(ctx context.Context, cctx client.Context, height *int64, page, limit int) (*GetLatestValidatorSetResponse, error) {
vs, err := rpc.GetValidators(ctx, cctx, height, &page, &limit)
if err != nil {
return nil, err
}
resp := GetLatestValidatorSetResponse{
BlockHeight: vs.BlockHeight,
Validators: make([]*Validator, len(vs.Validators)),
Pagination: &qtypes.PageResponse{
Total: vs.Total,
},
}
for i, v := range vs.Validators {
anyPub, err := codectypes.NewAnyWithValue(v.PubKey)
if err != nil {
return nil, err
}
resp.Validators[i] = &Validator{
Address: v.Address.String(),
ProposerPriority: v.ProposerPriority,
PubKey: anyPub,
VotingPower: v.VotingPower,
}
}
return &resp, nil
}
// GetNodeInfo implements ServiceServer.GetNodeInfo
func (s queryServer) GetNodeInfo(ctx context.Context, req *GetNodeInfoRequest) (*GetNodeInfoResponse, error) {
status, err := getNodeStatus(ctx, s.clientCtx)
if err != nil {
return nil, err
}
protoNodeInfo := status.NodeInfo.ToProto()
nodeInfo := version.NewInfo()
deps := make([]*Module, len(nodeInfo.BuildDeps))
for i, dep := range nodeInfo.BuildDeps {
deps[i] = &Module{
Path: dep.Path,
Sum: dep.Sum,
Version: dep.Version,
}
}
resp := GetNodeInfoResponse{
DefaultNodeInfo: protoNodeInfo,
ApplicationVersion: &VersionInfo{
AppName: nodeInfo.AppName,
Name: nodeInfo.Name,
GitCommit: nodeInfo.GitCommit,
GoVersion: nodeInfo.GoVersion,
Version: nodeInfo.Version,
BuildTags: nodeInfo.BuildTags,
BuildDeps: deps,
LbmSdkVersion: nodeInfo.LbmSdkVersion,
},
}
return &resp, nil
}
// RegisterTendermintService registers the tendermint queries on the gRPC router.
func RegisterTendermintService(
qrt gogogrpc.Server,
clientCtx client.Context,
interfaceRegistry codectypes.InterfaceRegistry,
) {
RegisterServiceServer(
qrt,
NewQueryServer(clientCtx, interfaceRegistry),
)
}
// RegisterGRPCGatewayRoutes mounts the tendermint service's GRPC-gateway routes on the
// given Mux.
func RegisterGRPCGatewayRoutes(clientConn gogogrpc.ClientConn, mux *runtime.ServeMux) {
RegisterServiceHandlerClient(context.Background(), mux, NewServiceClient(clientConn))
}