Skip to content

Commit

Permalink
feat: make blockstore compatible with cid (#5446)
Browse files Browse the repository at this point in the history
from different codex
  • Loading branch information
LinZexiao authored Nov 3, 2022
1 parent 766a934 commit 908a4af
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
31 changes: 19 additions & 12 deletions venus-shared/blockstore/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,47 @@ func NewMemory() MemBlockstore {
}

// MemBlockstore is a terminal blockstore that keeps blocks in memory.
type MemBlockstore map[cid.Cid]blocks.Block
type MemBlockstore map[string]blocks.Block

func (m MemBlockstore) DeleteBlock(ctx context.Context, k cid.Cid) error {
delete(m, k)
func (m MemBlockstore) DeleteBlock(ctx context.Context, c cid.Cid) error {
delete(m, genKey(c))
return nil
}

func (m MemBlockstore) DeleteMany(ctx context.Context, ks []cid.Cid) error {
for _, k := range ks {
delete(m, k)
delete(m, genKey(k))
}
return nil
}

func (m MemBlockstore) Has(ctx context.Context, k cid.Cid) (bool, error) {
_, ok := m[k]
_, ok := m[genKey(k)]
return ok, nil
}

func (m MemBlockstore) View(ctx context.Context, k cid.Cid, callback func([]byte) error) error {
b, ok := m[k]
b, ok := m[genKey(k)]
if !ok {
return ipld.ErrNotFound{Cid: k}
}
return callback(b.RawData())
}

func (m MemBlockstore) Get(ctx context.Context, k cid.Cid) (blocks.Block, error) {
b, ok := m[k]
b, ok := m[genKey(k)]
if !ok {
return nil, ipld.ErrNotFound{Cid: k}
}
if b.Cid().Prefix().Codec != k.Prefix().Codec {
return blocks.NewBlockWithCid(b.RawData(), k)
}
return b, nil
}

// GetSize returns the CIDs mapped BlockSize
func (m MemBlockstore) GetSize(ctx context.Context, k cid.Cid) (int, error) {
b, ok := m[k]
b, ok := m[genKey(k)]
if !ok {
return 0, ipld.ErrNotFound{Cid: k}
}
Expand All @@ -65,13 +68,13 @@ func (m MemBlockstore) Put(ctx context.Context, b blocks.Block) error {
k := b.Cid()
if _, ok := b.(*blocks.BasicBlock); !ok {
// If we already have the block, abort.
if _, ok := m[k]; ok {
if _, ok := m[genKey(k)]; ok {
return nil
}
// the error is only for debugging.
b, _ = blocks.NewBlockWithCid(b.RawData(), b.Cid())
}
m[b.Cid()] = b
m[genKey(b.Cid())] = b
return nil
}

Expand All @@ -89,8 +92,8 @@ func (m MemBlockstore) PutMany(ctx context.Context, bs []blocks.Block) error {
// the given context, closing the channel if it becomes Done.
func (m MemBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
ch := make(chan cid.Cid, len(m))
for k := range m {
ch <- k
for _, b := range m {
ch <- b.Cid()
}
close(ch)
return ch, nil
Expand All @@ -101,3 +104,7 @@ func (m MemBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error)
func (m MemBlockstore) HashOnRead(enabled bool) {
// no-op
}

func genKey(cid cid.Cid) string {
return string(cid.Hash())
}
45 changes: 45 additions & 0 deletions venus-shared/blockstore/mem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package blockstore

import (
"context"
"testing"

blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"
)

func TestMemGetCodec(t *testing.T) {
ctx := context.Background()
bs := NewMemory()

cborArr := []byte{0x82, 1, 2}

h, err := mh.Sum(cborArr, mh.SHA2_256, -1)
require.NoError(t, err)

rawCid := cid.NewCidV1(cid.Raw, h)
rawBlk, err := blocks.NewBlockWithCid(cborArr, rawCid)
require.NoError(t, err)

err = bs.Put(ctx, rawBlk)
require.NoError(t, err)

cborCid := cid.NewCidV1(cid.DagCBOR, h)

cborBlk, err := bs.Get(ctx, cborCid)
require.NoError(t, err)

require.Equal(t, cborCid.Prefix(), cborBlk.Cid().Prefix())
require.EqualValues(t, cborArr, cborBlk.RawData())

// was allocated
require.NotEqual(t, cborBlk, rawBlk)

gotRawBlk, err := bs.Get(ctx, rawCid)
require.NoError(t, err)

// not allocated
require.Equal(t, rawBlk, gotRawBlk)
}

0 comments on commit 908a4af

Please sign in to comment.