Skip to content

Commit

Permalink
fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
walldiss committed Jul 2, 2024
1 parent dd917de commit ac08071
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 68 deletions.
4 changes: 2 additions & 2 deletions share/new_eds/accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type AccessorStreamer interface {
}

type Streamer interface {
// Reader returns binary reader for the file (ODS) shares. It should read the shares from the
// Reader returns binary reader for the shares. It should read the shares from the
// ODS part of the square row by row.
Reader() (io.Reader, error)
io.Closer
Expand All @@ -45,6 +45,6 @@ type accessorStreamer struct {
Streamer
}

func WithStreamer(a Accessor, s Streamer) AccessorStreamer {
func AccessorAndStreamer(a Accessor, s Streamer) AccessorStreamer {
return &accessorStreamer{a, s}
}
2 changes: 1 addition & 1 deletion share/new_eds/proofs_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (c *proofsCache) getShare(rowIdx, colIdx int) ([]byte, error) {
// if share is from the same side of axis return share right away
if colIdx > odsSize == half.IsParity {
if half.IsParity {
colIdx = colIdx - odsSize
colIdx -= odsSize
}
return half.Shares[colIdx], nil
}
Expand Down
2 changes: 1 addition & 1 deletion share/new_eds/proofs_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestCache(t *testing.T) {
ODSSize := 8
ODSSize := 64
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
t.Cleanup(cancel)

Expand Down
22 changes: 11 additions & 11 deletions share/new_eds/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@ import (
"github.com/celestiaorg/celestia-node/share"
)

func NewSharesReader(odsSize int, getShare func(rowIdx, colIdx int) ([]byte, error)) *BufferedReader {
return &BufferedReader{
getShare: getShare,
buf: bytes.NewBuffer(nil),
odsSize: odsSize,
total: odsSize * odsSize,
}
}

// BufferedReader will read Shares from inMemOds into the buffer.
// BufferedReader will read Shares from getShare function into the buffer.
// It exposes the buffer to be read by io.Reader interface implementation
type BufferedReader struct {
buf *bytes.Buffer
Expand All @@ -28,6 +19,15 @@ type BufferedReader struct {
current, odsSize, total int
}

func NewSharesReader(odsSize int, getShare func(rowIdx, colIdx int) ([]byte, error)) *BufferedReader {
return &BufferedReader{
getShare: getShare,
buf: bytes.NewBuffer(nil),
odsSize: odsSize,
total: odsSize * odsSize,
}
}

func (r *BufferedReader) Read(p []byte) (int, error) {
if r.current >= r.total && r.buf.Len() == 0 {
return 0, io.EOF
Expand All @@ -49,7 +49,7 @@ func (r *BufferedReader) Read(p []byte) (int, error) {
rowIdx, colIdx := r.current/r.odsSize, r.current%r.odsSize
share, err := r.getShare(rowIdx, colIdx)
if err != nil {
return 0, fmt.Errorf("get share; %w", err)
return 0, fmt.Errorf("get share: %w", err)
}

// copy share to provided buffer
Expand Down
3 changes: 1 addition & 2 deletions share/new_eds/rsmt2d.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ func (eds *Rsmt2D) Reader() (io.Reader, error) {
return reader, nil
}

// Rsmt2DFromShares reads shares from the provided reader and constructs an Extended Data Square.
// Provided reader should contain shares in row-major order.
// Rsmt2DFromShares constructs an Extended Data Square from shares.
func Rsmt2DFromShares(shares []share.Share, odsSize int) (Rsmt2D, error) {
treeFn := wrapper.NewConstructor(uint64(odsSize))
eds, err := rsmt2d.ComputeExtendedDataSquare(shares, share.DefaultRSMT2DCodec(), treeFn)
Expand Down
2 changes: 1 addition & 1 deletion share/new_eds/rsmt2d_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestRsmt2dAccessor(t *testing.T) {
odsSize := 8
odsSize := 64
newAccessor := func(tb testing.TB, eds *rsmt2d.ExtendedDataSquare) Accessor {
return &Rsmt2D{ExtendedDataSquare: eds}
}
Expand Down
46 changes: 30 additions & 16 deletions share/new_eds/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,29 @@ func TestSuiteAccessor(
ctx context.Context,
t *testing.T,
createAccessor createAccessor,
odsSize int,
maxSize int,
) {
t.Run("Sample", func(t *testing.T) {
testAccessorSample(ctx, t, createAccessor, odsSize)
})

t.Run("AxisHalf", func(t *testing.T) {
testAccessorAxisHalf(ctx, t, createAccessor, odsSize)
})

t.Run("RowNamespaceData", func(t *testing.T) {
testAccessorRowNamespaceData(ctx, t, createAccessor, odsSize)
})

t.Run("Shares", func(t *testing.T) {
testAccessorShares(ctx, t, createAccessor, odsSize)
})
minSize := 2
if !checkPowerOfTwo(maxSize) {
t.Errorf("minSize must be power of 2: %v", maxSize)
}
for size := minSize; size <= maxSize; size *= 2 {
t.Run(fmt.Sprintf("Sample:%d", size), func(t *testing.T) {
testAccessorSample(ctx, t, createAccessor, size)
})

t.Run(fmt.Sprintf("AxisHalf:%d", size), func(t *testing.T) {
testAccessorAxisHalf(ctx, t, createAccessor, size)
})

t.Run(fmt.Sprintf("RowNamespaceData:%d", size), func(t *testing.T) {
testAccessorRowNamespaceData(ctx, t, createAccessor, size)
})

t.Run(fmt.Sprintf("Shares:%d", size), func(t *testing.T) {
testAccessorShares(ctx, t, createAccessor, size)
})
}
}

func TestStreamer(
Expand Down Expand Up @@ -350,3 +356,11 @@ func (q quadrant) coordinates(edsSize int) (rowIdx, colIdx int) {
rowIdx = edsSize/2*(int(q-1)/2) + 1
return rowIdx, colIdx
}

func checkPowerOfTwo(n int) bool {
// added one corner case if n is zero it will also consider as power 2
if n == 0 {
return true
}
return n&(n-1) == 0
}
6 changes: 3 additions & 3 deletions share/new_eds/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestValidation_Sample(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
randEDS := edstest.RandEDS(t, tt.odsSize)
accessor := &Rsmt2D{ExtendedDataSquare: randEDS}
validation := WithValidation(WithStreamer(accessor, nil))
validation := WithValidation(AccessorAndStreamer(accessor, nil))

_, err := validation.Sample(context.Background(), tt.rowIdx, tt.colIdx)
if tt.expectFail {
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestValidation_AxisHalf(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
randEDS := edstest.RandEDS(t, tt.odsSize)
accessor := &Rsmt2D{ExtendedDataSquare: randEDS}
validation := WithValidation(WithStreamer(accessor, nil))
validation := WithValidation(AccessorAndStreamer(accessor, nil))

_, err := validation.AxisHalf(context.Background(), tt.axisType, tt.axisIdx)
if tt.expectFail {
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestValidation_RowNamespaceData(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
randEDS := edstest.RandEDS(t, tt.odsSize)
accessor := &Rsmt2D{ExtendedDataSquare: randEDS}
validation := WithValidation(WithStreamer(accessor, nil))
validation := WithValidation(AccessorAndStreamer(accessor, nil))

ns := sharetest.RandV0Namespace()
_, err := validation.RowNamespaceData(context.Background(), ns, tt.rowIdx)
Expand Down
4 changes: 2 additions & 2 deletions share/shwap/p2p/bitswap/block_fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bitswap

import (
"context"
"math/rand/v2"
"math/rand"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestFetch_Duplicates(t *testing.T) {

wg.Add(1)
go func(i int) {
rint := rand.IntN(10)
rint := rand.Intn(10)
// this sleep ensures fetches aren't started simultaneously, allowing to check for edge-cases
time.Sleep(time.Millisecond * time.Duration(rint))

Expand Down
34 changes: 7 additions & 27 deletions store/file/ods.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ func (f *ODSFile) Reader() (io.Reader, error) {
return ods.reader()
}

offset := f.hdr.Size()
reader := newFileReader(f.fl, offset, int(f.hdr.shareSize), f.size())
offset := int64(f.hdr.Size())
total := int64(f.hdr.shareSize) * int64(f.size()*f.size()/4)
reader := io.NewSectionReader(f.fl, offset, total)
return reader, nil
}

Expand Down Expand Up @@ -233,11 +234,11 @@ func (f *ODSFile) readAxisHalf(axisType rsmt2d.Axis, axisIdx int) (eds.AxisHalf,

func (f *ODSFile) readODS() (square, error) {
f.lock.RLock()
if f.ods != nil {
f.lock.RUnlock()
return f.ods, nil
}
ods := f.ods
f.lock.RUnlock()
if ods != nil {
return ods, nil
}

// reset file pointer to the beginning of the file shares data
_, err := f.fl.Seek(int64(f.hdr.Size()), io.SeekStart)
Expand Down Expand Up @@ -306,24 +307,3 @@ func (f *ODSFile) axis(ctx context.Context, axisType rsmt2d.Axis, axisIdx int) (

return half.Extended()
}

type fileReader struct {
r io.ReaderAt
current, total int64
}

func newFileReader(r io.ReaderAt, offset, shareSize, odsSize int) *fileReader {
return &fileReader{
r: r,
current: int64(offset),
total: int64(odsSize*odsSize*shareSize + offset),
}
}

func (w *fileReader) Read(p []byte) (int, error) {
if w.current >= w.total {
return 0, io.EOF
}

return w.r.ReadAt(p, w.current)
}
2 changes: 1 addition & 1 deletion store/file/ods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestODSFile(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
t.Cleanup(cancel)

ODSSize := 8
ODSSize := 64
eds.TestSuiteAccessor(ctx, t, createAccessor, ODSSize)
eds.TestStreamer(ctx, t, createCachedStreamer, ODSSize)
eds.TestStreamer(ctx, t, createStreamer, ODSSize)
Expand Down
2 changes: 1 addition & 1 deletion store/file/q1q4_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestQ1Q4File(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
t.Cleanup(cancel)

ODSSize := 8
ODSSize := 64
eds.TestSuiteAccessor(ctx, t, createQ1Q4File, ODSSize)
}

Expand Down

0 comments on commit ac08071

Please sign in to comment.