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
// (recursive) structure inside or an interop item that can't be used
// outside of the interop subsystem anyway.
bytes, err := stackitem.SerializeItem(elem.Item())
bytes, err := stackitem.Serialize(elem.Item())
if err != nil {
return fmt.Errorf("bad notification: %w", err)
}

View file

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

View file

@ -266,11 +266,11 @@ func TestStorageFind(t *testing.T) {
stackitem.NewByteArray([]byte("second")),
stackitem.Null{},
}
rawArr, err := stackitem.SerializeItem(stackitem.NewArray(arr))
rawArr, err := stackitem.Serialize(stackitem.NewArray(arr))
require.NoError(t, err)
rawArr0, err := stackitem.SerializeItem(stackitem.NewArray(arr[:0]))
rawArr0, err := stackitem.Serialize(stackitem.NewArray(arr[:0]))
require.NoError(t, err)
rawArr1, err := stackitem.SerializeItem(stackitem.NewArray(arr[:1]))
rawArr1, err := stackitem.Serialize(stackitem.NewArray(arr[:1]))
require.NoError(t, err)
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)
item := stackitem.DecodeBinaryStackItem(r)
item := stackitem.DecodeBinary(r)
if r.Err != nil {
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.
func (c *candidate) Bytes() []byte {
w := io.NewBufBinWriter()
stackitem.EncodeBinaryStackItem(c.toStackItem(), w.BinWriter)
stackitem.EncodeBinary(c.toStackItem(), w.BinWriter)
return w.Bytes()
}
// FromBytes unmarshals candidate from byte array.
func (c *candidate) FromBytes(data []byte) *candidate {
r := io.NewBinReaderFromBuf(data)
item := stackitem.DecodeBinaryStackItem(r)
item := stackitem.DecodeBinary(r)
if r.Err != nil {
panic(r.Err)
}

View file

@ -86,7 +86,7 @@ func (k *keysWithVotes) fromStackItem(item stackitem.Item) error {
func (k keysWithVotes) Bytes() []byte {
var it = k.toStackItem()
var w = io.NewBufBinWriter()
stackitem.EncodeBinaryStackItem(it, w.BinWriter)
stackitem.EncodeBinary(it, w.BinWriter)
if w.Err != nil {
panic(w.Err)
}
@ -96,7 +96,7 @@ func (k keysWithVotes) Bytes() []byte {
// DecodeBytes deserializes keys and votes slice.
func (k *keysWithVotes) DecodeBytes(data []byte) error {
var r = io.NewBinReaderFromBuf(data)
var it = stackitem.DecodeBinaryStackItem(r)
var it = stackitem.DecodeBinary(r)
if r.Err != nil {
return r.Err
}

View file

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

View file

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

View file

@ -14,7 +14,7 @@ func getInvalidTestFunc(actual io.Serializable, value interface{}) func(t *testi
return func(t *testing.T) {
w := io.NewBufBinWriter()
it := stackitem.Make(value)
stackitem.EncodeBinaryStackItem(it, w.BinWriter)
stackitem.EncodeBinary(it, w.BinWriter)
require.NoError(t, w.Err)
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 {
data, err := stackitem.SerializeItem(args[0])
data, err := stackitem.Serialize(args[0])
if err != nil {
panic(err)
}
@ -177,7 +177,7 @@ func (s *Std) deserialize(_ *interop.Context, args []stackitem.Item) stackitem.I
panic(err)
}
item, err := stackitem.DeserializeItem(data)
item, err := stackitem.Deserialize(data)
if err != nil {
panic(err)
}

View file

@ -276,7 +276,7 @@ func TestStdLibSerialize(t *testing.T) {
})
w := io.NewBufBinWriter()
stackitem.EncodeBinaryStackItem(stackitem.Make(42), w.BinWriter)
stackitem.EncodeBinary(stackitem.Make(42), w.BinWriter)
require.NoError(t, w.Err)
encoded := w.Bytes()
@ -368,7 +368,7 @@ func TestStdLibSerializeDeserialize(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)
data[0] = 0xFF

View file

@ -94,7 +94,7 @@ func TestContractDeployAndUpdateWithParameter(t *testing.T) {
res, err := invokeContractMethod(bc, 1_00000000, cs1.Hash, "getValue")
require.NoError(t, err)
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)
expected := []stackitem.Item{stackitem.Make("create"), stackitem.Make(42)}
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")
require.NoError(t, err)
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)
expected := []stackitem.Item{stackitem.Make("update"), stackitem.Make("new data")}
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")
require.NoError(t, err)
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)
expected := []stackitem.Item{stackitem.Make("create"), stackitem.Null{}}
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")
require.NoError(t, err)
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)
expected := []stackitem.Item{stackitem.Make("update"), stackitem.Null{}}
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"))
require.NotNil(t, si)
item, err := stackitem.DeserializeItem(si)
item, err := stackitem.Deserialize(si)
require.NoError(t, err)
arr, ok := item.Value().([]stackitem.Item)
require.True(t, ok)

View file

@ -37,7 +37,7 @@ type NativeContract struct {
// DecodeBinary implements Serializable interface.
func (c *Contract) DecodeBinary(r *io.BinReader) {
si := stackitem.DecodeBinaryStackItem(r)
si := stackitem.DecodeBinary(r)
if r.Err != nil {
return
}
@ -51,7 +51,7 @@ func (c *Contract) EncodeBinary(w *io.BinWriter) {
w.Err = err
return
}
stackitem.EncodeBinaryStackItem(si, w)
stackitem.EncodeBinary(si, w)
}
// 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.
func (s *NEP17BalanceState) EncodeBinary(w *io.BinWriter) {
si := s.toStackItem()
stackitem.EncodeBinaryStackItem(si, w)
stackitem.EncodeBinary(si, w)
}
// DecodeBinary implements io.Serializable interface.
func (s *NEP17BalanceState) DecodeBinary(r *io.BinReader) {
si := stackitem.DecodeBinaryStackItem(r)
si := stackitem.DecodeBinary(r)
if r.Err != nil {
return
}
@ -98,12 +98,12 @@ func (s *NEOBalanceState) Bytes() []byte {
// EncodeBinary implements io.Serializable interface.
func (s *NEOBalanceState) EncodeBinary(w *io.BinWriter) {
si := s.toStackItem()
stackitem.EncodeBinaryStackItem(si, w)
stackitem.EncodeBinary(si, w)
}
// DecodeBinary implements io.Serializable interface.
func (s *NEOBalanceState) DecodeBinary(r *io.BinReader) {
si := stackitem.DecodeBinaryStackItem(r)
si := stackitem.DecodeBinary(r)
if r.Err != nil {
return
}

View file

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

View file

@ -77,7 +77,7 @@ func TestEncodeDecodeAppExecResult(t *testing.T) {
w.WriteB(byte(aer.Trigger))
w.WriteB(byte(aer.VMState))
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.Error(t, testserdes.DecodeBinary(w.Bytes(), new(AppExecResult)))
})

View file

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

View file

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

View file

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

View file

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