@@ -49,11 +49,13 @@ mod test {
49
49
models, operations:: DatabaseOperations , test_utils:: setup_test_db,
50
50
DatabaseConnectionProvider ,
51
51
} ;
52
+ use alloy_primitives:: B256 ;
53
+ use std:: sync:: Arc ;
52
54
53
55
use arbitrary:: { Arbitrary , Unstructured } ;
54
56
use futures:: StreamExt ;
55
57
use rand:: Rng ;
56
- use rollup_node_primitives:: { BatchCommitData , L1MessageWithBlockNumber } ;
58
+ use rollup_node_primitives:: { BatchCommitData , BatchInfo , BlockInfo , L1MessageWithBlockNumber } ;
57
59
use sea_orm:: { ColumnTrait , EntityTrait , QueryFilter } ;
58
60
59
61
#[ tokio:: test]
@@ -136,6 +138,113 @@ mod test {
136
138
assert ! ( data. is_some( ) ) ;
137
139
}
138
140
141
+ #[ tokio:: test]
142
+ async fn test_database_batch_to_block_exists ( ) {
143
+ // Set up the test database.
144
+ let db = setup_test_db ( ) . await ;
145
+
146
+ // Generate unstructured bytes.
147
+ let mut bytes = [ 0u8 ; 1024 ] ;
148
+ rand:: rng ( ) . fill ( bytes. as_mut_slice ( ) ) ;
149
+ let mut u = Unstructured :: new ( & bytes) ;
150
+
151
+ // Generate randoms BatchInfo and BlockInfo with increasing block numbers.
152
+ let mut block_number = 100 ;
153
+ let data = BatchCommitData { index : 100 , ..Arbitrary :: arbitrary ( & mut u) . unwrap ( ) } ;
154
+ let batch_info = data. clone ( ) . into ( ) ;
155
+ db. insert_batch ( data) . await . unwrap ( ) ;
156
+
157
+ for _ in 0 ..10 {
158
+ let block_info =
159
+ BlockInfo { number : block_number, hash : B256 :: arbitrary ( & mut u) . unwrap ( ) } ;
160
+ db. insert_batch_to_block ( batch_info, block_info) . await . unwrap ( ) ;
161
+ block_number += 1 ;
162
+ }
163
+
164
+ // Fetch the highest block for the batch and verify number.
165
+ let highest_block_info =
166
+ db. get_highest_block_for_batch ( batch_info. hash ) . await . unwrap ( ) . unwrap ( ) ;
167
+ assert_eq ! ( highest_block_info. number, block_number - 1 ) ;
168
+ }
169
+
170
+ #[ tokio:: test]
171
+ async fn test_database_batch_to_block_missing ( ) {
172
+ // Set up the test database.
173
+ let db = setup_test_db ( ) . await ;
174
+
175
+ // Generate unstructured bytes.
176
+ let mut bytes = [ 0u8 ; 1024 ] ;
177
+ rand:: rng ( ) . fill ( bytes. as_mut_slice ( ) ) ;
178
+ let mut u = Unstructured :: new ( & bytes) ;
179
+
180
+ // Generate randoms BatchInfo and BlockInfo with increasing block numbers.
181
+ let mut block_number = 100 ;
182
+ let first_batch = BatchCommitData { index : 100 , ..Arbitrary :: arbitrary ( & mut u) . unwrap ( ) } ;
183
+ let first_batch_info = first_batch. clone ( ) . into ( ) ;
184
+
185
+ let second_batch = BatchCommitData { index : 250 , ..Arbitrary :: arbitrary ( & mut u) . unwrap ( ) } ;
186
+ let second_batch_info: BatchInfo = second_batch. clone ( ) . into ( ) ;
187
+
188
+ db. insert_batch ( first_batch) . await . unwrap ( ) ;
189
+ db. insert_batch ( second_batch) . await . unwrap ( ) ;
190
+
191
+ for _ in 0 ..10 {
192
+ let block_info =
193
+ BlockInfo { number : block_number, hash : B256 :: arbitrary ( & mut u) . unwrap ( ) } ;
194
+ db. insert_batch_to_block ( first_batch_info, block_info) . await . unwrap ( ) ;
195
+ block_number += 1 ;
196
+ }
197
+
198
+ // Fetch the highest block for the batch and verify number.
199
+ let highest_block_info =
200
+ db. get_highest_block_for_batch ( second_batch_info. hash ) . await . unwrap ( ) . unwrap ( ) ;
201
+ assert_eq ! ( highest_block_info. number, block_number - 1 ) ;
202
+ }
203
+
204
+ #[ tokio:: test]
205
+ async fn test_database_finalized_batch_hash_at_height ( ) {
206
+ // Set up the test database.
207
+ let db = setup_test_db ( ) . await ;
208
+
209
+ // Generate unstructured bytes.
210
+ let mut bytes = [ 0u8 ; 2048 ] ;
211
+ rand:: rng ( ) . fill ( bytes. as_mut_slice ( ) ) ;
212
+ let mut u = Unstructured :: new ( & bytes) ;
213
+
214
+ // Generate randoms BatchInfoCommitData, insert in database and finalize.
215
+ let mut block_number = 100 ;
216
+ let mut batch_index = 100 ;
217
+ let mut highest_finalized_batch_hash = B256 :: ZERO ;
218
+
219
+ for _ in 0 ..20 {
220
+ let data = BatchCommitData {
221
+ index : batch_index,
222
+ calldata : Arc :: new ( vec ! [ ] . into ( ) ) ,
223
+ ..Arbitrary :: arbitrary ( & mut u) . unwrap ( )
224
+ } ;
225
+ let hash = data. hash ;
226
+ db. insert_batch ( data) . await . unwrap ( ) ;
227
+
228
+ // save batch hash finalized at block number 109.
229
+ if block_number == 109 {
230
+ highest_finalized_batch_hash = hash;
231
+ }
232
+
233
+ // Finalize batch up to block number 110.
234
+ if block_number <= 110 {
235
+ db. finalize_batch ( hash, block_number) . await . unwrap ( ) ;
236
+ }
237
+
238
+ block_number += 1 ;
239
+ batch_index += 1 ;
240
+ }
241
+
242
+ // Fetch the finalized batch for provided height and verify number.
243
+ let highest_batch_hash_from_db =
244
+ db. get_finalized_batch_hash_at_height ( 109 ) . await . unwrap ( ) . unwrap ( ) ;
245
+ assert_eq ! ( highest_finalized_batch_hash, highest_batch_hash_from_db) ;
246
+ }
247
+
139
248
#[ tokio:: test]
140
249
async fn test_database_tx ( ) {
141
250
// Setup the test database.
0 commit comments