@@ -2,6 +2,9 @@ package test
22
33import (
44 "context"
5+ "crypto/rand"
6+ "fmt"
7+ "io"
58 "math"
69 "testing"
710 "time"
@@ -14,7 +17,8 @@ import (
1417func TestTransactionQueue_AddTransaction (t * testing.T ) {
1518 queue := NewTransactionQueue ()
1619
17- tx1 := []byte ("transaction_1" )
20+ tx1 , err := GenerateSecureRandomBytes (32 )
21+ assert .NoError (t , err )
1822 queue .AddTransaction (tx1 )
1923
2024 // Check that the transaction was added
@@ -27,8 +31,10 @@ func TestTransactionQueue_GetNextBatch(t *testing.T) {
2731 queue := NewTransactionQueue ()
2832
2933 // 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 )
3238 queue .AddTransaction (tx1 )
3339 queue .AddTransaction (tx2 )
3440
@@ -46,7 +52,8 @@ func TestTransactionQueue_GetNextBatch(t *testing.T) {
4652func TestDummySequencer_SubmitRollupTransaction (t * testing.T ) {
4753 // Define a test rollup ID and transaction
4854 rollupId := []byte ("test_rollup_id" )
49- tx := []byte ("test_transaction" )
55+ tx , err := GenerateSecureRandomBytes (32 )
56+ assert .NoError (t , err )
5057 sequencer := NewDummySequencer (rollupId )
5158
5259 // Submit a transaction
@@ -92,9 +99,12 @@ func TestDummySequencer_SubmitEmptyTransaction(t *testing.T) {
9299func TestDummySequencer_SubmitMultipleTransactions (t * testing.T ) {
93100 // Define a test rollup ID and multiple transactions
94101 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 )
98108 sequencer := NewDummySequencer (rollupId )
99109
100110 // Submit multiple transactions
@@ -111,7 +121,7 @@ func TestDummySequencer_SubmitMultipleTransactions(t *testing.T) {
111121 Tx : tx3 ,
112122 }
113123
114- _ , err : = sequencer .SubmitRollupTransaction (context .Background (), req1 )
124+ _ , err = sequencer .SubmitRollupTransaction (context .Background (), req1 )
115125 assert .NoError (t , err )
116126 _ , err = sequencer .SubmitRollupTransaction (context .Background (), req2 )
117127 assert .NoError (t , err )
@@ -129,13 +139,14 @@ func TestDummySequencer_SubmitMultipleTransactions(t *testing.T) {
129139func TestDummySequencer_GetNextBatch (t * testing.T ) {
130140 // Add a transaction to the queue
131141 rollupId := []byte ("test_rollup_id" )
132- tx := []byte ("test_transaction" )
142+ tx , err := GenerateSecureRandomBytes (32 )
143+ assert .NoError (t , err )
133144 sequencer := NewDummySequencer (rollupId )
134145 req := sequencing.SubmitRollupTransactionRequest {
135146 RollupId : rollupId ,
136147 Tx : tx ,
137148 }
138- _ , err : = sequencer .SubmitRollupTransaction (context .Background (), req )
149+ _ , err = sequencer .SubmitRollupTransaction (context .Background (), req )
139150 assert .NoError (t , err )
140151
141152 // Retrieve the next batch
@@ -178,12 +189,13 @@ func TestDummySequencer_GetNextBatch_LastBatchHashMismatch(t *testing.T) {
178189 // Submit a transaction
179190 rollupId := []byte ("test_rollup_id" )
180191 sequencer := NewDummySequencer (rollupId )
181- tx := []byte ("test_transaction" )
192+ tx , err := GenerateSecureRandomBytes (32 )
193+ assert .NoError (t , err )
182194 req := sequencing.SubmitRollupTransactionRequest {
183195 RollupId : rollupId ,
184196 Tx : tx ,
185197 }
186- _ , err : = sequencer .SubmitRollupTransaction (context .Background (), req )
198+ _ , err = sequencer .SubmitRollupTransaction (context .Background (), req )
187199 assert .NoError (t , err )
188200
189201 // Retrieve the next batch
@@ -195,17 +207,20 @@ func TestDummySequencer_GetNextBatch_LastBatchHashMismatch(t *testing.T) {
195207
196208 // Assert that the batch hash mismatch error is returned
197209 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" )
199211}
200212
201213// Test retrieving a batch with maxBytes limit
202214func TestDummySequencer_GetNextBatch_MaxBytesLimit (t * testing.T ) {
203215 // Define a test rollup ID and multiple transactions
204216 rollupId := []byte ("test_rollup_id" )
205217 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 )
209224
210225 // Submit multiple transactions
211226 req1 := sequencing.SubmitRollupTransactionRequest {
@@ -221,7 +236,7 @@ func TestDummySequencer_GetNextBatch_MaxBytesLimit(t *testing.T) {
221236 Tx : tx3 ,
222237 }
223238
224- _ , err : = sequencer .SubmitRollupTransaction (context .Background (), req1 )
239+ _ , err = sequencer .SubmitRollupTransaction (context .Background (), req1 )
225240 assert .NoError (t , err )
226241 _ , err = sequencer .SubmitRollupTransaction (context .Background (), req2 )
227242 assert .NoError (t , err )
@@ -267,12 +282,13 @@ func TestDummySequencer_VerifyBatch(t *testing.T) {
267282 // Add and retrieve a batch
268283 rollupId := []byte ("test_rollup_id" )
269284 sequencer := NewDummySequencer (rollupId )
270- tx := []byte ("test_transaction" )
285+ tx , err := GenerateSecureRandomBytes (32 )
286+ assert .NoError (t , err )
271287 req := sequencing.SubmitRollupTransactionRequest {
272288 RollupId : rollupId ,
273289 Tx : tx ,
274290 }
275- _ , err : = sequencer .SubmitRollupTransaction (context .Background (), req )
291+ _ , err = sequencer .SubmitRollupTransaction (context .Background (), req )
276292 assert .NoError (t , err )
277293
278294 // Get the next batch to generate batch hash
@@ -320,8 +336,10 @@ func TestDummySequencer_VerifyBatchWithMultipleTransactions(t *testing.T) {
320336 // Define a test rollup ID and multiple transactions
321337 rollupId := []byte ("test_rollup_id" )
322338 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 )
325343
326344 // Submit multiple transactions
327345 req1 := sequencing.SubmitRollupTransactionRequest {
@@ -333,7 +351,7 @@ func TestDummySequencer_VerifyBatchWithMultipleTransactions(t *testing.T) {
333351 Tx : tx2 ,
334352 }
335353
336- _ , err : = sequencer .SubmitRollupTransaction (context .Background (), req1 )
354+ _ , err = sequencer .SubmitRollupTransaction (context .Background (), req1 )
337355 assert .NoError (t , err )
338356 _ , err = sequencer .SubmitRollupTransaction (context .Background (), req2 )
339357 assert .NoError (t , err )
@@ -375,3 +393,16 @@ func TestDummySequencer_VerifyBatch_NotFound(t *testing.T) {
375393 assert .NoError (t , err )
376394 assert .False (t , verifyResp .Status )
377395}
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