-
Notifications
You must be signed in to change notification settings - Fork 179
/
headers.go
95 lines (77 loc) · 3.49 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// (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)
}
// InsertBlockValidity marks a block as valid or invalid, defined by the consensus algorithm.
func InsertBlockValidity(blockID flow.Identifier, valid bool) func(*badger.Txn) error {
return insert(makePrefix(codeBlockValidity, blockID), valid)
}
// RetrieveBlockValidity returns a block's validity wrt the consensus algorithm.
func RetrieveBlockValidity(blockID flow.Identifier, valid *bool) func(*badger.Txn) error {
return retrieve(makePrefix(codeBlockValidity, blockID), valid)
}
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)
}
func IndexBlockIDByChunkID(chunkID, blockID flow.Identifier) func(*badger.Txn) error {
return insert(makePrefix(codeIndexBlockByChunkID, chunkID), blockID)
}
// BatchIndexBlockByChunkID indexes blockID by chunkID into a batch
func BatchIndexBlockByChunkID(blockID, chunkID flow.Identifier) func(batch *badger.WriteBatch) error {
return batchInsert(makePrefix(codeIndexBlockByChunkID, chunkID), 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)
}
// LookupBlockIDByChunkID looks up a block by a collection within that block.
func LookupBlockIDByChunkID(chunkID flow.Identifier, blockID *flow.Identifier) func(*badger.Txn) error {
return retrieve(makePrefix(codeIndexBlockByChunkID, chunkID), 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
})
}