2021-07-19 13:51:46 +00:00
|
|
|
package dagstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io"
|
2021-07-27 16:24:48 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2021-07-19 13:51:46 +00:00
|
|
|
"testing"
|
2021-07-27 16:24:48 +00:00
|
|
|
"time"
|
2021-07-19 13:51:46 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
ds "github.com/ipfs/go-datastore"
|
|
|
|
ds_sync "github.com/ipfs/go-datastore/sync"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-07-27 16:24:48 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2021-07-19 13:51:46 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/paych"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-fil-markets/piecestore"
|
|
|
|
piecestoreimpl "github.com/filecoin-project/go-fil-markets/piecestore/impl"
|
|
|
|
"github.com/filecoin-project/go-fil-markets/retrievalmarket"
|
|
|
|
"github.com/filecoin-project/go-fil-markets/shared"
|
|
|
|
)
|
|
|
|
|
|
|
|
const unsealedSectorID = abi.SectorNumber(1)
|
|
|
|
const sealedSectorID = abi.SectorNumber(2)
|
|
|
|
|
2021-07-20 09:04:47 +00:00
|
|
|
func TestLotusAccessorFetchUnsealedPiece(t *testing.T) {
|
2021-07-19 13:51:46 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
cid1, err := cid.Parse("bafkqaaa")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
unsealedSectorData := "unsealed"
|
|
|
|
sealedSectorData := "sealed"
|
|
|
|
mockData := map[abi.SectorNumber]string{
|
|
|
|
unsealedSectorID: unsealedSectorData,
|
|
|
|
sealedSectorID: sealedSectorData,
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
deals []abi.SectorNumber
|
|
|
|
fetchedData string
|
2021-07-30 14:25:18 +00:00
|
|
|
isUnsealed bool
|
2021-07-30 07:44:47 +00:00
|
|
|
|
2021-07-30 14:25:18 +00:00
|
|
|
expectErr bool
|
2021-07-19 13:51:46 +00:00
|
|
|
}{{
|
|
|
|
// Expect error if there is no deal info for piece CID
|
|
|
|
name: "no deals",
|
|
|
|
expectErr: true,
|
|
|
|
}, {
|
|
|
|
// Expect the API to always fetch the unsealed deal (because it's
|
|
|
|
// cheaper than fetching the sealed deal)
|
|
|
|
name: "prefer unsealed deal",
|
|
|
|
deals: []abi.SectorNumber{unsealedSectorID, sealedSectorID},
|
|
|
|
fetchedData: unsealedSectorData,
|
2021-07-30 14:25:18 +00:00
|
|
|
isUnsealed: true,
|
2021-07-19 13:51:46 +00:00
|
|
|
}, {
|
|
|
|
// Expect the API to unseal the data if there are no unsealed deals
|
|
|
|
name: "unseal if necessary",
|
|
|
|
deals: []abi.SectorNumber{sealedSectorID},
|
|
|
|
fetchedData: sealedSectorData,
|
2021-07-30 14:25:18 +00:00
|
|
|
isUnsealed: false,
|
2021-07-19 13:51:46 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2021-07-26 22:32:51 +00:00
|
|
|
tc := tc
|
2021-07-19 13:51:46 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
ps := getPieceStore(t)
|
|
|
|
rpn := &mockRPN{
|
|
|
|
sectors: mockData,
|
|
|
|
}
|
2021-08-03 22:52:11 +00:00
|
|
|
api := NewMinerAPI(ps, rpn, 100)
|
2021-07-21 12:04:07 +00:00
|
|
|
require.NoError(t, api.Start(ctx))
|
2021-07-19 13:51:46 +00:00
|
|
|
|
|
|
|
// Add deals to piece store
|
|
|
|
for _, sectorID := range tc.deals {
|
|
|
|
dealInfo := piecestore.DealInfo{
|
|
|
|
SectorID: sectorID,
|
|
|
|
}
|
|
|
|
err = ps.AddDealForPiece(cid1, dealInfo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the piece
|
|
|
|
r, err := api.FetchUnsealedPiece(ctx, cid1)
|
|
|
|
if tc.expectErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the returned reader is for the correct piece
|
|
|
|
require.NoError(t, err)
|
|
|
|
bz, err := io.ReadAll(r)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, tc.fetchedData, string(bz))
|
2021-07-30 07:44:47 +00:00
|
|
|
|
|
|
|
uns, err := api.IsUnsealed(ctx, cid1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.isUnsealed, uns)
|
2021-07-19 13:51:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 09:04:47 +00:00
|
|
|
func TestLotusAccessorGetUnpaddedCARSize(t *testing.T) {
|
2021-07-21 11:31:20 +00:00
|
|
|
ctx := context.Background()
|
2021-07-19 13:51:46 +00:00
|
|
|
cid1, err := cid.Parse("bafkqaaa")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ps := getPieceStore(t)
|
|
|
|
rpn := &mockRPN{}
|
2021-08-03 22:52:11 +00:00
|
|
|
api := NewMinerAPI(ps, rpn, 100)
|
2021-07-21 12:04:07 +00:00
|
|
|
require.NoError(t, api.Start(ctx))
|
2021-07-19 13:51:46 +00:00
|
|
|
|
|
|
|
// Add a deal with data Length 10
|
|
|
|
dealInfo := piecestore.DealInfo{
|
|
|
|
Length: 10,
|
|
|
|
}
|
|
|
|
err = ps.AddDealForPiece(cid1, dealInfo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Check that the data length is correct
|
2021-07-21 11:31:20 +00:00
|
|
|
len, err := api.GetUnpaddedCARSize(ctx, cid1)
|
2021-07-19 13:51:46 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, 10, len)
|
|
|
|
}
|
|
|
|
|
2021-07-27 16:24:48 +00:00
|
|
|
func TestThrottle(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
cid1, err := cid.Parse("bafkqaaa")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ps := getPieceStore(t)
|
|
|
|
rpn := &mockRPN{
|
|
|
|
sectors: map[abi.SectorNumber]string{
|
|
|
|
unsealedSectorID: "foo",
|
|
|
|
},
|
|
|
|
}
|
2021-08-03 22:52:11 +00:00
|
|
|
api := NewMinerAPI(ps, rpn, 3)
|
2021-07-27 16:24:48 +00:00
|
|
|
require.NoError(t, api.Start(ctx))
|
|
|
|
|
|
|
|
// Add a deal with data Length 10
|
|
|
|
dealInfo := piecestore.DealInfo{
|
|
|
|
SectorID: unsealedSectorID,
|
|
|
|
Length: 10,
|
|
|
|
}
|
|
|
|
err = ps.AddDealForPiece(cid1, dealInfo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// hold the lock to block.
|
|
|
|
rpn.lk.Lock()
|
|
|
|
|
|
|
|
// fetch the piece concurrently.
|
|
|
|
errgrp, ctx := errgroup.WithContext(context.Background())
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
errgrp.Go(func() error {
|
|
|
|
r, err := api.FetchUnsealedPiece(ctx, cid1)
|
|
|
|
if err == nil {
|
|
|
|
_ = r.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2021-08-03 22:52:11 +00:00
|
|
|
require.EqualValues(t, 3, atomic.LoadInt32(&rpn.calls)) // throttled
|
2021-07-27 16:24:48 +00:00
|
|
|
|
|
|
|
// allow to proceed.
|
|
|
|
rpn.lk.Unlock()
|
|
|
|
|
|
|
|
// allow all to finish.
|
|
|
|
err = errgrp.Wait()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.EqualValues(t, 10, atomic.LoadInt32(&rpn.calls)) // throttled
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-19 13:51:46 +00:00
|
|
|
func getPieceStore(t *testing.T) piecestore.PieceStore {
|
|
|
|
ps, err := piecestoreimpl.NewPieceStore(ds_sync.MutexWrap(ds.NewMapDatastore()))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-30 07:32:42 +00:00
|
|
|
ch := make(chan struct{}, 1)
|
|
|
|
ps.OnReady(func(_ error) {
|
|
|
|
ch <- struct{}{}
|
|
|
|
})
|
|
|
|
|
2021-07-19 13:51:46 +00:00
|
|
|
err = ps.Start(context.Background())
|
|
|
|
require.NoError(t, err)
|
2021-07-30 14:25:18 +00:00
|
|
|
<-ch
|
2021-07-19 13:51:46 +00:00
|
|
|
return ps
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockRPN struct {
|
2021-07-27 16:24:48 +00:00
|
|
|
calls int32 // guarded by atomic
|
|
|
|
lk sync.RWMutex // lock to simulate blocks.
|
2021-07-19 13:51:46 +00:00
|
|
|
sectors map[abi.SectorNumber]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockRPN) UnsealSector(ctx context.Context, sectorID abi.SectorNumber, offset abi.UnpaddedPieceSize, length abi.UnpaddedPieceSize) (io.ReadCloser, error) {
|
2021-07-27 16:24:48 +00:00
|
|
|
atomic.AddInt32(&m.calls, 1)
|
|
|
|
m.lk.RLock()
|
|
|
|
defer m.lk.RUnlock()
|
|
|
|
|
2021-07-19 13:51:46 +00:00
|
|
|
data, ok := m.sectors[sectorID]
|
|
|
|
if !ok {
|
|
|
|
panic("sector not found")
|
|
|
|
}
|
|
|
|
return io.NopCloser(bytes.NewBuffer([]byte(data))), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockRPN) IsUnsealed(ctx context.Context, sectorID abi.SectorNumber, offset abi.UnpaddedPieceSize, length abi.UnpaddedPieceSize) (bool, error) {
|
|
|
|
return sectorID == unsealedSectorID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockRPN) GetChainHead(ctx context.Context) (shared.TipSetToken, abi.ChainEpoch, error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockRPN) GetMinerWorkerAddress(ctx context.Context, miner address.Address, tok shared.TipSetToken) (address.Address, error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockRPN) SavePaymentVoucher(ctx context.Context, paymentChannel address.Address, voucher *paych.SignedVoucher, proof []byte, expectedAmount abi.TokenAmount, tok shared.TipSetToken) (abi.TokenAmount, error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockRPN) GetRetrievalPricingInput(ctx context.Context, pieceCID cid.Cid, storageDeals []abi.DealID) (retrievalmarket.PricingInput, error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ retrievalmarket.RetrievalProviderNode = (*mockRPN)(nil)
|