forked from mirror/go-ethereum
swarm/api: integrate tags to count chunks being split and stored swarm/api/http: integrate tags in middleware for HTTP `POST` calls and assert chunks being calculated and counted correctly swarm: remove deprecated and unused code, add swarm hash to DoneSplit signature, remove calls to the api client from the http packageChrisChinchilla-patch-3
parent
3030893a21
commit
ad6c39012f
@ -1,85 +0,0 @@ |
|||||||
// Copyright 2016 The go-ethereum Authors
|
|
||||||
// This file is part of the go-ethereum library.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package api |
|
||||||
|
|
||||||
import ( |
|
||||||
"context" |
|
||||||
"path" |
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/swarm/storage" |
|
||||||
) |
|
||||||
|
|
||||||
type Response struct { |
|
||||||
MimeType string |
|
||||||
Status int |
|
||||||
Size int64 |
|
||||||
// Content []byte
|
|
||||||
Content string |
|
||||||
} |
|
||||||
|
|
||||||
// implements a service
|
|
||||||
//
|
|
||||||
// DEPRECATED: Use the HTTP API instead
|
|
||||||
type Storage struct { |
|
||||||
api *API |
|
||||||
} |
|
||||||
|
|
||||||
func NewStorage(api *API) *Storage { |
|
||||||
return &Storage{api} |
|
||||||
} |
|
||||||
|
|
||||||
// Put uploads the content to the swarm with a simple manifest speficying
|
|
||||||
// its content type
|
|
||||||
//
|
|
||||||
// DEPRECATED: Use the HTTP API instead
|
|
||||||
func (s *Storage) Put(ctx context.Context, content string, contentType string, toEncrypt bool) (storage.Address, func(context.Context) error, error) { |
|
||||||
return s.api.Put(ctx, content, contentType, toEncrypt) |
|
||||||
} |
|
||||||
|
|
||||||
// Get retrieves the content from bzzpath and reads the response in full
|
|
||||||
// It returns the Response object, which serialises containing the
|
|
||||||
// response body as the value of the Content field
|
|
||||||
// NOTE: if error is non-nil, sResponse may still have partial content
|
|
||||||
// the actual size of which is given in len(resp.Content), while the expected
|
|
||||||
// size is resp.Size
|
|
||||||
//
|
|
||||||
// DEPRECATED: Use the HTTP API instead
|
|
||||||
func (s *Storage) Get(ctx context.Context, bzzpath string) (*Response, error) { |
|
||||||
uri, err := Parse(path.Join("bzz:/", bzzpath)) |
|
||||||
if err != nil { |
|
||||||
return nil, err |
|
||||||
} |
|
||||||
addr, err := s.api.Resolve(ctx, uri.Addr) |
|
||||||
if err != nil { |
|
||||||
return nil, err |
|
||||||
} |
|
||||||
reader, mimeType, status, _, err := s.api.Get(ctx, nil, addr, uri.Path) |
|
||||||
if err != nil { |
|
||||||
return nil, err |
|
||||||
} |
|
||||||
quitC := make(chan bool) |
|
||||||
expsize, err := reader.Size(ctx, quitC) |
|
||||||
if err != nil { |
|
||||||
return nil, err |
|
||||||
} |
|
||||||
body := make([]byte, expsize) |
|
||||||
size, err := reader.Read(body) |
|
||||||
if int64(size) == expsize { |
|
||||||
err = nil |
|
||||||
} |
|
||||||
return &Response{mimeType, status, expsize, string(body[:size])}, err |
|
||||||
} |
|
@ -1,56 +0,0 @@ |
|||||||
// Copyright 2016 The go-ethereum Authors
|
|
||||||
// This file is part of the go-ethereum library.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package api |
|
||||||
|
|
||||||
import ( |
|
||||||
"context" |
|
||||||
"testing" |
|
||||||
) |
|
||||||
|
|
||||||
func testStorage(t *testing.T, f func(*Storage, bool)) { |
|
||||||
testAPI(t, func(api *API, toEncrypt bool) { |
|
||||||
f(NewStorage(api), toEncrypt) |
|
||||||
}) |
|
||||||
} |
|
||||||
|
|
||||||
func TestStoragePutGet(t *testing.T) { |
|
||||||
testStorage(t, func(api *Storage, toEncrypt bool) { |
|
||||||
content := "hello" |
|
||||||
exp := expResponse(content, "text/plain", 0) |
|
||||||
// exp := expResponse([]byte(content), "text/plain", 0)
|
|
||||||
ctx := context.TODO() |
|
||||||
bzzkey, wait, err := api.Put(ctx, content, exp.MimeType, toEncrypt) |
|
||||||
if err != nil { |
|
||||||
t.Fatalf("unexpected error: %v", err) |
|
||||||
} |
|
||||||
err = wait(ctx) |
|
||||||
if err != nil { |
|
||||||
t.Fatalf("unexpected error: %v", err) |
|
||||||
} |
|
||||||
bzzhash := bzzkey.Hex() |
|
||||||
// to check put against the API#Get
|
|
||||||
resp0 := testGet(t, api.api, bzzhash, "") |
|
||||||
checkResponse(t, resp0, exp) |
|
||||||
|
|
||||||
// check storage#Get
|
|
||||||
resp, err := api.Get(context.TODO(), bzzhash) |
|
||||||
if err != nil { |
|
||||||
t.Fatalf("unexpected error: %v", err) |
|
||||||
} |
|
||||||
checkResponse(t, &testResponse{nil, resp}, exp) |
|
||||||
}) |
|
||||||
} |
|
@ -0,0 +1,48 @@ |
|||||||
|
// Copyright 2019 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package chunk |
||||||
|
|
||||||
|
import "testing" |
||||||
|
|
||||||
|
func TestAll(t *testing.T) { |
||||||
|
ts := NewTags() |
||||||
|
|
||||||
|
ts.New("1", 1) |
||||||
|
ts.New("2", 1) |
||||||
|
|
||||||
|
all := ts.All() |
||||||
|
|
||||||
|
if len(all) != 2 { |
||||||
|
t.Fatalf("expected length to be 2 got %d", len(all)) |
||||||
|
} |
||||||
|
|
||||||
|
if n := all[0].Total(); n != 1 { |
||||||
|
t.Fatalf("expected tag 0 total to be 1 got %d", n) |
||||||
|
} |
||||||
|
|
||||||
|
if n := all[1].Total(); n != 1 { |
||||||
|
t.Fatalf("expected tag 1 total to be 1 got %d", n) |
||||||
|
} |
||||||
|
|
||||||
|
ts.New("3", 1) |
||||||
|
all = ts.All() |
||||||
|
|
||||||
|
if len(all) != 3 { |
||||||
|
t.Fatalf("expected length to be 3 got %d", len(all)) |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,51 @@ |
|||||||
|
// Copyright 2019 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package testutil |
||||||
|
|
||||||
|
import ( |
||||||
|
"testing" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/swarm/chunk" |
||||||
|
) |
||||||
|
|
||||||
|
// CheckTag checks the first tag in the api struct to be in a certain state
|
||||||
|
func CheckTag(t *testing.T, tag *chunk.Tag, split, stored, seen, total int64) { |
||||||
|
t.Helper() |
||||||
|
if tag == nil { |
||||||
|
t.Fatal("no tag found") |
||||||
|
} |
||||||
|
|
||||||
|
tSplit := tag.Get(chunk.StateSplit) |
||||||
|
if tSplit != split { |
||||||
|
t.Fatalf("should have had split chunks, got %d want %d", tSplit, split) |
||||||
|
} |
||||||
|
|
||||||
|
tSeen := tag.Get(chunk.StateSeen) |
||||||
|
if tSeen != seen { |
||||||
|
t.Fatalf("should have had seen chunks, got %d want %d", tSeen, seen) |
||||||
|
} |
||||||
|
|
||||||
|
tStored := tag.Get(chunk.StateStored) |
||||||
|
if tStored != stored { |
||||||
|
t.Fatalf("mismatch stored chunks, got %d want %d", tStored, stored) |
||||||
|
} |
||||||
|
|
||||||
|
tTotal := tag.Total() |
||||||
|
if tTotal != total { |
||||||
|
t.Fatalf("mismatch total chunks, got %d want %d", tTotal, total) |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue