-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtop.go
361 lines (307 loc) · 11.4 KB
/
top.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
package migration
import (
"context"
"sync"
"sync/atomic"
"time"
init10 "github.com/filecoin-project/go-state-types/builtin/v10/init"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/builtin"
system9 "github.com/filecoin-project/go-state-types/builtin/v9/system"
adt9 "github.com/filecoin-project/go-state-types/builtin/v9/util/adt"
"github.com/filecoin-project/go-state-types/manifest"
"github.com/filecoin-project/go-state-types/migration"
"github.com/filecoin-project/go-state-types/rt"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
)
// MigrateStateTree Migrates the filecoin state tree starting from the global state tree and upgrading all actor state.
// The store must support concurrent writes (even if the configured worker count is 1).
func MigrateStateTree(ctx context.Context, store cbor.IpldStore, newManifestCID cid.Cid, actorsRootIn cid.Cid, priorEpoch abi.ChainEpoch, cfg migration.Config, log migration.Logger, cache migration.MigrationCache) (cid.Cid, error) {
if cfg.MaxWorkers <= 0 {
return cid.Undef, xerrors.Errorf("invalid migration config with %d workers", cfg.MaxWorkers)
}
adtStore := adt9.WrapStore(ctx, store)
// Load input and output state trees
actorsIn, err := builtin.LoadTree(adtStore, actorsRootIn)
if err != nil {
return cid.Undef, xerrors.Errorf("loading state tree: %w", err)
}
actorsOut, err := builtin.NewTree(adtStore)
if err != nil {
return cid.Undef, xerrors.Errorf("creating new state tree: %w", err)
}
// load old manifest data
systemActor, ok, err := actorsIn.GetActorV4(builtin.SystemActorAddr)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get system actor: %w", err)
}
if !ok {
return cid.Undef, xerrors.New("didn't find system actor")
}
var systemState system9.State
if err := store.Get(ctx, systemActor.Head, &systemState); err != nil {
return cid.Undef, xerrors.Errorf("failed to get system actor state: %w", err)
}
var oldManifestData manifest.ManifestData
if err := store.Get(ctx, systemState.BuiltinActors, &oldManifestData); err != nil {
return cid.Undef, xerrors.Errorf("failed to get old manifest data: %w", err)
}
// load new manifest
var newManifest manifest.Manifest
if err := adtStore.Get(ctx, newManifestCID, &newManifest); err != nil {
return cid.Undef, xerrors.Errorf("error reading actor manifest: %w", err)
}
if err := newManifest.Load(ctx, adtStore); err != nil {
return cid.Undef, xerrors.Errorf("error loading actor manifest: %w", err)
}
// Maps prior version code CIDs to migration functions.
migrations := make(map[cid.Cid]migration.ActorMigration)
// Set of prior version code CIDs for actors to defer during iteration, for explicit migration afterwards.
deferredCodeIDs := make(map[cid.Cid]struct{})
oldInitCodeCID := cid.Undef
for _, oldEntry := range oldManifestData.Entries {
newCodeCID, ok := newManifest.Get(oldEntry.Name)
if !ok {
return cid.Undef, xerrors.Errorf("code cid for %s actor not found in new manifest", oldEntry.Name)
}
migrations[oldEntry.Code] = migration.CodeMigrator{OutCodeCID: newCodeCID}
if oldEntry.Name == manifest.InitKey {
oldInitCodeCID = oldEntry.Code
}
}
if !oldInitCodeCID.Defined() {
return cid.Undef, xerrors.New("didn't find init actor in old manifest")
}
// migrations that migrate both code and state, override entries in `migrations`
// The System Actor
newSystemCodeCID, ok := newManifest.Get(manifest.SystemKey)
if !ok {
return cid.Undef, xerrors.Errorf("code cid for system actor not found in new manifest")
}
migrations[systemActor.Code] = systemActorMigrator{OutCodeCID: newSystemCodeCID, ManifestData: newManifest.Data}
// The Init Actor
newInitCodeCID, ok := newManifest.Get(manifest.InitKey)
if !ok {
return cid.Undef, xerrors.Errorf("code cid for init actor not found in new manifest")
}
ethZeroAddr, err := makeEthZeroAddress()
if err != nil {
return cid.Undef, xerrors.Errorf("failed to make eth zero address: %w", err)
}
migrations[oldInitCodeCID] = initActorMigrator{OutCodeCID: newInitCodeCID, EthZeroAddress: ethZeroAddr}
if len(migrations)+len(deferredCodeIDs) != len(oldManifestData.Entries) {
return cid.Undef, xerrors.Errorf("incomplete migration specification with %d code CIDs, need %d", len(migrations), len(oldManifestData.Entries))
}
startTime := time.Now()
// Setup synchronization
grp, ctx := errgroup.WithContext(ctx)
// Input and output queues for workers.
jobCh := make(chan *migrationJob, cfg.JobQueueSize)
jobResultCh := make(chan *migrationJobResult, cfg.ResultQueueSize)
// Atomically-modified counters for logging progress
var jobCount uint32
var doneCount uint32
// Iterate all actors in old state root to create migration jobs for each non-deferred actor.
grp.Go(func() error {
defer close(jobCh)
log.Log(rt.INFO, "Creating migration jobs for tree %s", actorsRootIn)
if err = actorsIn.ForEachV4(func(addr address.Address, actorIn *builtin.ActorV4) error {
if _, ok := deferredCodeIDs[actorIn.Code]; ok {
return nil
}
migration, ok := migrations[actorIn.Code]
if !ok {
return xerrors.Errorf("actor with code %s has no registered migration function", actorIn.Code)
}
nextInput := &migrationJob{
Address: addr,
ActorV4: *actorIn, // Must take a copy, the pointer is not stable.
cache: cache,
ActorMigration: migration,
}
select {
case jobCh <- nextInput:
case <-ctx.Done():
return ctx.Err()
}
atomic.AddUint32(&jobCount, 1)
return nil
}); err != nil {
return xerrors.Errorf("error iterating v4 actors: %w", err)
}
log.Log(rt.INFO, "Done creating %d migration jobs for tree %s after %v", jobCount, actorsRootIn, time.Since(startTime))
return nil
})
// Worker threads run jobs.
var workerWg sync.WaitGroup
for i := uint(0); i < cfg.MaxWorkers; i++ {
workerWg.Add(1)
workerId := i
grp.Go(func() error {
defer workerWg.Done()
for job := range jobCh {
result, err := job.run(ctx, store, priorEpoch)
if err != nil {
return xerrors.Errorf("running job: %w", err)
}
select {
case jobResultCh <- result:
case <-ctx.Done():
return ctx.Err()
}
atomic.AddUint32(&doneCount, 1)
}
log.Log(rt.INFO, "Worker %d done", workerId)
return nil
})
}
log.Log(rt.INFO, "Started %d workers", cfg.MaxWorkers)
// Monitor the job queue. This non-critical goroutine is outside the errgroup and exits when
// workersFinished is closed, or the context done.
workersFinished := make(chan struct{}) // Closed when waitgroup is emptied.
if cfg.ProgressLogPeriod > 0 {
go func() {
defer log.Log(rt.DEBUG, "Job queue monitor done")
for {
select {
case <-time.After(cfg.ProgressLogPeriod):
jobsNow := jobCount // Snapshot values to avoid incorrect-looking arithmetic if they change.
doneNow := doneCount
pendingNow := jobsNow - doneNow
elapsed := time.Since(startTime)
rate := float64(doneNow) / elapsed.Seconds()
log.Log(rt.INFO, "%d jobs created, %d done, %d pending after %v (%.0f/s)",
jobsNow, doneNow, pendingNow, elapsed, rate)
case <-workersFinished:
return
case <-ctx.Done():
return
}
}
}()
}
// Close result channel when workers are done sending to it.
grp.Go(func() error {
workerWg.Wait()
close(jobResultCh)
close(workersFinished)
log.Log(rt.INFO, "All workers done after %v", time.Since(startTime))
return nil
})
// Insert migrated records in output state tree and accumulators.
grp.Go(func() error {
log.Log(rt.INFO, "Result writer started")
resultCount := 0
for result := range jobResultCh {
if err := actorsOut.SetActorV5(result.Address, &result.ActorV5); err != nil {
return xerrors.Errorf("error setting actor %s: %w", result.Address, err)
}
resultCount++
}
log.Log(rt.INFO, "Result writer wrote %d results to state tree after %v", resultCount, time.Since(startTime))
return nil
})
if err := grp.Wait(); err != nil {
return cid.Undef, xerrors.Errorf("migration group error: %w", err)
}
// Make the empty state
emptyObj, err := makeEmptyState(store)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to make empty obj: %w", err)
}
// Create the EAM Actor
eamAct, err := CreateEAMActor(&newManifest, emptyObj)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create the EAM Actor: %w", err)
}
if err := actorsOut.SetActorV5(builtin.EthereumAddressManagerActorAddr, eamAct); err != nil {
return cid.Undef, xerrors.Errorf("failed to set EAM Actor: %w", err)
}
// Create the EthZeroAddress as an EthAccount
initAct, ok, err := actorsOut.GetActorV5(builtin.InitActorAddr)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get init actor: %w", err)
}
if !ok {
return cid.Undef, xerrors.New("didn't find init actor")
}
var initSt init10.State
if err := store.Get(ctx, initAct.Head, &initSt); err != nil {
return cid.Undef, xerrors.Errorf("failed to get init state: %w", err)
}
ethZeroAddrId, ok, err := initSt.ResolveAddress(adt9.WrapStore(ctx, store), ethZeroAddr)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get ethZero actor: %w", err)
}
if !ok {
return cid.Undef, xerrors.New("didn't find ethZero actor")
}
ethAccountCID, ok := newManifest.Get(manifest.EthAccountKey)
if !ok {
return cid.Undef, xerrors.New("didn't find ethAccount code CID")
}
if err := actorsOut.SetActorV5(ethZeroAddrId, &builtin.ActorV5{
Code: ethAccountCID,
Head: emptyObj,
CallSeqNum: 0,
Balance: abi.NewTokenAmount(0),
Address: ðZeroAddr,
}); err != nil {
return cid.Undef, xerrors.Errorf("failed to set ethZeroActor: %w", err)
}
elapsed := time.Since(startTime)
rate := float64(doneCount) / elapsed.Seconds()
log.Log(rt.INFO, "All %d done after %v (%.0f/s), flushing state root.", doneCount, elapsed, rate)
outCid, err := actorsOut.Flush()
if err != nil {
return cid.Undef, xerrors.Errorf("failed to flush actorsOut: %w", err)
}
return outCid, nil
}
type migrationJob struct {
address.Address
builtin.ActorV4
migration.ActorMigration
cache migration.MigrationCache
}
type migrationJobResult struct {
address.Address
builtin.ActorV5
}
func (job *migrationJob) run(ctx context.Context, store cbor.IpldStore, priorEpoch abi.ChainEpoch) (*migrationJobResult, error) {
result, err := job.MigrateState(ctx, store, migration.ActorMigrationInput{
Address: job.Address,
Head: job.ActorV4.Head,
PriorEpoch: priorEpoch,
Cache: job.cache,
})
if err != nil {
return nil, xerrors.Errorf("state migration failed for actor code %s, addr %s: %w",
job.ActorV4.Code, job.Address, err)
}
// Set up new actor record with the migrated state.
return &migrationJobResult{
job.Address, // Unchanged
builtin.ActorV5{
Code: result.NewCodeCID,
Head: result.NewHead,
CallSeqNum: job.ActorV4.CallSeqNum, // Unchanged
Balance: job.ActorV4.Balance, // Unchanged
Address: nil, // TODO check that this does not need to be populated
},
}, nil
}
func makeEthZeroAddress() (address.Address, error) {
return address.NewDelegatedAddress(builtin.EthereumAddressManagerActorID, make([]byte, 20))
}
func makeEmptyState(store cbor.IpldStore) (cid.Cid, error) {
emptyObject, err := store.Put(context.TODO(), []struct{}{})
if err != nil {
return cid.Undef, xerrors.Errorf("failed to make empty object: %w", err)
}
return emptyObject, nil
}