-
Notifications
You must be signed in to change notification settings - Fork 211
/
activeset.go
53 lines (47 loc) · 1.25 KB
/
activeset.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
package main
import (
"errors"
"flag"
"fmt"
"os"
"strconv"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
)
func main() {
flag.Usage = func() {
fmt.Println(`Usage:
> activeset <publish epoch> <db path>
Example:
query atxs that are published in epoch 3 and stored in state.sql file.
> activeset 3 state.sql`)
flag.PrintDefaults()
}
flag.Parse()
publish, err := strconv.Atoi(flag.Arg(0))
must(err, "publish epoch %v is not a valid integer: %s", flag.Arg(0), err)
dbpath := flag.Arg(1)
if len(dbpath) == 0 {
must(errors.New("dbpath is empty"), "dbpath is empty\n")
}
db, err := sql.Open("file:" + dbpath)
must(err, "can't open db at dbpath=%v. err=%s\n", dbpath, err)
ids, err := atxs.GetIDsByEpoch(db, types.EpochID(publish))
must(err, "get ids by epoch %d. dbpath=%v. err=%s\n", publish, dbpath, err)
var weight uint64
for _, id := range ids {
atx, err := atxs.Get(db, id)
must(err, "get id %v: %s\n", id, err)
weight += atx.GetWeight()
}
fmt.Printf("count = %d\nweight = %d\n", len(ids), weight)
}
func must(err error, msg string, vars ...any) {
if err != nil {
fmt.Printf(msg, vars...)
fmt.Println("")
flag.Usage()
os.Exit(1)
}
}