Skip to content

Commit 7f55b0c

Browse files
holisticodenonsense
authored andcommitted
cmd/swarm: hashes command (ethereum#19008)
1 parent 85b3b1c commit 7f55b0c

File tree

4 files changed

+71
-14
lines changed

4 files changed

+71
-14
lines changed

cmd/swarm/explore.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2019 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// go-ethereum is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// Command bzzhash computes a swarm tree hash.
18+
package main
19+
20+
import (
21+
"context"
22+
"fmt"
23+
"os"
24+
25+
"github.com/ethereum/go-ethereum/cmd/utils"
26+
"github.com/ethereum/go-ethereum/swarm/storage"
27+
"gopkg.in/urfave/cli.v1"
28+
)
29+
30+
var hashesCommand = cli.Command{
31+
Action: hashes,
32+
CustomHelpTemplate: helpTemplate,
33+
Name: "hashes",
34+
Usage: "print all hashes of a file to STDOUT",
35+
ArgsUsage: "<file>",
36+
Description: "Prints all hashes of a file to STDOUT",
37+
}
38+
39+
func hashes(ctx *cli.Context) {
40+
args := ctx.Args()
41+
if len(args) < 1 {
42+
utils.Fatalf("Usage: swarm hashes <file name>")
43+
}
44+
f, err := os.Open(args[0])
45+
if err != nil {
46+
utils.Fatalf("Error opening file " + args[1])
47+
}
48+
defer f.Close()
49+
50+
fileStore := storage.NewFileStore(&storage.FakeChunkStore{}, storage.NewFileStoreParams())
51+
refs, err := fileStore.GetAllReferences(context.TODO(), f, false)
52+
if err != nil {
53+
utils.Fatalf("%v\n", err)
54+
} else {
55+
for _, r := range refs {
56+
fmt.Println(r.String())
57+
}
58+
}
59+
}

cmd/swarm/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ func init() {
142142
dbCommand,
143143
// See config.go
144144
DumpConfigCommand,
145+
// hashesCommand
146+
hashesCommand,
145147
}
146148

147149
// append a hidden help subcommand to all commands that have subcommands

swarm/storage/filestore.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func NewFileStore(store ChunkStore, params *FileStoreParams) *FileStore {
7575
}
7676
}
7777

78-
// Public API. Main entry point for document retrieval directly. Used by the
78+
// Retrieve is a public API. Main entry point for document retrieval directly. Used by the
7979
// FS-aware API and httpaccess
8080
// Chunk retrieval blocks on netStore requests with a timeout so reader will
8181
// report error if retrieval of chunks within requested range time out.
@@ -87,7 +87,7 @@ func (f *FileStore) Retrieve(ctx context.Context, addr Address) (reader *LazyChu
8787
return
8888
}
8989

90-
// Public API. Main entry point for document storage directly. Used by the
90+
// Store is a public API. Main entry point for document storage directly. Used by the
9191
// FS-aware API and httpaccess
9292
func (f *FileStore) Store(ctx context.Context, data io.Reader, size int64, toEncrypt bool) (addr Address, wait func(context.Context) error, err error) {
9393
putter := NewHasherStore(f.ChunkStore, f.hashFunc, toEncrypt)
@@ -98,7 +98,7 @@ func (f *FileStore) HashSize() int {
9898
return f.hashFunc().Size()
9999
}
100100

101-
// Public API. This endpoint returns all chunk hashes (only) for a given file
101+
// GetAllReferences is a public API. This endpoint returns all chunk hashes (only) for a given file
102102
func (f *FileStore) GetAllReferences(ctx context.Context, data io.Reader, toEncrypt bool) (addrs AddressCollection, err error) {
103103
// create a special kind of putter, which only will store the references
104104
putter := &HashExplorer{

swarm/storage/filestore_test.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -190,22 +190,18 @@ func TestGetAllReferences(t *testing.T) {
190190
}
191191
fileStore := NewFileStore(localStore, NewFileStoreParams())
192192

193-
checkRefs := func(dataSize int, expectedLen int) {
194-
slice := testutil.RandomBytes(1, dataSize)
193+
// testRuns[i] and expectedLen[i] are dataSize and expected length respectively
194+
testRuns := []int{1024, 8192, 16000, 30000, 1000000}
195+
expectedLens := []int{1, 3, 5, 9, 248}
196+
for i, r := range testRuns {
197+
slice := testutil.RandomBytes(1, r)
195198

196199
addrs, err := fileStore.GetAllReferences(context.Background(), bytes.NewReader(slice), false)
197200
if err != nil {
198201
t.Fatal(err)
199202
}
200-
if len(addrs) != expectedLen {
201-
t.Fatalf("Expected reference array length to be %d, but is %d", expectedLen, len(addrs))
203+
if len(addrs) != expectedLens[i] {
204+
t.Fatalf("Expected reference array length to be %d, but is %d", expectedLens[i], len(addrs))
202205
}
203206
}
204-
205-
// testRuns[i] and expectedLen[i] are dataSize and expected length respectively
206-
testRuns := []int{1024, 8192, 16000, 30000, 1000000}
207-
expectedLens := []int{1, 3, 5, 9, 248}
208-
for i, r := range testRuns {
209-
checkRefs(r, expectedLens[i])
210-
}
211207
}

0 commit comments

Comments
 (0)