Skip to content
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

NET-82 update enhancements to diff report.html #1036

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
43 changes: 38 additions & 5 deletions scripts/bytecode-diff/cmd/run_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ var AddHashesCmd = &cobra.Command{
log.Fatal().Err(err).Msg("Failed to unmarshal YAML data")
}

// Get first sourceEnvironment key from data
var sourceEnvironment string
if diamonds, ok := data["diamonds"].([]interface{}); ok && len(diamonds) > 0 {
if diamond, ok := diamonds[0].(map[string]interface{}); ok {
if env, ok := diamond["sourceEnvironment"].(string); ok {
sourceEnvironment = env
}
}
}
if sourceEnvironment == "" {
log.Fatal().Msg("Invalid YAML structure: 'sourceEnvironment' not found in diamond")
}

// Process deployments
deployments, ok := data["deployments"].(map[string]interface{})
if !ok {
Expand Down Expand Up @@ -125,7 +138,7 @@ var AddHashesCmd = &cobra.Command{

// After writing the updated YAML file
if htmlRender {
htmlContent, err := renderYAMLToHTML(updatedYAML, environment)
htmlContent, err := renderYAMLToHTML(updatedYAML, sourceEnvironment, environment)
if err != nil {
log.Error().Err(err).Msg("Failed to render YAML to HTML")
} else {
Expand All @@ -143,19 +156,39 @@ var AddHashesCmd = &cobra.Command{
},
}

func renderYAMLToHTML(yamlData []byte, environment string) (string, error) {
func countMatches(hashes interface{}, sourceHash string) int {
count := 0
if hashSlice, ok := hashes.([]interface{}); ok {
for _, hash := range hashSlice {
if hashStr, ok := hash.(string); ok {
if hashStr == sourceHash {
count++
}
}
}
}
return count
}

func renderYAMLToHTML(yamlData []byte, sourceEnvironment string, targetEnvironment string) (string, error) {
var data map[string]interface{}
err := yaml.Unmarshal(yamlData, &data)
if err != nil {
return "", fmt.Errorf("failed to unmarshal YAML data: %w", err)
}

data["environment"] = environment
data["sourceEnvironment"] = sourceEnvironment
data["targetEnvironment"] = targetEnvironment
data["reportTime"] = time.Now().UTC().Format(time.RFC3339)

t, err := template.ParseFiles("templates/report.html")
funcMap := template.FuncMap{
"countMatches": countMatches,
}

// Load the main template and any associated templates
t, err := template.New("report.html").Funcs(funcMap).ParseFiles("templates/report.html", "templates/header.html")
if err != nil {
return "", fmt.Errorf("failed to parse HTML template: %w", err)
return "", fmt.Errorf("failed to parse HTML templates: %w", err)
}

var buf strings.Builder
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
8 changes: 8 additions & 0 deletions scripts/bytecode-diff/templates/header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{define "header"}}
<div class="report-header">
<h1>{{.targetEnvironment}} Facets Bytecode Diff Report</h1>
<p><strong>Source Environment:</strong> {{.sourceEnvironment}}</p>
<p><strong>Target Environment:</strong> {{.targetEnvironment}}</p>
<p><strong>Generated Time:</strong> {{.reportTime}}</p>
</div>
{{end}}
Loading
Loading