Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
70abccf
Convert add-chain.sh into a go package
bitwiseguy Apr 23, 2024
0b4d631
Create go module
bitwiseguy Apr 24, 2024
4de3faa
Fixes to produce correct output files from addchain module
bitwiseguy Apr 24, 2024
22b4748
Rename go module addchain to add-chain
bitwiseguy Apr 24, 2024
1b1acea
Remove duplicate invocation of registry-data go program
bitwiseguy Apr 24, 2024
9afdef3
Add conditional when searching for contract addrs from file
bitwiseguy Apr 24, 2024
4969abd
Add e2e test for add-chain go module
bitwiseguy Apr 24, 2024
9c04805
Finish removing add-chain go test dep on monorepo
bitwiseguy Apr 25, 2024
175f5ae
Rename add-chain test files to expected.[json|yam]l
bitwiseguy Apr 25, 2024
af89ef1
Merge branch 'main' into ss/addchain-go
bitwiseguy Apr 25, 2024
7205dbe
Retrieve L1 url from superchain package instead of hardcoding
bitwiseguy Apr 26, 2024
1b2967a
Use consistent go 1.21 version in all go.mod files
bitwiseguy Apr 26, 2024
61ea05f
Read contract addresses from .deploy file within add-chain
bitwiseguy Apr 26, 2024
cb6864a
Move add-chain/.env.test into testdata dir
bitwiseguy Apr 26, 2024
9b4f82e
Merge branch 'main' into ss/addchain-go
bitwiseguy Apr 26, 2024
2fd6532
Refactor 'cast call' commands to reduce duplicate code
bitwiseguy Apr 26, 2024
ccd2fdd
Fix call to OptimismPortalProxy.guardian() - instead of calling GUARD…
bitwiseguy Apr 26, 2024
c331023
Removed unused genesis.json file from add-chain/testdata dir
bitwiseguy Apr 26, 2024
7013cd3
Use custom yaml encoder to add whitespace
bitwiseguy Apr 26, 2024
bcdfb7c
Compare config raw bytes instead of structs in e2e test
bitwiseguy Apr 26, 2024
c5c6afe
Add human readable timestamp as comments in yaml file
bitwiseguy Apr 27, 2024
dda8220
Remove hardfork timestamp overrides if they match superchain defaults
bitwiseguy Apr 28, 2024
9446436
Move enhanceYAML to a method of RollupConfig
bitwiseguy Apr 28, 2024
354b976
Add godoc comments to a few functions
bitwiseguy Apr 29, 2024
80fad15
improve cast call error handling (#215)
geoknee Apr 29, 2024
7f55ffa
Merge branch 'main' into ss/addchain-go
bitwiseguy Apr 29, 2024
50b1c85
Address PR comments
bitwiseguy May 2, 2024
bb991a1
Run gofumpt and golangci-lint
bitwiseguy May 2, 2024
ef4fb1d
Install foundry in Circle CI for cast call in tests
bitwiseguy May 2, 2024
1a26e4c
Fix timestamp to use consistent UTC+0 timezone
bitwiseguy May 2, 2024
6b8483f
Set Circle CI shell type to bash
bitwiseguy May 2, 2024
019fe31
Move shell type to job level in Circle CI
bitwiseguy May 2, 2024
5f09d55
Merge branch 'main' into ss/addchain-go
bitwiseguy May 3, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ docs/
.env
.env*
!.env.example
!.env.test
*.log


Expand Down
10 changes: 10 additions & 0 deletions add-chain/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Required for both Standard Chains and Frontier Chains
Comment thread
bitwiseguy marked this conversation as resolved.
CHAIN_NAME=awesomechain # L2 chain name
SUPERCHAIN_TARGET=sepolia # L1 chain name
MONOREPO_DIR=./testdata/monorepo
DEPLOYMENTS_DIR=${MONOREPO_DIR}/deployments
ROLLUP_CONFIG=${MONOREPO_DIR}/op-node/rollup.json
GENESIS_CONFIG=${MONOREPO_DIR}/op-node/genesis.json
PUBLIC_RPC="http://awe.some.rpc" # L2 RPC URL
SEQUENCER_RPC="http://awe.some.seq.rpc"
EXPLORER="https://awesomescan.org" # L2 block explorer URL
1 change: 1 addition & 0 deletions add-chain/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testdata/**/awesomechain*
Comment thread
bitwiseguy marked this conversation as resolved.
119 changes: 119 additions & 0 deletions add-chain/chain_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package main

import (
"encoding/json"
"fmt"
"os"
"path/filepath"

"gopkg.in/yaml.v3"
)

type RollupConfig struct {
Name string `yaml:"name"`
L2ChainID uint64 `json:"l2_chain_id" yaml:"chain_id"`
PublicRPC string `yaml:"public_rpc"`
SequencerRPC string `yaml:"sequencer_rpc"`
Explorer string `yaml:"explorer"`
SuperchainLevel int `yaml:"superchain_level"`
BatchInboxAddr string `json:"batch_inbox_address" yaml:"batch_inbox_addr"`
Genesis GenesisData `json:"genesis" yaml:"genesis"`
CanyonTime *int `json:"canyon_time,omitempty" yaml:"canyon_time"`
DeltaTime *int `json:"delta_time,omitempty" yaml:"delta_time"`
EcotoneTime *int `json:"ecotone_time,omitempty" yaml:"ecotone_time"`
}

type GenesisData struct {
L1 GenesisLayer `json:"l1" yaml:"l1"`
L2 GenesisLayer `json:"l2" yaml:"l2"`
L2Time int `json:"l2_time" yaml:"l2_time"`
SystemConfig SystemConfig `json:"system_config" yaml:"system_config,omitempty"`
}

type SystemConfig struct {
BatcherAddr string `json:"batcherAddr"`
Overhead string `json:"overhead"`
Scalar string `json:"scalar"`
GasLimit uint64 `json:"gasLimit"`
BaseFeeScalar uint64 `json:"baseFeeScalar"`
BlobBaseFeeScalar uint64 `json:"blobBaseFeeScalar"`
}

type GenesisLayer struct {
Hash string `json:"hash" yaml:"hash"`
Number int `json:"number" yaml:"number"`
}

func constructRollupConfig(filePath, chainName, publicRPC, sequencerRPC, explorer string, superchainLevel int) (RollupConfig, error) {
fmt.Printf("Attempting to read from %s\n", filePath)
file, err := os.ReadFile(filePath)
if err != nil {
return RollupConfig{}, fmt.Errorf("error reading file: %v", err)
}
var config RollupConfig
if err = json.Unmarshal(file, &config); err != nil {
return RollupConfig{}, fmt.Errorf("error unmarshaling json: %v", err)
}

config.Name = chainName
config.PublicRPC = publicRPC
config.SequencerRPC = sequencerRPC
config.SuperchainLevel = superchainLevel
config.Explorer = explorer

fmt.Printf("Rollup config successfully constructed\n")
return config, nil
}

func writeChainConfig(
inputFilepath string,
targetDirectory string,
chainName string,
publicRPC string,
sequencerRPC string,
explorer string,
superchainLevel int,
superchainRepoPath string,
superchainTarget string,
) error {
rollupConfig, err := constructRollupConfig(inputFilepath, chainName, publicRPC, sequencerRPC, explorer, superchainLevel)
if err != nil {
return fmt.Errorf("failed to construct rollup config: %w", err)
}

// create genesis-system-config data
// (this is deprecated, users should load this from L1, when available via SystemConfig)
dirPath := filepath.Join(superchainRepoPath, "superchain", "extra", "genesis-system-configs", superchainTarget)

// Ensure the directory exists
if err := os.MkdirAll(dirPath, 0755); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}

systemConfigJSON, err := json.MarshalIndent(rollupConfig.Genesis.SystemConfig, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal genesis system config json: %w", err)
}

// Write the genesis system config JSON to a new file
filePath := filepath.Join(dirPath, chainName+".json")
if err := os.WriteFile(filePath, systemConfigJSON, 0644); err != nil {
return fmt.Errorf("failed to write genesis system config json: %w", err)
}
fmt.Printf("Genesis system config written to: %s\n", filePath)

rollupConfig.Genesis.SystemConfig = SystemConfig{} // remove SystemConfig so its omitted from yaml
yamlData, err := yaml.Marshal(rollupConfig)
if err != nil {
return fmt.Errorf("failed to marshal yaml: %w", err)
}

filename := filepath.Join(targetDirectory)
err = os.WriteFile(filename, yamlData, 0644)
if err != nil {
return fmt.Errorf("failed to write yaml file: %w", err)
}
fmt.Printf("Rollup config written to: %s\n", filename)

return nil
}
159 changes: 159 additions & 0 deletions add-chain/contract_addresses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package main

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
)

type AddressData struct {
Address string `json:"address"`
}

var (
// Addresses to retrieve from JSON
Comment thread
bitwiseguy marked this conversation as resolved.
AddressManager = "AddressManager"
L1CrossDomainMessengerProxy = "L1CrossDomainMessengerProxy"
L1ERC721BridgeProxy = "L1ERC721BridgeProxy"
L1StandardBridgeProxy = "L1StandardBridgeProxy"
L2OutputOracleProxy = "L2OutputOracleProxy"
OptimismMintableERC20FactoryProxy = "OptimismMintableERC20FactoryProxy"
SystemConfigProxy = "SystemConfigProxy"
OptimismPortalProxy = "OptimismPortalProxy"
ProxyAdmin = "ProxyAdmin"

// Addresses to retrieve from chain
SuperchainConfig = "SuperchainConfig"
Guardian = "Guardian"
Challenger = "Challenger"
ProxyAdminOwner = "ProxyAdminOwner"
SystemConfigOwner = "SystemConfigOwner"
)

func readAddressesFromChain(contractAddresses map[string]string, l1RpcUrl string) error {
// SuperchainConfig
address, err := executeCommand("cast", []string{"call", contractAddresses[OptimismPortalProxy], "superchainConfig()(address)", "-r", l1RpcUrl})
address = strings.Join(strings.Fields(address), "") // remove whitespace
if err != nil || address == "" || address == "0x" {
contractAddresses[SuperchainConfig] = ""
} else {
contractAddresses[SuperchainConfig] = address
}

// Guardian
address, err = executeCommand("cast", []string{"call", contractAddresses[SuperchainConfig], "guardian()(address)", "-r", l1RpcUrl})
Comment thread
bitwiseguy marked this conversation as resolved.
Outdated
address = strings.Join(strings.Fields(address), "") // remove whitespace
if err != nil || address == "" || address == "0x" {
address, err = executeCommand("cast", []string{"call", contractAddresses[OptimismPortalProxy], "GUARDIAN()(address)", "-r", l1RpcUrl})
address = strings.Join(strings.Fields(address), "") // remove whitespace
if err != nil || address == "" || address == "0x" {
return fmt.Errorf("could not retrieve address for Guardian")
}
contractAddresses[Guardian] = address
} else {
contractAddresses[Guardian] = address
}

// Challenger
address, err = executeCommand("cast", []string{"call", contractAddresses[L2OutputOracleProxy], "challenger()(address)", "-r", l1RpcUrl})
address = strings.Join(strings.Fields(address), "") // remove whitespace
if err != nil || address == "" || address == "0x" {
return fmt.Errorf("could not retrieve address for Guardian")
} else {
contractAddresses[Challenger] = address
}

// ProxyAdminOwner
address, err = executeCommand("cast", []string{"call", contractAddresses[ProxyAdmin], "owner()(address)", "-r", l1RpcUrl})
address = strings.Join(strings.Fields(address), "") // remove whitespace
if err != nil || address == "" || address == "0x" {
return fmt.Errorf("could not retrieve address for ProxyAdminOwner")
} else {
contractAddresses[ProxyAdminOwner] = address
}

// SystemConfigOwner
address, err = executeCommand("cast", []string{"call", contractAddresses[SystemConfigProxy], "owner()(address)", "-r", l1RpcUrl})
address = strings.Join(strings.Fields(address), "") // remove whitespace
if err != nil || address == "" || address == "0x" {
return fmt.Errorf("could not retrieve address for ProxyAdminOwner")
} else {
contractAddresses[SystemConfigOwner] = address
}
fmt.Printf("Contract addresses read from on-chain contracts\n")

return nil
}

func readAddressesFromJSON(contractAddresses map[string]string, deploymentsDir string) error {
var contractsFromJSON = []string{
AddressManager,
L1CrossDomainMessengerProxy,
L1ERC721BridgeProxy,
L1StandardBridgeProxy,
L2OutputOracleProxy,
OptimismMintableERC20FactoryProxy,
SystemConfigProxy,
OptimismPortalProxy,
ProxyAdmin,
}

deployFilePath := filepath.Join(deploymentsDir, ".deploy")
_, err := os.Stat(deployFilePath)
deployFileExists := true
if err != nil {
deployFileExists = false
}

for _, name := range contractsFromJSON {
var path string
if deployFileExists {
path = deployFilePath
} else {
path = filepath.Join(deploymentsDir, name+".json")
}
file, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read file: %v", err)
}
var data AddressData
if err = json.Unmarshal(file, &data); err != nil {
return fmt.Errorf("failed to unmarshal json: %v", err)
}
contractAddresses[name] = data.Address
}

fmt.Printf("Contract addresses read from deployments directory: %s\n", deploymentsDir)

return nil
}

func writeAddressesToJSON(contractsAddresses map[string]string, superchainRepoPath, target, chainName string) error {
dirPath := filepath.Join(superchainRepoPath, "superchain", "extra", "addresses", target)
if err := os.MkdirAll(dirPath, 0755); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}

filePath := filepath.Join(dirPath, chainName+".json")
file, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer file.Close()

// Marshal the map to JSON
jsonData, err := json.MarshalIndent(contractsAddresses, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal json: %w", err)
}

// Write the JSON data to the file
if _, err := file.Write(jsonData); err != nil {
return fmt.Errorf("failed to write json to file: %w", err)
}
fmt.Printf("Contract addresses written to: %s\n", filePath)

return nil
}
98 changes: 98 additions & 0 deletions add-chain/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"encoding/json"
"os"
"reflect"
"testing"

"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
)

func TestCLIApp(t *testing.T) {
app := &cli.App{
Name: "add-chain",
Usage: "Add a new chain to the superchain-registry",
Flags: []cli.Flag{ChainTypeFlag, TestFlag},
Action: entrypoint,
}

args := []string{"add-chain", "-chain-type", "standard", "-test", "true"}
err := app.Run(args)
if err != nil {
t.Errorf("add-chain app failed: %v", err)
}

yamlEqual, err := checkConfigYaml()
if err != nil {
t.Errorf("failed to read yaml config files: %v", err)
}
if !yamlEqual {
t.Error("test config yaml file does not match expected file")
}

jsonEqual, err := compareJsonFiles("./testdata/superchain/extra/addresses/sepolia/")
if err != nil {
t.Errorf("failed to read json address files: %v", err)
}
if !jsonEqual {
t.Error("test json address file does not match expected file")
}

jsonEqual, err = compareJsonFiles("./testdata/superchain/extra/genesis-system-configs/sepolia/")
if err != nil {
t.Errorf("failed to read json genesis files: %v", err)
}
if !jsonEqual {
t.Error("test json genesis file does not match expected file")
}
}

func compareJsonFiles(dirPath string) (bool, error) {
expectedBytes, err := os.ReadFile(dirPath + "expected.json")
if err != nil {
return false, err
}

var expectJSON map[string]interface{}
if err := json.Unmarshal(expectedBytes, &expectJSON); err != nil {
return false, err
}

testBytes, err := os.ReadFile(dirPath + "awesomechain.json")
if err != nil {
return false, err
}

var testJSON map[string]interface{}
if err := json.Unmarshal(testBytes, &testJSON); err != nil {
return false, err
}

return reflect.DeepEqual(expectJSON, testJSON), nil
}

func checkConfigYaml() (bool, error) {
expectedBytes, err := os.ReadFile("./testdata/superchain/configs/sepolia/expected.yaml")
if err != nil {
return false, err
}

var expectYaml RollupConfig
if err := yaml.Unmarshal(expectedBytes, &expectYaml); err != nil {
return false, err
}

testBytes, err := os.ReadFile("./testdata/superchain/configs/sepolia/awesomechain.yaml")
if err != nil {
return false, err
}

var testYaml RollupConfig
if err := yaml.Unmarshal(testBytes, &testYaml); err != nil {
return false, err
}

return reflect.DeepEqual(expectYaml, testYaml), nil
Comment thread
bitwiseguy marked this conversation as resolved.
Outdated
}
Loading