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

chore: share encoding refactor #1462

Merged
merged 19 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 6 additions & 11 deletions app/test/process_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,12 @@ func TestProcessProposal(t *testing.T) {

dataSquare, err := shares.Split(bd, true)
require.NoError(t, err)
dataSquare[1] = flipSequenceStart(dataSquare[1])

b := shares.NewEmptyBuilder().ImportRawShare(dataSquare[1].ToBytes())
b.FlipSequenceStart()
updatedShare, err := b.Build()
require.NoError(t, err)
dataSquare[1] = *updatedShare

eds, err := da.ExtendShares(d.SquareSize, shares.ToBytes(dataSquare))
require.NoError(t, err)
Expand Down Expand Up @@ -270,16 +275,6 @@ func TestProcessProposal(t *testing.T) {
}
}

// flipSequenceStart flips the sequence start indicator of the share provided
func flipSequenceStart(share shares.Share) shares.Share {
// the info byte is immediately after the namespace
infoByteIndex := appconsts.NamespaceSize
// the sequence start indicator is the last bit of the info byte so flip the
// last bit
share[infoByteIndex] = share[infoByteIndex] ^ 0x01
return share
}

func deref[T any](s []*T) []T {
t := make([]T, len(s))
for i, ss := range s {
Expand Down
2 changes: 1 addition & 1 deletion pkg/proof/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func NewShareInclusionProof(
tree := wrapper.NewErasuredNamespacedMerkleTree(squareSize, uint(i))
for _, share := range row {
tree.Push(
share,
share.ToBytes(),
)
}

Expand Down
34 changes: 17 additions & 17 deletions pkg/shares/compact_shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ func TestCompactShareSplitter(t *testing.T) {
css.WriteTx(tx)
}
shares, _ := css.Export(0)
rawShares := ToBytes(shares)
rawResTxs, err := parseCompactShares(rawShares, appconsts.SupportedShareVersions)
rawResTxs, err := parseCompactShares(shares, appconsts.SupportedShareVersions)
resTxs := coretypes.ToTxs(rawResTxs)
require.NoError(t, err)

Expand Down Expand Up @@ -77,9 +76,7 @@ func Test_processCompactShares(t *testing.T) {
txs := testfactory.GenerateRandomTxs(tc.txCount, tc.txSize)

shares, _, _ := SplitTxs(txs)
rawShares := ToBytes(shares)

parsedTxs, err := parseCompactShares(rawShares, appconsts.SupportedShareVersions)
parsedTxs, err := parseCompactShares(shares, appconsts.SupportedShareVersions)
if err != nil {
t.Error(err)
}
Expand All @@ -95,9 +92,7 @@ func Test_processCompactShares(t *testing.T) {
txs := testfactory.GenerateRandomlySizedTxs(tc.txCount, tc.txSize)

txShares, _, _ := SplitTxs(txs)
rawShares := ToBytes(txShares)

parsedTxs, err := parseCompactShares(rawShares, appconsts.SupportedShareVersions)
parsedTxs, err := parseCompactShares(txShares, appconsts.SupportedShareVersions)
if err != nil {
t.Error(err)
}
Expand All @@ -121,7 +116,7 @@ func TestCompactShareContainsInfoByte(t *testing.T) {
shares, _ := css.Export(0)
assert.Condition(t, func() bool { return len(shares) == 1 })

infoByte := shares[0][appconsts.NamespaceSize : appconsts.NamespaceSize+appconsts.ShareInfoBytes][0]
infoByte := shares[0].data[appconsts.NamespaceSize : appconsts.NamespaceSize+appconsts.ShareInfoBytes][0]

isSequenceStart := true
want, err := NewInfoByte(appconsts.ShareVersionZero, isSequenceStart)
Expand All @@ -141,7 +136,7 @@ func TestContiguousCompactShareContainsInfoByte(t *testing.T) {
shares, _ := css.Export(0)
assert.Condition(t, func() bool { return len(shares) > 1 })

infoByte := shares[1][appconsts.NamespaceSize : appconsts.NamespaceSize+appconsts.ShareInfoBytes][0]
infoByte := shares[1].data[appconsts.NamespaceSize : appconsts.NamespaceSize+appconsts.ShareInfoBytes][0]

isSequenceStart := false
want, err := NewInfoByte(appconsts.ShareVersionZero, isSequenceStart)
Expand All @@ -152,8 +147,8 @@ func TestContiguousCompactShareContainsInfoByte(t *testing.T) {

func Test_parseCompactSharesErrors(t *testing.T) {
type testCase struct {
name string
rawShares [][]byte
name string
shares []Share
}

txs := testfactory.GenerateRandomTxs(2, appconsts.ContinuationCompactShareContentSize*4)
Expand All @@ -162,23 +157,28 @@ func Test_parseCompactSharesErrors(t *testing.T) {

unsupportedShareVersion := 5
infoByte, _ := NewInfoByte(uint8(unsupportedShareVersion), true)
shareWithUnsupportedShareVersion := rawShares[0]
shareWithUnsupportedShareVersion[appconsts.NamespaceSize] = byte(infoByte)
shareWithUnsupportedShareVersionBytes := rawShares[0]
shareWithUnsupportedShareVersionBytes[appconsts.NamespaceSize] = byte(infoByte)

shareWithUnsupportedShareVersion, err := newShare(shareWithUnsupportedShareVersionBytes)
if err != nil {
t.Fatal(err)
}

testCases := []testCase{
{
"share with start indicator false",
rawShares[1:], // set the first share to the second share which has the start indicator set to false
txShares[1:], // set the first share to the second share which has the start indicator set to false
},
{
"share with unsupported share version",
[][]byte{shareWithUnsupportedShareVersion},
[]Share{*shareWithUnsupportedShareVersion},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
_, err := parseCompactShares(tt.rawShares, appconsts.SupportedShareVersions)
_, err := parseCompactShares(tt.shares, appconsts.SupportedShareVersions)
assert.Error(t, err)
})
}
Expand Down
21 changes: 13 additions & 8 deletions pkg/shares/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,29 @@ func merge(eds *rsmt2d.ExtendedDataSquare) (coretypes.Data, error) {

// sort block data shares by namespace
var (
sortedTxShares [][]byte
sortedPfbTxShares [][]byte
sortedBlobShares [][]byte
sortedTxShares []Share
sortedPfbTxShares []Share
sortedBlobShares []Share
)

// iterate over each row index
for x := uint(0); x < squareSize; x++ {
// iterate over each share in the original data square
row := eds.Row(x)

for _, share := range row[:squareSize] {
for _, shareBytes := range row[:squareSize] {
// sort the data of that share types via namespace
nid := share[:appconsts.NamespaceSize]
share, err := NewEmptyBuilder().ImportRawShare(shareBytes).Build()
if err != nil {
return coretypes.Data{}, err
}
nid := share.NamespaceID()

switch {
case bytes.Equal(appconsts.TxNamespaceID, nid):
sortedTxShares = append(sortedTxShares, share)
sortedTxShares = append(sortedTxShares, *share)
case bytes.Equal(appconsts.PayForBlobNamespaceID, nid):
sortedPfbTxShares = append(sortedPfbTxShares, share)
sortedPfbTxShares = append(sortedPfbTxShares, *share)
case bytes.Equal(appconsts.TailPaddingNamespaceID, nid):
continue

Expand All @@ -42,7 +47,7 @@ func merge(eds *rsmt2d.ExtendedDataSquare) (coretypes.Data, error) {

// every other namespaceID should be a blob
default:
sortedBlobShares = append(sortedBlobShares, share)
sortedBlobShares = append(sortedBlobShares, *share)
}
}
}
Expand Down
22 changes: 9 additions & 13 deletions pkg/shares/padding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package shares

import (
"bytes"
"encoding/binary"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/nmt/namespace"
Expand All @@ -13,22 +12,19 @@ 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 NamespacePaddingShare(ns namespace.ID) Share {
infoByte, err := NewInfoByte(appconsts.ShareVersionZero, true)
if err != nil {
b := NewBuilder(ns, appconsts.ShareVersionZero, true)
if err := b.WriteSequenceLen(0); err != nil {
panic(err)
}

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

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

share, err := b.Build()
if err != nil {
panic(err)
}

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
return *share
}

// NamespacePaddingShares returns n namespace padding shares.
Expand Down
12 changes: 6 additions & 6 deletions pkg/shares/padding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ var tailPadding, _ = zeroPadIfNecessary([]byte{
}, appconsts.ShareSize)

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

func TestNamespacePaddingShares(t *testing.T) {
Expand All @@ -41,8 +41,8 @@ func TestNamespacePaddingShares(t *testing.T) {
}

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

func TestReservedPaddingShares(t *testing.T) {
Expand All @@ -53,8 +53,8 @@ func TestReservedPaddingShares(t *testing.T) {
}

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

func TestTailPaddingShares(t *testing.T) {
Expand Down
13 changes: 6 additions & 7 deletions pkg/shares/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

// ParseTxs collects all of the transactions from the shares provided
func ParseTxs(shares [][]byte) (coretypes.Txs, error) {
// parse the sharse
func ParseTxs(shares []Share) (coretypes.Txs, error) {
// parse the shares
rawTxs, err := parseCompactShares(shares, appconsts.SupportedShareVersions)
if err != nil {
return nil, err
Expand All @@ -26,7 +26,7 @@ func ParseTxs(shares [][]byte) (coretypes.Txs, error) {
}

// ParseBlobs collects all blobs from the shares provided
func ParseBlobs(shares [][]byte) ([]coretypes.Blob, error) {
func ParseBlobs(shares []Share) ([]coretypes.Blob, error) {
blobList, err := parseSparseShares(shares, appconsts.SupportedShareVersions)
if err != nil {
return []coretypes.Blob{}, err
Expand All @@ -35,13 +35,12 @@ func ParseBlobs(shares [][]byte) ([]coretypes.Blob, error) {
return blobList, nil
}

func ParseShares(rawShares [][]byte) ([]ShareSequence, error) {
func ParseShares(shares []Share) ([]ShareSequence, error) {
sequences := []ShareSequence{}
currentSequence := ShareSequence{}

for _, rawShare := range rawShares {
share, err := NewShare(rawShare)
if err != nil {
for _, share := range shares {
if err := share.Validate(); err != nil {
return sequences, err
}
isStart, err := share.IsSequenceStart()
Expand Down
Loading