Commit 0ba3943a authored by Mark Tyneway's avatar Mark Tyneway

op-chain-ops: single database commit

Instead of having multiple statedbs created,
use a single statedb and make sure that it is
only committed once. Various APIs are required
for the full migration which wrap the low level
LevelDB at various abstractions. This will ensure
that changes to the statedb are committed to disk.
parent 0ac00985
......@@ -34,7 +34,7 @@ var (
}
)
func MigrateLegacyETH(db ethdb.Database, addresses []common.Address, allowances []*migration.Allowance, chainID int, commit, noCheck bool) (common.Hash, error) {
func MigrateLegacyETH(db ethdb.Database, stateDB *state.StateDB, addresses []common.Address, allowances []*migration.Allowance, chainID int, noCheck bool) error {
// Set of addresses that we will be migrating.
addressesToMigrate := make(map[common.Address]bool)
// Set of storage slots that we expect to see in the OVM ETH contract.
......@@ -42,7 +42,7 @@ func MigrateLegacyETH(db ethdb.Database, addresses []common.Address, allowances
// Chain params to use for integrity checking.
params := ParamsByChainID[chainID]
if params == nil {
return common.Hash{}, fmt.Errorf("no chain params for %d", chainID)
return fmt.Errorf("no chain params for %d", chainID)
}
log.Info("Chain params", "chain-id", chainID, "supply-delta", params.ExpectedSupplyDelta)
......@@ -85,7 +85,7 @@ func MigrateLegacyETH(db ethdb.Database, addresses []common.Address, allowances
return nil
})
if err != nil {
return common.Hash{}, wrapErr(err, "error reading mint events")
return wrapErr(err, "error reading mint events")
}
// Make sure all addresses are accounted for by iterating over
......@@ -96,9 +96,8 @@ func MigrateLegacyETH(db ethdb.Database, addresses []common.Address, allowances
backingStateDB := state.NewDatabaseWithConfig(db, &trie.Config{
Preimages: true,
})
stateDB, err := state.New(root, backingStateDB, nil)
if err != nil {
return common.Hash{}, wrapErr(err, "error opening state DB")
return wrapErr(err, "error opening state DB")
}
storageTrie := stateDB.StorageTrie(OVMETHAddress)
storageIt := trie.NewIterator(storageTrie.NodeIterator(nil))
......@@ -173,7 +172,7 @@ func MigrateLegacyETH(db ethdb.Database, addresses []common.Address, allowances
log.Info("trie dumping started", "root", root)
tr, err := backingStateDB.OpenTrie(root)
if err != nil {
return common.Hash{}, err
return err
}
it := trie.NewIterator(tr.NodeIterator(nil))
totalMigrated := new(big.Int)
......@@ -251,21 +250,5 @@ func MigrateLegacyETH(db ethdb.Database, addresses []common.Address, allowances
stateDB.SetState(predeploys.LegacyERC20ETHAddr, getOVMETHTotalSupplySlot(), common.Hash{})
log.Info("Set the totalSupply to 0")
if !commit {
log.Info("dry run, skipping commit")
return common.Hash{}, nil
}
log.Info("committing state DB")
newRoot, err := stateDB.Commit(true)
if err != nil {
return common.Hash{}, err
}
log.Info("committing trie DB")
if err := stateDB.Database().TrieDB().Commit(newRoot, true, nil); err != nil {
return common.Hash{}, err
}
return newRoot, nil
return nil
}
......@@ -121,11 +121,17 @@ func MigrateDB(ldb ethdb.Database, config *DeployConfig, l1Block *types.Block, m
log.Info("Starting to migrate ERC20 ETH")
addrs := migrationData.Addresses()
newRoot, err := ether.MigrateLegacyETH(ldb, addrs, migrationData.OvmAllowances, int(config.L1ChainID), commit, noCheck)
err = ether.MigrateLegacyETH(ldb, db, addrs, migrationData.OvmAllowances, int(config.L1ChainID), noCheck)
if err != nil {
return nil, fmt.Errorf("cannot migrate legacy eth: %w", err)
}
log.Info("Completed ERC20 ETH migration", "root", newRoot)
log.Info("Completed ERC20 ETH migration")
newRoot, err := db.Commit(true)
if err != nil {
return nil, err
}
log.Info("committing state DB", "root", newRoot)
// Set the amount of gas used so that EIP 1559 starts off stable
gasUsed := (uint64)(config.L2GenesisBlockGasLimit) * config.EIP1559Elasticity
......@@ -172,6 +178,11 @@ func MigrateDB(ldb ethdb.Database, config *DeployConfig, l1Block *types.Block, m
return res, nil
}
log.Info("committing trie DB")
if err := db.Database().TrieDB().Commit(newRoot, true, nil); err != nil {
return nil, err
}
rawdb.WriteTd(ldb, bedrockBlock.Hash(), bedrockBlock.NumberU64(), bedrockBlock.Difficulty())
rawdb.WriteBlock(ldb, bedrockBlock)
rawdb.WriteReceipts(ldb, bedrockBlock.Hash(), bedrockBlock.NumberU64(), nil)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment