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() script := io.NewBufBinWriter()
m, err := di.ConvertToManifest(nil) m, err := di.ConvertToManifest(nil)
require.NoError(t, err) require.NoError(t, err)
bs, err := m.MarshalJSON() bs, err := json.Marshal(m)
require.NoError(t, err) require.NoError(t, err)
emit.Bytes(script.BinWriter, bs) emit.Bytes(script.BinWriter, bs)
emit.Bytes(script.BinWriter, avm) emit.Bytes(script.BinWriter, avm)

View file

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

View file

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

View file

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

View file

@ -1,6 +1,7 @@
package request package request
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"strconv" "strconv"
@ -19,7 +20,7 @@ import (
// with its metadata. // with its metadata.
func CreateDeploymentScript(avm []byte, manif *manifest.Manifest) ([]byte, error) { func CreateDeploymentScript(avm []byte, manif *manifest.Manifest) ([]byte, error) {
script := io.NewBufBinWriter() script := io.NewBufBinWriter()
rawManifest, err := manif.MarshalJSON() rawManifest, err := json.Marshal(manif)
if err != nil { if err != nil {
return nil, err 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) } func (c *WildUint160s) Add(v util.Uint160) { c.Value = append(c.Value, v) }
// MarshalJSON implements json.Marshaler interface. // MarshalJSON implements json.Marshaler interface.
func (c *WildStrings) MarshalJSON() ([]byte, error) { func (c WildStrings) MarshalJSON() ([]byte, error) {
if c.IsWildcard() { if c.IsWildcard() {
return []byte(`"*"`), nil return []byte(`"*"`), nil
} }
@ -74,7 +74,7 @@ func (c *WildStrings) MarshalJSON() ([]byte, error) {
} }
// MarshalJSON implements json.Marshaler interface. // MarshalJSON implements json.Marshaler interface.
func (c *WildUint160s) MarshalJSON() ([]byte, error) { func (c WildUint160s) MarshalJSON() ([]byte, error) {
if c.IsWildcard() { if c.IsWildcard() {
return []byte(`"*"`), nil return []byte(`"*"`), nil
} }

View file

@ -36,27 +36,17 @@ type ABI struct {
// Manifest represens contract metadata. // Manifest represens contract metadata.
type Manifest struct { type Manifest struct {
// ABI is a contract's ABI. // ABI is a contract's ABI.
ABI ABI ABI ABI `json:"abi"`
// Groups is a set of groups to which a contract belongs. // Groups is a set of groups to which a contract belongs.
Groups []Group
Permissions []Permission
// SupportedStandards is a list of standards supported by the contract.
SupportedStandards []string
// Trusts is a set of hashes to a which contract trusts.
Trusts WildUint160s
// SafeMethods is a set of names of safe methods.
SafeMethods WildStrings
// Extra is an implementation-defined user data.
Extra interface{}
}
type manifestAux struct {
ABI *ABI `json:"abi"`
Groups []Group `json:"groups"` Groups []Group `json:"groups"`
Permissions []Permission `json:"permissions"` Permissions []Permission `json:"permissions"`
// SupportedStandards is a list of standards supported by the contract.
SupportedStandards []string `json:"supportedstandards"` SupportedStandards []string `json:"supportedstandards"`
Trusts *WildUint160s `json:"trusts"` // Trusts is a set of hashes to a which contract trusts.
SafeMethods *WildStrings `json:"safemethods"` Trusts WildUint160s `json:"trusts"`
// SafeMethods is a set of names of safe methods.
SafeMethods WildStrings `json:"safemethods"`
// Extra is an implementation-defined user data.
Extra interface{} `json:"extra"` Extra interface{} `json:"extra"`
} }
@ -122,40 +112,6 @@ func (m *Manifest) IsValid(hash util.Uint160) bool {
return true 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. // EncodeBinary implements io.Serializable.
func (m *Manifest) EncodeBinary(w *io.BinWriter) { func (m *Manifest) EncodeBinary(w *io.BinWriter) {
data, err := json.Marshal(m) data, err := json.Marshal(m)