-
Notifications
You must be signed in to change notification settings - Fork 211
Convert add-chain.sh into a go package #204
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
Merged
Merged
Changes from 1 commit
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 0b4d631
Create go module
bitwiseguy 4de3faa
Fixes to produce correct output files from addchain module
bitwiseguy 22b4748
Rename go module addchain to add-chain
bitwiseguy 1b1acea
Remove duplicate invocation of registry-data go program
bitwiseguy 9afdef3
Add conditional when searching for contract addrs from file
bitwiseguy 4969abd
Add e2e test for add-chain go module
bitwiseguy 9c04805
Finish removing add-chain go test dep on monorepo
bitwiseguy 175f5ae
Rename add-chain test files to expected.[json|yam]l
bitwiseguy af89ef1
Merge branch 'main' into ss/addchain-go
bitwiseguy 7205dbe
Retrieve L1 url from superchain package instead of hardcoding
bitwiseguy 1b2967a
Use consistent go 1.21 version in all go.mod files
bitwiseguy 61ea05f
Read contract addresses from .deploy file within add-chain
bitwiseguy cb6864a
Move add-chain/.env.test into testdata dir
bitwiseguy 9b4f82e
Merge branch 'main' into ss/addchain-go
bitwiseguy 2fd6532
Refactor 'cast call' commands to reduce duplicate code
bitwiseguy ccd2fdd
Fix call to OptimismPortalProxy.guardian() - instead of calling GUARD…
bitwiseguy c331023
Removed unused genesis.json file from add-chain/testdata dir
bitwiseguy 7013cd3
Use custom yaml encoder to add whitespace
bitwiseguy bcdfb7c
Compare config raw bytes instead of structs in e2e test
bitwiseguy c5c6afe
Add human readable timestamp as comments in yaml file
bitwiseguy dda8220
Remove hardfork timestamp overrides if they match superchain defaults
bitwiseguy 9446436
Move enhanceYAML to a method of RollupConfig
bitwiseguy 354b976
Add godoc comments to a few functions
bitwiseguy 80fad15
improve cast call error handling (#215)
geoknee 7f55ffa
Merge branch 'main' into ss/addchain-go
bitwiseguy 50b1c85
Address PR comments
bitwiseguy bb991a1
Run gofumpt and golangci-lint
bitwiseguy ef4fb1d
Install foundry in Circle CI for cast call in tests
bitwiseguy 1a26e4c
Fix timestamp to use consistent UTC+0 timezone
bitwiseguy 6b8483f
Set Circle CI shell type to bash
bitwiseguy 019fe31
Move shell type to job level in Circle CI
bitwiseguy 5f09d55
Merge branch 'main' into ss/addchain-go
bitwiseguy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| type RollupConfig struct { | ||
| Name string `yaml:"name"` | ||
| L2ChainID string `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" yaml:"canyon_time"` | ||
| DeltaTime int `json:"delta_time" yaml:"delta_time"` | ||
| EcotoneTime int `json:"ecotone_time" 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"` | ||
| } | ||
|
|
||
| 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 string, superchainLevel int) (RollupConfig, error) { | ||
| 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 | ||
|
|
||
| return config, nil | ||
| } | ||
|
|
||
| func writeChainConfig(inputFilepath, targetDirectory, chainName, publicRPC, sequencerRPC string, superchainLevel int, superchainRepoPath string, superchainTarget string) error { | ||
| rollupConfig, err := constructRollupConfig(inputFilepath, chainName, publicRPC, sequencerRPC, superchainLevel) | ||
| if err != nil { | ||
| return fmt.Errorf("Failed to construct rollup config: %w", err) | ||
| } | ||
|
|
||
| yamlData, err := yaml.Marshal(rollupConfig) | ||
| if err != nil { | ||
| return fmt.Errorf("Failed to marshal yaml: %w", err) | ||
| } | ||
|
|
||
| filename := filepath.Join(targetDirectory, chainName+".yaml") | ||
| err = os.WriteFile(filename, yamlData, 0644) | ||
| if err != nil { | ||
| return fmt.Errorf("Failed to write yaml file: %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) | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| ) | ||
|
|
||
| type AddressData struct { | ||
| Address string `json:"address"` | ||
| } | ||
|
|
||
| var ( | ||
| // Addresses to retrieve from JSON | ||
| 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}) | ||
| 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}) | ||
| if err != nil || address == "" || address == "0x" { | ||
| address, err = executeCommand("cast", []string{"call", contractAddresses[OptimismPortalProxy], "GUARDIAN()(address)", "-r", l1RpcUrl}) | ||
| 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}) | ||
| 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}) | ||
| 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}) | ||
| if err != nil || address == "" || address == "0x" { | ||
| return fmt.Errorf("Could not retrieve address for ProxyAdminOwner") | ||
| } else { | ||
| contractAddresses[SystemConfigOwner] = address | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func readAddressesFromJSON(contractAddresses map[string]string, deploymentsDir string) error { | ||
| var contractsFromJSON = []string{ | ||
| AddressManager, | ||
| L1CrossDomainMessengerProxy, | ||
| L1ERC721BridgeProxy, | ||
| L1StandardBridgeProxy, | ||
| L2OutputOracleProxy, | ||
| OptimismMintableERC20FactoryProxy, | ||
| SystemConfigProxy, | ||
| OptimismPortalProxy, | ||
| ProxyAdmin, | ||
| } | ||
|
|
||
| for _, name := range contractsFromJSON { | ||
| 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 | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| ) | ||
|
|
||
| func createExtraGenesisData(superchainRepoPath, superchainTarget, monorepoDir, genesisConfig, chainName string) error { | ||
| // Create directory for genesis data | ||
| genesisDir := filepath.Join(superchainRepoPath, "superchain", "extra", "genesis", superchainTarget) | ||
| if err := os.MkdirAll(genesisDir, 0755); err != nil { | ||
| return fmt.Errorf("failed to create genesis directory: %w", err) | ||
| } | ||
|
|
||
| // Set current working directory to the monorepo directory | ||
| if err := os.Chdir(monorepoDir); err != nil { | ||
| return fmt.Errorf("failed to change directory to monorepo: %w", err) | ||
| } | ||
|
|
||
| cmd := exec.Command("go", "run", "./op-chain-ops/cmd/registry-data", | ||
| "--l2-genesis", genesisConfig, | ||
| "--bytecodes-dir", filepath.Join(superchainRepoPath, "superchain", "extra", "bytecodes"), | ||
| "--output", filepath.Join(genesisDir, chainName+".json.gz")) | ||
|
|
||
|
geoknee marked this conversation as resolved.
Outdated
|
||
| if err := cmd.Run(); err != nil { | ||
| return fmt.Errorf("failed to run registry-data command: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "flag" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| func main() { | ||
| chainType := flag.String("chain_type", "", "Type of chain to add. Must be 'standard' or 'frontier'.") | ||
| help := flag.Bool("help", false, "Show help information") | ||
| flag.Parse() | ||
|
|
||
| if *help { | ||
| showUsage() | ||
| return | ||
| } | ||
|
|
||
| superchainLevel, err := getSuperchainLevel(*chainType) | ||
| if err != nil { | ||
| fmt.Println("Failed to get superchain level:", err) | ||
| showUsage() | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // Get the current script's directory | ||
| scriptDir, err := os.Getwd() | ||
| if err != nil { | ||
| fmt.Println("Error getting current directory:", err) | ||
| os.Exit(1) | ||
| } | ||
| superchainRepoPath := filepath.Dir(scriptDir) // Parent directory | ||
|
|
||
| // Load environment variables | ||
| viper.SetConfigName(".env") // name of config file (without extension) | ||
|
bitwiseguy marked this conversation as resolved.
Outdated
|
||
| viper.SetConfigType("env") // REQUIRED if the config file does not have the extension in the name | ||
| viper.AddConfigPath(superchainRepoPath) // path to look for the config file in | ||
| if err := viper.ReadInConfig(); err != nil { | ||
| fmt.Println("Error reading config file:", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| chainName := viper.GetString("CHAIN_NAME") | ||
| superchainTarget := viper.GetString("SUPERCHAIN_TARGET") | ||
| monorepoDir := viper.GetString("MONOREPO_DIR") | ||
| deploymentsDir := viper.GetString("DEPLOYMENTS_DIR") | ||
| rollupConfig := viper.GetString("ROLLUP_CONFIG") | ||
| genesisConfig := viper.GetString("GENESIS_CONFIG") | ||
| publicRPC := viper.GetString("PUBLIC_RPC") | ||
| sequencerRPC := viper.GetString("SEQUENCER_RPC") | ||
| explorer := viper.GetString("EXPLORER") | ||
|
|
||
| fmt.Printf("Chain Name: %s\n", chainName) | ||
| fmt.Printf("Superchain target: %s\n", superchainTarget) | ||
| fmt.Printf("Reading from monrepo directory: %s\n", monorepoDir) | ||
| fmt.Printf("With deployments directory: %s\n", deploymentsDir) | ||
| fmt.Printf("Rollup config: %s\n", rollupConfig) | ||
| fmt.Printf("Genesis config: %s\n", genesisConfig) | ||
| fmt.Printf("Public RPC endpoint: %s\n", publicRPC) | ||
| fmt.Printf("Sequencer RPC endpoint: %s\n", sequencerRPC) | ||
| fmt.Printf("Block Explorer: %s\n", explorer) | ||
|
|
||
| // Check if superchain target directory exists | ||
| targetDir := filepath.Join(superchainRepoPath, "superchain", "configs", superchainTarget) | ||
| if _, err := os.Stat(targetDir); os.IsNotExist(err) { | ||
| fmt.Println("Superchain target directory not found. Please follow instructions to add a superchain target in CONTRIBUTING.md") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| inputFilePath := filepath.Join(deploymentsDir, "RollupConfig.json") | ||
| targetFilePath := filepath.Join(targetDir, chainName+".yaml") | ||
| err = writeChainConfig(inputFilePath, targetFilePath, chainName, publicRPC, sequencerRPC, superchainLevel, superchainRepoPath, superchainTarget) | ||
| if err != nil { | ||
| fmt.Println("Error generating chain config .yaml file:", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| var contractAddresses map[string]string | ||
| err = readAddressesFromJSON(contractAddresses, deploymentsDir) | ||
| if err != nil { | ||
| fmt.Printf("Failed to read addresses from JSON files: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| l1RpcUrl, err := inferRpcUrl(superchainTarget) | ||
| if err != nil { | ||
| fmt.Printf("Failed to infer rpc url: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| err = readAddressesFromChain(contractAddresses, l1RpcUrl) | ||
| if err != nil { | ||
| fmt.Printf("Failed to read addresses from chain: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| err = writeAddressesToJSON(contractAddresses, superchainRepoPath, superchainTarget, chainName) | ||
| if err != nil { | ||
| fmt.Printf("Failed to write contract addresses to JSON file: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| err = createExtraGenesisData(superchainRepoPath, superchainTarget, monorepoDir, genesisConfig, chainName) | ||
| if err != nil { | ||
| fmt.Printf("Failed to create extra genesis data: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| func showUsage() { | ||
| fmt.Println("Usage: command <chain_type> [-h|--help]") | ||
| fmt.Println(" chain_type: The type of chain to add. Must be 'standard' or 'frontier'.") | ||
| fmt.Println(" -h, --help: Show this usage information.") | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.