manifest: simplify marshaling

This commit is contained in:
Roman Khimov 2020-11-13 21:46:26 +03:00
parent 286d9185f4
commit 3cb945f022
7 changed files with 24 additions and 64 deletions

View file

@ -379,7 +379,7 @@ func newDeployTx(t *testing.T, name string) (*transaction.Transaction, []byte) {
script := io.NewBufBinWriter()
m, err := di.ConvertToManifest(nil)
require.NoError(t, err)
bs, err := m.MarshalJSON()
bs, err := json.Marshal(m)
require.NoError(t, err)
emit.Bytes(script.BinWriter, bs)
emit.Bytes(script.BinWriter, avm)

View file

@ -3,6 +3,7 @@ package core
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"sort"
@ -75,7 +76,7 @@ func createContractStateFromVM(ic *interop.Context) (*state.Contract, error) {
return nil, errGasLimitExceeded
}
var m manifest.Manifest
err := m.UnmarshalJSON(manifestBytes)
err := json.Unmarshal(manifestBytes, &m)
if err != nil {
return nil, fmt.Errorf("unable to retrieve manifest from stack: %w", err)
}
@ -173,7 +174,7 @@ func contractUpdate(ic *interop.Context) error {
// storage items if needed
if manifestBytes != nil {
var newManifest manifest.Manifest
err := newManifest.UnmarshalJSON(manifestBytes)
err := json.Unmarshal(manifestBytes, &newManifest)
if err != nil {
return fmt.Errorf("unable to retrieve manifest from stack: %w", err)
}

View file

@ -2,6 +2,7 @@ package core
import (
"crypto/elliptic"
"encoding/json"
"errors"
"fmt"
"math"
@ -99,7 +100,7 @@ func bcGetBlock(ic *interop.Context) error {
// contractToStackItem converts state.Contract to stackitem.Item
func contractToStackItem(cs *state.Contract) (stackitem.Item, error) {
manifest, err := cs.Manifest.MarshalJSON()
manifest, err := json.Marshal(cs.Manifest)
if err != nil {
return nil, err
}

View file

@ -1,6 +1,7 @@
package core
import (
"encoding/json"
"errors"
"math/big"
"testing"
@ -637,7 +638,7 @@ func TestContractCreate(t *testing.T) {
defer bc.Close()
putArgsOnStack := func() {
manifest, err := cs.Manifest.MarshalJSON()
manifest, err := json.Marshal(cs.Manifest)
require.NoError(t, err)
v.Estack().PushVal(manifest)
v.Estack().PushVal(cs.Script)
@ -671,7 +672,7 @@ func compareContractStates(t *testing.T, expected *state.Contract, actual stacki
act, ok := actual.Value().([]stackitem.Item)
require.True(t, ok)
expectedManifest, err := expected.Manifest.MarshalJSON()
expectedManifest, err := json.Marshal(expected.Manifest)
require.NoError(t, err)
require.Equal(t, 2, len(act))
@ -799,7 +800,7 @@ func TestContractUpdate(t *testing.T) {
Hash: util.Uint160{4, 5, 6},
},
}
manifestBytes, err := manifest.MarshalJSON()
manifestBytes, err := json.Marshal(manifest)
require.NoError(t, err)
putArgsOnStack(stackitem.Null{}, manifestBytes)
@ -813,7 +814,7 @@ func TestContractUpdate(t *testing.T) {
Hash: cs.ScriptHash(),
},
}
manifestBytes, err := manifest.MarshalJSON()
manifestBytes, err := json.Marshal(manifest)
require.NoError(t, err)
t.Run("empty script", func(t *testing.T) {
@ -847,7 +848,7 @@ func TestContractUpdate(t *testing.T) {
Hash: hash.Hash160(newScript),
},
}
newManifestBytes, err := newManifest.MarshalJSON()
newManifestBytes, err := json.Marshal(newManifest)
require.NoError(t, err)
putArgsOnStack(newScript, newManifestBytes)
@ -880,7 +881,7 @@ func TestContractCreateDeploy(t *testing.T) {
v.GasLimit = -1
putArgs := func(cs *state.Contract) {
rawManifest, err := cs.Manifest.MarshalJSON()
rawManifest, err := json.Marshal(cs.Manifest)
require.NoError(t, err)
v.Estack().PushVal(rawManifest)
v.Estack().PushVal(cs.Script)

View file

@ -1,6 +1,7 @@
package request
import (
"encoding/json"
"errors"
"fmt"
"strconv"
@ -19,7 +20,7 @@ import (
// with its metadata.
func CreateDeploymentScript(avm []byte, manif *manifest.Manifest) ([]byte, error) {
script := io.NewBufBinWriter()
rawManifest, err := manif.MarshalJSON()
rawManifest, err := json.Marshal(manif)
if err != nil {
return nil, err
}

View file

@ -66,7 +66,7 @@ func (c *WildStrings) Add(v string) { c.Value = append(c.Value, v) }
func (c *WildUint160s) Add(v util.Uint160) { c.Value = append(c.Value, v) }
// MarshalJSON implements json.Marshaler interface.
func (c *WildStrings) MarshalJSON() ([]byte, error) {
func (c WildStrings) MarshalJSON() ([]byte, error) {
if c.IsWildcard() {
return []byte(`"*"`), nil
}
@ -74,7 +74,7 @@ func (c *WildStrings) MarshalJSON() ([]byte, error) {
}
// MarshalJSON implements json.Marshaler interface.
func (c *WildUint160s) MarshalJSON() ([]byte, error) {
func (c WildUint160s) MarshalJSON() ([]byte, error) {
if c.IsWildcard() {
return []byte(`"*"`), nil
}

View file

@ -36,28 +36,18 @@ type ABI struct {
// Manifest represens contract metadata.
type Manifest struct {
// ABI is a contract's ABI.
ABI ABI
ABI ABI `json:"abi"`
// Groups is a set of groups to which a contract belongs.
Groups []Group
Permissions []Permission
Groups []Group `json:"groups"`
Permissions []Permission `json:"permissions"`
// SupportedStandards is a list of standards supported by the contract.
SupportedStandards []string
SupportedStandards []string `json:"supportedstandards"`
// Trusts is a set of hashes to a which contract trusts.
Trusts WildUint160s
Trusts WildUint160s `json:"trusts"`
// SafeMethods is a set of names of safe methods.
SafeMethods WildStrings
SafeMethods WildStrings `json:"safemethods"`
// Extra is an implementation-defined user data.
Extra interface{}
}
type manifestAux struct {
ABI *ABI `json:"abi"`
Groups []Group `json:"groups"`
Permissions []Permission `json:"permissions"`
SupportedStandards []string `json:"supportedstandards"`
Trusts *WildUint160s `json:"trusts"`
SafeMethods *WildStrings `json:"safemethods"`
Extra interface{} `json:"extra"`
Extra interface{} `json:"extra"`
}
// NewManifest returns new manifest with necessary fields initialized.
@ -122,40 +112,6 @@ func (m *Manifest) IsValid(hash util.Uint160) bool {
return true
}
// MarshalJSON implements json.Marshaler interface.
func (m *Manifest) MarshalJSON() ([]byte, error) {
aux := &manifestAux{
ABI: &m.ABI,
Groups: m.Groups,
Permissions: m.Permissions,
SupportedStandards: m.SupportedStandards,
Trusts: &m.Trusts,
SafeMethods: &m.SafeMethods,
Extra: m.Extra,
}
return json.Marshal(aux)
}
// UnmarshalJSON implements json.Unmarshaler interface.
func (m *Manifest) UnmarshalJSON(data []byte) error {
aux := &manifestAux{
ABI: &m.ABI,
Trusts: &m.Trusts,
SafeMethods: &m.SafeMethods,
}
if err := json.Unmarshal(data, aux); err != nil {
return err
}
m.Groups = aux.Groups
m.Permissions = aux.Permissions
m.SupportedStandards = aux.SupportedStandards
m.Extra = aux.Extra
return nil
}
// EncodeBinary implements io.Serializable.
func (m *Manifest) EncodeBinary(w *io.BinWriter) {
data, err := json.Marshal(m)