-
Notifications
You must be signed in to change notification settings - Fork 0
/
antipast.go
196 lines (172 loc) · 6.99 KB
/
antipast.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
package syncmanager
import (
"github.com/karlsend/PYVERT/testfork/karlsend/domain/consensus/model"
"github.com/karlsend/PYVERT/testfork/karlsend/domain/consensus/model/externalapi"
"github.com/pkg/errors"
)
// antiPastHashesBetween returns the hashes of the blocks between the
// lowHash's antiPast and highHash's antiPast, or up to `maxBlocks`, if non-zero.
// The result excludes lowHash and includes highHash. If lowHash == highHash, returns nothing.
// If maxBlocks != 0 then maxBlocks MUST be >= MergeSetSizeLimit + 1
// because it returns blocks with MergeSet granularity,
// so if MergeSet > maxBlocks, function will return nothing
func (sm *syncManager) antiPastHashesBetween(stagingArea *model.StagingArea, lowHash, highHash *externalapi.DomainHash,
maxBlocks uint64) (hashes []*externalapi.DomainHash, actualHighHash *externalapi.DomainHash, err error) {
// Sanity check, for debugging only
if maxBlocks != 0 && maxBlocks < sm.mergeSetSizeLimit+1 {
return nil, nil,
errors.Errorf("maxBlocks (%d) MUST be >= MergeSetSizeLimit + 1 (%d)", maxBlocks, sm.mergeSetSizeLimit+1)
}
// If lowHash is not in the selectedParentChain of highHash - SelectedChildIterator will fail.
// Therefore, we traverse down lowHash's selectedParentChain until we reach a block that is in
// highHash's selectedParentChain.
// We keep originalLowHash to filter out blocks in it's past later down the road
originalLowHash := lowHash
lowHash, err = sm.findLowHashInHighHashSelectedParentChain(stagingArea, lowHash, highHash)
if err != nil {
return nil, nil, err
}
lowBlockGHOSTDAGData, err := sm.ghostdagDataStore.Get(sm.databaseContext, stagingArea, lowHash, false)
if err != nil {
return nil, nil, err
}
highBlockGHOSTDAGData, err := sm.ghostdagDataStore.Get(sm.databaseContext, stagingArea, highHash, false)
if err != nil {
return nil, nil, err
}
if lowBlockGHOSTDAGData.BlueScore() > highBlockGHOSTDAGData.BlueScore() {
return nil, nil, errors.Errorf("low hash blueScore > high hash blueScore (%d > %d)",
lowBlockGHOSTDAGData.BlueScore(), highBlockGHOSTDAGData.BlueScore())
}
// Collect all hashes by concatenating the merge-sets of all blocks between highHash and lowHash
blockHashes := []*externalapi.DomainHash{}
iterator, err := sm.dagTraversalManager.SelectedChildIterator(stagingArea, highHash, lowHash, false)
if err != nil {
return nil, nil, err
}
defer iterator.Close()
for ok := iterator.First(); ok; ok = iterator.Next() {
current, err := iterator.Get()
if err != nil {
return nil, nil, err
}
// Both blue and red merge sets are topologically sorted, but not the concatenation of the two.
// We require the blocks to be topologically sorted. In addition, for optimal performance,
// we want the selectedParent to be first.
// Since the rest of the merge set is in the anticone of selectedParent, it's position in the list does not
// matter, even though it's blue score is the highest, we can arbitrarily decide it comes first.
// Therefore we first append the selectedParent, then the rest of blocks in ghostdag order.
sortedMergeSet, err := sm.ghostdagManager.GetSortedMergeSet(stagingArea, current)
if err != nil {
return nil, nil, err
}
if maxBlocks != 0 && uint64(len(blockHashes)+len(sortedMergeSet)) > maxBlocks {
break
}
highHash = current
// append to blockHashes all blocks in sortedMergeSet which are not in the past of originalLowHash
for _, blockHash := range sortedMergeSet {
isInPastOfOriginalLowHash, err := sm.dagTopologyManager.IsAncestorOf(stagingArea, blockHash, originalLowHash)
if err != nil {
return nil, nil, err
}
if isInPastOfOriginalLowHash {
continue
}
blockHashes = append(blockHashes, blockHash)
}
}
// The process above doesn't return highHash, so include it explicitly, unless highHash == lowHash
if !lowHash.Equal(highHash) {
blockHashes = append(blockHashes, highHash)
}
return blockHashes, highHash, nil
}
func (sm *syncManager) findLowHashInHighHashSelectedParentChain(stagingArea *model.StagingArea,
lowHash *externalapi.DomainHash, highHash *externalapi.DomainHash) (*externalapi.DomainHash, error) {
for {
isInSelectedParentChain, err := sm.dagTopologyManager.IsInSelectedParentChainOf(stagingArea, lowHash, highHash)
if err != nil {
return nil, err
}
if isInSelectedParentChain {
break
}
lowBlockGHOSTDAGData, err := sm.ghostdagDataStore.Get(sm.databaseContext, stagingArea, lowHash, false)
if err != nil {
return nil, err
}
lowHash = lowBlockGHOSTDAGData.SelectedParent()
}
return lowHash, nil
}
func (sm *syncManager) missingBlockBodyHashes(stagingArea *model.StagingArea, highHash *externalapi.DomainHash) (
[]*externalapi.DomainHash, error) {
pruningPoint, err := sm.pruningStore.PruningPoint(sm.databaseContext, stagingArea)
if err != nil {
return nil, err
}
selectedChildIterator, err := sm.dagTraversalManager.SelectedChildIterator(stagingArea, highHash, pruningPoint, false)
if err != nil {
return nil, err
}
defer selectedChildIterator.Close()
lowHash := pruningPoint
foundHeaderOnlyBlock := false
for ok := selectedChildIterator.First(); ok; ok = selectedChildIterator.Next() {
selectedChild, err := selectedChildIterator.Get()
if err != nil {
return nil, err
}
blockStatus, err := sm.blockStatusStore.Get(sm.databaseContext, stagingArea, selectedChild)
if err != nil {
return nil, err
}
if blockStatus == externalapi.StatusHeaderOnly {
foundHeaderOnlyBlock = true
break
}
lowHash = selectedChild
}
if !foundHeaderOnlyBlock {
if lowHash.Equal(highHash) {
// Blocks can be inserted inside the DAG during IBD if those were requested before IBD started.
// In rare cases, all the IBD blocks might be already inserted by the time we reach this point.
// In these cases - return an empty list of blocks to sync
return []*externalapi.DomainHash{}, nil
}
// TODO: Once block children are fixed (https://github.com/karlsend/PYVERT/testfork/karlsend/issues/1499),
// this error should be returned rather the logged
log.Errorf("No header-only blocks between %s and %s",
lowHash, highHash)
}
hashesBetween, _, err := sm.antiPastHashesBetween(stagingArea, lowHash, highHash, 0)
if err != nil {
return nil, err
}
missingBlocks := make([]*externalapi.DomainHash, 0, len(hashesBetween))
for _, blockHash := range hashesBetween {
blockStatus, err := sm.blockStatusStore.Get(sm.databaseContext, stagingArea, blockHash)
if err != nil {
return nil, err
}
if blockStatus == externalapi.StatusHeaderOnly {
missingBlocks = append(missingBlocks, blockHash)
}
}
return missingBlocks, nil
}
func (sm *syncManager) isHeaderOnlyBlock(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (bool, error) {
exists, err := sm.blockStatusStore.Exists(sm.databaseContext, stagingArea, blockHash)
if err != nil {
return false, err
}
if !exists {
return false, nil
}
status, err := sm.blockStatusStore.Get(sm.databaseContext, stagingArea, blockHash)
if err != nil {
return false, err
}
return status == externalapi.StatusHeaderOnly, nil
}