mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-23 13:41:37 +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/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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in a new issue