Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(genesis): use embedded genesis config for default chains #56

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions anvil/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ethereum-optimism/supersim/config"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
Expand All @@ -29,13 +30,6 @@ const (
anvilListeningLogStr = "Listening on"
)

type Config struct {
config.ChainConfig

// only applicable when ForkConfig is not set
Genesis []byte
}

type Anvil struct {
rpcClient *rpc.Client

Expand All @@ -44,7 +38,7 @@ type Anvil struct {
log log.Logger
logFilePath string

cfg *Config
cfg *config.ChainConfig
cmd *exec.Cmd

resourceCtx context.Context
Expand All @@ -54,7 +48,7 @@ type Anvil struct {
stoppedCh chan struct{}
}

func New(log log.Logger, cfg *Config) *Anvil {
func New(log log.Logger, cfg *config.ChainConfig) *Anvil {
resCtx, resCancel := context.WithCancel(context.Background())
return &Anvil{
log: log,
Expand All @@ -79,12 +73,12 @@ func (a *Anvil) Start(ctx context.Context) error {
"--port", fmt.Sprintf("%d", a.cfg.Port),
}

if len(a.cfg.Genesis) > 0 && a.cfg.ForkConfig == nil {
if len(a.cfg.GenesisJSON) > 0 && a.cfg.ForkConfig == nil {
tempFile, err := os.CreateTemp("", "genesis-*.json")
if err != nil {
return fmt.Errorf("error creating temporary genesis file: %w", err)
}
if _, err = tempFile.Write(a.cfg.Genesis); err != nil {
if _, err = tempFile.Write(a.cfg.GenesisJSON); err != nil {
return fmt.Errorf("error writing to genesis file: %w", err)
}
args = append(args, "--init", tempFile.Name())
Expand Down Expand Up @@ -219,10 +213,6 @@ func (a *Anvil) ChainID() uint64 {
return a.cfg.ChainID
}

func (a *Anvil) SourceChainID() uint64 {
return a.cfg.SourceChainID
}

func (a *Anvil) LogPath() string {
return a.logFilePath
}
Expand Down Expand Up @@ -272,6 +262,10 @@ func (a *Anvil) Web3ClientVersion(ctx context.Context) (string, error) {
}

// eth_ API
func (a *Anvil) EthGetCode(ctx context.Context, account common.Address) ([]byte, error) {
jakim929 marked this conversation as resolved.
Show resolved Hide resolved
return a.ethClient.CodeAt(ctx, account, nil)
}

func (a *Anvil) EthGetLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) {
return a.ethClient.FilterLogs(ctx, q)
}
Expand Down
2 changes: 1 addition & 1 deletion anvil/anvil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestAnvil(t *testing.T) {
cfg := Config{config.ChainConfig{ChainID: 10, Port: 0}, nil}
cfg := config.ChainConfig{ChainID: 10, Port: 0}
testlog := testlog.Logger(t, log.LevelInfo)
anvil := New(testlog, &cfg)

Expand Down
40 changes: 32 additions & 8 deletions config/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import (
"fmt"
"strings"

"github.com/ethereum-optimism/optimism/op-chain-ops/genesis"
"github.com/ethereum-optimism/supersim/genesisdeployment"
"github.com/ethereum-optimism/supersim/hdaccount"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

Expand All @@ -22,20 +25,29 @@ var (
DefaultChainConfigs = []ChainConfig{
{
Name: "SourceChain",
ChainID: 1,
ChainID: genesisdeployment.GeneratedGenesisDeployment.L1.ChainID,
SecretsConfig: DefaultSecretsConfig,
GenesisJSON: genesisdeployment.GeneratedGenesisDeployment.L1.GenesisJSON,
},
{
Name: "OPChainA",
ChainID: 10,
SourceChainID: 1,
ChainID: genesisdeployment.GeneratedGenesisDeployment.L2s[0].ChainID,
SecretsConfig: DefaultSecretsConfig,
GenesisJSON: genesisdeployment.GeneratedGenesisDeployment.L2s[0].GenesisJSON,
L2Config: &L2Config{
L1ChainID: genesisdeployment.GeneratedGenesisDeployment.L1.ChainID,
L1DeploymentAddresses: genesisdeployment.GeneratedGenesisDeployment.L2s[0].L1DeploymentAddresses,
},
},
{
Name: "OPChainB",
ChainID: 30,
SourceChainID: 1,
ChainID: genesisdeployment.GeneratedGenesisDeployment.L2s[1].ChainID,
SecretsConfig: DefaultSecretsConfig,
GenesisJSON: genesisdeployment.GeneratedGenesisDeployment.L2s[1].GenesisJSON,
L2Config: &L2Config{
L1ChainID: genesisdeployment.GeneratedGenesisDeployment.L1.ChainID,
L1DeploymentAddresses: genesisdeployment.GeneratedGenesisDeployment.L2s[1].L1DeploymentAddresses,
},
},
}
)
Expand All @@ -51,17 +63,27 @@ type SecretsConfig struct {
DerivationPath accounts.DerivationPath
}

type L2Config struct {
L1ChainID uint64
L1DeploymentAddresses *genesis.L1Deployments
}

type ChainConfig struct {
Name string
Port uint64

ChainID uint64
SourceChainID uint64 // "L1"
ChainID uint64

GenesisJSON []byte

SecretsConfig SecretsConfig

// Optional Config
ForkConfig *ForkConfig

// Optional Config
// Chain is an L1 chain if L2Config is nil
L2Config *L2Config
}

type Chain interface {
Expand All @@ -71,8 +93,10 @@ type Chain interface {
LogPath() string

// API methods
EthSendTransaction(ctx context.Context, tx *types.Transaction) error
EthGetCode(ctx context.Context, account common.Address) ([]byte, error)
EthGetLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)
EthSendTransaction(ctx context.Context, tx *types.Transaction) error

SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)
}

Expand Down
2 changes: 1 addition & 1 deletion genesisdeployment/generated/l1-allocs/901-l1-allocs.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion genesisdeployment/generated/l1-allocs/902-l1-allocs.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion genesisdeployment/generated/l1-allocs/903-l1-allocs.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion genesisdeployment/generated/l1-allocs/904-l1-allocs.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion genesisdeployment/generated/l1-allocs/905-l1-allocs.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion genesisdeployment/generated/l1-combined-allocs.json

Large diffs are not rendered by default.

94 changes: 94 additions & 0 deletions genesisdeployment/genesisdeployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package genesisdeployment

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

"github.com/ethereum-optimism/optimism/op-chain-ops/genesis"
)

/*******************
L1 Genesis files
*******************/
//go:embed generated/l1-genesis.json
var l1GenesisJSON []byte

/*******************
L2 Genesis files
*******************/
//go:embed generated/l2-genesis/901-l2-genesis.json
var l2Genesis901JSON []byte

//go:embed generated/l2-genesis/902-l2-genesis.json
var l2Genesis902JSON []byte

//go:embed generated/l2-genesis/903-l2-genesis.json
var l2Genesis903JSON []byte

//go:embed generated/l2-genesis/904-l2-genesis.json
var l2Genesis904JSON []byte

//go:embed generated/l2-genesis/905-l2-genesis.json
var l2Genesis905JSON []byte

/*******************
L2 Addresses files
*******************/
//go:embed generated/addresses/901-addresses.json
var addresses901JSON []byte

//go:embed generated/addresses/902-addresses.json
var addresses902JSON []byte

//go:embed generated/addresses/903-addresses.json
var addresses903JSON []byte

//go:embed generated/addresses/904-addresses.json
var addresses904JSON []byte

//go:embed generated/addresses/905-addresses.json
var addresses905JSON []byte

type L1GenesisDeployment struct {
ChainID uint64
GenesisJSON []byte
}

type L2GenesisDeployment struct {
ChainID uint64
GenesisJSON []byte
L1DeploymentAddresses *genesis.L1Deployments
}

func newL2GenesisDeployment(l2ChainID uint64, l1DeploymentAddressesJSON []byte, l2GenesisJSON []byte) *L2GenesisDeployment {
var l1DeploymentAddresses genesis.L1Deployments
if err := json.Unmarshal(l1DeploymentAddressesJSON, &l1DeploymentAddresses); err != nil {
panic(fmt.Sprintf("failed to unmarshal L1 deployment addresses: %v", err))
}

return &L2GenesisDeployment{
ChainID: l2ChainID,
L1DeploymentAddresses: &l1DeploymentAddresses,
GenesisJSON: l2GenesisJSON,
}
}

type GenesisDeployment struct {
L1 *L1GenesisDeployment
L2s []*L2GenesisDeployment
}

var GeneratedGenesisDeployment = &GenesisDeployment{
L1: &L1GenesisDeployment{
ChainID: 900,
GenesisJSON: l1GenesisJSON,
},
L2s: []*L2GenesisDeployment{
newL2GenesisDeployment(901, addresses901JSON, l2Genesis901JSON),
newL2GenesisDeployment(902, addresses902JSON, l2Genesis902JSON),
newL2GenesisDeployment(903, addresses903JSON, l2Genesis903JSON),
newL2GenesisDeployment(904, addresses904JSON, l2Genesis904JSON),
newL2GenesisDeployment(905, addresses905JSON, l2Genesis905JSON),
},
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/ethereum-optimism/go-ethereum-hdwallet v0.1.3 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/fjl/memsize v0.0.2 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeC
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 h1:rpfIENRNNilwHwZeG5+P150SMrnNEcHYvcCuK6dPZSg=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218=
github.com/ethereum-optimism/go-ethereum-hdwallet v0.1.3 h1:RWHKLhCrQThMfch+QJ1Z8veEq5ZO3DfIhZ7xgRP9WTc=
github.com/ethereum-optimism/go-ethereum-hdwallet v0.1.3/go.mod h1:QziizLAiF0KqyLdNJYD7O5cpDlaFMNZzlxYNcWsJUxs=
github.com/ethereum-optimism/op-geth v1.101315.3-rc.1 h1:Q/B0FBdtglzsFtqurvUsiNfH8isCOFFF/pf9ss0L4eY=
github.com/ethereum-optimism/op-geth v1.101315.3-rc.1/go.mod h1:h5C5tP+7gkMrlUGENuiV5ddlwJ4RxLdmdapRuTAGlnw=
github.com/ethereum-optimism/optimism v1.7.7 h1:MDYeeTHNETEJjrRmAx/wUxzCzdkXOZFKExPRBbpMZ04=
Expand Down
15 changes: 9 additions & 6 deletions opsimulator/opsimulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type OpSimulator struct {
l1Chain config.Chain
l2Chain config.Chain

L2Config *config.L2Config

port uint64
httpServer *ophttp.HTTPServer

Expand All @@ -43,12 +45,13 @@ type JSONRPCRequest struct {
JSONRPC string `json:"jsonrpc"`
}

func New(log log.Logger, port uint64, l1Chain config.Chain, l2Chain config.Chain) *OpSimulator {
func New(log log.Logger, port uint64, l1Chain config.Chain, l2Chain config.Chain, l2Config *config.L2Config) *OpSimulator {
return &OpSimulator{
log: log,
port: port,
l1Chain: l1Chain,
l2Chain: l2Chain,
port: port,
log: log,
l1Chain: l1Chain,
l2Chain: l2Chain,
L2Config: l2Config,
}
}

Expand Down Expand Up @@ -155,7 +158,7 @@ func (opSim *OpSimulator) ChainID() uint64 {
}

func (opSim *OpSimulator) SourceChainID() uint64 {
return opSim.l1Chain.ChainID()
return opSim.L2Config.L1ChainID
}

func (opSim *OpSimulator) String() string {
Expand Down
Loading
Loading