Skip to content

Commit

Permalink
test: bump share size to 512 (#244)
Browse files Browse the repository at this point in the history
Closes #237
  • Loading branch information
rootulp authored Jul 10, 2023
1 parent 21850e7 commit 3f7187a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 78 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import (
)

func main() {
// Size of each share, in bytes
bufferSize := 64
// shareSize is the size of each share (in bytes).
shareSize := 512
// Init new codec
codec := rsmt2d.NewLeoRSCodec()

ones := bytes.Repeat([]byte{1}, bufferSize)
twos := bytes.Repeat([]byte{2}, bufferSize)
threes := bytes.Repeat([]byte{3}, bufferSize)
fours := bytes.Repeat([]byte{4}, bufferSize)
ones := bytes.Repeat([]byte{1}, shareSize)
twos := bytes.Repeat([]byte{2}, shareSize)
threes := bytes.Repeat([]byte{3}, shareSize)
fours := bytes.Repeat([]byte{4}, shareSize)

// Compute parity shares
eds, err := rsmt2d.ComputeExtendedDataSquare(
Expand Down
45 changes: 18 additions & 27 deletions extendeddatacrossword_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"github.com/stretchr/testify/require"
)

// shareSize is the size of each share (in bytes) used for testing.
const shareSize = 512

// PseudoFraudProof is an example fraud proof.
// TODO a real fraud proof would have a Merkle proof for each share.
type PseudoFraudProof struct {
Expand All @@ -20,19 +23,16 @@ type PseudoFraudProof struct {
}

func TestRepairExtendedDataSquare(t *testing.T) {
bufferSize := 64
tests := []struct {
name string
// Size of each share, in bytes
shareSize int
codec Codec
name string
codec Codec
}{
{"leopard", bufferSize, NewLeoRSCodec()},
{"leopard", NewLeoRSCodec()},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
name, codec, shareSize := test.name, test.codec, test.shareSize
name, codec := test.name, test.codec
original := createTestEds(codec, shareSize)

rowRoots, err := original.RowRoots()
Expand Down Expand Up @@ -90,20 +90,17 @@ func TestRepairExtendedDataSquare(t *testing.T) {
}

func TestValidFraudProof(t *testing.T) {
bufferSize := 64
corruptChunk := bytes.Repeat([]byte{66}, bufferSize)
corruptChunk := bytes.Repeat([]byte{66}, shareSize)
tests := []struct {
name string
// Size of each share, in bytes
shareSize int
codec Codec
name string
codec Codec
}{
{"leopard", bufferSize, NewLeoRSCodec()},
{"leopard", NewLeoRSCodec()},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
name, codec, shareSize := test.name, test.codec, test.shareSize
name, codec := test.name, test.codec
original := createTestEds(codec, shareSize)

var byzData *ErrByzantineData
Expand Down Expand Up @@ -151,21 +148,17 @@ func TestValidFraudProof(t *testing.T) {
}

func TestCannotRepairSquareWithBadRoots(t *testing.T) {
bufferSize := 64
corruptChunk := bytes.Repeat([]byte{66}, bufferSize)
corruptChunk := bytes.Repeat([]byte{66}, shareSize)
tests := []struct {
name string
// Size of each share, in bytes
shareSize int
codec Codec
name string
codec Codec
}{
{"leopard", bufferSize, NewLeoRSCodec()},
{"leopard", NewLeoRSCodec()},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
codec, shareSize := test.codec, test.shareSize
original := createTestEds(codec, shareSize)
original := createTestEds(test.codec, shareSize)

rowRoots, err := original.RowRoots()
require.NoError(t, err)
Expand All @@ -184,7 +177,6 @@ func TestCannotRepairSquareWithBadRoots(t *testing.T) {
}

func TestCorruptedEdsReturnsErrByzantineData(t *testing.T) {
shareSize := 64
corruptChunk := bytes.Repeat([]byte{66}, shareSize)

tests := []struct {
Expand Down Expand Up @@ -261,7 +253,6 @@ func TestCorruptedEdsReturnsErrByzantineData(t *testing.T) {
}

func BenchmarkRepair(b *testing.B) {
chunkSize := uint(256)
// For different ODS sizes
for originalDataWidth := 4; originalDataWidth <= 512; originalDataWidth *= 2 {
for codecName, codec := range codecs {
Expand All @@ -271,7 +262,7 @@ func BenchmarkRepair(b *testing.B) {
}

// Generate a new range original data square then extend it
square := genRandDS(originalDataWidth, int(chunkSize))
square := genRandDS(originalDataWidth, shareSize)
eds, err := ComputeExtendedDataSquare(square, codec, NewDefaultTree)
if err != nil {
b.Error(err)
Expand Down
40 changes: 17 additions & 23 deletions extendeddatasquare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ import (
"github.com/stretchr/testify/assert"
)

const ShardSize = 64

var (
zeros = bytes.Repeat([]byte{0}, ShardSize)
ones = bytes.Repeat([]byte{1}, ShardSize)
twos = bytes.Repeat([]byte{2}, ShardSize)
threes = bytes.Repeat([]byte{3}, ShardSize)
fours = bytes.Repeat([]byte{4}, ShardSize)
fives = bytes.Repeat([]byte{5}, ShardSize)
eights = bytes.Repeat([]byte{8}, ShardSize)
elevens = bytes.Repeat([]byte{11}, ShardSize)
thirteens = bytes.Repeat([]byte{13}, ShardSize)
fifteens = bytes.Repeat([]byte{15}, ShardSize)
zeros = bytes.Repeat([]byte{0}, shareSize)
ones = bytes.Repeat([]byte{1}, shareSize)
twos = bytes.Repeat([]byte{2}, shareSize)
threes = bytes.Repeat([]byte{3}, shareSize)
fours = bytes.Repeat([]byte{4}, shareSize)
fives = bytes.Repeat([]byte{5}, shareSize)
eights = bytes.Repeat([]byte{8}, shareSize)
elevens = bytes.Repeat([]byte{11}, shareSize)
thirteens = bytes.Repeat([]byte{13}, shareSize)
fifteens = bytes.Repeat([]byte{15}, shareSize)
)

func TestComputeExtendedDataSquare(t *testing.T) {
Expand Down Expand Up @@ -99,38 +97,34 @@ func TestMarshalJSON(t *testing.T) {
func TestNewExtendedDataSquare(t *testing.T) {
t.Run("returns an error if edsWidth is not even", func(t *testing.T) {
edsWidth := uint(1)
chunkSize := uint(512)

_, err := NewExtendedDataSquare(NewLeoRSCodec(), NewDefaultTree, edsWidth, chunkSize)
_, err := NewExtendedDataSquare(NewLeoRSCodec(), NewDefaultTree, edsWidth, shareSize)
assert.Error(t, err)
})
t.Run("returns a 4x4 EDS", func(t *testing.T) {
edsWidth := uint(4)
chunkSize := uint(512)

got, err := NewExtendedDataSquare(NewLeoRSCodec(), NewDefaultTree, edsWidth, chunkSize)
got, err := NewExtendedDataSquare(NewLeoRSCodec(), NewDefaultTree, edsWidth, shareSize)
assert.NoError(t, err)
assert.Equal(t, edsWidth, got.width)
assert.Equal(t, chunkSize, got.chunkSize)
assert.Equal(t, uint(shareSize), got.chunkSize)
})
t.Run("returns a 4x4 EDS that can be populated via SetCell", func(t *testing.T) {
edsWidth := uint(4)
chunkSize := uint(512)

got, err := NewExtendedDataSquare(NewLeoRSCodec(), NewDefaultTree, edsWidth, chunkSize)
got, err := NewExtendedDataSquare(NewLeoRSCodec(), NewDefaultTree, edsWidth, shareSize)
assert.NoError(t, err)

chunk := bytes.Repeat([]byte{1}, int(chunkSize))
chunk := bytes.Repeat([]byte{1}, int(shareSize))
err = got.SetCell(0, 0, chunk)
assert.NoError(t, err)
assert.Equal(t, chunk, got.squareRow[0][0])
})
t.Run("returns an error when SetCell is invoked on an EDS with a chunk that is not the correct size", func(t *testing.T) {
edsWidth := uint(4)
chunkSize := uint(512)
incorrectChunkSize := uint(513)
incorrectChunkSize := shareSize + 1

got, err := NewExtendedDataSquare(NewLeoRSCodec(), NewDefaultTree, edsWidth, chunkSize)
got, err := NewExtendedDataSquare(NewLeoRSCodec(), NewDefaultTree, edsWidth, shareSize)
assert.NoError(t, err)

chunk := bytes.Repeat([]byte{1}, int(incorrectChunkSize))
Expand Down
41 changes: 19 additions & 22 deletions rsmt2d_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import (
"github.com/stretchr/testify/require"
)

// shareSize is the size of each share (in bytes) used for testing.
const shareSize = 512

func TestEdsRepairRoundtripSimple(t *testing.T) {
bufferSize := 64
tests := []struct {
name string
// Size of each share, in bytes
shareSize int
codec rsmt2d.Codec
name string
codec rsmt2d.Codec
}{
{"leopard", bufferSize, rsmt2d.NewLeoRSCodec()},
{"leopard", rsmt2d.NewLeoRSCodec()},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ones := bytes.Repeat([]byte{1}, bufferSize)
twos := bytes.Repeat([]byte{2}, bufferSize)
threes := bytes.Repeat([]byte{3}, bufferSize)
fours := bytes.Repeat([]byte{4}, bufferSize)
ones := bytes.Repeat([]byte{1}, shareSize)
twos := bytes.Repeat([]byte{2}, shareSize)
threes := bytes.Repeat([]byte{3}, shareSize)
fours := bytes.Repeat([]byte{4}, shareSize)

// Compute parity shares
eds, err := rsmt2d.ComputeExtendedDataSquare(
Expand Down Expand Up @@ -76,22 +76,19 @@ func TestEdsRepairRoundtripSimple(t *testing.T) {
}

func TestEdsRepairTwice(t *testing.T) {
bufferSize := 64
tests := []struct {
name string
// Size of each share, in bytes
shareSize int
codec rsmt2d.Codec
name string
codec rsmt2d.Codec
}{
{"leopard", bufferSize, rsmt2d.NewLeoRSCodec()},
{"leopard", rsmt2d.NewLeoRSCodec()},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ones := bytes.Repeat([]byte{1}, bufferSize)
twos := bytes.Repeat([]byte{2}, bufferSize)
threes := bytes.Repeat([]byte{3}, bufferSize)
fours := bytes.Repeat([]byte{4}, bufferSize)
ones := bytes.Repeat([]byte{1}, shareSize)
twos := bytes.Repeat([]byte{2}, shareSize)
threes := bytes.Repeat([]byte{3}, shareSize)
fours := bytes.Repeat([]byte{4}, shareSize)

// Compute parity shares
eds, err := rsmt2d.ComputeExtendedDataSquare(
Expand All @@ -115,7 +112,7 @@ func TestEdsRepairTwice(t *testing.T) {
flattened := eds.Flattened()

// Delete some shares, just enough so that repairing is possible, then remove one more.
missing := make([]byte, bufferSize)
missing := make([]byte, shareSize)
copy(missing, flattened[1])
flattened[0], flattened[1], flattened[2], flattened[3] = nil, nil, nil, nil
flattened[4], flattened[5], flattened[6], flattened[7] = nil, nil, nil, nil
Expand All @@ -138,7 +135,7 @@ func TestEdsRepairTwice(t *testing.T) {
t.Errorf("RepairExtendedDataSquare did not fail with `%v`, got `%v`", rsmt2d.ErrUnrepairableDataSquare, err)
}
// Re-insert missing share and try again.
flattened[1] = make([]byte, bufferSize)
flattened[1] = make([]byte, shareSize)
copy(flattened[1], missing)

// Re-import the data square.
Expand Down

0 comments on commit 3f7187a

Please sign in to comment.