github.com/MetalBlockchain/subnet-evm@v0.4.9/cmd/precompilegen/main.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2016 The go-ethereum Authors
    12  // This file is part of go-ethereum.
    13  //
    14  // go-ethereum is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // go-ethereum is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU General Public License
    25  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package main
    28  
    29  import (
    30  	"fmt"
    31  	"io"
    32  	"os"
    33  	"path/filepath"
    34  	"strings"
    35  
    36  	"github.com/MetalBlockchain/subnet-evm/accounts/abi/bind"
    37  	"github.com/MetalBlockchain/subnet-evm/internal/flags"
    38  	"github.com/ethereum/go-ethereum/cmd/utils"
    39  	"github.com/ethereum/go-ethereum/log"
    40  	"github.com/urfave/cli/v2"
    41  )
    42  
    43  var (
    44  	// Git SHA1 commit hash of the release (set via linker flags)
    45  	gitCommit = ""
    46  	gitDate   = ""
    47  
    48  	app *cli.App
    49  )
    50  
    51  var (
    52  	// Flags needed by abigen
    53  	abiFlag = &cli.StringFlag{
    54  		Name:  "abi",
    55  		Usage: "Path to the Ethereum contract ABI json to bind, - for STDIN",
    56  	}
    57  	typeFlag = &cli.StringFlag{
    58  		Name:  "type",
    59  		Usage: "Struct name for the precompile (default = ABI name)",
    60  	}
    61  	pkgFlag = &cli.StringFlag{
    62  		Name:  "pkg",
    63  		Usage: "Package name to generate the precompile into (default = precompile)",
    64  	}
    65  	outFlag = &cli.StringFlag{
    66  		Name:  "out",
    67  		Usage: "Output file for the generated precompile (default = STDOUT)",
    68  	}
    69  )
    70  
    71  func init() {
    72  	app = flags.NewApp(gitCommit, gitDate, "subnet-evm precompile generator tool")
    73  	app.Name = "precompilegen"
    74  	app.Flags = []cli.Flag{
    75  		abiFlag,
    76  		outFlag,
    77  		pkgFlag,
    78  		typeFlag,
    79  	}
    80  	app.Action = precompilegen
    81  }
    82  
    83  func precompilegen(c *cli.Context) error {
    84  	if !c.IsSet(outFlag.Name) && !c.IsSet(typeFlag.Name) {
    85  		utils.Fatalf("type (--type) should be set explicitly for STDOUT ")
    86  	}
    87  	pkg := pkgFlag.Name
    88  	if pkg == "" {
    89  		pkg = "precompile"
    90  	}
    91  	lang := bind.LangGo
    92  	// If the entire solidity code was specified, build and bind based on that
    93  	var (
    94  		abis    []string
    95  		bins    []string
    96  		types   []string
    97  		sigs    []map[string]string
    98  		libs    = make(map[string]string)
    99  		aliases = make(map[string]string)
   100  	)
   101  	if c.String(abiFlag.Name) == "" {
   102  		utils.Fatalf("no abi path is specified (--abi)")
   103  	}
   104  	// Load up the ABI
   105  	var (
   106  		abi []byte
   107  		err error
   108  	)
   109  	input := c.String(abiFlag.Name)
   110  	if input == "-" {
   111  		abi, err = io.ReadAll(os.Stdin)
   112  	} else {
   113  		abi, err = os.ReadFile(input)
   114  	}
   115  	if err != nil {
   116  		utils.Fatalf("Failed to read input ABI: %v", err)
   117  	}
   118  	abis = append(abis, string(abi))
   119  	bins = append(bins, "")
   120  	kind := c.String(typeFlag.Name)
   121  	if kind == "" {
   122  		fn := filepath.Base(input)
   123  		kind = strings.TrimSuffix(fn, filepath.Ext(fn))
   124  		kind = strings.TrimSpace(kind)
   125  	}
   126  	types = append(types, kind)
   127  
   128  	// Generate the contract precompile
   129  	code, err := bind.Bind(types, abis, bins, sigs, pkg, lang, libs, aliases, true)
   130  	if err != nil {
   131  		utils.Fatalf("Failed to generate ABI precompile: %v", err)
   132  	}
   133  
   134  	// Either flush it out to a file or display on the standard output
   135  	if !c.IsSet(outFlag.Name) {
   136  		fmt.Printf("%s\n", code)
   137  		return nil
   138  	}
   139  
   140  	if err := os.WriteFile(c.String(outFlag.Name), []byte(code), 0o600); err != nil {
   141  		utils.Fatalf("Failed to write ABI precompile: %v", err)
   142  	}
   143  
   144  	fmt.Println("Precompile Generation was a success!")
   145  	return nil
   146  }
   147  
   148  func main() {
   149  	log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
   150  
   151  	if err := app.Run(os.Args); err != nil {
   152  		fmt.Fprintln(os.Stderr, err)
   153  		os.Exit(1)
   154  	}
   155  }