/
handle_blooms.go
103 lines (89 loc) · 2.62 KB
/
handle_blooms.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
// Copyright 2021 The TrueBlocks Authors. All rights reserved.
// Use of this source code is governed by a license that can
// be found in the LICENSE file.
package chunksPkg
import (
"context"
"fmt"
"strings"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/index"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/walk"
)
func (opts *ChunksOptions) HandleBlooms(blockNums []uint64) error {
chain := opts.Globals.Chain
ctx, cancel := context.WithCancel(context.Background())
fetchData := func(modelChan chan types.Modeler[types.RawModeler], errorChan chan error) {
showBloom := func(walker *walk.CacheWalker, path string, first bool) (bool, error) {
if path != index.ToBloomPath(path) {
return false, fmt.Errorf("should not happen in showBloom")
}
var bl index.Bloom
_ = bl.Read(path)
nInserted := 0
for _, bl := range bl.Blooms {
nInserted += int(bl.NInserted)
}
if opts.Globals.Verbose {
displayBloom(&bl, 1)
}
stats, err := GetChunkStats(chain, path)
if err != nil {
return false, err
}
s := simpleChunkBloom{
Magic: fmt.Sprintf("0x%x", bl.Header.Magic),
Hash: bl.Header.Hash,
Size: stats.BloomSz,
Range: base.RangeFromFilename(path).String(),
NBlooms: stats.NBlooms,
ByteWidth: index.BLOOM_WIDTH_IN_BYTES,
NInserted: uint64(nInserted),
}
modelChan <- &s
return true, nil
}
walker := walk.NewCacheWalker(
chain,
opts.Globals.TestMode,
10, /* maxTests */
showBloom,
)
if err := walker.WalkBloomFilters(blockNums); err != nil {
errorChan <- err
cancel()
}
}
return output.StreamMany(ctx, fetchData, opts.Globals.OutputOpts())
}
func displayBloom(bl *index.Bloom, verbose int) {
var bytesPerLine = (2048 / 16) /* 128 */
if verbose > 0 && verbose <= 4 {
bytesPerLine = 32
}
nInserted := uint32(0)
for i := uint32(0); i < bl.Count; i++ {
nInserted += bl.Blooms[i].NInserted
}
fmt.Println("range:", bl.Range)
fmt.Println("nBlooms:", bl.Count)
fmt.Println("byteWidth:", index.BLOOM_WIDTH_IN_BYTES)
fmt.Println("nInserted:", nInserted)
if verbose > 0 {
for i := uint32(0); i < bl.Count; i++ {
for j := 0; j < len(bl.Blooms[i].Bytes); j++ {
if (j % bytesPerLine) == 0 {
if j != 0 {
fmt.Println()
}
}
ch := bl.Blooms[i].Bytes[j]
str := fmt.Sprintf("%08b", ch)
fmt.Printf("%s ", strings.Replace(str, "0", ".", -1))
}
}
fmt.Println()
}
}