From 772398b07402ecb37a8f20dddd53e5e0270c8ae3 Mon Sep 17 00:00:00 2001 From: John Terzis Date: Tue, 10 Sep 2024 14:37:01 -0700 Subject: [PATCH] refactor origin to source everywhere --- scripts/bytecode-diff/README.md | 28 ++++---- scripts/bytecode-diff/cmd/root.go | 56 ++++++++-------- .../bytecode-diff/scripts/upgrade-facets.sh | 4 +- scripts/bytecode-diff/utils/diff.go | 64 +++++++++---------- scripts/bytecode-diff/utils/ethereum.go | 4 +- scripts/bytecode-diff/utils/utils.go | 27 ++++---- 6 files changed, 93 insertions(+), 90 deletions(-) diff --git a/scripts/bytecode-diff/README.md b/scripts/bytecode-diff/README.md index 0f18dd67c..4b8c15906 100644 --- a/scripts/bytecode-diff/README.md +++ b/scripts/bytecode-diff/README.md @@ -49,36 +49,36 @@ GOWORK=off go run ./main.go gamma omega -v ➜ bytecode-diff git:(jt/net-62-contract-differ) ✗ yq eval deployed-diffs/facet_diff_090324_18.yaml diamonds: - name: spaceOwner - origin: gamma + source: gamma target: omega facets: - - originContractName: "" - originFacetAddress: 0xfa98a1648761e494fc7d6efe5a06e357a76bd6fb + - sourceContractName: "" + sourceFacetAddress: 0xfa98a1648761e494fc7d6efe5a06e357a76bd6fb selectorsDiff: - "0x3953801b" - "0x91de4a83" - originBytecodeHash: 0xf86d9dbe53c89e14fa69cde925cca02b6efad519fe172f7b04d9515d7700a59b - originVerified: false + sourceBytecodeHash: 0xf86d9dbe53c89e14fa69cde925cca02b6efad519fe172f7b04d9515d7700a59b + sourceVerified: false targetVerified: false - - originContractName: SpaceOwner - originFacetAddress: 0x30c912d8ceb9793e4cd240862acfd0e6c4436c52 + - sourceContractName: SpaceOwner + sourceFacetAddress: 0x30c912d8ceb9793e4cd240862acfd0e6c4436c52 targetContractAddresses: - 0x63bC35259Ac32DF43Fba3b890F0F74951451976A - 0xe7EB1313f0E7076616534225e16E971B72b50C42 selectorsDiff: [] - originBytecodeHash: 0x461b53ab37fd24283ecd63eb0d4e71bd554a266036c73caf6d2ac39c435e7732 + sourceBytecodeHash: 0x461b53ab37fd24283ecd63eb0d4e71bd554a266036c73caf6d2ac39c435e7732 targetBytecodeHashes: - 0x86d20161a13671a6138b80551e94dd8c1638bc5151807ff2194aa1e50cdb3cac - 0xff0a94e93a4f4f6ee0ecd0d0e469e55ca40f1ab6c10e6af9da5b2b597f32b178 - originVerified: true + sourceVerified: true targetVerified: true - - originContractName: "" - originFacetAddress: 0xdba2ce6125cc6b7f93c63d181a0780d5b421940b + - sourceContractName: "" + sourceFacetAddress: 0xdba2ce6125cc6b7f93c63d181a0780d5b421940b selectorsDiff: - "0x0d653654" - "0x466a18de" - originBytecodeHash: 0x583c2852056f90c96ed1cab935489f644b8ef564e0a7f11564925d07cf3bc593 - originVerified: false + sourceBytecodeHash: 0x583c2852056f90c96ed1cab935489f644b8ef564e0a7f11564925d07cf3bc593 + sourceVerified: false targetVerified: false ``` @@ -112,7 +112,7 @@ Banning: A tool to retrieve and display contract bytecode diff for Base Usage: - bytecode-diff [origin_environment] [target_environment] [flags] + bytecode-diff [source_environment] [target_environment] [flags] Flags: -b, --base-rpc string Base RPC provider URL diff --git a/scripts/bytecode-diff/cmd/root.go b/scripts/bytecode-diff/cmd/root.go index 03d92d94a..47d041a0e 100644 --- a/scripts/bytecode-diff/cmd/root.go +++ b/scripts/bytecode-diff/cmd/root.go @@ -20,7 +20,7 @@ var ( sourceDiffDir string sourceDiff bool reportOutDir string - originEnvironment string + sourceEnvironment string targetEnvironment string deploymentsPath string baseSepoliaRpcUrl string @@ -74,7 +74,7 @@ func Execute() { } var rootCmd = &cobra.Command{ - Use: "bytecode-diff [origin_environment] [target_environment]", + Use: "bytecode-diff [source_environment] [target_environment]", Short: "A tool to retrieve and display contract bytecode diff for Base", PersistentPreRun: func(cmd *cobra.Command, args []string) { setLogLevel(logLevel) @@ -86,7 +86,7 @@ var rootCmd = &cobra.Command{ } } else { if len(args) < 2 { - return fmt.Errorf("at least two arguments required when --source-diff-only is not set, [origin_environment], [target_environment]") + return fmt.Errorf("at least two arguments required when --source-diff-only is not set, [source_environment], [target_environment]") } } return nil @@ -163,14 +163,14 @@ var rootCmd = &cobra.Command{ } } else { - originEnvironment, targetEnvironment = args[0], args[1] - for _, environment := range []string{originEnvironment, targetEnvironment} { + sourceEnvironment, targetEnvironment = args[0], args[1] + for _, environment := range []string{sourceEnvironment, targetEnvironment} { if !utils.Contains(supportedEnvironments, environment) { log.Fatal().Str("environment", environment).Msg("Environment not supported. Environment can be one of alpha, gamma, or omega.") } } - log.Info().Str("originEnvironment", originEnvironment).Str("targetEnvironment", targetEnvironment).Msg("Environment") + log.Info().Str("sourceEnvironment", sourceEnvironment).Str("targetEnvironment", targetEnvironment).Msg("Environment") if baseRpcUrl == "" { baseRpcUrl = os.Getenv("BASE_RPC_URL") @@ -191,7 +191,7 @@ var rootCmd = &cobra.Command{ log.Fatal().Msg("BaseScan API key not provided. Set it using BASESCAN_API_KEY environment variable") } - log.Info().Str("originEnvironment", originEnvironment).Str("targetEnvironment", targetEnvironment).Msg("Running diff for environment") + log.Info().Str("sourceEnvironment", sourceEnvironment).Str("targetEnvironment", targetEnvironment).Msg("Running diff for environment") // Create BaseConfig struct baseConfig := utils.BaseConfig{ BaseRpcUrl: baseRpcUrl, @@ -199,7 +199,7 @@ var rootCmd = &cobra.Command{ BasescanAPIKey: basescanAPIKey, } - if err := executeEnvrionmentDiff(verbose, baseConfig, deploymentsPath, originEnvironment, targetEnvironment, reportOutDir); err != nil { + if err := executeEnvrionmentDiff(verbose, baseConfig, deploymentsPath, sourceEnvironment, targetEnvironment, reportOutDir); err != nil { log.Fatal().Err(err).Msg("Error executing environment diff") } } @@ -246,7 +246,7 @@ func executeSourceDiff(verbose bool, facetSourcePath, compiledFacetsPath string, func executeEnvrionmentDiff( verbose bool, baseConfig utils.BaseConfig, - deploymentsPath, originEnvironment, targetEnvironment string, + deploymentsPath, sourceEnvironment, targetEnvironment string, reportOutDir string, ) error { // walk environment diamonds and get all facet addresses from DiamondLoupe facet view @@ -256,10 +256,10 @@ func executeEnvrionmentDiff( utils.SpaceFactory, utils.SpaceOwner, } - originDeploymentsPath := filepath.Join(deploymentsPath, originEnvironment) - originDiamonds, err := utils.GetDiamondAddresses(originDeploymentsPath, baseDiamonds, verbose) + sourceDeploymentsPath := filepath.Join(deploymentsPath, sourceEnvironment) + sourceDiamonds, err := utils.GetDiamondAddresses(sourceDeploymentsPath, baseDiamonds, verbose) if err != nil { - log.Error().Err(err).Msgf("Error getting diamond addresses for origin environment %s", originEnvironment) + log.Error().Err(err).Msgf("Error getting diamond addresses for source environment %s", sourceEnvironment) return err } targetDeploymentsPath := filepath.Join(deploymentsPath, targetEnvironment) @@ -272,7 +272,7 @@ func executeEnvrionmentDiff( clients, err := utils.CreateEthereumClients( baseConfig.BaseRpcUrl, baseConfig.BaseSepoliaRpcUrl, - originEnvironment, + sourceEnvironment, targetEnvironment, verbose, ) @@ -282,26 +282,26 @@ func executeEnvrionmentDiff( } }() // getCode for all facet addresses over base rpc url and compare with compiled hashes - originFacets := make(map[string][]utils.Facet) + sourceFacets := make(map[string][]utils.Facet) - for diamondName, diamondAddress := range originDiamonds { + for diamondName, diamondAddress := range sourceDiamonds { if verbose { log.Info(). Str("diamondName", fmt.Sprintf("%s", diamondName)). Str("diamondAddress", diamondAddress). - Msg("Origin Diamond Address") + Msg("source Diamond Address") } - facets, err := utils.ReadAllFacets(clients[originEnvironment], diamondAddress, baseConfig.BasescanAPIKey) + facets, err := utils.ReadAllFacets(clients[sourceEnvironment], diamondAddress, baseConfig.BasescanAPIKey) if err != nil { - log.Error().Err(err).Msgf("Error reading all facets for origin diamond %s", diamondName) + log.Error().Err(err).Msgf("Error reading all facets for source diamond %s", diamondName) return err } - err = utils.AddContractCodeHashes(clients[originEnvironment], facets) + err = utils.AddContractCodeHashes(clients[sourceEnvironment], facets) if err != nil { - log.Error().Err(err).Msgf("Error adding contract code hashes for origin diamond %s", diamondName) + log.Error().Err(err).Msgf("Error adding contract code hashes for source diamond %s", diamondName) return err } - originFacets[string(diamondName)] = facets + sourceFacets[string(diamondName)] = facets } targetFacets := make(map[string][]utils.Facet) @@ -319,8 +319,8 @@ func executeEnvrionmentDiff( targetFacets[string(diamondName)] = facets } if verbose { - for diamondName, facets := range originFacets { - log.Info().Str("diamondName", diamondName).Msg("Origin Facets for Diamond contract") + for diamondName, facets := range sourceFacets { + log.Info().Str("diamondName", diamondName).Msg("source Facets for Diamond contract") for _, facet := range facets { log.Info(). Str("facetAddress", facet.FacetAddress.Hex()). @@ -342,15 +342,15 @@ func executeEnvrionmentDiff( } // compare facets and create report - differences := utils.CompareFacets(originFacets, targetFacets) + differences := utils.CompareFacets(sourceFacets, targetFacets) if verbose { for diamondName, facets := range differences { log.Info().Str("diamondName", diamondName).Msg("Differences for Diamond contract") for _, facet := range facets { log.Info(). - Str("facetAddress", facet.OriginContractAddress.Hex()). - Str("originContractName", facet.OriginContractName). - Msg("Origin Facet") + Str("facetAddress", facet.SourceContractAddress.Hex()). + Str("sourceContractName", facet.SourceContractName). + Msg("Source Facet") log.Info(). Interface("selectorDiff", facet.SelectorsDiff). Msg("Selector Diff") @@ -361,7 +361,7 @@ func executeEnvrionmentDiff( // create report log.Info().Str("reportOutDir", reportOutDir).Msg("Generating YAML report") - err = utils.GenerateYAMLReport(originEnvironment, targetEnvironment, differences, reportOutDir) + err = utils.GenerateYAMLReport(sourceEnvironment, targetEnvironment, differences, reportOutDir) if err != nil { log.Error().Err(err).Msg("Error generating YAML report") return err diff --git a/scripts/bytecode-diff/scripts/upgrade-facets.sh b/scripts/bytecode-diff/scripts/upgrade-facets.sh index f523a5902..0e45d0d8d 100755 --- a/scripts/bytecode-diff/scripts/upgrade-facets.sh +++ b/scripts/bytecode-diff/scripts/upgrade-facets.sh @@ -50,8 +50,8 @@ process_file() { local file="$1" echo "Processing file: $file" - # Extract originContractNames into an array, strip "Facet" suffix, and remove duplicates - contract_names=($(yq e '.diamonds[].facets[].originContractName' "$file" | sed 's/Facet$//' | sort -u)) + # Extract sourceContractNames into an array, strip "Facet" suffix, and remove duplicates + contract_names=($(yq e '.diamonds[].facets[].sourceContractName' "$file" | sed 's/Facet$//' | sort -u)) # Determine which make command to use if [[ "$network" == "omega" ]]; then diff --git a/scripts/bytecode-diff/utils/diff.go b/scripts/bytecode-diff/utils/diff.go index abba5faf2..de624efbb 100644 --- a/scripts/bytecode-diff/utils/diff.go +++ b/scripts/bytecode-diff/utils/diff.go @@ -4,16 +4,16 @@ import ( "github.com/ethereum/go-ethereum/common" ) -// FacetDiff represents the differences between origin and target facets +// FacetDiff represents the differences between source and target facets type FacetDiff struct { - OriginContractName string `yaml:"originContractName"` + SourceContractName string `yaml:"sourceContractName"` TargetContractName string `yaml:"targetContractName,omitempty"` - OriginContractAddress common.Address `yaml:"originFacetAddress"` + SourceContractAddress common.Address `yaml:"sourceFacetAddress"` TargetContractAddresses []string `yaml:"targetContractAddresses,omitempty"` SelectorsDiff []string `yaml:"selectorsDiff"` - OriginBytecodeHash string `yaml:"originBytecodeHash,omitempty"` + SourceBytecodeHash string `yaml:"sourceBytecodeHash,omitempty"` TargetBytecodeHashes []string `yaml:"targetBytecodeHashes,omitempty"` - OriginVerified bool `yaml:"originVerified"` + SourceVerified bool `yaml:"sourceVerified"` TargetVerified bool `yaml:"targetVerified"` } @@ -23,15 +23,15 @@ type MergedFacet struct { ContractAddresses []string } -// CompareFacets compares origin and target Facet arrays and returns the differences -func CompareFacets(origin, target map[string][]Facet) map[string][]FacetDiff { +// CompareFacets compares source and target Facet arrays and returns the differences +func CompareFacets(source, target map[string][]Facet) map[string][]FacetDiff { differences := make(map[string][]FacetDiff) - for diamondName, originFacets := range origin { + for diamondName, sourceFacets := range source { targetFacets, exists := target[diamondName] if !exists { - // If the diamond doesn't exist in target, add all origin facets - differences[diamondName] = convertToFacetDiff(originFacets) + // If the diamond doesn't exist in target, add all source facets + differences[diamondName] = convertToFacetDiff(sourceFacets) continue } @@ -39,20 +39,20 @@ func CompareFacets(origin, target map[string][]Facet) map[string][]FacetDiff { mergedTargetFacets := mergeFacets(targetFacets) var diamondDifferences []FacetDiff - // compare each origin facet set for each diamond with target facets - for _, o := range originFacets { - // if origin facet is not verified, add it to differences + // compare each source facet set for each diamond with target facets + for _, o := range sourceFacets { + // if source facet is not verified, add it to differences if o.ContractName == "" { - Log.Info().Msgf("Origin facet is not verified: %+v", o) + Log.Info().Msgf("source facet is not verified: %+v", o) diamondDifferences = append(diamondDifferences, FacetDiff{ - OriginContractAddress: o.FacetAddress, + SourceContractAddress: o.FacetAddress, SelectorsDiff: o.SelectorsHex, - OriginContractName: o.ContractName, + SourceContractName: o.ContractName, TargetContractName: "", - OriginBytecodeHash: o.BytecodeHash, + SourceBytecodeHash: o.BytecodeHash, TargetBytecodeHashes: []string{}, TargetContractAddresses: []string{}, - OriginVerified: false, + SourceVerified: false, }) continue } @@ -69,13 +69,13 @@ func CompareFacets(origin, target map[string][]Facet) map[string][]FacetDiff { } if len(diffSelectors) > 0 || bytecodeChanged { diamondDifferences = append(diamondDifferences, FacetDiff{ - OriginContractAddress: o.FacetAddress, + SourceContractAddress: o.FacetAddress, SelectorsDiff: diffSelectors, - OriginContractName: o.ContractName, - OriginBytecodeHash: o.BytecodeHash, + SourceContractName: o.ContractName, + SourceBytecodeHash: o.BytecodeHash, TargetBytecodeHashes: t.BytecodeHashes, TargetContractAddresses: t.ContractAddresses, - OriginVerified: true, + SourceVerified: true, TargetVerified: true, }) } @@ -84,14 +84,14 @@ func CompareFacets(origin, target map[string][]Facet) map[string][]FacetDiff { if !found { // Contract by name doesn't exist in target set, add all selectors diamondDifferences = append(diamondDifferences, FacetDiff{ - OriginContractAddress: o.FacetAddress, + SourceContractAddress: o.FacetAddress, SelectorsDiff: o.SelectorsHex, - OriginContractName: o.ContractName, + SourceContractName: o.ContractName, TargetContractName: "", - OriginBytecodeHash: o.BytecodeHash, + SourceBytecodeHash: o.BytecodeHash, TargetBytecodeHashes: []string{}, TargetContractAddresses: []string{}, - OriginVerified: true, + SourceVerified: true, }) } } @@ -109,10 +109,10 @@ func convertToFacetDiff(facets []Facet) []FacetDiff { diffs := make([]FacetDiff, len(facets)) for i, f := range facets { diffs[i] = FacetDiff{ - OriginContractAddress: f.FacetAddress, + SourceContractAddress: f.FacetAddress, SelectorsDiff: f.SelectorsHex, - OriginContractName: f.ContractName, - OriginBytecodeHash: f.BytecodeHash, + SourceContractName: f.ContractName, + SourceBytecodeHash: f.BytecodeHash, TargetContractAddresses: []string{}, TargetContractName: "", } @@ -120,15 +120,15 @@ func convertToFacetDiff(facets []Facet) []FacetDiff { return diffs } -// getDifferentSelectors returns selectors from origin that are not in target -func getDifferentSelectors(origin, target []string) []string { +// getDifferentSelectors returns selectors from source that are not in target +func getDifferentSelectors(source, target []string) []string { targetSet := make(map[string]struct{}) for _, t := range target { targetSet[t] = struct{}{} } var different []string - for _, o := range origin { + for _, o := range source { if _, exists := targetSet[o]; !exists { different = append(different, o) } diff --git a/scripts/bytecode-diff/utils/ethereum.go b/scripts/bytecode-diff/utils/ethereum.go index 225cd5e23..15554a1ed 100644 --- a/scripts/bytecode-diff/utils/ethereum.go +++ b/scripts/bytecode-diff/utils/ethereum.go @@ -137,13 +137,13 @@ func ReadAllFacets(client *ethclient.Client, contractAddress string, basescanAPI func CreateEthereumClients( baseRpcUrl string, baseSepoliaRpcUrl string, - originEnvironment string, + sourceEnvironment string, targetEnvironment string, verbose bool, ) (map[string]*ethclient.Client, error) { clients := make(map[string]*ethclient.Client) - for _, env := range []string{originEnvironment, targetEnvironment} { + for _, env := range []string{sourceEnvironment, targetEnvironment} { var rpcUrl string if env == "alpha" || env == "gamma" { rpcUrl = baseSepoliaRpcUrl diff --git a/scripts/bytecode-diff/utils/utils.go b/scripts/bytecode-diff/utils/utils.go index 4aceb7c1f..a543d9ed9 100644 --- a/scripts/bytecode-diff/utils/utils.go +++ b/scripts/bytecode-diff/utils/utils.go @@ -1,6 +1,8 @@ package utils import ( + "encoding/hex" + "encoding/json" "fmt" "os" "os/exec" @@ -10,9 +12,6 @@ import ( "strings" "time" - "encoding/hex" - "encoding/json" - "github.com/ethereum/go-ethereum/crypto" "gopkg.in/yaml.v2" ) @@ -23,10 +22,10 @@ const ( ) type DiamondReport struct { - Name string `yaml:"name"` - Origin string `yaml:"origin"` - Target string `yaml:"target"` - Facets []FacetDiff `yaml:"facets"` + Name string `yaml:"name"` + SourceEnvironment string `yaml:"sourceEnvironment"` + TargetEnvironment string `yaml:"targetEnvironment"` + Facets []FacetDiff `yaml:"facets"` } type Data struct { @@ -323,7 +322,11 @@ func GetDiamondAddresses(basePath string, diamonds []Diamond, verbose bool) (map return diamondAddresses, nil } -func GenerateYAMLReport(originEnvironment, targetEnvironment string, facetDiffs map[string][]FacetDiff, reportOutDir string) error { +func GenerateYAMLReport( + sourceEnvironment, targetEnvironment string, + facetDiffs map[string][]FacetDiff, + reportOutDir string, +) error { type Report struct { Diamonds []DiamondReport `yaml:"diamonds"` } @@ -332,10 +335,10 @@ func GenerateYAMLReport(originEnvironment, targetEnvironment string, facetDiffs for diamondName, diffs := range facetDiffs { diamondReport := DiamondReport{ - Name: diamondName, - Origin: originEnvironment, - Target: targetEnvironment, - Facets: diffs, + Name: diamondName, + SourceEnvironment: sourceEnvironment, + TargetEnvironment: targetEnvironment, + Facets: diffs, } report.Diamonds = append(report.Diamonds, diamondReport) }