/
implementation.go
189 lines (165 loc) · 6.08 KB
/
implementation.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package grpc
import (
"context"
"errors"
"code.vegaprotocol.io/vega/blockexplorer/entities"
"code.vegaprotocol.io/vega/blockexplorer/store"
"code.vegaprotocol.io/vega/logging"
pb "code.vegaprotocol.io/vega/protos/blockexplorer/api/v1"
types "code.vegaprotocol.io/vega/protos/vega"
"code.vegaprotocol.io/vega/version"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var ErrNotMapped = errors.New("error not mapped")
type blockExplorerAPI struct {
Config
pb.UnimplementedBlockExplorerServiceServer
store *store.Store
log *logging.Logger
}
func NewBlockExplorerAPI(store *store.Store, config Config, log *logging.Logger) pb.BlockExplorerServiceServer {
log = log.Named(namedLogger)
log.SetLevel(config.Level.Get())
be := blockExplorerAPI{
Config: config,
store: store,
log: log.Named(namedLogger),
}
return &be
}
func (b *blockExplorerAPI) Info(ctx context.Context, _ *pb.InfoRequest) (*pb.InfoResponse, error) {
return &pb.InfoResponse{
Version: version.Get(),
CommitHash: version.GetCommitHash(),
}, nil
}
func (b *blockExplorerAPI) GetTransaction(ctx context.Context, req *pb.GetTransactionRequest) (*pb.GetTransactionResponse, error) {
transaction, err := b.store.GetTransaction(ctx, req.Hash)
if err != nil {
c := codes.Internal
if errors.Is(err, store.ErrTxNotFound) {
c = codes.NotFound
} else if errors.Is(err, store.ErrMultipleTxFound) {
c = codes.FailedPrecondition
}
return nil, apiError(c, err)
}
resp := pb.GetTransactionResponse{
Transaction: transaction,
}
return &resp, nil
}
func (b *blockExplorerAPI) ListTransactions(ctx context.Context, req *pb.ListTransactionsRequest) (*pb.ListTransactionsResponse, error) {
var before, after *entities.TxCursor
var first, last uint32
if req.Before != nil && req.After != nil && (req.First > 0 || req.Last > 0) {
return nil, apiError(codes.InvalidArgument, errors.New("cannot use neither limits `first`, nor `last` when both cursors `before` and `after` are set"))
}
if req.First > 0 && req.Last > 0 {
return nil, apiError(codes.InvalidArgument, errors.New("cannot use both limits `first` and `last` within the same query"))
}
if req.Before != nil {
cursor, err := entities.TxCursorFromString(*req.Before)
if err != nil {
return nil, apiError(codes.InvalidArgument, err)
}
before = &cursor
last = b.MaxPageSizeDefault
}
if req.After != nil {
cursor, err := entities.TxCursorFromString(*req.After)
if err != nil {
return nil, apiError(codes.InvalidArgument, err)
}
after = &cursor
first = b.MaxPageSizeDefault
}
if before != nil && after != nil {
// The order of the parameters may seem odd, but this is expected as we have
// to keep in mind the natural order of the block-explorer is reverse-chronological.
// so, given transactions 4.2, 4.1, 3.2, 3.1, 2.2, when applying the window between
// 3.1 and 4.2, then we have to set after to 3.1 and before to 4.2.
// So effectively, after is the start and before is the end of the set.
if entities.AreValidCursorBoundaries(after, before) {
return nil, apiError(codes.InvalidArgument, errors.New("cursors `before` and `after` do not create a valid window"))
}
}
if req.First > 0 {
if req.Before != nil {
return nil, apiError(codes.InvalidArgument, errors.New("cannot use cursor `before` when using limit `first`"))
}
first = req.First
} else if req.Last > 0 {
if req.After != nil {
return nil, apiError(codes.InvalidArgument, errors.New("cannot use cursor `after` when using limit `last`"))
}
last = req.Last
}
// Entering this condition means there is no pagination set, so it defaults
// to listing the MaxPageSizeDefault newest transactions.
// Note, setting limits on a cursor window is not supported.
if !(before != nil && after != nil) && first == 0 && last == 0 {
first = b.MaxPageSizeDefault
}
transactions, err := b.store.ListTransactions(ctx,
req.Filters,
req.CmdTypes,
req.ExcludeCmdTypes,
req.Parties,
first,
after,
last,
before,
)
if err != nil {
return nil, apiError(codes.Internal, err)
}
return &pb.ListTransactionsResponse{
Transactions: transactions,
}, nil
}
// errorMap contains a mapping between errors and Vega numeric error codes.
var errorMap = map[string]int32{
// General
ErrNotMapped.Error(): 10000,
store.ErrTxNotFound.Error(): 10001,
store.ErrMultipleTxFound.Error(): 10002,
}
// apiError is a helper function to build the Vega specific Error Details that
// can be returned by gRPC API and therefore also REST, GraphQL will be mapped too.
// It takes a standardised grpcCode, a Vega specific apiError, and optionally one
// or more internal errors (error from the core, rather than API).
func apiError(grpcCode codes.Code, apiError error) error {
s := status.Newf(grpcCode, "%v error", grpcCode)
// Create the API specific error detail for error e.g. missing party ID
detail := types.ErrorDetail{
Message: apiError.Error(),
}
// Lookup the API specific error in the table, return not found/not mapped
// if a code has not yet been added to the map, can happen if developer misses
// a step, periodic checking/ownership of API package can keep this up to date.
vegaCode, found := errorMap[apiError.Error()]
if found {
detail.Code = vegaCode
} else {
detail.Code = errorMap[ErrNotMapped.Error()]
}
// Pack the Vega domain specific errorDetails into the status returned by gRPC domain.
s, _ = s.WithDetails(&detail)
return s.Err()
}