Skip to content

Commit

Permalink
chore!: consistent share prefix for tail padding (#1353)
Browse files Browse the repository at this point in the history
  • Loading branch information
rootulp authored Feb 8, 2023
1 parent 1f52a58 commit 40ce231
Show file tree
Hide file tree
Showing 14 changed files with 243 additions and 234 deletions.
4 changes: 2 additions & 2 deletions pkg/appconsts/appconsts.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ var (
// PayForBlobNamespaceID is the namespace reserved for PayForBlobs transactions.
PayForBlobNamespaceID = namespace.ID{0, 0, 0, 0, 0, 0, 0, 4}

// ReservedNamespacePadding is the namespace used for padding after all
// ReservedPaddingNamespaceID is the namespace used for padding after all
// reserved namespaces. In practice this padding is after transactions
// (ordinary and PFBs) but before blobs.
ReservedNamespacePadding = namespace.ID{0, 0, 0, 0, 0, 0, 0, 255}
ReservedPaddingNamespaceID = namespace.ID{0, 0, 0, 0, 0, 0, 0, 255}

// MaxReservedNamespace is the lexicographically largest namespace that is
// reserved for protocol use.
Expand Down
16 changes: 4 additions & 12 deletions pkg/da/data_availability_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,14 @@ func (dah *DataAvailabilityHeader) IsZero() bool {
}

// MinDataAvailabilityHeader returns the minimum valid data availability header.
// It is equal to the data availability header for an empty block
// It is equal to the data availability header for a block with one tail padding
// share.
func MinDataAvailabilityHeader() DataAvailabilityHeader {
shares := GenerateEmptyShares(appconsts.MinShareCount)
eds, err := ExtendShares(appconsts.DefaultMinSquareSize, shares)
s := shares.ToBytes(shares.TailPaddingShares(appconsts.MinShareCount))
eds, err := ExtendShares(appconsts.DefaultMinSquareSize, s)
if err != nil {
panic(err)
}
dah := NewDataAvailabilityHeader(eds)
return dah
}

// GenerateEmptyShares generate an array of empty shares
func GenerateEmptyShares(size int) [][]byte {
result := make([][]byte, size)
for i := 0; i < size; i++ {
result[i] = shares.TailPaddingShare()
}
return result
}
7 changes: 2 additions & 5 deletions pkg/da/data_availability_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ func TestNilDataAvailabilityHeaderHashDoesntCrash(t *testing.T) {
func TestMinDataAvailabilityHeader(t *testing.T) {
dah := MinDataAvailabilityHeader()
expectedHash := []byte{
0x6f, 0x52, 0xda, 0xc1, 0x65, 0x45, 0xe4, 0x57, 0x25, 0xbe, 0x6e, 0xa3, 0x2a, 0xed, 0x55, 0x26,
0x6e, 0x45, 0x3, 0x48, 0x0, 0xee, 0xe1, 0xd8, 0x7c, 0x94, 0x28, 0xf4, 0x84, 0x4e, 0xa4, 0x7a,
0x25, 0x77, 0x60, 0x46, 0x19, 0x93, 0xf8, 0xf1, 0x97, 0xb4, 0x21, 0xec, 0x74, 0x35, 0xf3, 0xc3,
0x6c, 0x37, 0x34, 0x92, 0x3e, 0x3d, 0xa9, 0xa4, 0x2d, 0xc7, 0x3b, 0x5, 0xf0, 0x7b, 0x3d, 0x8,
}
require.Equal(t, expectedHash, dah.hash)
require.NoError(t, dah.ValidateBasic())
// important note: also see the types.TestEmptyBlockDataAvailabilityHeader test
// which ensures that empty block data results in the minimum data availability
// header
}

func TestNewDataAvailabilityHeader(t *testing.T) {
Expand Down
54 changes: 0 additions & 54 deletions pkg/shares/namespaced_padding.go

This file was deleted.

85 changes: 0 additions & 85 deletions pkg/shares/namespaced_padding_test.go

This file was deleted.

66 changes: 66 additions & 0 deletions pkg/shares/padding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package shares

import (
"bytes"
"encoding/binary"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/nmt/namespace"
)

// 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 non-interactive default rules. The ns parameter provided should
// be the namespace of the blob that precedes this padding in the data square.
func NamespacePaddingShare(ns namespace.ID) Share {
infoByte, err := NewInfoByte(appconsts.ShareVersionZero, true)
if err != nil {
panic(err)
}

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
}

// NamespacePaddingShares returns n namespace padding shares.
func NamespacePaddingShares(ns namespace.ID, n int) []Share {
shares := make([]Share, n)
for i := 0; i < n; i++ {
shares[i] = NamespacePaddingShare(ns)
}
return shares
}

// ReservedPaddingShare returns a share that acts as padding. Reserved padding
// shares follow all significant shares in the reserved namespace so that the
// first blob can start at an index that conforms to non-interactive default
// rules.
func ReservedPaddingShare() Share {
return NamespacePaddingShare(appconsts.ReservedPaddingNamespaceID)
}

// ReservedPaddingShare returns n reserved padding shares.
func ReservedPaddingShares(n int) []Share {
return NamespacePaddingShares(appconsts.ReservedPaddingNamespaceID, n)
}

// TailPaddingShare is a share that is used to pad a data square to the desired
// square size. Tail padding shares follow the last blob share in the data
// square.
func TailPaddingShare() Share {
return NamespacePaddingShare(appconsts.TailPaddingNamespaceID)
}

// TailPaddingShares returns n tail padding shares.
func TailPaddingShares(n int) []Share {
return NamespacePaddingShares(appconsts.TailPaddingNamespaceID, n)
}
65 changes: 65 additions & 0 deletions pkg/shares/padding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package shares

import (
"testing"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/nmt/namespace"
"github.com/stretchr/testify/assert"
)

var nsOne = namespace.ID{1, 1, 1, 1, 1, 1, 1, 1}

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

var reservedPadding, _ = zeroPadIfNecessary([]byte{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, // namespace ID
1, // info byte
0, 0, 0, 0, // sequence len
}, appconsts.ShareSize)

var tailPadding, _ = zeroPadIfNecessary([]byte{
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, // namespace ID
1, // info byte
0, 0, 0, 0, // sequence len
}, appconsts.ShareSize)

func TestNamespacePaddingShare(t *testing.T) {
got := NamespacePaddingShare(nsOne).ToBytes()
assert.Equal(t, nsOnePadding, got)
}

func TestNamespacePaddingShares(t *testing.T) {
shares := NamespacePaddingShares(nsOne, 2)
for _, share := range shares {
assert.Equal(t, nsOnePadding, share.ToBytes())
}
}

func TestReservedPaddingShare(t *testing.T) {
got := ReservedPaddingShare().ToBytes()
assert.Equal(t, reservedPadding, got)
}

func TestReservedPaddingShares(t *testing.T) {
shares := ReservedPaddingShares(2)
for _, share := range shares {
assert.Equal(t, reservedPadding, share.ToBytes())
}
}

func TestTailPaddingShare(t *testing.T) {
got := TailPaddingShare().ToBytes()
assert.Equal(t, tailPadding, got)
}

func TestTailPaddingShares(t *testing.T) {
shares := TailPaddingShares(2)
for _, share := range shares {
assert.Equal(t, tailPadding, share.ToBytes())
}
}
4 changes: 2 additions & 2 deletions pkg/shares/parse_sparse_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ 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)
isPadding, err := share.IsPadding()
if err != nil {
return nil, err
}
if isNamespacedPadded {
if isPadding {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/shares/share_splitting.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Split(data coretypes.Data, useShareIndexes bool) ([]Share, error) {
blobShareStart = int(blobIndexes[0])
}

padding = NamespacedPaddedShares(appconsts.ReservedNamespacePadding, blobShareStart-currentShareCount)
padding = NamespacePaddingShares(appconsts.ReservedPaddingNamespaceID, blobShareStart-currentShareCount)
}
currentShareCount += len(padding)

Expand Down
Loading

0 comments on commit 40ce231

Please sign in to comment.