@@ -14,6 +14,7 @@ import (
1414 "github.com/scroll-tech/go-ethereum/params"
1515 "github.com/stretchr/testify/assert"
1616 "github.com/stretchr/testify/require"
17+ "gorm.io/gorm"
1718
1819 "scroll-tech/common/database"
1920 "scroll-tech/common/types"
@@ -295,41 +296,64 @@ func testCommitBatchAndFinalizeBundleCodecV7(t *testing.T) {
295296 }, encoding .CodecV7 , chainConfig , db , nil )
296297
297298 bup := watcher .NewBundleProposer (context .Background (), & config.BundleProposerConfig {
298- MaxBatchNumPerBundle : 1000000 ,
299+ MaxBatchNumPerBundle : 2 ,
299300 BundleTimeoutSec : 300 ,
300301 }, encoding .CodecV7 , chainConfig , db , nil )
301302
302303 l2BlockOrm := orm .NewL2Block (db )
303304 batchOrm := orm .NewBatch (db )
304305 bundleOrm := orm .NewBundle (db )
305306
306- fmt .Println ("insert first 5 blocks ------------------------" )
307- err = l2BlockOrm .InsertL2Blocks (context .Background (), blocks [:5 ])
308- require .NoError (t , err )
309- batch1ExpectedLastL1MessageQueueHash , err := encoding .MessageQueueV2ApplyL1MessagesFromBlocks (common.Hash {}, blocks [:5 ])
310- require .NoError (t , err )
307+ var batch1ExpectedLastL1MessageQueueHash common.Hash
308+ {
309+ fmt .Println ("insert first 5 blocks ------------------------" )
310+ err = l2BlockOrm .InsertL2Blocks (context .Background (), blocks [:5 ])
311+ require .NoError (t , err )
312+ batch1ExpectedLastL1MessageQueueHash , err = encoding .MessageQueueV2ApplyL1MessagesFromBlocks (common.Hash {}, blocks [:5 ])
313+ require .NoError (t , err )
311314
312- cp .TryProposeChunk ()
313- bap .TryProposeBatch ()
315+ cp .TryProposeChunk ()
316+ bap .TryProposeBatch ()
317+ }
314318
315- fmt .Println ("insert last 5 blocks ------------------------" )
316- err = l2BlockOrm .InsertL2Blocks (context .Background (), blocks [5 :])
317- require .NoError (t , err )
318- batch2ExpectedLastL1MessageQueueHash , err := encoding .MessageQueueV2ApplyL1MessagesFromBlocks (batch1ExpectedLastL1MessageQueueHash , blocks [5 :])
319- require .NoError (t , err )
319+ var batch2ExpectedLastL1MessageQueueHash common.Hash
320+ {
321+ fmt .Println ("insert next 3 blocks ------------------------" )
322+ err = l2BlockOrm .InsertL2Blocks (context .Background (), blocks [5 :8 ])
323+ for _ , block := range blocks [5 :8 ] {
324+ fmt .Println ("insert[5:8] block number: " , block .Header .Number , block .Header .Hash ())
325+ }
326+ require .NoError (t , err )
327+ batch2ExpectedLastL1MessageQueueHash , err = encoding .MessageQueueV2ApplyL1MessagesFromBlocks (batch1ExpectedLastL1MessageQueueHash , blocks [5 :8 ])
328+ require .NoError (t , err )
320329
321- cp .TryProposeChunk ()
322- bap .TryProposeBatch ()
330+ cp .TryProposeChunk ()
331+ bap .TryProposeBatch ()
332+ }
323333
324- bup .TryProposeBundle () // The proposed bundle contains two batches when codec version is codecv3.
334+ var batch3ExpectedLastL1MessageQueueHash common.Hash
335+ {
336+ fmt .Println ("insert last 2 blocks ------------------------" )
337+ err = l2BlockOrm .InsertL2Blocks (context .Background (), blocks [8 :])
338+ for _ , block := range blocks [8 :] {
339+ fmt .Println ("insert[:8] block number: " , block .Header .Number , block .Header .Hash ())
340+ }
341+ require .NoError (t , err )
342+ batch3ExpectedLastL1MessageQueueHash , err = encoding .MessageQueueV2ApplyL1MessagesFromBlocks (batch2ExpectedLastL1MessageQueueHash , blocks [8 :])
343+ require .NoError (t , err )
344+
345+ cp .TryProposeChunk ()
346+ bap .TryProposeBatch ()
347+ }
325348
349+ var batches []* orm.Batch
326350 // make sure that batches are created as expected
327351 require .Eventually (t , func () bool {
328- batches , getErr : = batchOrm .GetBatches (context .Background (), map [string ]interface {}{}, nil , 0 )
329- if getErr != nil {
352+ batches , err = batchOrm .GetBatches (context .Background (), map [string ]interface {}{}, nil , 0 )
353+ if err != nil {
330354 return false
331355 }
332- if len (batches ) != 3 {
356+ if len (batches ) != 4 {
333357 return false
334358 }
335359
@@ -340,46 +364,91 @@ func testCommitBatchAndFinalizeBundleCodecV7(t *testing.T) {
340364 require .Equal (t , batch1ExpectedLastL1MessageQueueHash , common .HexToHash (batches [1 ].PostL1MessageQueueHash ))
341365 require .Equal (t , batch1ExpectedLastL1MessageQueueHash , common .HexToHash (batches [2 ].PrevL1MessageQueueHash ))
342366 require .Equal (t , batch2ExpectedLastL1MessageQueueHash , common .HexToHash (batches [2 ].PostL1MessageQueueHash ))
367+ require .Equal (t , batch2ExpectedLastL1MessageQueueHash , common .HexToHash (batches [3 ].PrevL1MessageQueueHash ))
368+ require .Equal (t , batch3ExpectedLastL1MessageQueueHash , common .HexToHash (batches [3 ].PostL1MessageQueueHash ))
343369
344370 return true
345371 }, 30 * time .Second , time .Second )
346372
347- // simulate proof generation -> all batches and bundle are verified
373+ // Nothing should happen since no batch is committed yet.
348374 {
349- batchProof := & message. OpenVMBatchProof {}
350- batches , err := batchOrm . GetBatches (context .Background (), map [string ]interface {}{}, nil , 0 )
375+ bup . TryProposeBundle ()
376+ bundles , err := bundleOrm . GetBundles (context .Background (), map [string ]interface {}{}, nil , 0 )
351377 require .NoError (t , err )
352- batches = batches [1 :]
353- for _ , batch := range batches {
354- err = batchOrm .UpdateProofByHash (context .Background (), batch .Hash , batchProof , 100 )
355- require .NoError (t , err )
356- err = batchOrm .UpdateProvingStatus (context .Background (), batch .Hash , types .ProvingTaskVerified )
357- require .NoError (t , err )
378+ require .Len (t , bundles , 0 )
379+ }
380+
381+ // simulate batches 2 and 3 being submitted together in a single transaction
382+ err = db .Transaction (func (dbTX * gorm.DB ) error {
383+ if err = batchOrm .UpdateCommitTxHashAndRollupStatus (context .Background (), batches [1 ].Hash , "0xdefdef" , types .RollupCommitted , dbTX ); err != nil {
384+ return fmt .Errorf ("UpdateCommitTxHashAndRollupStatus failed for batch %d: %s, err %v" , batches [1 ].Index , batches [1 ].Hash , err )
385+ }
386+
387+ for _ , batch := range batches [2 :] {
388+ if err = batchOrm .UpdateCommitTxHashAndRollupStatus (context .Background (), batch .Hash , "0xabcabc" , types .RollupCommitted , dbTX ); err != nil {
389+ return fmt .Errorf ("UpdateCommitTxHashAndRollupStatus failed for batch %d: %s, err %v" , batch .Index , batch .Hash , err )
390+ }
358391 }
359392
360- bundleProof := & message.OpenVMBundleProof {}
393+ return nil
394+ })
395+ require .NoError (t , err )
396+
397+ // We only allow bundles up to 2 batches. We should have 2 bundles:
398+ // 1. batch 1 -> because it was committed by itself and the next set of batches could not fit the bundle
399+ // 2. batch 2 and 3 -> because they were committed together in a single transaction
400+ {
401+ // need to propose 2 times to get 2 bundles with all batches
402+ bup .TryProposeBundle ()
403+ bup .TryProposeBundle ()
404+
361405 bundles , err := bundleOrm .GetBundles (context .Background (), map [string ]interface {}{}, nil , 0 )
362406 require .NoError (t , err )
363- for _ , bundle := range bundles {
364- err = bundleOrm .UpdateProofAndProvingStatusByHash (context .Background (), bundle .Hash , bundleProof , types .ProvingTaskVerified , 100 )
365- require .NoError (t , err )
366- }
407+ require .Len (t , bundles , 2 )
408+
409+ require .Equal (t , bundles [0 ].StartBatchIndex , batches [1 ].Index )
410+ require .Equal (t , bundles [0 ].EndBatchIndex , batches [1 ].Index )
411+ require .Equal (t , bundles [0 ].StartBatchHash , batches [1 ].Hash )
412+ require .Equal (t , bundles [0 ].EndBatchHash , batches [1 ].Hash )
413+
414+ require .Equal (t , bundles [1 ].StartBatchIndex , batches [2 ].Index )
415+ require .Equal (t , bundles [1 ].EndBatchIndex , batches [3 ].Index )
416+ require .Equal (t , bundles [1 ].StartBatchHash , batches [2 ].Hash )
417+ require .Equal (t , bundles [1 ].EndBatchHash , batches [3 ].Hash )
367418 }
368419
369- //return
370- // TODO: assert that batches have been submitted together in a single transaction after contract ABI is updated
371- //for _, batch := range batches {
372- // fmt.Println("batch hash: ", batch.Hash, batch.Index, batch.RollupStatus)
373- // //if types.RollupCommitted != types.RollupStatus(batch.RollupStatus) {
374- // // return false
375- // //}
420+ return
421+ // simulate proof generation -> all batches and bundle are verified
422+ //{
423+ // batchProof := &message.OpenVMBatchProof{}
424+ // batches, err := batchOrm.GetBatches(context.Background(), map[string]interface{}{}, nil, 0)
425+ // require.NoError(t, err)
426+ // batches = batches[1:]
427+ // for _, batch := range batches {
428+ // err = batchOrm.UpdateProofByHash(context.Background(), batch.Hash, batchProof, 100)
429+ // require.NoError(t, err)
430+ // err = batchOrm.UpdateProvingStatus(context.Background(), batch.Hash, types.ProvingTaskVerified)
431+ // require.NoError(t, err)
432+ // }
433+ //
434+ // bundleProof := &message.OpenVMBundleProof{}
435+ // bundles, err := bundleOrm.GetBundles(context.Background(), map[string]interface{}{}, nil, 0)
436+ // require.NoError(t, err)
437+ // for _, bundle := range bundles {
438+ // err = bundleOrm.UpdateProofAndProvingStatusByHash(context.Background(), bundle.Hash, bundleProof, types.ProvingTaskVerified, 100)
439+ // require.NoError(t, err)
440+ // }
376441 //}
442+
443+ // TODO: assert that batches have been submitted together in a single transaction after contract ABI is updated
444+
377445 //l2Relayer.ProcessPendingBatches()
378- //
446+ //l2Relayer.ProcessPendingBundles()
447+
379448 //assert.Eventually(t, func() bool {
380449 // l2Relayer.ProcessPendingBundles()
381450 //
382- // batches, err : = batchOrm.GetBatches(context.Background(), map[string]interface{}{}, nil, 0)
451+ // batches, err = batchOrm.GetBatches(context.Background(), map[string]interface{}{}, nil, 0)
383452 // assert.NoError(t, err)
384453 // assert.Len(t, batches, 3)
385454 // batches = batches[1:]
@@ -415,5 +484,5 @@ func testCommitBatchAndFinalizeBundleCodecV7(t *testing.T) {
415484 // }
416485 //
417486 // return true
418- //}, 30 *time.Second, time.Second)
487+ //}, 10 *time.Second, time.Second)
419488}
0 commit comments