Skip to content

Commit

Permalink
incorporate suggestions from reviewers
Browse files Browse the repository at this point in the history
  • Loading branch information
cmwaters committed Jun 12, 2024
1 parent 8bf0d21 commit baf553f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
17 changes: 13 additions & 4 deletions blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ func New(ns namespace.Namespace, blob []byte, shareVersion uint8) *Blob {

// Namespace returns the namespace of the blob
func (b *Blob) Namespace() (namespace.Namespace, error) {
return namespace.New(uint8(b.NamespaceVersion), b.NamespaceId)
return namespace.NewFromBytes(b.RawNamespace())
}

// Namespace returns the namespace of the blob
func (b *Blob) RawNamespace() []byte {
namespace := []byte{byte(b.NamespaceVersion)}
namespace = append(namespace, b.NamespaceId...)
return namespace
}

// Validate runs a stateless validity check on the form of the struct.
Expand All @@ -62,6 +69,10 @@ func (b *Blob) Validate() error {
return nil
}

func (b *Blob) Compare(other *Blob) int {
return bytes.Compare(b.RawNamespace(), other.RawNamespace())
}

// UnmarshalBlobTx attempts to unmarshal a transaction into blob transaction. If an
// error is thrown, false is returned.
func UnmarshalBlobTx(tx []byte) (*BlobTx, bool) {
Expand Down Expand Up @@ -102,9 +113,7 @@ func MarshalBlobTx(tx []byte, blobs ...*Blob) ([]byte, error) {
// Sort sorts the blobs by their namespace.
func Sort(blobs []*Blob) {
sort.SliceStable(blobs, func(i, j int) bool {
ns1 := append([]byte{byte(blobs[i].NamespaceVersion)}, blobs[i].NamespaceId...)
ns2 := append([]byte{byte(blobs[j].NamespaceVersion)}, blobs[j].NamespaceId...)
return bytes.Compare(ns1, ns2) < 0
return blobs[i].Compare(blobs[j]) < 0
})
}

Expand Down
8 changes: 2 additions & 6 deletions namespace/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,9 @@ var (
)

func primaryReservedNamespace(lastByte byte) Namespace {
return Namespace{
data: append([]byte{NamespaceVersionZero}, append(bytes.Repeat([]byte{0x00}, NamespaceIDSize-1), lastByte)...),
}
return newNamespace(NamespaceVersionZero, append(bytes.Repeat([]byte{0x00}, NamespaceIDSize-1), lastByte))
}

func secondaryReservedNamespace(lastByte byte) Namespace {
return Namespace{
data: append([]byte{NamespaceVersionMax}, append(bytes.Repeat([]byte{0xFF}, NamespaceIDSize-1), lastByte)...),
}
return newNamespace(NamespaceVersionMax, append(bytes.Repeat([]byte{0xFF}, NamespaceIDSize-1), lastByte))
}
39 changes: 29 additions & 10 deletions namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ func New(version uint8, id []byte) (Namespace, error) {
return Namespace{}, err
}

data := []byte{version}
data = append(data, id...)
return newNamespace(version, id), nil
}

func newNamespace(version uint8, id []byte) Namespace {
data := []byte{version}
return Namespace{
data: data,
}, nil
data: append(data, id...),
}
}

// MustNew returns a new namespace with the provided version and id. It panics
Expand All @@ -40,6 +42,27 @@ func MustNew(version uint8, id []byte) Namespace {
return ns
}

// NewFromBytes returns a new namespace from the provided byte slice.
func NewFromBytes(bytes []byte) (Namespace, error) {
if len(bytes) != NamespaceSize {
return Namespace{}, fmt.Errorf("invalid namespace length: %v must be %v", len(bytes), NamespaceSize)
}

err := validateVersionSupported(bytes[0])
if err != nil {
return Namespace{}, err
}

err = validateID(bytes[0], bytes[1:])
if err != nil {
return Namespace{}, err
}

return Namespace{
data: bytes,
}, nil
}

// NewV0 returns a new namespace with version 0 and the provided subID. subID
// must be <= 10 bytes. If subID is < 10 bytes, it will be left-padded with 0s
// to fill 10 bytes.
Expand Down Expand Up @@ -71,13 +94,9 @@ func MustNewV0(subID []byte) Namespace {
}

// From returns a namespace from the provided byte slice.
// Deprecated: Please use NewFromBytes instead.
func From(b []byte) (Namespace, error) {
if len(b) != NamespaceSize {
return Namespace{}, fmt.Errorf("invalid namespace length: %v must be %v", len(b), NamespaceSize)
}
rawVersion := b[0]
rawNamespace := b[1:]
return New(rawVersion, rawNamespace)
return NewFromBytes(b)
}

// Bytes returns this namespace as a byte slice.
Expand Down

0 comments on commit baf553f

Please sign in to comment.