2018-02-15 15:35:49 +00:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-04-02 12:38:53 +00:00
|
|
|
"encoding/json"
|
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-04-02 12:38:53 +00:00
|
|
|
"path/filepath"
|
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-06-25 16:21:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
2020-06-30 11:21:58 +00:00
|
|
|
"github.com/pkg/errors"
|
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
|
|
|
|
|
|
|
// Contract metadata.
|
2020-06-09 13:12:58 +00:00
|
|
|
ContractFeatures smartcontract.PropertyState
|
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-07-28 07:59:21 +00:00
|
|
|
// ForEachFile executes fn on each file used in current program.
|
2020-07-28 15:40:41 +00:00
|
|
|
func (c *codegen) ForEachFile(fn func(*ast.File, *types.Package)) {
|
2020-07-28 07:59:21 +00:00
|
|
|
for _, pkg := range c.buildInfo.program.AllPackages {
|
|
|
|
c.typeInfo = &pkg.Info
|
|
|
|
for _, f := range pkg.Files {
|
2020-07-28 15:40:41 +00:00
|
|
|
fn(f, pkg.Pkg)
|
2020-07-28 07:59:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 13:16:32 +00:00
|
|
|
func getBuildInfo(src interface{}) (*buildInfo, error) {
|
2018-02-25 12:26:56 +00:00
|
|
|
conf := loader.Config{ParserMode: parser.ParseComments}
|
2020-03-31 13:16:32 +00:00
|
|
|
f, err := conf.ParseFile("", src)
|
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
|
|
|
}
|
2018-02-25 12:26:56 +00:00
|
|
|
conf.CreateFromFiles("", f)
|
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{
|
2018-02-25 12:26:56 +00:00
|
|
|
initialPackage: f.Name.Name,
|
|
|
|
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.
|
|
|
|
func Compile(r io.Reader) ([]byte, error) {
|
2020-04-02 12:38:53 +00:00
|
|
|
buf, _, err := CompileWithDebugInfo(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.
|
|
|
|
func CompileWithDebugInfo(r io.Reader) ([]byte, *DebugInfo, error) {
|
|
|
|
ctx, err := getBuildInfo(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-02-24 09:10:45 +00:00
|
|
|
if !strings.HasSuffix(src, ".go") {
|
2019-11-22 14:16:52 +00:00
|
|
|
return nil, fmt.Errorf("%s is not a Go file", src)
|
2018-02-24 09:10:45 +00:00
|
|
|
}
|
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 {
|
|
|
|
o.Outfile = strings.TrimSuffix(src, ".go")
|
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
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
b, err := ioutil.ReadFile(src)
|
|
|
|
if err != nil {
|
2019-11-22 14:16:52 +00:00
|
|
|
return nil, err
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
2020-04-02 12:38:53 +00:00
|
|
|
b, di, err := CompileWithDebugInfo(bytes.NewReader(b))
|
2018-02-19 09:24:28 +00:00
|
|
|
if err != nil {
|
2019-11-22 14:16:52 +00:00
|
|
|
return nil, fmt.Errorf("error while trying to compile smart contract file: %v", 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 {
|
|
|
|
return nil, fmt.Errorf("error while trying to create .nef file: %v", err)
|
|
|
|
}
|
|
|
|
bytes, err := f.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error while serializing .nef file: %v", err)
|
|
|
|
}
|
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
|
|
|
|
2020-04-02 12:38:53 +00:00
|
|
|
p, err := filepath.Abs(src)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
di.Documents = append(di.Documents, p)
|
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-07-23 14:01:51 +00:00
|
|
|
m, err := di.ConvertToManifest(o.ContractFeatures)
|
2020-06-30 11:21:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return b, errors.Wrap(err, "failed to convert debug info to manifest")
|
|
|
|
}
|
|
|
|
mData, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return b, errors.Wrap(err, "failed to marshal manifest")
|
|
|
|
}
|
|
|
|
return b, ioutil.WriteFile(o.ManifestFile, mData, os.ModePerm)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|