2021-06-18 18:45:29 +00:00
|
|
|
package kit
|
2021-06-11 17:26:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-06-23 17:13:29 +00:00
|
|
|
"fmt"
|
2021-06-11 17:26:25 +00:00
|
|
|
"io/ioutil"
|
2021-06-13 23:10:37 +00:00
|
|
|
"os"
|
2021-06-11 17:26:25 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-fil-markets/storagemarket"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
sealing "github.com/filecoin-project/lotus/extern/storage-sealing"
|
2021-06-23 17:13:29 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
files "github.com/ipfs/go-ipfs-files"
|
2021-06-11 17:26:25 +00:00
|
|
|
ipld "github.com/ipfs/go-ipld-format"
|
|
|
|
dag "github.com/ipfs/go-merkledag"
|
|
|
|
dstest "github.com/ipfs/go-merkledag/test"
|
|
|
|
unixfile "github.com/ipfs/go-unixfs/file"
|
2021-06-23 17:13:29 +00:00
|
|
|
"github.com/ipld/go-car"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2021-06-11 17:26:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DealHarness struct {
|
|
|
|
t *testing.T
|
|
|
|
client *TestFullNode
|
|
|
|
miner *TestMiner
|
|
|
|
}
|
|
|
|
|
2021-06-14 04:10:34 +00:00
|
|
|
type MakeFullDealParams struct {
|
2021-06-21 17:19:26 +00:00
|
|
|
Rseed int
|
|
|
|
FastRet bool
|
|
|
|
StartEpoch abi.ChainEpoch
|
2021-06-23 18:14:27 +00:00
|
|
|
|
|
|
|
// SuspendUntilCryptoeconStable suspends deal-making, until cryptoecon
|
|
|
|
// parameters are stabilised. This affects projected collateral, and tests
|
|
|
|
// will fail in network version 13 and higher if deals are started too soon
|
|
|
|
// after network birth.
|
|
|
|
//
|
|
|
|
// The reason is that the formula for collateral calculation takes
|
|
|
|
// circulating supply into account:
|
|
|
|
//
|
|
|
|
// [portion of power this deal will be] * [~1% of tokens].
|
|
|
|
//
|
|
|
|
// In the first epochs after genesis, the total circulating supply is
|
|
|
|
// changing dramatically in percentual terms. Therefore, if the deal is
|
|
|
|
// proposed too soon, by the time it gets published on chain, the quoted
|
|
|
|
// provider collateral will no longer be valid.
|
|
|
|
//
|
|
|
|
// The observation is that deals fail with:
|
|
|
|
//
|
|
|
|
// GasEstimateMessageGas error: estimating gas used: message execution
|
|
|
|
// failed: exit 16, reason: Provider collateral out of bounds. (RetCode=16)
|
|
|
|
//
|
|
|
|
// Enabling this will suspend deal-making until the network has reached a
|
2021-06-23 18:21:42 +00:00
|
|
|
// height of 300.
|
2021-06-23 18:14:27 +00:00
|
|
|
SuspendUntilCryptoeconStable bool
|
2021-06-14 04:10:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 17:26:25 +00:00
|
|
|
// NewDealHarness creates a test harness that contains testing utilities for deals.
|
|
|
|
func NewDealHarness(t *testing.T, client *TestFullNode, miner *TestMiner) *DealHarness {
|
|
|
|
return &DealHarness{
|
|
|
|
t: t,
|
|
|
|
client: client,
|
|
|
|
miner: miner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeOnlineDeal makes an online deal, generating a random file with the
|
|
|
|
// supplied seed, and setting the specified fast retrieval flag and start epoch
|
|
|
|
// on the storage deal. It returns when the deal is sealed.
|
|
|
|
//
|
|
|
|
// TODO: convert input parameters to struct, and add size as an input param.
|
2021-06-21 17:19:26 +00:00
|
|
|
func (dh *DealHarness) MakeOnlineDeal(ctx context.Context, params MakeFullDealParams) (deal *cid.Cid, res *api.ImportRes, path string) {
|
|
|
|
res, path = dh.client.CreateImportFile(ctx, params.Rseed, 0)
|
2021-06-11 17:26:25 +00:00
|
|
|
|
|
|
|
dh.t.Logf("FILE CID: %s", res.Root)
|
|
|
|
|
2021-06-23 18:14:27 +00:00
|
|
|
if params.SuspendUntilCryptoeconStable {
|
|
|
|
dh.t.Logf("deal-making suspending until cryptecon parameters have stabilised")
|
2021-06-23 18:21:42 +00:00
|
|
|
ts := dh.client.WaitTillChain(ctx, HeightAtLeast(300))
|
2021-06-23 18:14:27 +00:00
|
|
|
dh.t.Logf("deal-making continuing; current height is %d", ts.Height())
|
|
|
|
}
|
|
|
|
|
2021-06-21 17:19:26 +00:00
|
|
|
deal = dh.StartDeal(ctx, res.Root, params.FastRet, params.StartEpoch)
|
2021-06-11 17:26:25 +00:00
|
|
|
|
|
|
|
// TODO: this sleep is only necessary because deals don't immediately get logged in the dealstore, we should fix this
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
dh.WaitDealSealed(ctx, deal, false, false, nil)
|
|
|
|
|
|
|
|
return deal, res, path
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartDeal starts a storage deal between the client and the miner.
|
|
|
|
func (dh *DealHarness) StartDeal(ctx context.Context, fcid cid.Cid, fastRet bool, startEpoch abi.ChainEpoch) *cid.Cid {
|
|
|
|
maddr, err := dh.miner.ActorAddress(ctx)
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
|
|
|
addr, err := dh.client.WalletDefaultAddress(ctx)
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
|
|
|
deal, err := dh.client.ClientStartDeal(ctx, &api.StartDealParams{
|
|
|
|
Data: &storagemarket.DataRef{
|
|
|
|
TransferType: storagemarket.TTGraphsync,
|
|
|
|
Root: fcid,
|
|
|
|
},
|
|
|
|
Wallet: addr,
|
|
|
|
Miner: maddr,
|
|
|
|
EpochPrice: types.NewInt(1000000),
|
|
|
|
DealStartEpoch: startEpoch,
|
|
|
|
MinBlocksDuration: uint64(build.MinDealDuration),
|
|
|
|
FastRetrieval: fastRet,
|
|
|
|
})
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
|
|
|
return deal
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitDealSealed waits until the deal is sealed.
|
|
|
|
func (dh *DealHarness) WaitDealSealed(ctx context.Context, deal *cid.Cid, noseal, noSealStart bool, cb func()) {
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
di, err := dh.client.ClientGetDealInfo(ctx, *deal)
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
|
|
|
switch di.State {
|
|
|
|
case storagemarket.StorageDealAwaitingPreCommit, storagemarket.StorageDealSealing:
|
|
|
|
if noseal {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !noSealStart {
|
|
|
|
dh.StartSealingWaiting(ctx)
|
|
|
|
}
|
|
|
|
case storagemarket.StorageDealProposalRejected:
|
|
|
|
dh.t.Fatal("deal rejected")
|
|
|
|
case storagemarket.StorageDealFailing:
|
|
|
|
dh.t.Fatal("deal failed")
|
|
|
|
case storagemarket.StorageDealError:
|
|
|
|
dh.t.Fatal("deal errored", di.Message)
|
|
|
|
case storagemarket.StorageDealActive:
|
|
|
|
dh.t.Log("COMPLETE", di)
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
|
|
|
|
mds, err := dh.miner.MarketListIncompleteDeals(ctx)
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
|
|
|
var minerState storagemarket.StorageDealStatus
|
|
|
|
for _, md := range mds {
|
|
|
|
if md.DealID == di.DealID {
|
|
|
|
minerState = md.State
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dh.t.Logf("Deal %d state: client:%s provider:%s\n", di.DealID, storagemarket.DealStates[di.State], storagemarket.DealStates[minerState])
|
|
|
|
time.Sleep(time.Second / 2)
|
|
|
|
if cb != nil {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-13 23:10:37 +00:00
|
|
|
// WaitDealPublished waits until the deal is published.
|
2021-06-11 17:26:25 +00:00
|
|
|
func (dh *DealHarness) WaitDealPublished(ctx context.Context, deal *cid.Cid) {
|
|
|
|
subCtx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
updates, err := dh.miner.MarketGetDealUpdates(subCtx)
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
dh.t.Fatal("context timeout")
|
|
|
|
case di := <-updates:
|
|
|
|
if deal.Equals(di.ProposalCid) {
|
|
|
|
switch di.State {
|
|
|
|
case storagemarket.StorageDealProposalRejected:
|
|
|
|
dh.t.Fatal("deal rejected")
|
|
|
|
case storagemarket.StorageDealFailing:
|
|
|
|
dh.t.Fatal("deal failed")
|
|
|
|
case storagemarket.StorageDealError:
|
|
|
|
dh.t.Fatal("deal errored", di.Message)
|
|
|
|
case storagemarket.StorageDealFinalizing, storagemarket.StorageDealAwaitingPreCommit, storagemarket.StorageDealSealing, storagemarket.StorageDealActive:
|
|
|
|
dh.t.Log("COMPLETE", di)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dh.t.Log("Deal state: ", storagemarket.DealStates[di.State])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dh *DealHarness) StartSealingWaiting(ctx context.Context) {
|
|
|
|
snums, err := dh.miner.SectorsList(ctx)
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
|
|
|
for _, snum := range snums {
|
|
|
|
si, err := dh.miner.SectorsStatus(ctx, snum, false)
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
|
|
|
dh.t.Logf("Sector state: %s", si.State)
|
|
|
|
if si.State == api.SectorState(sealing.WaitDeals) {
|
|
|
|
require.NoError(dh.t, dh.miner.SectorStartSealing(ctx, snum))
|
|
|
|
}
|
|
|
|
|
|
|
|
dh.miner.FlushSealingBatches(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dh *DealHarness) PerformRetrieval(ctx context.Context, deal *cid.Cid, root cid.Cid, carExport bool) (path string) {
|
|
|
|
// perform retrieval.
|
|
|
|
info, err := dh.client.ClientGetDealInfo(ctx, *deal)
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
|
|
|
offers, err := dh.client.ClientFindData(ctx, root, &info.PieceCID)
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
require.NotEmpty(dh.t, offers, "no offers")
|
|
|
|
|
2021-06-13 23:10:37 +00:00
|
|
|
carFile, err := ioutil.TempFile(dh.t.TempDir(), "ret-car")
|
2021-06-11 17:26:25 +00:00
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
2021-06-13 23:10:37 +00:00
|
|
|
defer carFile.Close() //nolint:errcheck
|
2021-06-11 17:26:25 +00:00
|
|
|
|
|
|
|
caddr, err := dh.client.WalletDefaultAddress(ctx)
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
|
|
|
ref := &api.FileRef{
|
2021-06-13 23:10:37 +00:00
|
|
|
Path: carFile.Name(),
|
2021-06-11 17:26:25 +00:00
|
|
|
IsCAR: carExport,
|
|
|
|
}
|
|
|
|
|
|
|
|
updates, err := dh.client.ClientRetrieveWithEvents(ctx, offers[0].Order(caddr), ref)
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
|
|
|
for update := range updates {
|
|
|
|
require.Emptyf(dh.t, update.Err, "retrieval failed: %s", update.Err)
|
|
|
|
}
|
|
|
|
|
2021-06-13 23:10:37 +00:00
|
|
|
ret := carFile.Name()
|
2021-06-11 17:26:25 +00:00
|
|
|
if carExport {
|
2021-06-13 23:10:37 +00:00
|
|
|
actualFile := dh.ExtractFileFromCAR(ctx, carFile)
|
|
|
|
ret = actualFile.Name()
|
|
|
|
_ = actualFile.Close() //nolint:errcheck
|
2021-06-11 17:26:25 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 23:10:37 +00:00
|
|
|
return ret
|
2021-06-11 17:26:25 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 23:10:37 +00:00
|
|
|
func (dh *DealHarness) ExtractFileFromCAR(ctx context.Context, file *os.File) (out *os.File) {
|
2021-06-11 17:26:25 +00:00
|
|
|
bserv := dstest.Bserv()
|
2021-06-13 23:10:37 +00:00
|
|
|
ch, err := car.LoadCar(bserv.Blockstore(), file)
|
2021-06-11 17:26:25 +00:00
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
|
|
|
b, err := bserv.GetBlock(ctx, ch.Roots[0])
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
|
|
|
nd, err := ipld.Decode(b)
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
|
|
|
dserv := dag.NewDAGService(bserv)
|
|
|
|
fil, err := unixfile.NewUnixfsFile(ctx, dserv, nd)
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
|
|
|
tmpfile, err := ioutil.TempFile(dh.t.TempDir(), "file-in-car")
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
2021-06-13 23:10:37 +00:00
|
|
|
defer tmpfile.Close() //nolint:errcheck
|
2021-06-11 17:26:25 +00:00
|
|
|
|
|
|
|
err = files.WriteTo(fil, tmpfile.Name())
|
|
|
|
require.NoError(dh.t, err)
|
|
|
|
|
2021-06-13 23:10:37 +00:00
|
|
|
return tmpfile
|
2021-06-11 17:26:25 +00:00
|
|
|
}
|
2021-06-23 17:13:29 +00:00
|
|
|
|
|
|
|
type RunConcurrentDealsOpts struct {
|
|
|
|
N int
|
|
|
|
FastRetrieval bool
|
|
|
|
CarExport bool
|
|
|
|
StartEpoch abi.ChainEpoch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dh *DealHarness) RunConcurrentDeals(opts RunConcurrentDealsOpts) {
|
|
|
|
errgrp, _ := errgroup.WithContext(context.Background())
|
|
|
|
for i := 0; i < opts.N; i++ {
|
|
|
|
i := i
|
|
|
|
errgrp.Go(func() (err error) {
|
2021-07-16 16:30:45 +00:00
|
|
|
defer dh.t.Logf("finished concurrent deal %d/%d", i, opts.N)
|
2021-06-23 17:13:29 +00:00
|
|
|
defer func() {
|
|
|
|
// This is necessary because golang can't deal with test
|
|
|
|
// failures being reported from children goroutines ¯\_(ツ)_/¯
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("deal failed: %s", r)
|
|
|
|
}
|
|
|
|
}()
|
2021-07-16 16:30:45 +00:00
|
|
|
|
|
|
|
dh.t.Logf("making storage deal %d/%d", i, opts.N)
|
|
|
|
|
2021-06-23 17:13:29 +00:00
|
|
|
deal, res, inPath := dh.MakeOnlineDeal(context.Background(), MakeFullDealParams{
|
|
|
|
Rseed: 5 + i,
|
|
|
|
FastRet: opts.FastRetrieval,
|
|
|
|
StartEpoch: opts.StartEpoch,
|
|
|
|
})
|
2021-07-16 16:30:45 +00:00
|
|
|
|
|
|
|
dh.t.Logf("retrieving deal %d/%d", i, opts.N)
|
|
|
|
|
2021-06-23 17:13:29 +00:00
|
|
|
outPath := dh.PerformRetrieval(context.Background(), deal, res.Root, opts.CarExport)
|
|
|
|
AssertFilesEqual(dh.t, inPath, outPath)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
require.NoError(dh.t, errgrp.Wait())
|
|
|
|
}
|