cli, compiler: switch from .avm to .nef

We don't generate clear .avm instructions anymore. Instead, use .nef
files with additional metadata.
This commit is contained in:
Anna Shaleva 2020-06-25 19:21:49 +03:00
parent 6b8957243a
commit c7746da023
6 changed files with 67 additions and 33 deletions

View file

@ -12,14 +12,15 @@ import (
"strings"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
"golang.org/x/tools/go/loader"
)
const fileExt = "avm"
const fileExt = "nef"
// 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
// The extension of the output file default set to .nef
Ext string
// The name of the output file.
@ -78,7 +79,7 @@ func CompileWithDebugInfo(r io.Reader) ([]byte, *DebugInfo, error) {
return CodeGen(ctx)
}
// CompileAndSave will compile and save the file to disk.
// CompileAndSave will compile and save the file to disk in the NEF format.
func CompileAndSave(src string, o *Options) ([]byte, error) {
if !strings.HasSuffix(src, ".go") {
return nil, fmt.Errorf("%s is not a Go file", src)
@ -98,8 +99,16 @@ func CompileAndSave(src string, o *Options) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("error while trying to compile smart contract file: %v", err)
}
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)
}
out := fmt.Sprintf("%s.%s", o.Outfile, o.Ext)
err = ioutil.WriteFile(out, b, os.ModePerm)
err = ioutil.WriteFile(out, bytes, os.ModePerm)
if o.DebugInfo == "" {
return b, err
}