Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: differentiate namespaced padded shares #1341

Merged
merged 4 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
rootulp marked this conversation as resolved.
Show resolved Hide resolved
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.ShareSize-appconsts.NamespaceSize-appconsts.ShareInfoBytes-appconsts.SequenceLenBytes)
rootulp marked this conversation as resolved.
Show resolved Hide resolved

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) {
rootulp marked this conversation as resolved.
Show resolved Hide resolved
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
rootulp marked this conversation as resolved.
Show resolved Hide resolved
}
6 changes: 4 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,7 +26,8 @@ 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)
Expand Down
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
}
evan-forbes marked this conversation as resolved.
Show resolved Hide resolved

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