From 61fd7bd6ba06d4831cea63ed0adaa124840c1821 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Thu, 9 Sep 2021 17:03:11 +0300 Subject: [PATCH] core: avoid `nil` values during natives manifest marshalling --- pkg/core/interop/context.go | 1 + pkg/core/native/native_nep17.go | 3 +++ pkg/smartcontract/nef/nef_test.go | 8 ++++++++ 3 files changed, 12 insertions(+) diff --git a/pkg/core/interop/context.go b/pkg/core/interop/context.go index 7c9ab541d..908b8a6ac 100644 --- a/pkg/core/interop/context.go +++ b/pkg/core/interop/context.go @@ -140,6 +140,7 @@ func NewContractMD(name string, id int32) *ContractMD { // Therefore values are taken from C# node. c.NEF.Header.Compiler = "neo-core-v3.0" c.NEF.Header.Magic = nef.Magic + c.NEF.Tokens = []nef.MethodToken{} // avoid `nil` result during JSON marshalling c.Hash = state.CreateContractHash(util.Uint160{}, 0, c.Name) c.Manifest = *manifest.DefaultManifest(name) diff --git a/pkg/core/native/native_nep17.go b/pkg/core/native/native_nep17.go index 3b2695fa8..4cc48db3a 100644 --- a/pkg/core/native/native_nep17.go +++ b/pkg/core/native/native_nep17.go @@ -294,6 +294,9 @@ func (c *nep17TokenNative) addTokens(ic *interop.Context, h util.Uint160, amount } func newDescriptor(name string, ret smartcontract.ParamType, ps ...manifest.Parameter) *manifest.Method { + if len(ps) == 0 { + ps = []manifest.Parameter{} + } return &manifest.Method{ Name: name, Parameters: ps, diff --git a/pkg/smartcontract/nef/nef_test.go b/pkg/smartcontract/nef/nef_test.go index 03d3af86c..9bd8873e1 100644 --- a/pkg/smartcontract/nef/nef_test.go +++ b/pkg/smartcontract/nef/nef_test.go @@ -69,6 +69,14 @@ func TestEncodeDecodeBinary(t *testing.T) { expected.Header.Magic = Magic testserdes.EncodeDecodeBinary(t, expected, &File{}) }) + t.Run("positive with empty tokens", func(t *testing.T) { + expected.Script = script + // Check `Tokens` won't be deserialized to `nil`. We expect them to be non-nil slice in the related code. + expected.Tokens = []MethodToken{} + expected.Checksum = expected.CalculateChecksum() + expected.Header.Magic = Magic + testserdes.EncodeDecodeBinary(t, expected, &File{}) + }) t.Run("invalid reserved bytes", func(t *testing.T) { expected.Script = script expected.Tokens = expected.Tokens[:0]