-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
stateroot-stats.go
204 lines (169 loc) · 3.96 KB
/
stateroot-stats.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
package main
import (
"fmt"
"sort"
"github.com/multiformats/go-multihash"
"github.com/urfave/cli/v2"
"github.com/ipfs/go-cid"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli"
)
var staterootCmd = &cli.Command{
Name: "stateroot",
Subcommands: []*cli.Command{
staterootDiffsCmd,
staterootStatCmd,
},
}
var staterootDiffsCmd = &cli.Command{
Name: "diffs",
Description: "Walk down the chain and collect stats-obj changes between tipsets",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "tipset",
Usage: "specify tipset to start from",
},
&cli.IntFlag{
Name: "count",
Usage: "number of tipsets to count back",
Value: 30,
},
&cli.BoolFlag{
Name: "diff",
Usage: "compare tipset with previous",
Value: false,
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := lcli.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
ts, err := lcli.LoadTipSet(ctx, cctx, api)
if err != nil {
return err
}
fn := func(ts *types.TipSet) (cid.Cid, []cid.Cid) {
blk := ts.Blocks()[0]
strt := blk.ParentStateRoot
cids := blk.Parents
return strt, cids
}
count := cctx.Int("count")
diff := cctx.Bool("diff")
fmt.Printf("Height\tSize\tLinks\tObj\tBase\n")
for i := 0; i < count; i++ {
if ts.Height() == 0 {
return nil
}
strt, cids := fn(ts)
k := types.NewTipSetKey(cids...)
ts, err = api.ChainGetTipSet(ctx, k)
if err != nil {
return err
}
pstrt, _ := fn(ts)
if !diff {
pstrt = cid.Undef
}
stats, err := api.ChainStatObj(ctx, strt, pstrt)
if err != nil {
return err
}
fmt.Printf("%d\t%d\t%d\t%s\t%s\n", ts.Height(), stats.Size, stats.Links, strt, pstrt)
}
return nil
},
}
type statItem struct {
Addr address.Address
Actor *types.Actor
Stat api.ObjStat
}
var staterootStatCmd = &cli.Command{
Name: "stat",
Usage: "print statistics for the stateroot of a given block",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "tipset",
Usage: "specify tipset to start from",
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := lcli.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
ts, err := lcli.LoadTipSet(ctx, cctx, api)
if err != nil {
return err
}
var addrs []address.Address
for _, inp := range cctx.Args().Slice() {
a, err := address.NewFromString(inp)
if err != nil {
return err
}
addrs = append(addrs, a)
}
if len(addrs) == 0 {
allActors, err := api.StateListActors(ctx, ts.Key())
if err != nil {
return err
}
addrs = allActors
}
var infos []statItem
for _, a := range addrs {
act, err := api.StateGetActor(ctx, a, ts.Key())
if err != nil {
return err
}
stat, err := api.ChainStatObj(ctx, act.Head, cid.Undef)
if err != nil {
return err
}
infos = append(infos, statItem{
Addr: a,
Actor: act,
Stat: stat,
})
}
sort.Slice(infos, func(i, j int) bool {
return infos[i].Stat.Size > infos[j].Stat.Size
})
var totalActorsSize uint64
for _, info := range infos {
totalActorsSize += info.Stat.Size
}
outcap := 10
if cctx.Args().Len() > outcap {
outcap = cctx.Args().Len()
}
if len(infos) < outcap {
outcap = len(infos)
}
totalStat, err := api.ChainStatObj(ctx, ts.ParentState(), cid.Undef)
if err != nil {
return err
}
fmt.Println("Total state tree size: ", totalStat.Size)
fmt.Println("Sum of actor state size: ", totalActorsSize)
fmt.Println("State tree structure size: ", totalStat.Size-totalActorsSize)
fmt.Print("Addr\tType\tSize\n")
for _, inf := range infos[:outcap] {
cmh, err := multihash.Decode(inf.Actor.Code.Hash())
if err != nil {
return err
}
fmt.Printf("%s\t%s\t%d\n", inf.Addr, string(cmh.Digest), inf.Stat.Size)
}
return nil
},
}