-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
83 lines (69 loc) · 1.89 KB
/
utils.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
package merkle_script
import (
"fmt"
"io"
"os"
"github.com/KYVENetwork/ksync/types"
"github.com/rs/zerolog"
)
type Pool struct {
PoolID int64 `mapstructure:"pool_id"`
TargetBundleID int `mapstructure:"target_bundle_id"`
}
type BundleInfo struct {
Bundle types.FinalizedBundle
Runtime string
BundleId int
PoolId int
}
type MerkleRootEntry struct {
BundleId int
PoolId int
Hash [32]byte
}
func MerkleLogger(moduleName string) zerolog.Logger {
writer := io.MultiWriter(os.Stdout)
customConsoleWriter := zerolog.ConsoleWriter{Out: writer}
customConsoleWriter.FormatCaller = func(i interface{}) string {
return "\x1b[36m[Merkle]\x1b[0m"
}
logger := zerolog.New(customConsoleWriter).With().Str("module", moduleName).Timestamp().Logger()
return logger
}
// An MerkleRootQueue is a min-heap.
type MerkleRootQueue []MerkleRootEntry
func (h MerkleRootQueue) Len() int { return len(h) }
func (h MerkleRootQueue) Less(i, j int) bool {
return h[i].BundleId < h[j].BundleId
}
func (h MerkleRootQueue) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *MerkleRootQueue) Push(x any) {
// Push and Pop use pointer receivers because they modify the slice's length,
// not just its contents.
*h = append(*h, x.(MerkleRootEntry))
}
func (h *MerkleRootQueue) Pop() any {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
func GetMerkleFileName(poolId int) string {
return fmt.Sprintf("merkle_roots_pool_%v", poolId)
}
func GetWrittenMerkleRootCount(pools []Pool) map[int]int {
poolHeights := map[int]int{}
for _, pool := range pools {
fileName := GetMerkleFileName(int(pool.PoolID))
stats, err := os.Stat(fileName)
if err != nil { // file does not exist
continue
}
// we have one hash per bundle, one hash is 32 bytes big
// bundleHeight = size / 32
height := stats.Size() / 32
poolHeights[int(pool.PoolID)] = int(height)
}
return poolHeights
}