Skip to content

Commit

Permalink
refactor: address namespace follow-ups from celestia-app v1.0.0-rc12 …
Browse files Browse the repository at this point in the history
…bump (#2597)

Addresses two follow-ups from
#2581
Closes #2561

(cherry picked from commit 1205437)
  • Loading branch information
rootulp authored and walldiss committed Sep 22, 2023
1 parent fdd5233 commit 1834813
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
25 changes: 17 additions & 8 deletions share/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ const NamespaceSize = appns.NamespaceSize

// Various reserved namespaces.
var (
MaxReservedNamespace = Namespace(appns.MaxReservedNamespace.Bytes())
ParitySharesNamespace = Namespace(appns.ParitySharesNamespace.Bytes())
TailPaddingNamespace = Namespace(appns.TailPaddingNamespace.Bytes())
ReservedPaddingNamespace = Namespace(appns.ReservedPaddingNamespace.Bytes())
TxNamespace = Namespace(appns.TxNamespace.Bytes())
PayForBlobNamespace = Namespace(appns.PayForBlobNamespace.Bytes())
ISRNamespace = Namespace(appns.IntermediateStateRootsNamespace.Bytes())
// MaxPrimaryReservedNamespace is the highest primary reserved namespace.
// Namespaces lower than this are reserved for protocol use.
MaxPrimaryReservedNamespace = Namespace(appns.MaxPrimaryReservedNamespace.Bytes())
// MinSecondaryReservedNamespace is the lowest secondary reserved namespace
// reserved for protocol use. Namespaces higher than this are reserved for
// protocol use.
MinSecondaryReservedNamespace = Namespace(appns.MinSecondaryReservedNamespace.Bytes())
ParitySharesNamespace = Namespace(appns.ParitySharesNamespace.Bytes())
TailPaddingNamespace = Namespace(appns.TailPaddingNamespace.Bytes())
PrimaryReservedPaddingNamespace = Namespace(appns.PrimaryReservedPaddingNamespace.Bytes())
TxNamespace = Namespace(appns.TxNamespace.Bytes())
PayForBlobNamespace = Namespace(appns.PayForBlobNamespace.Bytes())
ISRNamespace = Namespace(appns.IntermediateStateRootsNamespace.Bytes())
)

// Namespace represents namespace of a Share.
Expand Down Expand Up @@ -124,7 +130,10 @@ func (n Namespace) ValidateForBlob() error {
if err := n.ValidateForData(); err != nil {
return err
}
if bytes.Compare(n, MaxReservedNamespace) < 1 {
if bytes.Compare(n, MaxPrimaryReservedNamespace) < 1 {
return fmt.Errorf("invalid blob namespace(%s): reserved namespaces are forbidden", n)
}
if bytes.Compare(n, MinSecondaryReservedNamespace) > -1 {
return fmt.Errorf("invalid blob namespace(%s): reserved namespaces are forbidden", n)
}
return nil
Expand Down
26 changes: 26 additions & 0 deletions share/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ func TestValidateForBlob(t *testing.T) {
ns: append([]byte{appns.NamespaceVersionMax}, bytes.Repeat([]byte{0x0}, appns.NamespaceIDSize)...),
wantErr: true,
},
{
name: "invalid blob namespace: primary reserved namespace",
ns: primaryReservedNamespace(0x10),
wantErr: true,
},
{
name: "invalid blob namespace: secondary reserved namespace",
ns: secondaryReservedNamespace(0x10),
wantErr: true,
},
}

for _, tc := range testCases {
Expand All @@ -186,5 +196,21 @@ func TestValidateForBlob(t *testing.T) {
assert.NoError(t, err)
})
}
}

func primaryReservedNamespace(lastByte byte) Namespace {
result := make([]byte, NamespaceSize)
result = append(result, appns.NamespaceVersionZero)
result = append(result, appns.NamespaceVersionZeroPrefix...)
result = append(result, bytes.Repeat([]byte{0x0}, appns.NamespaceVersionZeroIDSize-1)...)
result = append(result, lastByte)
return result
}

func secondaryReservedNamespace(lastByte byte) Namespace {
result := make([]byte, NamespaceSize)
result = append(result, appns.NamespaceVersionMax)
result = append(result, bytes.Repeat([]byte{0xFF}, appns.NamespaceIDSize-1)...)
result = append(result, lastByte)
return result
}

0 comments on commit 1834813

Please sign in to comment.