@@ -15,7 +15,6 @@ import (
1515 "github.com/scroll-tech/da-codec/encoding"
1616 "github.com/scroll-tech/go-ethereum/accounts/abi"
1717 "github.com/scroll-tech/go-ethereum/common"
18- gethTypes "github.com/scroll-tech/go-ethereum/core/types"
1918 "github.com/scroll-tech/go-ethereum/crypto"
2019 "github.com/scroll-tech/go-ethereum/crypto/kzg4844"
2120 "github.com/scroll-tech/go-ethereum/ethclient"
@@ -181,79 +180,26 @@ func NewLayer2Relayer(ctx context.Context, l2Client *ethclient.Client, db *gorm.
181180}
182181
183182func (r * Layer2Relayer ) initializeGenesis () error {
184- if count , err := r .batchOrm .GetBatchCount (r .ctx ); err != nil {
185- return fmt .Errorf ("failed to get batch count: %v" , err )
186- } else if count > 0 {
187- log .Info ("genesis already imported" , "batch count" , count )
188- return nil
189- }
190-
191- genesis , err := r .l2Client .HeaderByNumber (r .ctx , big .NewInt (0 ))
183+ latestBatch , err := r .batchOrm .GetLatestBatch (r .ctx )
192184 if err != nil {
193- return fmt .Errorf ("failed to retrieve L2 genesis header : %v" , err )
185+ return fmt .Errorf ("failed to get latest batch : %v" , err )
194186 }
195187
196- log .Info ("retrieved L2 genesis header" , "hash" , genesis .Hash ().String ())
197-
198- chunk := & encoding.Chunk {
199- Blocks : []* encoding.Block {{
200- Header : genesis ,
201- Transactions : nil ,
202- WithdrawRoot : common.Hash {},
203- RowConsumption : & gethTypes.RowConsumption {},
204- }},
188+ startFinalizedBatchIndex := uint64 (337429 )
189+ if latestBatch .Index > startFinalizedBatchIndex {
190+ log .Info ("genesis already imported" )
191+ return nil
205192 }
206193
207- err = r .db .Transaction (func (dbTX * gorm.DB ) error {
208- if err = r .l2BlockOrm .InsertL2Blocks (r .ctx , chunk .Blocks ); err != nil {
209- return fmt .Errorf ("failed to insert genesis block: %v" , err )
210- }
211-
212- var dbChunk * orm.Chunk
213- dbChunk , err = r .chunkOrm .InsertChunk (r .ctx , chunk , encoding .CodecV0 , rutils.ChunkMetrics {}, dbTX )
214- if err != nil {
215- return fmt .Errorf ("failed to insert chunk: %v" , err )
216- }
217-
218- if err = r .chunkOrm .UpdateProvingStatus (r .ctx , dbChunk .Hash , types .ProvingTaskVerified , dbTX ); err != nil {
219- return fmt .Errorf ("failed to update genesis chunk proving status: %v" , err )
220- }
221-
222- batch := & encoding.Batch {
223- Index : 0 ,
224- TotalL1MessagePoppedBefore : 0 ,
225- ParentBatchHash : common.Hash {},
226- Chunks : []* encoding.Chunk {chunk },
227- }
228-
229- var dbBatch * orm.Batch
230- dbBatch , err = r .batchOrm .InsertBatch (r .ctx , batch , encoding .CodecV0 , rutils.BatchMetrics {}, dbTX )
231- if err != nil {
232- return fmt .Errorf ("failed to insert batch: %v" , err )
233- }
234-
235- if err = r .chunkOrm .UpdateBatchHashInRange (r .ctx , 0 , 0 , dbBatch .Hash , dbTX ); err != nil {
236- return fmt .Errorf ("failed to update batch hash for chunks: %v" , err )
237- }
238-
239- if err = r .batchOrm .UpdateProvingStatus (r .ctx , dbBatch .Hash , types .ProvingTaskVerified , dbTX ); err != nil {
240- return fmt .Errorf ("failed to update genesis batch proving status: %v" , err )
241- }
242-
243- if err = r .batchOrm .UpdateRollupStatus (r .ctx , dbBatch .Hash , types .RollupFinalized , dbTX ); err != nil {
244- return fmt .Errorf ("failed to update genesis batch rollup status: %v" , err )
245- }
246-
247- // commit genesis batch on L1
248- // note: we do this inside the DB transaction so that we can revert all DB changes if this step fails
249- return r .commitGenesisBatch (dbBatch .Hash , dbBatch .BatchHeader , common .HexToHash (dbBatch .StateRoot ))
250- })
251-
194+ startFinalizedBatch , err := r .batchOrm .GetBatchByIndex (r .ctx , startFinalizedBatchIndex )
252195 if err != nil {
253- return fmt .Errorf ("update genesis transaction failed : %v" , err )
196+ return fmt .Errorf ("failed to get batch by index : %v, err: %w" , startFinalizedBatchIndex , err )
254197 }
255198
256- log .Info ("successfully imported genesis chunk and batch" )
199+ if err = r .commitGenesisBatch (startFinalizedBatch .Hash , startFinalizedBatch .BatchHeader , common .HexToHash (startFinalizedBatch .StateRoot )); err != nil {
200+ return fmt .Errorf ("commit genesis batch failed: %v" , err )
201+ }
202+ log .Info ("import genesis transaction successfully" , "batch index" , startFinalizedBatchIndex , "batch hash" , startFinalizedBatch .Hash )
257203
258204 return nil
259205}
0 commit comments