/
preview_quick.go
291 lines (259 loc) · 8.14 KB
/
preview_quick.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
package terminate
import (
"context"
"math"
"math/big"
"github.com/filecoin-project/go-state-types/builtin/v12/miner"
"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"
"github.com/glifio/go-pools/util"
)
// PreviewTerminateSectorsQuick will preview the cost of terminating all
// the sectors for a miner
//
// It samples a subset of sectors and uses the "off-chain" calculation method.
//
// It has been "tuned" to provide reasonably accurate results (typically <1% error)
// while executing in a fair quick constant amount of time.
// Use the more complex [PreviewTerminateSectors] function for more control over the number of
// sectors to sample, or to use the "on-chain" calculation method, and also
// to stream results.
func PreviewTerminateSectorsQuick(
ctx context.Context,
api *lotusapi.FullNodeStruct,
minerAddr address.Address,
ts *types.TipSet,
) (*PreviewTerminateSectorsReturn, error) {
var batchSize uint64 = 40
var gasLimit uint64 = 270000000000
var maxPartitions uint64 = 21
h := ts.Height()
// Lookup actor balance
actor, mstate, err := mstat.LoadMinerActor(ctx, api, minerAddr, ts)
if err != nil {
return nil, err
}
actor.Address = &minerAddr
// Lookup current owner / worker
minerInfo, err := mstate.Info()
if err != nil {
return nil, err
}
lf, err := mstate.LockedFunds()
if err != nil {
return nil, err
}
provingDeadline, err := api.StateMinerProvingDeadline(ctx, minerAddr, ts.Key())
if err != nil {
return nil, err
}
prevHeight := h - 120
tsPrev, err := api.ChainGetTipSetByHeight(ctx, prevHeight, types.EmptyTSK)
if err != nil {
return nil, err
}
autoBatchSize := false
if batchSize == 0 && gasLimit == 0 {
autoBatchSize = true
batchSize = 2500
gasLimit = 90000000000 * 3 // 3 deadlines per batch
workerActorPrev, err := api.StateGetActor(ctx, minerInfo.Worker, tsPrev.Key())
if err != nil {
return nil, err
}
workerBal, _ := util.ToFIL(workerActorPrev.Balance.Int).Float64()
if workerBal < 3.0 {
ratio := workerBal / 3.0
batchSize = uint64(float64(batchSize) * ratio)
gasLimit = uint64(float64(gasLimit) * ratio)
}
}
if autoBatchSize && batchSize < 10 {
return nil, err
}
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 this error occurs, the miner hasn't been around long enough to process the deadlineTs, so we just skip it
if err != nil {
continue
}
for partIdx, partition := range partitions {
allLiveSectors = append(allLiveSectors, partition.LiveSectors)
sc, err := partition.LiveSectors.Count()
if err != nil {
return nil, err
}
if sc > 0 {
deadlinePartitions = append(deadlinePartitions, deadlinePartition{
dlIdx: dlIdx,
dlImmutable: dlImmutable,
deadlineTs: deadlineTs,
deadlineHeight: deadlineHeight,
partIdx: partIdx,
partition: partition,
})
}
}
}
sampledDeadlinePartitions := make([]bool, len(deadlinePartitions))
maxSectorsToSample := max(maxPartitions, 3)
allSectors, err := bitfield.MultiMerge(allLiveSectors...)
if err != nil {
return nil, err
}
sectors, err := allSectors.All(math.MaxUint64)
if err != nil {
return nil, err
}
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 {
return nil, err
}
for i, deadlinePartition := range deadlinePartitions {
if deadlinePartition.dlIdx == sectorPartition.Deadline &&
deadlinePartition.partIdx == int(sectorPartition.Partition) {
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)
errorCh := make(chan error)
offchain := true
go runTerminationsInBatches(ctx, api, minerAddr, minerInfo,
int64(gasLimit), offchain, terminationsCh, statsCh, errorCh)
go func() {
for deadlinePartitionIdx, deadlinePartition := range deadlinePartitions {
dlIdx := deadlinePartition.dlIdx
deadlineTs := deadlinePartition.deadlineTs
deadlineHeight := deadlinePartition.deadlineHeight
partIdx := deadlinePartition.partIdx
partition := deadlinePartition.partition
sc, err := partition.LiveSectors.Count()
if err != nil {
errorCh <- err
return
}
if sc > 0 {
if sampledDeadlinePartitions[deadlinePartitionIdx] {
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()
loop:
for {
select {
case stats := <-statsCh:
if stats == nil {
break loop
}
allStats = allStats.Accumulate(stats)
case err := <-errorCh:
return nil, err
}
}
if sectorsCount > 0 {
allStats = allStats.ScaleUp(int64(sectorsCount),
int64(sectorsCount-sectorsInSkippedPartitions))
}
return &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,
}, nil
}