Skip to content

Commit

Permalink
op-bindings: Check for duplicate artifacts
Browse files Browse the repository at this point in the history
Checks for duplicate artifacts when instantiating the `hardhat` object. This armors against an issue in the regentool where the regentool was passing in the deployments directory rather than the network directory. This led to artifacts being non-deterministically chosen based on however `fs.WalkDir` iterated over the deployments directory.

Fixes ENG-3054
  • Loading branch information
mslipper committed Jan 23, 2023
1 parent 64c8b4b commit 82b8c2e
Show file tree
Hide file tree
Showing 4 changed files with 951 additions and 0 deletions.
10 changes: 10 additions & 0 deletions op-bindings/hardhat/hardhat.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (h *Hardhat) init() error {
// initDeployments reads all of the deployment json files from disk and then
// caches the deserialized `Deployment` structs.
func (h *Hardhat) initDeployments() error {
knownDeployments := make(map[string]string)
for _, deploymentPath := range h.DeploymentPaths {
fileSystem := os.DirFS(filepath.Join(deploymentPath, h.network))
err := fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
Expand Down Expand Up @@ -103,7 +104,16 @@ func (h *Hardhat) initDeployments() error {
}

deployment.Name = filepath.Base(name[:len(name)-5])
if knownDeployments[deployment.Name] != "" {
return fmt.Errorf(
"discovered duplicate deployment %s. old: %s, new: %s",
deployment.Name,
knownDeployments[deployment.Name],
name,
)
}
h.deployments = append(h.deployments, &deployment)
knownDeployments[deployment.Name] = name
return nil
})
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions op-bindings/hardhat/hardhat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ func TestHardhatGetDeployments(t *testing.T) {
require.NotNil(t, deployment)
}

func TestHardhatGetDeploymentsDuplicates(t *testing.T) {
t.Parallel()

// Set the network to an empty string to simulate
// an invalid network name.
_, err := hardhat.New(
"",
[]string{"testdata/artifacts"},
[]string{"testdata/deployments"},
)
require.Error(t, err)
require.Contains(t, err.Error(), "duplicate deployment")
}

func TestHardhatGetStorageLayout(t *testing.T) {
t.Parallel()

Expand Down
Loading

0 comments on commit 82b8c2e

Please sign in to comment.