From dd5b1c6345824f0da4cf25df9e4cc7b7de3e3891 Mon Sep 17 00:00:00 2001 From: Wyatt Barnes Date: Fri, 24 Nov 2023 22:52:49 -1000 Subject: [PATCH] Bindings and metadata overwrite warning --- op-bindings/bindgen/remote_handlers.go | 26 +++++++++++++++++++++++++ op-bindings/bindgen/utils.go | 27 ++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/op-bindings/bindgen/remote_handlers.go b/op-bindings/bindgen/remote_handlers.go index a182db04200ce..d0fdae6a0b14c 100644 --- a/op-bindings/bindgen/remote_handlers.go +++ b/op-bindings/bindgen/remote_handlers.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "context" "fmt" "os" @@ -298,6 +299,15 @@ func (generator *bindGenGeneratorRemote) writeAllOutputs(contractMetadata *remot func (generator *bindGenGeneratorRemote) writeContractMetadata(contractMetadata *remoteContractMetadata, fileTemplate *template.Template) error { metadataFilePath := filepath.Join(generator.metadataOut, strings.ToLower(contractMetadata.Name)+"_more.go") + + var existingOutput []byte + if _, err := os.Stat(metadataFilePath); err == nil { + existingOutput, err = os.ReadFile(metadataFilePath) + if err != nil { + generator.logger.Crit("Error reading existing metadata output file", "path", metadataFilePath, "err", err) + } + } + metadataFile, err := os.OpenFile( metadataFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, @@ -312,6 +322,22 @@ func (generator *bindGenGeneratorRemote) writeContractMetadata(contractMetadata return fmt.Errorf("error writing %s's contract metadata at %s: %w", contractMetadata.Name, metadataFilePath, err) } + if len(existingOutput) != 0 { + var newOutput []byte + newOutput, err = os.ReadFile(metadataFilePath) + if err != nil { + return fmt.Errorf("error reading new file: %w", err) + } + + if bytes.Equal(existingOutput, newOutput) { + generator.logger.Debug("No changes detected in the contract metadata", "contract", contractMetadata.Name) + } else { + generator.logger.Warn("Changes detected in the contract metadata, old metadata has been overwritten", "contract", contractMetadata.Name) + } + } else { + generator.logger.Debug("No existing contract metadata found, skipping comparison", "contract", contractMetadata.Name) + } + generator.logger.Debug("Successfully wrote contract metadata", "contract", contractMetadata.Name, "path", metadataFilePath) return nil } diff --git a/op-bindings/bindgen/utils.go b/op-bindings/bindgen/utils.go index 8465e34af3c4e..8a0ebfad5947e 100644 --- a/op-bindings/bindgen/utils.go +++ b/op-bindings/bindgen/utils.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "encoding/json" "fmt" "os" @@ -119,14 +120,36 @@ func genContractBindings(logger log.Logger, abiFilePath, bytecodeFilePath, goPac } outFilePath := path.Join(cwd, goPackageName, strings.ToLower(contractName)+".go") - logger.Debug("Generating contract bindings", "contractName", contractName, "outFilePath", outFilePath) + var existingOutput []byte + if _, err := os.Stat(outFilePath); err == nil { + existingOutput, err = os.ReadFile(outFilePath) + if err != nil { + logger.Crit("Error reading existing bindings output file", "outFilePath", outFilePath, "err", err) + } + } + + logger.Debug("Generating contract bindings", "contractName", contractName, "outFilePath", outFilePath) cmd := exec.Command("abigen", "--abi", abiFilePath, "--bin", bytecodeFilePath, "--pkg", goPackageName, "--type", contractName, "--out", outFilePath) cmd.Stdout = os.Stdout - if err := cmd.Run(); err != nil { return fmt.Errorf("error running abigen for %s: %w", contractName, err) } + if len(existingOutput) != 0 { + newOutput, err := os.ReadFile(outFilePath) + if err != nil { + return fmt.Errorf("error reading new file: %w", err) + } + + if bytes.Equal(existingOutput, newOutput) { + logger.Debug("No changes detected in the contract bindings", "contractName", contractName) + } else { + logger.Warn("Changes detected in the contract bindings, old bindings have been overwritten", "contractName", contractName) + } + } else { + logger.Debug("No existing contract bindings found, skipping comparison", "contractName", contractName) + } + return nil }