@@ -2,6 +2,7 @@ package tests
22
33import (
44 "context"
5+ "fmt"
56 "math"
67 "math/big"
78 "testing"
@@ -12,6 +13,7 @@ import (
1213 gethTypes "github.com/scroll-tech/go-ethereum/core/types"
1314 "github.com/scroll-tech/go-ethereum/params"
1415 "github.com/stretchr/testify/assert"
16+ "github.com/stretchr/testify/require"
1517
1618 "scroll-tech/common/database"
1719 "scroll-tech/common/types"
@@ -216,3 +218,216 @@ func testCommitBatchAndFinalizeBundleCodecV4(t *testing.T) {
216218 database .CloseDB (db )
217219 }
218220}
221+
222+ func testCommitBatchAndFinalizeBundleCodecV7 (t * testing.T ) {
223+ db := setupDB (t )
224+
225+ prepareContracts (t )
226+
227+ chainConfig := & params.ChainConfig {
228+ LondonBlock : big .NewInt (0 ),
229+ BernoulliBlock : big .NewInt (0 ),
230+ CurieBlock : big .NewInt (0 ),
231+ DarwinTime : new (uint64 ),
232+ DarwinV2Time : new (uint64 ),
233+ EuclidTime : new (uint64 ),
234+ EuclidV2Time : new (uint64 ),
235+ }
236+
237+ // Create L2Relayer
238+ l2Cfg := rollupApp .Config .L2Config
239+ l2Relayer , err := relayer .NewLayer2Relayer (context .Background (), l2Client , db , l2Cfg .RelayerConfig , chainConfig , true , relayer .ServiceTypeL2RollupRelayer , nil )
240+ require .NoError (t , err )
241+
242+ defer l2Relayer .StopSenders ()
243+ defer database .CloseDB (db )
244+
245+ // add some blocks to db
246+ var blocks []* encoding.Block
247+ genesis , err := l2Client .HeaderByNumber (context .Background (), big .NewInt (0 ))
248+ require .NoError (t , err )
249+
250+ var l1MessageIndex uint64 = 0
251+ parentHash := genesis .Hash ()
252+ for i := int64 (0 ); i < 10 ; i ++ {
253+ header := gethTypes.Header {
254+ Number : big .NewInt (i + 1 ),
255+ ParentHash : parentHash ,
256+ Difficulty : big .NewInt (i + 1 ),
257+ BaseFee : big .NewInt (i + 1 ),
258+ Root : common .HexToHash ("0x1" ),
259+ }
260+ fmt .Println ("block number: " , i + 1 , header .Hash (), parentHash )
261+ var transactions []* gethTypes.TransactionData
262+ if i % 2 == 0 {
263+ txs := []* gethTypes.Transaction {
264+ gethTypes .NewTx (& gethTypes.L1MessageTx {
265+ QueueIndex : l1MessageIndex ,
266+ Gas : 0 ,
267+ To : & common.Address {1 , 2 , 3 },
268+ Value : big .NewInt (10 ),
269+ Data : nil ,
270+ Sender : common.Address {1 , 2 , 3 },
271+ }),
272+ }
273+ transactions = append (transactions , encoding .TxsToTxsData (txs )... )
274+ l1MessageIndex ++
275+ }
276+
277+ blocks = append (blocks , & encoding.Block {
278+ Header : & header ,
279+ Transactions : transactions ,
280+ WithdrawRoot : common .HexToHash ("0x2" ),
281+ RowConsumption : & gethTypes.RowConsumption {},
282+ })
283+ parentHash = header .Hash ()
284+ }
285+
286+ cp := watcher .NewChunkProposer (context .Background (), & config.ChunkProposerConfig {
287+ MaxBlockNumPerChunk : 100 ,
288+ MaxTxNumPerChunk : 10000 ,
289+ MaxL1CommitGasPerChunk : 50000000000 ,
290+ MaxL1CommitCalldataSizePerChunk : 1000000 ,
291+ MaxRowConsumptionPerChunk : 1048319 ,
292+ ChunkTimeoutSec : 300 ,
293+ MaxUncompressedBatchBytesSize : math .MaxUint64 ,
294+ }, encoding .CodecV7 , chainConfig , db , nil )
295+
296+ bap := watcher .NewBatchProposer (context .Background (), & config.BatchProposerConfig {
297+ MaxL1CommitGasPerBatch : 50000000000 ,
298+ MaxL1CommitCalldataSizePerBatch : 1000000 ,
299+ BatchTimeoutSec : 300 ,
300+ MaxUncompressedBatchBytesSize : math .MaxUint64 ,
301+ }, encoding .CodecV7 , chainConfig , db , nil )
302+
303+ bup := watcher .NewBundleProposer (context .Background (), & config.BundleProposerConfig {
304+ MaxBatchNumPerBundle : 1000000 ,
305+ BundleTimeoutSec : 300 ,
306+ }, encoding .CodecV7 , chainConfig , db , nil )
307+
308+ l2BlockOrm := orm .NewL2Block (db )
309+ batchOrm := orm .NewBatch (db )
310+ bundleOrm := orm .NewBundle (db )
311+
312+ fmt .Println ("insert first 5 blocks ------------------------" )
313+ err = l2BlockOrm .InsertL2Blocks (context .Background (), blocks [:5 ])
314+ require .NoError (t , err )
315+ batch1ExpectedLastL1MessageQueueHash , err := encoding .MessageQueueV2ApplyL1MessagesFromBlocks (common.Hash {}, blocks [:5 ])
316+ require .NoError (t , err )
317+
318+ cp .TryProposeChunk ()
319+ bap .TryProposeBatch ()
320+
321+ fmt .Println ("insert last 5 blocks ------------------------" )
322+ err = l2BlockOrm .InsertL2Blocks (context .Background (), blocks [5 :])
323+ require .NoError (t , err )
324+ batch2ExpectedLastL1MessageQueueHash , err := encoding .MessageQueueV2ApplyL1MessagesFromBlocks (batch1ExpectedLastL1MessageQueueHash , blocks [5 :])
325+ require .NoError (t , err )
326+
327+ cp .TryProposeChunk ()
328+ bap .TryProposeBatch ()
329+
330+ bup .TryProposeBundle () // The proposed bundle contains two batches when codec version is codecv3.
331+
332+ // make sure that batches are created as expected
333+ require .Eventually (t , func () bool {
334+ batches , getErr := batchOrm .GetBatches (context .Background (), map [string ]interface {}{}, nil , 0 )
335+ if getErr != nil {
336+ return false
337+ }
338+ if len (batches ) != 3 {
339+ return false
340+ }
341+
342+ // batches[0] is the genesis batch, no need to check
343+
344+ // assert correctness of L1 message queue hashes
345+ require .Equal (t , common.Hash {}, common .HexToHash (batches [1 ].InitialL1MessageQueueHash ))
346+ require .Equal (t , batch1ExpectedLastL1MessageQueueHash , common .HexToHash (batches [1 ].LastL1MessageQueueHash ))
347+ require .Equal (t , batch1ExpectedLastL1MessageQueueHash , common .HexToHash (batches [2 ].InitialL1MessageQueueHash ))
348+ require .Equal (t , batch2ExpectedLastL1MessageQueueHash , common .HexToHash (batches [2 ].LastL1MessageQueueHash ))
349+
350+ return true
351+ }, 30 * time .Second , time .Second )
352+
353+ // simulate proof generation -> all batches and bundle are verified
354+ {
355+ batchProof := & message.BatchProof {
356+ Proof : []byte {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 },
357+ Instances : []byte {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 },
358+ Vk : []byte {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 },
359+ }
360+ batches , err := batchOrm .GetBatches (context .Background (), map [string ]interface {}{}, nil , 0 )
361+ require .NoError (t , err )
362+ batches = batches [1 :]
363+ for _ , batch := range batches {
364+ err = batchOrm .UpdateProofByHash (context .Background (), batch .Hash , batchProof , 100 )
365+ require .NoError (t , err )
366+ err = batchOrm .UpdateProvingStatus (context .Background (), batch .Hash , types .ProvingTaskVerified )
367+ require .NoError (t , err )
368+ }
369+
370+ bundleProof := & message.BundleProof {
371+ Proof : []byte {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 },
372+ Instances : []byte {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 },
373+ Vk : []byte {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 },
374+ }
375+ bundles , err := bundleOrm .GetBundles (context .Background (), map [string ]interface {}{}, nil , 0 )
376+ require .NoError (t , err )
377+ for _ , bundle := range bundles {
378+ err = bundleOrm .UpdateProofAndProvingStatusByHash (context .Background (), bundle .Hash , bundleProof , types .ProvingTaskVerified , 100 )
379+ require .NoError (t , err )
380+ }
381+ }
382+
383+ //return
384+ // TODO: assert that batches have been submitted together in a single transaction after contract ABI is updated
385+ //for _, batch := range batches {
386+ // fmt.Println("batch hash: ", batch.Hash, batch.Index, batch.RollupStatus)
387+ // //if types.RollupCommitted != types.RollupStatus(batch.RollupStatus) {
388+ // // return false
389+ // //}
390+ //}
391+ //l2Relayer.ProcessPendingBatches()
392+ //
393+ //assert.Eventually(t, func() bool {
394+ // l2Relayer.ProcessPendingBundles()
395+ //
396+ // batches, err := batchOrm.GetBatches(context.Background(), map[string]interface{}{}, nil, 0)
397+ // assert.NoError(t, err)
398+ // assert.Len(t, batches, 3)
399+ // batches = batches[1:]
400+ // for _, batch := range batches {
401+ // if types.RollupStatus(batch.RollupStatus) != types.RollupFinalized {
402+ // return false
403+ // }
404+ //
405+ // assert.NotEmpty(t, batch.FinalizeTxHash)
406+ // receipt, getErr := l1Client.TransactionReceipt(context.Background(), common.HexToHash(batch.FinalizeTxHash))
407+ // assert.NoError(t, getErr)
408+ // assert.Equal(t, gethTypes.ReceiptStatusSuccessful, receipt.Status)
409+ // }
410+ //
411+ // bundles, err := bundleOrm.GetBundles(context.Background(), map[string]interface{}{}, nil, 0)
412+ // assert.NoError(t, err)
413+ // assert.Len(t, bundles, 1)
414+ //
415+ // bundle := bundles[0]
416+ // if types.RollupStatus(bundle.RollupStatus) != types.RollupFinalized {
417+ // return false
418+ // }
419+ // assert.NotEmpty(t, bundle.FinalizeTxHash)
420+ // receipt, err := l1Client.TransactionReceipt(context.Background(), common.HexToHash(bundle.FinalizeTxHash))
421+ // assert.NoError(t, err)
422+ // assert.Equal(t, gethTypes.ReceiptStatusSuccessful, receipt.Status)
423+ // batches, err = batchOrm.GetBatches(context.Background(), map[string]interface{}{"bundle_hash": bundle.Hash}, nil, 0)
424+ // assert.NoError(t, err)
425+ // assert.Len(t, batches, 2)
426+ // for _, batch := range batches {
427+ // assert.Equal(t, batch.RollupStatus, bundle.RollupStatus)
428+ // assert.Equal(t, bundle.FinalizeTxHash, batch.FinalizeTxHash)
429+ // }
430+ //
431+ // return true
432+ //}, 30*time.Second, time.Second)
433+ }
0 commit comments