Skip to content

Commit

Permalink
op-bindings: fix canonicalize impl
Browse files Browse the repository at this point in the history
  • Loading branch information
tynes committed Jul 17, 2023
1 parent 7ebc966 commit 1f67153
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
3 changes: 2 additions & 1 deletion op-bindings/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ bindings-build:
-out ./bindings \
-contracts ./artifacts.json \
-source-maps MIPS,PreimageOracle \
-package $(pkg)
-package $(pkg) \
-monorepo-base $(shell dirname $(realpath .))

mkdir:
mkdir -p $(pkg)
Expand Down
9 changes: 2 additions & 7 deletions op-bindings/ast/canonicalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"strconv"
"strings"

"golang.org/x/exp/slices"

"github.com/ethereum-optimism/optimism/op-bindings/solc"
)

Expand Down Expand Up @@ -36,7 +34,7 @@ type typeRemapping struct {
// inefficiency comes from replaceType, which performs a linear
// search of all replacements when performing substring matches of
// composite types.
func CanonicalizeASTIDs(in *solc.StorageLayout) *solc.StorageLayout {
func CanonicalizeASTIDs(in *solc.StorageLayout, monorepoBase string) *solc.StorageLayout {
lastId := uint(1000)
astIDRemappings := make(map[uint]uint)
typeRemappings := make(map[string]string)
Expand Down Expand Up @@ -92,10 +90,7 @@ func CanonicalizeASTIDs(in *solc.StorageLayout) *solc.StorageLayout {
// are used when there are 2 contracts imported with the same
// name
if filepath.IsAbs(contract) {
elements := strings.Split(contract, "/")
if idx := slices.Index(elements, "optimism"); idx != -1 {
contract = filepath.Join(elements[idx+1:]...)
}
contract = strings.TrimPrefix(strings.Replace(contract, monorepoBase, "", 1), "/")
}

outLayout.Storage = append(outLayout.Storage, solc.StorageLayoutEntry{
Expand Down
2 changes: 1 addition & 1 deletion op-bindings/ast/canonicalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestCanonicalize(t *testing.T) {
// Run 100 times to make sure that we aren't relying
// on random map iteration order.
for i := 0; i < 100; i++ {
require.Equal(t, testData.Out, CanonicalizeASTIDs(testData.In))
require.Equal(t, testData.Out, CanonicalizeASTIDs(testData.In, ""))
}
})
}
Expand Down
9 changes: 8 additions & 1 deletion op-bindings/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type flags struct {
SourceMaps string
OutDir string
Package string
MonorepoBase string
}

type data struct {
Expand All @@ -39,8 +40,14 @@ func main() {
flag.StringVar(&f.Contracts, "contracts", "artifacts.json", "Path to file containing list of contracts to generate bindings for")
flag.StringVar(&f.SourceMaps, "source-maps", "", "Comma-separated list of contracts to generate source-maps for")
flag.StringVar(&f.Package, "package", "artifacts", "Go package name")
flag.StringVar(&f.MonorepoBase, "monorepo-base", "", "Base of the monorepo")
flag.Parse()

if f.MonorepoBase == "" {
log.Fatal("must provide -monorepo-base")
}
log.Printf("Using monorepo base %s\n", f.MonorepoBase)

contractData, err := os.ReadFile(f.Contracts)
if err != nil {
log.Fatal("error reading contract list: %w\n", err)
Expand Down Expand Up @@ -147,7 +154,7 @@ func main() {
}

storage := artifact.StorageLayout
canonicalStorage := ast.CanonicalizeASTIDs(&storage)
canonicalStorage := ast.CanonicalizeASTIDs(&storage, f.MonorepoBase)
ser, err := json.Marshal(canonicalStorage)
if err != nil {
log.Fatalf("error marshaling storage: %v\n", err)
Expand Down

0 comments on commit 1f67153

Please sign in to comment.