From 8f895011186f14187e9c8a25be1e1f1fd055eab2 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Mon, 30 Mar 2020 11:08:20 -0300 Subject: [PATCH] fix ClientRetrieve with CAR exporting & add test coverage Signed-off-by: Ignacio Hagopian --- api/test/deals.go | 40 ++++++++++++++++++++++++++++++++++++-- node/impl/client/client.go | 2 +- node/node_test.go | 17 ++++++++++------ 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/api/test/deals.go b/api/test/deals.go index 695000b70..1d3baf138 100644 --- a/api/test/deals.go +++ b/api/test/deals.go @@ -11,13 +11,20 @@ import ( "testing" "time" + "github.com/ipfs/go-car" + files "github.com/ipfs/go-ipfs-files" logging "github.com/ipfs/go-log/v2" "github.com/filecoin-project/go-fil-markets/storagemarket" "github.com/filecoin-project/lotus/api" "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/node/impl" + ipld "github.com/ipfs/go-ipld-format" ) func init() { @@ -25,7 +32,7 @@ func init() { 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") ctx := context.Background() @@ -133,7 +140,7 @@ loop: ref := api.FileRef{ Path: filepath.Join(rpath, "ret"), - IsCAR: false, + IsCAR: carExport, } err = client.ClientRetrieve(ctx, offers[0].Order(caddr), ref) if err != nil { @@ -145,6 +152,35 @@ loop: 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) { t.Fatal("wrong data retrieved") } diff --git a/node/impl/client/client.go b/node/impl/client/client.go index dcc61866a..b2fcfc48c 100644 --- a/node/impl/client/client.go +++ b/node/impl/client/client.go @@ -377,7 +377,7 @@ func (a *API) ClientRetrieve(ctx context.Context, order api.RetrievalOrder, ref unsubscribe() 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 { return err } diff --git a/node/node_test.go b/node/node_test.go index 0e962cba8..cb4516c72 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -4,14 +4,15 @@ import ( "bytes" "context" "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" "net/http/httptest" "testing" "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/ipfs/go-datastore" logging "github.com/ipfs/go-log/v2" @@ -43,7 +44,7 @@ import ( "github.com/filecoin-project/lotus/node/modules" modtest "github.com/filecoin-project/lotus/node/modules/testing" "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" ) @@ -449,7 +450,11 @@ func TestAPIDealFlow(t *testing.T) { logging.SetLogLevel("sub", "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) { @@ -463,5 +468,5 @@ func TestAPIDealFlowReal(t *testing.T) { logging.SetLogLevel("sub", "ERROR") logging.SetLogLevel("storageminer", "ERROR") - test.TestDealFlow(t, builder, time.Second) + test.TestDealFlow(t, builder, time.Second, false) }