Skip to content

Commit

Permalink
Get rid of external library for abi generation (#1794)
Browse files Browse the repository at this point in the history
  • Loading branch information
begmaroman authored Aug 9, 2023
1 parent 5db2845 commit 032e5b9
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 59 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/e2e-polybft.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ jobs:
uses: actions/checkout@v3

- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.20.x
check-latest: true
cache-dependency-path: go.sum

- name: Run tests
run: make test-e2e-polybft
Expand Down
4 changes: 2 additions & 2 deletions consensus/polybft/contractsapi/artifact/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package artifact
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/0xPolygon/polygon-edge/helper/hex"
Expand All @@ -18,7 +18,7 @@ func ReadArtifactData(rootFolder, contractPath, contractName string) ([]byte, er
return nil, err
}

return ioutil.ReadFile(filepath.Clean(absolutePath))
return os.ReadFile(filepath.Clean(absolutePath))
}

func DecodeArtifact(data []byte) (*Artifact, error) {
Expand Down
27 changes: 18 additions & 9 deletions consensus/polybft/contractsapi/artifacts-gen/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"go/format"
"log"
"os"
"path"
"runtime"
"strings"

"github.com/dave/jennifer/jen"

"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi/artifact"
"github.com/0xPolygon/polygon-edge/helper/common"
)

const (
Expand All @@ -22,9 +24,6 @@ func main() {
currentPath := path.Dir(filename)
scpath := path.Join(currentPath, "../../../../core-contracts/artifacts/contracts/")

f := jen.NewFile("contractsapi")
f.Comment("This is auto-generated file. DO NOT EDIT.")

readContracts := []struct {
Path string
Name string
Expand Down Expand Up @@ -191,22 +190,32 @@ func main() {
},
}

str := `// This is auto-generated file. DO NOT EDIT.
package contractsapi
`

for _, v := range readContracts {
artifactBytes, err := artifact.ReadArtifactData(scpath, v.Path, getContractName(v.Path))
if err != nil {
log.Fatal(err)
}

f.Var().Id(v.Name + "Artifact").String().Op("=").Lit(string(artifactBytes))
dst := &bytes.Buffer{}
if err = json.Compact(dst, artifactBytes); err != nil {
log.Fatal(err)
}

str += fmt.Sprintf("var %sArtifact string = `%s`\n", v.Name, dst.String())
}

fl, err := os.Create(currentPath + "/../gen_sc_data.go")
output, err := format.Source([]byte(str))
if err != nil {
fmt.Println(str)
log.Fatal(err)
}

_, err = fmt.Fprintf(fl, "%#v", f)
if err != nil {
if err = common.SaveFileSafe(currentPath+"/../gen_sc_data.go", output, 0600); err != nil {
log.Fatal(err)
}
}
Expand Down
7 changes: 4 additions & 3 deletions consensus/polybft/contractsapi/bindings-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (
"errors"
"fmt"
"go/format"
"io/ioutil"
"log"
"strconv"
"strings"
"text/template"

"github.com/umbracle/ethgo/abi"

gensc "github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi/artifact"
"github.com/umbracle/ethgo/abi"
"github.com/0xPolygon/polygon-edge/helper/common"
)

const (
Expand Down Expand Up @@ -447,7 +448,7 @@ import (
log.Fatal(err)
}

if err := ioutil.WriteFile("./consensus/polybft/contractsapi/contractsapi.go", output, 0600); err != nil {
if err = common.SaveFileSafe("./consensus/polybft/contractsapi/contractsapi.go", output, 0600); err != nil {
log.Fatal(err)
}
}
Expand Down
82 changes: 41 additions & 41 deletions consensus/polybft/contractsapi/gen_sc_data.go

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ require (
)

require (
github.com/dave/jennifer v1.6.1
github.com/quasilyte/go-ruleguard v0.3.19
github.com/quasilyte/go-ruleguard/dsl v0.3.22
github.com/sethvargo/go-retry v0.2.4
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
github.com/dave/jennifer v1.6.1 h1:T4T/67t6RAA5AIV6+NP8Uk/BIsXgDoqEowgycdQQLuk=
github.com/dave/jennifer v1.6.1/go.mod h1:nXbxhEmQfOZhWml3D1cDK5M1FLnMSozpbFN/m3RmGZc=
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down

0 comments on commit 032e5b9

Please sign in to comment.