Skip to content

Commit

Permalink
refactor!: un-export namespace.ValidateBlobNamespace (#2145)
Browse files Browse the repository at this point in the history
Closes #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 committed Aug 14, 2023
1 parent b76d74c commit 6f8ece4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 43 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
}
2 changes: 1 addition & 1 deletion x/blob/types/blob_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Blob = tmproto.Blob
// NewBlob creates a new coretypes.Blob from the provided data after performing
// basic stateless checks over it.
func NewBlob(ns appns.Namespace, blob []byte, shareVersion uint8) (*Blob, error) {
err := ns.ValidateBlobNamespace()
err := ValidateBlobNamespace(ns)
if err != nil {
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions x/blob/types/payforblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ func DefaultEstimateGas(blobSizes []uint32) uint64 {
return EstimateGas(blobSizes, appconsts.DefaultGasPerBlobByte, auth.DefaultTxSizeCostPerByte)
}

// ValidateBlobNamespace returns an error if the provided namespace is reserved,
// parity shares, or tail padding.
// ValidateBlobNamespace returns an error if the provided namespace is an
// invalid user-specifiable blob namespace (e.g. reserved, parity shares, or
// tail padding).
func ValidateBlobNamespace(ns appns.Namespace) error {
if ns.IsReserved() {
return ErrReservedNamespace.Wrapf("got namespace: %x, want: > %x", ns, appns.MaxReservedNamespace)
Expand All @@ -196,7 +197,7 @@ func ValidateBlobNamespace(ns appns.Namespace) error {
return ErrTailPaddingNamespace
}

if ns.Version != appns.NamespaceVersionZero {
if !slices.Contains(appns.SupportedBlobNamespaceVersions, ns.Version) {
return ErrInvalidNamespaceVersion
}

Expand Down Expand Up @@ -313,7 +314,7 @@ func ValidateBlobs(blobs ...*Blob) error {
if err != nil {
return err
}
err = ns.ValidateBlobNamespace()
err = ValidateBlobNamespace(ns)
if err != nil {
return err
}
Expand Down
28 changes: 14 additions & 14 deletions x/blob/types/payforblob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,23 @@ func TestValidateBasic(t *testing.T) {

validMsg := validMsgPayForBlobs(t)

// MsgPayForBlobs that uses parity shares namespace id
// MsgPayForBlobs that uses parity shares namespace
paritySharesMsg := validMsgPayForBlobs(t)
paritySharesMsg.Namespaces[0] = appns.ParitySharesNamespace.Bytes()

// MsgPayForBlobs that uses tail padding namespace id
// MsgPayForBlobs that uses tail padding namespace
tailPaddingMsg := validMsgPayForBlobs(t)
tailPaddingMsg.Namespaces[0] = appns.TailPaddingNamespace.Bytes()

// MsgPayForBlobs that uses transaction namespace id
// MsgPayForBlobs that uses transaction namespace
txNamespaceMsg := validMsgPayForBlobs(t)
txNamespaceMsg.Namespaces[0] = appns.TxNamespace.Bytes()

// MsgPayForBlobs that uses intermediateStateRoots namespace id
// MsgPayForBlobs that uses intermediateStateRoots namespace
intermediateStateRootsNamespaceMsg := validMsgPayForBlobs(t)
intermediateStateRootsNamespaceMsg.Namespaces[0] = appns.IntermediateStateRootsNamespace.Bytes()

// MsgPayForBlobs that uses the max reserved namespace id
// MsgPayForBlobs that uses the max reserved namespace
maxReservedNamespaceMsg := validMsgPayForBlobs(t)
maxReservedNamespaceMsg.Namespaces[0] = appns.MaxReservedNamespace.Bytes()

Expand All @@ -151,9 +151,9 @@ func TestValidateBasic(t *testing.T) {
invalidShareCommitmentSize := validMsgPayForBlobs(t)
invalidShareCommitmentSize.ShareCommitments[0] = bytes.Repeat([]byte{0x1}, 31)

// MsgPayForBlobs that has no namespace ids
noNamespaceIds := validMsgPayForBlobs(t)
noNamespaceIds.Namespaces = [][]byte{}
// MsgPayForBlobs that has no namespaces
noNamespaces := validMsgPayForBlobs(t)
noNamespaces.Namespaces = [][]byte{}

// MsgPayForBlobs that has no share versions
noShareVersions := validMsgPayForBlobs(t)
Expand All @@ -174,27 +174,27 @@ func TestValidateBasic(t *testing.T) {
wantErr: nil,
},
{
name: "parity shares namespace id",
name: "parity shares namespace",
msg: paritySharesMsg,
wantErr: ErrParitySharesNamespace,
},
{
name: "tail padding namespace id",
name: "tail padding namespace",
msg: tailPaddingMsg,
wantErr: ErrTailPaddingNamespace,
},
{
name: "transaction namspace namespace id",
name: "tx namespace",
msg: txNamespaceMsg,
wantErr: ErrReservedNamespace,
},
{
name: "intermediate state root namespace id",
name: "intermediate state root namespace",
msg: intermediateStateRootsNamespaceMsg,
wantErr: ErrReservedNamespace,
},
{
name: "max reserved namespace id",
name: "max reserved namespace",
msg: maxReservedNamespaceMsg,
wantErr: ErrReservedNamespace,
},
Expand All @@ -210,7 +210,7 @@ func TestValidateBasic(t *testing.T) {
},
{
name: "no namespace ids",
msg: noNamespaceIds,
msg: noNamespaces,
wantErr: ErrNoNamespaces,
},
{
Expand Down

0 comments on commit 6f8ece4

Please sign in to comment.