1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-30 01:52:26 +08:00

blockstore: add fetch rehashing

License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
Jakub Sztandera
2016-06-24 21:31:58 +02:00
parent ce8c8a7a71
commit 989e91b1c4

View File

@ -22,7 +22,8 @@ var log = logging.Logger("blockstore")
// BlockPrefix namespaces blockstore datastores
var BlockPrefix = ds.NewKey("blocks")
var ValueTypeMismatch = errors.New("The retrieved value is not a Block")
var ValueTypeMismatch = errors.New("the retrieved value is not a Block")
var ErrHashMismatch = errors.New("block in storage has different hash than requested")
var ErrNotFound = errors.New("blockstore: block not found")
@ -71,6 +72,12 @@ type blockstore struct {
lk sync.RWMutex
gcreq int32
gcreqlk sync.Mutex
rehash bool
}
func (bs *blockstore) RuntimeHashing(enabled bool) {
bs.rehash = enabled
}
func (bs *blockstore) Get(k key.Key) (blocks.Block, error) {
@ -90,7 +97,16 @@ func (bs *blockstore) Get(k key.Key) (blocks.Block, error) {
return nil, ValueTypeMismatch
}
return blocks.NewBlockWithHash(bdata, mh.Multihash(k))
if bs.rehash {
rb := blocks.NewBlock(bdata)
if rb.Key() != k {
return nil, ErrHashMismatch
} else {
return rb, nil
}
} else {
return blocks.NewBlockWithHash(bdata, mh.Multihash(k))
}
}
func (bs *blockstore) Put(block blocks.Block) error {