Skip to content

Commit

Permalink
feat: apply SuperchainWETH contracts to forked networks
Browse files Browse the repository at this point in the history
  • Loading branch information
tremarkley committed Sep 16, 2024
1 parent 073392f commit 0c0b75d
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 6 deletions.
5 changes: 5 additions & 0 deletions anvil/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"math/big"
"os"
"os/exec"
"strconv"
Expand Down Expand Up @@ -248,6 +249,10 @@ func (a *Anvil) SetStorageAt(ctx context.Context, result interface{}, address co
return a.rpcClient.CallContext(ctx, result, "anvil_setStorageAt", address.Hex(), storageSlot, storageValue)
}

func (a *Anvil) SetBalance(ctx context.Context, result interface{}, address common.Address, value *big.Int) error {
return a.rpcClient.CallContext(ctx, result, "anvil_setBalance", address.Hex(), hexutil.EncodeBig(value))
}

func (a *Anvil) SetIntervalMining(ctx context.Context, result interface{}, interval int64) error {
return a.rpcClient.CallContext(ctx, result, "evm_setIntervalMining", interval)
}
Expand Down
7 changes: 7 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"

"github.com/joho/godotenv"
"github.com/urfave/cli/v2"
)

Expand All @@ -34,6 +35,12 @@ const (
)

func main() {
// Load the .env file
err := godotenv.Load()
if err != nil {
log.Crit("Error loading .env file", "err", err)
}

oplog.SetupDefaults()
logFlags := oplog.CLIFlags(envVarPrefix)

Expand Down
2 changes: 2 additions & 0 deletions config/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"context"
"fmt"
"math/big"
"strings"

registry "github.com/ethereum-optimism/superchain-registry/superchain"
Expand Down Expand Up @@ -81,6 +82,7 @@ type Chain interface {
SimulatedLogs(ctx context.Context, tx *types.Transaction) ([]types.Log, error)
SetCode(ctx context.Context, result interface{}, address common.Address, code string) error
SetStorageAt(ctx context.Context, result interface{}, address common.Address, storageSlot string, storageValue string) error
SetBalance(ctx context.Context, result interface{}, address common.Address, value *big.Int) error
SetIntervalMining(ctx context.Context, result interface{}, interval int64) error

// Lifecycle
Expand Down
2 changes: 1 addition & 1 deletion contracts/lib/optimism
Submodule optimism updated 37 files
+0 −1 op-chain-ops/interopgen/deploy.go
+0 −1 op-chain-ops/interopgen/deployers/implementations.go
+0 −9 op-chain-ops/script/deterministic.go
+0 −7 op-chain-ops/script/prank.go
+0 −14 op-chain-ops/script/script.go
+4 −6 op-chain-ops/script/script_test.go
+38 −39 op-e2e/actions/proofs/channel_timeout_test.go
+17 −17 op-e2e/actions/proofs/env.go
+1 −1 op-e2e/actions/proofs/fixture.go
+29 −30 op-e2e/actions/proofs/garbage_channel_test.go
+1 −1 op-e2e/actions/proofs/matrix.go
+21 −22 op-e2e/actions/proofs/sequence_window_expiry_test.go
+20 −21 op-e2e/actions/proofs/simple_program_test.go
+0 −31 op-e2e/e2eutils/setuputils/utils.go
+0 −85 op-e2e/interop/interop_test.go
+0 −616 op-e2e/interop/supersystem.go
+1 −2 op-e2e/sequencer_failover_setup.go
+23 −4 op-e2e/setup.go
+3 −7 op-e2e/tx_helper.go
+1 −20 op-service/sources/supervisor_client.go
+3 −7 op-supervisor/supervisor/backend/backend.go
+0 −3 op-supervisor/supervisor/backend/source/chain_processor.go
+1 −9 op-supervisor/supervisor/service.go
+24 −51 packages/contracts-bedrock/scripts/DeployImplementations.s.sol
+0 −1 packages/contracts-bedrock/scripts/DeployOPChain.s.sol
+0 −12 packages/contracts-bedrock/scripts/libraries/DeployUtils.sol
+2 −2 packages/contracts-bedrock/semver-lock.json
+79 −104 packages/contracts-bedrock/snapshots/abi/OPStackManager.json
+79 −104 packages/contracts-bedrock/snapshots/abi/OPStackManagerInterop.json
+4 −18 packages/contracts-bedrock/snapshots/storageLayout/OPStackManager.json
+4 −18 packages/contracts-bedrock/snapshots/storageLayout/OPStackManagerInterop.json
+29 −32 packages/contracts-bedrock/src/L1/OPStackManager.sol
+4 −3 packages/contracts-bedrock/src/L1/OPStackManagerInterop.sol
+3 −47 packages/contracts-bedrock/test/DeployImplementations.t.sol
+12 −9 packages/contracts-bedrock/test/L1/OPStackManager.t.sol
+2 −2 packages/contracts-bedrock/test/Specs.t.sol
+1 −4 packages/contracts-bedrock/test/vendor/Initializable.t.sol
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/ethereum-optimism/optimism v1.9.1-0.20240808190618-0c8d7c8c5186
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20240803025447-c92ef420eec2
github.com/ethereum/go-ethereum v1.13.15
github.com/joho/godotenv v1.5.1
github.com/stretchr/testify v1.9.0
github.com/tyler-smith/go-bip39 v1.1.0
github.com/urfave/cli/v2 v2.27.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0
github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
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=
Expand Down
22 changes: 22 additions & 0 deletions interop/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var interopPredeploys = []common.Address{
predeploys.CrossL2InboxAddr,
predeploys.L2toL2CrossDomainMessengerAddr,
predeploys.L1BlockAddr,
common.HexToAddress("0x4200000000000000000000000000000000000024"),
common.HexToAddress("0x4200000000000000000000000000000000000025"),
}

type predeploy struct {
Expand Down Expand Up @@ -114,6 +116,14 @@ func applyAllocToAddress(ctx context.Context, chain config.Chain, alloc *genesis
}
}
}

balance := hexStringToBigInt(alloc.Balance)
if balance.Cmp(big.NewInt(0)) > 0 {
if err := chain.SetBalance(ctx, nil, address, balance); err != nil {
return fmt.Errorf("failed to set balance for %s: %w", address, err)
}
}

return nil
}

Expand Down Expand Up @@ -177,3 +187,15 @@ func waitMinedWithTicker(ctx context.Context, b bind.DeployBackend, tx *types.Tr
}
}
}

func hexStringToBigInt(hexString string) *big.Int {
// Remove the "0x" prefix if it's present
if len(hexString) > 1 && hexString[:2] == "0x" {
hexString = hexString[2:]
}

bigInt := new(big.Int)
bigInt.SetString(hexString, 16)

return bigInt
}
4 changes: 2 additions & 2 deletions orchestrator/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NetworkConfigFromForkCLIConfig(log log.Logger, envPrefix string, forkConfig
// L1
l1RpcUrl := superchain.Config.L1.PublicRPC
if url, ok := os.LookupEnv(fmt.Sprintf("%s_RPC_URL_%s", envPrefix, strings.ToUpper(forkConfig.Network))); ok {
log.Info("detected rpc override", "name", forkConfig.Network, "url", url)
log.Info("detected rpc override", "name", forkConfig.Network)
l1RpcUrl = url
}

Expand Down Expand Up @@ -69,7 +69,7 @@ func NetworkConfigFromForkCLIConfig(log log.Logger, envPrefix string, forkConfig

l2RpcUrl := chainCfg.PublicRPC
if url, ok := os.LookupEnv(fmt.Sprintf("%s_RPC_URL_%s", envPrefix, strings.ToUpper(chainCfg.Chain))); ok {
log.Info("detected rpc override", "name", chainCfg.Chain, "url", url)
log.Info("detected rpc override", "name", chainCfg.Chain)
l2RpcUrl = url
}

Expand Down
62 changes: 59 additions & 3 deletions supersim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ethereum-optimism/supersim/bindings"
"github.com/ethereum-optimism/supersim/config"
"github.com/ethereum-optimism/supersim/hdaccount"
"github.com/joho/godotenv"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down Expand Up @@ -79,6 +80,11 @@ type InteropTestSuite struct {
}

func createTestSuite(t *testing.T, cliConfig *config.CLIConfig) *TestSuite {
// Load the .env file
err := godotenv.Load()
if err != nil {
log.Crit("Error loading .env file", "err", err)
}
testlog := testlog.Logger(t, log.LevelInfo)
hdAccountStore, err := hdaccount.NewHdAccountStore(defaultTestMnemonic, defaultTestMnemonicDerivationPath)
if err != nil {
Expand All @@ -87,7 +93,7 @@ func createTestSuite(t *testing.T, cliConfig *config.CLIConfig) *TestSuite {
}

ctx, closeApp := context.WithCancelCause(context.Background())
supersim, _ := NewSupersim(testlog, "", closeApp, cliConfig)
supersim, _ := NewSupersim(testlog, "SUPERSIM", closeApp, cliConfig)
t.Cleanup(func() {
closeApp(nil)
if err := supersim.Stop(context.Background()); err != nil {
Expand All @@ -108,7 +114,11 @@ func createTestSuite(t *testing.T, cliConfig *config.CLIConfig) *TestSuite {
}
}

func createForkedInteropTestSuite(t *testing.T) *InteropTestSuite {
type ForkInteropTestSuiteOptions struct {
interopAutoRelay bool
}

func createForkedInteropTestSuite(t *testing.T, testOptions ForkInteropTestSuiteOptions) *InteropTestSuite {
srcChain := "op"
destChain := "base"
cliConfig := &config.CLIConfig{
Expand All @@ -117,6 +127,7 @@ func createForkedInteropTestSuite(t *testing.T) *InteropTestSuite {
Network: "mainnet",
InteropEnabled: true,
},
InteropAutoRelay: testOptions.interopAutoRelay,
}
superchain := registry.Superchains[cliConfig.ForkConfig.Network]
srcChainCfg := config.OPChainByName(superchain, srcChain)
Expand Down Expand Up @@ -667,7 +678,7 @@ func TestInteropInvariantCheckBadBlockTimestamp(t *testing.T) {
}

func TestForkedInteropInvariantCheckSucceeds(t *testing.T) {
testSuite := createForkedInteropTestSuite(t)
testSuite := createForkedInteropTestSuite(t, ForkInteropTestSuiteOptions{interopAutoRelay: false})

privateKey, err := testSuite.HdAccountStore.DerivePrivateKeyAt(uint32(0))
require.NoError(t, err)
Expand Down Expand Up @@ -820,3 +831,48 @@ func TestAutoRelaySuperchainWETHTransferSucceeds(t *testing.T) {
diff := new(big.Int).Sub(destEndingBalance, destStartingBalance)
require.Equal(t, valueToTransfer, diff)
}

func TestForkAutoRelaySuperchainWETHTransferSucceeds(t *testing.T) {
testSuite := createForkedInteropTestSuite(t, ForkInteropTestSuiteOptions{interopAutoRelay: true})

privateKey, err := testSuite.HdAccountStore.DerivePrivateKeyAt(uint32(0))
require.NoError(t, err)

sourceTransactor, err := bind.NewKeyedTransactorWithChainID(privateKey, testSuite.SourceChainID)
require.NoError(t, err)

sourceSuperchainWETH, err := bindings.NewSuperchainWETH(common.HexToAddress(superchainWETHAddr), testSuite.SourceEthClient)
require.NoError(t, err)

destSuperchainWETH, err := bindings.NewSuperchainWETH(common.HexToAddress(superchainWETHAddr), testSuite.DestEthClient)
require.NoError(t, err)
valueToTransfer := big.NewInt(10_000_000)

sourceTransactor.Value = valueToTransfer
depositTx, err := sourceSuperchainWETH.Deposit(sourceTransactor)
require.NoError(t, err)
depositTxReceipt, err := bind.WaitMined(context.Background(), testSuite.SourceEthClient, depositTx)
require.NoError(t, err)
require.True(t, depositTxReceipt.Status == 1, "weth deposit transaction failed")
sourceTransactor.Value = nil

destStartingBalance, err := destSuperchainWETH.BalanceOf(&bind.CallOpts{}, sourceTransactor.From)
require.NoError(t, err)

_, err = sourceSuperchainWETH.BalanceOf(&bind.CallOpts{}, sourceTransactor.From)
require.NoError(t, err)

tx, err := sourceSuperchainWETH.SendERC20(sourceTransactor, sourceTransactor.From, valueToTransfer, testSuite.DestChainID)
require.NoError(t, err)

initiatingMessageTxReceipt, err := bind.WaitMined(context.Background(), testSuite.SourceEthClient, tx)
require.NoError(t, err)
require.True(t, initiatingMessageTxReceipt.Status == 1, "initiating message transaction failed")

time.Sleep(time.Second * 4)

destEndingBalance, err := destSuperchainWETH.BalanceOf(&bind.CallOpts{}, sourceTransactor.From)
require.NoError(t, err)
diff := new(big.Int).Sub(destEndingBalance, destStartingBalance)
require.Equal(t, valueToTransfer, diff)
}
5 changes: 5 additions & 0 deletions testutils/mockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testutils

import (
"context"
"math/big"

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

Expand Down Expand Up @@ -66,6 +67,10 @@ func (c *MockChain) SetCode(ctx context.Context, result interface{}, address com
return nil
}

func (c *MockChain) SetBalance(ctx context.Context, result interface{}, address common.Address, value *big.Int) error {
return nil
}

func (c *MockChain) SetStorageAt(ctx context.Context, result interface{}, address common.Address, storageSlot string, storageValue string) error {
return nil
}
Expand Down

0 comments on commit 0c0b75d

Please sign in to comment.