-
Notifications
You must be signed in to change notification settings - Fork 11
/
tx.go
482 lines (436 loc) · 14.3 KB
/
tx.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// SPDX-FileCopyrightText: 2024 PK Lab AG <contact@pklab.io>
// SPDX-License-Identifier: MIT
package handler
import (
"encoding/hex"
"net/http"
"time"
"github.com/celenium-io/celestia-indexer/cmd/api/handler/responses"
"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/celenium-io/celestia-indexer/internal/storage/types"
storageTypes "github.com/celenium-io/celestia-indexer/internal/storage/types"
"github.com/labstack/echo/v4"
)
type TxHandler struct {
tx storage.ITx
events storage.IEvent
messages storage.IMessage
namespaces storage.INamespace
blobLogs storage.IBlobLog
state storage.IState
indexerName string
}
func NewTxHandler(
tx storage.ITx,
events storage.IEvent,
messages storage.IMessage,
namespaces storage.INamespace,
blobLogs storage.IBlobLog,
state storage.IState,
indexerName string,
) *TxHandler {
return &TxHandler{
tx: tx,
events: events,
messages: messages,
namespaces: namespaces,
blobLogs: blobLogs,
state: state,
indexerName: indexerName,
}
}
type getTxRequest struct {
Hash string `param:"hash" validate:"required,hexadecimal,len=64"`
}
// Get godoc
//
// @Summary Get transaction by hash
// @Description Get transaction by hash
// @Tags transactions
// @ID get-transaction
// @Param hash path string true "Transaction hash in hexadecimal" minlength(64) maxlength(64)
// @Produce json
// @Success 200 {object} responses.Tx
// @Success 204
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/tx/{hash} [get]
func (handler *TxHandler) Get(c echo.Context) error {
req, err := bindAndValidate[getTxRequest](c)
if err != nil {
return badRequestError(c, err)
}
hash, err := hex.DecodeString(req.Hash)
if err != nil {
return badRequestError(c, err)
}
tx, err := handler.tx.ByHash(c.Request().Context(), hash)
if err != nil {
return handleError(c, err, handler.tx)
}
return c.JSON(http.StatusOK, responses.NewTx(tx))
}
// List godoc
//
// @Summary List transactions info
// @Description List transactions info
// @Tags transactions
// @ID list-transactions
// @Param limit query integer false "Count of requested entities" mininum(1) maximum(100)
// @Param offset query integer false "Offset" mininum(1)
// @Param sort query string false "Sort order" Enums(asc, desc)
// @Param status query types.Status false "Comma-separated status list"
// @Param msg_type query types.MsgType false "Comma-separated message types list"
// @Param excluded_msg_type query types.MsgType false "Comma-separated message types list which should be excluded"
// @Param from query integer false "Time from in unix timestamp" mininum(1)
// @Param to query integer false "Time to in unix timestamp" mininum(1)
// @Param height query integer false "Block number" mininum(1)
// @Param messages query boolean false "If true join messages" mininum(1)
// @Produce json
// @Success 200 {array} responses.Tx
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/tx [get]
func (handler *TxHandler) List(c echo.Context) error {
req, err := bindAndValidate[txListRequest](c)
if err != nil {
return badRequestError(c, err)
}
req.SetDefault()
fltrs := storage.TxFilter{
Limit: req.Limit,
Offset: req.Offset,
Sort: pgSort(req.Sort),
Status: req.Status,
Height: req.Height,
MessageTypes: types.NewMsgTypeBitMask(),
ExcludedMessageTypes: types.NewMsgTypeBitMask(),
WithMessages: req.Messages,
}
if req.From > 0 {
fltrs.TimeFrom = time.Unix(req.From, 0).UTC()
}
if req.To > 0 {
fltrs.TimeTo = time.Unix(req.To, 0).UTC()
}
for i := range req.MsgType {
fltrs.MessageTypes.SetByMsgType(storageTypes.MsgType(req.MsgType[i]))
}
for i := range req.ExcludedMsgType {
fltrs.ExcludedMessageTypes.SetByMsgType(storageTypes.MsgType(req.ExcludedMsgType[i]))
}
txs, err := handler.tx.Filter(c.Request().Context(), fltrs)
if err != nil {
return handleError(c, err, handler.tx)
}
response := make([]responses.Tx, len(txs))
for i := range txs {
response[i] = responses.NewTx(txs[i])
}
return returnArray(c, response)
}
type getTxRequestWithPagination struct {
Hash string `param:"hash" validate:"required,hexadecimal,len=64"`
Limit int `query:"limit" validate:"omitempty,min=1,max=100"`
Offset int `query:"offset" validate:"omitempty,min=0"`
}
func (p *getTxRequestWithPagination) SetDefault() {
if p.Limit == 0 {
p.Limit = 10
}
}
// GetEvents godoc
//
// @Summary Get transaction events
// @Description Get transaction events
// @Tags transactions
// @ID get-transaction-events
// @Param hash path string true "Transaction hash in hexadecimal" minlength(64) maxlength(64)
// @Param limit query integer false "Count of requested entities" mininum(1) maximum(100)
// @Param offset query integer false "Offset" mininum(1)
// @Produce json
// @Success 200 {array} responses.Event
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/tx/{hash}/events [get]
func (handler *TxHandler) GetEvents(c echo.Context) error {
req, err := bindAndValidate[getTxRequestWithPagination](c)
if err != nil {
return badRequestError(c, err)
}
req.SetDefault()
hash, err := hex.DecodeString(req.Hash)
if err != nil {
return badRequestError(c, err)
}
tx, err := handler.tx.ByHash(c.Request().Context(), hash)
if err != nil {
return handleError(c, err, handler.tx)
}
fltrs := storage.EventFilter{
Limit: req.Limit,
Offset: req.Offset,
Time: tx.Time.UTC(),
}
events, err := handler.events.ByTxId(c.Request().Context(), tx.Id, fltrs)
if err != nil {
return handleError(c, err, handler.tx)
}
response := make([]responses.Event, len(events))
for i := range events {
response[i] = responses.NewEvent(events[i])
}
return returnArray(c, response)
}
// GetMessages godoc
//
// @Summary Get transaction messages
// @Description Get transaction messages
// @Tags transactions
// @ID get-transaction-messages
// @Param hash path string true "Transaction hash in hexadecimal" minlength(64) maxlength(64)
// @Param limit query integer false "Count of requested entities" mininum(1) maximum(100)
// @Param offset query integer false "Offset" mininum(1)
// @Produce json
// @Success 200 {array} responses.Message
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/tx/{hash}/messages [get]
func (handler *TxHandler) GetMessages(c echo.Context) error {
req, err := bindAndValidate[getTxRequestWithPagination](c)
if err != nil {
return badRequestError(c, err)
}
req.SetDefault()
hash, err := hex.DecodeString(req.Hash)
if err != nil {
return badRequestError(c, err)
}
txId, err := handler.tx.IdByHash(c.Request().Context(), hash)
if err != nil {
return handleError(c, err, handler.tx)
}
messages, err := handler.messages.ByTxId(c.Request().Context(), txId, req.Limit, req.Offset)
if err != nil {
return handleError(c, err, handler.tx)
}
response := make([]responses.Message, len(messages))
for i := range messages {
response[i] = responses.NewMessage(messages[i])
}
return returnArray(c, response)
}
// Count godoc
//
// @Summary Get count of transactions in network
// @Description Get count of transactions in network
// @Tags transactions
// @ID get-transactions-count
// @Produce json
// @Success 200 {integer} uint64
// @Failure 500 {object} Error
// @Router /v1/tx/count [get]
func (handler *TxHandler) Count(c echo.Context) error {
state, err := handler.state.ByName(c.Request().Context(), handler.indexerName)
if err != nil {
return handleError(c, err, handler.tx)
}
return c.JSON(http.StatusOK, state.TotalTx)
}
// Genesis godoc
//
// @Summary List genesis transactions info
// @Description List genesis transactions info
// @Tags transactions
// @ID list-genesis-transactions
// @Param limit query integer false "Count of requested entities" mininum(1) maximum(100)
// @Param offset query integer false "Offset" mininum(1)
// @Param sort query string false "Sort order" Enums(asc, desc)
// @Produce json
// @Success 200 {array} responses.Tx
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/tx/genesis [get]
func (handler *TxHandler) Genesis(c echo.Context) error {
req, err := bindAndValidate[limitOffsetPagination](c)
if err != nil {
return badRequestError(c, err)
}
req.SetDefault()
txs, err := handler.tx.Genesis(c.Request().Context(), req.Limit, req.Offset, pgSort(req.Sort))
if err != nil {
return handleError(c, err, handler.tx)
}
response := make([]responses.Tx, len(txs))
for i := range txs {
response[i] = responses.NewTx(txs[i])
}
return returnArray(c, response)
}
// Namespaces godoc
//
// @Summary List namespaces affected by transaction
// @Description List namespaces affected by transaction
// @Tags transactions
// @ID list-namespaces-transactions
// @Param hash path string true "Transaction hash in hexadecimal" minlength(64) maxlength(64)
// @Param limit query integer false "Count of requested entities" mininum(1) maximum(100)
// @Param offset query integer false "Offset" mininum(1)
// @Produce json
// @Success 200 {array} responses.NamespaceMessage
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/tx/{hash}/namespace [get]
func (handler *TxHandler) Namespaces(c echo.Context) error {
req, err := bindAndValidate[listForTx](c)
if err != nil {
return badRequestError(c, err)
}
req.SetDefault()
hash, err := hex.DecodeString(req.Hash)
if err != nil {
return badRequestError(c, err)
}
txId, err := handler.tx.IdByHash(c.Request().Context(), hash)
if err != nil {
return handleError(c, err, handler.tx)
}
messages, err := handler.namespaces.MessagesByTxId(c.Request().Context(), txId, int(req.Limit), int(req.Offset))
if err != nil {
return handleError(c, err, handler.tx)
}
response := make([]responses.NamespaceMessage, len(messages))
for i := range messages {
response[i], err = responses.NewNamespaceMessage(messages[i])
if err != nil {
return handleError(c, err, handler.tx)
}
}
return returnArray(c, response)
}
// NamespacesCount godoc
//
// @Summary Count of namespaces affected by transaction
// @Description Count of namespaces affected by transaction
// @Tags transactions
// @ID list-namespaces-count-transactions
// @Param hash path string true "Transaction hash in hexadecimal" minlength(64) maxlength(64)
// @Produce json
// @Success 200 {integer} uint64
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/tx/{hash}/namespace/count [get]
func (handler *TxHandler) NamespacesCount(c echo.Context) error {
req, err := bindAndValidate[getTxRequest](c)
if err != nil {
return badRequestError(c, err)
}
hash, err := hex.DecodeString(req.Hash)
if err != nil {
return badRequestError(c, err)
}
txId, err := handler.tx.IdByHash(c.Request().Context(), hash)
if err != nil {
return handleError(c, err, handler.tx)
}
count, err := handler.namespaces.CountMessagesByTxId(c.Request().Context(), txId)
if err != nil {
return handleError(c, err, handler.tx)
}
return c.JSON(http.StatusOK, count)
}
type getBlobsForTx struct {
Hash string `param:"hash" validate:"required,hexadecimal,len=64"`
Limit uint64 `query:"limit" validate:"omitempty,min=1,max=100"`
Offset uint64 `query:"offset" validate:"omitempty,min=0"`
Sort string `query:"sort" validate:"omitempty,oneof=asc desc"`
SortBy string `query:"sort_by" validate:"omitempty,oneof=time size"`
}
func (req *getBlobsForTx) SetDefault() {
if req.Limit == 0 {
req.Limit = 10
}
if req.Sort == "" {
req.Sort = desc
}
}
// Blobs godoc
//
// @Summary List blobs which was pushed by transaction
// @Description List blobs which was pushed by transaction
// @Tags transactions
// @ID list-transaction-blobs
// @Param hash path string true "Transaction hash in hexadecimal" minlength(64) maxlength(64)
// @Param limit query integer false "Count of requested entities" mininum(1) maximum(100)
// @Param offset query integer false "Offset" mininum(1)
// @Param sort query string false "Sort order. Default: desc" Enums(asc, desc)
// @Param sort_by query string false "Sort field. If it's empty internal id is used" Enums(time, size)
// @Produce json
// @Success 200 {array} responses.BlobLog
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/tx/{hash}/blobs [get]
func (handler *TxHandler) Blobs(c echo.Context) error {
req, err := bindAndValidate[getBlobsForTx](c)
if err != nil {
return badRequestError(c, err)
}
req.SetDefault()
hash, err := hex.DecodeString(req.Hash)
if err != nil {
return badRequestError(c, err)
}
txId, err := handler.tx.IdByHash(c.Request().Context(), hash)
if err != nil {
return handleError(c, err, handler.tx)
}
blobs, err := handler.blobLogs.ByTxId(
c.Request().Context(),
txId,
storage.BlobLogFilters{
Limit: int(req.Limit),
Offset: int(req.Offset),
Sort: pgSort(req.Sort),
SortBy: req.SortBy,
},
)
if err != nil {
return handleError(c, err, handler.blobLogs)
}
response := make([]responses.BlobLog, len(blobs))
for i := range blobs {
response[i] = responses.NewBlobLog(blobs[i])
}
return returnArray(c, response)
}
// BlobsCount godoc
//
// @Summary Count of blobs which was pushed by transaction
// @Description Count of blobs which was pushed by transaction
// @Tags transactions
// @ID transaction-blobs-count
// @Param hash path string true "Transaction hash in hexadecimal" minlength(64) maxlength(64)
// @Produce json
// @Success 200 {integer} uint64
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/tx/{hash}/blobs/count [get]
func (handler *TxHandler) BlobsCount(c echo.Context) error {
req, err := bindAndValidate[getTxRequest](c)
if err != nil {
return badRequestError(c, err)
}
hash, err := hex.DecodeString(req.Hash)
if err != nil {
return badRequestError(c, err)
}
txId, err := handler.tx.IdByHash(c.Request().Context(), hash)
if err != nil {
return handleError(c, err, handler.tx)
}
count, err := handler.blobLogs.CountByTxId(c.Request().Context(), txId)
if err != nil {
return handleError(c, err, handler.blobLogs)
}
return c.JSON(http.StatusOK, count)
}