-
Notifications
You must be signed in to change notification settings - Fork 175
/
events.go
68 lines (57 loc) · 1.84 KB
/
events.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
package routes
import (
"fmt"
entitiesproto "github.com/onflow/flow/protobuf/go/flow/entities"
"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/engine/access/rest/models"
"github.com/onflow/flow-go/engine/access/rest/request"
)
const BlockQueryParam = "block_ids"
const EventTypeQuery = "type"
// GetEvents for the provided block range or list of block IDs filtered by type.
func GetEvents(r *request.Request, backend access.API, _ models.LinkGenerator) (interface{}, error) {
req, err := r.GetEventsRequest()
if err != nil {
return nil, models.NewBadRequestError(err)
}
// if the request has block IDs provided then return events for block IDs
var blocksEvents models.BlocksEvents
if len(req.BlockIDs) > 0 {
events, err := backend.GetEventsForBlockIDs(
r.Context(),
req.Type,
req.BlockIDs,
entitiesproto.EventEncodingVersion_JSON_CDC_V0,
)
if err != nil {
return nil, err
}
blocksEvents.Build(events)
return blocksEvents, nil
}
// if end height is provided with special values then load the height
if req.EndHeight == request.FinalHeight || req.EndHeight == request.SealedHeight {
latest, _, err := backend.GetLatestBlockHeader(r.Context(), req.EndHeight == request.SealedHeight)
if err != nil {
return nil, err
}
req.EndHeight = latest.Height
// special check after we resolve special height value
if req.StartHeight > req.EndHeight {
return nil, models.NewBadRequestError(fmt.Errorf("current retrieved end height value is lower than start height"))
}
}
// if request provided block height range then return events for that range
events, err := backend.GetEventsForHeightRange(
r.Context(),
req.Type,
req.StartHeight,
req.EndHeight,
entitiesproto.EventEncodingVersion_JSON_CDC_V0,
)
if err != nil {
return nil, err
}
blocksEvents.Build(events)
return blocksEvents, nil
}