-
Notifications
You must be signed in to change notification settings - Fork 175
/
headers.go
77 lines (63 loc) · 2.63 KB
/
headers.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
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package operation
import (
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/model/flow"
)
func InsertHeader(headerID flow.Identifier, header *flow.Header) func(*badger.Txn) error {
return insert(makePrefix(codeHeader, headerID), header)
}
func RetrieveHeader(blockID flow.Identifier, header *flow.Header) func(*badger.Txn) error {
return retrieve(makePrefix(codeHeader, blockID), header)
}
// IndexBlockHeight indexes the height of a block. It should only be called on
// finalized blocks.
func IndexBlockHeight(height uint64, blockID flow.Identifier) func(*badger.Txn) error {
return insert(makePrefix(codeHeightToBlock, height), blockID)
}
// LookupBlockHeight retrieves finalized blocks by height.
func LookupBlockHeight(height uint64, blockID *flow.Identifier) func(*badger.Txn) error {
return retrieve(makePrefix(codeHeightToBlock, height), blockID)
}
// BlockExists checks whether the block exists in the database.
// No errors are expected during normal operation.
func BlockExists(blockID flow.Identifier, blockExists *bool) func(*badger.Txn) error {
return exists(makePrefix(codeHeader, blockID), blockExists)
}
func InsertExecutedBlock(blockID flow.Identifier) func(*badger.Txn) error {
return insert(makePrefix(codeExecutedBlock), blockID)
}
func UpdateExecutedBlock(blockID flow.Identifier) func(*badger.Txn) error {
return update(makePrefix(codeExecutedBlock), blockID)
}
func RetrieveExecutedBlock(blockID *flow.Identifier) func(*badger.Txn) error {
return retrieve(makePrefix(codeExecutedBlock), blockID)
}
// IndexCollectionBlock indexes a block by a collection within that block.
func IndexCollectionBlock(collID flow.Identifier, blockID flow.Identifier) func(*badger.Txn) error {
return insert(makePrefix(codeCollectionBlock, collID), blockID)
}
// LookupCollectionBlock looks up a block by a collection within that block.
func LookupCollectionBlock(collID flow.Identifier, blockID *flow.Identifier) func(*badger.Txn) error {
return retrieve(makePrefix(codeCollectionBlock, collID), blockID)
}
// FindHeaders iterates through all headers, calling `filter` on each, and adding
// them to the `found` slice if `filter` returned true
func FindHeaders(filter func(header *flow.Header) bool, found *[]flow.Header) func(*badger.Txn) error {
return traverse(makePrefix(codeHeader), func() (checkFunc, createFunc, handleFunc) {
check := func(key []byte) bool {
return true
}
var val flow.Header
create := func() interface{} {
return &val
}
handle := func() error {
if filter(&val) {
*found = append(*found, val)
}
return nil
}
return check, create, handle
})
}