-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide embedded compiled Neo contracts
There is a need to embed Neo contracts' executables into NeoFS Inner Ring application. To do this, `contracts` directory is created. The dir contains compiled contracts (per-contract NEF and manifest). Create eponymous Go package that provides embedded `fs.FS` with the contracts. Add `Read` function which reads, decodes and validates all numerically-sored contracts from files and returns ready-to-go data for deployment. Refs #2195. Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
- Loading branch information
1 parent
93733ff
commit b88112b
Showing
3 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
Package contracts embeds compiled Neo contracts and provides access to them. | ||
*/ | ||
package contracts | ||
|
||
import ( | ||
"embed" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/nspcc-dev/neo-go/pkg/io" | ||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" | ||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef" | ||
) | ||
|
||
// Contract groups information about Neo contract stored in the current package. | ||
type Contract struct { | ||
NEF nef.File | ||
Manifest manifest.Manifest | ||
} | ||
|
||
//go:embed *.nef *.manifest.json | ||
var _fs embed.FS | ||
|
||
// Read reads compiled contracts stored in the package sorted numerically. | ||
// File schema: | ||
// - compiled executables (NEF) are named by pattern 'N.nef' | ||
// - JSON-encoded manifests are named by pattern 'N.manifest.json' | ||
// | ||
// where N is the serial number of the contract starting from 0. Leading zeros | ||
// are ignored (except zero sequence corresponding to N=0). | ||
// | ||
// If NEF file exists, corresponding manifest file must exist. If manifest | ||
// file is presented without corresponding NEF file, the contract is ignored. | ||
// | ||
// Read fails if contract files has invalid name or format. | ||
func Read() ([]Contract, error) { | ||
return read(_fs) | ||
} | ||
|
||
const nefFileSuffix = ".nef" | ||
|
||
var ( | ||
errInvalidFilename = errors.New("invalid file name") | ||
errDuplicatedContract = errors.New("duplicated contract") | ||
errInvalidNEF = errors.New("invalid NEF") | ||
errInvalidManifest = errors.New("invalid manifest") | ||
) | ||
|
||
type numberedContract struct { | ||
i int | ||
c Contract | ||
} | ||
|
||
type numberedContracts []numberedContract | ||
|
||
func (x numberedContracts) Len() int { return len(x) } | ||
func (x numberedContracts) Less(i, j int) bool { return x[i].i < x[j].i } | ||
func (x numberedContracts) Swap(i, j int) { x[i], x[j] = x[j], x[i] } | ||
|
||
// read same as Read by allows to override source fs.FS. | ||
func read(_fs fs.FS) ([]Contract, error) { | ||
nefFiles, err := fs.Glob(_fs, "*"+nefFileSuffix) | ||
if err != nil { | ||
return nil, fmt.Errorf("match files with suffix %s", nefFileSuffix) | ||
} | ||
|
||
cs := make(numberedContracts, 0, len(nefFiles)) | ||
|
||
for i := range nefFiles { | ||
prefix := strings.TrimSuffix(nefFiles[i], nefFileSuffix) | ||
if prefix == "" { | ||
return nil, fmt.Errorf("%w: missing prefix '%s'", errInvalidFilename, nefFiles[i]) | ||
} | ||
|
||
var ind int | ||
|
||
if noZerosPrefix := strings.TrimLeft(prefix, "0"); len(noZerosPrefix) > 0 { | ||
ind, err = strconv.Atoi(noZerosPrefix) | ||
if err != nil { | ||
return nil, fmt.Errorf("%w: invalid prefix of file name '%s' (expected serial number)", errInvalidFilename, nefFiles[i]) | ||
} else if ind < 0 { | ||
return nil, fmt.Errorf("%w: negative serial number in file name '%s'", errInvalidFilename, nefFiles[i]) | ||
} | ||
} | ||
|
||
for i := range cs { | ||
if cs[i].i == ind { | ||
return nil, fmt.Errorf("%w: more than one file with serial number #%d", errDuplicatedContract, ind) | ||
} | ||
} | ||
|
||
c, err := readContractFromFiles(_fs, prefix) | ||
if err != nil { | ||
return nil, fmt.Errorf("read contract #%d: %w", ind, err) | ||
} | ||
|
||
cs = append(cs, numberedContract{ | ||
i: ind, | ||
c: c, | ||
}) | ||
} | ||
|
||
sort.Sort(cs) | ||
|
||
res := make([]Contract, len(cs)) | ||
|
||
for i := range cs { | ||
res[i] = cs[i].c | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func readContractFromFiles(_fs fs.FS, filePrefix string) (c Contract, err error) { | ||
fNEF, err := _fs.Open(filePrefix + nefFileSuffix) | ||
if err != nil { | ||
return c, fmt.Errorf("open file containing contract NEF: %w", err) | ||
} | ||
defer fNEF.Close() | ||
|
||
fManifest, err := _fs.Open(filePrefix + ".manifest.json") | ||
if err != nil { | ||
return c, fmt.Errorf("open file containing contract NEF: %w", err) | ||
} | ||
defer fManifest.Close() | ||
|
||
bReader := io.NewBinReaderFromIO(fNEF) | ||
c.NEF.DecodeBinary(bReader) | ||
if bReader.Err != nil { | ||
return c, fmt.Errorf("%w: %v", errInvalidNEF, bReader.Err) | ||
} | ||
|
||
err = json.NewDecoder(fManifest).Decode(&c.Manifest) | ||
if err != nil { | ||
return c, fmt.Errorf("%w: %v", errInvalidManifest, err) | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package contracts | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/json" | ||
"testing" | ||
"testing/fstest" | ||
|
||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" | ||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestReadRepo(t *testing.T) { | ||
_, err := Read() | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestReadOrder(t *testing.T) { | ||
_nef0, bNEF0 := anyValidNEF(t) | ||
_manifest0, jManifest0 := anyValidManifest(t, "first") | ||
_nef1, bNEF1 := anyValidNEF(t) | ||
_manifest1, jManifest1 := anyValidManifest(t, "second") | ||
_nef11, bNEF11 := anyValidNEF(t) | ||
_manifest11, jManifest11 := anyValidManifest(t, "twelfth") | ||
|
||
_fs := fstest.MapFS{ | ||
"00.nef": {Data: bNEF0}, | ||
"00.manifest.json": {Data: jManifest0}, | ||
"01.nef": {Data: bNEF1}, | ||
"01.manifest.json": {Data: jManifest1}, | ||
"11.nef": {Data: bNEF11}, | ||
"11.manifest.json": {Data: jManifest11}, | ||
} | ||
|
||
cs, err := read(_fs) | ||
require.NoError(t, err) | ||
require.Len(t, cs, 3) | ||
|
||
require.Equal(t, _nef0, cs[0].NEF) | ||
require.Equal(t, _manifest0, cs[0].Manifest) | ||
require.Equal(t, _nef1, cs[1].NEF) | ||
require.Equal(t, _manifest1, cs[1].Manifest) | ||
require.Equal(t, _nef11, cs[2].NEF) | ||
require.Equal(t, _manifest11, cs[2].Manifest) | ||
} | ||
|
||
func TestReadInvalidFilenames(t *testing.T) { | ||
_fs := fstest.MapFS{} | ||
|
||
_, err := read(_fs) | ||
require.NoError(t, err) | ||
|
||
for _, invalidName := range []string{ | ||
"hello.nef", | ||
"-1.nef", | ||
"0-1.nef", | ||
".nef", | ||
} { | ||
_fs[invalidName] = &fstest.MapFile{} | ||
_, err = read(_fs) | ||
require.ErrorIs(t, err, errInvalidFilename, invalidName) | ||
delete(_fs, invalidName) | ||
} | ||
} | ||
|
||
func TestReadDuplicatedContract(t *testing.T) { | ||
_, bNEF := anyValidNEF(t) | ||
_, jManifest := anyValidManifest(t, "some name") | ||
|
||
_fs := fstest.MapFS{ | ||
"01.nef": {Data: bNEF}, | ||
"01.manifest.json": {Data: jManifest}, | ||
"001.nef": {Data: bNEF}, | ||
"001.manifest.json": {Data: jManifest}, | ||
} | ||
|
||
_, err := read(_fs) | ||
require.ErrorIs(t, err, errDuplicatedContract) | ||
} | ||
|
||
func TestReadInvalidFormat(t *testing.T) { | ||
_fs := fstest.MapFS{} | ||
|
||
_, validNEF := anyValidNEF(t) | ||
_, validManifest := anyValidManifest(t, "zero") | ||
|
||
_fs["00.nef"] = &fstest.MapFile{Data: validNEF} | ||
_fs["00.manifest.json"] = &fstest.MapFile{Data: validManifest} | ||
|
||
_, err := read(_fs) | ||
require.NoError(t, err, errInvalidNEF) | ||
|
||
_fs["00.nef"] = &fstest.MapFile{Data: []byte("not a NEF")} | ||
_fs["00.manifest.json"] = &fstest.MapFile{Data: validManifest} | ||
|
||
_, err = read(_fs) | ||
require.ErrorIs(t, err, errInvalidNEF) | ||
|
||
_fs["00.nef"] = &fstest.MapFile{Data: validNEF} | ||
_fs["00.manifest.json"] = &fstest.MapFile{Data: []byte("not a manifest")} | ||
|
||
_, err = read(_fs) | ||
require.ErrorIs(t, err, errInvalidManifest) | ||
} | ||
|
||
func anyValidNEF(tb testing.TB) (nef.File, []byte) { | ||
script := make([]byte, 32) | ||
rand.Read(script) | ||
|
||
_nef, err := nef.NewFile(script) | ||
require.NoError(tb, err) | ||
|
||
bNEF, err := _nef.Bytes() | ||
require.NoError(tb, err) | ||
|
||
return *_nef, bNEF | ||
} | ||
|
||
func anyValidManifest(tb testing.TB, name string) (manifest.Manifest, []byte) { | ||
_manifest := manifest.NewManifest(name) | ||
|
||
jManifest, err := json.Marshal(_manifest) | ||
require.NoError(tb, err) | ||
|
||
return *_manifest, jManifest | ||
} |