stackitem: remove Item/StackItem from function names

They're useless in a package named 'stackitem', make this package a bit more
user-friendly.
This commit is contained in:
Roman Khimov 2021-07-06 19:56:23 +03:00
parent b9ff07f32c
commit 0de949b575
21 changed files with 67 additions and 64 deletions

View file

@ -61,7 +61,7 @@ func Notify(ic *interop.Context) error {
// But it has to be serializable, otherwise we either have some broken // But it has to be serializable, otherwise we either have some broken
// (recursive) structure inside or an interop item that can't be used // (recursive) structure inside or an interop item that can't be used
// outside of the interop subsystem anyway. // outside of the interop subsystem anyway.
bytes, err := stackitem.SerializeItem(elem.Item()) bytes, err := stackitem.Serialize(elem.Item())
if err != nil { if err != nil {
return fmt.Errorf("bad notification: %w", err) return fmt.Errorf("bad notification: %w", err)
} }

View file

@ -57,7 +57,7 @@ func (s *Iterator) Value() stackitem.Item {
if s.opts&FindDeserialize != 0 { if s.opts&FindDeserialize != 0 {
bs := s.m[s.index].Value.Value().([]byte) bs := s.m[s.index].Value.Value().([]byte)
var err error var err error
value, err = stackitem.DeserializeItem(bs) value, err = stackitem.Deserialize(bs)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View file

@ -266,11 +266,11 @@ func TestStorageFind(t *testing.T) {
stackitem.NewByteArray([]byte("second")), stackitem.NewByteArray([]byte("second")),
stackitem.Null{}, stackitem.Null{},
} }
rawArr, err := stackitem.SerializeItem(stackitem.NewArray(arr)) rawArr, err := stackitem.Serialize(stackitem.NewArray(arr))
require.NoError(t, err) require.NoError(t, err)
rawArr0, err := stackitem.SerializeItem(stackitem.NewArray(arr[:0])) rawArr0, err := stackitem.Serialize(stackitem.NewArray(arr[:0]))
require.NoError(t, err) require.NoError(t, err)
rawArr1, err := stackitem.SerializeItem(stackitem.NewArray(arr[:1])) rawArr1, err := stackitem.Serialize(stackitem.NewArray(arr[:1]))
require.NoError(t, err) require.NoError(t, err)
skeys := [][]byte{{0x01, 0x02}, {0x02, 0x01}, {0x01, 0x01}, skeys := [][]byte{{0x01, 0x02}, {0x02, 0x01}, {0x01, 0x01},

View file

@ -904,7 +904,7 @@ func (n *NEO) getAccountState(ic *interop.Context, args []stackitem.Item) stacki
} }
r := io.NewBinReaderFromBuf(si) r := io.NewBinReaderFromBuf(si)
item := stackitem.DecodeBinaryStackItem(r) item := stackitem.DecodeBinary(r)
if r.Err != nil { if r.Err != nil {
panic(r.Err) // no errors are expected but we better be sure panic(r.Err) // no errors are expected but we better be sure
} }

View file

@ -15,14 +15,14 @@ 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() w := io.NewBufBinWriter()
stackitem.EncodeBinaryStackItem(c.toStackItem(), w.BinWriter) stackitem.EncodeBinary(c.toStackItem(), w.BinWriter)
return w.Bytes() return w.Bytes()
} }
// 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) r := io.NewBinReaderFromBuf(data)
item := stackitem.DecodeBinaryStackItem(r) item := stackitem.DecodeBinary(r)
if r.Err != nil { if r.Err != nil {
panic(r.Err) panic(r.Err)
} }

View file

@ -86,7 +86,7 @@ func (k *keysWithVotes) fromStackItem(item stackitem.Item) error {
func (k keysWithVotes) Bytes() []byte { func (k keysWithVotes) Bytes() []byte {
var it = k.toStackItem() var it = k.toStackItem()
var w = io.NewBufBinWriter() var w = io.NewBufBinWriter()
stackitem.EncodeBinaryStackItem(it, w.BinWriter) stackitem.EncodeBinary(it, w.BinWriter)
if w.Err != nil { if w.Err != nil {
panic(w.Err) panic(w.Err)
} }
@ -96,7 +96,7 @@ func (k keysWithVotes) Bytes() []byte {
// 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) var r = io.NewBinReaderFromBuf(data)
var it = stackitem.DecodeBinaryStackItem(r) var it = stackitem.DecodeBinary(r)
if r.Err != nil { if r.Err != nil {
return r.Err return r.Err
} }

View file

@ -278,7 +278,7 @@ func (o *Oracle) FinishInternal(ic *interop.Context) error {
}) })
r := io.NewBinReaderFromBuf(req.UserData) r := io.NewBinReaderFromBuf(req.UserData)
userData := stackitem.DecodeBinaryStackItem(r) userData := stackitem.DecodeBinary(r)
args := []stackitem.Item{ args := []stackitem.Item{
stackitem.Make(req.URL), stackitem.Make(req.URL),
stackitem.Make(userData), stackitem.Make(userData),
@ -358,7 +358,7 @@ func (o *Oracle) RequestInternal(ic *interop.Context, url string, filter *string
} }
w := io.NewBufBinWriter() w := io.NewBufBinWriter()
stackitem.EncodeBinaryStackItem(userData, w.BinWriter) stackitem.EncodeBinary(userData, w.BinWriter)
if w.Err != nil { if w.Err != nil {
return w.Err return w.Err
} }

View file

@ -25,12 +25,12 @@ func (l IDList) Bytes() []byte {
// EncodeBinary implements io.Serializable. // EncodeBinary implements io.Serializable.
func (l IDList) EncodeBinary(w *io.BinWriter) { func (l IDList) EncodeBinary(w *io.BinWriter) {
stackitem.EncodeBinaryStackItem(l.toStackItem(), w) stackitem.EncodeBinary(l.toStackItem(), w)
} }
// DecodeBinary implements io.Serializable. // DecodeBinary implements io.Serializable.
func (l *IDList) DecodeBinary(r *io.BinReader) { func (l *IDList) DecodeBinary(r *io.BinReader) {
item := stackitem.DecodeBinaryStackItem(r) item := stackitem.DecodeBinary(r)
if r.Err != nil || item == nil { if r.Err != nil || item == nil {
return return
} }
@ -84,12 +84,12 @@ func (l NodeList) Bytes() []byte {
// EncodeBinary implements io.Serializable. // EncodeBinary implements io.Serializable.
func (l NodeList) EncodeBinary(w *io.BinWriter) { func (l NodeList) EncodeBinary(w *io.BinWriter) {
stackitem.EncodeBinaryStackItem(l.toStackItem(), w) stackitem.EncodeBinary(l.toStackItem(), w)
} }
// DecodeBinary implements io.Serializable. // DecodeBinary implements io.Serializable.
func (l *NodeList) DecodeBinary(r *io.BinReader) { func (l *NodeList) DecodeBinary(r *io.BinReader) {
item := stackitem.DecodeBinaryStackItem(r) item := stackitem.DecodeBinary(r)
if r.Err != nil || item == nil { if r.Err != nil || item == nil {
return return
} }

View file

@ -14,7 +14,7 @@ func getInvalidTestFunc(actual io.Serializable, value interface{}) func(t *testi
return func(t *testing.T) { return func(t *testing.T) {
w := io.NewBufBinWriter() w := io.NewBufBinWriter()
it := stackitem.Make(value) it := stackitem.Make(value)
stackitem.EncodeBinaryStackItem(it, w.BinWriter) stackitem.EncodeBinary(it, w.BinWriter)
require.NoError(t, w.Err) require.NoError(t, w.Err)
require.Error(t, testserdes.DecodeBinary(w.Bytes(), actual)) require.Error(t, testserdes.DecodeBinary(w.Bytes(), actual))
} }

View file

@ -160,7 +160,7 @@ func newStd() *Std {
} }
func (s *Std) serialize(_ *interop.Context, args []stackitem.Item) stackitem.Item { func (s *Std) serialize(_ *interop.Context, args []stackitem.Item) stackitem.Item {
data, err := stackitem.SerializeItem(args[0]) data, err := stackitem.Serialize(args[0])
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -177,7 +177,7 @@ func (s *Std) deserialize(_ *interop.Context, args []stackitem.Item) stackitem.I
panic(err) panic(err)
} }
item, err := stackitem.DeserializeItem(data) item, err := stackitem.Deserialize(data)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View file

@ -276,7 +276,7 @@ func TestStdLibSerialize(t *testing.T) {
}) })
w := io.NewBufBinWriter() w := io.NewBufBinWriter()
stackitem.EncodeBinaryStackItem(stackitem.Make(42), w.BinWriter) stackitem.EncodeBinary(stackitem.Make(42), w.BinWriter)
require.NoError(t, w.Err) require.NoError(t, w.Err)
encoded := w.Bytes() encoded := w.Bytes()
@ -368,7 +368,7 @@ func TestStdLibSerializeDeserialize(t *testing.T) {
}) })
}) })
t.Run("Deserialize unknown", func(t *testing.T) { t.Run("Deserialize unknown", func(t *testing.T) {
data, err := stackitem.SerializeItem(stackitem.NewBigInteger(big.NewInt(123))) data, err := stackitem.Serialize(stackitem.NewBigInteger(big.NewInt(123)))
require.NoError(t, err) require.NoError(t, err)
data[0] = 0xFF data[0] = 0xFF

View file

@ -94,7 +94,7 @@ func TestContractDeployAndUpdateWithParameter(t *testing.T) {
res, err := invokeContractMethod(bc, 1_00000000, cs1.Hash, "getValue") res, err := invokeContractMethod(bc, 1_00000000, cs1.Hash, "getValue")
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(res.Stack)) require.Equal(t, 1, len(res.Stack))
item, err := stackitem.DeserializeItem(res.Stack[0].Value().([]byte)) item, err := stackitem.Deserialize(res.Stack[0].Value().([]byte))
require.NoError(t, err) require.NoError(t, err)
expected := []stackitem.Item{stackitem.Make("create"), stackitem.Make(42)} expected := []stackitem.Item{stackitem.Make("create"), stackitem.Make(42)}
require.Equal(t, stackitem.NewArray(expected), item) require.Equal(t, stackitem.NewArray(expected), item)
@ -114,7 +114,7 @@ func TestContractDeployAndUpdateWithParameter(t *testing.T) {
res, err := invokeContractMethod(bc, 1_00000000, cs1.Hash, "getValue") res, err := invokeContractMethod(bc, 1_00000000, cs1.Hash, "getValue")
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(res.Stack)) require.Equal(t, 1, len(res.Stack))
item, err := stackitem.DeserializeItem(res.Stack[0].Value().([]byte)) item, err := stackitem.Deserialize(res.Stack[0].Value().([]byte))
require.NoError(t, err) require.NoError(t, err)
expected := []stackitem.Item{stackitem.Make("update"), stackitem.Make("new data")} expected := []stackitem.Item{stackitem.Make("update"), stackitem.Make("new data")}
require.Equal(t, stackitem.NewArray(expected), item) require.Equal(t, stackitem.NewArray(expected), item)
@ -259,7 +259,7 @@ func TestContractDeploy(t *testing.T) {
res, err := invokeContractMethod(bc, 1_00000000, cs1.Hash, "getValue") res, err := invokeContractMethod(bc, 1_00000000, cs1.Hash, "getValue")
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(res.Stack)) require.Equal(t, 1, len(res.Stack))
item, err := stackitem.DeserializeItem(res.Stack[0].Value().([]byte)) item, err := stackitem.Deserialize(res.Stack[0].Value().([]byte))
require.NoError(t, err) require.NoError(t, err)
expected := []stackitem.Item{stackitem.Make("create"), stackitem.Null{}} expected := []stackitem.Item{stackitem.Make("create"), stackitem.Null{}}
require.Equal(t, stackitem.NewArray(expected), item) require.Equal(t, stackitem.NewArray(expected), item)
@ -466,7 +466,7 @@ func TestContractUpdate(t *testing.T) {
res, err := invokeContractMethod(bc, 1_00000000, cs1.Hash, "getValue") res, err := invokeContractMethod(bc, 1_00000000, cs1.Hash, "getValue")
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(res.Stack)) require.Equal(t, 1, len(res.Stack))
item, err := stackitem.DeserializeItem(res.Stack[0].Value().([]byte)) item, err := stackitem.Deserialize(res.Stack[0].Value().([]byte))
require.NoError(t, err) require.NoError(t, err)
expected := []stackitem.Item{stackitem.Make("update"), stackitem.Null{}} expected := []stackitem.Item{stackitem.Make("update"), stackitem.Null{}}
require.Equal(t, stackitem.NewArray(expected), item) require.Equal(t, stackitem.NewArray(expected), item)

View file

@ -183,7 +183,7 @@ func TestOracle_Request(t *testing.T) {
si := ic.DAO.GetStorageItem(cs.ID, []byte("lastOracleResponse")) si := ic.DAO.GetStorageItem(cs.ID, []byte("lastOracleResponse"))
require.NotNil(t, si) require.NotNil(t, si)
item, err := stackitem.DeserializeItem(si) item, err := stackitem.Deserialize(si)
require.NoError(t, err) require.NoError(t, err)
arr, ok := item.Value().([]stackitem.Item) arr, ok := item.Value().([]stackitem.Item)
require.True(t, ok) require.True(t, ok)

View file

@ -37,7 +37,7 @@ type NativeContract struct {
// DecodeBinary implements Serializable interface. // DecodeBinary implements Serializable interface.
func (c *Contract) DecodeBinary(r *io.BinReader) { func (c *Contract) DecodeBinary(r *io.BinReader) {
si := stackitem.DecodeBinaryStackItem(r) si := stackitem.DecodeBinary(r)
if r.Err != nil { if r.Err != nil {
return return
} }
@ -51,7 +51,7 @@ func (c *Contract) EncodeBinary(w *io.BinWriter) {
w.Err = err w.Err = err
return return
} }
stackitem.EncodeBinaryStackItem(si, w) stackitem.EncodeBinary(si, w)
} }
// ToStackItem converts state.Contract to stackitem.Item. // ToStackItem converts state.Contract to stackitem.Item.

View file

@ -58,12 +58,12 @@ func (s *NEP17BalanceState) fromStackItem(item stackitem.Item) {
// EncodeBinary implements io.Serializable interface. // EncodeBinary implements io.Serializable interface.
func (s *NEP17BalanceState) EncodeBinary(w *io.BinWriter) { func (s *NEP17BalanceState) EncodeBinary(w *io.BinWriter) {
si := s.toStackItem() si := s.toStackItem()
stackitem.EncodeBinaryStackItem(si, w) stackitem.EncodeBinary(si, w)
} }
// DecodeBinary implements io.Serializable interface. // DecodeBinary implements io.Serializable interface.
func (s *NEP17BalanceState) DecodeBinary(r *io.BinReader) { func (s *NEP17BalanceState) DecodeBinary(r *io.BinReader) {
si := stackitem.DecodeBinaryStackItem(r) si := stackitem.DecodeBinary(r)
if r.Err != nil { if r.Err != nil {
return return
} }
@ -98,12 +98,12 @@ func (s *NEOBalanceState) Bytes() []byte {
// EncodeBinary implements io.Serializable interface. // EncodeBinary implements io.Serializable interface.
func (s *NEOBalanceState) EncodeBinary(w *io.BinWriter) { func (s *NEOBalanceState) EncodeBinary(w *io.BinWriter) {
si := s.toStackItem() si := s.toStackItem()
stackitem.EncodeBinaryStackItem(si, w) stackitem.EncodeBinary(si, w)
} }
// DecodeBinary implements io.Serializable interface. // DecodeBinary implements io.Serializable interface.
func (s *NEOBalanceState) DecodeBinary(r *io.BinReader) { func (s *NEOBalanceState) DecodeBinary(r *io.BinReader) {
si := stackitem.DecodeBinaryStackItem(r) si := stackitem.DecodeBinary(r)
if r.Err != nil { if r.Err != nil {
return return
} }

View file

@ -31,14 +31,14 @@ type AppExecResult struct {
func (ne *NotificationEvent) EncodeBinary(w *io.BinWriter) { func (ne *NotificationEvent) EncodeBinary(w *io.BinWriter) {
ne.ScriptHash.EncodeBinary(w) ne.ScriptHash.EncodeBinary(w)
w.WriteString(ne.Name) w.WriteString(ne.Name)
stackitem.EncodeBinaryStackItem(ne.Item, w) stackitem.EncodeBinary(ne.Item, w)
} }
// DecodeBinary implements the Serializable interface. // DecodeBinary implements the Serializable interface.
func (ne *NotificationEvent) DecodeBinary(r *io.BinReader) { func (ne *NotificationEvent) DecodeBinary(r *io.BinReader) {
ne.ScriptHash.DecodeBinary(r) ne.ScriptHash.DecodeBinary(r)
ne.Name = r.ReadString() ne.Name = r.ReadString()
item := stackitem.DecodeBinaryStackItem(r) item := stackitem.DecodeBinary(r)
if r.Err != nil { if r.Err != nil {
return return
} }
@ -59,7 +59,7 @@ func (aer *AppExecResult) EncodeBinary(w *io.BinWriter) {
// Stack items are expected to be marshaled one by one. // Stack items are expected to be marshaled one by one.
w.WriteVarUint(uint64(len(aer.Stack))) w.WriteVarUint(uint64(len(aer.Stack)))
for _, it := range aer.Stack { for _, it := range aer.Stack {
stackitem.EncodeBinaryStackItemAppExec(it, w) stackitem.EncodeBinaryProtected(it, w)
} }
w.WriteArray(aer.Events) w.WriteArray(aer.Events)
w.WriteVarBytes([]byte(aer.FaultException)) w.WriteVarBytes([]byte(aer.FaultException))
@ -80,7 +80,7 @@ func (aer *AppExecResult) DecodeBinary(r *io.BinReader) {
} }
arr := make([]stackitem.Item, sz) arr := make([]stackitem.Item, sz)
for i := 0; i < int(sz); i++ { for i := 0; i < int(sz); i++ {
arr[i] = stackitem.DecodeBinaryStackItemAppExec(r) arr[i] = stackitem.DecodeBinaryProtected(r)
if r.Err != nil { if r.Err != nil {
return return
} }

View file

@ -77,7 +77,7 @@ func TestEncodeDecodeAppExecResult(t *testing.T) {
w.WriteB(byte(aer.Trigger)) w.WriteB(byte(aer.Trigger))
w.WriteB(byte(aer.VMState)) w.WriteB(byte(aer.VMState))
w.WriteU64LE(uint64(aer.GasConsumed)) w.WriteU64LE(uint64(aer.GasConsumed))
stackitem.EncodeBinaryStackItem(stackitem.NewBool(true), w.BinWriter) stackitem.EncodeBinary(stackitem.NewBool(true), w.BinWriter)
require.NoError(t, w.Err) require.NoError(t, w.Err)
require.Error(t, testserdes.DecodeBinary(w.Bytes(), new(AppExecResult))) require.Error(t, testserdes.DecodeBinary(w.Bytes(), new(AppExecResult)))
}) })

View file

@ -30,12 +30,12 @@ func (o *OracleRequest) Bytes() []byte {
// EncodeBinary implements io.Serializable. // EncodeBinary implements io.Serializable.
func (o *OracleRequest) EncodeBinary(w *io.BinWriter) { func (o *OracleRequest) EncodeBinary(w *io.BinWriter) {
stackitem.EncodeBinaryStackItem(o.toStackItem(), w) stackitem.EncodeBinary(o.toStackItem(), w)
} }
// DecodeBinary implements io.Serializable. // DecodeBinary implements io.Serializable.
func (o *OracleRequest) DecodeBinary(r *io.BinReader) { func (o *OracleRequest) DecodeBinary(r *io.BinReader) {
item := stackitem.DecodeBinaryStackItem(r) item := stackitem.DecodeBinary(r)
if r.Err != nil || item == nil { if r.Err != nil || item == nil {
return return
} }

View file

@ -34,7 +34,7 @@ func TestOracleRequest_EncodeBinary(t *testing.T) {
t.Run("NotArray", func(t *testing.T) { t.Run("NotArray", func(t *testing.T) {
w.Reset() w.Reset()
it := stackitem.NewByteArray([]byte{}) it := stackitem.NewByteArray([]byte{})
stackitem.EncodeBinaryStackItem(it, w.BinWriter) stackitem.EncodeBinary(it, w.BinWriter)
require.Error(t, testserdes.DecodeBinary(w.Bytes(), new(OracleRequest))) require.Error(t, testserdes.DecodeBinary(w.Bytes(), new(OracleRequest)))
}) })
t.Run("NotStackItem", func(t *testing.T) { t.Run("NotStackItem", func(t *testing.T) {
@ -57,7 +57,7 @@ func TestOracleRequest_EncodeBinary(t *testing.T) {
w.Reset() w.Reset()
before := items[i] before := items[i]
items[i] = elem items[i] = elem
stackitem.EncodeBinaryStackItem(arrItem, w.BinWriter) stackitem.EncodeBinary(arrItem, w.BinWriter)
items[i] = before items[i] = before
require.Error(t, testserdes.DecodeBinary(w.Bytes(), new(OracleRequest))) require.Error(t, testserdes.DecodeBinary(w.Bytes(), new(OracleRequest)))
} }

View file

@ -17,8 +17,8 @@ type serContext struct {
seen map[Item]bool seen map[Item]bool
} }
// SerializeItem encodes given Item into the byte slice. // Serialize encodes given Item into the byte slice.
func SerializeItem(item Item) ([]byte, error) { func Serialize(item Item) ([]byte, error) {
w := io.NewBufBinWriter() w := io.NewBufBinWriter()
sc := serContext{ sc := serContext{
BinWriter: w.BinWriter, BinWriter: w.BinWriter,
@ -33,10 +33,10 @@ func SerializeItem(item Item) ([]byte, error) {
return w.Bytes(), nil return w.Bytes(), nil
} }
// EncodeBinaryStackItem encodes given Item into the given BinWriter. It's // EncodeBinary encodes given Item into the given BinWriter. It's
// similar to io.Serializable's EncodeBinary, but works with Item // similar to io.Serializable's EncodeBinary, but works with Item
// interface. // interface.
func EncodeBinaryStackItem(item Item, w *io.BinWriter) { func EncodeBinary(item Item, w *io.BinWriter) {
sc := serContext{ sc := serContext{
BinWriter: w, BinWriter: w,
allowInvalid: false, allowInvalid: false,
@ -45,9 +45,12 @@ func EncodeBinaryStackItem(item Item, w *io.BinWriter) {
sc.serialize(item) sc.serialize(item)
} }
// EncodeBinaryStackItemAppExec encodes given Item into the given BinWriter. It's // EncodeBinaryProtected encodes given Item into the given BinWriter. It's
// similar to EncodeBinaryStackItem but allows to encode interop (only type, value is lost). // similar to EncodeBinary but allows to encode interop items (only type,
func EncodeBinaryStackItemAppExec(item Item, w *io.BinWriter) { // value is lost) and doesn't return any errors in w, instead if error
// (like recursive array) is encountered it just writes special InvalidT
// type of element to w.
func EncodeBinaryProtected(item Item, w *io.BinWriter) {
bw := io.NewBufBinWriter() bw := io.NewBufBinWriter()
sc := serContext{ sc := serContext{
BinWriter: bw.BinWriter, BinWriter: bw.BinWriter,
@ -132,31 +135,31 @@ func (w *serContext) serialize(item Item) {
} }
} }
// DeserializeItem decodes Item from the given byte slice. // Deserialize decodes Item from the given byte slice.
func DeserializeItem(data []byte) (Item, error) { func Deserialize(data []byte) (Item, error) {
r := io.NewBinReaderFromBuf(data) r := io.NewBinReaderFromBuf(data)
item := DecodeBinaryStackItem(r) item := DecodeBinary(r)
if r.Err != nil { if r.Err != nil {
return nil, r.Err return nil, r.Err
} }
return item, nil return item, nil
} }
// DecodeBinaryStackItem decodes previously serialized Item from the given // DecodeBinary decodes previously serialized Item from the given
// reader. It's similar to the io.Serializable's DecodeBinary(), but implemented // reader. It's similar to the io.Serializable's DecodeBinary(), but implemented
// as a function because Item itself is an interface. Caveat: always check // as a function because Item itself is an interface. Caveat: always check
// reader's error value before using the returned Item. // reader's error value before using the returned Item.
func DecodeBinaryStackItem(r *io.BinReader) Item { func DecodeBinary(r *io.BinReader) Item {
return decodeBinaryStackItem(r, false) return decodeBinary(r, false)
} }
// DecodeBinaryStackItemAppExec is similar to DecodeBinaryStackItem // DecodeBinaryProtected is similar to DecodeBinary but allows Interop and
// but allows Interop values to be present. // Invalid values to be present (making it symmetric to EncodeBinaryProtected).
func DecodeBinaryStackItemAppExec(r *io.BinReader) Item { func DecodeBinaryProtected(r *io.BinReader) Item {
return decodeBinaryStackItem(r, true) return decodeBinary(r, true)
} }
func decodeBinaryStackItem(r *io.BinReader, allowInvalid bool) Item { func decodeBinary(r *io.BinReader, allowInvalid bool) Item {
var t = Type(r.ReadB()) var t = Type(r.ReadB())
if r.Err != nil { if r.Err != nil {
return nil return nil
@ -180,7 +183,7 @@ func decodeBinaryStackItem(r *io.BinReader, allowInvalid bool) Item {
size := int(r.ReadVarUint()) size := int(r.ReadVarUint())
arr := make([]Item, size) arr := make([]Item, size)
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
arr[i] = DecodeBinaryStackItem(r) arr[i] = DecodeBinary(r)
} }
if t == ArrayT { if t == ArrayT {
@ -191,8 +194,8 @@ func decodeBinaryStackItem(r *io.BinReader, allowInvalid bool) Item {
size := int(r.ReadVarUint()) size := int(r.ReadVarUint())
m := NewMap() m := NewMap()
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
key := DecodeBinaryStackItem(r) key := DecodeBinary(r)
value := DecodeBinaryStackItem(r) value := DecodeBinary(r)
if r.Err != nil { if r.Err != nil {
break break
} }

View file

@ -13,9 +13,9 @@ func TestSerializationMaxErr(t *testing.T) {
arr := []Item{item, item.Dup()} arr := []Item{item, item.Dup()}
aitem := Make(arr) aitem := Make(arr)
_, err := SerializeItem(item) _, err := Serialize(item)
require.NoError(t, err) require.NoError(t, err)
_, err = SerializeItem(aitem) _, err = Serialize(aitem)
require.Error(t, err) require.Error(t, err)
} }