Commit f49cb353 authored by Jakub Sztandera's avatar Jakub Sztandera

blockstore: add fetch rehashing

License: MIT
Signed-off-by: default avatarJakub Sztandera <kubuxu@protonmail.ch>
parent 34f3ab65
......@@ -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 {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment