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_state.go
302 lines (255 loc) · 9.6 KB
/
miner_state.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
package miner
import (
"reflect"
addr "github.com/filecoin-project/go-address"
cid "github.com/ipfs/go-cid"
peer "github.com/libp2p/go-libp2p-core/peer"
errors "github.com/pkg/errors"
xerrors "golang.org/x/xerrors"
abi "github.com/filecoin-project/specs-actors/actors/abi"
power "github.com/filecoin-project/specs-actors/actors/builtin/power"
exitcode "github.com/filecoin-project/specs-actors/actors/runtime/exitcode"
adt "github.com/filecoin-project/specs-actors/actors/util/adt"
)
// Balance of a Actor should equal exactly the sum of PreCommit deposits
// that are not yet returned or burned.
type State struct {
PreCommittedSectors cid.Cid // Map, HAMT[SectorNumber]SectorPreCommitOnChainInfo
Sectors cid.Cid // Array, AMT[]SectorOnChainInfo (sparse)
FaultSet abi.BitField
ProvingSet cid.Cid // Array, AMT[]SectorOnChainInfo (sparse)
Info MinerInfo // TODO: this needs to be a cid of the miner Info struct
PoStState PoStState
}
type MinerInfo struct {
// Account that owns this miner.
// - Income and returned collateral are paid to this address.
// - This address is also allowed to change the worker address for the miner.
Owner addr.Address // Must be an ID-address.
// Worker account for this miner.
// The associated pubkey-type address is used to sign blocks and messages on behalf of this miner.
Worker addr.Address // Must be an ID-address.
PendingWorkerKey *WorkerKeyChange
// Libp2p identity that should be used when connecting to this miner.
PeerId peer.ID
// Amount of space in each sector committed to the network by this miner.
SectorSize abi.SectorSize
}
type PoStState struct {
// Epoch that starts the current proving period
ProvingPeriodStart abi.ChainEpoch
// Number of surprised post challenges that have been failed since last successful PoSt.
// Indicates that the claimed storage power may not actually be proven. Recovery can proceed by
// submitting a correct response to a subsequent PoSt challenge, up until
// the limit of number of consecutive failures.
NumConsecutiveFailures int64
}
type WorkerKeyChange struct {
NewWorker addr.Address // Must be an ID address
EffectiveAt abi.ChainEpoch
}
type SectorPreCommitInfo struct {
RegisteredProof abi.RegisteredProof
SectorNumber abi.SectorNumber
SealedCID cid.Cid // CommR
SealRandEpoch abi.ChainEpoch
DealIDs []abi.DealID
Expiration abi.ChainEpoch // Sector Expiration
}
type SectorPreCommitOnChainInfo struct {
Info SectorPreCommitInfo
PreCommitDeposit abi.TokenAmount
PreCommitEpoch abi.ChainEpoch
}
type SectorOnChainInfo struct {
Info SectorPreCommitInfo
ActivationEpoch abi.ChainEpoch // Epoch at which SectorProveCommit is accepted
DealWeight abi.DealWeight // Integral of active deals over sector lifetime, 0 if CommittedCapacity sector
PledgeRequirement abi.TokenAmount // Fixed pledge collateral requirement determined at activation
DeclaredFaultEpoch abi.ChainEpoch // -1 if not currently declared faulted.
DeclaredFaultDuration abi.ChainEpoch // -1 if not currently declared faulted.
}
func ConstructState(emptyArrayCid, emptyMapCid cid.Cid, ownerAddr, workerAddr addr.Address, peerId peer.ID, sectorSize abi.SectorSize) *State {
return &State{
PreCommittedSectors: emptyMapCid,
Sectors: emptyArrayCid,
FaultSet: abi.NewBitField(),
ProvingSet: emptyArrayCid,
Info: MinerInfo{
Owner: ownerAddr,
Worker: workerAddr,
PendingWorkerKey: nil,
PeerId: peerId,
SectorSize: sectorSize,
},
PoStState: PoStState{
ProvingPeriodStart: epochUndefined,
NumConsecutiveFailures: 0,
},
}
}
func (st *State) GetWorker() addr.Address {
return st.Info.Worker
}
func (st *State) GetSectorSize() abi.SectorSize {
return st.Info.SectorSize
}
func (st *State) GetSectorCount(store adt.Store) (uint64, error) {
arr := adt.AsArray(store, st.Sectors)
return arr.Length()
}
func (st *State) GetMaxAllowedFaults(store adt.Store) (uint64, error) {
sectorCount, err := st.GetSectorCount(store)
if err != nil {
return 0, err
}
return 2 * sectorCount, nil
}
func (st *State) PutPrecommittedSector(store adt.Store, info *SectorPreCommitOnChainInfo) error {
precommitted := adt.AsMap(store, st.PreCommittedSectors)
err := precommitted.Put(sectorKey(info.Info.SectorNumber), info)
if err != nil {
return errors.Wrapf(err, "failed to store precommitment for %v", info)
}
st.PreCommittedSectors = precommitted.Root()
return nil
}
func (st *State) GetPrecommittedSector(store adt.Store, sectorNo abi.SectorNumber) (*SectorPreCommitOnChainInfo, bool, error) {
precommitted := adt.AsMap(store, st.PreCommittedSectors)
var info SectorPreCommitOnChainInfo
found, err := precommitted.Get(sectorKey(sectorNo), &info)
if err != nil {
return nil, false, errors.Wrapf(err, "failed to load precommitment for %v", sectorNo)
}
return &info, found, nil
}
func (st *State) DeletePrecommittedSector(store adt.Store, sectorNo abi.SectorNumber) error {
precommitted := adt.AsMap(store, st.PreCommittedSectors)
err := precommitted.Delete(sectorKey(sectorNo))
if err != nil {
return errors.Wrapf(err, "failed to delete precommitment for %v", sectorNo)
}
st.PreCommittedSectors = precommitted.Root()
return nil
}
func (st *State) HasSectorNo(store adt.Store, sectorNo abi.SectorNumber) (bool, error) {
sectors := adt.AsArray(store, st.Sectors)
var info SectorOnChainInfo
found, err := sectors.Get(uint64(sectorNo), &info)
if err != nil {
return false, xerrors.Errorf("failed to get sector %v: %w", sectorNo, err)
}
return found, nil
}
func (st *State) PutSector(store adt.Store, sector *SectorOnChainInfo) error {
sectors := adt.AsArray(store, st.Sectors)
if err := sectors.Set(uint64(sector.Info.SectorNumber), sector); err != nil {
return errors.Wrapf(err, "failed to put sector %v", sector)
}
st.Sectors = sectors.Root()
return nil
}
func (st *State) GetSector(store adt.Store, sectorNo abi.SectorNumber) (*SectorOnChainInfo, bool, error) {
sectors := adt.AsArray(store, st.Sectors)
var info SectorOnChainInfo
found, err := sectors.Get(uint64(sectorNo), &info)
if err != nil {
return nil, false, errors.Wrapf(err, "failed to get sector %v", sectorNo)
}
return &info, found, nil
}
func (st *State) DeleteSector(store adt.Store, sectorNo abi.SectorNumber) error {
sectors := adt.AsArray(store, st.Sectors)
if err := sectors.Delete(uint64(sectorNo)); err != nil {
return errors.Wrapf(err, "failed to delete sector %v", sectorNo)
}
st.Sectors = sectors.Root()
return nil
}
func (st *State) ForEachSector(store adt.Store, f func(*SectorOnChainInfo)) error {
sectors := adt.AsArray(store, st.Sectors)
var sector SectorOnChainInfo
return sectors.ForEach(§or, func(idx int64) error {
f(§or)
return nil
})
}
func (st *State) GetStorageWeightDescForSector(store adt.Store, sectorNo abi.SectorNumber) (*power.SectorStorageWeightDesc, error) {
sectorInfo, found, err := st.GetSector(store, sectorNo)
if err != nil {
return nil, err
} else if !found {
return nil, errors.Errorf("no such sector %v", sectorNo)
}
return asStorageWeightDesc(st.Info.SectorSize, sectorInfo), nil
}
func (st *State) InChallengeWindow(rt Runtime) bool {
return rt.CurrEpoch() > st.PoStState.ProvingPeriodStart // TODO: maybe also a few blocks beforehand?
}
func (st *State) ComputeProvingSet(store adt.Store) ([]abi.SectorInfo, error) {
// ProvingSet is a snapshot of the Sectors AMT, must subtract sectors in the FaultSet
provingSet := adt.AsArray(store, st.ProvingSet)
var sectorInfos []abi.SectorInfo
var ssinfo SectorOnChainInfo
maxAllowedFaults, err := st.GetMaxAllowedFaults(store)
if err != nil {
return nil, err
}
faultsMap, err := st.FaultSet.AllMap(maxAllowedFaults)
if err != nil {
return nil, err
}
err = provingSet.ForEach(&ssinfo, func(sectorNum int64) error {
_, fault := faultsMap[uint64(sectorNum)]
if ssinfo.DeclaredFaultEpoch != epochUndefined || ssinfo.DeclaredFaultDuration != epochUndefined {
return errors.Errorf("sector faultEpoch or duration invalid %v", ssinfo.Info.SectorNumber)
}
// if not a temp fault sector, add to computed proving set
if !fault {
sectorInfos = append(sectorInfos, abi.SectorInfo{
SealedCID: ssinfo.Info.SealedCID,
SectorNumber: ssinfo.Info.SectorNumber,
RegisteredProof: ssinfo.Info.RegisteredProof,
})
}
return nil
})
if err != nil {
return sectorInfos, errors.Wrapf(err, "failed to traverse sectors for proving set: %v", err)
}
return sectorInfos, nil
}
func (mps *PoStState) IsPoStOk() bool {
return !mps.HasFailedPost()
}
func (mps *PoStState) HasFailedPost() bool {
return mps.NumConsecutiveFailures > 0
}
func asStorageWeightDesc(sectorSize abi.SectorSize, sectorInfo *SectorOnChainInfo) *power.SectorStorageWeightDesc {
return &power.SectorStorageWeightDesc{
SectorSize: sectorSize,
DealWeight: sectorInfo.DealWeight,
Duration: sectorInfo.Info.Expiration - sectorInfo.ActivationEpoch,
}
}
func sectorKey(e abi.SectorNumber) adt.Keyer {
return adt.UIntKey(uint64(e))
}
func init() {
// Check that ChainEpoch is indeed an unsigned integer to confirm that sectorKey is making the right interpretation.
var e abi.SectorNumber
if reflect.TypeOf(e).Kind() != reflect.Uint64 {
panic("incorrect sector number encoding")
}
}
func bitfieldToSectorNos(rt Runtime, bf *abi.BitField) []abi.SectorNumber {
sns, err := bf.All(MaxFaultsCount)
if err != nil {
rt.Abortf(exitcode.ErrIllegalArgument, "failed to enumerate sector numbers from bitfield: %s", err)
}
var snums []abi.SectorNumber
for _, sn := range sns {
snums = append(snums, abi.SectorNumber(sn))
}
return snums
}