forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution_events_server.go
273 lines (246 loc) · 7.95 KB
/
execution_events_server.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
package rpcevents
import (
"context"
"fmt"
"io"
"github.com/gogo/protobuf/proto"
bcm "github.com/hyperledger/burrow/bcm"
"github.com/hyperledger/burrow/event"
"github.com/hyperledger/burrow/event/query"
"github.com/hyperledger/burrow/execution/exec"
"github.com/hyperledger/burrow/logging"
"google.golang.org/grpc"
)
const SubscribeBufferSize = 100
type Provider interface {
// Get a particular BlockExecution
GetBlock(height uint64) (*exec.BlockExecution, error)
// Get a partiualr TxExecution by hash
GetTx(txHash []byte) (*exec.TxExecution, error)
// Get events between startKey (inclusive) and endKey (exclusive) - i.e. the half open interval [start, end)
GetBlocks(startHeight, endHeight uint64, consumer func(*exec.BlockExecution) (stop bool)) (stopped bool, err error)
}
type executionEventsServer struct {
eventsProvider Provider
subscribable event.Subscribable
tip bcm.BlockchainInfo
logger *logging.Logger
}
func NewExecutionEventsServer(eventsProvider Provider, subscribable event.Subscribable,
tip bcm.BlockchainInfo, logger *logging.Logger) ExecutionEventsServer {
return &executionEventsServer{
eventsProvider: eventsProvider,
subscribable: subscribable,
tip: tip,
logger: logger.WithScope("NewExecutionEventsServer"),
}
}
func (ees *executionEventsServer) GetBlock(ctx context.Context, request *GetBlockRequest) (*exec.BlockExecution, error) {
be, err := ees.eventsProvider.GetBlock(request.GetHeight())
if err != nil {
return nil, err
}
if be != nil {
return be, nil
}
if !request.Wait {
if ees.tip.LastBlockHeight() < request.Height {
return nil, fmt.Errorf("block at height %v not yet produced (last block height: %v)",
request.Height, ees.tip.LastBlockHeight())
}
return nil, fmt.Errorf("block at height %v not found in state but should have been! (last block height: %v)",
request.Height, ees.tip.LastBlockHeight())
}
err = ees.streamBlocks(ctx, &BlockRange{End: StreamBound()}, func(block *exec.BlockExecution) error {
if block.Height == request.Height {
be = block
return io.EOF
}
return nil
})
if err != io.EOF {
return nil, err
}
return be, nil
}
func (ees *executionEventsServer) GetBlocks(request *BlocksRequest, stream ExecutionEvents_GetBlocksServer) error {
qry, err := query.NewBuilder(request.Query).Query()
if err != nil {
return fmt.Errorf("could not parse BlockExecution query: %v", err)
}
return ees.streamBlocks(stream.Context(), request.BlockRange, func(block *exec.BlockExecution) error {
if qry.Matches(block.Tagged()) {
return flush(stream, block)
}
return nil
})
}
func (ees *executionEventsServer) GetTx(ctx context.Context, request *GetTxRequest) (*exec.TxExecution, error) {
txe, err := ees.eventsProvider.GetTx(request.TxHash)
if err != nil {
return nil, err
}
if txe != nil {
return txe, nil
}
if !request.Wait {
return nil, fmt.Errorf("transaction with hash %v not found in state", request.TxHash)
}
subID := event.GenSubID()
out, err := ees.subscribable.Subscribe(ctx, subID, exec.QueryForTxExecution(request.TxHash), SubscribeBufferSize)
if err != nil {
return nil, err
}
defer ees.subscribable.UnsubscribeAll(ctx, subID)
for msg := range out {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
return msg.(*exec.TxExecution), nil
}
}
return nil, fmt.Errorf("subscription waiting for tx %v ended prematurely", request.TxHash)
}
func (ees *executionEventsServer) GetTxs(request *BlocksRequest, stream ExecutionEvents_GetTxsServer) error {
qry, err := query.NewBuilder(request.Query).Query()
if err != nil {
return fmt.Errorf("could not parse TxExecution query: %v", err)
}
return ees.streamBlocks(stream.Context(), request.BlockRange, func(block *exec.BlockExecution) error {
txs := filterTxs(block, qry)
if len(txs) > 0 {
response := &GetTxsResponse{
Height: block.Height,
TxExecutions: txs,
}
return flush(stream, response)
}
return nil
})
}
func (ees *executionEventsServer) GetEvents(request *BlocksRequest, stream ExecutionEvents_GetEventsServer) error {
qry, err := query.NewBuilder(request.Query).Query()
if err != nil {
return fmt.Errorf("could not parse Event query: %v", err)
}
return ees.streamBlocks(stream.Context(), request.BlockRange, func(block *exec.BlockExecution) error {
evs := filterEvents(block, qry)
if len(evs) == 0 {
return nil
}
response := &GetEventsResponse{
Height: block.Height,
Events: evs,
}
return flush(stream, response)
})
}
func (ees *executionEventsServer) streamBlocks(ctx context.Context, blockRange *BlockRange,
consumer func(*exec.BlockExecution) error) error {
// Converts the bounds to half-open interval needed
start, end, streaming := blockRange.Bounds(ees.tip.LastBlockHeight())
ees.logger.TraceMsg("Streaming blocks", "start", start, "end", end, "streaming", streaming)
// Pull blocks from state and receive the upper bound (exclusive) on the what we were able to send
// Set this to start since it will be the start of next streaming batch (if needed)
start, err := ees.iterateBlocks(start, end, consumer)
// If we are not streaming and all blocks requested were retrieved from state then we are done
if !streaming && start == end {
return err
}
// Otherwise we need to begin streaming blocks as they are produced
subID := event.GenSubID()
// Subscribe to BlockExecution events
out, err := ees.subscribable.Subscribe(ctx, subID, exec.QueryForBlockExecutionFromHeight(end),
SubscribeBufferSize)
if err != nil {
return err
}
defer ees.subscribable.UnsubscribeAll(context.Background(), subID)
for msg := range out {
select {
case <-ctx.Done():
return ctx.Err()
default:
block := msg.(*exec.BlockExecution)
streamEnd := block.Height
finished := !streaming && streamEnd >= end
if finished {
// Truncate streamEnd to final end to get exactly the blocks we want from state
streamEnd = end
}
if start < streamEnd {
// This implies there are some blocks between the previous batchEnd (now start) and the current BlockExecution that
// we have not emitted so we will pull them from state. This can occur if a block is emitted during/after
// the initial streaming but before we have subscribed to block events or if we spill BlockExecutions
// when streaming them and need to catch up
_, err := ees.iterateBlocks(start, streamEnd, consumer)
if err != nil {
return err
}
}
if finished {
return nil
}
err = consumer(block)
if err != nil {
return err
}
// We've just streamed block so our next start marker is the next block
start = block.Height + 1
}
}
return nil
}
// Converts blocks into responses and streams them returning the height one greater than the last seen block
// that can be used as next start point (half-open interval)
func (ees *executionEventsServer) iterateBlocks(start, end uint64, consumer func(*exec.BlockExecution) error) (uint64, error) {
var streamErr error
var lastHeightSeen uint64
_, err := ees.eventsProvider.GetBlocks(start, end,
func(be *exec.BlockExecution) (stop bool) {
lastHeightSeen = be.Height
streamErr = consumer(be)
if streamErr != nil {
return true
}
return false
})
if err != nil {
return 0, err
}
if streamErr != nil {
return 0, streamErr
}
// Returns the appropriate starting block for the next stream
return lastHeightSeen + 1, nil
}
func filterTxs(be *exec.BlockExecution, qry query.Query) []*exec.TxExecution {
var txs []*exec.TxExecution
for _, txe := range be.TxExecutions {
if qry.Matches(txe.Tagged()) {
txs = append(txs, txe)
}
}
return txs
}
func filterEvents(be *exec.BlockExecution, qry query.Query) []*exec.Event {
var evs []*exec.Event
for _, txe := range be.TxExecutions {
for _, ev := range txe.Events {
if qry.Matches(ev.Tagged()) {
evs = append(evs, ev)
}
}
}
return evs
}
func flush(stream grpc.Stream, buf proto.Message) error {
if buf != nil {
err := stream.SendMsg(buf)
if err != nil {
return err
}
}
return nil
}