Commit 8b39517e authored by Wyatt Barnes's avatar Wyatt Barnes Committed by GitHub

Bindings and metadata overwrite warning (#8283)

* Add deployed bytecode retrieval mitigation

* Bindings and metadata overwrite warning
parent 4abef208
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 {
return fmt.Errorf("error reading existing metadata output file, metadataFilePath: %s err: %w", metadataFilePath, 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
}
......
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 {
return fmt.Errorf("error reading existing bindings output file, outFilePath: %s err: %w", outFilePath, 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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment