mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-05 03:58:23 +00:00
99d0bafa2c
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).
127 lines
2.8 KiB
Go
127 lines
2.8 KiB
Go
package compiler
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"go/parser"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
"golang.org/x/tools/go/loader"
|
|
)
|
|
|
|
const fileExt = "avm"
|
|
|
|
// 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
|
|
|
|
// The name of the output file.
|
|
Outfile string
|
|
|
|
// The name of the output for debug info.
|
|
DebugInfo string
|
|
|
|
// The name of the output for application binary interface info.
|
|
ABIInfo string
|
|
|
|
// Contract metadata.
|
|
ContractDetails *smartcontract.ContractDetails
|
|
}
|
|
|
|
type buildInfo struct {
|
|
initialPackage string
|
|
program *loader.Program
|
|
}
|
|
|
|
func getBuildInfo(src interface{}) (*buildInfo, error) {
|
|
conf := loader.Config{ParserMode: parser.ParseComments}
|
|
f, err := conf.ParseFile("", src)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
conf.CreateFromFiles("", f)
|
|
|
|
prog, err := conf.Load()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &buildInfo{
|
|
initialPackage: f.Name.Name,
|
|
program: prog,
|
|
}, nil
|
|
}
|
|
|
|
// Compile compiles a Go program into bytecode that can run on the NEO virtual machine.
|
|
func Compile(r io.Reader) ([]byte, error) {
|
|
buf, _, err := CompileWithDebugInfo(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
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)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
return CodeGen(ctx)
|
|
}
|
|
|
|
// CompileAndSave will compile and save the file to disk.
|
|
func CompileAndSave(src string, o *Options) ([]byte, error) {
|
|
if !strings.HasSuffix(src, ".go") {
|
|
return nil, fmt.Errorf("%s is not a Go file", src)
|
|
}
|
|
o.Outfile = strings.TrimSuffix(o.Outfile, fmt.Sprintf(".%s", fileExt))
|
|
if len(o.Outfile) == 0 {
|
|
o.Outfile = strings.TrimSuffix(src, ".go")
|
|
}
|
|
if len(o.Ext) == 0 {
|
|
o.Ext = fileExt
|
|
}
|
|
b, err := ioutil.ReadFile(src)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
b, di, err := CompileWithDebugInfo(bytes.NewReader(b))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error while trying to compile smart contract file: %v", err)
|
|
}
|
|
out := fmt.Sprintf("%s.%s", o.Outfile, o.Ext)
|
|
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
|
|
}
|
|
if err := ioutil.WriteFile(o.DebugInfo, data, os.ModePerm); err != nil {
|
|
return b, err
|
|
}
|
|
if o.ABIInfo == "" {
|
|
return b, err
|
|
}
|
|
abi := di.convertToABI(b, o.ContractDetails)
|
|
abiData, err := json.Marshal(abi)
|
|
if err != nil {
|
|
return b, err
|
|
}
|
|
return b, ioutil.WriteFile(o.ABIInfo, abiData, os.ModePerm)
|
|
}
|