@@ -2,6 +2,9 @@ package test
2
2
3
3
import (
4
4
"context"
5
+ "crypto/rand"
6
+ "fmt"
7
+ "io"
5
8
"math"
6
9
"testing"
7
10
"time"
@@ -14,7 +17,8 @@ import (
14
17
func TestTransactionQueue_AddTransaction (t * testing.T ) {
15
18
queue := NewTransactionQueue ()
16
19
17
- tx1 := []byte ("transaction_1" )
20
+ tx1 , err := GenerateSecureRandomBytes (32 )
21
+ assert .NoError (t , err )
18
22
queue .AddTransaction (tx1 )
19
23
20
24
// Check that the transaction was added
@@ -27,8 +31,10 @@ func TestTransactionQueue_GetNextBatch(t *testing.T) {
27
31
queue := NewTransactionQueue ()
28
32
29
33
// Add multiple transactions
30
- tx1 := []byte ("transaction_1" )
31
- tx2 := []byte ("transaction_2" )
34
+ tx1 , err := GenerateSecureRandomBytes (32 )
35
+ assert .NoError (t , err )
36
+ tx2 , err := GenerateSecureRandomBytes (32 )
37
+ assert .NoError (t , err )
32
38
queue .AddTransaction (tx1 )
33
39
queue .AddTransaction (tx2 )
34
40
@@ -46,7 +52,8 @@ func TestTransactionQueue_GetNextBatch(t *testing.T) {
46
52
func TestDummySequencer_SubmitRollupTransaction (t * testing.T ) {
47
53
// Define a test rollup ID and transaction
48
54
rollupId := []byte ("test_rollup_id" )
49
- tx := []byte ("test_transaction" )
55
+ tx , err := GenerateSecureRandomBytes (32 )
56
+ assert .NoError (t , err )
50
57
sequencer := NewDummySequencer (rollupId )
51
58
52
59
// Submit a transaction
@@ -92,9 +99,12 @@ func TestDummySequencer_SubmitEmptyTransaction(t *testing.T) {
92
99
func TestDummySequencer_SubmitMultipleTransactions (t * testing.T ) {
93
100
// Define a test rollup ID and multiple transactions
94
101
rollupId := []byte ("test_rollup_id" )
95
- tx1 := []byte ("transaction_1" )
96
- tx2 := []byte ("transaction_2" )
97
- tx3 := []byte ("transaction_3" )
102
+ tx1 , err := GenerateSecureRandomBytes (32 )
103
+ assert .NoError (t , err )
104
+ tx2 , err := GenerateSecureRandomBytes (32 )
105
+ assert .NoError (t , err )
106
+ tx3 , err := GenerateSecureRandomBytes (32 )
107
+ assert .NoError (t , err )
98
108
sequencer := NewDummySequencer (rollupId )
99
109
100
110
// Submit multiple transactions
@@ -111,7 +121,7 @@ func TestDummySequencer_SubmitMultipleTransactions(t *testing.T) {
111
121
Tx : tx3 ,
112
122
}
113
123
114
- _ , err : = sequencer .SubmitRollupTransaction (context .Background (), req1 )
124
+ _ , err = sequencer .SubmitRollupTransaction (context .Background (), req1 )
115
125
assert .NoError (t , err )
116
126
_ , err = sequencer .SubmitRollupTransaction (context .Background (), req2 )
117
127
assert .NoError (t , err )
@@ -129,13 +139,14 @@ func TestDummySequencer_SubmitMultipleTransactions(t *testing.T) {
129
139
func TestDummySequencer_GetNextBatch (t * testing.T ) {
130
140
// Add a transaction to the queue
131
141
rollupId := []byte ("test_rollup_id" )
132
- tx := []byte ("test_transaction" )
142
+ tx , err := GenerateSecureRandomBytes (32 )
143
+ assert .NoError (t , err )
133
144
sequencer := NewDummySequencer (rollupId )
134
145
req := sequencing.SubmitRollupTransactionRequest {
135
146
RollupId : rollupId ,
136
147
Tx : tx ,
137
148
}
138
- _ , err : = sequencer .SubmitRollupTransaction (context .Background (), req )
149
+ _ , err = sequencer .SubmitRollupTransaction (context .Background (), req )
139
150
assert .NoError (t , err )
140
151
141
152
// Retrieve the next batch
@@ -178,12 +189,13 @@ func TestDummySequencer_GetNextBatch_LastBatchHashMismatch(t *testing.T) {
178
189
// Submit a transaction
179
190
rollupId := []byte ("test_rollup_id" )
180
191
sequencer := NewDummySequencer (rollupId )
181
- tx := []byte ("test_transaction" )
192
+ tx , err := GenerateSecureRandomBytes (32 )
193
+ assert .NoError (t , err )
182
194
req := sequencing.SubmitRollupTransactionRequest {
183
195
RollupId : rollupId ,
184
196
Tx : tx ,
185
197
}
186
- _ , err : = sequencer .SubmitRollupTransaction (context .Background (), req )
198
+ _ , err = sequencer .SubmitRollupTransaction (context .Background (), req )
187
199
assert .NoError (t , err )
188
200
189
201
// Retrieve the next batch
@@ -195,17 +207,20 @@ func TestDummySequencer_GetNextBatch_LastBatchHashMismatch(t *testing.T) {
195
207
196
208
// Assert that the batch hash mismatch error is returned
197
209
assert .Error (t , err )
198
- assert .Equal (t , "lastBatch is supposed to be nil" , err . Error () )
210
+ assert .ErrorContains (t , err , "batch hash mismatch" , "unexpected error message" )
199
211
}
200
212
201
213
// Test retrieving a batch with maxBytes limit
202
214
func TestDummySequencer_GetNextBatch_MaxBytesLimit (t * testing.T ) {
203
215
// Define a test rollup ID and multiple transactions
204
216
rollupId := []byte ("test_rollup_id" )
205
217
sequencer := NewDummySequencer (rollupId )
206
- tx1 := []byte ("transaction_1" )
207
- tx2 := []byte ("transaction_2" )
208
- tx3 := []byte ("transaction_3" )
218
+ tx1 , err := GenerateSecureRandomBytes (32 )
219
+ assert .NoError (t , err )
220
+ tx2 , err := GenerateSecureRandomBytes (32 )
221
+ assert .NoError (t , err )
222
+ tx3 , err := GenerateSecureRandomBytes (32 )
223
+ assert .NoError (t , err )
209
224
210
225
// Submit multiple transactions
211
226
req1 := sequencing.SubmitRollupTransactionRequest {
@@ -221,7 +236,7 @@ func TestDummySequencer_GetNextBatch_MaxBytesLimit(t *testing.T) {
221
236
Tx : tx3 ,
222
237
}
223
238
224
- _ , err : = sequencer .SubmitRollupTransaction (context .Background (), req1 )
239
+ _ , err = sequencer .SubmitRollupTransaction (context .Background (), req1 )
225
240
assert .NoError (t , err )
226
241
_ , err = sequencer .SubmitRollupTransaction (context .Background (), req2 )
227
242
assert .NoError (t , err )
@@ -267,12 +282,13 @@ func TestDummySequencer_VerifyBatch(t *testing.T) {
267
282
// Add and retrieve a batch
268
283
rollupId := []byte ("test_rollup_id" )
269
284
sequencer := NewDummySequencer (rollupId )
270
- tx := []byte ("test_transaction" )
285
+ tx , err := GenerateSecureRandomBytes (32 )
286
+ assert .NoError (t , err )
271
287
req := sequencing.SubmitRollupTransactionRequest {
272
288
RollupId : rollupId ,
273
289
Tx : tx ,
274
290
}
275
- _ , err : = sequencer .SubmitRollupTransaction (context .Background (), req )
291
+ _ , err = sequencer .SubmitRollupTransaction (context .Background (), req )
276
292
assert .NoError (t , err )
277
293
278
294
// Get the next batch to generate batch hash
@@ -320,8 +336,10 @@ func TestDummySequencer_VerifyBatchWithMultipleTransactions(t *testing.T) {
320
336
// Define a test rollup ID and multiple transactions
321
337
rollupId := []byte ("test_rollup_id" )
322
338
sequencer := NewDummySequencer (rollupId )
323
- tx1 := []byte ("transaction_1" )
324
- tx2 := []byte ("transaction_2" )
339
+ tx1 , err := GenerateSecureRandomBytes (32 )
340
+ assert .NoError (t , err )
341
+ tx2 , err := GenerateSecureRandomBytes (32 )
342
+ assert .NoError (t , err )
325
343
326
344
// Submit multiple transactions
327
345
req1 := sequencing.SubmitRollupTransactionRequest {
@@ -333,7 +351,7 @@ func TestDummySequencer_VerifyBatchWithMultipleTransactions(t *testing.T) {
333
351
Tx : tx2 ,
334
352
}
335
353
336
- _ , err : = sequencer .SubmitRollupTransaction (context .Background (), req1 )
354
+ _ , err = sequencer .SubmitRollupTransaction (context .Background (), req1 )
337
355
assert .NoError (t , err )
338
356
_ , err = sequencer .SubmitRollupTransaction (context .Background (), req2 )
339
357
assert .NoError (t , err )
@@ -375,3 +393,16 @@ func TestDummySequencer_VerifyBatch_NotFound(t *testing.T) {
375
393
assert .NoError (t , err )
376
394
assert .False (t , verifyResp .Status )
377
395
}
396
+
397
+ // GenerateSecureRandomBytes generates cryptographically secure random bytes of the given length.
398
+ func GenerateSecureRandomBytes (length int ) ([]byte , error ) {
399
+ if length <= 0 {
400
+ return nil , fmt .Errorf ("invalid length: %d, must be greater than 0" , length )
401
+ }
402
+
403
+ buf := make ([]byte , length )
404
+ if _ , err := io .ReadFull (rand .Reader , buf ); err != nil {
405
+ return nil , fmt .Errorf ("failed to generate random bytes: %w" , err )
406
+ }
407
+ return buf , nil
408
+ }
0 commit comments