lotus/storage/pipeline/precommit_policy_test.go

195 lines
5.0 KiB
Go
Raw Normal View History

package sealing_test
import (
"context"
"testing"
2021-07-22 18:44:39 +00:00
"time"
2022-06-14 15:00:51 +00:00
"github.com/ipfs/go-cid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
commcid "github.com/filecoin-project/go-fil-commcid"
"github.com/filecoin-project/go-state-types/abi"
2020-09-17 15:30:15 +00:00
"github.com/filecoin-project/go-state-types/network"
2022-06-14 15:00:51 +00:00
"github.com/filecoin-project/lotus/api"
2020-09-17 15:30:15 +00:00
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors/builtin"
2021-07-22 18:44:39 +00:00
"github.com/filecoin-project/lotus/chain/actors/policy"
"github.com/filecoin-project/lotus/chain/types"
2022-06-14 18:31:17 +00:00
pipeline "github.com/filecoin-project/lotus/storage/pipeline"
"github.com/filecoin-project/lotus/storage/pipeline/sealiface"
)
type fakeChain struct {
h abi.ChainEpoch
}
type fakeConfigStub struct {
CCSectorLifetime time.Duration
}
2022-06-14 18:31:17 +00:00
func fakeConfigGetter(stub *fakeConfigStub) pipeline.GetSealingConfigFunc {
2021-07-22 18:44:39 +00:00
return func() (sealiface.Config, error) {
if stub == nil {
return sealiface.Config{}, nil
}
return sealiface.Config{
CommittedCapacitySectorLifetime: stub.CCSectorLifetime,
2021-07-22 18:44:39 +00:00
}, nil
}
}
func (f *fakeChain) StateNetworkVersion(ctx context.Context, tsk types.TipSetKey) (network.Version, error) {
2020-09-17 15:30:15 +00:00
return build.NewestNetworkVersion, nil
}
func (f *fakeChain) ChainHead(ctx context.Context) (*types.TipSet, error) {
return makeTs(nil, f.h), nil
}
2020-08-12 19:05:09 +00:00
func fakePieceCid(t *testing.T) cid.Cid {
comm := [32]byte{1, 2, 3}
fakePieceCid, err := commcid.ReplicaCommitmentV1ToCID(comm[:])
require.NoError(t, err)
return fakePieceCid
}
func TestBasicPolicyEmptySector(t *testing.T) {
cfg := fakeConfigGetter(nil)
2021-07-22 18:44:39 +00:00
h := abi.ChainEpoch(55)
pBuffer := abi.ChainEpoch(2)
2022-06-14 18:31:17 +00:00
pcp := pipeline.NewBasicPreCommitPolicy(&fakeChain{h: h}, cfg, pBuffer)
2021-07-22 18:44:39 +00:00
exp, err := pcp.Expiration(context.Background())
require.NoError(t, err)
2021-07-22 18:44:39 +00:00
// as set when there are no deal pieces
expected := h + policy.GetMaxSectorExpirationExtension() - pBuffer
2021-07-22 18:44:39 +00:00
assert.Equal(t, int(expected), int(exp))
}
func TestCustomCCSectorConfig(t *testing.T) {
customLifetime := 200 * 24 * time.Hour
2021-07-24 00:12:30 +00:00
customLifetimeEpochs := abi.ChainEpoch(int64(customLifetime.Seconds()) / builtin.EpochDurationSeconds)
cfgStub := fakeConfigStub{CCSectorLifetime: customLifetime}
cfg := fakeConfigGetter(&cfgStub)
h := abi.ChainEpoch(55)
pBuffer := abi.ChainEpoch(2)
2022-06-14 18:31:17 +00:00
pcp := pipeline.NewBasicPreCommitPolicy(&fakeChain{h: h}, cfg, pBuffer)
exp, err := pcp.Expiration(context.Background())
require.NoError(t, err)
// as set when there are no deal pieces
expected := h + customLifetimeEpochs - pBuffer
assert.Equal(t, int(expected), int(exp))
}
func TestBasicPolicyMostConstrictiveSchedule(t *testing.T) {
cfg := fakeConfigGetter(nil)
2022-06-14 18:31:17 +00:00
policy := pipeline.NewBasicPreCommitPolicy(&fakeChain{
h: abi.ChainEpoch(55),
}, cfg, 2)
longestDealEpochEnd := abi.ChainEpoch(547300)
2022-06-14 18:31:17 +00:00
pieces := []pipeline.Piece{
{
Piece: abi.PieceInfo{
Size: abi.PaddedPieceSize(1024),
2020-08-12 19:05:09 +00:00
PieceCID: fakePieceCid(t),
},
2021-06-01 09:45:34 +00:00
DealInfo: &api.PieceDealInfo{
DealID: abi.DealID(42),
2021-06-01 09:45:34 +00:00
DealSchedule: api.DealSchedule{
StartEpoch: abi.ChainEpoch(70),
EndEpoch: abi.ChainEpoch(547275),
},
},
},
{
Piece: abi.PieceInfo{
Size: abi.PaddedPieceSize(1024),
2020-08-12 19:05:09 +00:00
PieceCID: fakePieceCid(t),
},
2021-06-01 09:45:34 +00:00
DealInfo: &api.PieceDealInfo{
DealID: abi.DealID(43),
2021-06-01 09:45:34 +00:00
DealSchedule: api.DealSchedule{
StartEpoch: abi.ChainEpoch(80),
2021-07-22 18:44:39 +00:00
EndEpoch: longestDealEpochEnd,
},
},
},
}
exp, err := policy.Expiration(context.Background(), pieces...)
require.NoError(t, err)
assert.Equal(t, int(longestDealEpochEnd), int(exp))
}
func TestBasicPolicyIgnoresExistingScheduleIfExpired(t *testing.T) {
cfg := fakeConfigGetter(nil)
2022-06-14 18:31:17 +00:00
policy := pipeline.NewBasicPreCommitPolicy(&fakeChain{
h: abi.ChainEpoch(55),
}, cfg, 0)
2022-06-14 18:31:17 +00:00
pieces := []pipeline.Piece{
{
Piece: abi.PieceInfo{
Size: abi.PaddedPieceSize(1024),
2020-08-12 19:05:09 +00:00
PieceCID: fakePieceCid(t),
},
2021-06-01 09:45:34 +00:00
DealInfo: &api.PieceDealInfo{
DealID: abi.DealID(44),
2021-06-01 09:45:34 +00:00
DealSchedule: api.DealSchedule{
StartEpoch: abi.ChainEpoch(1),
EndEpoch: abi.ChainEpoch(10),
},
},
},
}
exp, err := policy.Expiration(context.Background(), pieces...)
require.NoError(t, err)
// Treated as a CC sector, so expiration becomes currEpoch + maxLifetime = 55 + 1555200
assert.Equal(t, 1555255, int(exp))
}
func TestMissingDealIsIgnored(t *testing.T) {
cfg := fakeConfigGetter(nil)
2022-06-14 18:31:17 +00:00
policy := pipeline.NewBasicPreCommitPolicy(&fakeChain{
h: abi.ChainEpoch(55),
}, cfg, 0)
2022-06-14 18:31:17 +00:00
pieces := []pipeline.Piece{
{
Piece: abi.PieceInfo{
Size: abi.PaddedPieceSize(1024),
2020-08-12 19:05:09 +00:00
PieceCID: fakePieceCid(t),
},
2021-06-01 09:45:34 +00:00
DealInfo: &api.PieceDealInfo{
DealID: abi.DealID(44),
2021-06-01 09:45:34 +00:00
DealSchedule: api.DealSchedule{
StartEpoch: abi.ChainEpoch(1),
EndEpoch: abi.ChainEpoch(547300),
},
},
},
{
Piece: abi.PieceInfo{
Size: abi.PaddedPieceSize(1024),
2020-08-12 19:05:09 +00:00
PieceCID: fakePieceCid(t),
},
DealInfo: nil,
},
}
exp, err := policy.Expiration(context.Background(), pieces...)
require.NoError(t, err)
assert.Equal(t, 547300, int(exp))
}