mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 18:13:54 +08:00
blockstore: add fetch rehashing
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
@ -22,7 +22,8 @@ var log = logging.Logger("blockstore")
|
|||||||
// BlockPrefix namespaces blockstore datastores
|
// BlockPrefix namespaces blockstore datastores
|
||||||
var BlockPrefix = ds.NewKey("blocks")
|
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")
|
var ErrNotFound = errors.New("blockstore: block not found")
|
||||||
|
|
||||||
@ -71,6 +72,12 @@ type blockstore struct {
|
|||||||
lk sync.RWMutex
|
lk sync.RWMutex
|
||||||
gcreq int32
|
gcreq int32
|
||||||
gcreqlk sync.Mutex
|
gcreqlk sync.Mutex
|
||||||
|
|
||||||
|
rehash bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bs *blockstore) RuntimeHashing(enabled bool) {
|
||||||
|
bs.rehash = enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bs *blockstore) Get(k key.Key) (blocks.Block, error) {
|
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 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 {
|
func (bs *blockstore) Put(block blocks.Block) error {
|
||||||
|
Reference in New Issue
Block a user