Skip to content

Commit

Permalink
refact : using compiledTy
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-shin-wt committed Jun 18, 2024
1 parent 62f6725 commit c4052f4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 76 deletions.
71 changes: 69 additions & 2 deletions wemix/governance-contract/common/compile/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common/compiler"
"github.com/ethereum/go-ethereum/crypto"
"github.com/fabelx/go-solc-select/pkg/config"
"github.com/fabelx/go-solc-select/pkg/installer"
"github.com/fabelx/go-solc-select/pkg/versions"
"github.com/pkg/errors"
)

var (
solcVersion string = "0.8.14"
solcCmd string = fmt.Sprintf("solc-%s", solcVersion)
)

func Compile(root string, sourceFiles ...string) (map[string]*compiler.Contract, error) {
func Compile(root string, sourceFiles ...string) (compiledTy, error) {
if err := installSolc(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -54,7 +58,7 @@ func Compile(root string, sourceFiles ...string) (map[string]*compiler.Contract,
} else if contracts, err := compiler.ParseCombinedJSON(stdout.Bytes(), "", "", compilerVersion.Version, strings.Join(args, " ")); err != nil {
return nil, err
} else {
out := make(map[string]*compiler.Contract)
out := make(compiledTy)
for name, v := range contracts {
n := strings.Split(name, ":")[1]
if _, ok := out[n]; ok {
Expand Down Expand Up @@ -89,3 +93,66 @@ func installSolc() error {
}
return fmt.Errorf("failed to install version %s", solcVersion)
}

type compiledTy map[string]*compiler.Contract

func (compiled compiledTy) BindContracts(pkg, filename string, contracts ...string) error {
var (
length = len(contracts)
types = make([]string, length)
abis = make([]string, length)
bytecodes = make([]string, length)
)

var err error
for i, name := range contracts {
contract, ok := compiled[name]
if !ok {
return fmt.Errorf("not found contract : %v", name)
}
types[i] = name
bytecodes[i] = contract.Code
if abis[i], err = abiToString(contract); err != nil {
return errors.Wrap(err, name)
}
}

str, err := bind.Bind(types, abis, bytecodes, nil, pkg, bind.LangGo, nil, nil)
if err != nil {
return err
}

filedata := []byte(str)
if read, err := os.ReadFile(filename); err == nil {
// if out file is already exists, compare the file contents
if crypto.Keccak256Hash(read) == crypto.Keccak256Hash(filedata) {
return nil
}
} else {
// check dir is exist
outDir := filepath.Dir(filename)
if _, err := os.Stat(outDir); err != nil {
if !os.IsNotExist(err) {
return err
}
if err = os.MkdirAll(outDir, 0755); err != nil {
return err
}
}
}

return os.WriteFile(filename, filedata, 0644)
}

func abiToString(contract *compiler.Contract) (abi string, err error) {
switch v := contract.Info.AbiDefinition.(type) {
case string:
abi = v
default:
var bytes []byte
if bytes, err = json.Marshal(v); err == nil {
abi = string(bytes)
}
}
return
}
77 changes: 9 additions & 68 deletions wemix/governance-contract/contracts/abigen.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common/compiler"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/wemix/governance-contract/common/compile"
)

const pkg string = "gov"

var (
rootFlag = flag.String("root", "../contracts", "")
outDir string
)

const pkg string = "gov"

func main() {
flag.Parse()
root := *rootFlag
outDir = filepath.Join(root, "../../bind")
outDir := filepath.Join(root, "../../bind")
if contracts, err := compile.Compile(root,
filepath.Join(root, "Registry.sol"),
filepath.Join(root, "Gov.sol"),
Expand All @@ -39,71 +32,19 @@ func main() {
filepath.Join(root, "storage", "EnvStorageImp.sol"),
); err != nil {
panic(err)
} else if err := bindContracts(contracts, "registry", "Registry"); err != nil {
} else if err := contracts.BindContracts(pkg, filepath.Join(outDir, "registry.go"), "Registry"); err != nil {
panic(err)
} else if err := bindContracts(contracts, "gov", "Gov", "GovImp"); err != nil {
} else if err := contracts.BindContracts(pkg, filepath.Join(outDir, "gov.go"), "Gov", "GovImp"); err != nil {
panic(err)
} else if err := bindContracts(contracts, "ncpExit", "NCPExit", "NCPExitImp"); err != nil {
} else if err := contracts.BindContracts(pkg, filepath.Join(outDir, "ncpExit.go"), "NCPExit", "NCPExitImp"); err != nil {
panic(err)
} else if err := bindContracts(contracts, "staking", "Staking", "StakingImp"); err != nil {
} else if err := contracts.BindContracts(pkg, filepath.Join(outDir, "staking.go"), "Staking", "StakingImp"); err != nil {
panic(err)
} else if err := bindContracts(contracts, "ballotStorage", "BallotStorage", "BallotStorageImp"); err != nil {
} else if err := contracts.BindContracts(pkg, filepath.Join(outDir, "ballotStorage.go"), "BallotStorage", "BallotStorageImp"); err != nil {
panic(err)
} else if err := bindContracts(contracts, "envStorage", "EnvStorage", "EnvStorageImp"); err != nil {
} else if err := contracts.BindContracts(pkg, filepath.Join(outDir, "envStorage.go"), "EnvStorage", "EnvStorageImp"); err != nil {
panic(err)
} else {
fmt.Println("success!")
}
}

func bindContracts(contracts map[string]*compiler.Contract, fname string, cnames ...string) error {
length := len(cnames)
types := make([]string, length)
abis := make([]string, length)
bytecodes := make([]string, length)

for i, name := range cnames {
contract, ok := contracts[name]
if !ok {
return ethereum.NotFound
}
types[i] = name
abis[i] = abiToString(contract.Info.AbiDefinition)
bytecodes[i] = contract.Code
}

str, err := bind.Bind(types, abis, bytecodes, nil, pkg, bind.LangGo, nil, nil)
if err != nil {
return err
}

var (
filename = filepath.Join(outDir, fmt.Sprintf("%s.go", fname))
filedata = []byte(str)
)

if read, err := os.ReadFile(filename); err == nil {
if crypto.Keccak256Hash(read) == crypto.Keccak256Hash(filedata) {
return nil
}
}

if _, err := os.Stat(outDir); err != nil {
if !os.IsNotExist(err) {
return err
}
if err = os.MkdirAll(outDir, 0755); err != nil {
return err
}
}

return os.WriteFile(filename, filedata, 0644)
}

func abiToString(definition interface{}) string {
if str, ok := definition.(string); ok {
return str
}
bytes, _ := json.Marshal(definition)
return string(bytes)
}
6 changes: 0 additions & 6 deletions wemix/governance-contract/test/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ func DeployGovernance(t *testing.T) *Governance {
gov.NCPExit.ExecuteOk(t, gov.client.Owner, "initialize", gov.Registry.Address)
gov.Staking.ExecuteOk(t, gov.client.Owner, "init", gov.Registry.Address, []byte{})
gov.BallotStorage.ExecuteOk(t, gov.client.Owner, "initialize", gov.Registry.Address)

// [EnvStorage] env 의 기본값 설정 데이터
envNames, envValues := makeEnvParams(
EnvConstants.BLOCKS_PER,
EnvConstants.BALLOT_DURATION_MIN,
Expand All @@ -70,9 +68,7 @@ func DeployGovernance(t *testing.T) *Governance {
EnvConstants.BASE_FEE_MAX_CHANGE_RATE,
EnvConstants.GAS_TARGET_PERCENTAGE,
)

gov.EnvStorage.ExecuteOk(t, gov.client.Owner, "initialize", gov.Registry.Address, envNames, envValues)

gov.Staking.ExecuteWithETHOk(t, gov.client.Owner, LOCK_AMOUNT, "deposit")
node := nodeInfo{
[]byte("name"),
Expand Down Expand Up @@ -144,10 +140,8 @@ func (c *Compiled) Compile(root string) {
}

func (c *Compiled) Copy(g *Governance) *Governance {
// 같은 이름의 field가 있으면 카피
typ := reflect.TypeOf(c).Elem()
for i := 0; i < typ.NumField(); i++ {
// 없으면 스킵.
if v := reflect.ValueOf(g).Elem().FieldByName(typ.Field(i).Name); v.IsValid() {
v.Set(reflect.ValueOf(c).Elem().Field(i))
}
Expand Down

0 comments on commit c4052f4

Please sign in to comment.