-
Notifications
You must be signed in to change notification settings - Fork 92
/
main.go
216 lines (185 loc) · 5 KB
/
main.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"flag"
"fmt"
"os"
"sync"
"sync/atomic"
"time"
"github.com/FactomProject/factomd/Utilities/tools"
"github.com/FactomProject/factomd/common/interfaces"
)
var CheckFloating bool
var UsingAPI bool
var FixIt bool
const level string = "level"
const bolt string = "bolt"
func main() {
var (
useApi = flag.Bool("api", false, "Use API instead")
checkFloating = flag.Bool("floating", false, "Check Floating")
fix = flag.Bool("fix", false, "Actually fix head")
)
flag.Parse()
UsingAPI = *useApi
CheckFloating = *checkFloating
FixIt = *fix
fmt.Println("Usage:")
fmt.Println("CorrectChainHeads level/bolt/api DBFileLocation")
fmt.Println("Program will fix chainheads")
if len(flag.Args()) < 2 {
fmt.Println("\nNot enough arguments passed")
os.Exit(1)
}
if len(flag.Args()) > 2 {
fmt.Println("\nToo many arguments passed")
os.Exit(1)
}
if flag.Args()[0] == "api" {
UsingAPI = true
}
var reader tools.Fetcher
if UsingAPI {
reader = tools.NewAPIReader(flag.Args()[1])
} else {
levelBolt := flag.Args()[0]
if levelBolt != level && levelBolt != bolt {
fmt.Println("\nFirst argument should be `level` or `bolt`")
os.Exit(1)
}
path := flag.Args()[1]
reader = tools.NewDBReader(levelBolt, path)
}
// dblock, err := reader.FetchDBlockHead()
FindHeads(reader)
}
func FindHeads(f tools.Fetcher) {
chainHeads := make(map[string]interfaces.IHash)
var allEblockLock sync.Mutex
allEblks := make(map[string]interfaces.IHash)
var err error
var dblock interfaces.IDirectoryBlock
head, err := f.FetchDBlockHead()
if err != nil {
panic(fmt.Sprintf("Error fetching head"))
}
height := head.GetDatabaseHeight()
dblock = head
top := height
fmt.Println("Starting at", height)
errCount := 0
waiting := new(int32)
done := new(int32)
total := 0
var wg sync.WaitGroup
allowedSimulataneous := 1000
permission := make(chan bool, allowedSimulataneous)
for i := 0; i < allowedSimulataneous; i++ {
permission <- true
}
start := time.Now()
doPrint := CheckFloating
go func() {
for {
if !doPrint {
return
}
time.Sleep(10 * time.Second)
v := atomic.LoadInt32(waiting)
d := atomic.LoadInt32(done)
fmt.Printf("%d are still waiting. %d Done. Permission: %d\n", v, d, len(permission))
}
}()
for ; height > 0; height-- {
v := atomic.LoadInt32(waiting)
for v > 50000 {
time.Sleep(1 * time.Second)
}
dblock, err = f.FetchDBlockByHeight(height)
if err != nil {
fmt.Printf("Error fetching height %d: %s\n", height, err.Error())
continue
}
eblockEnts := dblock.GetEBlockDBEntries()
total += len(eblockEnts)
if CheckFloating {
for i := 0; i < len(eblockEnts); i++ {
wg.Add(1)
atomic.AddInt32(waiting, 1)
func(eb interfaces.IDBEntry) {
defer wg.Done()
defer atomic.AddInt32(waiting, -1)
<-permission
defer func() {
permission <- true
atomic.AddInt32(done, 1)
}()
eblkF, err := f.FetchEBlock(eb.GetKeyMR())
if err != nil {
fmt.Printf("Error getting eblock %s for %s\n", eb.GetKeyMR().String(), eb.GetChainID().String())
return
}
kmr, err := eblkF.KeyMR()
if err != nil {
fmt.Printf("Error getting eblock keymr %s for %s\n", eb.GetKeyMR().String(), eb.GetChainID().String())
return
}
allEblockLock.Lock()
allEblks[kmr.String()] = eblkF.GetHeader().GetPrevKeyMR()
allEblockLock.Unlock()
}(eblockEnts[i])
}
}
for _, eblk := range eblockEnts {
if _, ok := chainHeads[eblk.GetChainID().String()]; ok {
// Chainhead already exists
continue
}
chainHeads[eblk.GetChainID().String()] = eblk.GetKeyMR()
ch, err := f.FetchHeadIndexByChainID(eblk.GetChainID())
if err != nil {
fmt.Printf("Error getting chainhead for %s\n", eblk.GetChainID().String())
} else {
if !ch.IsSameAs(eblk.GetKeyMR()) {
fmt.Printf("ERROR: Chainhead found: %s, Expected %s :: For Chain: %s at height %d\n",
ch.String(), eblk.GetKeyMR().String(), eblk.GetChainID().String(), height)
errCount++
if FixIt {
f.SetChainHeads([]interfaces.IHash{eblk.GetKeyMR()}, []interfaces.IHash{eblk.GetChainID()})
}
}
}
}
if height%500 == 0 {
d := atomic.LoadInt32(done)
ps := float64(top-height) / time.Since(start).Seconds()
fmt.Printf("Currently on %d out of %d at %.3fp/s. %d EblocksPerHeight, %d done. %d ChainHeads so far. %d Are bad\n", height, top, ps, total, d, len(chainHeads), errCount)
}
var _ = dblock
}
if CheckFloating {
wg.Wait()
}
doPrint = false
fmt.Printf("%d Chains found in %f seconds", len(chainHeads), time.Since(start).Seconds())
errCount = 0
if CheckFloating {
fmt.Println("Checking all EBLK links")
for k, h := range chainHeads {
var prev interfaces.IHash
prev = h
for {
if prev.IsZero() {
break
}
p, ok := allEblks[prev.String()]
if !ok {
errCount++
fmt.Printf("Error finding Eblock %s for chain %s\n", h.String(), k)
}
prev = p
}
}
}
fmt.Printf("%d Errors found checking for bad links\n", errCount)
}