compiler: check supported standards

Check that emitted manifest complies with supported standards.
This can be made a separate flag.
This commit is contained in:
Evgenii Stratonikov 2020-11-24 13:38:24 +03:00
parent 279b769fa3
commit 25f1db6de0
7 changed files with 90 additions and 14 deletions

View file

@ -104,3 +104,54 @@ func TestComlileAndInvokeFunction(t *testing.T) {
require.Equal(t, []byte("on update|sub update"), res.Stack[0].Value()) require.Equal(t, []byte("on update|sub update"), res.Stack[0].Value())
}) })
} }
func TestCompileExamples(t *testing.T) {
const examplePath = "../examples"
infos, err := ioutil.ReadDir(examplePath)
require.NoError(t, err)
// For proper nef generation.
config.Version = "0.90.0-test"
tmpDir := os.TempDir()
e := newExecutor(t, false)
defer e.Close(t)
for _, info := range infos {
t.Run(info.Name(), func(t *testing.T) {
infos, err := ioutil.ReadDir(path.Join(examplePath, info.Name()))
require.NoError(t, err)
require.False(t, len(infos) == 0, "detected smart contract folder with no contract in it")
outPath := path.Join(tmpDir, info.Name()+".nef")
manifestPath := path.Join(tmpDir, info.Name()+".manifest.json")
defer func() {
os.Remove(outPath)
os.Remove(manifestPath)
}()
cfgName := filterFilename(infos, ".yml")
opts := []string{
"neo-go", "contract", "compile",
"--in", path.Join(examplePath, info.Name()),
"--out", outPath,
"--manifest", manifestPath,
"--config", path.Join(examplePath, info.Name(), cfgName),
}
e.Run(t, opts...)
})
}
}
func filterFilename(infos []os.FileInfo, ext string) string {
for _, info := range infos {
if !info.IsDir() {
name := info.Name()
if strings.HasSuffix(name, ext) {
return name
}
}
}
return ""
}

View file

@ -150,6 +150,10 @@ func NewCommands() []cli.Command {
Name: "config, c", Name: "config, c",
Usage: "Configuration input file (*.yml)", Usage: "Configuration input file (*.yml)",
}, },
cli.BoolFlag{
Name: "no-standards",
Usage: "do not check compliance with supported standards",
},
}, },
}, },
{ {
@ -398,6 +402,8 @@ func contractCompile(ctx *cli.Context) error {
DebugInfo: debugFile, DebugInfo: debugFile,
ManifestFile: manifestFile, ManifestFile: manifestFile,
NoStandardCheck: ctx.Bool("no-standards"),
} }
if len(confFile) != 0 { if len(confFile) != 0 {

View file

@ -1,6 +1,7 @@
package tokensale package tokensale
import ( import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime" "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/storage" "github.com/nspcc-dev/neo-go/pkg/interop/storage"
"github.com/nspcc-dev/neo-go/pkg/interop/util" "github.com/nspcc-dev/neo-go/pkg/interop/util"
@ -134,39 +135,39 @@ func checkOwnerWitness() bool {
} }
// Decimals returns the token decimals // Decimals returns the token decimals
func Decimals() interface{} { func Decimals() int {
if trigger != runtime.Application { if trigger != runtime.Application {
return false panic("invalid trigger")
} }
return token.Decimals return token.Decimals
} }
// Symbol returns the token symbol // Symbol returns the token symbol
func Symbol() interface{} { func Symbol() string {
if trigger != runtime.Application { if trigger != runtime.Application {
return false panic("invalid trigger")
} }
return token.Symbol return token.Symbol
} }
// TotalSupply returns the token total supply value // TotalSupply returns the token total supply value
func TotalSupply() interface{} { func TotalSupply() int {
if trigger != runtime.Application { if trigger != runtime.Application {
return false panic("invalid trigger")
} }
return getIntFromDB(ctx, token.CirculationKey) return getIntFromDB(ctx, token.CirculationKey)
} }
// BalanceOf returns the amount of token on the specified address // BalanceOf returns the amount of token on the specified address
func BalanceOf(holder []byte) interface{} { func BalanceOf(holder interop.Hash160) int {
if trigger != runtime.Application { if trigger != runtime.Application {
return false panic("invalid trigger")
} }
return getIntFromDB(ctx, holder) return getIntFromDB(ctx, holder)
} }
// Transfer transfers specified amount of token from one user to another // Transfer transfers specified amount of token from one user to another
func Transfer(from, to []byte, amount int) bool { func Transfer(from, to interop.Hash160, amount int, _ interface{}) bool {
if trigger != runtime.Application { if trigger != runtime.Application {
return false return false
} }

View file

@ -1,3 +1,11 @@
name: "My awesome token" name: "My awesome token"
supportedstandards: ["NEP-17"] supportedstandards: ["NEP-17"]
events: [] events:
- name: Transfer
parameters:
- name: from
type: Hash160
- name: to
type: Hash160
- name: amount
type: Integer

View file

@ -48,7 +48,7 @@ func TotalSupply() int {
} }
// BalanceOf returns the amount of token on the specified address // BalanceOf returns the amount of token on the specified address
func BalanceOf(holder interop.Hash160) interface{} { func BalanceOf(holder interop.Hash160) int {
return token.BalanceOf(ctx, holder) return token.BalanceOf(ctx, holder)
} }
@ -58,6 +58,6 @@ func Transfer(from interop.Hash160, to interop.Hash160, amount int, data interfa
} }
// Mint initial supply of tokens // Mint initial supply of tokens
func Mint(to []byte) bool { func Mint(to interop.Hash160) bool {
return token.Mint(ctx, to) return token.Mint(ctx, to)
} }

View file

@ -4,8 +4,8 @@ events:
- name: Transfer - name: Transfer
parameters: parameters:
- name: from - name: from
type: ByteString type: Hash160
- name: to - name: to
type: ByteString type: Hash160
- name: amount - name: amount
type: Integer type: Integer

View file

@ -14,6 +14,7 @@ import (
"strings" "strings"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest/standard"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef" "github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
"golang.org/x/tools/go/loader" "golang.org/x/tools/go/loader"
) )
@ -34,6 +35,10 @@ type Options struct {
// The name of the output for contract manifest file. // The name of the output for contract manifest file.
ManifestFile string ManifestFile string
// NoStandardCheck specifies if supported standards compliance needs to be checked.
// This setting has effect only if manifest is emitted.
NoStandardCheck bool
// Name is contract's name to be written to manifest. // Name is contract's name to be written to manifest.
Name string Name string
@ -214,6 +219,11 @@ func CompileAndSave(src string, o *Options) ([]byte, error) {
if err != nil { if err != nil {
return b, fmt.Errorf("failed to convert debug info to manifest: %w", err) return b, fmt.Errorf("failed to convert debug info to manifest: %w", err)
} }
if !o.NoStandardCheck {
if err := standard.Check(m, o.ContractSupportedStandards...); err != nil {
return b, err
}
}
mData, err := json.Marshal(m) mData, err := json.Marshal(m)
if err != nil { if err != nil {
return b, fmt.Errorf("failed to marshal manifest to JSON: %w", err) return b, fmt.Errorf("failed to marshal manifest to JSON: %w", err)