-
Notifications
You must be signed in to change notification settings - Fork 147
/
kv.go
559 lines (454 loc) · 13.6 KB
/
kv.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package kv
import (
"context"
"errors"
"fmt"
"math/big"
"sort"
"strings"
ds "github.com/ipfs/go-datastore"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/pubsub/query"
"github.com/cometbft/cometbft/libs/pubsub/query/syntax"
"github.com/cometbft/cometbft/types"
"github.com/rollkit/rollkit/state"
"github.com/rollkit/rollkit/state/indexer"
"github.com/rollkit/rollkit/store"
)
var _ indexer.BlockIndexer = (*BlockerIndexer)(nil)
// BlockerIndexer implements a block indexer, indexing BeginBlock and EndBlock
// events with an underlying KV store. Block events are indexed by their height,
// such that matching search criteria returns the respective block height(s).
type BlockerIndexer struct {
store ds.TxnDatastore
ctx context.Context
}
func New(ctx context.Context, store ds.TxnDatastore) *BlockerIndexer {
return &BlockerIndexer{
store: store,
ctx: ctx,
}
}
// Has returns true if the given height has been indexed. An error is returned
// upon database query failure.
func (idx *BlockerIndexer) Has(height int64) (bool, error) {
_, err := idx.store.Get(idx.ctx, ds.NewKey(heightKey(height)))
if err == ds.ErrNotFound {
return false, nil
}
return err == nil, err
}
// Index indexes BeginBlock and EndBlock events for a given block by its height.
// The following is indexed:
//
// primary key: encode(block.height | height) => encode(height)
// BeginBlock events: encode(eventType.eventAttr|eventValue|height|begin_block) => encode(height)
// EndBlock events: encode(eventType.eventAttr|eventValue|height|end_block) => encode(height)
func (idx *BlockerIndexer) Index(bh types.EventDataNewBlockEvents) error {
batch, err := idx.store.NewTransaction(idx.ctx, false)
if err != nil {
return fmt.Errorf("failed to create a new batch for transaction: %w", err)
}
defer batch.Discard(idx.ctx)
height := bh.Height
// 1. index by height
if err := batch.Put(idx.ctx, ds.NewKey(heightKey(height)), int64ToBytes(height)); err != nil {
return err
}
// 2. index BeginBlock events
if err := idx.indexEvents(batch, bh.Events, "begin_block", height); err != nil {
return fmt.Errorf("failed to index BeginBlock events: %w", err)
}
// 3. index EndBlock events
if err := idx.indexEvents(batch, bh.Events, "end_block", height); err != nil {
return fmt.Errorf("failed to index EndBlock events: %w", err)
}
return batch.Commit(idx.ctx)
}
// Search performs a query for block heights that match a given BeginBlock
// and Endblock event search criteria. The given query can match against zero,
// one or more block heights. In the case of height queries, i.e. block.height=H,
// if the height is indexed, that height alone will be returned. An error and
// nil slice is returned. Otherwise, a non-nil slice and nil error is returned.
func (idx *BlockerIndexer) Search(ctx context.Context, q *query.Query) ([]int64, error) {
results := make([]int64, 0)
select {
case <-ctx.Done():
return results, nil
default:
}
conditions := q.Syntax()
// conditions to skip because they're handled before "everything else"
skipIndexes := make([]int, 0)
var ok bool
var heightInfo HeightInfo
// If we are not matching events and block.height occurs more than once, the later value will
// overwrite the first one.
conditions, heightInfo, ok = dedupHeight(conditions)
// Extract ranges. If both upper and lower bounds exist, it's better to get
// them in order as to not iterate over kvs that are not within range.
ranges, rangeIndexes, heightRange := indexer.LookForRangesWithHeight(conditions)
heightInfo.heightRange = heightRange
// If we have additional constraints and want to query per event
// attributes, we cannot simply return all blocks for a height.
// But we remember the height we want to find and forward it to
// match(). If we only have the height constraint
// in the query (the second part of the ||), we don't need to query
// per event conditions and return all events within the height range.
if ok && heightInfo.onlyHeightEq {
ok, err := idx.Has(heightInfo.height)
if err != nil {
return nil, err
}
if ok {
return []int64{heightInfo.height}, nil
}
return results, nil
}
var heightsInitialized bool
filteredHeights := make(map[string][]byte)
var err error
if heightInfo.heightEqIdx != -1 {
skipIndexes = append(skipIndexes, heightInfo.heightEqIdx)
}
if len(ranges) > 0 {
skipIndexes = append(skipIndexes, rangeIndexes...)
for _, qr := range ranges {
// If we have a query range over height and want to still look for
// specific event values we do not want to simply return all
// blocks in this height range. We remember the height range info
// and pass it on to match() to take into account when processing events.
if qr.Key == types.BlockHeightKey && !heightInfo.onlyHeightRange {
// If the query contains ranges other than the height then we need to treat the height
// range when querying the conditions of the other range.
// Otherwise we can just return all the blocks within the height range (as there is no
// additional constraint on events)
continue
}
if !heightsInitialized {
filteredHeights, err = idx.matchRange(ctx, qr, ds.NewKey(qr.Key).String(), filteredHeights, true, heightInfo)
if err != nil {
return nil, err
}
heightsInitialized = true
// Ignore any remaining conditions if the first condition resulted in no
// matches (assuming implicit AND operand).
if len(filteredHeights) == 0 {
break
}
} else {
filteredHeights, err = idx.matchRange(ctx, qr, ds.NewKey(qr.Key).String(), filteredHeights, false, heightInfo)
if err != nil {
return nil, err
}
}
}
}
// for all other conditions
for i, c := range conditions {
if intInSlice(i, skipIndexes) {
continue
}
startKey := store.GenerateKey([]string{c.Tag, c.Arg.Value()})
if !heightsInitialized {
filteredHeights, err = idx.match(ctx, c, ds.NewKey(startKey).String(), filteredHeights, true)
if err != nil {
return nil, err
}
heightsInitialized = true
// Ignore any remaining conditions if the first condition resulted in no
// matches (assuming implicit AND operand).
if len(filteredHeights) == 0 {
break
}
} else {
filteredHeights, err = idx.match(ctx, c, ds.NewKey(string(startKey)).String(), filteredHeights, false)
if err != nil {
return nil, err
}
}
}
// fetch matching heights
results = make([]int64, 0, len(filteredHeights))
resultMap := make(map[int64]struct{})
for _, hBz := range filteredHeights {
cont := true
h := int64FromBytes(hBz)
ok, err := idx.Has(h)
if err != nil {
return nil, err
}
if ok {
if _, ok := resultMap[h]; !ok {
resultMap[h] = struct{}{}
results = append(results, h)
}
}
select {
case <-ctx.Done():
cont = false
default:
}
if !cont {
break
}
}
sort.Slice(results, func(i, j int) bool { return results[i] < results[j] })
return results, nil
}
// matchRange returns all matching block heights that match a given QueryRange
// and start key. An already filtered result (filteredHeights) is provided such
// that any non-intersecting matches are removed.
//
// NOTE: The provided filteredHeights may be empty if no previous condition has
// matched.
func (idx *BlockerIndexer) matchRange(
ctx context.Context,
qr indexer.QueryRange,
startKey string,
filteredHeights map[string][]byte,
firstRun bool,
heightInfo HeightInfo,
) (map[string][]byte, error) {
// A previous match was attempted but resulted in no matches, so we return
// no matches (assuming AND operand).
if !firstRun && len(filteredHeights) == 0 {
return filteredHeights, nil
}
tmpHeights := make(map[string][]byte)
// lowerBound := qr.LowerBoundValue()
// upperBound := qr.UpperBoundValue()
results, err := store.PrefixEntries(ctx, idx.store, startKey)
if err != nil {
return nil, err
}
LOOP:
for result := range results.Next() {
cont := true
var (
// v float64
eventValue string
err error
)
if qr.Key == types.BlockHeightKey {
eventValue = parseValueFromPrimaryKey(result.Entry.Key)
} else {
eventValue = parseValueFromEventKey(result.Entry.Key)
}
if err != nil {
continue
}
if _, ok := qr.AnyBound().(*big.Float); ok {
// // include := true
// if lowerBound != nil {
// lF, _ := lowerBound.(*big.Float).Float64()
// if v < lF {
// include = false
// }
// }
// if upperBound != nil {
// uF, _ := upperBound.(*big.Float).Float64()
// if v > uF {
// include = false
// }
// }
v := new(big.Int)
v, ok := v.SetString(eventValue, 10)
var vF *big.Float
if !ok {
// The precision here is 125. For numbers bigger than this, the value
// will not be parsed properly
vF, _, err = big.ParseFloat(eventValue, 10, 125, big.ToNearestEven)
if err != nil {
continue LOOP
}
}
if qr.Key != types.BlockHeightKey {
keyHeight, err := parseHeightFromEventKey(result.Entry.Key)
if err != nil {
continue LOOP
}
withinHeight, err := checkHeightConditions(heightInfo, keyHeight)
if err != nil {
continue LOOP
}
if !withinHeight {
continue LOOP
}
}
var withinBounds bool
var err error
if !ok {
withinBounds, err = state.CheckBounds(qr, vF)
} else {
withinBounds, err = state.CheckBounds(qr, v)
}
if err != nil {
} else {
if withinBounds {
tmpHeights[string(result.Entry.Value)] = result.Entry.Value
}
}
}
select {
case <-ctx.Done():
cont = false
default:
}
if !cont {
break
}
}
if len(tmpHeights) == 0 || firstRun {
// Either:
//
// 1. Regardless if a previous match was attempted, which may have had
// results, but no match was found for the current condition, then we
// return no matches (assuming AND operand).
//
// 2. A previous match was not attempted, so we return all results.
return tmpHeights, nil
}
// Remove/reduce matches in filteredHashes that were not found in this
// match (tmpHashes).
for k := range filteredHeights {
cont := true
if tmpHeights[k] == nil {
delete(filteredHeights, k)
select {
case <-ctx.Done():
cont = false
default:
}
}
if !cont {
break
}
}
return filteredHeights, nil
}
// match returns all matching heights that meet a given query condition and start
// key. An already filtered result (filteredHeights) is provided such that any
// non-intersecting matches are removed.
//
// NOTE: The provided filteredHeights may be empty if no previous condition has
// matched.
func (idx *BlockerIndexer) match(
ctx context.Context,
c syntax.Condition,
startKeyBz string,
filteredHeights map[string][]byte,
firstRun bool,
) (map[string][]byte, error) {
// A previous match was attempted but resulted in no matches, so we return
// no matches (assuming AND operand).
if !firstRun && len(filteredHeights) == 0 {
return filteredHeights, nil
}
tmpHeights := make(map[string][]byte)
switch {
case c.Op == syntax.TEq:
results, err := store.PrefixEntries(ctx, idx.store, startKeyBz)
if err != nil {
return nil, err
}
for result := range results.Next() {
tmpHeights[string(result.Entry.Value)] = result.Entry.Value
if err := ctx.Err(); err != nil {
break
}
}
case c.Op == syntax.TExists:
results, err := store.PrefixEntries(ctx, idx.store, ds.NewKey(c.Tag).String())
if err != nil {
return nil, err
}
for result := range results.Next() {
cont := true
tmpHeights[string(result.Entry.Value)] = result.Entry.Value
select {
case <-ctx.Done():
cont = false
default:
}
if !cont {
break
}
}
case c.Op == syntax.TContains:
results, err := store.PrefixEntries(ctx, idx.store, ds.NewKey(c.Tag).String())
if err != nil {
return nil, err
}
for result := range results.Next() {
cont := true
eventValue := parseValueFromEventKey(result.Entry.Key)
if strings.Contains(eventValue, c.Arg.Value()) {
tmpHeights[string(result.Entry.Value)] = result.Entry.Value
}
select {
case <-ctx.Done():
cont = false
default:
}
if !cont {
break
}
}
default:
return nil, errors.New("other operators should be handled already")
}
if len(tmpHeights) == 0 || firstRun {
// Either:
//
// 1. Regardless if a previous match was attempted, which may have had
// results, but no match was found for the current condition, then we
// return no matches (assuming AND operand).
//
// 2. A previous match was not attempted, so we return all results.
return tmpHeights, nil
}
// Remove/reduce matches in filteredHeights that were not found in this
// match (tmpHeights).
for k := range filteredHeights {
cont := true
if tmpHeights[k] == nil {
delete(filteredHeights, k)
select {
case <-ctx.Done():
cont = false
default:
}
}
if !cont {
break
}
}
return filteredHeights, nil
}
func (idx *BlockerIndexer) indexEvents(batch ds.Txn, events []abci.Event, typ string, height int64) error {
heightBz := int64ToBytes(height)
for _, event := range events {
// only index events with a non-empty type
if len(event.Type) == 0 {
continue
}
for _, attr := range event.Attributes {
if len(attr.Key) == 0 {
continue
}
// index iff the event specified index:true and it's not a reserved event
compositeKey := fmt.Sprintf("%s.%s", event.Type, string(attr.Key))
if compositeKey == types.BlockHeightKey {
return fmt.Errorf("event type and attribute key \"%s\" is reserved; please use a different key", compositeKey)
}
if attr.GetIndex() {
key := eventKey(compositeKey, typ, string(attr.Value), height)
if err := batch.Put(idx.ctx, ds.NewKey(key), heightBz); err != nil {
return err
}
}
}
}
return nil
}