2020-07-01 16:29:09 +00:00
|
|
|
package testkit
|
2020-06-30 22:02:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"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-01 16:29:09 +00:00
|
|
|
func RetrieveData(t *TestEnvironment, ctx context.Context, err error, client api.FullNode, fcid cid.Cid, carExport bool, data []byte) {
|
2020-06-30 22:02:01 +00:00
|
|
|
t1 := time.Now()
|
|
|
|
offers, err := client.ClientFindData(ctx, fcid)
|
|
|
|
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 {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ref := &api.FileRef{
|
|
|
|
Path: filepath.Join(rpath, "ret"),
|
|
|
|
IsCAR: carExport,
|
|
|
|
}
|
|
|
|
t1 = time.Now()
|
|
|
|
err = client.ClientRetrieve(ctx, offers[0].Order(caddr), ref)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
t.D().ResettingHistogram("retrieve-data").Update(int64(time.Since(t1)))
|
|
|
|
|
|
|
|
rdata, err := ioutil.ReadFile(filepath.Join(rpath, "ret"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
panic("wrong data retrieved")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.RecordMessage("retrieved successfully")
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
ch, err := car.LoadCar(bserv.Blockstore(), bytes.NewReader(rdata))
|
|
|
|
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
|
|
|
|
}
|