-
Notifications
You must be signed in to change notification settings - Fork 2
/
preview.go
445 lines (405 loc) · 12.7 KB
/
preview.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package terminate
import (
"context"
"math"
"math/big"
"github.com/filecoin-project/go-state-types/builtin/v12/miner"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/abi"
lotusapi "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/glifio/go-pools/mstat"
)
// PreviewTerminateSector will preview the cost of terminating a single sector
// on a miner.
//
// - tipset takes a string selector, similar to Lotus, eg. "@1234", or "cid,cid,cid"
// - offchain controls whether or not to use the "on-chain" or "off-chain"
// calculation method.
func PreviewTerminateSector(
ctx context.Context,
api *lotusapi.FullNodeStruct,
minerAddr address.Address,
tipset string,
vmHeight uint64,
sectorNumber uint64,
gasLimit uint64,
offchain bool,
) (actor *types.ActorV5, sectorStats *SectorStats, epoch abi.ChainEpoch, err error) {
h, ts, err := parseTipSetAndHeight(ctx, api, tipset, vmHeight)
if err != nil {
return nil, nil, 0, err
}
// Lookup actor balance
mactor, mstate, err := mstat.LoadMinerActor(ctx, api, minerAddr, ts)
if err != nil {
return nil, nil, 0, err
}
actor = mactor
// Lookup current owner / worker
minerInfo, err := mstate.Info()
if err != nil {
return nil, nil, 0, err
}
var params miner.TerminateSectorsParams
if gasLimit == 0 {
gasLimit = 6000000000
}
sectorPartition, err := api.StateSectorPartition(ctx, minerAddr, abi.SectorNumber(sectorNumber), ts.Key())
if err != nil {
return nil, nil, 0, err
}
termination := miner.TerminationDeclaration{
Deadline: sectorPartition.Deadline,
Partition: sectorPartition.Partition,
Sectors: bitfield.NewFromSet([]uint64{sectorNumber}),
}
params = miner.TerminateSectorsParams{
Terminations: []miner.TerminationDeclaration{termination},
}
stats, err := terminateSectors(ctx, api, h, ts, minerAddr, minerInfo,
params, int64(gasLimit), offchain)
if err != nil {
return nil, nil, 0, err
}
return actor, stats, h, nil
}
// PreviewTerminateSectors will preview the cost of terminating all the sectors
// for a miner.
//
// This function is intended to be run as a goroutine. Channels are used to
// stream back progress and results/errors, so clients can report progress as
// the query progresses.
//
// - tipset takes a string selector, similar to Lotus, eg. "@1234", or "cid,cid,cid"
// - batchSize is the number of sectors to query per message. If set to zero,
// and gasLimit is also zero, a default batch size will be calculated based
// on the miner's worker balance.
// - offchain controls whether or not to use the "on-chain" or "off-chain"
// calculation method.
// - optimize will make additional queries to try to select deadlines to
// sample that are evenly distributed over historical time.
// - maxPartitions controls how many partitions to sample. If set to zero,
// all partitions will be sampled.
//
// Use the simpler, more opinionated [PreviewTerminateSectorsQuick] function
// for quick results using sampling, "off-chain" calcuation, and a simpler
// API (no channels for streaming progress)
func PreviewTerminateSectors(
ctx context.Context,
api *lotusapi.FullNodeStruct,
minerAddr address.Address,
tipset string,
vmHeight uint64,
batchSize uint64,
gasLimit uint64,
useSampling bool, // if set, not all sectors will be sampled
optimize bool,
offchain bool,
maxPartitions uint64,
errorCh chan error,
progressCh chan *PreviewTerminateSectorsProgress,
resultCh chan *PreviewTerminateSectorsReturn,
) {
h, ts, err := parseTipSetAndHeight(ctx, api, tipset, vmHeight)
if err != nil {
errorCh <- err
return
}
// Lookup actor balance
actor, mstate, err := mstat.LoadMinerActor(ctx, api, minerAddr, ts)
// Lookup current owner / worker
minerInfo, err := mstate.Info()
if err != nil {
errorCh <- err
return
}
lf, err := mstate.LockedFunds()
if err != nil {
errorCh <- err
return
}
workerActor, err := api.StateGetActor(ctx, minerInfo.Worker, ts.Key())
if err != nil {
errorCh <- err
return
}
provingDeadline, err := api.StateMinerProvingDeadline(ctx, minerAddr, ts.Key())
if err != nil {
errorCh <- err
return
}
prevHeight := h - 120
tsPrev, err := api.ChainGetTipSetByHeight(ctx, prevHeight, types.EmptyTSK)
if err != nil {
errorCh <- err
return
}
workerActorPrev, err := api.StateGetActor(ctx, minerInfo.Worker, tsPrev.Key())
if err != nil {
errorCh <- err
return
}
autoBatchSize := false
if batchSize == 0 && gasLimit == 0 {
autoBatchSize = true
batchSize = 2500
gasLimit = 90000000000 * 3 // 3 deadlines per batch
}
progressCh <- &PreviewTerminateSectorsProgress{
Epoch: h,
MinerInfo: minerInfo,
WorkerActor: workerActor,
PrevHeightForImmutable: prevHeight,
WorkerActorPrev: workerActorPrev,
BatchSize: batchSize,
GasLimit: gasLimit,
}
if autoBatchSize && batchSize < 10 {
errorCh <- xerrors.Errorf("Not enough worker funds! Batch size too small!")
return
}
type deadlinePartition struct {
dlIdx uint64
dlImmutable bool
deadlineTs *types.TipSet
deadlineHeight abi.ChainEpoch
partIdx int
partition lotusapi.Partition
}
var sectorsTerminated uint64
var sectorsCount uint64
var sectorsInSkippedPartitions uint64
var dlIdx uint64
deadlinePartitions := make([]deadlinePartition, 0)
allLiveSectors := make([]bitfield.BitField, 0)
for dlIdx = 0; dlIdx < 48; dlIdx++ {
dlImmutable := false
deadlineTs := ts
deadlineHeight := h
if dlIdx == provingDeadline.Index || dlIdx == (provingDeadline.Index+1)%48 {
dlImmutable = true
deadlineTs = tsPrev
deadlineHeight = prevHeight
}
partitions, err := api.StateMinerPartitions(ctx, minerAddr, uint64(dlIdx), deadlineTs.Key())
if err != nil {
errorCh <- err
return
}
for partIdx, partition := range partitions {
if optimize {
allLiveSectors = append(allLiveSectors, partition.LiveSectors)
}
sc, err := partition.LiveSectors.Count()
if err != nil {
errorCh <- err
return
}
if sc > 0 {
deadlinePartitions = append(deadlinePartitions, deadlinePartition{
dlIdx: dlIdx,
dlImmutable: dlImmutable,
deadlineTs: deadlineTs,
deadlineHeight: deadlineHeight,
partIdx: partIdx,
partition: partition,
})
}
}
}
sampledDeadlinePartitions := make([]bool, len(deadlinePartitions))
if optimize {
maxSectorsToSample := max(maxPartitions, 3)
allSectors, err := bitfield.MultiMerge(allLiveSectors...)
if err != nil {
errorCh <- err
return
}
sectors, err := allSectors.All(math.MaxUint64)
if err != nil {
errorCh <- err
return
}
sampledSectors := make([]uint64, 0)
step := max(float64(len(sectors))/float64(maxSectorsToSample-1), 1.0)
lastIndex := -1
for sampleIndex := 0.0; int(sampleIndex) < len(sectors); sampleIndex += step {
i := int(sampleIndex)
if i > lastIndex {
sampledSectors = append(sampledSectors, sectors[i])
lastIndex = i
}
}
if lastIndex < len(sectors)-1 {
sampledSectors = append(sampledSectors, sectors[len(sectors)-1])
}
for _, sectorNumber := range sampledSectors {
sectorPartition, err := api.StateSectorPartition(ctx, minerAddr, abi.SectorNumber(sectorNumber), ts.Key())
if err != nil {
errorCh <- err
return
}
for i, deadlinePartition := range deadlinePartitions {
if deadlinePartition.dlIdx == sectorPartition.Deadline &&
deadlinePartition.partIdx == int(sectorPartition.Partition) {
sampledDeadlinePartitions[i] = true
}
}
}
} else {
var step float64
if maxPartitions > 0 {
step = max(float64(len(deadlinePartitions))/float64(maxPartitions), 1.0)
} else {
step = 1
}
for sampleIndex := 0.0; int(sampleIndex) < len(deadlinePartitions); sampleIndex += step {
i := int(sampleIndex)
sampledDeadlinePartitions[i] = true
}
}
sampledDeadlinePartitionCount := uint64(0)
for i := range deadlinePartitions {
if sampledDeadlinePartitions[i] &&
(maxPartitions == 0 || sampledDeadlinePartitionCount < maxPartitions) {
sampledDeadlinePartitionCount++
}
}
terminationsCh := make(chan terminationTask)
statsCh := make(chan *SectorStats)
go runTerminationsInBatches(ctx, api, minerAddr, minerInfo,
int64(gasLimit), offchain, terminationsCh, statsCh, errorCh)
go func() {
for deadlinePartitionIdx, deadlinePartition := range deadlinePartitions {
dlIdx := deadlinePartition.dlIdx
dlImmutable := deadlinePartition.dlImmutable
deadlineTs := deadlinePartition.deadlineTs
deadlineHeight := deadlinePartition.deadlineHeight
partIdx := deadlinePartition.partIdx
partition := deadlinePartition.partition
sc, err := partition.LiveSectors.Count()
if err != nil {
errorCh <- err
return
}
progressCh <- &PreviewTerminateSectorsProgress{
DeadlinePartitionCount: len(deadlinePartitions),
DeadlinePartitionIndex: deadlinePartitionIdx,
Deadline: dlIdx,
DeadlineImmutable: dlImmutable,
Partition: partIdx,
SectorsCount: sc,
}
if sc > 0 {
if sampledDeadlinePartitions[deadlinePartitionIdx] {
if !useSampling {
var i uint64
for i = 0; i < sc; i += batchSize {
lastIndex := min(sc, i+batchSize)
slicedSectors, err := partition.LiveSectors.Slice(i, lastIndex-i)
if err != nil {
errorCh <- err
return
}
sliceCount, err := slicedSectors.Count()
if err != nil {
errorCh <- err
return
}
progressCh <- &PreviewTerminateSectorsProgress{
DeadlinePartitionCount: len(deadlinePartitions),
DeadlinePartitionIndex: deadlinePartitionIdx,
Deadline: dlIdx,
DeadlineImmutable: dlImmutable,
Partition: partIdx,
SectorsCount: sc,
SliceStart: i,
SliceEnd: lastIndex,
SliceCount: sliceCount,
}
termination := miner.TerminationDeclaration{
Deadline: uint64(dlIdx),
Partition: uint64(partIdx),
Sectors: slicedSectors,
}
terminationsCh <- terminationTask{
termination: termination,
deadlineHeight: deadlineHeight,
deadlineTs: deadlineTs,
sectorsCount: int64(sc),
sampledSectorsCount: int64(sc),
}
sectorsTerminated += sliceCount
sectorsCount += sliceCount
}
} else {
// useSampling
sectors, err := partition.LiveSectors.All(math.MaxUint64)
if err != nil {
errorCh <- err
return
}
sampledSectors := make([]uint64, 0)
step := max(float64(len(sectors))/float64(batchSize-1), 1.0)
lastIndex := -1
for sampleIndex := 0.0; int(sampleIndex) < len(sectors); sampleIndex += step {
i := int(sampleIndex)
if i > lastIndex {
sampledSectors = append(sampledSectors, sectors[i])
lastIndex = i
}
}
if lastIndex < len(sectors)-1 {
sampledSectors = append(sampledSectors, sectors[len(sectors)-1])
}
sampledSectorsBitfield := bitfield.NewFromSet(sampledSectors)
termination := miner.TerminationDeclaration{
Deadline: uint64(dlIdx),
Partition: uint64(partIdx),
Sectors: sampledSectorsBitfield,
}
terminationsCh <- terminationTask{
termination: termination,
deadlineHeight: deadlineHeight,
deadlineTs: deadlineTs,
sectorsCount: int64(sc),
sampledSectorsCount: int64(len(sampledSectors)),
}
sectorsTerminated += uint64(len(sampledSectors))
sectorsCount += uint64(len(sectors))
}
} else {
// DeadlinePartition not sampled
sectorsInSkippedPartitions += sc
sectorsCount += sc
}
}
}
close(terminationsCh)
}()
allStats := NewSectorStats()
for stats := range statsCh {
allStats = allStats.Accumulate(stats)
}
if sectorsCount > 0 {
allStats = allStats.ScaleUp(int64(sectorsCount),
int64(sectorsCount-sectorsInSkippedPartitions))
}
resultCh <- &PreviewTerminateSectorsReturn{
Actor: actor,
MinerInfo: minerInfo,
VestingBalance: lf.VestingFunds.Int,
InitialPledge: new(big.Int).Add(lf.InitialPledgeRequirement.Int, lf.PreCommitDeposits.Int),
SectorStats: allStats,
SectorsTerminated: sectorsTerminated,
SectorsCount: sectorsCount,
SectorsInSkippedPartitions: sectorsInSkippedPartitions,
Epoch: h,
PartitionsCount: uint64(len(deadlinePartitions)),
SampledPartitionsCount: sampledDeadlinePartitionCount,
Tipset: ts,
}
}