-
Notifications
You must be signed in to change notification settings - Fork 178
/
traverse.go
73 lines (63 loc) · 2.16 KB
/
traverse.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
package state
import (
"fmt"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/storage"
)
// functor that will be called on each block header when traversing blocks.
type onVisitBlock = func(header *flow.Header) error
// functor that will be called on each block header to know if we should
// continue traversing the chain (specifically, visit the block's parent)
type shouldVisitParent = func(block *flow.Header) bool
// TraverseBackward traverses a chain segment beginning with the start block (inclusive)
// Blocks are traversed in reverse
// height order, meaning the end block must be an ancestor of the start block.
// The callback is called for each block in this segment.
// Return value of callback is used to decide if it should continue or not.
func TraverseBackward(headers storage.Headers, startBlockID flow.Identifier, visitor onVisitBlock, shouldVisitParent shouldVisitParent) error {
blockID := startBlockID
// in case we reached genesis
for {
block, err := headers.ByBlockID(blockID)
if err != nil {
return fmt.Errorf("could not get block header (%x): %w", blockID, err)
}
err = visitor(block)
if err != nil {
return err
}
if !shouldVisitParent(block) {
return nil
}
blockID = block.ParentID
}
}
// TraverseForward traverses a chain segment in forward order.
// The algorithm starts at the `forkHead` and walks the chain backwards towards
// the genesis block. The descend continues as long as `shouldVisitParent` returns
// true. Starting with the first block where `shouldVisitParent` returned false,
// the visited blocks are fed into `visitor` in a forward order (order of
// increasing height). The last block that is fed into `visitor` is `forkHead`.
func TraverseForward(headers storage.Headers,
forkHead flow.Identifier,
visitor onVisitBlock,
shouldVisitParent shouldVisitParent,
) error {
var blocks []*flow.Header
err := TraverseBackward(headers, forkHead, func(header *flow.Header) error {
blocks = append(blocks, header)
return nil
}, shouldVisitParent)
if err != nil {
return err
}
i := len(blocks) - 1
for i >= 0 {
err = visitor(blocks[i])
i--
if err != nil {
return err
}
}
return nil
}