2020-04-07 22:26:07 +00:00
|
|
|
package sealing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-07-22 12:37:35 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-09-17 02:34:13 +00:00
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-04-20 21:34:28 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/builtin/v8/miner"
|
2021-07-22 12:37:35 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
2022-08-26 00:37:36 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2021-07-22 12:37:35 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/policy"
|
2022-06-16 09:12:33 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2022-08-09 11:30:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
2020-04-07 22:26:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PreCommitPolicy interface {
|
2022-08-26 00:37:36 +00:00
|
|
|
Expiration(ctx context.Context, ps ...api.SectorPiece) (abi.ChainEpoch, error)
|
2020-04-07 22:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Chain interface {
|
2022-06-16 11:15:49 +00:00
|
|
|
ChainHead(context.Context) (*types.TipSet, error)
|
2022-06-17 09:20:33 +00:00
|
|
|
StateNetworkVersion(ctx context.Context, tsk types.TipSetKey) (network.Version, error)
|
2020-04-07 22:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BasicPreCommitPolicy satisfies PreCommitPolicy. It has two modes:
|
|
|
|
//
|
|
|
|
// Mode 1: The sector contains a non-zero quantity of pieces with deal info
|
|
|
|
// Mode 2: The sector contains no pieces with deal info
|
|
|
|
//
|
|
|
|
// The BasicPreCommitPolicy#Expiration method is given a slice of the pieces
|
|
|
|
// which the miner has encoded into the sector, and from that slice picks either
|
|
|
|
// the first or second mode.
|
|
|
|
//
|
|
|
|
// If we're in Mode 1: The pre-commit expiration epoch will be the maximum
|
|
|
|
// deal end epoch of a piece in the sector.
|
|
|
|
//
|
|
|
|
// If we're in Mode 2: The pre-commit expiration epoch will be set to the
|
|
|
|
// current epoch + the provided default duration.
|
|
|
|
type BasicPreCommitPolicy struct {
|
2021-07-22 12:37:35 +00:00
|
|
|
api Chain
|
2022-08-09 11:30:34 +00:00
|
|
|
getSealingConfig dtypes.GetSealingConfigFunc
|
2020-04-07 22:26:07 +00:00
|
|
|
|
2021-07-11 17:57:09 +00:00
|
|
|
provingBuffer abi.ChainEpoch
|
2020-04-07 22:26:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 18:45:47 +00:00
|
|
|
// NewBasicPreCommitPolicy produces a BasicPreCommitPolicy.
|
|
|
|
//
|
|
|
|
// The provided duration is used as the default sector expiry when the sector
|
|
|
|
// contains no deals. The proving boundary is used to adjust/align the sector's expiration.
|
2022-08-09 11:30:34 +00:00
|
|
|
func NewBasicPreCommitPolicy(api Chain, cfgGetter dtypes.GetSealingConfigFunc, provingBuffer abi.ChainEpoch) BasicPreCommitPolicy {
|
2020-04-07 22:26:07 +00:00
|
|
|
return BasicPreCommitPolicy{
|
2021-07-22 12:37:35 +00:00
|
|
|
api: api,
|
|
|
|
getSealingConfig: cfgGetter,
|
2021-07-22 18:44:39 +00:00
|
|
|
provingBuffer: provingBuffer,
|
2020-04-07 22:26:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expiration produces the pre-commit sector expiration epoch for an encoded
|
|
|
|
// replica containing the provided enumeration of pieces and deals.
|
2022-08-26 00:37:36 +00:00
|
|
|
func (p *BasicPreCommitPolicy) Expiration(ctx context.Context, ps ...api.SectorPiece) (abi.ChainEpoch, error) {
|
2022-06-16 11:15:49 +00:00
|
|
|
ts, err := p.api.ChainHead(ctx)
|
2020-04-07 22:26:07 +00:00
|
|
|
if err != nil {
|
2020-09-17 02:34:13 +00:00
|
|
|
return 0, err
|
2020-04-07 22:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var end *abi.ChainEpoch
|
|
|
|
|
2020-04-08 14:56:35 +00:00
|
|
|
for _, p := range ps {
|
2020-04-07 22:26:07 +00:00
|
|
|
if p.DealInfo == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-16 11:15:49 +00:00
|
|
|
if p.DealInfo.DealSchedule.EndEpoch < ts.Height() {
|
|
|
|
log.Warnf("piece schedule %+v ended before current epoch %d", p, ts.Height())
|
2020-04-07 22:26:07 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if end == nil || *end < p.DealInfo.DealSchedule.EndEpoch {
|
2020-04-17 19:47:44 +00:00
|
|
|
tmp := p.DealInfo.DealSchedule.EndEpoch
|
|
|
|
end = &tmp
|
2020-04-07 22:26:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if end == nil {
|
2021-07-22 12:37:35 +00:00
|
|
|
// no deal pieces, get expiration for committed capacity sector
|
|
|
|
expirationDuration, err := p.getCCSectorLifetime()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2022-06-16 11:15:49 +00:00
|
|
|
tmp := ts.Height() + expirationDuration
|
2020-04-07 22:26:07 +00:00
|
|
|
end = &tmp
|
|
|
|
}
|
|
|
|
|
2021-07-11 17:57:09 +00:00
|
|
|
// Ensure there is at least one day for the PC message to land without falling below min sector lifetime
|
|
|
|
// TODO: The "one day" should probably be a config, though it doesn't matter too much
|
2022-06-16 11:15:49 +00:00
|
|
|
minExp := ts.Height() + policy.GetMinSectorExpiration() + miner.WPoStProvingPeriod
|
2021-07-11 17:57:09 +00:00
|
|
|
if *end < minExp {
|
|
|
|
end = &minExp
|
|
|
|
}
|
2020-04-17 19:47:44 +00:00
|
|
|
|
2020-04-07 22:26:07 +00:00
|
|
|
return *end, nil
|
|
|
|
}
|
2021-07-22 12:37:35 +00:00
|
|
|
|
|
|
|
func (p *BasicPreCommitPolicy) getCCSectorLifetime() (abi.ChainEpoch, error) {
|
|
|
|
c, err := p.getSealingConfig()
|
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("sealing config load error: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-07-24 00:12:30 +00:00
|
|
|
var ccLifetimeEpochs = abi.ChainEpoch(uint64(c.CommittedCapacitySectorLifetime.Seconds()) / builtin.EpochDurationSeconds)
|
2023-04-10 17:39:56 +00:00
|
|
|
// if zero value in config, assume default sector extension
|
2021-07-22 18:44:39 +00:00
|
|
|
if ccLifetimeEpochs == 0 {
|
2023-04-19 14:45:22 +00:00
|
|
|
ccLifetimeEpochs = policy.GetMaxSectorExpirationExtension()
|
2021-07-22 18:44:39 +00:00
|
|
|
}
|
2021-07-22 12:37:35 +00:00
|
|
|
|
2021-07-22 19:05:03 +00:00
|
|
|
if minExpiration := abi.ChainEpoch(miner.MinSectorExpiration); ccLifetimeEpochs < minExpiration {
|
2021-07-22 12:37:35 +00:00
|
|
|
log.Warnf("value for CommittedCapacitySectorLiftime is too short, using default minimum (%d epochs)", minExpiration)
|
|
|
|
return minExpiration, nil
|
|
|
|
}
|
2021-07-22 18:44:39 +00:00
|
|
|
if maxExpiration := policy.GetMaxSectorExpirationExtension(); ccLifetimeEpochs > maxExpiration {
|
2021-07-22 12:37:35 +00:00
|
|
|
log.Warnf("value for CommittedCapacitySectorLiftime is too long, using default maximum (%d epochs)", maxExpiration)
|
|
|
|
return maxExpiration, nil
|
|
|
|
}
|
|
|
|
|
2021-07-11 17:57:09 +00:00
|
|
|
return ccLifetimeEpochs - p.provingBuffer, nil
|
2021-07-22 12:37:35 +00:00
|
|
|
}
|