2020-07-01 13:30:25 +00:00
|
|
|
package sealing
|
|
|
|
|
|
|
|
import (
|
2020-07-15 14:51:02 +00:00
|
|
|
"context"
|
|
|
|
|
2020-09-17 02:34:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
|
2021-12-08 17:11:19 +00:00
|
|
|
market7 "github.com/filecoin-project/specs-actors/v7/actors/builtin/market"
|
2020-09-17 02:34:13 +00:00
|
|
|
|
2020-07-01 13:30:25 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2020-07-01 13:30:25 +00:00
|
|
|
)
|
|
|
|
|
2020-08-21 19:43:30 +00:00
|
|
|
func (m *Sealing) IsMarkedForUpgrade(id abi.SectorNumber) bool {
|
|
|
|
m.upgradeLk.Lock()
|
|
|
|
_, found := m.toUpgrade[id]
|
|
|
|
m.upgradeLk.Unlock()
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2021-12-08 17:11:19 +00:00
|
|
|
func (m *Sealing) MarkForUpgrade(ctx context.Context, id abi.SectorNumber) error {
|
|
|
|
|
2020-07-01 13:30:25 +00:00
|
|
|
m.upgradeLk.Lock()
|
|
|
|
defer m.upgradeLk.Unlock()
|
|
|
|
|
|
|
|
_, found := m.toUpgrade[id]
|
|
|
|
if found {
|
|
|
|
return xerrors.Errorf("sector %d already marked for upgrade", id)
|
|
|
|
}
|
|
|
|
|
2021-12-08 17:11:19 +00:00
|
|
|
si, err := m.GetSectorInfo(id)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting sector info: %w", err)
|
|
|
|
}
|
|
|
|
if si.State != Proving {
|
|
|
|
return xerrors.Errorf("can't mark sectors not in the 'Proving' state for upgrade")
|
|
|
|
}
|
|
|
|
if len(si.Pieces) != 1 {
|
|
|
|
return xerrors.Errorf("not a committed-capacity sector, expected 1 piece")
|
|
|
|
}
|
|
|
|
if si.Pieces[0].DealInfo != nil {
|
|
|
|
return xerrors.Errorf("not a committed-capacity sector, has deals")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.toUpgrade[id] = struct{}{}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Sealing) MarkForSnapUpgrade(ctx context.Context, id abi.SectorNumber) error {
|
|
|
|
cfg, err := m.getConfig()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting storage config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
curStaging := m.stats.curStaging()
|
|
|
|
if cfg.MaxWaitDealsSectors > 0 && curStaging >= cfg.MaxWaitDealsSectors {
|
|
|
|
return xerrors.Errorf("already waiting for deals in %d >= %d (cfg.MaxWaitDealsSectors) sectors, no free resources to wait for deals in another",
|
|
|
|
curStaging, cfg.MaxWaitDealsSectors)
|
|
|
|
}
|
|
|
|
|
2020-07-01 13:30:25 +00:00
|
|
|
si, err := m.GetSectorInfo(id)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting sector info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if si.State != Proving {
|
|
|
|
return xerrors.Errorf("can't mark sectors not in the 'Proving' state for upgrade")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(si.Pieces) != 1 {
|
|
|
|
return xerrors.Errorf("not a committed-capacity sector, expected 1 piece")
|
|
|
|
}
|
|
|
|
|
|
|
|
if si.Pieces[0].DealInfo != nil {
|
|
|
|
return xerrors.Errorf("not a committed-capacity sector, has deals")
|
|
|
|
}
|
|
|
|
|
2021-12-08 17:11:19 +00:00
|
|
|
tok, head, err := m.Api.ChainHead(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("couldnt get chain head: %w", err)
|
|
|
|
}
|
|
|
|
onChainInfo, err := m.Api.StateSectorGetInfo(ctx, m.maddr, id, tok)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to read sector on chain info: %w", err)
|
|
|
|
}
|
2020-07-01 13:30:25 +00:00
|
|
|
|
2021-12-08 17:11:19 +00:00
|
|
|
active, err := m.Api.StateMinerActiveSectors(ctx, m.maddr, tok)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to check active sectors: %w", err)
|
|
|
|
}
|
|
|
|
// Ensure the upgraded sector is active
|
|
|
|
var found bool
|
|
|
|
for _, si := range active {
|
|
|
|
if si.SectorNumber == id {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return xerrors.Errorf("cannot mark inactive sector for upgrade")
|
|
|
|
}
|
2020-07-01 13:30:25 +00:00
|
|
|
|
2021-12-08 17:11:19 +00:00
|
|
|
if onChainInfo.Expiration-head < market7.DealMinDuration {
|
|
|
|
return xerrors.Errorf("pointless to upgrade sector %d, expiration %d is less than a min deal duration away from current epoch."+
|
|
|
|
"Upgrade expiration before marking for upgrade", id, onChainInfo.Expiration)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.sectors.Send(uint64(id), SectorStartCCUpdate{})
|
2020-07-01 13:30:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 14:51:02 +00:00
|
|
|
func (m *Sealing) tryUpgradeSector(ctx context.Context, params *miner.SectorPreCommitInfo) big.Int {
|
2020-08-20 02:41:23 +00:00
|
|
|
if len(params.DealIDs) == 0 {
|
|
|
|
return big.Zero()
|
|
|
|
}
|
2020-07-01 14:33:59 +00:00
|
|
|
replace := m.maybeUpgradableSector()
|
|
|
|
if replace != nil {
|
2021-08-18 10:43:44 +00:00
|
|
|
loc, err := m.Api.StateSectorPartition(ctx, m.maddr, *replace, nil)
|
2020-07-15 14:51:02 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error calling StateSectorPartition for replaced sector: %+v", err)
|
|
|
|
return big.Zero()
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:33:59 +00:00
|
|
|
params.ReplaceCapacity = true
|
2020-07-15 14:51:02 +00:00
|
|
|
params.ReplaceSectorNumber = *replace
|
|
|
|
params.ReplaceSectorDeadline = loc.Deadline
|
|
|
|
params.ReplaceSectorPartition = loc.Partition
|
2020-07-01 14:33:59 +00:00
|
|
|
|
2020-09-14 05:23:29 +00:00
|
|
|
log.Infof("replacing sector %d with %d", *replace, params.SectorNumber)
|
|
|
|
|
2021-08-18 10:43:44 +00:00
|
|
|
ri, err := m.Api.StateSectorGetInfo(ctx, m.maddr, *replace, nil)
|
2020-07-01 14:33:59 +00:00
|
|
|
if err != nil {
|
2020-08-18 22:55:18 +00:00
|
|
|
log.Errorf("error calling StateSectorGetInfo for replaced sector: %+v", err)
|
2020-07-01 14:33:59 +00:00
|
|
|
return big.Zero()
|
|
|
|
}
|
2020-08-27 11:51:13 +00:00
|
|
|
if ri == nil {
|
|
|
|
log.Errorf("couldn't find sector info for sector to replace: %+v", replace)
|
|
|
|
return big.Zero()
|
|
|
|
}
|
2020-07-01 14:33:59 +00:00
|
|
|
|
2020-08-18 22:55:18 +00:00
|
|
|
if params.Expiration < ri.Expiration {
|
2020-07-01 14:33:59 +00:00
|
|
|
// TODO: Some limit on this
|
2020-08-18 22:55:18 +00:00
|
|
|
params.Expiration = ri.Expiration
|
2020-07-01 14:33:59 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 22:55:18 +00:00
|
|
|
return ri.InitialPledge
|
2020-07-01 14:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return big.Zero()
|
|
|
|
}
|
|
|
|
|
2020-07-01 13:30:25 +00:00
|
|
|
func (m *Sealing) maybeUpgradableSector() *abi.SectorNumber {
|
|
|
|
m.upgradeLk.Lock()
|
|
|
|
defer m.upgradeLk.Unlock()
|
|
|
|
for number := range m.toUpgrade {
|
|
|
|
// TODO: checks to match actor constraints
|
|
|
|
|
|
|
|
// this one looks good
|
2020-07-01 13:30:36 +00:00
|
|
|
/*if checks */
|
|
|
|
{
|
2020-07-01 13:30:25 +00:00
|
|
|
delete(m.toUpgrade, number)
|
|
|
|
return &number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|