@cryptotaxi247 / kubo / commits / 989e91b1c

blockstore: add fetch rehashing

License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>

Jakub Sztandera committed Jun 24, 2016 at 21:31 UTC 989e91b1c457e710adc05b5eedde4a3bc28bd83b
1 file changed +18 -2
blocks/blockstore/blockstore.go
+18 -2
@@ -22,7 +22,8 @@ var log = logging.Logger("blockstore")
22 // BlockPrefix namespaces blockstore datastores
23 var BlockPrefix = ds.NewKey("blocks")
24
25 -var ValueTypeMismatch = errors.New("The retrieved value is not a Block")
25 +var ValueTypeMismatch = errors.New("the retrieved value is not a Block")
26 +var ErrHashMismatch = errors.New("block in storage has different hash than requested")
27
28 var ErrNotFound = errors.New("blockstore: block not found")
29
@@ -71,6 +72,12 @@ type blockstore struct {
72 lk sync.RWMutex
73 gcreq int32
74 gcreqlk sync.Mutex
75 +
76 + rehash bool
77 +}
78 +
79 +func (bs *blockstore) RuntimeHashing(enabled bool) {
80 + bs.rehash = enabled
81 }
82
83 func (bs *blockstore) Get(k key.Key) (blocks.Block, error) {
@@ -90,7 +97,16 @@ func (bs *blockstore) Get(k key.Key) (blocks.Block, error) {
97 return nil, ValueTypeMismatch
98 }
99
93 - return blocks.NewBlockWithHash(bdata, mh.Multihash(k))
100 + if bs.rehash {
101 + rb := blocks.NewBlock(bdata)
102 + if rb.Key() != k {
103 + return nil, ErrHashMismatch
104 + } else {
105 + return rb, nil
106 + }
107 + } else {
108 + return blocks.NewBlockWithHash(bdata, mh.Multihash(k))
109 + }
110 }
111
112 func (bs *blockstore) Put(block blocks.Block) error {