check blobs when reading them

This commit is contained in:
Niko Storni 2021-04-05 23:34:45 +02:00
parent 7da49a4ccb
commit e37eeba0c9

View file

@ -1,6 +1,9 @@
package store
import (
"crypto/sha512"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"path"
@ -10,6 +13,7 @@ import (
"github.com/lbryio/lbry.go/v2/stream"
"github.com/lbryio/reflector.go/shared"
"github.com/lbryio/reflector.go/store/speedwalk"
log "github.com/sirupsen/logrus"
)
// DiskStore stores blobs on a local disk
@ -68,6 +72,17 @@ func (d *DiskStore) Get(hash string) (stream.Blob, shared.BlobTrace, error) {
}
return nil, shared.NewBlobTrace(time.Since(start), d.Name()), errors.Err(err)
}
hashBytes := sha512.Sum384(blob)
readHash := hex.EncodeToString(hashBytes[:])
if hash != readHash {
message := fmt.Sprintf("[%s] found a broken blob while reading from disk. Actual hash: %s", hash, readHash)
log.Errorf("%s", message)
err := d.Delete(hash)
if err != nil {
return nil, shared.NewBlobTrace(time.Since(start), d.Name()), err
}
return nil, shared.NewBlobTrace(time.Since(start), d.Name()), errors.Err(message)
}
return blob, shared.NewBlobTrace(time.Since(start), d.Name()), nil
}