-
Notifications
You must be signed in to change notification settings - Fork 179
/
events.go
56 lines (45 loc) · 1.63 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
package rest
import (
"fmt"
"github.com/onflow/flow-go/engine/access/rest/models"
"github.com/onflow/flow-go/engine/access/rest/request"
"github.com/onflow/flow-go/access"
)
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, 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)
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, 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)
if err != nil {
return nil, err
}
blocksEvents.Build(events)
return blocksEvents, nil
}