-
Notifications
You must be signed in to change notification settings - Fork 459
/
miner_proving.go
376 lines (318 loc) · 10.2 KB
/
miner_proving.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package cmd
import (
"bytes"
"fmt"
"strconv"
"text/tabwriter"
"github.com/filecoin-project/go-address"
cmds "github.com/ipfs/go-ipfs-cmds"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/filecoin-project/venus/app/node"
"github.com/filecoin-project/venus/venus-shared/actors/adt"
"github.com/filecoin-project/venus/venus-shared/actors/builtin/miner"
"github.com/filecoin-project/venus/venus-shared/blockstore"
"github.com/filecoin-project/venus/venus-shared/types"
)
var minerProvingCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "View proving information.",
},
Subcommands: map[string]*cmds.Command{
"info": provingInfoCmd,
"deadlines": provingDeadlinesCmd,
"deadline": provingDeadlineInfoCmd,
"faults": provingFaultsCmd,
},
}
var provingInfoCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "View current state information.",
},
Arguments: []cmds.Argument{
cmds.StringArg("address", true, false, "Address of miner to show"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
maddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
ctx := req.Context
blockDelay, err := getBlockDelay(ctx, env)
if err != nil {
return err
}
chainAPI := env.(*node.Env).ChainAPI
head, err := chainAPI.ChainHead(ctx)
if err != nil {
return fmt.Errorf("getting chain head: %v", err)
}
mact, err := chainAPI.StateGetActor(ctx, maddr, head.Key())
if err != nil {
return err
}
stor := adt.WrapStore(ctx, cbor.NewCborStore(blockstore.NewAPIBlockstore(env.(*node.Env).BlockStoreAPI)))
mas, err := miner.Load(stor, mact)
if err != nil {
return err
}
cd, err := chainAPI.StateMinerProvingDeadline(ctx, maddr, head.Key())
if err != nil {
return fmt.Errorf("getting miner info: %v", err)
}
buf := new(bytes.Buffer)
writer := NewSilentWriter(buf)
writer.Printf("Miner: %s\n", maddr)
proving := uint64(0)
faults := uint64(0)
recovering := uint64(0)
curDeadlineSectors := uint64(0)
if err := mas.ForEachDeadline(func(dlIdx uint64, dl miner.Deadline) error {
return dl.ForEachPartition(func(partIdx uint64, part miner.Partition) error {
if bf, err := part.LiveSectors(); err != nil {
return err
} else if count, err := bf.Count(); err != nil {
return err
} else {
proving += count
if dlIdx == cd.Index {
curDeadlineSectors += count
}
}
if bf, err := part.FaultySectors(); err != nil {
return err
} else if count, err := bf.Count(); err != nil {
return err
} else {
faults += count
}
if bf, err := part.RecoveringSectors(); err != nil {
return err
} else if count, err := bf.Count(); err != nil {
return err
} else {
recovering += count
}
return nil
})
}); err != nil {
return fmt.Errorf("walking miner deadlines and partitions: %v", err)
}
var faultPerc float64
if proving > 0 {
faultPerc = float64(faults*10000/proving) / 100
}
writer.Printf("Current Epoch: %d\n", cd.CurrentEpoch)
writer.Printf("Proving Period Boundary: %d\n", cd.PeriodStart%cd.WPoStProvingPeriod)
writer.Printf("Proving Period Start: %s\n", EpochTimeTs(cd.CurrentEpoch, cd.PeriodStart, blockDelay, head))
writer.Printf("Next Period Start: %s\n", EpochTimeTs(cd.CurrentEpoch, cd.PeriodStart+cd.WPoStProvingPeriod, blockDelay, head))
writer.Println()
writer.Printf("Faults: %d (%.2f%%)\n", faults, faultPerc)
writer.Printf("Recovering: %d\n", recovering)
writer.Printf("Deadline Index: %d\n", cd.Index)
writer.Printf("Deadline Sectors: %d\n", curDeadlineSectors)
writer.Printf("Deadline Open: %s\n", EpochTime(cd.CurrentEpoch, cd.Open, blockDelay))
writer.Printf("Deadline Close: %s\n", EpochTime(cd.CurrentEpoch, cd.Close, blockDelay))
writer.Printf("Deadline Challenge: %s\n", EpochTime(cd.CurrentEpoch, cd.Challenge, blockDelay))
writer.Printf("Deadline FaultCutoff: %s\n", EpochTime(cd.CurrentEpoch, cd.FaultCutoff, blockDelay))
return re.Emit(buf)
},
}
var provingDeadlinesCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "View the current proving period deadlines information.",
},
Options: []cmds.Option{
cmds.BoolOption("all", "Count all sectors (only live sectors are counted by default)"),
},
Arguments: []cmds.Argument{
cmds.StringArg("address", true, false, "Address of miner to show"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
maddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
ctx := req.Context
api := env.(*node.Env).ChainAPI
deadlines, err := api.StateMinerDeadlines(ctx, maddr, types.EmptyTSK)
if err != nil {
return fmt.Errorf("getting deadlines: %w", err)
}
di, err := api.StateMinerProvingDeadline(ctx, maddr, types.EmptyTSK)
if err != nil {
return fmt.Errorf("getting deadlines: %w", err)
}
buf := new(bytes.Buffer)
writer := NewSilentWriter(buf)
writer.Printf("Miner: %s\n", maddr)
tw := tabwriter.NewWriter(buf, 2, 4, 2, ' ', 0)
_, _ = fmt.Fprintln(tw, "deadline\tpartitions\tsectors (faults)\tproven partitions")
all, _ := req.Options["all"].(bool)
for dlIdx, deadline := range deadlines {
partitions, err := api.StateMinerPartitions(ctx, maddr, uint64(dlIdx), types.EmptyTSK)
if err != nil {
return fmt.Errorf("getting partitions for deadline %d: %w", dlIdx, err)
}
provenPartitions, err := deadline.PostSubmissions.Count()
if err != nil {
return err
}
var partitionCount int
sectors := uint64(0)
faults := uint64(0)
for _, partition := range partitions {
if !all {
sc, err := partition.LiveSectors.Count()
if err != nil {
return err
}
if sc > 0 {
partitionCount++
}
sectors += sc
} else {
sc, err := partition.AllSectors.Count()
if err != nil {
return err
}
partitionCount++
sectors += sc
}
fc, err := partition.FaultySectors.Count()
if err != nil {
return err
}
faults += fc
}
var cur string
if di.Index == uint64(dlIdx) {
cur += "\t(current)"
}
_, _ = fmt.Fprintf(tw, "%d\t%d\t%d (%d)\t%d%s\n", dlIdx, partitionCount, sectors, faults, provenPartitions, cur)
}
if err := tw.Flush(); err != nil {
return err
}
return re.Emit(buf)
},
}
var provingDeadlineInfoCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "View the current proving period deadlines information.",
},
Arguments: []cmds.Argument{
cmds.StringArg("address", true, false, "Address of miner to show"),
cmds.StringArg("index", true, false, "Index of deadline to show"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
if len(req.Arguments) != 2 {
return fmt.Errorf("must pass two parameters")
}
maddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
ctx := req.Context
api := env.(*node.Env).ChainAPI
dlIdx, err := strconv.ParseUint(req.Arguments[1], 10, 64)
if err != nil {
return fmt.Errorf("could not parse deadline index: %w", err)
}
deadlines, err := api.StateMinerDeadlines(ctx, maddr, types.EmptyTSK)
if err != nil {
return fmt.Errorf("getting deadlines: %w", err)
}
di, err := api.StateMinerProvingDeadline(ctx, maddr, types.EmptyTSK)
if err != nil {
return fmt.Errorf("getting deadlines: %w", err)
}
partitions, err := api.StateMinerPartitions(ctx, maddr, dlIdx, types.EmptyTSK)
if err != nil {
return fmt.Errorf("getting partitions for deadline %d: %w", dlIdx, err)
}
provenPartitions, err := deadlines[dlIdx].PostSubmissions.Count()
if err != nil {
return err
}
buf := new(bytes.Buffer)
writer := NewSilentWriter(buf)
writer.Printf("Deadline Index: %d\n", dlIdx)
writer.Printf("Partitions: %d\n", len(partitions))
writer.Printf("Proven Partitions: %d\n", provenPartitions)
writer.Printf("Current: %t\n\n", di.Index == dlIdx)
for pIdx, partition := range partitions {
sectorCount, err := partition.AllSectors.Count()
if err != nil {
return err
}
sectorNumbers, err := partition.AllSectors.All(sectorCount)
if err != nil {
return err
}
faultsCount, err := partition.FaultySectors.Count()
if err != nil {
return err
}
fn, err := partition.FaultySectors.All(faultsCount)
if err != nil {
return err
}
writer.Printf("Partition Index: %d\n", pIdx)
writer.Printf("Sectors: %d\n", sectorCount)
writer.Printf("Sector Numbers: %v\n", sectorNumbers)
writer.Printf("Faults: %d\n", faultsCount)
writer.Printf("Faulty Sectors: %d\n", fn)
}
return re.Emit(buf)
},
}
var provingFaultsCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "View the currently known proving faulty sectors information.",
},
Arguments: []cmds.Argument{
cmds.StringArg("address", true, false, "Address of miner to show"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
maddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
ctx := req.Context
api := env.(*node.Env).ChainAPI
bstoreAPI := env.(*node.Env).BlockStoreAPI
stor := adt.WrapStore(ctx, cbor.NewCborStore(blockstore.NewAPIBlockstore(bstoreAPI)))
mact, err := api.StateGetActor(ctx, maddr, types.EmptyTSK)
if err != nil {
return err
}
mas, err := miner.Load(stor, mact)
if err != nil {
return err
}
buf := new(bytes.Buffer)
writer := NewSilentWriter(buf)
writer.Printf("Miner: %s\n", maddr)
tw := tabwriter.NewWriter(buf, 2, 4, 2, ' ', 0)
_, _ = fmt.Fprintln(tw, "deadline\tpartition\tsectors")
err = mas.ForEachDeadline(func(dlIdx uint64, dl miner.Deadline) error {
return dl.ForEachPartition(func(partIdx uint64, part miner.Partition) error {
faults, err := part.FaultySectors()
if err != nil {
return err
}
return faults.ForEach(func(num uint64) error {
_, _ = fmt.Fprintf(tw, "%d\t%d\t%d\n", dlIdx, partIdx, num)
return nil
})
})
})
if err != nil {
return err
}
if err := tw.Flush(); err != nil {
return err
}
return re.Emit(buf)
},
}