native: reuse stackitem.(De)Serialize more for data structures

Less code bloat, no functional changes.
This commit is contained in:
Roman Khimov 2021-07-16 16:00:25 +03:00
parent 4775b513f9
commit 70ddbf7180
5 changed files with 23 additions and 35 deletions

View file

@ -20,7 +20,6 @@ import (
"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/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/callflag"
"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{}
}
r := io.NewBinReaderFromBuf(si)
item := stackitem.DecodeBinary(r)
if r.Err != nil {
panic(r.Err) // no errors are expected but we better be sure
item, err := stackitem.Deserialize(si)
if err != nil {
panic(err) // no errors are expected but we better be sure
}
return item
}

View file

@ -3,7 +3,6 @@ package native
import (
"math/big"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
@ -14,17 +13,18 @@ type candidate struct {
// Bytes marshals c to byte array.
func (c *candidate) Bytes() []byte {
w := io.NewBufBinWriter()
stackitem.EncodeBinary(c.toStackItem(), w.BinWriter)
return w.Bytes()
res, err := stackitem.Serialize(c.toStackItem())
if err != nil {
panic(err)
}
return res
}
// FromBytes unmarshals candidate from byte array.
func (c *candidate) FromBytes(data []byte) *candidate {
r := io.NewBinReaderFromBuf(data)
item := stackitem.DecodeBinary(r)
if r.Err != nil {
panic(r.Err)
item, err := stackitem.Deserialize(data)
if err != nil {
panic(err)
}
return c.fromStackItem(item)
}

View file

@ -6,7 +6,6 @@ import (
"math/big"
"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"
)
@ -84,21 +83,18 @@ func (k *keysWithVotes) fromStackItem(item stackitem.Item) error {
// Bytes serializes keys with votes slice.
func (k keysWithVotes) Bytes() []byte {
var it = k.toStackItem()
var w = io.NewBufBinWriter()
stackitem.EncodeBinary(it, w.BinWriter)
if w.Err != nil {
panic(w.Err)
buf, err := stackitem.Serialize(k.toStackItem())
if err != nil {
panic(err)
}
return w.Bytes()
return buf
}
// DecodeBytes deserializes keys and votes slice.
func (k *keysWithVotes) DecodeBytes(data []byte) error {
var r = io.NewBinReaderFromBuf(data)
var it = stackitem.DecodeBinary(r)
if r.Err != nil {
return r.Err
it, err := stackitem.Deserialize(data)
if err != nil {
return err
}
return k.fromStackItem(it)
}

View file

@ -359,12 +359,10 @@ func (o *Oracle) RequestInternal(ic *interop.Context, url string, filter *string
return err
}
w := io.NewBufBinWriter()
stackitem.EncodeBinary(userData, w.BinWriter)
if w.Err != nil {
return w.Err
data, err := stackitem.Serialize(userData)
if err != nil {
return err
}
data := w.Bytes()
if len(data) > maxUserDataLength {
return ErrBigArgument
}

View file

@ -11,7 +11,6 @@ import (
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
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/stackitem"
"github.com/stretchr/testify/assert"
@ -275,11 +274,8 @@ func TestStdLibSerialize(t *testing.T) {
actualSerialized = s.serialize(ic, []stackitem.Item{stackitem.Make(42)})
})
w := io.NewBufBinWriter()
stackitem.EncodeBinary(stackitem.Make(42), w.BinWriter)
require.NoError(t, w.Err)
encoded := w.Bytes()
encoded, err := stackitem.Serialize(stackitem.Make(42))
require.NoError(t, err)
require.Equal(t, stackitem.Make(encoded), actualSerialized)
require.NotPanics(t, func() {