diff options
author | Felix Lange <fjl@twurst.com> | 2017-01-09 18:16:06 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2017-01-09 23:24:42 +0800 |
commit | b9b3efb09f9281a5859646d2dcf36b5813132efb (patch) | |
tree | f9dc8f9d82108b33bec4669b09a99d06d24239a9 /swarm | |
parent | 0f34d506b5ae9b76de97318c906e56dddd5309f6 (diff) | |
download | dexon-b9b3efb09f9281a5859646d2dcf36b5813132efb.tar.gz dexon-b9b3efb09f9281a5859646d2dcf36b5813132efb.tar.zst dexon-b9b3efb09f9281a5859646d2dcf36b5813132efb.zip |
all: fix ineffectual assignments and remove uses of crypto.Sha3
go get github.com/gordonklaus/ineffassign
ineffassign .
Diffstat (limited to 'swarm')
-rw-r--r-- | swarm/api/api.go | 9 | ||||
-rw-r--r-- | swarm/api/config.go | 2 | ||||
-rw-r--r-- | swarm/api/filesystem.go | 38 | ||||
-rw-r--r-- | swarm/api/filesystem_test.go | 1 | ||||
-rw-r--r-- | swarm/network/kademlia/kademlia_test.go | 6 | ||||
-rw-r--r-- | swarm/network/syncdb_test.go | 10 | ||||
-rw-r--r-- | swarm/storage/common_test.go | 2 | ||||
-rw-r--r-- | swarm/storage/dbstore_test.go | 4 | ||||
-rw-r--r-- | swarm/storage/dpa_test.go | 3 |
9 files changed, 42 insertions, 33 deletions
diff --git a/swarm/api/api.go b/swarm/api/api.go index 673cff350..3f48437a5 100644 --- a/swarm/api/api.go +++ b/swarm/api/api.go @@ -140,8 +140,11 @@ func (self *Api) Put(content, contentType string) (string, error) { // to resolve path to content using dpa retrieve // it returns a section reader, mimeType, status and an error func (self *Api) Get(uri string, nameresolver bool) (reader storage.LazySectionReader, mimeType string, status int, err error) { - key, _, path, err := self.parseAndResolve(uri, nameresolver) + if err != nil { + return nil, "", 500, fmt.Errorf("can't resolve: %v", err) + } + quitC := make(chan bool) trie, err := loadManifest(self.dpa, key, quitC) if err != nil { @@ -166,6 +169,10 @@ func (self *Api) Get(uri string, nameresolver bool) (reader storage.LazySectionR func (self *Api) Modify(uri, contentHash, contentType string, nameresolver bool) (newRootHash string, err error) { root, _, path, err := self.parseAndResolve(uri, nameresolver) + if err != nil { + return "", fmt.Errorf("can't resolve: %v", err) + } + quitC := make(chan bool) trie, err := loadManifest(self.dpa, root, quitC) if err != nil { diff --git a/swarm/api/config.go b/swarm/api/config.go index 14a559c75..b4c6e3d4a 100644 --- a/swarm/api/config.go +++ b/swarm/api/config.go @@ -69,7 +69,7 @@ func NewConfig(path string, contract common.Address, prvKey *ecdsa.PrivateKey, n var data []byte pubkey := crypto.FromECDSAPub(&prvKey.PublicKey) pubkeyhex := common.ToHex(pubkey) - keyhex := crypto.Sha3Hash(pubkey).Hex() + keyhex := crypto.Keccak256Hash(pubkey).Hex() self = &Config{ SyncParams: network.NewSyncParams(dirpath), diff --git a/swarm/api/filesystem.go b/swarm/api/filesystem.go index 428f3e3ac..96aaf36df 100644 --- a/swarm/api/filesystem.go +++ b/swarm/api/filesystem.go @@ -241,24 +241,7 @@ func (self *FileSystem) Download(bzzpath, localpath string) error { } go func(i int, entry *downloadListEntry) { defer wg.Done() - f, err := os.Create(entry.path) // TODO: path separators - if err == nil { - - reader := self.api.dpa.Retrieve(entry.key) - writer := bufio.NewWriter(f) - size, err := reader.Size(quitC) - if err == nil { - _, err = io.CopyN(writer, reader, size) // TODO: handle errors - err2 := writer.Flush() - if err == nil { - err = err2 - } - err2 = f.Close() - if err == nil { - err = err2 - } - } - } + err := retrieveToFile(quitC, self.api.dpa, entry.key, entry.path) if err != nil { select { case errC <- err: @@ -279,5 +262,24 @@ func (self *FileSystem) Download(bzzpath, localpath string) error { case <-quitC: return fmt.Errorf("aborted") } +} +func retrieveToFile(quitC chan bool, dpa *storage.DPA, key storage.Key, path string) error { + f, err := os.Create(path) // TODO: path separators + if err != nil { + return err + } + reader := dpa.Retrieve(key) + writer := bufio.NewWriter(f) + size, err := reader.Size(quitC) + if err != nil { + return err + } + if _, err = io.CopyN(writer, reader, size); err != nil { + return err + } + if err := writer.Flush(); err != nil { + return err + } + return f.Close() } diff --git a/swarm/api/filesystem_test.go b/swarm/api/filesystem_test.go index f6657aede..4a27cb1da 100644 --- a/swarm/api/filesystem_test.go +++ b/swarm/api/filesystem_test.go @@ -130,6 +130,7 @@ func TestApiDirUploadModify(t *testing.T) { content = readPath(t, "testdata", "test0", "index.css") resp = testGet(t, api, bzzhash+"/index.css") exp = expResponse(content, "text/css", 0) + checkResponse(t, resp, exp) _, _, _, err = api.Get(bzzhash, true) if err == nil { diff --git a/swarm/network/kademlia/kademlia_test.go b/swarm/network/kademlia/kademlia_test.go index 66edfe654..417ccecae 100644 --- a/swarm/network/kademlia/kademlia_test.go +++ b/swarm/network/kademlia/kademlia_test.go @@ -58,9 +58,9 @@ func (n *testNode) LastActive() time.Time { } func TestOn(t *testing.T) { - addr, ok := gen(Address{}, quickrand).(Address) - other, ok := gen(Address{}, quickrand).(Address) - if !ok { + addr, ok1 := gen(Address{}, quickrand).(Address) + other, ok2 := gen(Address{}, quickrand).(Address) + if !ok1 || !ok2 { t.Errorf("oops") } kad := New(addr, NewKadParams()) diff --git a/swarm/network/syncdb_test.go b/swarm/network/syncdb_test.go index ed43fbd06..21453a110 100644 --- a/swarm/network/syncdb_test.go +++ b/swarm/network/syncdb_test.go @@ -63,7 +63,7 @@ func newTestSyncDb(priority, bufferSize, batchSize int, dbdir string, t *testing dbdir: dbdir, t: t, } - h := crypto.Sha3Hash([]byte{0}) + h := crypto.Keccak256Hash([]byte{0}) key := storage.Key(h[:]) self.syncDb = newSyncDb(db, key, uint(priority), uint(bufferSize), uint(batchSize), self.deliver) // kick off db iterator right away, if no items on db this will allow @@ -79,7 +79,7 @@ func (self *testSyncDb) close() { func (self *testSyncDb) push(n int) { for i := 0; i < n; i++ { - self.buffer <- storage.Key(crypto.Sha3([]byte{byte(self.c)})) + self.buffer <- storage.Key(crypto.Keccak256([]byte{byte(self.c)})) self.sent = append(self.sent, self.c) self.c++ } @@ -126,7 +126,7 @@ func (self *testSyncDb) expect(n int, db bool) { if self.at+1 > len(self.delivered) { self.t.Fatalf("expected %v, got %v", self.at+1, len(self.delivered)) } - if len(self.sent) > self.at && !bytes.Equal(crypto.Sha3([]byte{byte(self.sent[self.at])}), self.delivered[self.at]) { + if len(self.sent) > self.at && !bytes.Equal(crypto.Keccak256([]byte{byte(self.sent[self.at])}), self.delivered[self.at]) { self.t.Fatalf("expected delivery %v/%v/%v to be hash of %v, from db: %v = %v", i, n, self.at, self.sent[self.at], ok, db) glog.V(logger.Debug).Infof("%v/%v/%v to be hash of %v, from db: %v = %v", i, n, self.at, self.sent[self.at], ok, db) } @@ -195,7 +195,7 @@ func TestSaveSyncDb(t *testing.T) { go s.dbRead(false, 0, s.deliver) s.expect(amount, true) for i, key := range s.delivered { - expKey := crypto.Sha3([]byte{byte(i)}) + expKey := crypto.Keccak256([]byte{byte(i)}) if !bytes.Equal(key, expKey) { t.Fatalf("delivery %v expected to be key %x, got %x", i, expKey, key) } @@ -204,7 +204,7 @@ func TestSaveSyncDb(t *testing.T) { s.expect(amount, false) for i := amount; i < 2*amount; i++ { key := s.delivered[i] - expKey := crypto.Sha3([]byte{byte(i - amount)}) + expKey := crypto.Keccak256([]byte{byte(i - amount)}) if !bytes.Equal(key, expKey) { t.Fatalf("delivery %v expected to be key %x, got %x", i, expKey, key) } diff --git a/swarm/storage/common_test.go b/swarm/storage/common_test.go index 889b28a70..2a83f471d 100644 --- a/swarm/storage/common_test.go +++ b/swarm/storage/common_test.go @@ -80,7 +80,7 @@ func testStore(m ChunkStore, l int64, branches int64, t *testing.T) { Hash: defaultHash, }) swg := &sync.WaitGroup{} - key, err := chunker.Split(rand.Reader, l, chunkC, swg, nil) + key, _ := chunker.Split(rand.Reader, l, chunkC, swg, nil) swg.Wait() close(chunkC) chunkC = make(chan *Chunk) diff --git a/swarm/storage/dbstore_test.go b/swarm/storage/dbstore_test.go index e2f36a6bc..3d2b5bc36 100644 --- a/swarm/storage/dbstore_test.go +++ b/swarm/storage/dbstore_test.go @@ -144,7 +144,7 @@ func TestDbStoreSyncIterator(t *testing.T) { t.Fatalf("unexpected error creating NewSyncIterator") } - it, err = m.NewSyncIterator(DbSyncState{ + it, _ = m.NewSyncIterator(DbSyncState{ Start: Key(common.Hex2Bytes("1000000000000000000000000000000000000000000000000000000000000000")), Stop: Key(common.Hex2Bytes("4000000000000000000000000000000000000000000000000000000000000000")), First: 2, @@ -168,7 +168,7 @@ func TestDbStoreSyncIterator(t *testing.T) { t.Fatalf("Expected %v chunk, got %v", keys[3], res[1]) } - it, err = m.NewSyncIterator(DbSyncState{ + it, _ = m.NewSyncIterator(DbSyncState{ Start: Key(common.Hex2Bytes("2000000000000000000000000000000000000000000000000000000000000000")), Stop: Key(common.Hex2Bytes("4000000000000000000000000000000000000000000000000000000000000000")), First: 2, diff --git a/swarm/storage/dpa_test.go b/swarm/storage/dpa_test.go index a68232407..a23b9efeb 100644 --- a/swarm/storage/dpa_test.go +++ b/swarm/storage/dpa_test.go @@ -120,8 +120,7 @@ func TestDPA_capacity(t *testing.T) { // check whether it is, indeed, empty dpa.ChunkStore = memStore resultReader = dpa.Retrieve(key) - n, err = resultReader.ReadAt(resultSlice, 0) - if err == nil { + if _, err = resultReader.ReadAt(resultSlice, 0); err == nil { t.Errorf("Was able to read %d bytes from an empty memStore.", len(slice)) } // check how it works with localStore |