From 0c01d89827c3a3abf0c0c8225693c778af45cad3 Mon Sep 17 00:00:00 2001 From: Evgeniy Stratonikov Date: Wed, 6 Oct 2021 15:18:57 +0300 Subject: [PATCH] compiler: check that safe methods exist If a method is missing from the manifest, it is most likely a typo or regression after refactoring. There is no "turn-off" flag for this error because we can do this precisely. Signed-off-by: Evgeniy Stratonikov --- pkg/compiler/compiler.go | 5 +++++ pkg/compiler/compiler_test.go | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/pkg/compiler/compiler.go b/pkg/compiler/compiler.go index 8933605ac..ba9dc0ee4 100644 --- a/pkg/compiler/compiler.go +++ b/pkg/compiler/compiler.go @@ -270,6 +270,11 @@ func CreateManifest(di *DebugInfo, o *Options) (*manifest.Manifest, error) { if err != nil { return m, fmt.Errorf("failed to convert debug info to manifest: %w", err) } + for _, name := range o.SafeMethods { + if m.ABI.GetMethod(name, -1) == nil { + return m, fmt.Errorf("method %s is marked as safe but missing from manifest", name) + } + } if !o.NoStandardCheck { if err := standard.CheckABI(m, o.ContractSupportedStandards...); err != nil { return m, err diff --git a/pkg/compiler/compiler_test.go b/pkg/compiler/compiler_test.go index 62c5f2b87..b2f00490e 100644 --- a/pkg/compiler/compiler_test.go +++ b/pkg/compiler/compiler_test.go @@ -125,6 +125,20 @@ func TestOnPayableChecks(t *testing.T) { }) } +func TestSafeMethodWarnings(t *testing.T) { + src := `package payable + func Main() int { return 1 }` + + _, di, err := compiler.CompileWithDebugInfo("eventTest", strings.NewReader(src)) + require.NoError(t, err) + + _, err = compiler.CreateManifest(di, &compiler.Options{SafeMethods: []string{"main"}}) + require.NoError(t, err) + + _, err = compiler.CreateManifest(di, &compiler.Options{SafeMethods: []string{"main", "mississippi"}}) + require.Error(t, err) +} + func TestEventWarnings(t *testing.T) { src := `package payable import "github.com/nspcc-dev/neo-go/pkg/interop/runtime"