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
}

View file

@ -7,6 +7,7 @@ import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/compiler"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/stretchr/testify/require"
)
@ -20,6 +21,8 @@ type compilerTestCase struct {
}
func TestCompiler(t *testing.T) {
// CompileAndSave use config.Version for proper .nef generation.
config.Version = "0.90.0-test"
testCases := []compilerTestCase{
{
name: "TestCompile",
@ -44,7 +47,7 @@ func TestCompiler(t *testing.T) {
require.NoError(t, err)
err = os.MkdirAll(exampleSavePath, os.ModePerm)
require.NoError(t, err)
outfile := exampleSavePath + "/test.avm"
outfile := exampleSavePath + "/test.nef"
_, err = compiler.CompileAndSave(exampleCompilePath+"/"+infos[0].Name(), &compiler.Options{Outfile: outfile})
require.NoError(t, err)
defer func() {

View file

@ -145,7 +145,16 @@ func methodStruct() struct{} { return struct{}{} }
},
Events: []Event{},
}
assert.Equal(t, expected, actual)
require.True(t, expected.ABI.Hash.Equals(actual.ABI.Hash))
require.ElementsMatch(t, expected.ABI.Methods, actual.ABI.Methods)
require.Equal(t, expected.ABI.EntryPoint, actual.ABI.EntryPoint)
require.Equal(t, expected.ABI.Events, actual.ABI.Events)
require.Equal(t, expected.Groups, actual.Groups)
require.Equal(t, expected.Features, actual.Features)
require.Equal(t, expected.Permissions, actual.Permissions)
require.Equal(t, expected.Trusts, actual.Trusts)
require.Equal(t, expected.SafeMethods, actual.SafeMethods)
require.Equal(t, expected.Extra, actual.Extra)
})
}