fix: PreCommitPolicy unit tests

This commit is contained in:
Mike Greenberg 2021-07-22 14:44:39 -04:00
parent adb62a3fff
commit 7ee46ad4e0
3 changed files with 61 additions and 21 deletions

View File

@ -40,17 +40,19 @@ type BasicPreCommitPolicy struct {
getSealingConfig GetSealingConfigFunc
provingBoundary abi.ChainEpoch
provingBuffer abi.ChainEpoch
}
// 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.
func NewBasicPreCommitPolicy(api Chain, cfgGetter GetSealingConfigFunc, provingBoundary abi.ChainEpoch) BasicPreCommitPolicy {
func NewBasicPreCommitPolicy(api Chain, cfgGetter GetSealingConfigFunc, provingBoundary abi.ChainEpoch, provingBuffer abi.ChainEpoch) BasicPreCommitPolicy {
return BasicPreCommitPolicy{
api: api,
getSealingConfig: cfgGetter,
provingBoundary: provingBoundary,
provingBuffer: provingBuffer,
}
}
@ -102,16 +104,20 @@ func (p *BasicPreCommitPolicy) getCCSectorLifetime() (abi.ChainEpoch, error) {
return 0, xerrors.Errorf("sealing config load error: %w", err)
}
sectorExpiration := abi.ChainEpoch(c.CommittedCapacitySectorLifetime.Truncate(builtin.EpochDurationSeconds) / builtin.EpochDurationSeconds)
var ccLifetimeEpochs = abi.ChainEpoch(uint64(c.CommittedCapacitySectorLifetime.Truncate(builtin.EpochDurationSeconds).Seconds()) / builtin.EpochDurationSeconds)
// if zero value in config, assume maximum sector extension
if ccLifetimeEpochs == 0 {
ccLifetimeEpochs = policy.GetMaxSectorExpirationExtension()
}
if minExpiration := policy.GetMinSectorExpiration(); sectorExpiration < minExpiration {
if minExpiration := policy.GetMinSectorExpiration(); ccLifetimeEpochs < minExpiration {
log.Warnf("value for CommittedCapacitySectorLiftime is too short, using default minimum (%d epochs)", minExpiration)
return minExpiration, nil
}
if maxExpiration := policy.GetMaxSectorExpirationExtension(); sectorExpiration > maxExpiration {
if maxExpiration := policy.GetMaxSectorExpirationExtension(); ccLifetimeEpochs > maxExpiration {
log.Warnf("value for CommittedCapacitySectorLiftime is too long, using default maximum (%d epochs)", maxExpiration)
return maxExpiration, nil
}
return sectorExpiration, nil
return (ccLifetimeEpochs - p.provingBuffer), nil
}

View File

@ -3,10 +3,15 @@ package sealing_test
import (
"context"
"testing"
"time"
"github.com/filecoin-project/go-state-types/network"
api "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/actors/policy"
sealing "github.com/filecoin-project/lotus/extern/storage-sealing"
"github.com/filecoin-project/lotus/extern/storage-sealing/sealiface"
"github.com/ipfs/go-cid"
"github.com/stretchr/testify/assert"
@ -14,14 +19,31 @@ import (
commcid "github.com/filecoin-project/go-fil-commcid"
"github.com/filecoin-project/go-state-types/abi"
sealing "github.com/filecoin-project/lotus/extern/storage-sealing"
)
type fakeChain struct {
h abi.ChainEpoch
}
func fakeConfigGetter(stub interface{}, t *testing.T) sealing.GetSealingConfigFunc {
return func() (sealiface.Config, error) {
if stub == nil {
return sealiface.Config{}, nil
}
castStub, ok := stub.(struct {
CCSectorLifetime time.Duration
})
if !ok {
t.Fatal("failed to cast fakeConfig")
}
return sealiface.Config{
CommittedCapacitySectorLifetime: castStub.CCSectorLifetime,
}, nil
}
}
func (f *fakeChain) StateNetworkVersion(ctx context.Context, tok sealing.TipSetToken) (network.Version, error) {
return build.NewestNetworkVersion, nil
}
@ -38,21 +60,29 @@ func fakePieceCid(t *testing.T) cid.Cid {
}
func TestBasicPolicyEmptySector(t *testing.T) {
policy := sealing.NewBasicPreCommitPolicy(&fakeChain{
h: abi.ChainEpoch(55),
}, 10, 0)
cfg := fakeConfigGetter(nil, t)
h := abi.ChainEpoch(55)
pBoundary := abi.ChainEpoch(0)
pBuffer := abi.ChainEpoch(2)
pcp := sealing.NewBasicPreCommitPolicy(&fakeChain{h: h}, cfg, pBoundary, pBuffer)
exp, err := pcp.Expiration(context.Background())
exp, err := policy.Expiration(context.Background())
require.NoError(t, err)
assert.Equal(t, 2879, int(exp))
// as set when there are no deal pieces
expected := h + policy.GetMaxSectorExpirationExtension() - (pBuffer * 2)
// as set just before returning within Expiration()
expected += miner.WPoStProvingPeriod - (expected % miner.WPoStProvingPeriod) + pBoundary - 1
assert.Equal(t, int(expected), int(exp))
}
func TestBasicPolicyMostConstrictiveSchedule(t *testing.T) {
cfg := fakeConfigGetter(nil, t)
pPeriod := abi.ChainEpoch(11)
policy := sealing.NewBasicPreCommitPolicy(&fakeChain{
h: abi.ChainEpoch(55),
}, 100, 11)
}, cfg, pPeriod, 2)
longestDealEpochEnd := abi.ChainEpoch(100)
pieces := []sealing.Piece{
{
Piece: abi.PieceInfo{
@ -76,7 +106,7 @@ func TestBasicPolicyMostConstrictiveSchedule(t *testing.T) {
DealID: abi.DealID(43),
DealSchedule: api.DealSchedule{
StartEpoch: abi.ChainEpoch(80),
EndEpoch: abi.ChainEpoch(100),
EndEpoch: longestDealEpochEnd,
},
},
},
@ -85,13 +115,15 @@ func TestBasicPolicyMostConstrictiveSchedule(t *testing.T) {
exp, err := policy.Expiration(context.Background(), pieces...)
require.NoError(t, err)
assert.Equal(t, 2890, int(exp))
expected := longestDealEpochEnd + miner.WPoStProvingPeriod - (longestDealEpochEnd % miner.WPoStProvingPeriod) + pPeriod - 1
assert.Equal(t, int(expected), int(exp))
}
func TestBasicPolicyIgnoresExistingScheduleIfExpired(t *testing.T) {
cfg := fakeConfigGetter(nil, t)
policy := sealing.NewBasicPreCommitPolicy(&fakeChain{
h: abi.ChainEpoch(55),
}, 100, 0)
}, cfg, 0, 0)
pieces := []sealing.Piece{
{
@ -112,13 +144,14 @@ func TestBasicPolicyIgnoresExistingScheduleIfExpired(t *testing.T) {
exp, err := policy.Expiration(context.Background(), pieces...)
require.NoError(t, err)
assert.Equal(t, 2879, int(exp))
assert.Equal(t, 1558079, int(exp))
}
func TestMissingDealIsIgnored(t *testing.T) {
cfg := fakeConfigGetter(nil, t)
policy := sealing.NewBasicPreCommitPolicy(&fakeChain{
h: abi.ChainEpoch(55),
}, 100, 11)
}, cfg, 11, 0)
pieces := []sealing.Piece{
{
@ -146,5 +179,5 @@ func TestMissingDealIsIgnored(t *testing.T) {
exp, err := policy.Expiration(context.Background(), pieces...)
require.NoError(t, err)
assert.Equal(t, 2890, int(exp))
assert.Equal(t, 1558090, int(exp))
}

View File

@ -180,9 +180,10 @@ func (m *Miner) Run(ctx context.Context) error {
// Instantiate a precommit policy.
cfg = sealing.GetSealingConfigFunc(m.getSealConfig)
provingBoundary = md.PeriodStart % md.WPoStProvingPeriod
provingBuffer = md.WPoStProvingPeriod * 2
// TODO: Maybe we update this policy after actor upgrades?
pcp = sealing.NewBasicPreCommitPolicy(adaptedAPI, cfg, provingBoundary)
pcp = sealing.NewBasicPreCommitPolicy(adaptedAPI, cfg, provingBoundary, provingBuffer)
// address selector.
as = func(ctx context.Context, mi miner.MinerInfo, use api.AddrUse, goodFunds, minFunds abi.TokenAmount) (address.Address, abi.TokenAmount, error) {