lotus/extern/storage-sealing/precommit_policy_test.go

150 lines
3.4 KiB
Go
Raw Normal View History

package sealing_test
import (
"context"
"testing"
2020-09-17 15:30:15 +00:00
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/lotus/build"
2020-08-12 19:05:09 +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"
2020-09-07 03:49:10 +00:00
"github.com/filecoin-project/go-state-types/abi"
2020-08-17 13:39:33 +00:00
sealing "github.com/filecoin-project/lotus/extern/storage-sealing"
)
type fakeChain struct {
h abi.ChainEpoch
}
2020-09-17 15:30:15 +00:00
func (f *fakeChain) StateNetworkVersion(ctx context.Context, tok sealing.TipSetToken) (network.Version, error) {
return build.NewestNetworkVersion, nil
}
func (f *fakeChain) ChainHead(ctx context.Context) (sealing.TipSetToken, abi.ChainEpoch, error) {
return []byte{1, 2, 3}, 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) {
policy := sealing.NewBasicPreCommitPolicy(&fakeChain{
h: abi.ChainEpoch(55),
2020-04-17 20:16:52 +00:00
}, 10, 0)
exp, err := policy.Expiration(context.Background())
require.NoError(t, err)
2020-08-12 19:05:09 +00:00
assert.Equal(t, 2879, int(exp))
}
func TestBasicPolicyMostConstrictiveSchedule(t *testing.T) {
policy := sealing.NewBasicPreCommitPolicy(&fakeChain{
h: abi.ChainEpoch(55),
2020-04-17 20:16:52 +00:00
}, 100, 11)
pieces := []sealing.Piece{
{
Piece: abi.PieceInfo{
Size: abi.PaddedPieceSize(1024),
2020-08-12 19:05:09 +00:00
PieceCID: fakePieceCid(t),
},
DealInfo: &sealing.DealInfo{
DealID: abi.DealID(42),
DealSchedule: sealing.DealSchedule{
StartEpoch: abi.ChainEpoch(70),
EndEpoch: abi.ChainEpoch(75),
},
},
},
{
Piece: abi.PieceInfo{
Size: abi.PaddedPieceSize(1024),
2020-08-12 19:05:09 +00:00
PieceCID: fakePieceCid(t),
},
DealInfo: &sealing.DealInfo{
DealID: abi.DealID(43),
DealSchedule: sealing.DealSchedule{
StartEpoch: abi.ChainEpoch(80),
EndEpoch: abi.ChainEpoch(100),
},
},
},
}
exp, err := policy.Expiration(context.Background(), pieces...)
require.NoError(t, err)
2020-08-12 19:05:09 +00:00
assert.Equal(t, 2890, int(exp))
}
func TestBasicPolicyIgnoresExistingScheduleIfExpired(t *testing.T) {
policy := sealing.NewBasicPreCommitPolicy(&fakeChain{
h: abi.ChainEpoch(55),
2020-04-17 20:16:52 +00:00
}, 100, 0)
pieces := []sealing.Piece{
{
Piece: abi.PieceInfo{
Size: abi.PaddedPieceSize(1024),
2020-08-12 19:05:09 +00:00
PieceCID: fakePieceCid(t),
},
DealInfo: &sealing.DealInfo{
DealID: abi.DealID(44),
DealSchedule: sealing.DealSchedule{
StartEpoch: abi.ChainEpoch(1),
EndEpoch: abi.ChainEpoch(10),
},
},
},
}
exp, err := policy.Expiration(context.Background(), pieces...)
require.NoError(t, err)
2020-08-12 19:05:09 +00:00
assert.Equal(t, 2879, int(exp))
}
func TestMissingDealIsIgnored(t *testing.T) {
policy := sealing.NewBasicPreCommitPolicy(&fakeChain{
h: abi.ChainEpoch(55),
2020-04-17 20:16:52 +00:00
}, 100, 11)
pieces := []sealing.Piece{
{
Piece: abi.PieceInfo{
Size: abi.PaddedPieceSize(1024),
2020-08-12 19:05:09 +00:00
PieceCID: fakePieceCid(t),
},
DealInfo: &sealing.DealInfo{
DealID: abi.DealID(44),
DealSchedule: sealing.DealSchedule{
StartEpoch: abi.ChainEpoch(1),
EndEpoch: abi.ChainEpoch(10),
},
},
},
{
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)
2020-08-12 19:05:09 +00:00
assert.Equal(t, 2890, int(exp))
}