Skip to content

Commit

Permalink
feat(store): Add HasQ4ByHash method on store (#4035)
Browse files Browse the repository at this point in the history
  • Loading branch information
renaynay authored Jan 28, 2025
1 parent 5a5d450 commit 87bc6c8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestExchange_StoreHistoricIfArchival(t *testing.T) {
headers, err := ce.GetRangeByHeight(ctx, genHeader, 30)
require.NoError(t, err)

// ensure all "historic" EDSs were stored
// ensure all "historic" EDSs were stored but not the .q4 files
for _, h := range headers {
has, err := store.HasByHeight(ctx, h.Height())
require.NoError(t, err)
Expand All @@ -159,6 +159,11 @@ func TestExchange_StoreHistoricIfArchival(t *testing.T) {
has, err = store.HasByHash(ctx, h.DAH.Hash())
require.NoError(t, err)
assert.True(t, has)

// ensure .q4 file was not stored
has, err = store.HasQ4ByHash(ctx, h.DAH.Hash())
require.NoError(t, err)
assert.False(t, has)
}
}

Expand Down
5 changes: 5 additions & 0 deletions core/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ func TestListener_DoesNotStoreHistoric(t *testing.T) {
has, err := store.HasByHash(ctx, hash)
require.NoError(t, err)
assert.False(t, has)

// ensure .q4 file was not stored
has, err = store.HasQ4ByHash(ctx, hash)
require.NoError(t, err)
assert.False(t, has)
}
}

Expand Down
9 changes: 9 additions & 0 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,15 @@ func (s *Store) hasByHeight(height uint64) (bool, error) {
return exists(pathODS)
}

func (s *Store) HasQ4ByHash(_ context.Context, datahash share.DataHash) (bool, error) {
lock := s.stripLock.byHash(datahash)
lock.RLock()
defer lock.RUnlock()

pathQ4File := s.hashToPath(datahash, q4FileExt)
return exists(pathQ4File)
}

func (s *Store) RemoveODSQ4(ctx context.Context, height uint64, datahash share.DataHash) error {
lock := s.stripLock.byHashAndHeight(datahash, height)
lock.lock()
Expand Down
28 changes: 28 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

libshare "github.com/celestiaorg/go-square/v2/share"
Expand Down Expand Up @@ -268,6 +269,33 @@ func TestEDSStore(t *testing.T) {
})
}

t.Run("HasQ4", func(t *testing.T) {
dir := t.TempDir()
edsStore, err := NewStore(paramsNoCache(), dir)
require.NoError(t, err)

square, roots := randomEDS(t)
randHeight := uint64(8)

has, err := edsStore.HasQ4ByHash(ctx, roots.Hash())
require.NoError(t, err)
assert.False(t, has)

err = edsStore.PutODSQ4(ctx, roots, randHeight, square)
require.NoError(t, err)

has, err = edsStore.HasQ4ByHash(ctx, roots.Hash())
require.NoError(t, err)
assert.True(t, has)

err = edsStore.RemoveQ4(ctx, randHeight, roots.Hash())
require.NoError(t, err)

has, err = edsStore.HasQ4ByHash(ctx, roots.Hash())
require.NoError(t, err)
assert.False(t, has)
})

t.Run("Does not exist", func(t *testing.T) {
dir := t.TempDir()
edsStore, err := NewStore(paramsNoCache(), dir)
Expand Down

0 comments on commit 87bc6c8

Please sign in to comment.