2018-02-15 15:35:49 +00:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
2020-04-02 12:38:53 +00:00
|
|
|
"encoding/json"
|
2020-08-10 15:23:45 +00:00
|
|
|
"errors"
|
2018-02-19 09:24:28 +00:00
|
|
|
"fmt"
|
2020-07-28 07:59:21 +00:00
|
|
|
"go/ast"
|
2018-02-15 15:35:49 +00:00
|
|
|
"go/parser"
|
2020-07-28 15:40:41 +00:00
|
|
|
"go/types"
|
2018-02-15 15:35:49 +00:00
|
|
|
"io"
|
2018-02-19 09:24:28 +00:00
|
|
|
"io/ioutil"
|
2018-02-15 15:35:49 +00:00
|
|
|
"os"
|
2020-08-10 15:23:45 +00:00
|
|
|
"path"
|
2018-02-19 09:24:28 +00:00
|
|
|
"strings"
|
2018-02-15 15:35:49 +00:00
|
|
|
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-08-11 08:21:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-06-25 16:21:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
2018-02-25 12:26:56 +00:00
|
|
|
"golang.org/x/tools/go/loader"
|
2018-02-15 15:35:49 +00:00
|
|
|
)
|
|
|
|
|
2020-06-25 16:21:49 +00:00
|
|
|
const fileExt = "nef"
|
2018-02-15 15:35:49 +00:00
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
// Options contains all the parameters that affect the behaviour of the compiler.
|
|
|
|
type Options struct {
|
2020-06-25 16:21:49 +00:00
|
|
|
// The extension of the output file default set to .nef
|
2018-02-19 09:24:28 +00:00
|
|
|
Ext string
|
2018-02-15 15:35:49 +00:00
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
// The name of the output file.
|
|
|
|
Outfile string
|
2018-02-15 15:35:49 +00:00
|
|
|
|
2020-04-02 12:38:53 +00:00
|
|
|
// The name of the output for debug info.
|
|
|
|
DebugInfo string
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
|
2020-06-25 13:10:08 +00:00
|
|
|
// The name of the output for contract manifest file.
|
|
|
|
ManifestFile string
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
|
2020-08-04 09:55:36 +00:00
|
|
|
// Contract features.
|
2020-06-09 13:12:58 +00:00
|
|
|
ContractFeatures smartcontract.PropertyState
|
2020-08-04 09:55:36 +00:00
|
|
|
|
2020-08-11 08:21:54 +00:00
|
|
|
// Runtime notifications.
|
|
|
|
ContractEvents []manifest.Event
|
|
|
|
|
2020-08-04 09:55:36 +00:00
|
|
|
// The list of standards supported by the contract.
|
|
|
|
ContractSupportedStandards []string
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
type buildInfo struct {
|
|
|
|
initialPackage string
|
|
|
|
program *loader.Program
|
|
|
|
}
|
|
|
|
|
2020-08-05 08:14:43 +00:00
|
|
|
// ForEachPackage executes fn on each package used in the current program
|
|
|
|
// in the order they should be initialized.
|
|
|
|
func (c *codegen) ForEachPackage(fn func(*loader.PackageInfo)) {
|
2020-08-05 07:56:36 +00:00
|
|
|
for i := range c.packages {
|
|
|
|
pkg := c.buildInfo.program.Package(c.packages[i])
|
2020-07-28 07:59:21 +00:00
|
|
|
c.typeInfo = &pkg.Info
|
2020-07-29 14:20:00 +00:00
|
|
|
c.currPkg = pkg.Pkg
|
2020-08-05 08:14:43 +00:00
|
|
|
fn(pkg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForEachFile executes fn on each file used in current program.
|
|
|
|
func (c *codegen) ForEachFile(fn func(*ast.File, *types.Package)) {
|
|
|
|
c.ForEachPackage(func(pkg *loader.PackageInfo) {
|
2020-07-28 07:59:21 +00:00
|
|
|
for _, f := range pkg.Files {
|
2020-07-28 16:35:41 +00:00
|
|
|
c.fillImportMap(f, pkg.Pkg)
|
2020-07-28 15:40:41 +00:00
|
|
|
fn(f, pkg.Pkg)
|
2020-07-28 07:59:21 +00:00
|
|
|
}
|
2020-08-05 08:14:43 +00:00
|
|
|
})
|
2020-07-28 07:59:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 16:35:41 +00:00
|
|
|
// fillImportMap fills import map for f.
|
|
|
|
func (c *codegen) fillImportMap(f *ast.File, pkg *types.Package) {
|
|
|
|
c.importMap = map[string]string{"": pkg.Path()}
|
|
|
|
for _, imp := range f.Imports {
|
|
|
|
// We need to load find package metadata because
|
|
|
|
// name specified in `package ...` decl, can be in
|
|
|
|
// conflict with package path.
|
|
|
|
pkgPath := strings.Trim(imp.Path.Value, `"`)
|
|
|
|
realPkg := c.buildInfo.program.Package(pkgPath)
|
|
|
|
name := realPkg.Pkg.Name()
|
|
|
|
if imp.Name != nil {
|
|
|
|
name = imp.Name.Name
|
|
|
|
}
|
|
|
|
c.importMap[name] = realPkg.Pkg.Path()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 10:06:06 +00:00
|
|
|
func getBuildInfo(name string, src interface{}) (*buildInfo, error) {
|
2018-02-25 12:26:56 +00:00
|
|
|
conf := loader.Config{ParserMode: parser.ParseComments}
|
2020-08-10 15:23:45 +00:00
|
|
|
if src != nil {
|
|
|
|
f, err := conf.ParseFile(name, src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conf.CreateFromFiles("", f)
|
|
|
|
} else {
|
|
|
|
var names []string
|
|
|
|
if strings.HasSuffix(name, ".go") {
|
|
|
|
names = append(names, name)
|
|
|
|
} else {
|
|
|
|
ds, err := ioutil.ReadDir(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("'%s' is neither Go source nor a directory", name)
|
|
|
|
}
|
|
|
|
for i := range ds {
|
|
|
|
if !ds[i].IsDir() && strings.HasSuffix(ds[i].Name(), ".go") {
|
|
|
|
names = append(names, path.Join(name, ds[i].Name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(names) == 0 {
|
|
|
|
return nil, errors.New("no files provided")
|
|
|
|
}
|
|
|
|
conf.CreateFromFilenames("", names...)
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
prog, err := conf.Load()
|
2018-02-15 15:35:49 +00:00
|
|
|
if err != nil {
|
2018-02-19 09:24:28 +00:00
|
|
|
return nil, err
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 13:16:32 +00:00
|
|
|
return &buildInfo{
|
2020-08-10 15:23:45 +00:00
|
|
|
initialPackage: prog.InitialPackages()[0].Pkg.Name(),
|
2018-02-25 12:26:56 +00:00
|
|
|
program: prog,
|
2020-03-31 13:16:32 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile compiles a Go program into bytecode that can run on the NEO virtual machine.
|
2020-08-10 15:23:45 +00:00
|
|
|
// If `r != nil`, `name` is interpreted as a filename, and `r` as file contents.
|
|
|
|
// Otherwise `name` is either file name or name of the directory containing source files.
|
2020-08-10 10:06:06 +00:00
|
|
|
func Compile(name string, r io.Reader) ([]byte, error) {
|
|
|
|
buf, _, err := CompileWithDebugInfo(name, r)
|
2020-03-31 13:16:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 12:38:53 +00:00
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompileWithDebugInfo compiles a Go program into bytecode and emits debug info.
|
2020-08-10 10:06:06 +00:00
|
|
|
func CompileWithDebugInfo(name string, r io.Reader) ([]byte, *DebugInfo, error) {
|
|
|
|
ctx, err := getBuildInfo(name, r)
|
2018-02-19 09:24:28 +00:00
|
|
|
if err != nil {
|
2020-04-02 12:38:53 +00:00
|
|
|
return nil, nil, err
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
2020-04-02 12:38:53 +00:00
|
|
|
return CodeGen(ctx)
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 16:21:49 +00:00
|
|
|
// CompileAndSave will compile and save the file to disk in the NEF format.
|
2019-11-22 14:16:52 +00:00
|
|
|
func CompileAndSave(src string, o *Options) ([]byte, error) {
|
2018-03-29 06:24:45 +00:00
|
|
|
o.Outfile = strings.TrimSuffix(o.Outfile, fmt.Sprintf(".%s", fileExt))
|
2018-02-19 09:24:28 +00:00
|
|
|
if len(o.Outfile) == 0 {
|
2020-08-10 15:23:45 +00:00
|
|
|
if strings.HasSuffix(src, ".go") {
|
|
|
|
o.Outfile = strings.TrimSuffix(src, ".go")
|
|
|
|
} else {
|
|
|
|
o.Outfile = "out"
|
|
|
|
}
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
if len(o.Ext) == 0 {
|
|
|
|
o.Ext = fileExt
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
2020-08-10 15:23:45 +00:00
|
|
|
b, di, err := CompileWithDebugInfo(src, nil)
|
2018-02-19 09:24:28 +00:00
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return nil, fmt.Errorf("error while trying to compile smart contract file: %w", err)
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
2020-06-25 16:21:49 +00:00
|
|
|
f, err := nef.NewFile(b)
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return nil, fmt.Errorf("error while trying to create .nef file: %w", err)
|
2020-06-25 16:21:49 +00:00
|
|
|
}
|
|
|
|
bytes, err := f.Bytes()
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return nil, fmt.Errorf("error while serializing .nef file: %w", err)
|
2020-06-25 16:21:49 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
out := fmt.Sprintf("%s.%s", o.Outfile, o.Ext)
|
2020-06-25 16:21:49 +00:00
|
|
|
err = ioutil.WriteFile(out, bytes, os.ModePerm)
|
2020-06-26 10:40:19 +00:00
|
|
|
if err != nil {
|
2020-04-02 12:38:53 +00:00
|
|
|
return b, err
|
|
|
|
}
|
2020-06-30 11:21:58 +00:00
|
|
|
if o.DebugInfo == "" && o.ManifestFile == "" {
|
2020-06-26 10:40:19 +00:00
|
|
|
return b, nil
|
|
|
|
}
|
2020-06-30 11:21:58 +00:00
|
|
|
|
|
|
|
if o.DebugInfo != "" {
|
|
|
|
data, err := json.Marshal(di)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(o.DebugInfo, data, os.ModePerm); err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.ManifestFile != "" {
|
2020-08-11 08:21:54 +00:00
|
|
|
m, err := di.ConvertToManifest(o.ContractFeatures, o.ContractEvents, o.ContractSupportedStandards...)
|
2020-06-30 11:21:58 +00:00
|
|
|
if err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return b, fmt.Errorf("failed to convert debug info to manifest: %w", err)
|
2020-06-30 11:21:58 +00:00
|
|
|
}
|
|
|
|
mData, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return b, fmt.Errorf("failed to marshal manifest to JSON: %w", err)
|
2020-06-30 11:21:58 +00:00
|
|
|
}
|
|
|
|
return b, ioutil.WriteFile(o.ManifestFile, mData, os.ModePerm)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|