Skip to content

Commit

Permalink
Add bindings for SR2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mslipper committed Jan 16, 2025
1 parent 6c1047a commit 45ad786
Show file tree
Hide file tree
Showing 14 changed files with 450 additions and 106 deletions.
18 changes: 18 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ jobs:
branch_pattern: optimism
event: fail
template: basic_fail_1
check-sr-diff:
docker:
- image: cimg/go:<<pipeline.parameters.go_version>>
steps:
- checkout
- run:
name: install dasel
command: go install github.com/tomwright/dasel/v2/cmd/[email protected]
- run:
name: install jq
command: sudo apt-get install jq
- run:
name: generate artifact and check diff
command: |
bash ./sync-superchain.sh
git diff --exit-code
workflows:
main:
Expand All @@ -235,6 +251,8 @@ workflows:
docker_tags: <<pipeline.git.revision>>
context:
- oplabs-gcr
- check-sr-diff:
name: Check superchain registry bundle diff
release:
jobs:
- hold:
Expand Down
5 changes: 3 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/netutil"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/superchain"
"github.com/ethereum/go-ethereum/triedb"
"github.com/ethereum/go-ethereum/triedb/hashdb"
"github.com/ethereum/go-ethereum/triedb/pathdb"
Expand Down Expand Up @@ -159,7 +160,7 @@ var (
Name: "op-network",
Aliases: []string{"beta.op-network"},
Usage: "Select a pre-configured OP-Stack network (warning: op-mainnet and op-goerli require special sync," +
" datadir is recommended), options: " + strings.Join(params.OPStackChainNames(), ", "),
" datadir is recommended), options: " + strings.Join(superchain.ChainNames(), ", "),
Category: flags.EthCategory,
}

Expand Down Expand Up @@ -2314,7 +2315,7 @@ func MakeGenesis(ctx *cli.Context) *core.Genesis {
genesis = core.DefaultSepoliaGenesisBlock()
case ctx.IsSet(OPNetworkFlag.Name):
name := ctx.String(OPNetworkFlag.Name)
ch, err := params.OPStackChainIDByName(name)
ch, err := superchain.ChainIDByName(name)
if err != nil {
Fatalf("failed to load OP-Stack chain %q: %v", name, err)
}
Expand Down
Binary file added consensus/misc/create2deployer.bin
Binary file not shown.
12 changes: 7 additions & 5 deletions consensus/misc/create2deployer.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package misc

import (
"github.com/ethereum-optimism/superchain-registry/superchain"
_ "embed"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
)
Expand All @@ -18,14 +19,15 @@ import (

var create2DeployerAddress = common.HexToAddress("0x13b0D85CcB8bf860b6b79AF3029fCA081AE9beF2")
var create2DeployerCodeHash = common.HexToHash("0xb0550b5b431e30d38000efb7107aaa0ade03d48a7198a140edda9d27134468b2")

//go:embed create2deployer.bin
var create2DeployerCode []byte

func init() {
code, err := superchain.LoadContractBytecode(superchain.Hash(create2DeployerCodeHash))
if err != nil {
panic(err)
testCodeHash := crypto.Keccak256Hash(create2DeployerCode)
if testCodeHash != create2DeployerCodeHash {
panic("create2deployer hash and code mismatch")
}
create2DeployerCode = code
}

func EnsureCreate2Deployer(c *params.ChainConfig, timestamp uint64, db vm.StateDB) {
Expand Down
26 changes: 19 additions & 7 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"math/big"
"strings"

"github.com/ethereum-optimism/superchain-registry/superchain"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
Expand All @@ -38,6 +36,7 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/superchain"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/triedb"
"github.com/ethereum/go-ethereum/triedb/pathdb"
Expand Down Expand Up @@ -306,13 +305,26 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, triedb *triedb.Database, g
// If applying the superchain-registry to a known OP-Stack chain,
// then override the local chain-config with that from the registry.
if overrides != nil && overrides.ApplySuperchainUpgrades && config.IsOptimism() && config.ChainID != nil && config.ChainID.IsUint64() {
if _, ok := superchain.OPChains[config.ChainID.Uint64()]; ok {
conf, err := params.LoadOPStackChainConfig(config.ChainID.Uint64())
getChainConfig := func() (*params.ChainConfig, error) {
chain, err := superchain.GetChain(config.ChainID.Uint64())
if err != nil {
return nil, err
}
chainConf, err := chain.Config()
if err != nil {
log.Warn("failed to load chain config from superchain-registry, skipping override", "err", err, "chain_id", config.ChainID)
} else {
*config = *conf
return nil, err
}
genConf, err := params.LoadOPStackChainConfig(chainConf)
if err != nil {
return nil, err
}
return genConf, err
}

if genConf, err := getChainConfig(); err == nil {
*config = *genConf
} else {
log.Warn("failed to load chain config from superchain-registry, skipping override", "err", err, "chain_id", config.ChainID)
}
}
if overrides != nil && overrides.OverrideCancun != nil {
Expand Down
76 changes: 33 additions & 43 deletions core/superchain.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
package core

import (
_ "embed"
"encoding/json"
"fmt"
"math/big"

"github.com/ethereum-optimism/superchain-registry/superchain"
"github.com/ethereum/go-ethereum/superchain"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"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)
chain, err := superchain.GetChain(chainID)
if err != nil {
return nil, fmt.Errorf("error getting superchain: %w", err)
}

chConfig, err := chain.Config()
if err != nil {
return nil, fmt.Errorf("error getting chain config from superchain: %w", err)
}

cfg, err := params.LoadOPStackChainConfig(chainID)
cfg, err := params.LoadOPStackChainConfig(chConfig)
if err != nil {
return nil, fmt.Errorf("failed to load params.ChainConfig for chain %d: %w", chainID, err)
}

gen, err := superchain.LoadGenesis(chainID)
gen, err := readSuperchainGenesis(chain)
if err != nil {
return nil, fmt.Errorf("failed to load genesis definition for chain %d: %w", chainID, err)
}
Expand All @@ -33,56 +38,29 @@ func LoadOPStackGenesis(chainID uint64) (*Genesis, error) {
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(types.GenesisAlloc),
Difficulty: gen.Difficulty,
Mixhash: gen.Mixhash,
Coinbase: gen.Coinbase,
Alloc: gen.Alloc,
Number: gen.Number,
GasUsed: gen.GasUsed,
ParentHash: common.Hash(gen.ParentHash),
BaseFee: (*big.Int)(gen.BaseFee),
ParentHash: gen.ParentHash,
BaseFee: gen.BaseFee,
ExcessBlobGas: gen.ExcessBlobGas,
BlobGasUsed: gen.BlobGasUsed,
}

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)
genesis.StateHash = gen.StateHash
genesis.Alloc = nil
}

genesisBlock := genesis.ToBlock()
genesisBlockHash := genesisBlock.Hash()
expectedHash := common.Hash([32]byte(chConfig.Genesis.L2.Hash))
expectedHash := 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.
Expand All @@ -99,3 +77,15 @@ func LoadOPStackGenesis(chainID uint64) (*Genesis, error) {
}
return genesis, nil
}

func readSuperchainGenesis(chain *superchain.Chain) (*Genesis, error) {
genData, err := chain.GenesisData()
if err != nil {
return nil, fmt.Errorf("error getting genesis data from superchain: %w", err)
}
gen := new(Genesis)
if err := json.Unmarshal(genData, gen); err != nil {
return nil, fmt.Errorf("failed to unmarshal genesis data: %w", err)
}
return gen, nil
}
17 changes: 10 additions & 7 deletions core/superchain_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package core

import (
"fmt"
"testing"

"github.com/ethereum-optimism/superchain-registry/superchain"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/superchain"
"github.com/ethereum/go-ethereum/triedb"
)

func TestOPStackGenesis(t *testing.T) {
for id := range superchain.OPChains {
_, err := LoadOPStackGenesis(id)
if err != nil {
t.Error(err)
}
for id, cfg := range superchain.Chains {
t.Run(fmt.Sprintf("chain-%s", cfg.Name), func(t *testing.T) {
t.Parallel()
_, err := LoadOPStackGenesis(id)
if err != nil {
t.Error(err)
}
})
}
}

Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.22.7

require (
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.2.0
github.com/BurntSushi/toml v1.4.0
github.com/Microsoft/go-winio v0.6.2
github.com/VictoriaMetrics/fastcache v1.12.2
github.com/aws/aws-sdk-go-v2 v1.21.2
Expand All @@ -23,7 +24,6 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1
github.com/donovanhide/eventsource v0.0.0-20210830082556-c59027999da0
github.com/dop251/goja v0.0.0-20230806174421-c933cf95e127
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20250115145553-996c7aba6565
github.com/ethereum/c-kzg-4844 v1.0.0
github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9
github.com/fatih/color v1.16.0
Expand All @@ -48,6 +48,7 @@ require (
github.com/jackpal/go-nat-pmp v1.0.2
github.com/jedisct1/go-minisign v0.0.0-20230811132847-661be99b8267
github.com/karalabe/hid v1.0.1-0.20240306101548-573246063e52
github.com/klauspost/compress v1.17.11
github.com/kylelemons/godebug v1.1.0
github.com/mattn/go-colorable v0.1.13
github.com/mattn/go-isatty v0.0.20
Expand Down Expand Up @@ -80,7 +81,6 @@ require (
require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/DataDog/zstd v1.5.6-0.20230824185856-869dae002e5e // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.13 // indirect
Expand Down Expand Up @@ -118,7 +118,6 @@ require (
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kilic/bls12-381 v0.1.0 // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20250115145553-996c7aba6565 h1:jhMVWUohS71nsNg8Q8d7DatiGpPocvUNGr/zP8a+79A=
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20250115145553-996c7aba6565/go.mod h1:9feO8jcL5OZ1tvRjEfNAHz4Aggvd6373l+ZxmZZAyZs=
github.com/ethereum/c-kzg-4844 v1.0.0 h1:0X1LBXxaEtYD9xsyj9B9ctQEZIpnvVDeoBx8aHEwTNA=
github.com/ethereum/c-kzg-4844 v1.0.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0=
github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 h1:8NfxH2iXvJ60YRB8ChToFTUzl8awsc3cJ8CbLjGIl/A=
Expand Down Expand Up @@ -347,8 +345,8 @@ github.com/kilic/bls12-381 v0.1.0 h1:encrdjqKMEvabVQ7qYOKu1OvhqpK4s47wDYtNiPtlp4
github.com/kilic/bls12-381 v0.1.0/go.mod h1:vDTTHJONJ6G+P2R74EhnyotQDTliQDnFEwhdmfzw1ig=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.16.0 h1:iULayQNOReoYUe+1qtKOqw9CwJv3aNQu8ivo7lw1HU4=
github.com/klauspost/compress v1.16.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand Down
Loading

0 comments on commit 45ad786

Please sign in to comment.