Skip to content

Commit

Permalink
separate verify and validate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
walldiss committed Jul 3, 2024
1 parent ba26102 commit 5c198d7
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 98 deletions.
27 changes: 9 additions & 18 deletions share/new_eds/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,17 @@ func (f validation) Size(ctx context.Context) int {
}

func (f validation) Sample(ctx context.Context, rowIdx, colIdx int) (shwap.Sample, error) {
if err := validateIndexBounds(ctx, f, colIdx); err != nil {
return shwap.Sample{}, fmt.Errorf("col: %w", err)
}
if err := validateIndexBounds(ctx, f, rowIdx); err != nil {
return shwap.Sample{}, fmt.Errorf("row: %w", err)
_, err := shwap.NewSampleID(1, rowIdx, colIdx, f.Size(ctx))
if err != nil {
return shwap.Sample{}, fmt.Errorf("sample validation: %w", err)
}
return f.Accessor.Sample(ctx, rowIdx, colIdx)
}

func (f validation) AxisHalf(ctx context.Context, axisType rsmt2d.Axis, axisIdx int) (AxisHalf, error) {
if err := validateIndexBounds(ctx, f, axisIdx); err != nil {
return AxisHalf{}, fmt.Errorf("%s: %w", axisType, err)
_, err := shwap.NewRowID(1, axisIdx, f.Size(ctx))
if err != nil {
return AxisHalf{}, fmt.Errorf("axis half validation: %w", err)
}
return f.Accessor.AxisHalf(ctx, axisType, axisIdx)
}
Expand All @@ -60,17 +59,9 @@ func (f validation) RowNamespaceData(
namespace share.Namespace,
rowIdx int,
) (shwap.RowNamespaceData, error) {
if err := validateIndexBounds(ctx, f, rowIdx); err != nil {
return shwap.RowNamespaceData{}, fmt.Errorf("row: %w", err)
_, err := shwap.NewRowNamespaceDataID(1, rowIdx, namespace, f.Size(ctx))
if err != nil {
return shwap.RowNamespaceData{}, fmt.Errorf("row namespace data validation: %w", err)
}
return f.Accessor.RowNamespaceData(ctx, namespace, rowIdx)
}

// validateIndexBounds checks if the index is within the bounds of the eds.
func validateIndexBounds(ctx context.Context, f Accessor, idx int) error {
size := f.Size(ctx)
if idx < 0 || idx >= size {
return fmt.Errorf("%w: index %d is out of bounds: [0, %d)", ErrOutOfBounds, idx, size)
}
return nil
}
6 changes: 3 additions & 3 deletions share/new_eds/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestValidation_Sample(t *testing.T) {

_, err := validation.Sample(context.Background(), tt.rowIdx, tt.colIdx)
if tt.expectFail {
require.ErrorIs(t, err, ErrOutOfBounds)
require.ErrorIs(t, err, shwap.ErrInvalidShwapID)
} else {
require.NoError(t, err)
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestValidation_AxisHalf(t *testing.T) {

_, err := validation.AxisHalf(context.Background(), tt.axisType, tt.axisIdx)
if tt.expectFail {
require.ErrorIs(t, err, ErrOutOfBounds)
require.ErrorIs(t, err, shwap.ErrInvalidShwapID)
} else {
require.NoError(t, err)
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestValidation_RowNamespaceData(t *testing.T) {
ns := sharetest.RandV0Namespace()
_, err := validation.RowNamespaceData(context.Background(), ns, tt.rowIdx)
if tt.expectFail {
require.ErrorIs(t, err, ErrOutOfBounds)
require.ErrorIs(t, err, shwap.ErrInvalidShwapID)
} else {
require.True(t, err == nil || errors.Is(err, shwap.ErrNamespaceOutsideRange), err)
}
Expand Down
20 changes: 11 additions & 9 deletions share/shwap/eds_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ package shwap

import (
"encoding/binary"
"errors"
"fmt"

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

// EdsIDSize defines the byte size of the EdsID.
const EdsIDSize = 8

// ErrOutOfBounds is returned whenever an index is out of bounds.
var (
ErrInvalidShwapID = errors.New("invalid shwap ID")
ErrOutOfBounds = fmt.Errorf("index out of bounds: %w", ErrInvalidShwapID)
)

// EdsID represents a unique identifier for a row, using the height of the block
// to identify the data square in the chain.
type EdsID struct {
Expand All @@ -18,11 +23,11 @@ type EdsID struct {

// NewEdsID creates a new EdsID using the given height and verifies it against the provided Root.
// It returns an error if the verification fails.
func NewEdsID(height uint64, root *share.Root) (EdsID, error) {
func NewEdsID(height uint64) (EdsID, error) {
eid := EdsID{
Height: height,
}
return eid, eid.Validate(root)
return eid, eid.Validate()
}

// EdsIDFromBinary decodes a byte slice into an EdsID, validating the length of the data.
Expand All @@ -46,12 +51,9 @@ func (eid EdsID) MarshalBinary() ([]byte, error) {

// Validate checks the integrity of an EdsID's fields against the provided Root.
// It ensures that the EdsID is not constructed with a zero Height and that the root is not nil.
func (eid EdsID) Validate(root *share.Root) error {
if root == nil {
return fmt.Errorf("provided Root is nil")
}
func (eid EdsID) Validate() error {
if eid.Height == 0 {
return fmt.Errorf("height cannot be zero")
return fmt.Errorf("%w: Height: %d", ErrInvalidShwapID, eid.Height)
}
return nil
}
Expand Down
11 changes: 2 additions & 9 deletions share/shwap/eds_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@ import (

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

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

func TestEdsID(t *testing.T) {
square := edstest.RandEDS(t, 2)
root, err := share.NewRoot(square)
require.NoError(t, err)

id, err := NewEdsID(2, root)
id, err := NewEdsID(2)
require.NoError(t, err)

data, err := id.MarshalBinary()
Expand All @@ -25,6 +18,6 @@ func TestEdsID(t *testing.T) {
require.NoError(t, err)
assert.EqualValues(t, id, idOut)

err = idOut.Validate(root)
err = idOut.Validate()
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion share/shwap/p2p/bitswap/row_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type RowBlock struct {

// NewEmptyRowBlock constructs a new empty RowBlock.
func NewEmptyRowBlock(height uint64, rowIdx int, root *share.Root) (*RowBlock, error) {
id, err := shwap.NewRowID(height, rowIdx, root)
id, err := shwap.NewRowID(height, rowIdx, len(root.RowRoots))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion share/shwap/p2p/bitswap/row_namespace_data_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewEmptyRowNamespaceDataBlock(
namespace share.Namespace,
root *share.Root,
) (*RowNamespaceDataBlock, error) {
id, err := shwap.NewRowNamespaceDataID(height, rowIdx, namespace, root)
id, err := shwap.NewRowNamespaceDataID(height, rowIdx, namespace, len(root.RowRoots))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion share/shwap/p2p/bitswap/sample_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type SampleBlock struct {

// NewEmptySampleBlock constructs a new empty SampleBlock.
func NewEmptySampleBlock(height uint64, rowIdx, colIdx int, root *share.Root) (*SampleBlock, error) {
id, err := shwap.NewSampleID(height, rowIdx, colIdx, root)
id, err := shwap.NewSampleID(height, rowIdx, colIdx, len(root.RowRoots))
if err != nil {
return nil, err
}
Expand Down
33 changes: 18 additions & 15 deletions share/shwap/row_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package shwap
import (
"encoding/binary"
"fmt"

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

// RowIDSize defines the size in bytes of RowID, consisting of the size of EdsID and 2 bytes for
Expand All @@ -19,16 +17,16 @@ type RowID struct {
}

// NewRowID creates a new RowID with the specified block height, row index, and validates it
// against the provided Root. It returns an error if the validation fails, ensuring the RowID
// against the provided eds size. It returns an error if the validation fails, ensuring the RowID
// conforms to expected constraints.
func NewRowID(height uint64, rowIdx int, root *share.Root) (RowID, error) {
func NewRowID(height uint64, rowIdx, edsSize int) (RowID, error) {
rid := RowID{
EdsID: EdsID{
Height: height,
},
RowIndex: rowIdx,
}
return rid, rid.Validate(root)
return rid, rid.Verify(edsSize)
}

// RowIDFromBinary decodes a RowID from its binary representation.
Expand All @@ -53,22 +51,27 @@ func (rid RowID) MarshalBinary() ([]byte, error) {
return rid.appendTo(data), nil
}

// Validate ensures the RowID's fields are valid given the specified root structure, particularly
// Verify ensures the RowID's fields are valid given the specified root structure, particularly
// that the row index is within bounds.
func (rid RowID) Validate(root *share.Root) error {
if err := rid.EdsID.Validate(root); err != nil {
return err
func (rid RowID) Verify(edsSize int) error {
if edsSize == 0 {
return fmt.Errorf("provided EDS size is zero")
}

if root == nil || len(root.RowRoots) == 0 {
return fmt.Errorf("provided root is nil or empty")
if rid.RowIndex >= edsSize {
return fmt.Errorf("RowIndex: %w: %d >= %d", ErrOutOfBounds, rid.RowIndex, edsSize)
}

if rid.RowIndex >= len(root.RowRoots) {
return fmt.Errorf("RowIndex out of bounds: %d >= %d", rid.RowIndex, len(root.RowRoots))
}
return rid.Validate()
}

return nil
// Validate ensures the RowID's fields are valid given the specified root structure, particularly
// that the row index is within bounds.
func (rid RowID) Validate() error {
if rid.RowIndex < 0 {
return fmt.Errorf("RowIndex: %w: %d", ErrInvalidShwapID, rid.RowIndex)
}
return rid.EdsID.Validate()
}

// appendTo assists in binary encoding of RowID by appending the encoded fields to the given byte
Expand Down
11 changes: 3 additions & 8 deletions share/shwap/row_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ import (

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

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

func TestRowID(t *testing.T) {
square := edstest.RandEDS(t, 4)
root, err := share.NewRoot(square)
require.NoError(t, err)
edsSize := 4

id, err := NewRowID(2, 1, root)
id, err := NewRowID(2, 1, edsSize)
require.NoError(t, err)

data, err := id.MarshalBinary()
Expand All @@ -25,6 +20,6 @@ func TestRowID(t *testing.T) {
require.NoError(t, err)
assert.EqualValues(t, id, idOut)

err = idOut.Validate(root)
err = idOut.Verify(edsSize)
require.NoError(t, err)
}
20 changes: 15 additions & 5 deletions share/shwap/row_namespace_data_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewRowNamespaceDataID(
height uint64,
rowIdx int,
namespace share.Namespace,
root *share.Root,
edsSize int,
) (RowNamespaceDataID, error) {
did := RowNamespaceDataID{
RowID: RowID{
Expand All @@ -35,7 +35,7 @@ func NewRowNamespaceDataID(
DataNamespace: namespace,
}

if err := did.Validate(root); err != nil {
if err := did.Verify(edsSize); err != nil {
return RowNamespaceDataID{}, err
}
return did, nil
Expand Down Expand Up @@ -75,14 +75,24 @@ func (s RowNamespaceDataID) MarshalBinary() ([]byte, error) {
return s.appendTo(data), nil
}

// Verify checks the validity of RowNamespaceDataID's fields, including the RowID and the
// namespace.
func (s RowNamespaceDataID) Verify(edsSize int) error {
if err := s.RowID.Verify(edsSize); err != nil {
return fmt.Errorf("error validating RowID: %w", err)
}

return s.Validate()
}

// Validate checks the validity of RowNamespaceDataID's fields, including the RowID and the
// namespace.
func (s RowNamespaceDataID) Validate(root *share.Root) error {
if err := s.RowID.Validate(root); err != nil {
func (s RowNamespaceDataID) Validate() error {
if err := s.RowID.Validate(); err != nil {
return fmt.Errorf("error validating RowID: %w", err)
}
if err := s.DataNamespace.ValidateForData(); err != nil {
return fmt.Errorf("error validating DataNamespace: %w", err)
return fmt.Errorf("%w: error validating DataNamespace: %w", ErrInvalidShwapID, err)
}

return nil
Expand Down
9 changes: 4 additions & 5 deletions share/shwap/row_namespace_data_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

func TestDataID(t *testing.T) {
func TestRowNamespaceDataID(t *testing.T) {
edsSize := 4
ns := sharetest.RandV0Namespace()
_, root := edstest.RandEDSWithNamespace(t, ns, 8, 4)

id, err := NewRowNamespaceDataID(1, 1, ns, root)
id, err := NewRowNamespaceDataID(1, 1, ns, edsSize)
require.NoError(t, err)

data, err := id.MarshalBinary()
Expand All @@ -24,6 +23,6 @@ func TestDataID(t *testing.T) {
require.NoError(t, err)
assert.EqualValues(t, id, sidOut)

err = sidOut.Validate(root)
err = sidOut.Verify(edsSize)
require.NoError(t, err)
}
Loading

0 comments on commit 5c198d7

Please sign in to comment.