Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.

Commit 8f266ca

Browse files
committed
feat: switch to raw multihashes for blocks
Part of: ipfs/kubo#6815
1 parent 5f9214c commit 8f266ca

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

arc_cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (b *arccache) hasCached(k cid.Cid) (has bool, size int, ok bool) {
5959
return false, -1, false
6060
}
6161

62-
h, ok := b.arc.Get(k.KeyString())
62+
h, ok := b.arc.Get(string(k.Hash()))
6363
if ok {
6464
b.hits.Inc()
6565
switch h := h.(type) {
@@ -160,11 +160,11 @@ func (b *arccache) HashOnRead(enabled bool) {
160160
}
161161

162162
func (b *arccache) cacheHave(c cid.Cid, have bool) {
163-
b.arc.Add(c.KeyString(), cacheHave(have))
163+
b.arc.Add(string(c.Hash()), cacheHave(have))
164164
}
165165

166166
func (b *arccache) cacheSize(c cid.Cid, blockSize int) {
167-
b.arc.Add(c.KeyString(), cacheSize(blockSize))
167+
b.arc.Add(string(c.Hash()), cacheSize(blockSize))
168168
}
169169

170170
func (b *arccache) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {

blockstore_test.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ func TestPutThenGetBlock(t *testing.T) {
5353
}
5454
}
5555

56+
func TestCidv0v1(t *testing.T) {
57+
bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
58+
block := blocks.NewBlock([]byte("some data"))
59+
60+
err := bs.Put(block)
61+
if err != nil {
62+
t.Fatal(err)
63+
}
64+
65+
blockFromBlockstore, err := bs.Get(cid.NewCidV1(cid.DagProtobuf, block.Cid().Hash()))
66+
if err != nil {
67+
t.Fatal(err)
68+
}
69+
if !bytes.Equal(block.RawData(), blockFromBlockstore.RawData()) {
70+
t.Fail()
71+
}
72+
}
73+
5674
func TestPutThenGetSizeBlock(t *testing.T) {
5775
bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
5876
block := blocks.NewBlock([]byte("some data"))
@@ -218,18 +236,19 @@ func TestAllKeysRespectsContext(t *testing.T) {
218236
}
219237

220238
func expectMatches(t *testing.T, expect, actual []cid.Cid) {
239+
t.Helper()
221240

222241
if len(expect) != len(actual) {
223242
t.Errorf("expect and actual differ: %d != %d", len(expect), len(actual))
224243
}
244+
245+
actualSet := make(map[string]bool, len(actual))
246+
for _, k := range actual {
247+
actualSet[string(k.Hash())] = true
248+
}
249+
225250
for _, ek := range expect {
226-
found := false
227-
for _, ak := range actual {
228-
if ek.Equals(ak) {
229-
found = true
230-
}
231-
}
232-
if !found {
251+
if !actualSet[string(ek.Hash())] {
233252
t.Error("expected key not found: ", ek)
234253
}
235254
}

bloom_cache.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (b *bloomcache) build(ctx context.Context) error {
103103
atomic.StoreInt32(&b.active, 1)
104104
return nil
105105
}
106-
b.bloom.AddTS(key.Bytes()) // Use binary key, the more compact the better
106+
b.bloom.AddTS(key.Hash()) // Use binary key, the more compact the better
107107
case <-ctx.Done():
108108
b.buildErr = ctx.Err()
109109
return b.buildErr
@@ -130,7 +130,7 @@ func (b *bloomcache) hasCached(k cid.Cid) (has bool, ok bool) {
130130
return false, false
131131
}
132132
if b.BloomActive() {
133-
blr := b.bloom.HasTS(k.Bytes())
133+
blr := b.bloom.HasTS(k.Hash())
134134
if !blr { // not contained in bloom is only conclusive answer bloom gives
135135
b.hits.Inc()
136136
return false, true
@@ -163,7 +163,7 @@ func (b *bloomcache) Put(bl blocks.Block) error {
163163
// See comment in PutMany
164164
err := b.blockstore.Put(bl)
165165
if err == nil {
166-
b.bloom.AddTS(bl.Cid().Bytes())
166+
b.bloom.AddTS(bl.Cid().Hash())
167167
}
168168
return err
169169
}
@@ -178,7 +178,7 @@ func (b *bloomcache) PutMany(bs []blocks.Block) error {
178178
return err
179179
}
180180
for _, bl := range bs {
181-
b.bloom.AddTS(bl.Cid().Bytes())
181+
b.bloom.AddTS(bl.Cid().Hash())
182182
}
183183
return nil
184184
}

0 commit comments

Comments
 (0)