This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
miner.go
228 lines (195 loc) · 7.92 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
package nv10
import (
"context"
"github.com/filecoin-project/go-bitfield"
cid "github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"golang.org/x/xerrors"
miner2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/miner"
adt2 "github.com/filecoin-project/specs-actors/v2/actors/util/adt"
builtin3 "github.com/filecoin-project/specs-actors/v3/actors/builtin"
miner3 "github.com/filecoin-project/specs-actors/v3/actors/builtin/miner"
adt3 "github.com/filecoin-project/specs-actors/v3/actors/util/adt"
)
type minerMigrator struct{}
func (m minerMigrator) migrateState(ctx context.Context, store cbor.IpldStore, in actorMigrationInput) (*actorMigrationResult, error) {
var inState miner2.State
if err := store.Get(ctx, in.head, &inState); err != nil {
return nil, err
}
infoOut, err := m.migrateInfo(ctx, store, inState.Info)
if err != nil {
return nil, err
}
preCommittedSectorsOut, err := migrateHAMTRaw(ctx, store, inState.PreCommittedSectors, builtin3.DefaultHamtBitwidth)
if err != nil {
return nil, err
}
preCommittedSectorsExpiryOut, err := migrateAMTRaw(ctx, store, inState.PreCommittedSectorsExpiry, miner3.PrecommitExpiryAmtBitwidth)
if err != nil {
return nil, err
}
sectorsOut, err := in.cache.Load(SectorsRootKey(inState.Sectors), func() (cid.Cid, error) {
return migrateAMTRaw(ctx, store, inState.Sectors, miner3.SectorsAmtBitwidth)
})
if err != nil {
return nil, err
}
deadlinesOut, err := m.migrateDeadlines(ctx, store, in.cache, inState.Deadlines)
if err != nil {
return nil, err
}
outState := miner3.State{
Info: infoOut,
PreCommitDeposits: inState.PreCommitDeposits,
LockedFunds: inState.LockedFunds,
VestingFunds: inState.VestingFunds,
FeeDebt: inState.FeeDebt,
InitialPledge: inState.InitialPledge,
PreCommittedSectors: preCommittedSectorsOut,
PreCommittedSectorsExpiry: preCommittedSectorsExpiryOut,
AllocatedSectors: inState.AllocatedSectors,
Sectors: sectorsOut,
ProvingPeriodStart: inState.ProvingPeriodStart,
CurrentDeadline: inState.CurrentDeadline,
Deadlines: deadlinesOut,
EarlyTerminations: inState.EarlyTerminations,
}
newHead, err := store.Put(ctx, &outState)
return &actorMigrationResult{
newCodeCID: m.migratedCodeCID(),
newHead: newHead,
}, err
}
func (m minerMigrator) migratedCodeCID() cid.Cid {
return builtin3.StorageMinerActorCodeID
}
func (m *minerMigrator) migrateInfo(ctx context.Context, store cbor.IpldStore, c cid.Cid) (cid.Cid, error) {
var oldInfo miner2.MinerInfo
err := store.Get(ctx, c, &oldInfo)
if err != nil {
return cid.Undef, err
}
var newWorkerKeyChange *miner3.WorkerKeyChange
if oldInfo.PendingWorkerKey != nil {
newWorkerKeyChange = &miner3.WorkerKeyChange{
NewWorker: oldInfo.PendingWorkerKey.NewWorker,
EffectiveAt: oldInfo.PendingWorkerKey.EffectiveAt,
}
}
windowPoStProof, err := oldInfo.SealProofType.RegisteredWindowPoStProof()
if err != nil {
return cid.Undef, err
}
newInfo := miner3.MinerInfo{
Owner: oldInfo.Owner,
Worker: oldInfo.Worker,
ControlAddresses: oldInfo.ControlAddresses,
PendingWorkerKey: newWorkerKeyChange,
PeerId: oldInfo.PeerId,
Multiaddrs: oldInfo.Multiaddrs,
WindowPoStProofType: windowPoStProof,
SectorSize: oldInfo.SectorSize,
WindowPoStPartitionSectors: oldInfo.WindowPoStPartitionSectors,
ConsensusFaultElapsed: oldInfo.ConsensusFaultElapsed,
PendingOwnerAddress: oldInfo.PendingOwnerAddress,
}
return store.Put(ctx, &newInfo)
}
func (m *minerMigrator) migrateDeadlines(ctx context.Context, store cbor.IpldStore, cache MigrationCache, deadlines cid.Cid) (cid.Cid, error) {
var inDeadlines miner2.Deadlines
err := store.Get(ctx, deadlines, &inDeadlines)
if err != nil {
return cid.Undef, err
}
if miner2.WPoStPeriodDeadlines != miner3.WPoStPeriodDeadlines {
return cid.Undef, xerrors.Errorf("unexpected WPoStPeriodDeadlines changed from %d to %d",
miner2.WPoStPeriodDeadlines, miner3.WPoStPeriodDeadlines)
}
outDeadlines := miner3.Deadlines{Due: [miner3.WPoStPeriodDeadlines]cid.Cid{}}
// Start from an empty template to zero-initialize new fields.
deadlineTemplate, err := miner3.ConstructDeadline(adt3.WrapStore(ctx, store))
if err != nil {
return cid.Undef, xerrors.Errorf("failed to construct new deadline template")
}
for i, c := range inDeadlines.Due {
outDlCid, err := cache.Load(DeadlineKey(c), func() (cid.Cid, error) {
var inDeadline miner2.Deadline
if err = store.Get(ctx, c, &inDeadline); err != nil {
return cid.Undef, err
}
partitions, err := m.migratePartitions(ctx, store, inDeadline.Partitions)
if err != nil {
return cid.Undef, xerrors.Errorf("partitions: %w", err)
}
expirationEpochs, err := migrateAMTRaw(ctx, store, inDeadline.ExpirationsEpochs, miner3.DeadlineExpirationAmtBitwidth)
if err != nil {
return cid.Undef, xerrors.Errorf("bitfield queue: %w", err)
}
outDeadline := *deadlineTemplate
outDeadline.Partitions = partitions
outDeadline.ExpirationsEpochs = expirationEpochs
outDeadline.PartitionsPoSted = inDeadline.PostSubmissions
outDeadline.EarlyTerminations = inDeadline.EarlyTerminations
outDeadline.LiveSectors = inDeadline.LiveSectors
outDeadline.TotalSectors = inDeadline.TotalSectors
outDeadline.FaultyPower = miner3.PowerPair(inDeadline.FaultyPower)
// If there are no live sectors in this partition, zero out the "partitions
// posted" bitfield. This corrects a state issue where:
// 1. A proof is submitted and a partition is marked as proven.
// 2. All sectors in a deadline are terminated during the challenge window.
// 3. The end of deadline logic is skipped because there are no live sectors.
// This bug has been fixed in actors v3 (no terminations allowed during the
// challenge window) but the state still needs to be fixed.
// See: https://github.com/filecoin-project/specs-actors/issues/1348
if outDeadline.LiveSectors == 0 {
outDeadline.PartitionsPoSted = bitfield.New()
}
return store.Put(ctx, &outDeadline)
})
if err != nil {
return cid.Undef, err
}
outDeadlines.Due[i] = outDlCid
}
return store.Put(ctx, &outDeadlines)
}
func (m *minerMigrator) migratePartitions(ctx context.Context, store cbor.IpldStore, root cid.Cid) (cid.Cid, error) {
// AMT[PartitionNumber]Partition
inArray, err := adt2.AsArray(adt2.WrapStore(ctx, store), root)
if err != nil {
return cid.Undef, err
}
outArray, err := adt3.MakeEmptyArray(adt2.WrapStore(ctx, store), miner3.DeadlinePartitionsAmtBitwidth)
if err != nil {
return cid.Undef, err
}
var inPartition miner2.Partition
if err = inArray.ForEach(&inPartition, func(i int64) error {
expirationEpochs, err := migrateAMTRaw(ctx, store, inPartition.ExpirationsEpochs, miner3.PartitionExpirationAmtBitwidth)
if err != nil {
return xerrors.Errorf("expiration queue: %w", err)
}
earlyTerminated, err := migrateAMTRaw(ctx, store, inPartition.EarlyTerminated, miner3.PartitionEarlyTerminationArrayAmtBitwidth)
if err != nil {
return xerrors.Errorf("early termination queue: %w", err)
}
outPartition := miner3.Partition{
Sectors: inPartition.Sectors,
Unproven: inPartition.Unproven,
Faults: inPartition.Faults,
Recoveries: inPartition.Recoveries,
Terminated: inPartition.Terminated,
ExpirationsEpochs: expirationEpochs,
EarlyTerminated: earlyTerminated,
LivePower: miner3.PowerPair(inPartition.LivePower),
UnprovenPower: miner3.PowerPair(inPartition.UnprovenPower),
FaultyPower: miner3.PowerPair(inPartition.FaultyPower),
RecoveringPower: miner3.PowerPair(inPartition.RecoveringPower),
}
return outArray.Set(uint64(i), &outPartition)
}); err != nil {
return cid.Undef, err
}
return outArray.Root()
}