2020-07-01 16:29:09 +00:00
|
|
|
package testkit
|
2020-06-30 22:02:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-07-27 11:57:01 +00:00
|
|
|
"errors"
|
2020-06-30 22:02:01 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
2021-11-16 13:30:14 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/v0api"
|
2020-06-30 22:02:01 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
files "github.com/ipfs/go-ipfs-files"
|
|
|
|
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"
|
|
|
|
"github.com/ipld/go-car"
|
|
|
|
)
|
|
|
|
|
2020-07-27 11:57:01 +00:00
|
|
|
func RetrieveData(t *TestEnvironment, ctx context.Context, client api.FullNode, fcid cid.Cid, _ *cid.Cid, carExport bool, data []byte) error {
|
2020-06-30 22:02:01 +00:00
|
|
|
t1 := time.Now()
|
2020-07-17 12:28:52 +00:00
|
|
|
offers, err := client.ClientFindData(ctx, fcid, nil)
|
2020-06-30 22:02:01 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, o := range offers {
|
|
|
|
t.D().Counter(fmt.Sprintf("find-data.offer,miner=%s", o.Miner)).Inc(1)
|
|
|
|
}
|
|
|
|
t.D().ResettingHistogram("find-data").Update(int64(time.Since(t1)))
|
|
|
|
|
|
|
|
if len(offers) < 1 {
|
|
|
|
panic("no offers")
|
|
|
|
}
|
|
|
|
|
|
|
|
rpath, err := ioutil.TempDir("", "lotus-retrieve-test-")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(rpath)
|
|
|
|
|
|
|
|
caddr, err := client.WalletDefaultAddress(ctx)
|
|
|
|
if err != nil {
|
2020-07-27 11:57:01 +00:00
|
|
|
return err
|
2020-06-30 22:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ref := &api.FileRef{
|
|
|
|
Path: filepath.Join(rpath, "ret"),
|
|
|
|
IsCAR: carExport,
|
|
|
|
}
|
|
|
|
t1 = time.Now()
|
2021-11-16 13:30:14 +00:00
|
|
|
err = (&v0api.WrapperV1Full{FullNode: client}).ClientRetrieve(ctx, v0api.OfferOrder(offers[0], caddr), ref)
|
2020-06-30 22:02:01 +00:00
|
|
|
if err != nil {
|
2020-07-27 11:57:01 +00:00
|
|
|
return err
|
2020-06-30 22:02:01 +00:00
|
|
|
}
|
|
|
|
t.D().ResettingHistogram("retrieve-data").Update(int64(time.Since(t1)))
|
|
|
|
|
|
|
|
rdata, err := ioutil.ReadFile(filepath.Join(rpath, "ret"))
|
|
|
|
if err != nil {
|
2020-07-27 11:57:01 +00:00
|
|
|
return err
|
2020-06-30 22:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if carExport {
|
2020-07-01 16:29:09 +00:00
|
|
|
rdata = ExtractCarData(ctx, rdata, rpath)
|
2020-06-30 22:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(rdata, data) {
|
2020-07-27 11:57:01 +00:00
|
|
|
return errors.New("wrong data retrieved")
|
2020-06-30 22:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.RecordMessage("retrieved successfully")
|
2020-07-27 11:57:01 +00:00
|
|
|
|
|
|
|
return nil
|
2020-06-30 22:02:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 16:29:09 +00:00
|
|
|
func ExtractCarData(ctx context.Context, rdata []byte, rpath string) []byte {
|
2020-06-30 22:02:01 +00:00
|
|
|
bserv := dstest.Bserv()
|
2021-12-14 16:23:43 +00:00
|
|
|
ch, err := car.LoadCar(ctx, bserv.Blockstore(), bytes.NewReader(rdata))
|
2020-06-30 22:02:01 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
b, err := bserv.GetBlock(ctx, ch.Roots[0])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
nd, err := ipld.Decode(b)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
dserv := dag.NewDAGService(bserv)
|
|
|
|
fil, err := unixfile.NewUnixfsFile(ctx, dserv, nd)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
outPath := filepath.Join(rpath, "retLoadedCAR")
|
|
|
|
if err := files.WriteTo(fil, outPath); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
rdata, err = ioutil.ReadFile(outPath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return rdata
|
|
|
|
}
|