Skip to content

Commit 1e44e03

Browse files
committed
Use cel2 migration header for rollup config
1 parent 123d4ee commit 1e44e03

File tree

1 file changed

+47
-50
lines changed
  • op-chain-ops/cmd/celo-migrate

1 file changed

+47
-50
lines changed

op-chain-ops/cmd/celo-migrate/main.go

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ func main() {
123123
return fmt.Errorf("must specify --l1-rpc")
124124
}
125125

126+
outfileL2 := ctx.Path("outfile.l2")
127+
if outfileL2 == "" {
128+
return fmt.Errorf("must specify --outfile.l2")
129+
}
130+
131+
outfileRollup := ctx.Path("outfile.rollup")
132+
if outfileRollup == "" {
133+
return fmt.Errorf("must specify --outfile.rollup")
134+
}
135+
126136
var l1StartBlock *types.Block
127137
client, err := ethclient.Dial(l1RPC)
128138
if err != nil {
@@ -167,32 +177,6 @@ func main() {
167177
return fmt.Errorf("error creating l2 genesis: %w", err)
168178
}
169179

170-
l2GenesisBlock := l2Genesis.ToBlock()
171-
rollupConfig, err := config.RollupConfig(l1StartBlock, l2GenesisBlock.Hash(), l2GenesisBlock.Number().Uint64())
172-
if err != nil {
173-
return err
174-
}
175-
if err := rollupConfig.Check(); err != nil {
176-
return fmt.Errorf("generated rollup config does not pass validation: %w", err)
177-
}
178-
179-
outfileL2 := ctx.Path("outfile.l2")
180-
if outfileL2 == "" {
181-
return fmt.Errorf("must specify --outfile.l2")
182-
}
183-
184-
outfileRollup := ctx.Path("outfile.rollup")
185-
if outfileRollup == "" {
186-
return fmt.Errorf("must specify --outfile.rollup")
187-
}
188-
189-
if err := jsonutil.WriteJSON(outfileL2, l2Genesis); err != nil {
190-
return err
191-
}
192-
if err := jsonutil.WriteJSON(outfileRollup, rollupConfig); err != nil {
193-
return err
194-
}
195-
196180
// So far we applied changes in the memory VM and collected changes in the genesis struct
197181
// No we iterate through all accounts that have been written there and set them inside the statedb.
198182
// This will change the state root
@@ -213,8 +197,10 @@ func main() {
213197
if err != nil {
214198
return fmt.Errorf("cannot open DB: %w", err)
215199
}
200+
log.Info("Loaded Celo L1 DB", "db", ldb)
216201

217-
if err := ApplyMigrationChangesToDB(ldb, l2Genesis, !dryRun); err != nil {
202+
cel2Header, err := ApplyMigrationChangesToDB(ldb, l2Genesis, !dryRun)
203+
if err != nil {
218204
return err
219205
}
220206

@@ -223,7 +209,26 @@ func main() {
223209
return err
224210
}
225211

226-
log.Info("Loaded Celo L1 DB", "db", ldb)
212+
log.Info("Updated Cel2 state")
213+
214+
log.Info("Writing state diff", "file", outfileL2)
215+
// Write genesis file to check created state
216+
if err := jsonutil.WriteJSON(outfileL2, l2Genesis); err != nil {
217+
return err
218+
}
219+
220+
rollupConfig, err := config.RollupConfig(l1StartBlock, cel2Header.Hash(), cel2Header.Number.Uint64())
221+
if err != nil {
222+
return err
223+
}
224+
if err := rollupConfig.Check(); err != nil {
225+
return fmt.Errorf("generated rollup config does not pass validation: %w", err)
226+
}
227+
228+
log.Info("Writing rollup config", "file", outfileRollup)
229+
if err := jsonutil.WriteJSON(outfileRollup, rollupConfig); err != nil {
230+
return err
231+
}
227232

228233
return nil
229234
},
@@ -235,7 +240,7 @@ func main() {
235240
log.Info("Finished migration successfully!")
236241
}
237242

238-
func ApplyMigrationChangesToDB(ldb ethdb.Database, genesis *core.Genesis, commit bool) error {
243+
func ApplyMigrationChangesToDB(ldb ethdb.Database, genesis *core.Genesis, commit bool) (*types.Header, error) {
239244
log.Info("Migrating DB")
240245

241246
// Grab the hash of the tip of the legacy chain.
@@ -245,7 +250,7 @@ func ApplyMigrationChangesToDB(ldb ethdb.Database, genesis *core.Genesis, commit
245250
// Grab the header number.
246251
num := rawdb.ReadHeaderNumber(ldb, hash)
247252
if num == nil {
248-
return fmt.Errorf("cannot find header number for %s", hash)
253+
return nil, fmt.Errorf("cannot find header number for %s", hash)
249254
}
250255
log.Info("Reading chain tip num from database", "number", num)
251256

@@ -279,7 +284,7 @@ func ApplyMigrationChangesToDB(ldb ethdb.Database, genesis *core.Genesis, commit
279284

280285
db, err := dbFactory()
281286
if err != nil {
282-
return fmt.Errorf("cannot create StateDB: %w", err)
287+
return nil, fmt.Errorf("cannot create StateDB: %w", err)
283288
}
284289

285290
for k, v := range genesis.Alloc {
@@ -302,28 +307,28 @@ func ApplyMigrationChangesToDB(ldb ethdb.Database, genesis *core.Genesis, commit
302307
// TODO(pl): What block info to put here?
303308
newRoot, err := db.Commit(1234, true)
304309
if err != nil {
305-
return err
310+
return nil, err
306311
}
307312

308313
log.Info("Creating new Genesis block")
309314
// Create the header for the Bedrock transition block.
310315
cel2Header := &types.Header{
311316
ParentHash: header.Hash(),
312317
UncleHash: types.EmptyUncleHash,
313-
Coinbase: predeploys.SequencerFeeVaultAddr, // TODO(pl)
318+
Coinbase: predeploys.SequencerFeeVaultAddr,
314319
Root: newRoot,
315320
TxHash: types.EmptyRootHash,
316321
ReceiptHash: types.EmptyRootHash,
317322
Bloom: types.Bloom{},
318-
Difficulty: common.Big0,
319-
Number: common.Big0,
320-
GasLimit: (uint64)(20_000_000),
323+
Difficulty: new(big.Int).Set(common.Big0),
324+
Number: new(big.Int).Add(header.Number, common.Big1),
325+
GasLimit: header.GasLimit,
321326
GasUsed: 0,
322-
Time: uint64(12345),
323-
Extra: []byte("CeL2"),
327+
Time: header.Time,
328+
Extra: []byte("CeL2 migration"),
324329
MixDigest: common.Hash{},
325330
Nonce: types.BlockNonce{},
326-
BaseFee: big.NewInt(params.InitialBaseFee),
331+
BaseFee: new(big.Int).Set(header.BaseFee),
327332
}
328333

329334
// Create the Bedrock transition block from the header. Note that there are no transactions,
@@ -341,25 +346,17 @@ func ApplyMigrationChangesToDB(ldb ethdb.Database, genesis *core.Genesis, commit
341346
)
342347

343348
log.Info("Header", "header", cel2Header)
344-
log.Info("Body", "Body", cel2Block)
345-
346-
// Create the result of the migration.
347-
// res := &MigrationResult{
348-
// TransitionHeight: cel2Block.NumberU64(),
349-
// TransitionTimestamp: cel2Block.Time(),
350-
// TransitionBlockHash: cel2Block.Hash(),
351-
// }
352349

353350
// If we're not actually writing this to disk, then we're done.
354351
if !commit {
355352
log.Info("Dry run complete")
356-
return nil
353+
return nil, nil
357354
}
358355

359356
// Otherwise we need to write the changes to disk. First we commit the state changes.
360357
log.Info("Committing trie DB")
361358
if err := db.Database().TrieDB().Commit(newRoot, true); err != nil {
362-
return err
359+
return nil, err
363360
}
364361

365362
// Next we write the Cel2 genesis block to the database.
@@ -418,7 +415,7 @@ func ApplyMigrationChangesToDB(ldb ethdb.Database, genesis *core.Genesis, commit
418415
"timestamp", cel2Header.Time,
419416
)
420417

421-
return nil
418+
return cel2Header, nil
422419
}
423420

424421
func Open(path string, cache int, handles int) (ethdb.Database, error) {

0 commit comments

Comments
 (0)