@@ -10,9 +10,11 @@ import (
1010
1111 "github.com/scroll-tech/da-codec/encoding"
1212 "github.com/scroll-tech/go-ethereum/common"
13+ "github.com/scroll-tech/go-ethereum/core/types"
1314 "github.com/scroll-tech/go-ethereum/log"
1415
1516 "scroll-tech/common/database"
17+ ctypes "scroll-tech/common/types"
1618
1719 "scroll-tech/rollup/internal/orm"
1820 "scroll-tech/rollup/internal/utils"
@@ -46,6 +48,7 @@ func importData(ctx context.Context, beginBlk, endBlk uint64, chkNum, batchNum,
4648 if err != nil {
4749 return nil , err
4850 }
51+
4952 ret := & importRecord {}
5053 // Create a new random source with the provided seed
5154 source := rand .NewSource (seed )
@@ -62,6 +65,9 @@ func importData(ctx context.Context, beginBlk, endBlk uint64, chkNum, batchNum,
6265 log .Info ("separated chunk" , "border" , chkSep )
6366 head := beginBlk
6467 lastMsgHash := common.Hash {}
68+ if err := initLeadingChunk (ctx , db , beginBlk , endBlk , lastMsgHash ); err != nil {
69+ return nil , err
70+ }
6571
6672 ormChks := make ([]* orm.Chunk , 0 , chkNum )
6773 encChks := make ([]* encoding.Chunk , 0 , chkNum )
@@ -118,6 +124,73 @@ func importData(ctx context.Context, beginBlk, endBlk uint64, chkNum, batchNum,
118124 return ret , nil
119125}
120126
127+ func initLeadingChunk (ctx context.Context , db * gorm.DB , beginBlk , endBlk uint64 , prevMsgQueueHash common.Hash ) error {
128+ blockOrm := orm .NewL2Block (db )
129+ if beginBlk <= 1 {
130+ log .Info ("start from genesis, no need to insert leading chunk" )
131+ return nil
132+ }
133+
134+ var l1MsgPoppedBefore uint64
135+ blks , err := blockOrm .GetL2BlocksGEHeight (ctx , beginBlk , int (endBlk - beginBlk + 1 ))
136+ if err != nil {
137+ return err
138+ }
139+ for i , block := range blks {
140+ for _ , tx := range block .Transactions {
141+ if tx .Type == types .L1MessageTxType {
142+ l1MsgPoppedBefore = tx .Nonce
143+ log .Info ("search first l1 nonce" , "index" , l1MsgPoppedBefore , "blk" , beginBlk + uint64 (i ))
144+ break
145+ }
146+ }
147+ if l1MsgPoppedBefore != 0 {
148+ break
149+ }
150+ }
151+
152+ if l1MsgPoppedBefore == 0 {
153+ log .Info ("no l1 message in target blks, no need for leading chunk" )
154+ return nil
155+ }
156+
157+ prevBlks , err := blockOrm .GetL2BlocksGEHeight (ctx , beginBlk - 1 , 1 )
158+ if err != nil {
159+ log .Error ("get prev block fail, we also need at least 1 block before selected range" , "need block" , beginBlk - 1 , "err" , err )
160+ return err
161+ }
162+
163+ // we use InsertTestChunkForProposerTool to insert leading chunk, which do not calculate l1 message
164+ // so we simply exclude l1 in this hacked chunk
165+ prevBlk := prevBlks [0 ]
166+ var trimLen int
167+ for _ , tx := range prevBlk .Transactions {
168+ if tx .Type != types .L1MessageTxType {
169+ prevBlk .Transactions [trimLen ] = tx
170+ trimLen ++
171+ }
172+ }
173+ prevBlk .Transactions = prevBlk .Transactions [:trimLen ]
174+
175+ postHash , err := encoding .MessageQueueV2ApplyL1MessagesFromBlocks (prevMsgQueueHash , prevBlks )
176+ if err != nil {
177+ return err
178+ }
179+ chunkOrm := orm .NewChunk (db )
180+
181+ log .Info ("Insert leading chunk with prev block" , "msgPoppedBefore" , l1MsgPoppedBefore )
182+ leadingChunk , err := chunkOrm .InsertTestChunkForProposerTool (ctx , & encoding.Chunk {
183+ Blocks : prevBlks ,
184+ PrevL1MessageQueueHash : prevMsgQueueHash ,
185+ PostL1MessageQueueHash : postHash ,
186+ }, codecCfg , l1MsgPoppedBefore )
187+ if err != nil {
188+ return err
189+ }
190+
191+ return chunkOrm .UpdateProvingStatus (ctx , leadingChunk .Hash , ctypes .ProvingTaskProvedDEPRECATED )
192+ }
193+
121194func importChunk (ctx context.Context , db * gorm.DB , beginBlk , endBlk uint64 , prevMsgQueueHash common.Hash ) (* orm.Chunk , * encoding.Chunk , error ) {
122195 nblk := int (endBlk - beginBlk ) + 1
123196 blockOrm := orm .NewL2Block (db )
@@ -183,6 +256,8 @@ func importBatch(ctx context.Context, db *gorm.DB, chks []*orm.Chunk, encChks []
183256 ParentBatchHash : parentHash ,
184257 Chunks : encChks ,
185258 Blocks : blks ,
259+ PrevL1MessageQueueHash : encChks [0 ].PrevL1MessageQueueHash ,
260+ PostL1MessageQueueHash : encChks [len (encChks )- 1 ].PostL1MessageQueueHash ,
186261 }
187262
188263 dbBatch , err := batchOrm .InsertBatch (ctx , batch , codecCfg , utils.BatchMetrics {
0 commit comments