Skip to content

Commit

Permalink
feat!: differentiate namespaced padded shares (#1341)
Browse files Browse the repository at this point in the history
Implements the proposal in
#783 (comment).

Closes #1136
Closes #783
Opens #1344

---------

Co-authored-by: Callum Waters <[email protected]>
  • Loading branch information
2 people authored and evan-forbes committed Feb 27, 2023
1 parent 5e7635e commit a4c7af0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
23 changes: 21 additions & 2 deletions pkg/shares/namespaced_padding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package shares

import (
"bytes"
"encoding/binary"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/nmt/namespace"
Expand All @@ -12,15 +13,20 @@ import (
// conforms to non-interactive default rules. The ns parameter provided should
// be the namespace of the blob that precedes this padding in the data square.
func NamespacedPaddedShare(ns namespace.ID) Share {
infoByte, err := NewInfoByte(appconsts.ShareVersionZero, false)
infoByte, err := NewInfoByte(appconsts.ShareVersionZero, true)
if err != nil {
panic(err)
}
padding := bytes.Repeat([]byte{0}, appconsts.ShareSize-appconsts.NamespaceSize-appconsts.ShareInfoBytes)

sequenceLen := make([]byte, appconsts.SequenceLenBytes)
binary.BigEndian.PutUint32(sequenceLen, uint32(0))

padding := bytes.Repeat([]byte{0}, appconsts.FirstSparseShareContentSize)

share := make([]byte, 0, appconsts.ShareSize)
share = append(share, ns...)
share = append(share, byte(infoByte))
share = append(share, sequenceLen...)
share = append(share, padding...)
return share
}
Expand All @@ -33,3 +39,16 @@ func NamespacedPaddedShares(ns namespace.ID, n int) []Share {
}
return shares
}

func IsNamespacedPadded(s Share) (bool, error) {
isSequenceStart, err := s.IsSequenceStart()
if err != nil {
return false, err
}
sequenceLen, err := s.SequenceLen()
if err != nil {
return false, err
}

return isSequenceStart && sequenceLen == 0, nil
}
54 changes: 52 additions & 2 deletions pkg/shares/namespaced_padding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ func TestNamespacedPaddedShare(t *testing.T) {

want, _ := zeroPadIfNecessary([]byte{
1, 1, 1, 1, 1, 1, 1, 1, // namespace ID
0x00, // info byte
1, // info byte
0, 0, 0, 0, // sequence len
}, appconsts.ShareSize)

got := NamespacedPaddedShare(namespaceOne).ToBytes()
Expand All @@ -25,11 +26,60 @@ func TestNamespacedPaddedShares(t *testing.T) {

want, _ := zeroPadIfNecessary([]byte{
1, 1, 1, 1, 1, 1, 1, 1, // namespace ID
0x00, // info byte
1, // info byte
0, 0, 0, 0, // sequence len
}, appconsts.ShareSize)

shares := NamespacedPaddedShares(namespaceOne, 2)
for _, share := range shares {
assert.Equal(t, want, share.ToBytes())
}
}

func TestIsNamespacedPadded(t *testing.T) {
type testCase struct {
name string
share Share
want bool
wantErr bool
}
emptyShare := Share{}
blobShare, _ := zeroPadIfNecessary([]byte{
1, 1, 1, 1, 1, 1, 1, 1, // namespace ID
1, // info byte
0, 0, 0, 1, // sequence len
0xff, // data
}, appconsts.ShareSize)

testCases := []testCase{
{
name: "namespaced padded share",
share: NamespacedPaddedShare(namespace.ID{1, 1, 1, 1, 1, 1, 1, 1}),
want: true,
},
{
name: "empty share",
share: emptyShare,
want: false,
wantErr: true,
},
{
name: "blob share",
share: blobShare,
want: false,
wantErr: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got, err := IsNamespacedPadded(tc.share)
if tc.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tc.want, got)
})
}
}
8 changes: 8 additions & 0 deletions pkg/shares/parse_sparse_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ func parseSparseShares(rawShares [][]byte, supportedShareVersions []uint8) (blob
return nil, fmt.Errorf("unsupported share version %v is not present in supported share versions %v", version, supportedShareVersions)
}

isNamespacedPadded, err := IsNamespacedPadded(share)
if err != nil {
return nil, err
}
if isNamespacedPadded {
continue
}

isStart, err := share.IsSequenceStart()
if err != nil {
return nil, err
Expand Down

0 comments on commit a4c7af0

Please sign in to comment.