forked from ava-labs/subnet-evm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
198 lines (173 loc) · 5.36 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// (c) 2019-2020, Ava Labs, Inc.
//
// This file is a derived work, based on the go-ethereum library whose original
// notices appear below.
//
// It is distributed under a license compatible with the licensing terms of the
// original code from which it is derived.
//
// Much love to the original authors for their work.
// **********
// Copyright 2016 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
_ "embed"
"github.com/MetalBlockchain/subnet-evm/accounts/abi/bind"
"github.com/MetalBlockchain/subnet-evm/accounts/abi/bind/precompilebind"
"github.com/MetalBlockchain/subnet-evm/cmd/utils"
"github.com/MetalBlockchain/subnet-evm/internal/flags"
"github.com/ethereum/go-ethereum/log"
"github.com/urfave/cli/v2"
)
//go:embed template-readme.md
var readme string
var (
// Flags needed by abigen
abiFlag = &cli.StringFlag{
Name: "abi",
Usage: "Path to the contract ABI json to generate, - for STDIN",
}
typeFlag = &cli.StringFlag{
Name: "type",
Usage: "Struct name for the precompile (default = {abi file name})",
}
pkgFlag = &cli.StringFlag{
Name: "pkg",
Usage: "Go package name to generate the precompile into (default = {type})",
}
outFlag = &cli.StringFlag{
Name: "out",
Usage: "Output folder for the generated precompile files, - for STDOUT (default = ./precompile/contracts/{pkg}). Test files won't be generated if STDOUT is used",
}
)
var app = flags.NewApp("subnet-evm precompile generator tool")
func init() {
app.Name = "precompilegen"
app.Flags = []cli.Flag{
abiFlag,
outFlag,
pkgFlag,
typeFlag,
}
app.Action = precompilegen
}
func precompilegen(c *cli.Context) error {
outFlagStr := c.String(outFlag.Name)
isOutStdout := outFlagStr == "-"
if isOutStdout && !c.IsSet(typeFlag.Name) {
utils.Fatalf("type (--type) should be set explicitly for STDOUT ")
}
lang := bind.LangGo
// If the entire solidity code was specified, build and bind based on that
var (
bins []string
types []string
sigs []map[string]string
libs = make(map[string]string)
aliases = make(map[string]string)
)
if c.String(abiFlag.Name) == "" {
utils.Fatalf("no abi path is specified (--abi)")
}
// Load up the ABI
var (
abi []byte
err error
)
input := c.String(abiFlag.Name)
if input == "-" {
abi, err = io.ReadAll(os.Stdin)
} else {
abi, err = os.ReadFile(input)
}
if err != nil {
utils.Fatalf("Failed to read input ABI: %v", err)
}
bins = append(bins, "")
kind := c.String(typeFlag.Name)
if kind == "" {
fn := filepath.Base(input)
kind = strings.TrimSuffix(fn, filepath.Ext(fn))
kind = strings.TrimSpace(kind)
}
types = append(types, kind)
pkg := c.String(pkgFlag.Name)
if pkg == "" {
pkg = strings.ToLower(kind)
}
if outFlagStr == "" {
outFlagStr = filepath.Join("./precompile/contracts", pkg)
}
abifilename := ""
abipath := ""
// we should not generate the abi file if output is set to stdout
if !isOutStdout {
// get file name from the output path
abifilename = "contract.abi"
abipath = filepath.Join(outFlagStr, abifilename)
}
// if output is set to stdout, we should not generate the test codes
generateTests := !isOutStdout
// Generate the contract precompile
bindedFiles, err := precompilebind.PrecompileBind(types, string(abi), bins, sigs, pkg, lang, libs, aliases, abifilename, generateTests)
if err != nil {
utils.Fatalf("Failed to generate precompile: %v", err)
}
// Either flush it out to a file or display on the standard output
// Skip displaying test codes here.
if isOutStdout {
for _, file := range bindedFiles {
if !file.IsTest {
fmt.Printf("-----file: %s-----\n", file.FileName)
fmt.Printf("%s\n", file.Content)
}
}
return nil
}
if _, err := os.Stat(outFlagStr); os.IsNotExist(err) {
os.MkdirAll(outFlagStr, 0o700) // Create your file
}
for _, file := range bindedFiles {
outputPath := filepath.Join(outFlagStr, file.FileName)
if err := os.WriteFile(outputPath, []byte(file.Content), 0o600); err != nil {
utils.Fatalf("Failed to write generated file %s: %v", file.FileName, err)
}
}
// Write the ABI to the output folder
if err := os.WriteFile(abipath, abi, 0o600); err != nil {
utils.Fatalf("Failed to write ABI: %v", err)
}
// Write the README to the output folder
readmeOut := filepath.Join(outFlagStr, "README.md")
if err := os.WriteFile(readmeOut, []byte(readme), 0o600); err != nil {
utils.Fatalf("Failed to write README: %v", err)
}
fmt.Println("Precompile files generated successfully at: ", outFlagStr)
return nil
}
func main() {
log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}