Skip to content

Commit

Permalink
test suite creator
Browse files Browse the repository at this point in the history
  • Loading branch information
hamdiallam committed Jul 9, 2024
1 parent a30767b commit 803a3de
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 47 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ go 1.22.3
require (
github.com/ethereum-optimism/optimism v1.7.7
github.com/ethereum/go-ethereum v1.13.15
github.com/stretchr/testify v1.9.0
github.com/urfave/cli/v2 v2.27.1
)

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20240614103325-d8902381f5d8 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
Expand Down
16 changes: 14 additions & 2 deletions supersim.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,20 @@ var genesisL1JSON []byte
var genesisL2JSON []byte

var DefaultConfig = Config{
l1Chain: Chain{anvilConfig: anvil.Config{ChainId: 1, Port: 8545, Genesis: genesisL1JSON}, opSimConfig: op_simulator.Config{Port: 8546}},
l2Chains: []Chain{{anvilConfig: anvil.Config{ChainId: 10, Port: 9545, Genesis: genesisL2JSON}, opSimConfig: op_simulator.Config{Port: 9546}}, {anvilConfig: anvil.Config{ChainId: 30, Port: 9555, Genesis: genesisL2JSON}, opSimConfig: op_simulator.Config{Port: 9556}}},
l1Chain: Chain{
anvilConfig: anvil.Config{ChainId: 1, Port: 8545, Genesis: genesisL1JSON},
opSimConfig: op_simulator.Config{Port: 8546},
},
l2Chains: []Chain{
{
anvilConfig: anvil.Config{ChainId: 10, Port: 9545, Genesis: genesisL2JSON},
opSimConfig: op_simulator.Config{Port: 9546},
},
{
anvilConfig: anvil.Config{ChainId: 30, Port: 9555, Genesis: genesisL2JSON},
opSimConfig: op_simulator.Config{Port: 9556},
},
},
}

type Supersim struct {
Expand Down
102 changes: 57 additions & 45 deletions supersim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package supersim

import (
"context"
"fmt"
"log"
"os"
"testing"
"time"

oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/stretchr/testify/require"

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

Expand All @@ -21,57 +20,70 @@ const (
l1BlockAddress = "0x4200000000000000000000000000000000000015"
)

func TestGenesisState(t *testing.T) {
logger := oplog.NewLogger(os.Stderr, oplog.DefaultCLIConfig())
supersim := NewSupersim(logger, &DefaultConfig)
err := supersim.Start(context.Background())

defer func() {
err := supersim.Stop(context.Background())
if err != nil {
t.Fatalf("Failed to stop supersim: %v", err)
type TestSuite struct {
t *testing.T

Cfg *Config
Supersim *Supersim
}

func createTestSuite(t *testing.T) *TestSuite {
cfg := &DefaultConfig

testlog := testlog.Logger(t, log.LevelInfo)
supersim := NewSupersim(testlog, cfg)
t.Cleanup(func() {
if err := supersim.Stop(context.Background()); err != nil {
t.Errorf("failed to stop supersim: %s", err)
}
}()
})

if err != nil {
t.Fatalf("Failed to start supersim: %v", err)
if err := supersim.Start(context.Background()); err != nil {
t.Fatalf("unable to start supersim: %s", err)
return nil
}

for _, l2ChainConfig := range DefaultConfig.l2Chains {
rpcUrl := fmt.Sprintf("http://127.0.0.1:%d", l2ChainConfig.opSimConfig.Port)
client, clientCreateErr := rpc.Dial(rpcUrl)
return &TestSuite{
t: t,
Cfg: cfg,
Supersim: supersim,
}
}

if clientCreateErr != nil {
t.Fatalf("Failed to create client: %v", clientCreateErr)
}
func TestStartup(t *testing.T) {
testSuite := createTestSuite(t)

// test that all chains can be queried
client, err := rpc.Dial(testSuite.Supersim.l1OpSim.Endpoint())
require.NoError(t, err)
require.NoError(t, client.CallContext(context.Background(), nil, "eth_chainId"))
client.Close()

for _, l2Chain := range testSuite.Supersim.l2OpSims {
client, err := rpc.Dial(l2Chain.Endpoint())
require.NoError(t, err)
require.NoError(t, client.CallContext(context.Background(), nil, "eth_chainId"))
client.Close()
}
}

func TestGenesisState(t *testing.T) {
testSuite := createTestSuite(t)

// assert that the predeploys exists on the l2 anvil instances
for _, l2Chain := range testSuite.Supersim.l2OpSims {
client, err := rpc.Dial(l2Chain.Endpoint())
require.NoError(t, err)
defer client.Close()

var code string
err := client.CallContext(context.Background(), &code, "eth_getCode", crossL2InboxAddress, "latest")
if err != nil {
log.Fatalf("Failed to get code: %v", err)
}
require.NoError(t, client.CallContext(context.Background(), &code, "eth_getCode", crossL2InboxAddress, "latest"))
require.NotEqual(t, code, emptyCode, "CrossL2Inbox is not deployed")

if code == emptyCode {
t.Error("CrossL2Inbox is not deployed")
}
require.NoError(t, client.CallContext(context.Background(), &code, "eth_getCode", l2toL2CrossDomainMessengerAddress, "latest"))
require.NotEqual(t, code, emptyCode, "L2ToL2CrosSDomainMessenger is not deployed")

err = client.CallContext(context.Background(), &code, "eth_getCode", l2toL2CrossDomainMessengerAddress, "latest")
if err != nil {
log.Fatalf("Failed to get code: %v", err)
}
if code == emptyCode {
t.Error("L2toL2CrossDomainMessenger is not deployed")
}

err = client.CallContext(context.Background(), &code, "eth_getCode", l1BlockAddress, "latest")
if err != nil {
log.Fatalf("Failed to get code: %v", err)
}
if code == emptyCode {
t.Error("L1Block is not deployed")
}
require.NoError(t, client.CallContext(context.Background(), &code, "eth_getCode", l1BlockAddress, "latest"))
require.NotEqual(t, code, emptyCode, "L1Block is not deployed")
}

}

0 comments on commit 803a3de

Please sign in to comment.