mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-02-18 11:15:36 +00:00
native: reuse stackitem.(De)Serialize more for data structures
Less code bloat, no functional changes.
This commit is contained in:
parent
4775b513f9
commit
70ddbf7180
5 changed files with 23 additions and 35 deletions
|
@ -20,7 +20,6 @@ import (
|
||||||
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
||||||
|
@ -903,10 +902,9 @@ func (n *NEO) getAccountState(ic *interop.Context, args []stackitem.Item) stacki
|
||||||
return stackitem.Null{}
|
return stackitem.Null{}
|
||||||
}
|
}
|
||||||
|
|
||||||
r := io.NewBinReaderFromBuf(si)
|
item, err := stackitem.Deserialize(si)
|
||||||
item := stackitem.DecodeBinary(r)
|
if err != nil {
|
||||||
if r.Err != nil {
|
panic(err) // no errors are expected but we better be sure
|
||||||
panic(r.Err) // no errors are expected but we better be sure
|
|
||||||
}
|
}
|
||||||
return item
|
return item
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package native
|
||||||
import (
|
import (
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,17 +13,18 @@ type candidate struct {
|
||||||
|
|
||||||
// Bytes marshals c to byte array.
|
// Bytes marshals c to byte array.
|
||||||
func (c *candidate) Bytes() []byte {
|
func (c *candidate) Bytes() []byte {
|
||||||
w := io.NewBufBinWriter()
|
res, err := stackitem.Serialize(c.toStackItem())
|
||||||
stackitem.EncodeBinary(c.toStackItem(), w.BinWriter)
|
if err != nil {
|
||||||
return w.Bytes()
|
panic(err)
|
||||||
|
}
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromBytes unmarshals candidate from byte array.
|
// FromBytes unmarshals candidate from byte array.
|
||||||
func (c *candidate) FromBytes(data []byte) *candidate {
|
func (c *candidate) FromBytes(data []byte) *candidate {
|
||||||
r := io.NewBinReaderFromBuf(data)
|
item, err := stackitem.Deserialize(data)
|
||||||
item := stackitem.DecodeBinary(r)
|
if err != nil {
|
||||||
if r.Err != nil {
|
panic(err)
|
||||||
panic(r.Err)
|
|
||||||
}
|
}
|
||||||
return c.fromStackItem(item)
|
return c.fromStackItem(item)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -84,21 +83,18 @@ func (k *keysWithVotes) fromStackItem(item stackitem.Item) error {
|
||||||
|
|
||||||
// Bytes serializes keys with votes slice.
|
// Bytes serializes keys with votes slice.
|
||||||
func (k keysWithVotes) Bytes() []byte {
|
func (k keysWithVotes) Bytes() []byte {
|
||||||
var it = k.toStackItem()
|
buf, err := stackitem.Serialize(k.toStackItem())
|
||||||
var w = io.NewBufBinWriter()
|
if err != nil {
|
||||||
stackitem.EncodeBinary(it, w.BinWriter)
|
panic(err)
|
||||||
if w.Err != nil {
|
|
||||||
panic(w.Err)
|
|
||||||
}
|
}
|
||||||
return w.Bytes()
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeBytes deserializes keys and votes slice.
|
// DecodeBytes deserializes keys and votes slice.
|
||||||
func (k *keysWithVotes) DecodeBytes(data []byte) error {
|
func (k *keysWithVotes) DecodeBytes(data []byte) error {
|
||||||
var r = io.NewBinReaderFromBuf(data)
|
it, err := stackitem.Deserialize(data)
|
||||||
var it = stackitem.DecodeBinary(r)
|
if err != nil {
|
||||||
if r.Err != nil {
|
return err
|
||||||
return r.Err
|
|
||||||
}
|
}
|
||||||
return k.fromStackItem(it)
|
return k.fromStackItem(it)
|
||||||
}
|
}
|
||||||
|
|
|
@ -359,12 +359,10 @@ func (o *Oracle) RequestInternal(ic *interop.Context, url string, filter *string
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
w := io.NewBufBinWriter()
|
data, err := stackitem.Serialize(userData)
|
||||||
stackitem.EncodeBinary(userData, w.BinWriter)
|
if err != nil {
|
||||||
if w.Err != nil {
|
return err
|
||||||
return w.Err
|
|
||||||
}
|
}
|
||||||
data := w.Bytes()
|
|
||||||
if len(data) > maxUserDataLength {
|
if len(data) > maxUserDataLength {
|
||||||
return ErrBigArgument
|
return ErrBigArgument
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
"github.com/mr-tron/base58"
|
"github.com/mr-tron/base58"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
||||||
base58neogo "github.com/nspcc-dev/neo-go/pkg/encoding/base58"
|
base58neogo "github.com/nspcc-dev/neo-go/pkg/encoding/base58"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/vm"
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -275,11 +274,8 @@ func TestStdLibSerialize(t *testing.T) {
|
||||||
actualSerialized = s.serialize(ic, []stackitem.Item{stackitem.Make(42)})
|
actualSerialized = s.serialize(ic, []stackitem.Item{stackitem.Make(42)})
|
||||||
})
|
})
|
||||||
|
|
||||||
w := io.NewBufBinWriter()
|
encoded, err := stackitem.Serialize(stackitem.Make(42))
|
||||||
stackitem.EncodeBinary(stackitem.Make(42), w.BinWriter)
|
require.NoError(t, err)
|
||||||
require.NoError(t, w.Err)
|
|
||||||
|
|
||||||
encoded := w.Bytes()
|
|
||||||
require.Equal(t, stackitem.Make(encoded), actualSerialized)
|
require.Equal(t, stackitem.Make(encoded), actualSerialized)
|
||||||
|
|
||||||
require.NotPanics(t, func() {
|
require.NotPanics(t, func() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue