forked from iotaledger/wasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (104 loc) · 2.38 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
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"runtime"
"github.com/iotaledger/hive.go/kvstore"
hivedb "github.com/iotaledger/hive.go/kvstore/database"
"github.com/iotaledger/hive.go/kvstore/rocksdb"
"github.com/iotaledger/wasp/packages/database"
"github.com/iotaledger/wasp/packages/state"
"github.com/iotaledger/wasp/packages/state/indexedstore"
)
type processFunc func(context.Context, kvstore.KVStore)
var (
blockIndex int64
blockIndex2 int64
)
func main() {
flag.Int64Var(&blockIndex, "b", -1, "Block index")
flag.Int64Var(&blockIndex2, "B", -1, "Block index 2")
flag.Parse()
if flag.NArg() != 2 {
log.Fatalf("usage: %s [-b index] <command> <chain-db-dir>", os.Args[0])
}
args := flag.Args()
var f processFunc
switch args[0] {
case "state-stats-per-hname":
f = stateStatsPerHname
case "trie-stats":
f = trieStats
case "trie-diff":
f = trieDiff
default:
log.Fatalf("unknown command: %s", args[0])
}
process(args[1], f)
}
func getState(kvs kvstore.KVStore, index int64) state.State {
store := indexedstore.New(state.NewStoreWithUniqueWriteMutex(kvs))
if index < 0 {
state, err := store.LatestState()
mustNoError(err)
return state
}
state, err := store.StateByIndex(uint32(index))
mustNoError(err)
return state
}
func process(dbDir string, f processFunc) {
rocksDatabase, err := rocksdb.OpenDBReadOnly(dbDir,
rocksdb.IncreaseParallelism(runtime.NumCPU()-1),
rocksdb.Custom([]string{
"periodic_compaction_seconds=43200",
"level_compaction_dynamic_level_bytes=true",
"keep_log_file_num=2",
"max_log_file_size=50000000", // 50MB per log file
}),
)
mustNoError(err)
db := database.New(
dbDir,
rocksdb.New(rocksDatabase),
hivedb.EngineRocksDB,
true,
func() bool { panic("should not be called") },
)
kvs := db.KVStore()
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{}, 1)
go func() {
defer close(done)
f(ctx, kvs)
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
select {
case <-c:
cancel()
<-done
case <-done:
cancel()
}
}
func percent(a, n int) int {
return int(percentf(a, n))
}
func percentf(a, n int) float64 {
return (100.0 * float64(a)) / float64(n)
}
func clearScreen() {
fmt.Print("\033[H\033[2J")
}
func mustNoError(err error) {
if err != nil {
panic(err)
}
}