Skip to content

Commit

Permalink
sort namespaced shares before splitting (#546)
Browse files Browse the repository at this point in the history
* sort namespaced shares

* patch the patch
  • Loading branch information
evan-forbes authored Sep 24, 2021
1 parent a9b7b48 commit 488ac31
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
8 changes: 8 additions & 0 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -1100,6 +1101,7 @@ func (roots IntermediateStateRoots) SplitIntoShares() NamespacedShares {

func (msgs Messages) SplitIntoShares() NamespacedShares {
shares := make([]NamespacedShare, 0)
msgs.sortMessages()
for _, m := range msgs.MessagesList {
rawData, err := m.MarshalDelimited()
if err != nil {
Expand All @@ -1110,6 +1112,12 @@ func (msgs Messages) SplitIntoShares() NamespacedShares {
return shares
}

func (msgs *Messages) sortMessages() {
sort.Slice(msgs.MessagesList, func(i, j int) bool {
return bytes.Compare(msgs.MessagesList[i].NamespaceID, msgs.MessagesList[j].NamespaceID) < 0
})
}

// ComputeShares splits block data into shares of an original data square and
// returns them along with an amount of non-redundant shares. The shares
// returned are padded to complete a square size that is a power of two
Expand Down
12 changes: 7 additions & 5 deletions types/shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
tmbytes "github.com/celestiaorg/celestia-core/libs/bytes"
"github.com/celestiaorg/celestia-core/libs/protoio"
"github.com/celestiaorg/celestia-core/pkg/consts"
"github.com/celestiaorg/celestia-core/pkg/wrapper"
"github.com/celestiaorg/nmt/namespace"
"github.com/celestiaorg/rsmt2d"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type Splitter interface {
Expand Down Expand Up @@ -248,14 +250,14 @@ func TestDataFromSquare(t *testing.T) {
tc.msgCount,
tc.maxSize,
)

shares, _ := data.ComputeShares()
rawShares := shares.RawShares()
squareSize := uint64(math.Sqrt(float64(len(shares))))

eds, err := rsmt2d.ComputeExtendedDataSquare(rawShares, rsmt2d.NewRSGF8Codec(), rsmt2d.NewDefaultTree)
if err != nil {
t.Error(err)
}
tree := wrapper.NewErasuredNamespacedMerkleTree(squareSize)

eds, err := rsmt2d.ComputeExtendedDataSquare(rawShares, rsmt2d.NewRSGF8Codec(), tree.Constructor)
require.NoError(t, err)

res, err := DataFromSquare(eds)
if err != nil {
Expand Down

0 comments on commit 488ac31

Please sign in to comment.