-
Notifications
You must be signed in to change notification settings - Fork 179
/
handler.go
163 lines (138 loc) · 4.75 KB
/
handler.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
package state_stream
import (
"context"
"sync/atomic"
access "github.com/onflow/flow/protobuf/go/flow/executiondata"
executiondata "github.com/onflow/flow/protobuf/go/flow/executiondata"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/onflow/flow-go/engine/common/rpc"
"github.com/onflow/flow-go/engine/common/rpc/convert"
"github.com/onflow/flow-go/model/flow"
)
type Handler struct {
api API
chain flow.Chain
eventFilterConfig EventFilterConfig
maxStreams int32
streamCount atomic.Int32
}
func NewHandler(api API, chain flow.Chain, conf EventFilterConfig, maxGlobalStreams uint32) *Handler {
h := &Handler{
api: api,
chain: chain,
eventFilterConfig: conf,
maxStreams: int32(maxGlobalStreams),
streamCount: atomic.Int32{},
}
return h
}
func (h *Handler) GetExecutionDataByBlockID(ctx context.Context, request *access.GetExecutionDataByBlockIDRequest) (*access.GetExecutionDataByBlockIDResponse, error) {
blockID, err := convert.BlockID(request.GetBlockId())
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "could not convert block ID: %v", err)
}
execData, err := h.api.GetExecutionDataByBlockID(ctx, blockID)
if err != nil {
return nil, rpc.ConvertError(err, "could no get execution data", codes.Internal)
}
message, err := convert.BlockExecutionDataToMessage(execData)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not convert execution data to entity: %v", err)
}
return &access.GetExecutionDataByBlockIDResponse{BlockExecutionData: message}, nil
}
func (h *Handler) SubscribeExecutionData(request *access.SubscribeExecutionDataRequest, stream access.ExecutionDataAPI_SubscribeExecutionDataServer) error {
// check if the maximum number of streams is reached
if h.streamCount.Load() >= h.maxStreams {
return status.Errorf(codes.ResourceExhausted, "maximum number of streams reached")
}
h.streamCount.Add(1)
defer h.streamCount.Add(-1)
startBlockID := flow.ZeroID
if request.GetStartBlockId() != nil {
blockID, err := convert.BlockID(request.GetStartBlockId())
if err != nil {
return status.Errorf(codes.InvalidArgument, "could not convert start block ID: %v", err)
}
startBlockID = blockID
}
sub := h.api.SubscribeExecutionData(stream.Context(), startBlockID, request.GetStartBlockHeight())
for {
v, ok := <-sub.Channel()
if !ok {
if sub.Err() != nil {
return rpc.ConvertError(sub.Err(), "stream encountered an error", codes.Internal)
}
return nil
}
resp, ok := v.(*ExecutionDataResponse)
if !ok {
return status.Errorf(codes.Internal, "unexpected response type: %T", v)
}
execData, err := convert.BlockExecutionDataToMessage(resp.ExecutionData)
if err != nil {
return status.Errorf(codes.Internal, "could not convert execution data to entity: %v", err)
}
err = stream.Send(&executiondata.SubscribeExecutionDataResponse{
BlockHeight: resp.Height,
BlockExecutionData: execData,
})
if err != nil {
return rpc.ConvertError(err, "could not send response", codes.Internal)
}
}
}
func (h *Handler) SubscribeEvents(request *access.SubscribeEventsRequest, stream access.ExecutionDataAPI_SubscribeEventsServer) error {
// check if the maximum number of streams is reached
if h.streamCount.Load() >= h.maxStreams {
return status.Errorf(codes.ResourceExhausted, "maximum number of streams reached")
}
h.streamCount.Add(1)
defer h.streamCount.Add(-1)
startBlockID := flow.ZeroID
if request.GetStartBlockId() != nil {
blockID, err := convert.BlockID(request.GetStartBlockId())
if err != nil {
return status.Errorf(codes.InvalidArgument, "could not convert start block ID: %v", err)
}
startBlockID = blockID
}
filter := EventFilter{}
if request.GetFilter() != nil {
var err error
reqFilter := request.GetFilter()
filter, err = NewEventFilter(
h.eventFilterConfig,
h.chain,
reqFilter.GetEventType(),
reqFilter.GetAddress(),
reqFilter.GetContract(),
)
if err != nil {
return status.Errorf(codes.InvalidArgument, "invalid event filter: %v", err)
}
}
sub := h.api.SubscribeEvents(stream.Context(), startBlockID, request.GetStartBlockHeight(), filter)
for {
v, ok := <-sub.Channel()
if !ok {
if sub.Err() != nil {
return rpc.ConvertError(sub.Err(), "stream encountered an error", codes.Internal)
}
return nil
}
resp, ok := v.(*EventsResponse)
if !ok {
return status.Errorf(codes.Internal, "unexpected response type: %T", v)
}
err := stream.Send(&executiondata.SubscribeEventsResponse{
BlockHeight: resp.Height,
BlockId: convert.IdentifierToMessage(resp.BlockID),
Events: convert.EventsToMessages(resp.Events),
})
if err != nil {
return rpc.ConvertError(err, "could not send response", codes.Internal)
}
}
}