-
Notifications
You must be signed in to change notification settings - Fork 35
/
top.go
149 lines (117 loc) · 5.29 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
package migration
import (
"context"
adt13 "github.com/filecoin-project/go-state-types/builtin/v13/util/adt"
system12 "github.com/filecoin-project/go-state-types/builtin/v12/system"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/go-state-types/manifest"
"github.com/filecoin-project/go-state-types/migration"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"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 := adt13.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)
}
// load old manifest data
systemActor, ok, err := actorsIn.GetActorV5(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 system12.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{})
market12Cid := cid.Undef
miner12Cid := cid.Undef
for _, oldEntry := range oldManifestData.Entries {
if oldEntry.Name == manifest.MarketKey {
market12Cid = oldEntry.Code
}
if oldEntry.Name == manifest.MinerKey {
miner12Cid = oldEntry.Code
}
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.CachedMigration(cache, migration.CodeMigrator{OutCodeCID: newCodeCID})
}
if market12Cid == cid.Undef || miner12Cid == cid.Undef {
return cid.Undef, xerrors.Errorf("could not find market or miner 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}
// Miner actors
miner13Cid, ok := newManifest.Get(manifest.MinerKey)
if !ok {
return cid.Undef, xerrors.Errorf("code cid for miner actor not found in new manifest")
}
ps := &providerSectors{
dealToSector: map[abi.DealID]abi.SectorID{},
minerToSectorToDeals: map[abi.ActorID]map[abi.SectorNumber][]abi.DealID{},
newDealsToSector: map[abi.DealID]abi.SectorID{},
updatesToMinerToSectorToDeals: map[abi.ActorID]map[abi.SectorNumber][]abi.DealID{},
}
minerMig, err := newMinerMigrator(ctx, store, miner13Cid, ps, cfg.UpgradeEpoch)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create miner migrator: %w", err)
}
migrations[miner12Cid] = migration.CachedMigration(cache, minerMig)
// The Market Actor
market13Cid, ok := newManifest.Get(manifest.MarketKey)
if !ok {
return cid.Undef, xerrors.Errorf("code cid for market actor not found in new manifest")
}
marketMig, err := newMarketMigrator(ctx, store, market13Cid, ps, cfg.UpgradeEpoch)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create market migrator: %w", err)
}
migrations[market12Cid] = migration.CachedMigration(cache, marketMig)
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(deferredCodeIDs), len(oldManifestData.Entries))
}
actorsOut, err := migration.RunMigration(ctx, cfg, cache, store, log, actorsIn, migrations)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to run migration: %w", err)
}
outCid, err := actorsOut.Flush()
if err != nil {
return cid.Undef, xerrors.Errorf("failed to flush actorsOut: %w", err)
}
return outCid, nil
}