1
1
package shares
2
2
3
3
import (
4
+ "encoding/binary"
4
5
"math/rand"
5
6
"reflect"
6
7
"testing"
7
8
8
9
"github.com/celestiaorg/celestia-app/pkg/appconsts"
9
10
"github.com/celestiaorg/nmt/namespace"
11
+ "github.com/stretchr/testify/assert"
10
12
tmrand "github.com/tendermint/tendermint/libs/rand"
11
13
"github.com/tendermint/tendermint/types"
12
14
)
@@ -41,8 +43,14 @@ func TestParseShares(t *testing.T) {
41
43
messageTwoStart := messageTwoShares [0 ]
42
44
messageTwoContinuation := messageTwoShares [1 ]
43
45
44
- invalidShare := generateRawShare (messageOneNamespace , start )
45
- invalidShare = append (invalidShare , []byte {0 }... )
46
+ invalidShare := generateRawShare (messageOneNamespace , start , 1 )
47
+ invalidShare = append (invalidShare , []byte {0 }... ) // invalidShare is now longer than the length of a valid share
48
+
49
+ largeSequenceLength := 1000 // it takes more than one share to store a sequence of 1000 bytes
50
+ oneShareWithTooLargeSequenceLength := generateRawShare (messageOneNamespace , start , uint64 (largeSequenceLength ))
51
+
52
+ shortSequenceLength := 0
53
+ oneShareWithTooShortSequenceLength := generateRawShare (messageOneNamespace , start , uint64 (shortSequenceLength ))
46
54
47
55
tests := []testCase {
48
56
{
@@ -115,12 +123,25 @@ func TestParseShares(t *testing.T) {
115
123
[]ShareSequence {},
116
124
true ,
117
125
},
126
+ {
127
+ "one share with too large sequence length" ,
128
+ [][]byte {oneShareWithTooLargeSequenceLength },
129
+ []ShareSequence {},
130
+ true ,
131
+ },
132
+ {
133
+ "one share with too short sequence length" ,
134
+ [][]byte {oneShareWithTooShortSequenceLength },
135
+ []ShareSequence {},
136
+ true ,
137
+ },
118
138
}
119
139
for _ , tt := range tests {
120
140
t .Run (tt .name , func (t * testing.T ) {
121
141
got , err := ParseShares (tt .shares )
122
- if tt .expectErr && err == nil {
123
- t .Errorf ("ParseShares() error %v, expectErr %v" , err , tt .expectErr )
142
+ if tt .expectErr {
143
+ assert .Error (t , err )
144
+ return
124
145
}
125
146
if ! reflect .DeepEqual (got , tt .want ) {
126
147
t .Errorf ("ParseShares() got %v, want %v" , got , tt .want )
@@ -129,16 +150,64 @@ func TestParseShares(t *testing.T) {
129
150
}
130
151
}
131
152
132
- func generateRawShare (namespace namespace.ID , isMessageStart bool ) (rawShare []byte ) {
133
- infoByte , _ := NewInfoByte (appconsts .ShareVersion , isMessageStart )
134
- rawData := make ([]byte , appconsts .ShareSize - len (rawShare ))
135
- rand .Read (rawData )
153
+ func Test_compactSharesNeeded (t * testing.T ) {
154
+ type testCase struct {
155
+ sequenceLength int
156
+ want int
157
+ }
158
+ testCases := []testCase {
159
+ {0 , 0 },
160
+ {1 , 1 },
161
+ {2 , 1 },
162
+ {appconsts .FirstCompactShareContentSize , 1 },
163
+ {appconsts .FirstCompactShareContentSize + 1 , 2 },
164
+ {appconsts .FirstCompactShareContentSize + appconsts .ContinuationCompactShareContentSize , 2 },
165
+ {appconsts .FirstCompactShareContentSize + appconsts .ContinuationCompactShareContentSize * 100 , 101 },
166
+ }
167
+ for _ , tc := range testCases {
168
+ got := compactSharesNeeded (tc .sequenceLength )
169
+ assert .Equal (t , tc .want , got )
170
+ }
171
+ }
172
+
173
+ func Test_sparseSharesNeeded (t * testing.T ) {
174
+ type testCase struct {
175
+ sequenceLength int
176
+ want int
177
+ }
178
+ testCases := []testCase {
179
+ {0 , 0 },
180
+ {1 , 1 },
181
+ {2 , 1 },
182
+ {appconsts .SparseShareContentSize , 1 },
183
+ {appconsts .SparseShareContentSize + 1 , 2 },
184
+ {appconsts .SparseShareContentSize * 2 , 2 },
185
+ {appconsts .SparseShareContentSize * 100 + 1 , 101 },
186
+ }
187
+ for _ , tc := range testCases {
188
+ got := sparseSharesNeeded (tc .sequenceLength )
189
+ assert .Equal (t , tc .want , got )
190
+ }
191
+ }
192
+
193
+ func generateRawShare (namespace namespace.ID , isSequenceStart bool , sequenceLength uint64 ) (rawShare []byte ) {
194
+ infoByte , _ := NewInfoByte (appconsts .ShareVersion , isSequenceStart )
195
+
196
+ sequenceLengthVarint := make ([]byte , binary .MaxVarintLen64 )
197
+ numBytesWritten := binary .PutUvarint (sequenceLengthVarint , sequenceLength )
136
198
137
199
rawShare = append (rawShare , namespace ... )
138
200
rawShare = append (rawShare , byte (infoByte ))
139
- rawShare = append (rawShare , rawData ... )
201
+ rawShare = append (rawShare , sequenceLengthVarint [:numBytesWritten ]... )
202
+
203
+ return padWithRandomBytes (rawShare )
204
+ }
140
205
141
- return rawShare
206
+ func padWithRandomBytes (partialShare Share ) (paddedShare Share ) {
207
+ paddedShare = make ([]byte , appconsts .ShareSize )
208
+ copy (paddedShare , partialShare )
209
+ rand .Read (paddedShare [len (partialShare ):])
210
+ return paddedShare
142
211
}
143
212
144
213
func generateRandomTxs (count , size int ) types.Txs {
0 commit comments