Skip to content

Commit a2c4252

Browse files
rootulpevan-forbes
authored andcommitted
feat!: increase share size to 512 bytes (celestiaorg#850)
Closes celestiaorg#825 This is ready for review but we may not merge it b/c we haven't made a final decision on what the share size should be Co-authored-by: evan-forbes <[email protected]>
1 parent c07572a commit a2c4252

14 files changed

+261
-122
lines changed

app/estimate_square_size_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ func Test_estimateSquareSize(t *testing.T) {
2626
tests := []test{
2727
{"empty block minimum square size", 0, 0, 0, appconsts.MinSquareSize},
2828
{"full block with only txs", 10000, 0, 0, appconsts.MaxSquareSize},
29-
{"random small block square size 4", 0, 1, appconsts.SparseShareContentSize, 4},
29+
{"random small block square size 2", 0, 1, appconsts.SparseShareContentSize, 2},
3030
{"random small block square size 4", 0, 1, appconsts.SparseShareContentSize * 10, 4},
31-
{"random small block w/ 10 normal txs square size 4", 10, 1, appconsts.SparseShareContentSize, 8},
31+
{"random small block w/ 10 normal txs square size 4", 10, 1, appconsts.SparseShareContentSize, 4},
3232
{"random small block square size 16", 0, 4, appconsts.SparseShareContentSize * 8, 16},
3333
{"random medium block square size 32", 0, 50, appconsts.SparseShareContentSize * 4, 32},
3434
{"full block max square size", 0, 8000, appconsts.SparseShareContentSize, appconsts.MaxSquareSize},

app/test/block_size_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestIntegrationTestSuite(t *testing.T) {
3131
cfg.EnableTMLogging = false
3232
cfg.MinGasPrices = "0ucls"
3333
cfg.NumValidators = 1
34-
cfg.TimeoutCommit = time.Millisecond * 200
34+
cfg.TimeoutCommit = time.Millisecond * 400
3535
suite.Run(t, NewIntegrationTestSuite(cfg))
3636
}
3737

pkg/appconsts/appconsts.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// https://github.com/celestiaorg/celestia-specs/blob/master/src/specs/consensus.md#constants
1414
const (
1515
// ShareSize is the size of a share in bytes.
16-
ShareSize = 256
16+
ShareSize = 512
1717

1818
// NamespaceSize is the namespace size in bytes.
1919
NamespaceSize = 8
@@ -27,7 +27,7 @@ const (
2727

2828
// CompactShareReservedBytes is the number of bytes reserved for the location of
2929
// the first unit (transaction, ISR, evidence) in a compact share.
30-
CompactShareReservedBytes = 1
30+
CompactShareReservedBytes = 2
3131

3232
// ContinuationCompactShareContentSize is the number of bytes usable for
3333
// data in a continuation compact share. A continuation share is any

pkg/da/data_availability_header_test.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func TestNilDataAvailabilityHeaderHashDoesntCrash(t *testing.T) {
2525
func TestMinDataAvailabilityHeader(t *testing.T) {
2626
dah := MinDataAvailabilityHeader()
2727
expectedHash := []byte{
28-
0x7b, 0x57, 0x8b, 0x35, 0x1b, 0x1b, 0xb, 0xbd, 0x70, 0xbb, 0x35, 0x0, 0x19, 0xeb, 0xc9, 0x64,
29-
0xc4, 0x4a, 0x14, 0xa, 0x37, 0xef, 0x71, 0x5b, 0x55, 0x2a, 0x7f, 0x8f, 0x31, 0x5a, 0xcd, 0x19,
28+
0x6f, 0x52, 0xda, 0xc1, 0x65, 0x45, 0xe4, 0x57, 0x25, 0xbe, 0x6e, 0xa3, 0x2a, 0xed, 0x55, 0x26,
29+
0x6e, 0x45, 0x3, 0x48, 0x0, 0xee, 0xe1, 0xd8, 0x7c, 0x94, 0x28, 0xf4, 0x84, 0x4e, 0xa4, 0x7a,
3030
}
3131
require.Equal(t, expectedHash, dah.hash)
3232
require.NoError(t, dah.ValidateBasic())
@@ -47,31 +47,32 @@ func TestNewDataAvailabilityHeader(t *testing.T) {
4747
{
4848
name: "typical",
4949
expectedHash: []byte{
50-
0xfe, 0x9c, 0x6b, 0xd8, 0xe5, 0x7c, 0xd1, 0x5d, 0x1f, 0xd6, 0x55, 0x7e, 0x87, 0x7d, 0xd9, 0x7d,
51-
0xdb, 0xf2, 0x66, 0xfa, 0x60, 0x24, 0x2d, 0xb3, 0xa0, 0x9c, 0x4f, 0x4e, 0x5b, 0x2a, 0x2c, 0x2a,
50+
0x57, 0x71, 0xc6, 0x77, 0x2f, 0x32, 0x95, 0x73, 0xaa, 0xb8, 0x20, 0xd1, 0xbe, 0x4c, 0xc2, 0x21,
51+
0x7d, 0x54, 0xb6, 0x7e, 0xf2, 0x4f, 0xbc, 0xd3, 0x9a, 0x95, 0x15, 0xd0, 0x92, 0x63, 0xc1, 0xf9,
5252
},
5353
squareSize: 2,
5454
shares: generateShares(4, 1),
5555
},
5656
{
5757
name: "max square size",
5858
expectedHash: []byte{
59-
0xe2, 0x87, 0x23, 0xd0, 0x2d, 0x54, 0x25, 0x5f, 0x79, 0x43, 0x8e, 0xfb, 0xb7, 0xe8, 0xfa, 0xf5,
60-
0xbf, 0x93, 0x50, 0xb3, 0x64, 0xd0, 0x4f, 0xa7, 0x7b, 0xb1, 0x83, 0x3b, 0x8, 0xba, 0xd3, 0xa4,
59+
0xbf, 0xe5, 0x8f, 0x4b, 0xae, 0x2b, 0x65, 0x8b, 0xa8, 0xcb, 0xf9, 0xee, 0x8c, 0x6a, 0x1f, 0x72,
60+
0xa9, 0x58, 0xc4, 0xcc, 0xca, 0x41, 0x4c, 0xbf, 0x8b, 0x18, 0xf9, 0x53, 0xe, 0xb1, 0x40, 0x54,
6161
},
6262
squareSize: appconsts.MaxSquareSize,
6363
shares: generateShares(appconsts.MaxSquareSize*appconsts.MaxSquareSize, 99),
6464
},
6565
}
6666

6767
for _, tt := range tests {
68-
tt := tt
69-
eds, err := ExtendShares(tt.squareSize, tt.shares)
70-
require.NoError(t, err)
71-
resdah := NewDataAvailabilityHeader(eds)
72-
require.Equal(t, tt.squareSize*2, uint64(len(resdah.ColumnRoots)), tt.name)
73-
require.Equal(t, tt.squareSize*2, uint64(len(resdah.RowsRoots)), tt.name)
74-
require.Equal(t, tt.expectedHash, resdah.hash, tt.name)
68+
t.Run(tt.name, func(t *testing.T) {
69+
eds, err := ExtendShares(tt.squareSize, tt.shares)
70+
require.NoError(t, err)
71+
resdah := NewDataAvailabilityHeader(eds)
72+
require.Equal(t, tt.squareSize*2, uint64(len(resdah.ColumnRoots)), tt.name)
73+
require.Equal(t, tt.squareSize*2, uint64(len(resdah.RowsRoots)), tt.name)
74+
require.Equal(t, tt.expectedHash, resdah.hash, tt.name)
75+
})
7576
}
7677
}
7778

pkg/prove/proof_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@ func TestTxShareIndex(t *testing.T) {
152152
{appconsts.FirstCompactShareContentSize + appconsts.ContinuationCompactShareContentSize + 1, 2},
153153
{appconsts.FirstCompactShareContentSize + (appconsts.ContinuationCompactShareContentSize * 2), 2},
154154
{appconsts.FirstCompactShareContentSize + (appconsts.ContinuationCompactShareContentSize * 2) + 1, 3},
155-
// 81 compact shares + partially filled out last share
155+
// 81 full compact shares then a partially filled out 82nd share (which is index 81 because 0-indexed)
156156
{appconsts.FirstCompactShareContentSize + (appconsts.ContinuationCompactShareContentSize * 80) + 160, 81},
157-
// 81 compact shares + full last share
158-
{appconsts.FirstCompactShareContentSize + (appconsts.ContinuationCompactShareContentSize * 80) + 246, 81},
159-
// 82 compact shares + one byte in last share
160-
{appconsts.FirstCompactShareContentSize + (appconsts.ContinuationCompactShareContentSize * 80) + 247, 82},
161-
// 82 compact shares + two bytes in last share
162-
{appconsts.FirstCompactShareContentSize + (appconsts.ContinuationCompactShareContentSize * 80) + 248, 82},
157+
// 81 full compact shares then a full 82nd share
158+
{appconsts.FirstCompactShareContentSize + (appconsts.ContinuationCompactShareContentSize * 80) + 501, 81},
159+
// 82 full compact shares then one byte in 83rd share
160+
{appconsts.FirstCompactShareContentSize + (appconsts.ContinuationCompactShareContentSize * 80) + 502, 82},
161+
// 82 compact shares then two bytes in 83rd share
162+
{appconsts.FirstCompactShareContentSize + (appconsts.ContinuationCompactShareContentSize * 80) + 503, 82},
163163
}
164164

165165
for _, tt := range tests {

pkg/shares/reserved_bytes.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package shares
2+
3+
import (
4+
"bytes"
5+
"encoding/binary"
6+
"fmt"
7+
8+
"github.com/celestiaorg/celestia-app/pkg/appconsts"
9+
)
10+
11+
// NewReservedBytes returns a byte slice of length
12+
// appconsts.CompactShareReservedBytes that contains a varint of the byteIndex
13+
// of the first unit that starts in a compact share. If no unit starts in the
14+
// compact share, ReservedBytes is [0, 0].
15+
func NewReservedBytes(byteIndex uint64) ([]byte, error) {
16+
if byteIndex >= appconsts.ShareSize {
17+
return []byte{}, fmt.Errorf("byte index %d must be less than share size %d", byteIndex, appconsts.ShareSize)
18+
}
19+
reservedBytes := make([]byte, appconsts.CompactShareReservedBytes)
20+
binary.PutUvarint(reservedBytes, byteIndex)
21+
return reservedBytes, nil
22+
}
23+
24+
// ParseReservedBytes parses a byte slice of length
25+
// appconsts.CompactShareReservedBytes into a byteIndex.
26+
func ParseReservedBytes(reservedBytes []byte) (uint64, error) {
27+
if len(reservedBytes) != appconsts.CompactShareReservedBytes {
28+
return 0, fmt.Errorf("reserved bytes must be of length %d", appconsts.CompactShareReservedBytes)
29+
}
30+
reader := bytes.NewReader(reservedBytes)
31+
byteIndex, err := binary.ReadUvarint(reader)
32+
if err != nil {
33+
return 0, err
34+
}
35+
if byteIndex >= appconsts.ShareSize {
36+
return 0, fmt.Errorf("reserved bytes varint %d must be less than share size %d", byteIndex, appconsts.ShareSize)
37+
}
38+
return byteIndex, nil
39+
}

pkg/shares/reserved_bytes_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package shares
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestParseReservedBytes(t *testing.T) {
10+
type testCase struct {
11+
name string
12+
input []byte
13+
want uint64
14+
expectErr bool
15+
}
16+
testCases := []testCase{
17+
{"byte index of 0", []byte{0, 0}, 0, false},
18+
{"byte index of 2", []byte{2, 0}, 2, false},
19+
{"byte index of 4", []byte{4, 0}, 4, false},
20+
{"byte index of 8", []byte{8, 0}, 8, false},
21+
{"byte index of 16", []byte{16, 0}, 16, false},
22+
{"byte index of 32", []byte{32, 0}, 32, false},
23+
{"byte index of 64", []byte{64, 0}, 64, false},
24+
{"byte index of 128", []byte{128, 1}, 128, false},
25+
{"byte index of 256", []byte{128, 2}, 256, false},
26+
{"byte index of 511", []byte{255, 3}, 511, false},
27+
28+
// error cases
29+
{"empty", []byte{}, 0, true},
30+
{"too few reserved bytes", []byte{1}, 0, true},
31+
{"too many reserved bytes", []byte{3, 3, 3}, 0, true},
32+
{"byte index of 512 is equal to share size", []byte{128, 4}, 0, true},
33+
{"byte index of 1000 is greater than share size", []byte{232, 7}, 0, true},
34+
}
35+
36+
for _, tc := range testCases {
37+
t.Run(tc.name, func(t *testing.T) {
38+
got, err := ParseReservedBytes(tc.input)
39+
if tc.expectErr {
40+
assert.Error(t, err)
41+
return
42+
}
43+
assert.NoError(t, err)
44+
assert.Equal(t, tc.want, got)
45+
})
46+
}
47+
}
48+
49+
func TestNewReservedBytes(t *testing.T) {
50+
type testCase struct {
51+
name string
52+
input uint64
53+
want []byte
54+
expectErr bool
55+
}
56+
testCases := []testCase{
57+
{"byte index of 0", 0, []byte{0, 0}, false},
58+
{"byte index of 2", 2, []byte{2, 0}, false},
59+
{"byte index of 4", 4, []byte{4, 0}, false},
60+
{"byte index of 8", 8, []byte{8, 0}, false},
61+
{"byte index of 16", 16, []byte{16, 0}, false},
62+
{"byte index of 32", 32, []byte{32, 0}, false},
63+
{"byte index of 64", 64, []byte{64, 0}, false},
64+
{"byte index of 128", 128, []byte{128, 1}, false},
65+
{"byte index of 256", 256, []byte{128, 2}, false},
66+
{"byte index of 511", 511, []byte{255, 3}, false},
67+
68+
// error cases
69+
{"byte index of 512 is equal to share size", 512, []byte{}, true},
70+
{"byte index of 1000 is greater than share size", 1000, []byte{}, true},
71+
}
72+
73+
for _, tc := range testCases {
74+
t.Run(tc.name, func(t *testing.T) {
75+
got, err := NewReservedBytes(tc.input)
76+
if tc.expectErr {
77+
assert.Error(t, err)
78+
return
79+
}
80+
assert.NoError(t, err)
81+
assert.Equal(t, tc.want, got)
82+
})
83+
}
84+
}

0 commit comments

Comments
 (0)