Skip to content

Commit

Permalink
refactor chache into pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
walldiss committed Aug 31, 2023
1 parent 72f63d7 commit 458665d
Show file tree
Hide file tree
Showing 17 changed files with 722 additions and 346 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ openrpc-gen:
lint-imports:
@echo "--> Running imports linter"
@for file in `find . -type f -name '*.go'`; \
do goimports-reviser -list-diff -set-exit-status -company-prefixes "github.com/celestiaorg" -project-name "github.com/celestiaorg/celestia-node" -output stdout $$file \
do goimports-reviser -list-diff -set-exit-status -company-prefixes "github.com/celestiaorg" -project-name "github.com/celestiaorg/"$(PROJECTNAME)"" -output stdout $$file \
|| exit 1; \
done;
.PHONY: lint-imports

## sort-imports: Sort Go imports.
sort-imports:
@goimports-reviser -company-prefixes "github.com/celestiaorg" -project-name "github.com/celestiaorg/celestia-node" -output stdout .
@goimports-reviser -company-prefixes "github.com/celestiaorg" -project-name "github.com/celestiaorg/"$(PROJECTNAME)"" -output stdout .
.PHONY: sort-imports

## adr-gen: Generate ADR from template. Must set NUM and TITLE parameters.
Expand Down
1 change: 1 addition & 0 deletions nodebuilder/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func TestStoreRestart(t *testing.T) {
require.NoError(t, err)
_, err = eds.ReadEDS(ctx, odsReader, h)
require.NoError(t, err)
require.NoError(t, edsReader.Close())
}
}

Expand Down
304 changes: 0 additions & 304 deletions share/eds/accessor_cache.go

This file was deleted.

16 changes: 10 additions & 6 deletions share/eds/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/ipfs/go-datastore/namespace"
dshelp "github.com/ipfs/go-ipfs-ds-help"
ipld "github.com/ipfs/go-ipld-format"

"github.com/celestiaorg/celestia-node/share/eds/cache"
)

var _ bstore.Blockstore = (*blockstore)(nil)
Expand All @@ -32,12 +34,14 @@ var (
// implementation to allow for the blockstore operations to be routed to the underlying stores.
type blockstore struct {
store *Store
cache cache.Cache
ds datastore.Batching
}

func newBlockstore(store *Store, ds datastore.Batching) *blockstore {
func newBlockstore(store *Store, cache cache.Cache, ds datastore.Batching) *blockstore {
return &blockstore{
store: store,
cache: cache,
ds: namespace.Wrap(ds, blockstoreCacheKey),
}
}
Expand Down Expand Up @@ -146,18 +150,18 @@ func (bs *blockstore) getReadOnlyBlockstore(ctx context.Context, cid cid.Cid) (d
return nil, fmt.Errorf("failed to find shards containing multihash: %w", err)
}

// check hash for any of keys
// a share can exist in multiple EDSes, check cache to contain any of accessors containing shard
for _, k := range keys {
if accessor, err := bs.store.cache.get(k); err == nil {
return accessor.bs, nil
if accessor, err := bs.store.cache.Get(k); err == nil {
return accessor.Blockstore()
}
}

// a share can exist in multiple EDSes, so just take the first one.
shardKey := keys[0]
accessor, err := bs.store.cache.ipldRequestedBlocks.getOrLoad(ctx, shardKey, bs.store.getAccessor)
accessor, err := bs.cache.GetOrLoad(ctx, shardKey, bs.store.getAccessor)
if err != nil {
return nil, fmt.Errorf("failed to get accessor for shard %s: %w", shardKey, err)
}
return accessor.bs, nil
return accessor.Blockstore()
}
3 changes: 3 additions & 0 deletions share/eds/blockstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func TestBlockstore_Operations(t *testing.T) {
topLevelBS := edsStore.Blockstore()
carBS, err := edsStore.CARBlockstore(ctx, dah.Hash())
require.NoError(t, err)
defer func() {
require.NoError(t, carBS.Close())
}()

root, err := edsStore.GetDAH(ctx, dah.Hash())
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 458665d

Please sign in to comment.