Commit 1f671532 authored by Mark Tyneway's avatar Mark Tyneway

op-bindings: fix canonicalize impl

parent 7ebc966a
...@@ -21,7 +21,8 @@ bindings-build: ...@@ -21,7 +21,8 @@ bindings-build:
-out ./bindings \ -out ./bindings \
-contracts ./artifacts.json \ -contracts ./artifacts.json \
-source-maps MIPS,PreimageOracle \ -source-maps MIPS,PreimageOracle \
-package $(pkg) -package $(pkg) \
-monorepo-base $(shell dirname $(realpath .))
mkdir: mkdir:
mkdir -p $(pkg) mkdir -p $(pkg)
......
...@@ -7,8 +7,6 @@ import ( ...@@ -7,8 +7,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"golang.org/x/exp/slices"
"github.com/ethereum-optimism/optimism/op-bindings/solc" "github.com/ethereum-optimism/optimism/op-bindings/solc"
) )
...@@ -36,7 +34,7 @@ type typeRemapping struct { ...@@ -36,7 +34,7 @@ type typeRemapping struct {
// inefficiency comes from replaceType, which performs a linear // inefficiency comes from replaceType, which performs a linear
// search of all replacements when performing substring matches of // search of all replacements when performing substring matches of
// composite types. // composite types.
func CanonicalizeASTIDs(in *solc.StorageLayout) *solc.StorageLayout { func CanonicalizeASTIDs(in *solc.StorageLayout, monorepoBase string) *solc.StorageLayout {
lastId := uint(1000) lastId := uint(1000)
astIDRemappings := make(map[uint]uint) astIDRemappings := make(map[uint]uint)
typeRemappings := make(map[string]string) typeRemappings := make(map[string]string)
...@@ -92,10 +90,7 @@ func CanonicalizeASTIDs(in *solc.StorageLayout) *solc.StorageLayout { ...@@ -92,10 +90,7 @@ func CanonicalizeASTIDs(in *solc.StorageLayout) *solc.StorageLayout {
// are used when there are 2 contracts imported with the same // are used when there are 2 contracts imported with the same
// name // name
if filepath.IsAbs(contract) { if filepath.IsAbs(contract) {
elements := strings.Split(contract, "/") contract = strings.TrimPrefix(strings.Replace(contract, monorepoBase, "", 1), "/")
if idx := slices.Index(elements, "optimism"); idx != -1 {
contract = filepath.Join(elements[idx+1:]...)
}
} }
outLayout.Storage = append(outLayout.Storage, solc.StorageLayoutEntry{ outLayout.Storage = append(outLayout.Storage, solc.StorageLayoutEntry{
......
...@@ -49,7 +49,7 @@ func TestCanonicalize(t *testing.T) { ...@@ -49,7 +49,7 @@ func TestCanonicalize(t *testing.T) {
// Run 100 times to make sure that we aren't relying // Run 100 times to make sure that we aren't relying
// on random map iteration order. // on random map iteration order.
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
require.Equal(t, testData.Out, CanonicalizeASTIDs(testData.In)) require.Equal(t, testData.Out, CanonicalizeASTIDs(testData.In, ""))
} }
}) })
} }
......
...@@ -22,6 +22,7 @@ type flags struct { ...@@ -22,6 +22,7 @@ type flags struct {
SourceMaps string SourceMaps string
OutDir string OutDir string
Package string Package string
MonorepoBase string
} }
type data struct { type data struct {
...@@ -39,8 +40,14 @@ func main() { ...@@ -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.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.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.Package, "package", "artifacts", "Go package name")
flag.StringVar(&f.MonorepoBase, "monorepo-base", "", "Base of the monorepo")
flag.Parse() 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) contractData, err := os.ReadFile(f.Contracts)
if err != nil { if err != nil {
log.Fatal("error reading contract list: %w\n", err) log.Fatal("error reading contract list: %w\n", err)
...@@ -147,7 +154,7 @@ func main() { ...@@ -147,7 +154,7 @@ func main() {
} }
storage := artifact.StorageLayout storage := artifact.StorageLayout
canonicalStorage := ast.CanonicalizeASTIDs(&storage) canonicalStorage := ast.CanonicalizeASTIDs(&storage, f.MonorepoBase)
ser, err := json.Marshal(canonicalStorage) ser, err := json.Marshal(canonicalStorage)
if err != nil { if err != nil {
log.Fatalf("error marshaling storage: %v\n", err) log.Fatalf("error marshaling storage: %v\n", err)
......
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