-
Notifications
You must be signed in to change notification settings - Fork 973
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/shwap' into update-file-interface
# Conflicts: # share/share.go # share/shwap/namespaced_data.go # share/shwap/row.go
- Loading branch information
Showing
6 changed files
with
401 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package shwap | ||
|
||
import ( | ||
"bytes" | ||
"slices" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/celestiaorg/celestia-node/share" | ||
"github.com/celestiaorg/celestia-node/share/eds/edstest" | ||
"github.com/celestiaorg/celestia-node/share/sharetest" | ||
) | ||
|
||
func TestNamespacedRowFromShares(t *testing.T) { | ||
const odsSize = 8 | ||
|
||
minNamespace, err := share.NewBlobNamespaceV0(slices.Concat(bytes.Repeat([]byte{0}, 8), []byte{1, 0})) | ||
require.NoError(t, err) | ||
err = minNamespace.ValidateForData() | ||
require.NoError(t, err) | ||
|
||
for namespacedAmount := 1; namespacedAmount < odsSize; namespacedAmount++ { | ||
shares := sharetest.RandSharesWithNamespace(t, minNamespace, namespacedAmount, odsSize) | ||
parity, err := share.DefaultRSMT2DCodec().Encode(shares) | ||
require.NoError(t, err) | ||
extended := slices.Concat(shares, parity) | ||
|
||
nr, err := NamespacedRowFromShares(extended, minNamespace, 0) | ||
require.NoError(t, err) | ||
require.Equal(t, namespacedAmount, len(nr.Shares)) | ||
} | ||
} | ||
|
||
func TestNamespacedRowFromSharesNonIncluded(t *testing.T) { | ||
// TODO: this will fail until absence proof support is added | ||
t.Skip() | ||
|
||
const odsSize = 8 | ||
// Test absent namespace | ||
shares := sharetest.RandShares(t, odsSize) | ||
absentNs, err := share.GetNamespace(shares[0]).AddInt(1) | ||
require.NoError(t, err) | ||
|
||
parity, err := share.DefaultRSMT2DCodec().Encode(shares) | ||
require.NoError(t, err) | ||
extended := slices.Concat(shares, parity) | ||
|
||
nr, err := NamespacedRowFromShares(extended, absentNs, 0) | ||
require.NoError(t, err) | ||
require.Len(t, nr.Shares, 0) | ||
require.True(t, nr.Proof.IsOfAbsence()) | ||
} | ||
|
||
func TestNamespacedSharesFromEDS(t *testing.T) { | ||
const odsSize = 8 | ||
sharesAmount := odsSize * odsSize | ||
namespace := sharetest.RandV0Namespace() | ||
for amount := 1; amount < sharesAmount; amount++ { | ||
eds, root := edstest.RandEDSWithNamespace(t, namespace, amount, odsSize) | ||
nd, err := NewNamespacedSharesFromEDS(eds, namespace) | ||
require.NoError(t, err) | ||
require.True(t, len(nd) > 0) | ||
require.Len(t, nd.Flatten(), amount) | ||
|
||
err = nd.Verify(root, namespace) | ||
require.NoError(t, err) | ||
} | ||
} | ||
|
||
func TestValidateNamespacedRow(t *testing.T) { | ||
const odsSize = 8 | ||
sharesAmount := odsSize * odsSize | ||
namespace := sharetest.RandV0Namespace() | ||
for amount := 1; amount < sharesAmount; amount++ { | ||
eds, root := edstest.RandEDSWithNamespace(t, namespace, amount, odsSize) | ||
nd, err := NewNamespacedSharesFromEDS(eds, namespace) | ||
require.NoError(t, err) | ||
require.True(t, len(nd) > 0) | ||
|
||
_, from, to := share.FilterRootByNamespace(root, namespace) | ||
require.Len(t, nd, to-from) | ||
idx := from | ||
for _, row := range nd { | ||
err = row.Validate(root, namespace, idx) | ||
require.NoError(t, err) | ||
idx++ | ||
} | ||
} | ||
} | ||
|
||
func TestNamespacedRowProtoEncoding(t *testing.T) { | ||
const odsSize = 8 | ||
namespace := sharetest.RandV0Namespace() | ||
eds, _ := edstest.RandEDSWithNamespace(t, namespace, odsSize, odsSize) | ||
nd, err := NewNamespacedSharesFromEDS(eds, namespace) | ||
require.NoError(t, err) | ||
require.True(t, len(nd) > 0) | ||
|
||
expected := nd[0] | ||
pb := expected.ToProto() | ||
ndOut := NamespacedRowFromProto(pb) | ||
require.Equal(t, expected, ndOut) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package shwap | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/celestiaorg/celestia-node/share" | ||
"github.com/celestiaorg/celestia-node/share/eds/edstest" | ||
) | ||
|
||
func TestNewRowFromEDS(t *testing.T) { | ||
const odsSize = 8 | ||
eds := edstest.RandEDS(t, odsSize) | ||
|
||
for rowIdx := 0; rowIdx < odsSize*2; rowIdx++ { | ||
for _, side := range []RowSide{Left, Right} { | ||
row := RowFromEDS(eds, rowIdx, side) | ||
|
||
want := eds.Row(uint(rowIdx)) | ||
shares, err := row.Shares() | ||
require.NoError(t, err) | ||
require.Equal(t, want, shares) | ||
|
||
var half []share.Share | ||
if side == Right { | ||
half = want[odsSize:] | ||
} else { | ||
half = want[:odsSize] | ||
} | ||
require.Equal(t, half, row.halfShares) | ||
require.Equal(t, side, row.side) | ||
} | ||
} | ||
} | ||
|
||
func TestRowValidate(t *testing.T) { | ||
const odsSize = 8 | ||
eds := edstest.RandEDS(t, odsSize) | ||
root, err := share.NewRoot(eds) | ||
require.NoError(t, err) | ||
|
||
for rowIdx := 0; rowIdx < odsSize*2; rowIdx++ { | ||
for _, side := range []RowSide{Left, Right} { | ||
row := RowFromEDS(eds, rowIdx, side) | ||
|
||
err := row.VerifyRoot(root, rowIdx) | ||
require.NoError(t, err) | ||
err = row.Validate(root, rowIdx) | ||
require.NoError(t, err) | ||
} | ||
} | ||
} | ||
|
||
func TestRowValidateNegativeCases(t *testing.T) { | ||
eds := edstest.RandEDS(t, 8) // Generate a random Extended Data Square of size 8 | ||
root, err := share.NewRoot(eds) | ||
require.NoError(t, err) | ||
row := RowFromEDS(eds, 0, Left) | ||
|
||
// Test with incorrect side specification | ||
invalidSideRow := Row{halfShares: row.halfShares, side: RowSide(999)} | ||
err = invalidSideRow.Validate(root, 0) | ||
require.Error(t, err, "should error on invalid row side") | ||
|
||
// Test with invalid shares (more shares than expected) | ||
incorrectShares := make([]share.Share, (eds.Width()/2)+1) // Adding an extra share | ||
for i := range incorrectShares { | ||
incorrectShares[i] = eds.GetCell(uint(i), 0) | ||
} | ||
invalidRow := Row{halfShares: incorrectShares, side: Left} | ||
err = invalidRow.Validate(root, 0) | ||
require.Error(t, err, "should error on incorrect number of shares") | ||
|
||
// Test with empty shares | ||
emptyRow := Row{halfShares: []share.Share{}, side: Left} | ||
err = emptyRow.Validate(root, 0) | ||
require.Error(t, err, "should error on empty halfShares") | ||
|
||
// Doesn't match root. Corrupt root hash | ||
root.RowRoots[0][len(root.RowRoots[0])-1] ^= 0xFF | ||
err = row.Validate(root, 0) | ||
require.Error(t, err, "should error on invalid root hash") | ||
} | ||
|
||
func TestRowProtoEncoding(t *testing.T) { | ||
const odsSize = 8 | ||
eds := edstest.RandEDS(t, odsSize) | ||
|
||
for rowIdx := 0; rowIdx < odsSize*2; rowIdx++ { | ||
for _, side := range []RowSide{Left, Right} { | ||
row := RowFromEDS(eds, rowIdx, side) | ||
|
||
pb := row.ToProto() | ||
rowOut := RowFromProto(pb) | ||
require.Equal(t, row, rowOut) | ||
} | ||
} | ||
} | ||
|
||
// BenchmarkRowValidate benchmarks the performance of row validation. | ||
// BenchmarkRowValidate-10 9591 121802 ns/op | ||
func BenchmarkRowValidate(b *testing.B) { | ||
const odsSize = 32 | ||
eds := edstest.RandEDS(b, odsSize) | ||
root, err := share.NewRoot(eds) | ||
require.NoError(b, err) | ||
row := RowFromEDS(eds, 0, Left) | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
_ = row.Validate(root, 0) | ||
} | ||
} |
Oops, something went wrong.