Skip to content

Commit

Permalink
refactor!: un-export namespace.ValidateBlobNamespace (#2145)
Browse files Browse the repository at this point in the history
Closes celestiaorg/celestia-app#2143

De-duplicate `ValidateBlobNamespace` and export it from `x/blob/types`
package. The namespace package needs to define its own `isBlobNamespace`
because it can't import `ValidateBlobNamespace` from `x/blob/types` as
that would introduce a circular dependency.
  • Loading branch information
rootulp authored Jul 27, 2023
1 parent 0191885 commit f81805a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
3 changes: 3 additions & 0 deletions pkg/namespace/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,7 @@ var (
Version: math.MaxUint8,
ID: bytes.Repeat([]byte{0xFF}, NamespaceIDSize),
}

// SupportedBlobNamespaceVersions is a list of namespace versions that can be specified by a user for blobs.
SupportedBlobNamespaceVersions = []uint8{NamespaceVersionZero}
)
23 changes: 3 additions & 20 deletions pkg/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Namespace struct {

// New returns a new namespace with the provided version and id.
func New(version uint8, id []byte) (Namespace, error) {
err := validateVersion(version)
err := validateVersionSupported(version)
if err != nil {
return Namespace{}, err
}
Expand Down Expand Up @@ -83,25 +83,8 @@ func (n Namespace) Bytes() []byte {
return append([]byte{n.Version}, n.ID...)
}

// ValidateBlobNamespace returns an error if this namespace is not a valid blob namespace.
func (n Namespace) ValidateBlobNamespace() error {
if n.IsReserved() {
return fmt.Errorf("invalid blob namespace: %v cannot use a reserved namespace ID, want > %v", n.Bytes(), MaxReservedNamespace.Bytes())
}

if n.IsParityShares() {
return fmt.Errorf("invalid blob namespace: %v cannot use parity shares namespace ID", n.Bytes())
}

if n.IsTailPadding() {
return fmt.Errorf("invalid blob namespace: %v cannot use tail padding namespace ID", n.Bytes())
}

return nil
}

// validateVersion returns an error if the version is not supported.
func validateVersion(version uint8) error {
// validateVersionSupported returns an error if the version is not supported.
func validateVersionSupported(version uint8) error {
if version != NamespaceVersionZero && version != NamespaceVersionMax {
return fmt.Errorf("unsupported namespace version %v", version)
}
Expand Down
29 changes: 25 additions & 4 deletions pkg/namespace/random_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package namespace

import (
tmrand "github.com/tendermint/tendermint/libs/rand"
"golang.org/x/exp/slices"
)

func RandomBlobNamespaceID() []byte {
Expand All @@ -22,11 +23,9 @@ func RandomBlobNamespaceWithPRG(prg *tmrand.Rand) Namespace {
for {
id := RandomBlobNamespaceIDWithPRG(prg)
namespace := MustNewV0(id)
err := namespace.ValidateBlobNamespace()
if err != nil {
continue
if isBlobNamespace(namespace) {
return namespace
}
return namespace
}
}

Expand All @@ -36,3 +35,25 @@ func RandomBlobNamespaces(rand *tmrand.Rand, count int) (namespaces []Namespace)
}
return namespaces
}

// isBlobNamespace returns an true if this namespace is a valid user-specifiable
// blob namespace.
func isBlobNamespace(ns Namespace) bool {
if ns.IsReserved() {
return false
}

if ns.IsParityShares() {
return false
}

if ns.IsTailPadding() {
return false
}

if !slices.Contains(SupportedBlobNamespaceVersions, ns.Version) {
return false
}

return true
}

0 comments on commit f81805a

Please sign in to comment.