-
Notifications
You must be signed in to change notification settings - Fork 38
/
miner.go
368 lines (310 loc) · 13 KB
/
miner.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
package migration
import (
"context"
miner11 "github.com/filecoin-project/go-state-types/builtin/v11/miner"
adt11 "github.com/filecoin-project/go-state-types/builtin/v11/util/adt"
miner12 "github.com/filecoin-project/go-state-types/builtin/v12/miner"
adt12 "github.com/filecoin-project/go-state-types/builtin/v12/util/adt"
"github.com/filecoin-project/go-state-types/migration"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-amt-ipld/v4"
"golang.org/x/xerrors"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/filecoin-project/go-state-types/abi"
)
// The minerMigrator performs the following migrations:
// - #914 fix: Set ActivationEpoch to when the sector was FIRST activated, and PowerBaseEpoch to latest update epoch
type minerMigrator struct {
emptyDeadlineV11 cid.Cid
emptyDeadlinesV11 cid.Cid
emptyDeadlineV12 cid.Cid
emptyDeadlinesV12 cid.Cid
OutCodeCID cid.Cid
}
func newMinerMigrator(ctx context.Context, store cbor.IpldStore, outCode cid.Cid) (*minerMigrator, error) {
ctxStore := adt11.WrapStore(ctx, store)
edv11, err := miner11.ConstructDeadline(ctxStore)
if err != nil {
return nil, xerrors.Errorf("failed to construct empty deadline v11: %w", err)
}
edv11cid, err := store.Put(ctx, edv11)
if err != nil {
return nil, xerrors.Errorf("failed to put empty deadline v11: %w", err)
}
edsv11 := miner11.ConstructDeadlines(edv11cid)
edsv11cid, err := store.Put(ctx, edsv11)
if err != nil {
return nil, xerrors.Errorf("failed to construct empty deadlines v11: %w", err)
}
edv12, err := miner12.ConstructDeadline(ctxStore)
if err != nil {
return nil, xerrors.Errorf("failed to construct empty deadline v12: %w", err)
}
edv12cid, err := store.Put(ctx, edv12)
if err != nil {
return nil, xerrors.Errorf("failed to put empty deadline v12: %w", err)
}
edsv12 := miner12.ConstructDeadlines(edv12cid)
edsv12cid, err := store.Put(ctx, edsv12)
if err != nil {
return nil, xerrors.Errorf("failed to construct empty deadlines v12: %w", err)
}
return &minerMigrator{
emptyDeadlineV11: edv11cid,
emptyDeadlinesV11: edsv11cid,
emptyDeadlineV12: edv12cid,
emptyDeadlinesV12: edsv12cid,
OutCodeCID: outCode,
}, nil
}
func (m minerMigrator) MigratedCodeCID() cid.Cid {
return m.OutCodeCID
}
func (m minerMigrator) Deferred() bool {
return false
}
func (m minerMigrator) MigrateState(ctx context.Context, store cbor.IpldStore, in migration.ActorMigrationInput) (*migration.ActorMigrationResult, error) {
var inState miner11.State
if err := store.Get(ctx, in.Head, &inState); err != nil {
return nil, xerrors.Errorf("failed to load miner state for %s: %w", in.Address, err)
}
wrappedStore := adt11.WrapStore(ctx, store)
newSectors, err := migrateSectorsWithCache(ctx, wrappedStore, in.Cache, in.Address, inState.Sectors)
if err != nil {
return nil, xerrors.Errorf("failed to migrate sectors for miner: %s: %w", in.Address, err)
}
newDeadlines, err := m.migrateDeadlines(ctx, wrappedStore, in.Cache, in.Address, inState.Deadlines)
if err != nil {
return nil, xerrors.Errorf("failed to migrate deadlines: %w", err)
}
outState := miner12.State{
Info: inState.Info,
PreCommitDeposits: inState.PreCommitDeposits,
LockedFunds: inState.LockedFunds,
VestingFunds: inState.VestingFunds,
FeeDebt: inState.FeeDebt,
InitialPledge: inState.InitialPledge,
PreCommittedSectors: inState.PreCommittedSectors,
PreCommittedSectorsCleanUp: inState.PreCommittedSectorsCleanUp,
AllocatedSectors: inState.AllocatedSectors,
Sectors: newSectors,
ProvingPeriodStart: inState.ProvingPeriodStart,
CurrentDeadline: inState.CurrentDeadline,
Deadlines: newDeadlines,
EarlyTerminations: inState.EarlyTerminations,
DeadlineCronActive: inState.DeadlineCronActive,
}
newHead, err := store.Put(ctx, &outState)
if err != nil {
return nil, xerrors.Errorf("failed to put new state: %w", err)
}
return &migration.ActorMigrationResult{
NewCodeCID: m.MigratedCodeCID(),
NewHead: newHead,
}, nil
}
func migrateSectorsWithCache(ctx context.Context, store adt11.Store, cache migration.MigrationCache, minerAddr address.Address, inRoot cid.Cid) (cid.Cid, error) {
return cache.Load(migration.SectorsAmtKey(inRoot), func() (cid.Cid, error) {
okIn, prevInRoot, err := cache.Read(migration.MinerPrevSectorsInKey(minerAddr))
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get previous inRoot from cache: %w", err)
}
okOut, prevOutRoot, err := cache.Read(migration.MinerPrevSectorsOutKey(minerAddr))
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get previous outRoot from cache: %w", err)
}
var outRoot cid.Cid
if okIn && okOut {
// we have previous work, but the AMT has changed -- diff them
outRoot, err = migrateSectorsWithDiff(ctx, store, inRoot, prevInRoot, prevOutRoot)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to migrate sectors from diff: %w", err)
}
} else {
// first time we're doing this, do all the work
inArray, err := adt11.AsArray(store, inRoot, miner11.SectorsAmtBitwidth)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to read sectors array: %w", err)
}
outArray, err := migrateSectorsFromScratch(ctx, store, inArray)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to migrate sectors from scratch: %w", err)
}
outRoot, err = outArray.Root()
if err != nil {
return cid.Undef, xerrors.Errorf("error writing new sectors AMT: %w", err)
}
}
if err = cache.Write(migration.MinerPrevSectorsInKey(minerAddr), inRoot); err != nil {
return cid.Undef, xerrors.Errorf("failed to write inkey to cache: %w", err)
}
if err = cache.Write(migration.MinerPrevSectorsOutKey(minerAddr), outRoot); err != nil {
return cid.Undef, xerrors.Errorf("failed to write inkey to cache: %w", err)
}
return outRoot, nil
})
}
func migrateSectorsWithDiff(ctx context.Context, store adt11.Store, inRoot cid.Cid, prevInRoot cid.Cid, prevOutRoot cid.Cid) (cid.Cid, error) {
// we have previous work, but the AMT has changed -- diff them
diffs, err := amt.Diff(ctx, store, store, prevInRoot, inRoot, amt.UseTreeBitWidth(miner11.SectorsAmtBitwidth))
if err != nil {
return cid.Undef, xerrors.Errorf("failed to diff old and new Sector AMTs: %w", err)
}
inSectors, err := miner11.LoadSectors(store, inRoot)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to load inSectors: %w", err)
}
prevOutSectors, err := miner12.LoadSectors(store, prevOutRoot)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to load prevOutSectors: %w", err)
}
for _, change := range diffs {
switch change.Type {
case amt.Remove:
if err := prevOutSectors.Delete(change.Key); err != nil {
return cid.Undef, xerrors.Errorf("failed to delete sector from prevOutSectors: %w", err)
}
case amt.Add:
fallthrough
case amt.Modify:
sectorNo := abi.SectorNumber(change.Key)
info, found, err := inSectors.Get(sectorNo)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get sector %d in inSectors: %w", sectorNo, err)
}
if !found {
return cid.Undef, xerrors.Errorf("didn't find sector %d in inSectors", sectorNo)
}
if err := prevOutSectors.Set(change.Key, migrateSectorInfo(*info)); err != nil {
return cid.Undef, xerrors.Errorf("failed to set migrated sector %d in prevOutSectors", sectorNo)
}
}
}
return prevOutSectors.Root()
}
func migrateSectorsFromScratch(ctx context.Context, store adt11.Store, inArray *adt11.Array) (*adt12.Array, error) {
outArray, err := adt12.MakeEmptyArray(store, miner12.SectorsAmtBitwidth)
if err != nil {
return nil, xerrors.Errorf("failed to construct new sectors array: %w", err)
}
var sectorInfo miner11.SectorOnChainInfo
if err = inArray.ForEach(§orInfo, func(k int64) error {
return outArray.Set(uint64(k), migrateSectorInfo(sectorInfo))
}); err != nil {
return nil, err
}
return outArray, err
}
func (m minerMigrator) migrateDeadlines(ctx context.Context, store adt11.Store, cache migration.MigrationCache, minerAddr address.Address, deadlines cid.Cid) (cid.Cid, error) {
if deadlines == m.emptyDeadlinesV11 {
return m.emptyDeadlinesV12, nil
}
var inDeadlines miner11.Deadlines
err := store.Get(store.Context(), deadlines, &inDeadlines)
if err != nil {
return cid.Undef, err
}
var outDeadlines miner12.Deadlines
for i, c := range inDeadlines.Due {
if c == m.emptyDeadlineV11 {
outDeadlines.Due[i] = m.emptyDeadlineV12
} else {
var inDeadline miner11.Deadline
if err = store.Get(ctx, c, &inDeadline); err != nil {
return cid.Undef, err
}
outSectorsSnapshotCid, err := cache.Load(migration.SectorsAmtKey(inDeadline.SectorsSnapshot), func() (cid.Cid, error) {
okIn, currentInRoot, err := cache.Read(migration.MinerPrevSectorsInKey(minerAddr))
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get previous inRoot from cache: %s", err)
}
okOut, currentOutRoot, err := cache.Read(migration.MinerPrevSectorsOutKey(minerAddr))
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get previous outRoot from cache: %w", err)
}
var outSnapshotRoot cid.Cid
if okIn && okOut {
outSnapshotRoot, err = migrateSectorsWithDiff(ctx, store, inDeadline.SectorsSnapshot, currentInRoot, currentOutRoot)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to migrate sectors from diff: %w", err)
}
} else {
inSectorsSnapshot, err := adt11.AsArray(store, inDeadline.SectorsSnapshot, miner11.SectorsAmtBitwidth)
if err != nil {
return cid.Undef, err
}
outSnapshot, err := migrateSectorsFromScratch(ctx, store, inSectorsSnapshot)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to migrate sectors: %w", err)
}
outSnapshotRoot, err = outSnapshot.Root()
if err != nil {
return cid.Undef, xerrors.Errorf("failed to take root of snapshot: %w", err)
}
}
return outSnapshotRoot, nil
})
if err != nil {
return cid.Undef, xerrors.Errorf("failed to migrate sectors snapshot: %w", err)
}
outDeadline := miner12.Deadline{
Partitions: inDeadline.Partitions,
ExpirationsEpochs: inDeadline.ExpirationsEpochs,
PartitionsPoSted: inDeadline.PartitionsPoSted,
EarlyTerminations: inDeadline.EarlyTerminations,
LiveSectors: inDeadline.LiveSectors,
TotalSectors: inDeadline.TotalSectors,
FaultyPower: miner12.PowerPair(inDeadline.FaultyPower),
OptimisticPoStSubmissions: inDeadline.OptimisticPoStSubmissions,
SectorsSnapshot: outSectorsSnapshotCid,
PartitionsSnapshot: inDeadline.PartitionsSnapshot,
OptimisticPoStSubmissionsSnapshot: inDeadline.OptimisticPoStSubmissionsSnapshot,
}
outDlCid, err := store.Put(ctx, &outDeadline)
if err != nil {
return cid.Undef, err
}
outDeadlines.Due[i] = outDlCid
}
}
return store.Put(ctx, &outDeadlines)
}
func migrateSectorInfo(sectorInfo miner11.SectorOnChainInfo) *miner12.SectorOnChainInfo {
// For a sector that has not been updated: the Activation is correct and ReplacedSectorAge is zero.
// For a sector that has been updated through SnapDeals: Activation is the epoch at which it was upgraded, and ReplacedSectorAge is delta since the true activation.
// For a sector that has been updated through the old CC path: Activation is correct
// Thus, we want to set:
//
// PowerBaseEpoch := Activation (in all cases)
// Activation := Activation (for non-upgraded sectors and sectors upgraded through old CC path)
// Activation := OldActivation - ReplacedSectorAge (for sectors updated through SnapDeals)
//
// SimpleQAPower field got replaced by flags field. We set the flag SIMPLE_QA_POWER if the sector had the field set to true.
powerBaseEpoch := sectorInfo.Activation
activationEpoch := sectorInfo.Activation
if sectorInfo.SectorKeyCID != nil {
activationEpoch = sectorInfo.Activation - sectorInfo.ReplacedSectorAge
}
flags := miner12.SectorOnChainInfoFlags(0)
if sectorInfo.SimpleQAPower {
flags = flags | miner12.SIMPLE_QA_POWER
}
return &miner12.SectorOnChainInfo{
SectorNumber: sectorInfo.SectorNumber,
SealProof: sectorInfo.SealProof,
SealedCID: sectorInfo.SealedCID,
DealIDs: sectorInfo.DealIDs,
Activation: activationEpoch,
Expiration: sectorInfo.Expiration,
DealWeight: sectorInfo.DealWeight,
VerifiedDealWeight: sectorInfo.VerifiedDealWeight,
InitialPledge: sectorInfo.InitialPledge,
ExpectedDayReward: sectorInfo.ExpectedDayReward,
ExpectedStoragePledge: sectorInfo.ExpectedStoragePledge,
PowerBaseEpoch: powerBaseEpoch,
ReplacedDayReward: sectorInfo.ReplacedDayReward,
SectorKeyCID: sectorInfo.SectorKeyCID,
Flags: flags,
}
}