fix ClientRetrieve with CAR exporting & add test coverage

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
This commit is contained in:
Ignacio Hagopian 2020-03-30 11:08:20 -03:00
parent 34e9e9fe34
commit 8f89501118
3 changed files with 50 additions and 9 deletions

View File

@ -11,13 +11,20 @@ import (
"testing" "testing"
"time" "time"
"github.com/ipfs/go-car"
files "github.com/ipfs/go-ipfs-files"
logging "github.com/ipfs/go-log/v2" logging "github.com/ipfs/go-log/v2"
"github.com/filecoin-project/go-fil-markets/storagemarket" "github.com/filecoin-project/go-fil-markets/storagemarket"
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/build"
dag "github.com/ipfs/go-merkledag"
dstest "github.com/ipfs/go-merkledag/test"
unixfile "github.com/ipfs/go-unixfs/file"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/node/impl" "github.com/filecoin-project/lotus/node/impl"
ipld "github.com/ipfs/go-ipld-format"
) )
func init() { func init() {
@ -25,7 +32,7 @@ func init() {
build.InsecurePoStValidation = true build.InsecurePoStValidation = true
} }
func TestDealFlow(t *testing.T, b APIBuilder, blocktime time.Duration) { func TestDealFlow(t *testing.T, b APIBuilder, blocktime time.Duration, carExport bool) {
os.Setenv("BELLMAN_NO_GPU", "1") os.Setenv("BELLMAN_NO_GPU", "1")
ctx := context.Background() ctx := context.Background()
@ -133,7 +140,7 @@ loop:
ref := api.FileRef{ ref := api.FileRef{
Path: filepath.Join(rpath, "ret"), Path: filepath.Join(rpath, "ret"),
IsCAR: false, IsCAR: carExport,
} }
err = client.ClientRetrieve(ctx, offers[0].Order(caddr), ref) err = client.ClientRetrieve(ctx, offers[0].Order(caddr), ref)
if err != nil { if err != nil {
@ -145,6 +152,35 @@ loop:
t.Fatal(err) t.Fatal(err)
} }
if carExport {
bserv := dstest.Bserv()
ch, err := car.LoadCar(bserv.Blockstore(), bytes.NewReader(rdata))
if err != nil {
t.Fatal(err)
}
b, err := bserv.GetBlock(ctx, ch.Roots[0])
if err != nil {
t.Fatal(err)
}
nd, err := ipld.Decode(b)
if err != nil {
t.Fatal(err)
}
dserv := dag.NewDAGService(bserv)
fil, err := unixfile.NewUnixfsFile(ctx, dserv, nd)
if err != nil {
t.Fatal(err)
}
outPath := filepath.Join(rpath, "retLoadedCAR")
if err := files.WriteTo(fil, outPath); err != nil {
t.Fatal(err)
}
rdata, err = ioutil.ReadFile(outPath)
if err != nil {
t.Fatal(err)
}
}
if !bytes.Equal(rdata, data) { if !bytes.Equal(rdata, data) {
t.Fatal("wrong data retrieved") t.Fatal("wrong data retrieved")
} }

View File

@ -377,7 +377,7 @@ func (a *API) ClientRetrieve(ctx context.Context, order api.RetrievalOrder, ref
unsubscribe() unsubscribe()
if ref.IsCAR { if ref.IsCAR {
f, err := os.Open(ref.Path) f, err := os.OpenFile(ref.Path, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
return err return err
} }

View File

@ -4,14 +4,15 @@ import (
"bytes" "bytes"
"context" "context"
"crypto/rand" "crypto/rand"
"github.com/filecoin-project/lotus/lib/lotuslog"
"github.com/filecoin-project/lotus/storage/mockstorage"
"github.com/filecoin-project/sector-storage/ffiwrapper"
"io/ioutil" "io/ioutil"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"time" "time"
"github.com/filecoin-project/lotus/lib/lotuslog"
"github.com/filecoin-project/lotus/storage/mockstorage"
"github.com/filecoin-project/sector-storage/ffiwrapper"
"github.com/filecoin-project/go-fil-markets/storedcounter" "github.com/filecoin-project/go-fil-markets/storedcounter"
"github.com/ipfs/go-datastore" "github.com/ipfs/go-datastore"
logging "github.com/ipfs/go-log/v2" logging "github.com/ipfs/go-log/v2"
@ -43,7 +44,7 @@ import (
"github.com/filecoin-project/lotus/node/modules" "github.com/filecoin-project/lotus/node/modules"
modtest "github.com/filecoin-project/lotus/node/modules/testing" modtest "github.com/filecoin-project/lotus/node/modules/testing"
"github.com/filecoin-project/lotus/node/repo" "github.com/filecoin-project/lotus/node/repo"
"github.com/filecoin-project/sector-storage" sectorstorage "github.com/filecoin-project/sector-storage"
"github.com/filecoin-project/sector-storage/mock" "github.com/filecoin-project/sector-storage/mock"
) )
@ -449,7 +450,11 @@ func TestAPIDealFlow(t *testing.T) {
logging.SetLogLevel("sub", "ERROR") logging.SetLogLevel("sub", "ERROR")
logging.SetLogLevel("storageminer", "ERROR") logging.SetLogLevel("storageminer", "ERROR")
test.TestDealFlow(t, mockSbBuilder, 10*time.Millisecond) test.TestDealFlow(t, mockSbBuilder, 10*time.Millisecond, false)
t.Run("WithExportedCAR", func(t *testing.T) {
test.TestDealFlow(t, mockSbBuilder, 10*time.Millisecond, true)
})
} }
func TestAPIDealFlowReal(t *testing.T) { func TestAPIDealFlowReal(t *testing.T) {
@ -463,5 +468,5 @@ func TestAPIDealFlowReal(t *testing.T) {
logging.SetLogLevel("sub", "ERROR") logging.SetLogLevel("sub", "ERROR")
logging.SetLogLevel("storageminer", "ERROR") logging.SetLogLevel("storageminer", "ERROR")
test.TestDealFlow(t, builder, time.Second) test.TestDealFlow(t, builder, time.Second, false)
} }