/
children_traverser.go
164 lines (128 loc) · 4.32 KB
/
children_traverser.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
package dag
import (
"container/list"
"context"
"fmt"
"sync"
"github.com/pkg/errors"
"github.com/iotaledger/hive.go/runtime/contextutils"
"github.com/iotaledger/hornet/v2/pkg/common"
iotago "github.com/iotaledger/iota.go/v3"
)
// ChildrenTraverser can be used to walk the dag in direction of the tips (future cone).
type ChildrenTraverser struct {
// interface to the used storage.
childrenTraverserStorage ChildrenTraverserStorage
// stack holding the ordered blocks to process.
stack *list.List
// discovers map with already found blocks.
discovered map[iotago.BlockID]struct{}
ctx context.Context
condition Predicate
consumer Consumer
walkAlreadyDiscovered bool
traverserLock sync.Mutex
}
// NewChildrenTraverser create a new traverser to traverse the children (future cone).
func NewChildrenTraverser(childrenTraverserStorage ChildrenTraverserStorage) *ChildrenTraverser {
t := &ChildrenTraverser{
childrenTraverserStorage: childrenTraverserStorage,
stack: list.New(),
discovered: make(map[iotago.BlockID]struct{}),
}
return t
}
// reset the traverser for the next walk.
func (t *ChildrenTraverser) reset() {
t.discovered = make(map[iotago.BlockID]struct{})
t.stack = list.New()
}
// Traverse starts to traverse the children (future cone) of the given start block until
// the traversal stops due to no more blocks passing the given condition.
// It is unsorted BFS because the children are not ordered in the database.
func (t *ChildrenTraverser) Traverse(ctx context.Context, startBlockID iotago.BlockID, condition Predicate, consumer Consumer, walkAlreadyDiscovered bool) error {
// make sure only one traversal is running
t.traverserLock.Lock()
// release lock so the traverser can be reused
defer t.traverserLock.Unlock()
t.ctx = ctx
t.condition = condition
t.consumer = consumer
t.walkAlreadyDiscovered = walkAlreadyDiscovered
// Prepare for a new traversal
t.reset()
t.stack.PushFront(startBlockID)
if !t.walkAlreadyDiscovered {
t.discovered[startBlockID] = struct{}{}
}
for t.stack.Len() > 0 {
if err := t.processStackChildren(); err != nil {
return err
}
}
return nil
}
// processStackChildren checks if the current element in the stack must be processed and traversed.
// current element gets consumed first, afterwards it's children get traversed in random order.
func (t *ChildrenTraverser) processStackChildren() error {
if err := contextutils.ReturnErrIfCtxDone(t.ctx, common.ErrOperationAborted); err != nil {
return err
}
// load candidate block
ele := t.stack.Front()
currentBlockID, ok := ele.Value.(iotago.BlockID)
if !ok {
return fmt.Errorf("expected iotago.BlockID, got %T", ele.Value)
}
// remove the block from the stack
t.stack.Remove(ele)
// we also need to walk the children of solid entry points, but we don't consume them
contains, err := t.childrenTraverserStorage.SolidEntryPointsContain(currentBlockID)
if err != nil {
return err
}
if !contains {
cachedBlockMeta, err := t.childrenTraverserStorage.CachedBlockMetadata(currentBlockID) // meta +1
if err != nil {
return err
}
if cachedBlockMeta == nil {
// there was an error, stop processing the stack
return errors.Wrapf(common.ErrBlockNotFound, "block ID: %s", currentBlockID.ToHex())
}
defer cachedBlockMeta.Release(true) // meta -1
// check condition to decide if block should be consumed and traversed
traverse, err := t.condition(cachedBlockMeta.Retain()) // meta pass +1
if err != nil {
// there was an error, stop processing the stack
return err
}
if !traverse {
// block will not get consumed and children are not traversed
return nil
}
if t.consumer != nil {
// consume the block
if err := t.consumer(cachedBlockMeta.Retain()); err != nil { // meta pass +1
// there was an error, stop processing the stack
return err
}
}
}
childrenBlockIDs, err := t.childrenTraverserStorage.ChildrenBlockIDs(currentBlockID)
if err != nil {
return err
}
for _, childBlockID := range childrenBlockIDs {
if !t.walkAlreadyDiscovered {
if _, childDiscovered := t.discovered[childBlockID]; childDiscovered {
// child was already discovered
continue
}
t.discovered[childBlockID] = struct{}{}
}
// traverse the child
t.stack.PushBack(childBlockID)
}
return nil
}