Skip to content

Commit

Permalink
supersim: config refactors (#52)
Browse files Browse the repository at this point in the history
* config refactors

* fix tests

* lint

* lint
  • Loading branch information
hamdiallam authored Jul 18, 2024
1 parent 0c0a8d6 commit 6073ce7
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 266 deletions.
56 changes: 23 additions & 33 deletions anvil/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,27 @@ import (
"sync/atomic"
"time"

"github.com/ethereum-optimism/supersim/chainapi"
"github.com/ethereum-optimism/supersim/config"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"
)

type ForkConfig struct {
RPCUrl string
BlockNumber uint64
}
var _ config.Chain = &Anvil{}

const (
host = "127.0.0.1"
anvilListeningLogStr = "Listening on"
)

type Config struct {
ChainID uint64
SourceChainID uint64
Port uint64
Accounts uint64
Mnemonic string
DerivationPath string

// Genesis is not applied if the fork config is set
Genesis []byte
ForkConfig *ForkConfig
config.ChainConfig

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

type Anvil struct {
Expand All @@ -59,13 +54,6 @@ type Anvil struct {
stoppedCh chan struct{}
}

const (
host = "127.0.0.1"
anvilListeningLogStr = "Listening on"
)

var _ chainapi.Chain = &Anvil{}

func New(log log.Logger, cfg *Config) *Anvil {
resCtx, resCancel := context.WithCancel(context.Background())
return &Anvil{
Expand All @@ -84,9 +72,9 @@ func (a *Anvil) Start(ctx context.Context) error {

args := []string{
"--host", host,
"--accounts", fmt.Sprintf("%d", a.cfg.Accounts),
"--mnemonic", a.cfg.Mnemonic,
"--derivation-path", a.cfg.DerivationPath,
"--accounts", fmt.Sprintf("%d", a.cfg.SecretsConfig.Accounts),
"--mnemonic", a.cfg.SecretsConfig.Mnemonic,
"--derivation-path", a.cfg.SecretsConfig.DerivationPath.String(),
"--chain-id", fmt.Sprintf("%d", a.cfg.ChainID),
"--port", fmt.Sprintf("%d", a.cfg.Port),
}
Expand All @@ -107,7 +95,7 @@ func (a *Anvil) Start(ctx context.Context) error {
"--fork-block-number", fmt.Sprintf("%d", a.cfg.ForkConfig.BlockNumber))
}

anvilLog := a.log.New("role", "anvil", "chain.id", a.cfg.ChainID)
anvilLog := a.log.New("role", "anvil", "name", a.cfg.Name, "chain.id", a.cfg.ChainID)
anvilLog.Debug("generated cmd arguments", "args", args)

a.cmd = exec.CommandContext(a.resourceCtx, "anvil", args...)
Expand Down Expand Up @@ -162,7 +150,7 @@ func (a *Anvil) Start(ctx context.Context) error {
}()

// Start anvil
anvilLog.Info("starting anvil")
anvilLog.Debug("starting anvil")
if err := a.cmd.Start(); err != nil {
return fmt.Errorf("failed to start anvil: %w", err)
}
Expand All @@ -171,7 +159,7 @@ func (a *Anvil) Start(ctx context.Context) error {
if err := a.cmd.Wait(); err != nil {
anvilLog.Error("anvil terminated with an error", "error", err)
} else {
anvilLog.Info("anvil terminated")
anvilLog.Debug("anvil terminated")
}

a.stoppedCh <- struct{}{}
Expand All @@ -193,9 +181,7 @@ func (a *Anvil) Start(ctx context.Context) error {
return fmt.Errorf("failed to create RPC client: %w", err)
}
a.rpcClient = rpcClient

a.ethClient = ethclient.NewClient(rpcClient)

return nil
}

Expand Down Expand Up @@ -225,6 +211,10 @@ func (a *Anvil) wsEndpoint() string {
return fmt.Sprintf("ws://%s:%d", host, a.cfg.Port)
}

func (a *Anvil) Name() string {
return a.cfg.Name
}

func (a *Anvil) ChainID() uint64 {
return a.cfg.ChainID
}
Expand Down Expand Up @@ -268,7 +258,7 @@ func (a *Anvil) WaitUntilReady(ctx context.Context) error {

func (a *Anvil) String() string {
var b strings.Builder
fmt.Fprintf(&b, "Chain ID: %d RPC: %s LogPath: %s", a.ChainID(), a.Endpoint(), a.LogPath())
fmt.Fprintf(&b, "Name: %s Chain ID: %d RPC: %s LogPath: %s", a.Name(), a.ChainID(), a.Endpoint(), a.LogPath())
return b.String()
}

Expand Down
6 changes: 4 additions & 2 deletions anvil/anvil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"testing"

"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/stretchr/testify/require"
"github.com/ethereum-optimism/supersim/config"

"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"

"github.com/stretchr/testify/require"
)

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

Expand Down
22 changes: 0 additions & 22 deletions chainapi/chainapi.go

This file was deleted.

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

import (
"context"
"fmt"
"strings"

"github.com/ethereum-optimism/supersim/hdaccount"

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

var (
DefaultSecretsConfig = SecretsConfig{
Accounts: 10,
Mnemonic: "test test test test test test test test test test test junk",
DerivationPath: accounts.DefaultRootDerivationPath,
}

DefaultChainConfigs = []ChainConfig{
{
Name: "SourceChain",
ChainID: 1,
SecretsConfig: DefaultSecretsConfig,
},
{
Name: "OPChainA",
ChainID: 10,
SourceChainID: 1,
SecretsConfig: DefaultSecretsConfig,
},
{
Name: "OPChainB",
ChainID: 30,
SourceChainID: 1,
SecretsConfig: DefaultSecretsConfig,
},
}
)

type ForkConfig struct {
RPCUrl string
BlockNumber uint64
}

type SecretsConfig struct {
Accounts uint64
Mnemonic string
DerivationPath accounts.DerivationPath
}

type ChainConfig struct {
Name string
Port uint64

ChainID uint64
SourceChainID uint64 // "L1"

SecretsConfig SecretsConfig

// Optional Config
ForkConfig *ForkConfig
}

type Chain interface {
Name() string
Endpoint() string
ChainID() uint64
LogPath() string

// API methods
EthSendTransaction(ctx context.Context, tx *types.Transaction) error
EthGetLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)
SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)
}

// Note: The default secrets config is used everywhere
func DefaultSecretsConfigAsString() string {
hdAccountStore, err := hdaccount.NewHdAccountStore(DefaultSecretsConfig.Mnemonic, DefaultSecretsConfig.DerivationPath)
if err != nil {
panic(err)
}

var b strings.Builder

fmt.Fprintf(&b, "\nAvailable Accounts\n")
fmt.Fprintf(&b, "-----------------------\n")

for i := range DefaultSecretsConfig.Accounts {
addressHex, _ := hdAccountStore.AddressHexAt(uint32(i))
fmt.Fprintf(&b, "(%d): %s\n", i, addressHex)
}

fmt.Fprintf(&b, "\nPrivate Keys\n")
fmt.Fprintf(&b, "-----------------------\n")

for i := range DefaultSecretsConfig.Accounts {
privateKeyHex, _ := hdAccountStore.PrivateKeyHexAt(uint32(i))
fmt.Fprintf(&b, "(%d): %s\n", i, privateKeyHex)
}

return b.String()
}
5 changes: 3 additions & 2 deletions opsimulator/deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"fmt"

"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum-optimism/supersim/chainapi"
"github.com/ethereum-optimism/supersim/config"

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

Expand All @@ -32,7 +33,7 @@ func (d *DepositTxSubscription) Err() <-chan error {
}

// transforms Deposit event logs into DepositTx
func SubscribeDepositTx(ctx context.Context, l1Chain chainapi.Chain, depositContractAddr common.Address, ch chan<- *types.DepositTx) (ethereum.Subscription, error) {
func SubscribeDepositTx(ctx context.Context, l1Chain config.Chain, depositContractAddr common.Address, ch chan<- *types.DepositTx) (ethereum.Subscription, error) {
f := ethereum.FilterQuery{
Addresses: []common.Address{depositContractAddr},
Topics: [][]common.Hash{{derive.DepositEventABIHash}},
Expand Down
12 changes: 6 additions & 6 deletions opsimulator/deposits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ import (

"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
optestutils "github.com/ethereum-optimism/optimism/op-service/testutils"
"github.com/stretchr/testify/require"

"github.com/ethereum-optimism/supersim/chainapi"
"github.com/ethereum-optimism/supersim/config"
"github.com/ethereum-optimism/supersim/testutils"

"github.com/ethereum/go-ethereum/common"

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

"github.com/stretchr/testify/require"
)

var _ config.Chain = &MockChainWithSubscriptions{}

func createMockDepositTxs() []*types.DepositTx {
num := 10
out := make([]*types.DepositTx, num)
Expand All @@ -36,8 +38,6 @@ func createMockDepositTxs() []*types.DepositTx {
return out
}

var _ chainapi.Chain = &MockChainWithSubscriptions{}

type MockChainWithSubscriptions struct {
*testutils.MockChain
mockDepositTxs []*types.DepositTx
Expand Down
Loading

0 comments on commit 6073ce7

Please sign in to comment.