-
Notifications
You must be signed in to change notification settings - Fork 11
/
block.go
521 lines (463 loc) 路 14.8 KB
/
block.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// SPDX-FileCopyrightText: 2024 PK Lab AG <contact@pklab.io>
// SPDX-License-Identifier: MIT
package handler
import (
"net/http"
"time"
"github.com/celenium-io/celestia-indexer/pkg/node"
"github.com/celenium-io/celestia-indexer/pkg/types"
"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/celestia-app/pkg/da"
"github.com/celestiaorg/go-square/shares"
"github.com/celestiaorg/go-square/square"
"github.com/celenium-io/celestia-indexer/cmd/api/handler/responses"
"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/labstack/echo/v4"
)
type BlockHandler struct {
block storage.IBlock
blockStats storage.IBlockStats
events storage.IEvent
namespace storage.INamespace
blobLogs storage.IBlobLog
message storage.IMessage
state storage.IState
node node.Api
indexerName string
}
func NewBlockHandler(
block storage.IBlock,
blockStats storage.IBlockStats,
events storage.IEvent,
namespace storage.INamespace,
message storage.IMessage,
blobLogs storage.IBlobLog,
state storage.IState,
node node.Api,
indexerName string,
) *BlockHandler {
return &BlockHandler{
block: block,
blockStats: blockStats,
events: events,
namespace: namespace,
blobLogs: blobLogs,
message: message,
state: state,
node: node,
indexerName: indexerName,
}
}
type getBlockByHeightRequest struct {
Height types.Level `param:"height" validate:"min=0"`
}
type getBlockRequest struct {
Height types.Level `param:"height" validate:"min=0"`
Stats bool `query:"stats" validate:"omitempty"`
}
// Get godoc
//
// @Summary Get block info
// @Description Get block info
// @Tags block
// @ID get-block
// @Param height path integer true "Block height" minimum(1)
// @Param stats query boolean false "Need join stats for block"
// @Produce json
// @Success 200 {object} responses.Block
// @Success 204
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/block/{height} [get]
func (handler *BlockHandler) Get(c echo.Context) error {
req, err := bindAndValidate[getBlockRequest](c)
if err != nil {
return badRequestError(c, err)
}
var block storage.Block
if req.Stats {
block, err = handler.block.ByHeightWithStats(c.Request().Context(), req.Height)
} else {
block, err = handler.block.ByHeight(c.Request().Context(), req.Height)
}
if err != nil {
return handleError(c, err, handler.block)
}
return c.JSON(http.StatusOK, responses.NewBlock(block, req.Stats))
}
type blockListRequest struct {
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"`
Stats bool `query:"stats" validate:"omitempty"`
}
func (p *blockListRequest) SetDefault() {
if p.Limit == 0 {
p.Limit = 10
}
if p.Sort == "" {
p.Sort = asc
}
}
// List godoc
//
// @Summary List blocks info
// @Description List blocks info
// @Tags block
// @ID list-block
// @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 stats query boolean false "Need join stats for block"
// @Produce json
// @Success 200 {array} responses.Block
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/block [get]
func (handler *BlockHandler) List(c echo.Context) error {
req, err := bindAndValidate[blockListRequest](c)
if err != nil {
return badRequestError(c, err)
}
req.SetDefault()
var blocks []*storage.Block
if req.Stats {
blocks, err = handler.block.ListWithStats(c.Request().Context(), req.Limit, req.Offset, pgSort(req.Sort))
} else {
blocks, err = handler.block.List(c.Request().Context(), req.Limit, req.Offset, pgSort(req.Sort))
}
if err != nil {
return handleError(c, err, handler.block)
}
response := make([]responses.Block, len(blocks))
for i := range blocks {
response[i] = responses.NewBlock(*blocks[i], req.Stats)
}
return returnArray(c, response)
}
type getBlockEvents struct {
Height types.Level `param:"height" validate:"min=0"`
Limit int `query:"limit" validate:"omitempty,min=1,max=100"`
Offset int `query:"offset" validate:"omitempty,min=0"`
}
func (p *getBlockEvents) SetDefault() {
if p.Limit == 0 {
p.Limit = 10
}
}
// GetEvents godoc
//
// @Summary Get events from begin and end of block
// @Description Get events from begin and end of block
// @Tags block
// @ID get-block-events
// @Param height path integer true "Block height" minimum(1)
// @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/block/{height}/events [get]
func (handler *BlockHandler) GetEvents(c echo.Context) error {
req, err := bindAndValidate[getBlockEvents](c)
if err != nil {
return badRequestError(c, err)
}
req.SetDefault()
blockTime, err := handler.block.Time(c.Request().Context(), req.Height)
if err != nil {
if handler.block.IsNoRows(err) {
return returnArray(c, []any{})
}
return handleError(c, err, handler.block)
}
fltrs := storage.EventFilter{
Limit: req.Limit,
Offset: req.Offset,
Time: blockTime.UTC(),
}
events, err := handler.events.ByBlock(c.Request().Context(), req.Height, fltrs)
if err != nil {
return handleError(c, err, handler.block)
}
response := make([]responses.Event, len(events))
for i := range events {
response[i] = responses.NewEvent(events[i])
}
return returnArray(c, response)
}
// GetStats godoc
//
// @Summary Get block stats by height
// @Description Get block stats by height
// @Tags block
// @ID get-block-stats
// @Param height path integer true "Block height" minimum(1)
// @Produce json
// @Success 200 {object} responses.BlockStats
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/block/{height}/stats [get]
func (handler *BlockHandler) GetStats(c echo.Context) error {
req, err := bindAndValidate[getBlockByHeightRequest](c)
if err != nil {
return badRequestError(c, err)
}
stats, err := handler.blockStats.ByHeight(c.Request().Context(), req.Height)
if err != nil {
return handleError(c, err, handler.block)
}
return c.JSON(http.StatusOK, responses.NewBlockStats(stats))
}
// GetNamespaces godoc
//
// @Summary Get namespaces affected in the block
// @Description Get namespaces affected in the block
// @Tags block
// @ID get-block-namespaces
// @Param height path integer true "Block height" minimum(1)
// @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/block/{height}/namespace [get]
func (handler *BlockHandler) GetNamespaces(c echo.Context) error {
req, err := bindAndValidate[namespacesByHeightRequest](c)
if err != nil {
return badRequestError(c, err)
}
req.SetDefault()
messages, err := handler.namespace.MessagesByHeight(c.Request().Context(), req.Height, int(req.Limit), int(req.Offset))
if err != nil {
return handleError(c, err, handler.block)
}
response := make([]responses.NamespaceMessage, len(messages))
for i := range response {
msg, err := responses.NewNamespaceMessage(messages[i])
if err != nil {
return handleError(c, err, handler.block)
}
response[i] = msg
}
return c.JSON(http.StatusOK, response)
}
// GetNamespacesCount godoc
//
// @Summary Get count of affected in the block namespaces
// @Description Get count of affected in the block namespaces
// @Tags block
// @ID get-block-namespaces-count
// @Param height path integer true "Block height" minimum(1)
// @Produce json
// @Success 200 {integer} uint64
// @Failure 500 {object} Error
// @Router /v1/block/{height}/namespace/count [get]
func (handler *BlockHandler) GetNamespacesCount(c echo.Context) error {
req, err := bindAndValidate[getBlockByHeightRequest](c)
if err != nil {
return badRequestError(c, err)
}
count, err := handler.namespace.CountMessagesByHeight(c.Request().Context(), req.Height)
if err != nil {
return handleError(c, err, handler.block)
}
return c.JSON(http.StatusOK, count)
}
// Count godoc
//
// @Summary Get count of blocks in network
// @Description Get count of blocks in network
// @Tags block
// @ID get-block-count
// @Produce json
// @Success 200 {integer} uint64
// @Failure 500 {object} Error
// @Router /v1/block/count [get]
func (handler *BlockHandler) Count(c echo.Context) error {
state, err := handler.state.ByName(c.Request().Context(), handler.indexerName)
if err != nil {
return handleError(c, err, handler.block)
}
return c.JSON(http.StatusOK, state.LastHeight+1) // + genesis block
}
// GetMessages godoc
//
// @Summary Get messages contained in the block
// @Description Get messages contained in the block
// @Tags block
// @ID get-block-messages
// @Param height path integer true "Block height" minimum(1)
// @Param limit query integer false "Count of requested entities" mininum(1) maximum(100)
// @Param offset query integer false "Offset" mininum(1)
// @Param msg_type query types.MsgType false "Comma-separated message types list"
// @Param excluded_msg_type query types.MsgType false "Comma-separated message types which should be excluded from list"
// @Produce json
// @Success 200 {array} responses.Message
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/block/{height}/messages [get]
func (handler *BlockHandler) GetMessages(c echo.Context) error {
req, err := bindAndValidate[listMessageByBlockRequest](c)
if err != nil {
return badRequestError(c, err)
}
req.SetDefault()
fltrs := storage.MessageListWithTxFilters{
Limit: int(req.Limit),
Offset: int(req.Offset),
Height: req.Height,
MessageTypes: req.MsgType,
ExcludedMessageTypes: req.ExcludedMsgType,
}
messages, err := handler.message.ListWithTx(c.Request().Context(), fltrs)
if err != nil {
return handleError(c, err, handler.block)
}
response := make([]responses.Message, len(messages))
for i := range response {
msg := responses.NewMessageWithTx(messages[i])
response[i] = msg
}
return c.JSON(http.StatusOK, response)
}
type getBlobsForBlock struct {
Height types.Level `param:"height" validate:"min=0"`
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 *getBlobsForBlock) SetDefault() {
if req.Limit == 0 {
req.Limit = 10
}
if req.Sort == "" {
req.Sort = desc
}
}
// Blobs godoc
//
// @Summary List blobs which was pushed in the block
// @Description List blobs which was pushed in the block
// @Tags block
// @ID get-block-blobs
// @Param height path integer true "Block height" minimum(1)
// @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/block/{height}/blobs [get]
func (handler *BlockHandler) Blobs(c echo.Context) error {
req, err := bindAndValidate[getBlobsForBlock](c)
if err != nil {
return badRequestError(c, err)
}
req.SetDefault()
blockTime, err := handler.block.Time(c.Request().Context(), req.Height)
if err != nil {
if handler.block.IsNoRows(err) {
return returnArray(c, []any{})
}
return handleError(c, err, handler.block)
}
blobs, err := handler.blobLogs.ByHeight(
c.Request().Context(),
req.Height,
storage.BlobLogFilters{
Limit: int(req.Limit),
Offset: int(req.Offset),
Sort: pgSort(req.Sort),
SortBy: req.SortBy,
// using time filters to take certain partition
From: blockTime,
To: blockTime.Add(time.Minute),
},
)
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 block
// @ID block-blobs-count
// @Param height path integer true "Block height" minimum(1)
// @Produce json
// @Success 200 {integer} uint64
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/block/{height}/blobs/count [get]
func (handler *BlockHandler) BlobsCount(c echo.Context) error {
req, err := bindAndValidate[getBlockByHeightRequest](c)
if err != nil {
return badRequestError(c, err)
}
count, err := handler.blobLogs.CountByHeight(c.Request().Context(), req.Height)
if err != nil {
return handleError(c, err, handler.blobLogs)
}
return c.JSON(http.StatusOK, count)
}
// BlockODS godoc
//
// @Summary ODS for block
// @Description ODS for block
// @Tags block
// @ID block-ods
// @Param height path integer true "Block height" minimum(1)
// @Produce json
// @Success 200 {object} responses.ODS
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/block/{height}/ods [get]
func (handler *BlockHandler) BlockODS(c echo.Context) error {
req, err := bindAndValidate[getBlockByHeightRequest](c)
if err != nil {
return badRequestError(c, err)
}
blockStats, err := handler.blockStats.ByHeight(c.Request().Context(), req.Height)
if err != nil {
return handleError(c, err, handler.block)
}
if blockStats.TxCount == 0 {
return c.JSON(http.StatusOK, responses.ODS{
Width: 0,
Items: make([]responses.ODSItem, 0),
})
}
block, err := handler.node.Block(c.Request().Context(), req.Height)
if err != nil {
return handleError(c, err, handler.block)
}
dataSquare, err := square.Construct(
block.Block.Data.Txs.ToSliceOfBytes(),
appconsts.SquareSizeUpperBound(0),
appconsts.SubtreeRootThreshold(0),
)
if err != nil {
return internalServerError(c, err)
}
eds, err := da.ExtendShares(shares.ToBytes(dataSquare))
if err != nil {
return internalServerError(c, err)
}
ods, err := responses.NewODS(eds)
if err != nil {
return internalServerError(c, err)
}
return c.JSON(http.StatusOK, ods)
}