-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from ethereum-optimism/superchain-config
params: source chainConfig and genesis from superchain-registry
- Loading branch information
Showing
8 changed files
with
266 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package core | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/ethereum-optimism/superchain-registry/superchain" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/params" | ||
) | ||
|
||
func LoadOPStackGenesis(chainID uint64) (*Genesis, error) { | ||
chConfig, ok := superchain.OPChains[chainID] | ||
if !ok { | ||
return nil, fmt.Errorf("unknown chain ID: %d", chainID) | ||
} | ||
|
||
cfg, err := params.LoadOPStackChainConfig(chainID) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to load params.ChainConfig for chain %d: %w", chainID, err) | ||
} | ||
|
||
gen, err := superchain.LoadGenesis(chainID) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to load genesis definition for chain %d: %w", chainID, err) | ||
} | ||
|
||
genesis := &Genesis{ | ||
Config: cfg, | ||
Nonce: gen.Nonce, | ||
Timestamp: gen.Timestamp, | ||
ExtraData: gen.ExtraData, | ||
GasLimit: gen.GasLimit, | ||
Difficulty: (*big.Int)(gen.Difficulty), | ||
Mixhash: common.Hash(gen.Mixhash), | ||
Coinbase: common.Address(gen.Coinbase), | ||
Alloc: make(GenesisAlloc), | ||
Number: gen.Number, | ||
GasUsed: gen.GasUsed, | ||
ParentHash: common.Hash(gen.ParentHash), | ||
BaseFee: (*big.Int)(gen.BaseFee), | ||
} | ||
|
||
for addr, acc := range gen.Alloc { | ||
var code []byte | ||
if acc.CodeHash != ([32]byte{}) { | ||
dat, err := superchain.LoadContractBytecode(acc.CodeHash) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to load bytecode %s of address %s in chain %d: %w", acc.CodeHash, addr, chainID, err) | ||
} | ||
code = dat | ||
} | ||
var storage map[common.Hash]common.Hash | ||
if len(acc.Storage) > 0 { | ||
storage = make(map[common.Hash]common.Hash) | ||
for k, v := range acc.Storage { | ||
storage[common.Hash(k)] = common.Hash(v) | ||
} | ||
} | ||
bal := common.Big0 | ||
if acc.Balance != nil { | ||
bal = (*big.Int)(acc.Balance) | ||
} | ||
genesis.Alloc[common.Address(addr)] = GenesisAccount{ | ||
Code: code, | ||
Storage: storage, | ||
Balance: bal, | ||
Nonce: acc.Nonce, | ||
} | ||
} | ||
if gen.StateHash != nil { | ||
if len(gen.Alloc) > 0 { | ||
return nil, fmt.Errorf("chain definition unexpectedly contains both allocation (%d) and state-hash %s", len(gen.Alloc), *gen.StateHash) | ||
} | ||
genesis.StateHash = (*common.Hash)(gen.StateHash) | ||
} | ||
|
||
genesisBlock := genesis.ToBlock() | ||
genesisBlockHash := genesisBlock.Hash() | ||
expectedHash := common.Hash([32]byte(chConfig.Genesis.L2.Hash)) | ||
|
||
// Verify we correctly produced the genesis config by recomputing the genesis-block-hash, | ||
// and check the genesis matches the chain genesis definition. | ||
if chConfig.Genesis.L2.Number != genesisBlock.NumberU64() { | ||
switch chainID { | ||
case params.OPMainnetChainID: | ||
expectedHash = common.HexToHash("0x7ca38a1916c42007829c55e69d3e9a73265554b586a499015373241b8a3fa48b") | ||
case params.OPGoerliChainID: | ||
expectedHash = common.HexToHash("0xc1fc15cd51159b1f1e5cbc4b82e85c1447ddfa33c52cf1d98d14fba0d6354be1") | ||
default: | ||
return nil, fmt.Errorf("unknown stateless genesis definition for chain %d", chainID) | ||
} | ||
} | ||
if expectedHash != genesisBlockHash { | ||
return nil, fmt.Errorf("produced genesis with hash %s but expected %s", genesisBlockHash, expectedHash) | ||
} | ||
return genesis, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ethereum-optimism/superchain-registry/superchain" | ||
) | ||
|
||
func TestOPStackGenesis(t *testing.T) { | ||
for id := range superchain.OPChains { | ||
gen, err := LoadOPStackGenesis(id) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Logf("chain: %d, genesis block hash: %s", id, gen.ToBlock().Hash()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package params | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/ethereum-optimism/superchain-registry/superchain" | ||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
func init() { | ||
for id, ch := range superchain.OPChains { | ||
NetworkNames[fmt.Sprintf("%d", id)] = ch.Name | ||
} | ||
} | ||
|
||
func OPStackChainIDByName(name string) (uint64, error) { | ||
for id, ch := range superchain.OPChains { | ||
if ch.Chain+"-"+ch.Superchain == name { | ||
return id, nil | ||
} | ||
} | ||
return 0, fmt.Errorf("unknown chain %q", name) | ||
} | ||
|
||
func LoadOPStackChainConfig(chainID uint64) (*ChainConfig, error) { | ||
chConfig, ok := superchain.OPChains[chainID] | ||
if !ok { | ||
return nil, fmt.Errorf("unknown chain ID: %d", chainID) | ||
} | ||
superchainConfig, ok := superchain.Superchains[chConfig.Superchain] | ||
if !ok { | ||
return nil, fmt.Errorf("unknown superchain %q", chConfig.Superchain) | ||
} | ||
|
||
genesisActivation := uint64(0) | ||
out := &ChainConfig{ | ||
ChainID: new(big.Int).SetUint64(chainID), | ||
HomesteadBlock: common.Big0, | ||
DAOForkBlock: nil, | ||
DAOForkSupport: false, | ||
EIP150Block: common.Big0, | ||
EIP155Block: common.Big0, | ||
EIP158Block: common.Big0, | ||
ByzantiumBlock: common.Big0, | ||
ConstantinopleBlock: common.Big0, | ||
PetersburgBlock: common.Big0, | ||
IstanbulBlock: common.Big0, | ||
MuirGlacierBlock: common.Big0, | ||
BerlinBlock: common.Big0, | ||
LondonBlock: common.Big0, | ||
ArrowGlacierBlock: common.Big0, | ||
GrayGlacierBlock: common.Big0, | ||
MergeNetsplitBlock: common.Big0, | ||
ShanghaiTime: nil, | ||
CancunTime: nil, | ||
PragueTime: nil, | ||
BedrockBlock: common.Big0, | ||
RegolithTime: &genesisActivation, | ||
TerminalTotalDifficulty: common.Big0, | ||
TerminalTotalDifficultyPassed: true, | ||
Ethash: nil, | ||
Clique: nil, | ||
Optimism: &OptimismConfig{ | ||
EIP1559Elasticity: 6, | ||
EIP1559Denominator: 50, | ||
}, | ||
} | ||
|
||
// note: no actual parameters are being loaded, yet. | ||
// Future superchain upgrades are loaded from the superchain chConfig and applied to the geth ChainConfig here. | ||
_ = superchainConfig.Config | ||
|
||
// special overrides for OP-Stack chains with pre-Regolith upgrade history | ||
switch chainID { | ||
case OPGoerliChainID: | ||
out.LondonBlock = big.NewInt(4061224) | ||
out.ArrowGlacierBlock = big.NewInt(4061224) | ||
out.GrayGlacierBlock = big.NewInt(4061224) | ||
out.MergeNetsplitBlock = big.NewInt(4061224) | ||
out.BedrockBlock = big.NewInt(4061224) | ||
out.RegolithTime = &OptimismGoerliRegolithTime | ||
out.Optimism.EIP1559Elasticity = 10 | ||
case OPMainnetChainID: | ||
out.BerlinBlock = big.NewInt(3950000) | ||
out.LondonBlock = big.NewInt(105235063) | ||
out.ArrowGlacierBlock = big.NewInt(105235063) | ||
out.GrayGlacierBlock = big.NewInt(105235063) | ||
out.MergeNetsplitBlock = big.NewInt(105235063) | ||
out.BedrockBlock = big.NewInt(105235063) | ||
case BaseGoerliChainID: | ||
out.RegolithTime = &BaseGoerliRegolithTime | ||
} | ||
|
||
return out, nil | ||
} |