Skip to content

Commit

Permalink
refactor origin to source everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
jterzis committed Sep 10, 2024
1 parent 877f591 commit 772398b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 90 deletions.
28 changes: 14 additions & 14 deletions scripts/bytecode-diff/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down Expand Up @@ -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
Expand Down
56 changes: 28 additions & 28 deletions scripts/bytecode-diff/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
sourceDiffDir string
sourceDiff bool
reportOutDir string
originEnvironment string
sourceEnvironment string
targetEnvironment string
deploymentsPath string
baseSepoliaRpcUrl string
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -191,15 +191,15 @@ 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,
BaseSepoliaRpcUrl: baseSepoliaRpcUrl,
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")
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -272,7 +272,7 @@ func executeEnvrionmentDiff(
clients, err := utils.CreateEthereumClients(
baseConfig.BaseRpcUrl,
baseConfig.BaseSepoliaRpcUrl,
originEnvironment,
sourceEnvironment,
targetEnvironment,
verbose,
)
Expand All @@ -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)
Expand All @@ -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()).
Expand All @@ -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")
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions scripts/bytecode-diff/scripts/upgrade-facets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 32 additions & 32 deletions scripts/bytecode-diff/utils/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -23,36 +23,36 @@ 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
}

// Merge target facets with the same ContractName
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
}
Expand All @@ -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,
})
}
Expand All @@ -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,
})
}
}
Expand All @@ -109,26 +109,26 @@ 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: "",
}
}
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)
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/bytecode-diff/utils/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 772398b

Please sign in to comment.