2020-04-07 22:26:07 +00:00
|
|
|
package sealing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-09-22 04:35:15 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
|
|
|
|
|
2020-09-17 02:34:13 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-04-07 22:26:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PreCommitPolicy interface {
|
2020-04-08 14:56:35 +00:00
|
|
|
Expiration(ctx context.Context, ps ...Piece) (abi.ChainEpoch, error)
|
2020-04-07 22:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Chain interface {
|
|
|
|
ChainHead(ctx context.Context) (TipSetToken, abi.ChainEpoch, error)
|
2020-09-17 02:34:13 +00:00
|
|
|
StateNetworkVersion(ctx context.Context, tok TipSetToken) (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 {
|
|
|
|
api Chain
|
|
|
|
|
2020-04-17 19:47:44 +00:00
|
|
|
provingBoundary abi.ChainEpoch
|
|
|
|
duration 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.
|
2020-04-17 19:47:44 +00:00
|
|
|
func NewBasicPreCommitPolicy(api Chain, duration abi.ChainEpoch, provingBoundary abi.ChainEpoch) BasicPreCommitPolicy {
|
2020-04-07 22:26:07 +00:00
|
|
|
return BasicPreCommitPolicy{
|
2020-04-17 20:16:52 +00:00
|
|
|
api: api,
|
2020-04-17 19:47:44 +00:00
|
|
|
provingBoundary: provingBoundary,
|
2020-04-17 20:16:52 +00:00
|
|
|
duration: duration,
|
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.
|
2020-04-08 14:56:35 +00:00
|
|
|
func (p *BasicPreCommitPolicy) Expiration(ctx context.Context, ps ...Piece) (abi.ChainEpoch, error) {
|
2020-09-22 04:35:15 +00:00
|
|
|
_, epoch, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.DealInfo.DealSchedule.EndEpoch < epoch {
|
|
|
|
log.Warnf("piece schedule %+v ended before current epoch %d", p, epoch)
|
|
|
|
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 {
|
|
|
|
tmp := epoch + p.duration
|
|
|
|
end = &tmp
|
|
|
|
}
|
|
|
|
|
2020-09-22 04:35:15 +00:00
|
|
|
*end += miner.WPoStProvingPeriod - (*end % miner.WPoStProvingPeriod) + p.provingBoundary - 1
|
2020-04-17 19:47:44 +00:00
|
|
|
|
2020-04-07 22:26:07 +00:00
|
|
|
return *end, nil
|
|
|
|
}
|