-
Notifications
You must be signed in to change notification settings - Fork 38
/
miner.go
202 lines (167 loc) · 6.6 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
package migration
import (
"context"
"sync"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-amt-ipld/v4"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/builtin/v13/miner"
"github.com/filecoin-project/go-state-types/builtin/v13/util/adt"
miner12 "github.com/filecoin-project/go-state-types/builtin/v12/miner"
"github.com/filecoin-project/go-state-types/migration"
)
type providerSectors struct {
lk sync.Mutex
// Populated ONLY on the first run-through, when there's no cache to diff against
dealToSector map[abi.DealID]abi.SectorID
minerToSectorToDeals map[abi.ActorID]map[abi.SectorNumber][]abi.DealID
// Populated ONLY on future run-throughs if there is a cache to diff against
newDealsToSector map[abi.DealID]abi.SectorID
updatesToMinerToSectorToDeals map[abi.ActorID]map[abi.SectorNumber][]abi.DealID
}
// minerMigration is technically a no-op, but it collects a cache for market migration
type minerMigrator struct {
providerSectors *providerSectors
upgradeEpoch abi.ChainEpoch
OutCodeCID cid.Cid
}
func newMinerMigrator(ctx context.Context, store cbor.IpldStore, outCode cid.Cid, ps *providerSectors, upgradeEpoch abi.ChainEpoch) (*minerMigrator, error) {
return &minerMigrator{
providerSectors: ps,
upgradeEpoch: upgradeEpoch,
OutCodeCID: outCode,
}, nil
}
func (m *minerMigrator) MigrateState(ctx context.Context, store cbor.IpldStore, in migration.ActorMigrationInput) (result *migration.ActorMigrationResult, err error) {
var inState miner12.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)
}
ctxStore := adt.WrapStore(ctx, store)
mid, err := address.IDFromAddress(in.Address)
if err != nil {
return nil, xerrors.Errorf("failed to get miner ID: %w", err)
}
inSectors, err := adt.AsArray(ctxStore, inState.Sectors, miner12.SectorsAmtBitwidth)
if err != nil {
return nil, xerrors.Errorf("failed to load sectors array: %w", err)
}
hasCached, prevSectors, err := in.Cache.Read(migration.MinerPrevSectorsInKey(in.Address))
if err != nil {
return nil, xerrors.Errorf("failed to read prev sectors from cache: %w", err)
}
var sector miner.SectorOnChainInfo
if !hasCached {
// no cached migration, so we simply iterate all sectors and collect deal IDs
err = inSectors.ForEach(§or, func(i int64) error {
if len(sector.DealIDs) == 0 || sector.Expiration < m.upgradeEpoch {
return nil
}
m.providerSectors.lk.Lock()
for _, dealID := range sector.DealIDs {
m.providerSectors.dealToSector[dealID] = abi.SectorID{
Miner: abi.ActorID(mid),
Number: abi.SectorNumber(i),
}
}
sectorDeals, ok := m.providerSectors.minerToSectorToDeals[abi.ActorID(mid)]
if !ok {
sectorDeals = make(map[abi.SectorNumber][]abi.DealID)
m.providerSectors.minerToSectorToDeals[abi.ActorID(mid)] = sectorDeals
}
// There can't already be an entry for this sector, so just set (not append)
// TODO: golang: I need to copy this, since sector is a reference that's constantly updated? Right? Steb?
sectorDealIDs := sector.DealIDs
sectorDeals[abi.SectorNumber(i)] = sectorDealIDs
m.providerSectors.lk.Unlock()
return nil
})
if err != nil {
return nil, xerrors.Errorf("failed to iterate sectors: %w", err)
}
} else {
prevInSectors, err := adt.AsArray(ctxStore, prevSectors, miner12.SectorsAmtBitwidth)
if err != nil {
return nil, xerrors.Errorf("failed to load previous input sectors array: %w", err)
}
diffs, err := amt.Diff(ctx, store, store, prevSectors, inState.Sectors, amt.UseTreeBitWidth(miner12.SectorsAmtBitwidth))
if err != nil {
return nil, xerrors.Errorf("failed to diff old and new Sector AMTs: %w", err)
}
for _, change := range diffs {
sectorNo := abi.SectorNumber(change.Key)
switch change.Type {
case amt.Add:
fallthrough
case amt.Modify:
found, err := inSectors.Get(change.Key, §or)
if err != nil {
return nil, xerrors.Errorf("failed to get sector %d in inSectors: %w", sectorNo, err)
}
if !found {
return nil, xerrors.Errorf("didn't find sector %d in inSectors", sectorNo)
}
if len(sector.DealIDs) == 0 {
// if no deals don't even bother taking the lock
continue
}
m.providerSectors.lk.Lock()
for _, dealID := range sector.DealIDs {
m.providerSectors.newDealsToSector[dealID] = abi.SectorID{
Miner: abi.ActorID(mid),
Number: sectorNo,
}
}
sectorDeals, ok := m.providerSectors.updatesToMinerToSectorToDeals[abi.ActorID(mid)]
if !ok {
sectorDeals = make(map[abi.SectorNumber][]abi.DealID)
m.providerSectors.updatesToMinerToSectorToDeals[abi.ActorID(mid)] = sectorDeals
}
// There can't already be an entry for this sector, so just set (not append)
// TODO: golang: I need to copy this, since sector is a reference that's constantly updated? Right? Steb?
sectorDealIDs := sector.DealIDs
sectorDeals[sectorNo] = sectorDealIDs
m.providerSectors.lk.Unlock()
case amt.Remove:
// In this unlikely case, we need to load the deleted SectorOnChainInfo, and mark it as not having any deals
var oldSector miner.SectorOnChainInfo
found, err := prevInSectors.Get(change.Key, &oldSector)
if err != nil {
return nil, xerrors.Errorf("failed to get previous sector info: %w", err)
}
if !found {
return nil, xerrors.Errorf("didn't find previous sector info for %d", sectorNo)
}
if len(oldSector.DealIDs) != 0 && oldSector.Expiration >= m.upgradeEpoch {
m.providerSectors.lk.Lock()
sectorDeals, ok := m.providerSectors.updatesToMinerToSectorToDeals[abi.ActorID(mid)]
if !ok {
sectorDeals = make(map[abi.SectorNumber][]abi.DealID)
m.providerSectors.updatesToMinerToSectorToDeals[abi.ActorID(mid)] = sectorDeals
}
// TODO: Is it better to use nil to communicate deals to be removed, or just a separate map (of maps)?
sectorDeals[sectorNo] = nil
m.providerSectors.lk.Unlock()
}
}
}
}
err = in.Cache.Write(migration.MinerPrevSectorsInKey(in.Address), inState.Sectors)
if err != nil {
return nil, xerrors.Errorf("failed to write prev sectors to cache: %w", err)
}
return &migration.ActorMigrationResult{
NewCodeCID: m.MigratedCodeCID(),
NewHead: in.Head,
}, nil
}
func (m *minerMigrator) MigratedCodeCID() cid.Cid {
return m.OutCodeCID
}
func (m *minerMigrator) Deferred() bool {
return false
}
var _ migration.ActorMigration = (*minerMigrator)(nil)