/
miner_state.go
333 lines (272 loc) · 12.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
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
package miner
import (
addr "github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/go-state-types/builtin/v11/util/adt"
"github.com/filecoin-project/go-state-types/dline"
xc "github.com/filecoin-project/go-state-types/exitcode"
cid "github.com/ipfs/go-cid"
"golang.org/x/xerrors"
)
// Balance of Miner Actor should be greater than or equal to
// the sum of PreCommitDeposits and LockedFunds.
// It is possible for balance to fall below the sum of
// PCD, LF and InitialPledgeRequirements, and this is a bad
// state (IP Debt) that limits a miner actor's behavior (i.e. no balance withdrawals)
// Excess balance as computed by st.GetAvailableBalance will be
// withdrawable or usable for pre-commit deposit or pledge lock-up.
type State struct {
// Information not related to sectors.
Info cid.Cid
PreCommitDeposits abi.TokenAmount // Total funds locked as PreCommitDeposits
LockedFunds abi.TokenAmount // Total rewards and added funds locked in vesting table
VestingFunds cid.Cid // VestingFunds (Vesting Funds schedule for the miner).
FeeDebt abi.TokenAmount // Absolute value of debt this miner owes from unpaid fees
InitialPledge abi.TokenAmount // Sum of initial pledge requirements of all active sectors
// Sectors that have been pre-committed but not yet proven.
PreCommittedSectors cid.Cid // Map, HAMT[SectorNumber]SectorPreCommitOnChainInfo
// PreCommittedSectorsCleanUp maintains the state required to cleanup expired PreCommittedSectors.
PreCommittedSectorsCleanUp cid.Cid // BitFieldQueue (AMT[Epoch]*BitField)
// Allocated sector IDs. Sector IDs can never be reused once allocated.
AllocatedSectors cid.Cid // BitField
// Information for all proven and not-yet-garbage-collected sectors.
//
// Sectors are removed from this AMT when the partition to which the
// sector belongs is compacted.
Sectors cid.Cid // Array, AMT[SectorNumber]SectorOnChainInfo (sparse)
// DEPRECATED. This field will change names and no longer be updated every proving period in a future upgrade
// The first epoch in this miner's current proving period. This is the first epoch in which a PoSt for a
// partition at the miner's first deadline may arrive. Alternatively, it is after the last epoch at which
// a PoSt for the previous window is valid.
// Always greater than zero, this may be greater than the current epoch for genesis miners in the first
// WPoStProvingPeriod epochs of the chain; the epochs before the first proving period starts are exempt from Window
// PoSt requirements.
// Updated at the end of every period by a cron callback.
ProvingPeriodStart abi.ChainEpoch
// DEPRECATED. This field will be removed from state in a future upgrade.
// Index of the deadline within the proving period beginning at ProvingPeriodStart that has not yet been
// finalized.
// Updated at the end of each deadline window by a cron callback.
CurrentDeadline uint64
// The sector numbers due for PoSt at each deadline in the current proving period, frozen at period start.
// New sectors are added and expired ones removed at proving period boundary.
// Faults are not subtracted from this in state, but on the fly.
Deadlines cid.Cid
// Deadlines with outstanding fees for early sector termination.
EarlyTerminations bitfield.BitField
// True when miner cron is active, false otherwise
DeadlineCronActive bool
}
// Bitwidth of AMTs determined empirically from mutation patterns and projections of mainnet data.
const PrecommitCleanUpAmtBitwidth = 6
const SectorsAmtBitwidth = 5
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.
// Additional addresses that are permitted to submit messages controlling this actor (optional).
ControlAddresses []addr.Address // Must all be ID addresses.
PendingWorkerKey *WorkerKeyChange
// Byte array representing a Libp2p identity that should be used when connecting to this miner.
PeerId abi.PeerID
// Slice of byte arrays representing Libp2p multi-addresses used for establishing a connection with this miner.
Multiaddrs []abi.Multiaddrs
// The proof type used for Window PoSt for this miner.
// A miner may commit sectors with different seal proof types (but compatible sector size and
// corresponding PoSt proof types).
WindowPoStProofType abi.RegisteredPoStProof
// Amount of space in each sector committed by this miner.
// This is computed from the proof type and represented here redundantly.
SectorSize abi.SectorSize
// The number of sectors in each Window PoSt partition (proof).
// This is computed from the proof type and represented here redundantly.
WindowPoStPartitionSectors uint64
// The next epoch this miner is eligible for certain permissioned actor methods
// and winning block elections as a result of being reported for a consensus fault.
ConsensusFaultElapsed abi.ChainEpoch
// A proposed new owner account for this miner.
// Must be confirmed by a message from the pending address itself.
PendingOwnerAddress *addr.Address
// Beneficiary address for this miner.
// This is the address that tokens will be withdrawn to
Beneficiary addr.Address
// Beneficiary's withdrawal quota, how much of the quota has been withdrawn,
// and when the Beneficiary expires.
BeneficiaryTerm BeneficiaryTerm
// A proposed change to `BenificiaryTerm`
PendingBeneficiaryTerm *PendingBeneficiaryChange
}
type WorkerKeyChange struct {
NewWorker addr.Address // Must be an ID address
EffectiveAt abi.ChainEpoch
}
// Information provided by a miner when pre-committing a sector.
type SectorPreCommitInfo struct {
SealProof abi.RegisteredSealProof
SectorNumber abi.SectorNumber
SealedCID cid.Cid `checked:"true"` // CommR
SealRandEpoch abi.ChainEpoch
DealIDs []abi.DealID
Expiration abi.ChainEpoch
UnsealedCid *cid.Cid
}
// Information stored on-chain for a pre-committed sector.
type SectorPreCommitOnChainInfo struct {
Info SectorPreCommitInfo
PreCommitDeposit abi.TokenAmount
PreCommitEpoch abi.ChainEpoch
}
// Information stored on-chain for a proven sector.
type SectorOnChainInfo struct {
SectorNumber abi.SectorNumber
SealProof abi.RegisteredSealProof // The seal proof type implies the PoSt proof/s
SealedCID cid.Cid // CommR
DealIDs []abi.DealID
Activation abi.ChainEpoch // Epoch during which the sector proof was accepted
Expiration abi.ChainEpoch // Epoch during which the sector expires
DealWeight abi.DealWeight // Integral of active deals over sector lifetime
VerifiedDealWeight abi.DealWeight // Integral of active verified deals over sector lifetime
InitialPledge abi.TokenAmount // Pledge collected to commit this sector
ExpectedDayReward abi.TokenAmount // Expected one day projection of reward for sector computed at activation time
ExpectedStoragePledge abi.TokenAmount // Expected twenty day projection of reward for sector computed at activation time
ReplacedSectorAge abi.ChainEpoch // Age of sector this sector replaced or zero
ReplacedDayReward abi.TokenAmount // Day reward of sector this sector replace or zero
SectorKeyCID *cid.Cid // The original SealedSectorCID, only gets set on the first ReplicaUpdate
SimpleQAPower bool // Flag for QA power mechanism introduced in FIP-0045
}
func (st *State) GetInfo(store adt.Store) (*MinerInfo, error) {
var info MinerInfo
if err := store.Get(store.Context(), st.Info, &info); err != nil {
return nil, xerrors.Errorf("failed to get miner info %w", err)
}
return &info, nil
}
type BeneficiaryTerm struct {
Quota abi.TokenAmount
UsedQuota abi.TokenAmount
Expiration abi.ChainEpoch
}
type PendingBeneficiaryChange struct {
NewBeneficiary addr.Address
NewQuota abi.TokenAmount
NewExpiration abi.ChainEpoch
ApprovedByBeneficiary bool
ApprovedByNominee bool
}
// Returns deadline calculations for the state recorded proving period and deadline. This is out of date if the a
// miner does not have an active miner cron
func (st *State) RecordedDeadlineInfo(currEpoch abi.ChainEpoch) *dline.Info {
return NewDeadlineInfo(st.ProvingPeriodStart, st.CurrentDeadline, currEpoch)
}
// Returns deadline calculations for the current (according to state) proving period
func (st *State) QuantSpecForDeadline(dlIdx uint64) builtin.QuantSpec {
return QuantSpecForDeadline(NewDeadlineInfo(st.ProvingPeriodStart, dlIdx, 0))
}
func (st *State) GetPrecommittedSector(store adt.Store, sectorNo abi.SectorNumber) (*SectorPreCommitOnChainInfo, bool, error) {
precommitted, err := adt.AsMap(store, st.PreCommittedSectors, builtin.DefaultHamtBitwidth)
if err != nil {
return nil, false, err
}
var info SectorPreCommitOnChainInfo
found, err := precommitted.Get(SectorKey(sectorNo), &info)
if err != nil {
return nil, false, xerrors.Errorf("failed to load precommitment for %v: %w", sectorNo, err)
}
return &info, found, nil
}
func (st *State) GetSector(store adt.Store, sectorNo abi.SectorNumber) (*SectorOnChainInfo, bool, error) {
sectors, err := LoadSectors(store, st.Sectors)
if err != nil {
return nil, false, err
}
return sectors.Get(sectorNo)
}
func (st *State) FindSector(store adt.Store, sno abi.SectorNumber) (uint64, uint64, error) {
deadlines, err := st.LoadDeadlines(store)
if err != nil {
return 0, 0, err
}
return FindSector(store, deadlines, sno)
}
func (st *State) LoadDeadlines(store adt.Store) (*Deadlines, error) {
var deadlines Deadlines
if err := store.Get(store.Context(), st.Deadlines, &deadlines); err != nil {
return nil, xc.ErrIllegalState.Wrapf("failed to load deadlines (%s): %w", st.Deadlines, err)
}
return &deadlines, nil
}
func (st *State) SaveDeadlines(store adt.Store, deadlines *Deadlines) error {
c, err := store.Put(store.Context(), deadlines)
if err != nil {
return err
}
st.Deadlines = c
return nil
}
// LoadVestingFunds loads the vesting funds table from the store
func (st *State) LoadVestingFunds(store adt.Store) (*VestingFunds, error) {
var funds VestingFunds
if err := store.Get(store.Context(), st.VestingFunds, &funds); err != nil {
return nil, xerrors.Errorf("failed to load vesting funds (%s): %w", st.VestingFunds, err)
}
return &funds, nil
}
// CheckVestedFunds returns the amount of vested funds that have vested before the provided epoch.
func (st *State) CheckVestedFunds(store adt.Store, currEpoch abi.ChainEpoch) (abi.TokenAmount, error) {
vestingFunds, err := st.LoadVestingFunds(store)
if err != nil {
return big.Zero(), xerrors.Errorf("failed to load vesting funds: %w", err)
}
amountVested := abi.NewTokenAmount(0)
for i := range vestingFunds.Funds {
vf := vestingFunds.Funds[i]
epoch := vf.Epoch
amount := vf.Amount
if epoch >= currEpoch {
break
}
amountVested = big.Add(amountVested, amount)
}
return amountVested, nil
}
// Unclaimed funds that are not locked -- includes free funds and does not
// account for fee debt. Always greater than or equal to zero
func (st *State) GetUnlockedBalance(actorBalance abi.TokenAmount) (abi.TokenAmount, error) {
unlockedBalance := big.Subtract(actorBalance, st.LockedFunds, st.PreCommitDeposits, st.InitialPledge)
if unlockedBalance.LessThan(big.Zero()) {
return big.Zero(), xerrors.Errorf("negative unlocked balance %v", unlockedBalance)
}
return unlockedBalance, nil
}
// Unclaimed funds. Actor balance - (locked funds, precommit deposit, initial pledge, fee debt)
// Can go negative if the miner is in IP debt
func (st *State) GetAvailableBalance(actorBalance abi.TokenAmount) (abi.TokenAmount, error) {
unlockedBalance, err := st.GetUnlockedBalance(actorBalance)
if err != nil {
return big.Zero(), err
}
return big.Subtract(unlockedBalance, st.FeeDebt), nil
}
//
// Misc helpers
//
func SectorKey(e abi.SectorNumber) abi.Keyer {
return abi.UIntKey(uint64(e))
}
// pre-commit clean up
func (st *State) QuantSpecEveryDeadline() builtin.QuantSpec {
return builtin.NewQuantSpec(WPoStChallengeWindow, st.ProvingPeriodStart)
}
// Return true when the miner actor needs to continue scheduling deadline crons
func (st *State) ContinueDeadlineCron() bool {
return !st.PreCommitDeposits.IsZero() ||
!st.InitialPledge.IsZero() ||
!st.LockedFunds.IsZero()
}