Skip to content

Commit

Permalink
various text fixes and validation during unmarshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Jul 3, 2024
1 parent 5c198d7 commit 8b678e1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 46 deletions.
13 changes: 8 additions & 5 deletions share/shwap/eds_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ type EdsID struct {
Height uint64 // Height specifies the block height.
}

// NewEdsID creates a new EdsID using the given height and verifies it against the provided Root.
// It returns an error if the verification fails.
// NewEdsID creates a new EdsID using the given height.
func NewEdsID(height uint64) (EdsID, error) {
eid := EdsID{
Height: height,
Expand All @@ -36,10 +35,14 @@ func EdsIDFromBinary(data []byte) (EdsID, error) {
if len(data) != EdsIDSize {
return EdsID{}, fmt.Errorf("invalid EdsID data length: %d != %d", len(data), EdsIDSize)
}
rid := EdsID{
eid := EdsID{
Height: binary.BigEndian.Uint64(data),
}
return rid, nil
if err := eid.Validate(); err != nil {
return EdsID{}, fmt.Errorf("validating EdsID: %w", err)
}

return eid, nil
}

// MarshalBinary encodes an EdsID into its binary form, primarily for storage or network
Expand All @@ -53,7 +56,7 @@ func (eid EdsID) MarshalBinary() ([]byte, error) {
// It ensures that the EdsID is not constructed with a zero Height and that the root is not nil.
func (eid EdsID) Validate() error {
if eid.Height == 0 {
return fmt.Errorf("%w: Height: %d", ErrInvalidShwapID, eid.Height)
return fmt.Errorf("%w: Height == 0", ErrInvalidShwapID)
}
return nil
}
Expand Down
33 changes: 21 additions & 12 deletions share/shwap/row_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type RowID struct {
RowIndex int // RowIndex specifies the position of the row within the data square.
}

// NewRowID creates a new RowID with the specified block height, row index, and validates it
// against the provided eds size. It returns an error if the validation fails, ensuring the RowID
// NewRowID creates a new RowID with the specified block height, row index, and EDS size.
// It returns an error if the validation fails, ensuring the RowID
// conforms to expected constraints.
func NewRowID(height uint64, rowIdx, edsSize int) (RowID, error) {
rid := RowID{
Expand All @@ -26,7 +26,11 @@ func NewRowID(height uint64, rowIdx, edsSize int) (RowID, error) {
},
RowIndex: rowIdx,
}
return rid, rid.Verify(edsSize)
if err := rid.Verify(edsSize); err != nil {
return RowID{}, fmt.Errorf("verifying RowID: %w", err)
}

return rid, nil
}

// RowIDFromBinary decodes a RowID from its binary representation.
Expand All @@ -37,12 +41,18 @@ func RowIDFromBinary(data []byte) (RowID, error) {
}
eid, err := EdsIDFromBinary(data[:EdsIDSize])
if err != nil {
return RowID{}, fmt.Errorf("error decoding EdsID: %w", err)
return RowID{}, fmt.Errorf("decoding EdsID: %w", err)
}
return RowID{

rid := RowID{
EdsID: eid,
RowIndex: int(binary.BigEndian.Uint16(data[EdsIDSize:])),
}, nil
}
if err := rid.Validate(); err != nil {
return RowID{}, fmt.Errorf("validating RowID: %w", err)
}

return rid, nil
}

// MarshalBinary encodes the RowID into a binary form for storage or network transmission.
Expand All @@ -51,25 +61,24 @@ func (rid RowID) MarshalBinary() ([]byte, error) {
return rid.appendTo(data), nil
}

// Verify ensures the RowID's fields are valid given the specified root structure, particularly
// that the row index is within bounds.
// Verify validates the RowID fields and verifies that RowIndex is within the bounds of
// the square size
func (rid RowID) Verify(edsSize int) error {
if edsSize == 0 {
return fmt.Errorf("provided EDS size is zero")
}

if rid.RowIndex >= edsSize {
return fmt.Errorf("RowIndex: %w: %d >= %d", ErrOutOfBounds, rid.RowIndex, edsSize)
return fmt.Errorf("%w, RowIndex: %d >= %d", ErrOutOfBounds, rid.RowIndex, edsSize)
}

return rid.Validate()
}

// Validate ensures the RowID's fields are valid given the specified root structure, particularly
// that the row index is within bounds.
// Validate performs basic field validation.
func (rid RowID) Validate() error {
if rid.RowIndex < 0 {
return fmt.Errorf("RowIndex: %w: %d", ErrInvalidShwapID, rid.RowIndex)
return fmt.Errorf("%w: RowIndex: %d < 0", ErrInvalidShwapID, rid.RowIndex)
}
return rid.EdsID.Validate()
}
Expand Down
33 changes: 15 additions & 18 deletions share/shwap/row_namespace_data_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type RowNamespaceDataID struct {
}

// NewRowNamespaceDataID creates a new RowNamespaceDataID with the specified parameters. It
// validates the RowNamespaceDataID against the provided Root before returning.
// validates the RowNamespaceDataID against the provided EDS size.
func NewRowNamespaceDataID(
height uint64,
rowIdx int,
Expand All @@ -36,7 +36,7 @@ func NewRowNamespaceDataID(
}

if err := did.Verify(edsSize); err != nil {
return RowNamespaceDataID{}, err
return RowNamespaceDataID{}, fmt.Errorf("verifying RowNamespaceDataID: %w", err)
}
return did, nil
}
Expand All @@ -51,19 +51,18 @@ func RowNamespaceDataIDFromBinary(data []byte) (RowNamespaceDataID, error) {

rid, err := RowIDFromBinary(data[:RowIDSize])
if err != nil {
return RowNamespaceDataID{}, fmt.Errorf("error unmarshaling RowID: %w", err)
return RowNamespaceDataID{}, fmt.Errorf("unmarshaling RowID: %w", err)
}

nsData := data[RowIDSize:]
ns := share.Namespace(nsData)
if err := ns.ValidateForData(); err != nil {
return RowNamespaceDataID{}, fmt.Errorf("error validating DataNamespace: %w", err)
rndid := RowNamespaceDataID{
RowID: rid,
DataNamespace: data[RowIDSize:],
}
if err := rndid.Validate(); err != nil {
return RowNamespaceDataID{}, fmt.Errorf("validating RowNamespaceDataID: %w", err)
}

return RowNamespaceDataID{
RowID: rid,
DataNamespace: ns,
}, nil
return rndid, nil
}

// MarshalBinary encodes RowNamespaceDataID into binary form.
Expand All @@ -75,24 +74,22 @@ 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.
// Verify validates the RowNamespaceDataID and verifies the embedded RowID.
func (s RowNamespaceDataID) Verify(edsSize int) error {
if err := s.RowID.Verify(edsSize); err != nil {
return fmt.Errorf("error validating RowID: %w", err)
return fmt.Errorf("error verifying RowID: %w", err)
}

return s.Validate()
}

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

return nil
Expand Down
27 changes: 16 additions & 11 deletions share/shwap/sample_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ type SampleID struct {
ShareIndex int // ShareIndex specifies the index of the sample within the row.
}

// NewSampleID constructs a new SampleID using the provided block height, sample index, and a root
// structure for validation. It calculates the row and share index based on the sample index and
// the length of the row roots.
// NewSampleID constructs a new SampleID using the provided block height, sample index, and EDS size.
// It calculates the row and share index based on the sample index and EDS size.
func NewSampleID(height uint64, rowIdx, colIdx, edsSize int) (SampleID, error) {
sid := SampleID{
RowID: RowID{
Expand All @@ -30,7 +29,7 @@ func NewSampleID(height uint64, rowIdx, colIdx, edsSize int) (SampleID, error) {
}

if err := sid.Verify(edsSize); err != nil {
return SampleID{}, err
return SampleID{}, fmt.Errorf("verifying SampleID: %w", err)
}
return sid, nil
}
Expand All @@ -44,13 +43,18 @@ func SampleIDFromBinary(data []byte) (SampleID, error) {

rid, err := RowIDFromBinary(data[:RowIDSize])
if err != nil {
return SampleID{}, fmt.Errorf("error decoding RowID: %w", err)
return SampleID{}, fmt.Errorf("decoding RowID: %w", err)
}

return SampleID{
sid := SampleID{
RowID: rid,
ShareIndex: int(binary.BigEndian.Uint16(data[RowIDSize:])),
}, nil
}
if err := sid.Validate(); err != nil {
return SampleID{}, fmt.Errorf("validating SampleID: %w", err)
}

return sid, nil
}

// MarshalBinary encodes SampleID into binary form.
Expand All @@ -62,21 +66,22 @@ func (sid SampleID) MarshalBinary() ([]byte, error) {
return sid.appendTo(data), nil
}

// Verify verifies the SampleID by ensuring the ShareIndex is within the bounds of
// Verify validates the SampleID and verifies that the ShareIndex is within the bounds of
// the square size.
func (sid SampleID) Verify(edsSize int) error {
if err := sid.RowID.Verify(edsSize); err != nil {
return err
return fmt.Errorf("verifying RowID: %w", err)
}
if sid.ShareIndex >= edsSize {
return fmt.Errorf("ShareIndex: %w: %d >= %d", ErrOutOfBounds, sid.ShareIndex, edsSize)
return fmt.Errorf("%w: ShareIndex: %d >= %d", ErrOutOfBounds, sid.ShareIndex, edsSize)
}
return sid.Validate()
}

// Validate performs basic field validation.
func (sid SampleID) Validate() error {
if sid.ShareIndex < 0 {
return fmt.Errorf("ShareIndex: %w: %d", ErrInvalidShwapID, sid.ShareIndex)
return fmt.Errorf("%w: ShareIndex: %d < 0", ErrInvalidShwapID, sid.ShareIndex)
}
return sid.RowID.Validate()
}
Expand Down

0 comments on commit 8b678e1

Please sign in to comment.