From bdbdf6e2b7b2da6a4ccc30623cfce72bf85e8ea9 Mon Sep 17 00:00:00 2001 From: Wondertan Date: Sun, 16 Jul 2023 20:21:15 +0200 Subject: [PATCH] feat(share/eds): introduce List method for the Store --- share/eds/store.go | 15 +++++++++++++++ share/eds/store_test.go | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/share/eds/store.go b/share/eds/store.go index 0683df0d99..f3e3e1209e 100644 --- a/share/eds/store.go +++ b/share/eds/store.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "context" + "encoding/hex" "errors" "fmt" "io" @@ -403,6 +404,20 @@ func (s *Store) Has(ctx context.Context, root share.DataHash) (bool, error) { } } +// List lists all the registered EDSes. +func (s *Store) List() ([]share.DataHash, error) { + shards := s.dgstr.AllShardsInfo() + hashes := make([]share.DataHash, 0, len(shards)) + for shrd := range shards { + hash, err := hex.DecodeString(shrd.String()) + if err != nil { + return nil, err + } + hashes = append(hashes, hash) + } + return hashes, nil +} + func setupPath(basepath string) error { err := os.MkdirAll(basepath+blocksPath, os.ModePerm) if err != nil { diff --git a/share/eds/store_test.go b/share/eds/store_test.go index 491b63c48a..4b263e7062 100644 --- a/share/eds/store_test.go +++ b/share/eds/store_test.go @@ -154,6 +154,23 @@ func TestEDSStore(t *testing.T) { _, err = edsStore.cache.Get(shardKey) assert.NoError(t, err, errCacheMiss) }) + + t.Run("List", func(t *testing.T) { + const amount = 10 + hashes := make([]share.DataHash, 0, amount) + for range make([]byte, amount) { + eds, dah := randomEDS(t) + err = edsStore.Put(ctx, dah.Hash(), eds) + require.NoError(t, err) + hashes = append(hashes, dah.Hash()) + } + + hashesOut, err := edsStore.List() + require.NoError(t, err) + for _, hash := range hashes { + assert.Contains(t, hashesOut, hash) + } + }) } // TestEDSStore_GC verifies that unused transient shards are collected by the GC periodically.