diff --git a/README.md b/README.md index 3cbd07b..b020516 100644 --- a/README.md +++ b/README.md @@ -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( diff --git a/extendeddatacrossword_test.go b/extendeddatacrossword_test.go index 149f635..fcd8a19 100644 --- a/extendeddatacrossword_test.go +++ b/extendeddatacrossword_test.go @@ -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 { @@ -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() @@ -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 @@ -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) @@ -184,7 +177,6 @@ func TestCannotRepairSquareWithBadRoots(t *testing.T) { } func TestCorruptedEdsReturnsErrByzantineData(t *testing.T) { - shareSize := 64 corruptChunk := bytes.Repeat([]byte{66}, shareSize) tests := []struct { @@ -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 { @@ -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) diff --git a/extendeddatasquare_test.go b/extendeddatasquare_test.go index ddf351a..21f3f45 100644 --- a/extendeddatasquare_test.go +++ b/extendeddatasquare_test.go @@ -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) { @@ -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)) diff --git a/rsmt2d_test.go b/rsmt2d_test.go index d81e733..517793d 100644 --- a/rsmt2d_test.go +++ b/rsmt2d_test.go @@ -9,23 +9,23 @@ import ( "github.com/stretchr/testify/assert" ) +// 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( @@ -75,22 +75,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( @@ -114,7 +111,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 @@ -137,7 +134,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.