From 0f1473897bc90b46d7f1f427c4588bfbbdd3d331 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 12 Feb 2021 22:32:34 +0300 Subject: [PATCH] core: temp manifest.Extra marshalling fix Manifest.Extra still serialized as JSON message with undefined order of items, so it affects contract states dumps. --- pkg/compiler/debug_test.go | 3 ++- pkg/core/native_management_test.go | 4 ++-- pkg/smartcontract/manifest/manifest.go | 15 +++++---------- pkg/smartcontract/manifest/manifest_test.go | 10 +--------- 4 files changed, 10 insertions(+), 22 deletions(-) diff --git a/pkg/compiler/debug_test.go b/pkg/compiler/debug_test.go index 5e1c0346d..08d5da8f9 100644 --- a/pkg/compiler/debug_test.go +++ b/pkg/compiler/debug_test.go @@ -1,6 +1,7 @@ package compiler import ( + "encoding/json" "testing" "github.com/nspcc-dev/neo-go/internal/testserdes" @@ -265,7 +266,7 @@ func _deploy(data interface{}, isUpdate bool) {} Trusts: manifest.WildUint160s{ Value: []util.Uint160{}, }, - Extra: nil, + Extra: json.RawMessage("null"), } require.ElementsMatch(t, expected.ABI.Methods, actual.ABI.Methods) require.Equal(t, expected.ABI.Events, actual.ABI.Events) diff --git a/pkg/core/native_management_test.go b/pkg/core/native_management_test.go index b0ba0082a..1445fd901 100644 --- a/pkg/core/native_management_test.go +++ b/pkg/core/native_management_test.go @@ -481,7 +481,7 @@ func TestContractUpdate(t *testing.T) { }) }) - cs1.Manifest.Extra = "update me" + cs1.Manifest.Extra = []byte(`"update me"`) manif1, err = json.Marshal(cs1.Manifest) require.NoError(t, err) cs1.UpdateCounter++ @@ -504,7 +504,7 @@ func TestContractUpdate(t *testing.T) { cs1.NEF.Checksum = cs1.NEF.CalculateChecksum() nef1b, err = cs1.NEF.Bytes() require.NoError(t, err) - cs1.Manifest.Extra = "update me once more" + cs1.Manifest.Extra = []byte(`"update me once more"`) manif1, err = json.Marshal(cs1.Manifest) require.NoError(t, err) cs1.UpdateCounter++ diff --git a/pkg/smartcontract/manifest/manifest.go b/pkg/smartcontract/manifest/manifest.go index c7bbd0fe7..31a3011a4 100644 --- a/pkg/smartcontract/manifest/manifest.go +++ b/pkg/smartcontract/manifest/manifest.go @@ -34,7 +34,7 @@ type Manifest struct { // Trusts is a set of hashes to a which contract trusts. Trusts WildUint160s `json:"trusts"` // Extra is an implementation-defined user data. - Extra interface{} `json:"extra"` + Extra json.RawMessage `json:"extra"` } // NewManifest returns new manifest with necessary fields initialized. @@ -48,6 +48,7 @@ func NewManifest(name string) *Manifest { Groups: []Group{}, Permissions: []Permission{}, SupportedStandards: []string{}, + Extra: json.RawMessage("null"), } m.Trusts.Restrict() return m @@ -143,11 +144,7 @@ func (m *Manifest) ToStackItem() (stackitem.Item, error) { } extra := stackitem.Make("null") if m.Extra != nil { - e, err := json.Marshal(m.Extra) - if err != nil { - return nil, err - } - extra = stackitem.NewByteArray(e) + extra = stackitem.NewByteArray(m.Extra) } return stackitem.NewStruct([]stackitem.Item{ stackitem.Make(m.Name), @@ -238,8 +235,6 @@ func (m *Manifest) FromStackItem(item stackitem.Item) error { if err != nil { return err } - if string(extra) == "null" { - return nil - } - return json.Unmarshal(extra, &m.Extra) + m.Extra = extra + return nil } diff --git a/pkg/smartcontract/manifest/manifest_test.go b/pkg/smartcontract/manifest/manifest_test.go index 60a917252..4532dcbba 100644 --- a/pkg/smartcontract/manifest/manifest_test.go +++ b/pkg/smartcontract/manifest/manifest_test.go @@ -278,7 +278,7 @@ func TestManifestToStackItem(t *testing.T) { Trusts: WildUint160s{ Value: []util.Uint160{{1, 2, 3}}, }, - Extra: "some extra data", + Extra: []byte(`even not a json allowed`), } check(t, expected) }) @@ -335,14 +335,6 @@ func TestManifest_FromStackItemErrors(t *testing.T) { stackitem.NewArray([]stackitem.Item{}), stackitem.NewArray([]stackitem.Item{}), stackitem.Null{}}), - "invalid extra": stackitem.NewStruct([]stackitem.Item{ - stackitem.NewByteArray([]byte{}), - stackitem.NewArray([]stackitem.Item{}), - stackitem.NewArray([]stackitem.Item{}), - stackitem.NewStruct([]stackitem.Item{stackitem.NewArray([]stackitem.Item{}), stackitem.NewArray([]stackitem.Item{})}), - stackitem.NewArray([]stackitem.Item{}), - stackitem.NewArray([]stackitem.Item{}), - stackitem.NewByteArray([]byte("not a json"))}), } for name, errCase := range errCases { t.Run(name, func(t *testing.T) {