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"
|
2018-02-15 15:35:49 +00:00
|
|
|
"go/parser"
|
|
|
|
"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"
|
2018-02-25 12:26:56 +00:00
|
|
|
"golang.org/x/tools/go/loader"
|
2018-02-15 15:35:49 +00:00
|
|
|
)
|
|
|
|
|
2018-02-24 09:06:48 +00:00
|
|
|
const fileExt = "avm"
|
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 {
|
|
|
|
// The extension of the output file default set to .avm
|
|
|
|
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
|
|
|
|
|
|
|
// The name of the output for application binary interface info.
|
|
|
|
ABIInfo string
|
|
|
|
|
|
|
|
// 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-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
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
// CompileAndSave will compile and save the file to disk.
|
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
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
out := fmt.Sprintf("%s.%s", o.Outfile, o.Ext)
|
2020-04-02 12:38:53 +00:00
|
|
|
err = ioutil.WriteFile(out, b, os.ModePerm)
|
|
|
|
if o.DebugInfo == "" {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
p, err := filepath.Abs(src)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
di.Documents = append(di.Documents, p)
|
|
|
|
data, err := json.Marshal(di)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
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
|
|
|
if err := ioutil.WriteFile(o.DebugInfo, data, os.ModePerm); err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
if o.ABIInfo == "" {
|
|
|
|
return b, err
|
|
|
|
}
|
2020-06-09 13:12:58 +00:00
|
|
|
abi := di.convertToABI(b, o.ContractFeatures)
|
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
|
|
|
abiData, err := json.Marshal(abi)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
return b, ioutil.WriteFile(o.ABIInfo, abiData, os.ModePerm)
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|