Skip to content

Commit

Permalink
Bindings and metadata overwrite warning
Browse files Browse the repository at this point in the history
  • Loading branch information
spacesailor24 committed Nov 27, 2023
1 parent c0b5253 commit aa63240
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
26 changes: 26 additions & 0 deletions op-bindings/bindgen/remote_handlers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"context"
"fmt"
"os"
Expand Down Expand Up @@ -263,6 +264,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,
Expand All @@ -277,6 +287,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
}
Expand Down
27 changes: 25 additions & 2 deletions op-bindings/bindgen/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -117,14 +118,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
}

0 comments on commit aa63240

Please sign in to comment.