From db820cb0dc50c530df413b6a263fad54f6f89194 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Mon, 26 Aug 2024 19:50:50 +0300 Subject: [PATCH] manifest: rework method duplicate check Don't use an additional type, it's inconvenient and this method is not performance-critical. Signed-off-by: Roman Khimov --- pkg/smartcontract/manifest/abi.go | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/pkg/smartcontract/manifest/abi.go b/pkg/smartcontract/manifest/abi.go index 3027e5c97..15211919e 100644 --- a/pkg/smartcontract/manifest/abi.go +++ b/pkg/smartcontract/manifest/abi.go @@ -1,9 +1,10 @@ package manifest import ( + "cmp" "errors" "fmt" - "sort" + "slices" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" ) @@ -63,29 +64,19 @@ func (a *ABI) IsValid() error { } } if len(a.Methods) > 1 { - methods := make([]struct { - name string - params int - }, len(a.Methods)) - for i := range methods { - methods[i].name = a.Methods[i].Name - methods[i].params = len(a.Methods[i].Parameters) - } - sort.Slice(methods, func(i, j int) bool { - if methods[i].name < methods[j].name { - return true - } - if methods[i].name == methods[j].name { - return methods[i].params < methods[j].params - } - return false + var methods = slices.Clone(a.Methods) + slices.SortFunc(methods, func(a, b Method) int { + return cmp.Or( + cmp.Compare(a.Name, b.Name), + cmp.Compare(len(a.Parameters), len(b.Parameters)), + ) }) for i := range methods { if i == 0 { continue } - if methods[i].name == methods[i-1].name && - methods[i].params == methods[i-1].params { + if methods[i].Name == methods[i-1].Name && + len(methods[i].Parameters) == len(methods[i-1].Parameters) { return errors.New("duplicate method specifications") } }