Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
e9d6d6c
storage,shed,api,cmd: Pin with upload
jmozah Jun 23, 2019
992a65a
Moved pinning to a seperate indexs instead of retrievalDataIndex
jmozah Jun 24, 2019
c0e3dd7
GC now ignores pinned chunks
jmozah Jun 24, 2019
96f1555
Added pin support for manifest and access commands
jmozah Jun 25, 2019
9b61c0e
Fix tests errors
jmozah Jun 25, 2019
a60d601
Fix lint errors
jmozah Jun 25, 2019
52bf5c6
Fix lint errors
jmozah Jun 25, 2019
179179e
Fix address copy bug
jmozah Jun 25, 2019
6c1bb08
Fix client api testcase
jmozah Jun 25, 2019
6ef0469
Fix the 'could not add file in pinFilesIndex' yesycase failing
jmozah Jun 26, 2019
3ac561b
Unpin support, Display merkle tree from a root chunk
jmozah Jun 26, 2019
14b4acf
Fix lint errors
jmozah Jun 26, 2019
a8f2f56
Fix more lint issues
jmozah Jun 26, 2019
1cb08b3
Fix lint error
jmozah Jul 10, 2019
6451240
Added testcases. Fixed bugs
jmozah Jul 15, 2019
63dfa67
Fix test case for multi manifest scenario
jmozah Jul 15, 2019
bd22661
Add multipart upload support and testcase for that
jmozah Jul 16, 2019
1379bc1
Added new mode for pinning and fixed test cases
jmozah Jul 16, 2019
a99b873
Testcase refactoring to accomodate seperate pin mode
jmozah Jul 16, 2019
c06c937
Fix import cycles in testcase
jmozah Jul 16, 2019
b5519a5
Added gdoc
jmozah Jul 16, 2019
ed6609b
Final touches
jmozah Jul 16, 2019
ca3be88
GC testcase fixes
jmozah Jul 16, 2019
aa18362
Merge branch 'master' of https://github.com/ethersphere/swarm into fe…
jmozah Jul 16, 2019
addb820
Lint fixes
jmozah Jul 16, 2019
2bc9fef
Lint lint oh my lint
jmozah Jul 16, 2019
b9ee58a
Regression during lint
jmozah Jul 16, 2019
76e6bf4
Display noOfChunks for each pinned hash
jmozah Jul 17, 2019
754b6bf
Fix regression
jmozah Jul 17, 2019
e2d8008
Fixed comments given by Viktor
jmozah Jul 19, 2019
9aedc2b
Fixed Janos's review comments
jmozah Jul 19, 2019
1115873
Fixing smoke test merge issue
jmozah Jul 19, 2019
92bd83a
Fix more review comments
jmozah Jul 19, 2019
1920496
Some more review comments fix
jmozah Jul 19, 2019
9362108
Fixing Elad's review comment
jmozah Jul 19, 2019
cd378d8
Fix few more comments of Viktor
jmozah Jul 19, 2019
c5fc971
Fixed comments by Elad
jmozah Jul 19, 2019
f4f70af
Lint, regression and few review comments
jmozah Jul 20, 2019
2a6e212
Fixed import cycle issue for pin.go and fixed review few comments
jmozah Jul 20, 2019
adc19a2
Moved pin files to /storage. Removed exporting test functions. other …
jmozah Jul 20, 2019
092b9b0
Fix lint error
jmozah Jul 20, 2019
083d20c
Fix linnt errors
jmozah Jul 21, 2019
1bd9740
Fixed the second round of review comments from Viktor and Elad
jmozah Jul 28, 2019
71de4d4
Added close function
jmozah Jul 29, 2019
c9502dd
Merge branch 'master' of https://github.com/ethersphere/swarm into fe…
jmozah Jul 29, 2019
d74ab00
Fixed linter errors
jmozah Jul 29, 2019
ad3e0df
Lint fixes
jmozah Jul 29, 2019
fb2dddc
storage/pin: Removed state storage helper
jmozah Jul 29, 2019
134ece7
storage/pin: Increased the file sizes in collection testcases
jmozah Jul 29, 2019
dc05379
storage/pin: Fix lint
jmozah Jul 29, 2019
ac77d31
storage/pin: test case for state store of file info and other review …
jmozah Jul 29, 2019
e213688
storage/pin, storage/localstore: Fix review comments
jmozah Jul 31, 2019
bacfe01
Fix review comments
jmozah Jul 31, 2019
b12ed54
Make Error to Fatal
jmozah Jul 31, 2019
d29796b
Fixing Review comments
jmozah Aug 1, 2019
627f173
Fix edge test case when unpinning file
jmozah Aug 1, 2019
78c731c
Walker now returning error
jmozah Aug 1, 2019
e4e8138
Lint fixes
jmozah Aug 1, 2019
1b46b19
Fix more lints
jmozah Aug 1, 2019
11a4915
Code simplification
jmozah Aug 1, 2019
f83b860
Lint error
jmozah Aug 1, 2019
0e7e2e1
Fix race condition in walker
jmozah Aug 1, 2019
cbab2c1
Simplify conditon
jmozah Aug 1, 2019
c450bd5
spelling mistake
jmozah Aug 1, 2019
729c8f9
Merge branch 'master' of https://github.com/fairDataSociety/swarm int…
jmozah Aug 1, 2019
f90ab40
Merge branch 'master' of https://github.com/ethersphere/swarm into fe…
jmozah Aug 1, 2019
3ff9e96
Add comments removed while refactoring
jmozah Aug 2, 2019
90ebad7
Fixed final comments from Janos
jmozah Aug 2, 2019
1ca63c6
Fixed few comment review from Elad
jmozah Aug 5, 2019
8762cb3
Changing pin header name and moving to http package
jmozah Aug 5, 2019
1c775d4
Changed the API input from string to []byte
jmozah Aug 5, 2019
410df80
Fixed errors while converting it to []bytes
jmozah Aug 5, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@ package api

import (
"archive/tar"
"bytes"
"context"
"crypto/ecdsa"
"encoding/hex"
"errors"
"fmt"
"io"
"math/big"
"mime"
"net/http"
"path"
"strings"

"bytes"
"mime"
"path/filepath"
"strings"
"time"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -47,8 +46,7 @@ import (
"github.com/ethersphere/swarm/storage"
"github.com/ethersphere/swarm/storage/feed"
"github.com/ethersphere/swarm/storage/feed/lookup"

opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go"
)

var (
Expand Down Expand Up @@ -400,6 +398,8 @@ func (a *API) Get(ctx context.Context, decrypt DecryptFunc, manifestAddr storage
return
}

// Delete handles removing a file from the manifest.
// This creates a new manifest without the given path
func (a *API) Delete(ctx context.Context, addr string, path string) (storage.Address, error) {
apiDeleteCount.Inc(1)
uri, err := Parse("bzz:/" + addr)
Expand Down
32 changes: 23 additions & 9 deletions api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Client struct {

// UploadRaw uploads raw data to swarm and returns the resulting hash. If toEncrypt is true it
// uploads encrypted data
func (c *Client) UploadRaw(r io.Reader, size int64, toEncrypt bool) (string, error) {
func (c *Client) UploadRaw(r io.Reader, size int64, toEncrypt bool, toPin bool) (string, error) {
if size <= 0 {
return "", errors.New("data size must be greater than zero")
}
Expand All @@ -78,6 +78,11 @@ func (c *Client) UploadRaw(r io.Reader, size int64, toEncrypt bool) (string, err
req.ContentLength = size
req.Header.Set(swarmhttp.SwarmTagHeaderName, fmt.Sprintf("raw_upload_%d", time.Now().Unix()))

// Set the pinning header if the file needs to be pinned
if toPin {
req.Header.Set(swarmhttp.PinHeaderName, "true")
}

res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
Expand Down Expand Up @@ -151,11 +156,11 @@ func Open(path string) (*File, error) {
// (if the manifest argument is non-empty) or creates a new manifest containing
// the file, returning the resulting manifest hash (the file will then be
// available at bzz:/<hash>/<path>)
func (c *Client) Upload(file *File, manifest string, toEncrypt bool) (string, error) {
func (c *Client) Upload(file *File, manifest string, toEncrypt bool, toPin bool) (string, error) {
if file.Size <= 0 {
return "", errors.New("file size must be greater than zero")
}
return c.TarUpload(manifest, &FileUploader{file}, "", toEncrypt)
return c.TarUpload(manifest, &FileUploader{file}, "", toEncrypt, toPin)
}

// Download downloads a file with the given path from the swarm manifest with
Expand Down Expand Up @@ -185,7 +190,7 @@ func (c *Client) Download(hash, path string) (*File, error) {
// directory will then be available at bzz:/<hash>/path/to/file), with
// the file specified in defaultPath being uploaded to the root of the manifest
// (i.e. bzz:/<hash>/)
func (c *Client) UploadDirectory(dir, defaultPath, manifest string, toEncrypt bool) (string, error) {
func (c *Client) UploadDirectory(dir, defaultPath, manifest string, toEncrypt bool, toPin bool) (string, error) {
stat, err := os.Stat(dir)
if err != nil {
return "", err
Expand All @@ -200,7 +205,7 @@ func (c *Client) UploadDirectory(dir, defaultPath, manifest string, toEncrypt bo
return "", fmt.Errorf("default path: %v", err)
}
}
return c.TarUpload(manifest, &DirectoryUploader{dir}, defaultPath, toEncrypt)
return c.TarUpload(manifest, &DirectoryUploader{dir}, defaultPath, toEncrypt, toPin)
}

// DownloadDirectory downloads the files contained in a swarm manifest under
Expand Down Expand Up @@ -358,12 +363,13 @@ func (c *Client) DownloadFile(hash, path, dest, credentials string) error {
}

// UploadManifest uploads the given manifest to swarm
func (c *Client) UploadManifest(m *api.Manifest, toEncrypt bool) (string, error) {
func (c *Client) UploadManifest(m *api.Manifest, toEncrypt bool, toPin bool) (string, error) {
data, err := json.Marshal(m)
if err != nil {
return "", err
}
return c.UploadRaw(bytes.NewReader(data), int64(len(data)), toEncrypt)

return c.UploadRaw(bytes.NewReader(data), int64(len(data)), toEncrypt, toPin)
}

// DownloadManifest downloads a swarm manifest
Expand Down Expand Up @@ -498,7 +504,7 @@ type UploadFn func(file *File) error

// TarUpload uses the given Uploader to upload files to swarm as a tar stream,
// returning the resulting manifest hash
func (c *Client) TarUpload(hash string, uploader Uploader, defaultPath string, toEncrypt bool) (string, error) {
func (c *Client) TarUpload(hash string, uploader Uploader, defaultPath string, toEncrypt bool, toPin bool) (string, error) {
ctx, sp := spancontext.StartSpan(context.Background(), "api.client.tarupload")
defer sp.Finish()

Expand Down Expand Up @@ -540,6 +546,11 @@ func (c *Client) TarUpload(hash string, uploader Uploader, defaultPath string, t

req.Header.Set(swarmhttp.SwarmTagHeaderName, tag)

// Set the pinning header if the file is to be pinned
if toPin {
req.Header.Set(swarmhttp.PinHeaderName, "true")
}

// use 'Expect: 100-continue' so we don't send the request body if
// the server refuses the request
req.Header.Set("Expect", "100-continue")
Expand Down Expand Up @@ -591,7 +602,7 @@ func (c *Client) TarUpload(hash string, uploader Uploader, defaultPath string, t

// MultipartUpload uses the given Uploader to upload files to swarm as a
// multipart form, returning the resulting manifest hash
func (c *Client) MultipartUpload(hash string, uploader Uploader) (string, error) {
func (c *Client) MultipartUpload(hash string, uploader Uploader, toPin bool) (string, error) {
reqR, reqW := io.Pipe()
defer reqR.Close()
req, err := http.NewRequest("POST", c.Gateway+"/bzz:/"+hash, reqR)
Expand All @@ -606,6 +617,9 @@ func (c *Client) MultipartUpload(hash string, uploader Uploader) (string, error)
mw := multipart.NewWriter(reqW)
req.Header.Set("Content-Type", fmt.Sprintf("multipart/form-data; boundary=%q", mw.Boundary()))
req.Header.Set(swarmhttp.SwarmTagHeaderName, fmt.Sprintf("multipart_upload_%d", time.Now().Unix()))
if toPin {
req.Header.Set(swarmhttp.PinHeaderName, "true")
}

// define an UploadFn which adds files to the multipart form
uploadFn := func(file *File) error {
Expand Down
84 changes: 49 additions & 35 deletions api/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,47 @@ import (
)

func serverFunc(api *api.API) swarmhttp.TestServer {
return swarmhttp.NewServer(api, "")
return swarmhttp.NewServer(api, nil, "")
}

// TestClientUploadDownloadRaw test uploading and downloading raw data to swarm
func TestClientUploadDownloadRaw(t *testing.T) {
testClientUploadDownloadRaw(false, t)
Comment thread
jmozah marked this conversation as resolved.
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil, nil)
Comment thread
jmozah marked this conversation as resolved.
defer srv.Close()

data := []byte("foo123")
testClientUploadDownloadRaw(srv, false, t, data, false)

// check the tag was created successfully
tag := srv.Tags.All()[0]
testutil.CheckTag(t, tag, 1, 1, 0, 1)
}

func TestClientUploadDownloadRawEncrypted(t *testing.T) {

if testutil.RaceEnabled {
t.Skip("flaky with -race on Travis")
// See: https://github.com/ethersphere/go-ethereum/issues/1254
}

testClientUploadDownloadRaw(true, t)
}

func testClientUploadDownloadRaw(toEncrypt bool, t *testing.T) {
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil, nil)
defer srv.Close()

client := NewClient(srv.URL)

// upload some raw data
data := []byte("foo123")
hash, err := client.UploadRaw(bytes.NewReader(data), int64(len(data)), toEncrypt)
if err != nil {
t.Fatal(err)
}
testClientUploadDownloadRaw(srv, true, t, data, false)

// check the tag was created successfully
tag := srv.Tags.All()[0]
testutil.CheckTag(t, tag, 1, 1, 0, 1)
}

func testClientUploadDownloadRaw(srv *swarmhttp.TestSwarmServer, toEncrypt bool, t *testing.T, data []byte, toPin bool) string {
client := NewClient(srv.URL)

hash, err := client.UploadRaw(bytes.NewReader(data), int64(len(data)), toEncrypt, toPin)
if err != nil {
t.Fatal(err)
}

// check we can download the same data
res, isEncrypted, err := client.DownloadRaw(hash)
Expand All @@ -86,6 +94,8 @@ func testClientUploadDownloadRaw(toEncrypt bool, t *testing.T) {
if !bytes.Equal(gotData, data) {
t.Fatalf("expected downloaded data to be %q, got %q", data, gotData)
}

return hash
}

// TestClientUploadDownloadFiles test uploading and downloading files to swarm
Expand All @@ -98,12 +108,13 @@ func TestClientUploadDownloadFilesEncrypted(t *testing.T) {
testClientUploadDownloadFiles(true, t)
}

func testClientUploadDownloadFiles(toEncrypt bool, t *testing.T) {
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
func testClientUploadDownloadFiles(toEncrypt bool, t *testing.T) string {

srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil, nil)
defer srv.Close()

client := NewClient(srv.URL)
upload := func(manifest, path string, data []byte) string {
upload := func(manifest, path string, data []byte, toPin bool) string {
file := &File{
ReadCloser: ioutil.NopCloser(bytes.NewReader(data)),
ManifestEntry: api.ManifestEntry{
Expand All @@ -112,7 +123,7 @@ func testClientUploadDownloadFiles(toEncrypt bool, t *testing.T) {
Size: int64(len(data)),
},
}
hash, err := client.Upload(file, manifest, toEncrypt)
hash, err := client.Upload(file, manifest, toEncrypt, toPin)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -141,25 +152,27 @@ func testClientUploadDownloadFiles(toEncrypt bool, t *testing.T) {

// upload a file to the root of a manifest
rootData := []byte("some-data")
rootHash := upload("", "", rootData)
rootHash := upload("", "", rootData, false)

// check we can download the root file
checkDownload(rootHash, "", rootData)

// upload another file to the same manifest
otherData := []byte("some-other-data")
newHash := upload(rootHash, "some/other/path", otherData)
newHash := upload(rootHash, "some/other/path", otherData, false)

// check we can download both files from the new manifest
checkDownload(newHash, "", rootData)
checkDownload(newHash, "some/other/path", otherData)

// replace the root file with different data
newHash = upload(newHash, "", otherData)
newHash = upload(newHash, "", otherData, false)

// check both files have the other data
checkDownload(newHash, "", otherData)
checkDownload(newHash, "some/other/path", otherData)

return newHash
}

var testDirFiles = []string{
Expand Down Expand Up @@ -194,10 +207,10 @@ func newTestDirectory(t *testing.T) string {
return dir
}

// TestClientUploadDownloadDirectory tests uploading and downloading a
// TestClientUploadDownloadDirectory tests uploading and downloading
// directory of files to a swarm manifest
func TestClientUploadDownloadDirectory(t *testing.T) {
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil, nil)
defer srv.Close()

dir := newTestDirectory(t)
Expand All @@ -206,7 +219,7 @@ func TestClientUploadDownloadDirectory(t *testing.T) {
// upload the directory
client := NewClient(srv.URL)
defaultPath := testDirFiles[0]
hash, err := client.UploadDirectory(dir, defaultPath, "", false)
hash, err := client.UploadDirectory(dir, defaultPath, "", false, false)
if err != nil {
t.Fatalf("error uploading directory: %s", err)
}
Expand Down Expand Up @@ -267,14 +280,14 @@ func TestClientFileListEncrypted(t *testing.T) {
}

func testClientFileList(toEncrypt bool, t *testing.T) {
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil, nil)
defer srv.Close()

dir := newTestDirectory(t)
defer os.RemoveAll(dir)

client := NewClient(srv.URL)
hash, err := client.UploadDirectory(dir, "", "", toEncrypt)
hash, err := client.UploadDirectory(dir, "", "", toEncrypt, false)
if err != nil {
t.Fatalf("error uploading directory: %s", err)
}
Expand Down Expand Up @@ -325,14 +338,14 @@ func testClientFileList(toEncrypt bool, t *testing.T) {
// TestClientMultipartUpload tests uploading files to swarm using a multipart
// upload
func TestClientMultipartUpload(t *testing.T) {
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil, nil)
defer srv.Close()

// define an uploader which uploads testDirFiles with some data
// note: this test should result in SEEN chunks. assert accordingly
data := []byte("some-data")
uploader := UploaderFunc(func(upload UploadFn) error {
for _, name := range testDirFiles {
data := []byte(name)
file := &File{
ReadCloser: ioutil.NopCloser(bytes.NewReader(data)),
ManifestEntry: api.ManifestEntry{
Expand All @@ -350,14 +363,14 @@ func TestClientMultipartUpload(t *testing.T) {

// upload the files as a multipart upload
client := NewClient(srv.URL)
hash, err := client.MultipartUpload("", uploader)
hash, err := client.MultipartUpload("", uploader, false)
if err != nil {
t.Fatal(err)
}

// check the tag was created successfully
tag := srv.Tags.All()[0]
testutil.CheckTag(t, tag, 9, 9, 7, 9)
testutil.CheckTag(t, tag, 9, 9, 0, 9)

// check we can download the individual files
checkDownloadFile := func(path string) {
Expand All @@ -370,8 +383,9 @@ func TestClientMultipartUpload(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(gotData, data) {
t.Fatalf("expected data to be %q, got %q", data, gotData)
// The content is the file name just to make them different
if !bytes.Equal(gotData, []byte(path)) {
Comment thread
jmozah marked this conversation as resolved.
t.Fatalf("expected data to be %q, got %q", path, gotData)
}
}
for _, file := range testDirFiles {
Expand All @@ -398,7 +412,7 @@ func TestClientBzzWithFeed(t *testing.T) {
signer, _ := newTestSigner()

// Initialize a Swarm test server
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil, nil)
swarmClient := NewClient(srv.URL)
defer srv.Close()

Expand Down Expand Up @@ -433,7 +447,7 @@ func TestClientBzzWithFeed(t *testing.T) {
}

// upload data to bzz:// and retrieve the content-addressed manifest hash, hex-encoded.
manifestAddressHex, err := swarmClient.Upload(f, "", false)
manifestAddressHex, err := swarmClient.Upload(f, "", false, false)
if err != nil {
t.Fatalf("Error creating manifest: %s", err)
}
Expand Down Expand Up @@ -516,7 +530,7 @@ func TestClientCreateUpdateFeed(t *testing.T) {

signer, _ := newTestSigner()

srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil, nil)
client := NewClient(srv.URL)
defer srv.Close()

Expand Down
Loading