Skip to content

Commit

Permalink
refactor: use blob share version for padding shares (#2148)
Browse files Browse the repository at this point in the history
Closes #2142
  • Loading branch information
rootulp authored Jul 24, 2023
1 parent d3284fd commit 39b81f5
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 16 deletions.
21 changes: 11 additions & 10 deletions pkg/shares/padding.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (

// NamespacePaddingShare returns a share that acts as padding. Namespace padding
// shares follow a blob so that the next blob may start at an index that
// conforms to blob share commitment rules. The ns parameter provided should
// be the namespace of the blob that precedes this padding in the data square.
func NamespacePaddingShare(ns appns.Namespace) (Share, error) {
b, err := NewBuilder(ns, appconsts.ShareVersionZero, true).Init()
// conforms to blob share commitment rules. The ns and shareVersion parameters
// provided should be the namespace and shareVersion of the blob that precedes
// this padding in the data square.
func NamespacePaddingShare(ns appns.Namespace, shareVersion uint8) (Share, error) {
b, err := NewBuilder(ns, shareVersion, true).Init()
if err != nil {
return Share{}, err
}
Expand All @@ -32,14 +33,14 @@ func NamespacePaddingShare(ns appns.Namespace) (Share, error) {
}

// NamespacePaddingShares returns n namespace padding shares.
func NamespacePaddingShares(ns appns.Namespace, n int) ([]Share, error) {
func NamespacePaddingShares(ns appns.Namespace, shareVersion uint8, n int) ([]Share, error) {
var err error
if n < 0 {
return nil, errors.New("n must be positive")
}
shares := make([]Share, n)
for i := 0; i < n; i++ {
shares[i], err = NamespacePaddingShare(ns)
shares[i], err = NamespacePaddingShare(ns, shareVersion)
if err != nil {
return shares, err
}
Expand All @@ -52,7 +53,7 @@ func NamespacePaddingShares(ns appns.Namespace, n int) ([]Share, error) {
// first blob can start at an index that conforms to non-interactive default
// rules.
func ReservedPaddingShare() Share {
share, err := NamespacePaddingShare(appns.ReservedPaddingNamespace)
share, err := NamespacePaddingShare(appns.ReservedPaddingNamespace, appconsts.ShareVersionZero)
if err != nil {
panic(err)
}
Expand All @@ -61,7 +62,7 @@ func ReservedPaddingShare() Share {

// ReservedPaddingShare returns n reserved padding shares.
func ReservedPaddingShares(n int) []Share {
shares, err := NamespacePaddingShares(appns.ReservedPaddingNamespace, n)
shares, err := NamespacePaddingShares(appns.ReservedPaddingNamespace, appconsts.ShareVersionZero, n)
if err != nil {
panic(err)
}
Expand All @@ -72,7 +73,7 @@ func ReservedPaddingShares(n int) []Share {
// square size. Tail padding shares follow the last blob share in the data
// square.
func TailPaddingShare() Share {
share, err := NamespacePaddingShare(appns.TailPaddingNamespace)
share, err := NamespacePaddingShare(appns.TailPaddingNamespace, appconsts.ShareVersionZero)
if err != nil {
panic(err)
}
Expand All @@ -81,7 +82,7 @@ func TailPaddingShare() Share {

// TailPaddingShares returns n tail padding shares.
func TailPaddingShares(n int) []Share {
shares, err := NamespacePaddingShares(appns.TailPaddingNamespace, n)
shares, err := NamespacePaddingShares(appns.TailPaddingNamespace, appconsts.ShareVersionZero, n)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/shares/padding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ var tailPadding, _ = zeroPadIfNecessary(
), appconsts.ShareSize)

func TestNamespacePaddingShare(t *testing.T) {
got, err := NamespacePaddingShare(ns1)
got, err := NamespacePaddingShare(ns1, appconsts.ShareVersionZero)
assert.NoError(t, err)
assert.Equal(t, nsOnePadding, got.ToBytes())
}

func TestNamespacePaddingShares(t *testing.T) {
shares, err := NamespacePaddingShares(ns1, 2)
shares, err := NamespacePaddingShares(ns1, appconsts.ShareVersionZero, 2)
assert.NoError(t, err)
for _, share := range shares {
assert.Equal(t, nsOnePadding, share.ToBytes())
Expand Down
2 changes: 1 addition & 1 deletion pkg/shares/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestParseShares(t *testing.T) {
// because it takes more than one share to store a sequence of 1000 bytes
tooLargeSequenceLen := generateRawShare(t, ns1, true, uint32(1000))

ns1Padding, err := NamespacePaddingShare(ns1)
ns1Padding, err := NamespacePaddingShare(ns1, appconsts.ShareVersionZero)
require.NoError(t, err)

type testCase struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/shares/share_sequence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func Test_validSequenceLen(t *testing.T) {
}

ns1 := appns.MustNewV0(bytes.Repeat([]byte{0x1}, appns.NamespaceVersionZeroIDSize))
share, err := NamespacePaddingShare(ns1)
share, err := NamespacePaddingShare(ns1, appconsts.ShareVersionZero)
require.NoError(t, err)
namespacePadding := ShareSequence{
Namespace: ns1,
Expand Down
2 changes: 1 addition & 1 deletion pkg/shares/shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func TestIsPadding(t *testing.T) {
),
appconsts.ShareSize)

nsPadding, err := NamespacePaddingShare(ns1)
nsPadding, err := NamespacePaddingShare(ns1, appconsts.ShareVersionZero)
require.NoError(t, err)

testCases := []testCase{
Expand Down
6 changes: 5 additions & 1 deletion pkg/shares/split_sparse_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ func (sss *SparseShareSplitter) WriteNamespacePaddingShares(count int) error {
if err != nil {
return err
}
nsPaddingShares, err := NamespacePaddingShares(lastBlobNs, count)
lastBlobInfo, err := lastBlob.InfoByte()
if err != nil {
return err
}
nsPaddingShares, err := NamespacePaddingShares(lastBlobNs, lastBlobInfo.Version(), count)
if err != nil {
return err
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/shares/split_sparse_shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"testing"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
appns "github.com/celestiaorg/celestia-app/pkg/namespace"
"github.com/stretchr/testify/assert"
coretypes "github.com/tendermint/tendermint/types"
Expand Down Expand Up @@ -38,3 +39,38 @@ func TestSparseShareSplitter(t *testing.T) {
got := sss.Export()
assert.Len(t, got, 2)
}

func TestWriteNamespacePaddingShares(t *testing.T) {
ns1 := appns.MustNewV0(bytes.Repeat([]byte{1}, appns.NamespaceVersionZeroIDSize))
blob1 := newBlob(ns1, appconsts.ShareVersionZero)

sss := NewSparseShareSplitter()

err := sss.Write(blob1)
assert.NoError(t, err)
err = sss.WriteNamespacePaddingShares(1)
assert.NoError(t, err)

// got is expected to be [blob1, padding]
got := sss.Export()
assert.Len(t, got, 2)

// verify that the second share is padding
isPadding, err := got[1].IsPadding()
assert.NoError(t, err)
assert.True(t, isPadding)

// verify that the padding share has the same share version as blob1
info, err := got[1].InfoByte()
assert.NoError(t, err)
assert.Equal(t, info.Version(), appconsts.ShareVersionZero)
}

func newBlob(ns appns.Namespace, shareVersion uint8) coretypes.Blob {
return coretypes.Blob{
NamespaceVersion: ns.Version,
NamespaceID: ns.ID,
ShareVersion: shareVersion,
Data: []byte("data"),
}
}

0 comments on commit 39b81f5

Please sign in to comment.