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

@ -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))
})
}