From 043d3c593ff7a77736d9917ca1a29df3f5255d59 Mon Sep 17 00:00:00 2001 From: aron Date: Sat, 11 Feb 2017 21:55:06 +0100 Subject: [PATCH] cmd/swarm: added cleandb subcommand --- cmd/swarm/cleandb.go | 39 +++++++++++++++++++++++++++++++++++++++ cmd/swarm/main.go | 9 +++++++++ swarm/storage/dbstore.go | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 cmd/swarm/cleandb.go diff --git a/cmd/swarm/cleandb.go b/cmd/swarm/cleandb.go new file mode 100644 index 0000000000..81636ada5d --- /dev/null +++ b/cmd/swarm/cleandb.go @@ -0,0 +1,39 @@ +// 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 . + +package main + +import ( + "log" + + "github.com/ethereum/go-ethereum/swarm/storage" + "gopkg.in/urfave/cli.v1" +) + +func cleandb(ctx *cli.Context) { + args := ctx.Args() + if len(args) != 1 { + log.Fatal("need path to chunks database as the first and only argument") + } + + chunkDbPath := args[0] + hash := storage.MakeHashFunc("SHA3") + dbStore, err := storage.NewDbStore(chunkDbPath, hash, 10000000, 0) + if err != nil { + log.Fatalf("cannot initialise dbstore: %v", err) + } + dbStore.Cleanup() +} diff --git a/cmd/swarm/main.go b/cmd/swarm/main.go index 546c646f11..7d76d55c1f 100644 --- a/cmd/swarm/main.go +++ b/cmd/swarm/main.go @@ -191,6 +191,15 @@ Removes a path from the manifest }, }, }, + { + Action: cleandb, + Name: "cleandb", + Usage: "Cleans database of corrupted entries", + ArgsUsage: " ", + Description: ` +Cleans database of corrupted entries. +`, + }, } app.Flags = []cli.Flag{ diff --git a/swarm/storage/dbstore.go b/swarm/storage/dbstore.go index c34c415943..e320cd3279 100644 --- a/swarm/storage/dbstore.go +++ b/swarm/storage/dbstore.go @@ -261,6 +261,43 @@ func (s *DbStore) collectGarbage(ratio float32) { s.db.Put(keyGCPos, s.gcPos) } +func (s *DbStore) Cleanup() { + //Iterates over the database and checks that there are no faulty chunks + it := s.db.NewIterator() + startPosition := []byte{kpIndex} + it.Seek(startPosition) + var key []byte + var errorsFound, total int + for it.Valid() { + key = it.Key() + if (key == nil) || (key[0] != kpIndex) { + break + } + total++ + var index dpaDBIndex + decodeIndex(it.Value(), &index) + + data, err := s.db.Get(getDataKey(index.Idx)) + if err != nil { + glog.V(logger.Warn).Infof("Chunk %x found but could not be accessed: %v", key[:], err) + s.delete(index.Idx, getIndexKey(key[1:])) + errorsFound++ + } else { + hasher := s.hashfunc() + hasher.Write(data) + hash := hasher.Sum(nil) + if !bytes.Equal(hash, key[1:]) { + glog.V(logger.Warn).Infof("Found invalid chunk. Hash mismatch. hash=%x, key=%x", hash, key[:]) + s.delete(index.Idx, getIndexKey(key[1:])) + errorsFound++ + } + } + it.Next() + } + it.Release() + glog.V(logger.Warn).Infof("Found %v errors out of %v entries", errorsFound, total) +} + func (s *DbStore) delete(idx uint64, idxKey []byte) { batch := new(leveldb.Batch) batch.Delete(idxKey) @@ -363,9 +400,7 @@ func (s *DbStore) Get(key Key) (chunk *Chunk, err error) { hash := hasher.Sum(nil) if !bytes.Equal(hash, key) { s.delete(index.Idx, getIndexKey(key)) - err = fmt.Errorf("invalid chunk. hash=%x, key=%v", hash, key[:]) panic("Invalid Chunk in Database. Please repair with command: 'swarm cleandb'") - return } chunk = &Chunk{