smartcontract: add checks for onNEP*Payable methods

When they are present in contract, we want them
to have correct signature.
This commit is contained in:
Evgeniy Stratonikov 2021-02-19 13:48:45 +03:00
parent d89f055697
commit b38443fe20
5 changed files with 127 additions and 29 deletions

View file

@ -222,13 +222,39 @@ func CompileAndSave(src string, o *Options) ([]byte, error) {
}
if o.ManifestFile != "" {
m, err := CreateManifest(di, o)
if err != nil {
return b, err
}
mData, err := json.Marshal(m)
if err != nil {
return b, fmt.Errorf("failed to marshal manifest to JSON: %w", err)
}
return b, ioutil.WriteFile(o.ManifestFile, mData, os.ModePerm)
}
return b, nil
}
// CreateManifest creates manifest and checks that is is valid.
func CreateManifest(di *DebugInfo, o *Options) (*manifest.Manifest, error) {
m, err := di.ConvertToManifest(o)
if err != nil {
return b, fmt.Errorf("failed to convert debug info to manifest: %w", err)
return m, fmt.Errorf("failed to convert debug info to manifest: %w", err)
}
if !o.NoStandardCheck {
if err := standard.CheckABI(m, o.ContractSupportedStandards...); err != nil {
return b, err
return m, err
}
if m.ABI.GetMethod(manifest.MethodOnNEP11Payment, -1) != nil {
if err := standard.CheckABI(m, manifest.NEP11Payable); err != nil {
return m, err
}
}
if m.ABI.GetMethod(manifest.MethodOnNEP17Payment, -1) != nil {
if err := standard.CheckABI(m, manifest.NEP17Payable); err != nil {
return m, err
}
}
}
if !o.NoEventsCheck {
@ -253,12 +279,5 @@ func CompileAndSave(src string, o *Options) ([]byte, error) {
}
}
}
mData, err := json.Marshal(m)
if err != nil {
return b, fmt.Errorf("failed to marshal manifest to JSON: %w", err)
}
return b, ioutil.WriteFile(o.ManifestFile, mData, os.ModePerm)
}
return b, nil
return m, nil
}

View file

@ -4,6 +4,7 @@ import (
"io/ioutil"
"os"
"path"
"strings"
"testing"
"github.com/nspcc-dev/neo-go/pkg/compiler"
@ -90,3 +91,37 @@ func compileFile(src string) error {
_, err := compiler.Compile(src, nil)
return err
}
func TestOnPayableChecks(t *testing.T) {
compileAndCheck := func(t *testing.T, src string) error {
_, di, err := compiler.CompileWithDebugInfo("payable", strings.NewReader(src))
require.NoError(t, err)
_, err = compiler.CreateManifest(di, &compiler.Options{})
return err
}
t.Run("NEP-11, good", func(t *testing.T) {
src := `package payable
import "github.com/nspcc-dev/neo-go/pkg/interop"
func OnNEP11Payment(from interop.Hash160, amount int, tokenID []byte) {}`
require.NoError(t, compileAndCheck(t, src))
})
t.Run("NEP-11, bad", func(t *testing.T) {
src := `package payable
import "github.com/nspcc-dev/neo-go/pkg/interop"
func OnNEP11Payment(from interop.Hash160, amount int, oldParam string, tokenID []byte) {}`
require.Error(t, compileAndCheck(t, src))
})
t.Run("NEP-17, good", func(t *testing.T) {
src := `package payable
import "github.com/nspcc-dev/neo-go/pkg/interop"
func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) {}`
require.NoError(t, compileAndCheck(t, src))
})
t.Run("NEP-17, bad", func(t *testing.T) {
src := `package payable
import "github.com/nspcc-dev/neo-go/pkg/interop"
func OnNEP17Payment(from interop.Hash160, amount int, data interface{}, extra int) {}`
require.Error(t, compileAndCheck(t, src))
})
}

View file

@ -20,6 +20,10 @@ const (
NEP11StandardName = "NEP-11"
// NEP17StandardName represents the name of NEP17 smartcontract standard.
NEP17StandardName = "NEP-17"
// NEP11Payable represents the name of contract interface which can receive NEP-11 tokens.
NEP11Payable = "NEP-11-Payable"
// NEP17Payable represents the name of contract interface which can receive NEP-17 tokens.
NEP17Payable = "NEP-17-Payable"
)
// Manifest represens contract metadata.

View file

@ -21,6 +21,8 @@ var (
var checks = map[string][]*Standard{
manifest.NEP11StandardName: {nep11NonDivisible, nep11Divisible},
manifest.NEP17StandardName: {nep17},
manifest.NEP11Payable: {nep11payable},
manifest.NEP17Payable: {nep17payable},
}
// Check checks if manifest complies with all provided standards.

View file

@ -0,0 +1,38 @@
package standard
import (
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
)
var nep11payable = &Standard{
Manifest: manifest.Manifest{
ABI: manifest.ABI{
Methods: []manifest.Method{{
Name: manifest.MethodOnNEP11Payment,
Parameters: []manifest.Parameter{
{Name: "from", Type: smartcontract.Hash160Type},
{Name: "amount", Type: smartcontract.IntegerType},
{Name: "tokenid", Type: smartcontract.ByteArrayType},
},
ReturnType: smartcontract.VoidType,
}},
},
},
}
var nep17payable = &Standard{
Manifest: manifest.Manifest{
ABI: manifest.ABI{
Methods: []manifest.Method{{
Name: manifest.MethodOnNEP17Payment,
Parameters: []manifest.Parameter{
{Name: "from", Type: smartcontract.Hash160Type},
{Name: "amount", Type: smartcontract.IntegerType},
{Name: "data", Type: smartcontract.AnyType},
},
ReturnType: smartcontract.VoidType,
}},
},
},
}