From aab18c3083e1fcaf37a79bcb119c27c3818e2bc4 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sat, 17 Jul 2021 18:37:33 +0300 Subject: [PATCH] stackitem: introduce Convertible interface We have a lot of native contract types that are converted to stack items before serialization, then deserialized as stack items and converted back to regular structures. stackitem.Convertible allows to remove a lot of repetitive io.Serializable code. This also introduces to/from converter in testserdes which unfortunately required to change util tests to avoid circular references. --- internal/testserdes/testing.go | 10 +++ pkg/core/blockchain.go | 18 ++-- pkg/core/native/designate.go | 13 ++- pkg/core/native/management.go | 15 ++-- pkg/core/native/native_neo.go | 6 +- pkg/core/native/native_neo_candidate.go | 30 +++---- pkg/core/native/native_neo_test.go | 7 +- pkg/core/native/notary.go | 4 +- pkg/core/native/oracle.go | 27 +++--- pkg/core/native/oracle_types.go | 59 +++---------- pkg/core/native/oracle_types_test.go | 24 ++--- pkg/core/native/util.go | 18 ++-- pkg/core/state/contract.go | 19 ---- pkg/core/state/contract_test.go | 6 +- pkg/core/state/deposit.go | 24 ++--- pkg/core/state/deposit_test.go | 21 ++--- pkg/core/state/native_state.go | 111 ++++++++++-------------- pkg/core/state/oracle.go | 31 ++----- pkg/core/state/oracle_test.go | 21 ++--- pkg/util/uint160_test.go | 41 ++++----- pkg/util/uint256_test.go | 33 +++---- pkg/vm/stackitem/item.go | 6 ++ pkg/vm/stackitem/serialization.go | 18 ++++ 23 files changed, 223 insertions(+), 339 deletions(-) diff --git a/internal/testserdes/testing.go b/internal/testserdes/testing.go index 0be71a844..d30e1ddaa 100644 --- a/internal/testserdes/testing.go +++ b/internal/testserdes/testing.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/nspcc-dev/neo-go/pkg/io" + "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/stretchr/testify/require" ) @@ -26,6 +27,15 @@ func EncodeDecodeBinary(t *testing.T, expected, actual io.Serializable) { require.Equal(t, expected, actual) } +// ToFromStackItem checks if expected stays the same after converting to/from +// StackItem. +func ToFromStackItem(t *testing.T, expected, actual stackitem.Convertible) { + item, err := expected.ToStackItem() + require.NoError(t, err) + require.NoError(t, actual.FromStackItem(item)) + require.Equal(t, expected, actual) +} + // EncodeBinary serializes a to a byte slice. func EncodeBinary(a io.Serializable) ([]byte, error) { w := io.NewBufBinWriter() diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 46e711615..a3e068f78 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -361,24 +361,18 @@ func (bc *Blockchain) init() error { if storedCS == nil { return fmt.Errorf("native contract %s is not stored", md.Name) } - w := io.NewBufBinWriter() - storedCS.EncodeBinary(w.BinWriter) - if w.Err != nil { - return fmt.Errorf("failed to check native %s state against autogenerated one: %w", md.Name, w.Err) + storedCSBytes, err := stackitem.SerializeConvertible(storedCS) + if err != nil { + return fmt.Errorf("failed to check native %s state against autogenerated one: %w", md.Name, err) } - buff := w.Bytes() - storedCSBytes := make([]byte, len(buff)) - copy(storedCSBytes, buff) - w.Reset() autogenCS := &state.Contract{ ContractBase: md.ContractBase, UpdateCounter: storedCS.UpdateCounter, // it can be restored only from the DB, so use the stored value. } - autogenCS.EncodeBinary(w.BinWriter) - if w.Err != nil { - return fmt.Errorf("failed to check native %s state against autogenerated one: %w", md.Name, w.Err) + autogenCSBytes, err := stackitem.SerializeConvertible(autogenCS) + if err != nil { + return fmt.Errorf("failed to check native %s state against autogenerated one: %w", md.Name, err) } - autogenCSBytes := w.Bytes() if !bytes.Equal(storedCSBytes, autogenCSBytes) { return fmt.Errorf("native %s: version mismatch (stored contract state differs from autogenerated one), "+ "try to resynchronize the node from the genesis", md.Name) diff --git a/pkg/core/native/designate.go b/pkg/core/native/designate.go index 05f387dec..8c75eebc8 100644 --- a/pkg/core/native/designate.go +++ b/pkg/core/native/designate.go @@ -18,7 +18,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/state" "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/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" @@ -272,10 +271,9 @@ func (s *Designate) GetDesignatedByRole(d dao.DAO, r noderoles.Role, index uint3 } } if resSi != nil { - reader := io.NewBinReaderFromBuf(resSi) - ns.DecodeBinary(reader) - if reader.Err != nil { - return nil, 0, reader.Err + err = stackitem.DeserializeConvertible(resSi, &ns) + if err != nil { + return nil, 0, err } } return keys.PublicKeys(ns), bestIndex, err @@ -287,7 +285,7 @@ func (s *Designate) designateAsRole(ic *interop.Context, args []stackitem.Item) panic(ErrInvalidRole) } var ns NodeList - if err := ns.fromStackItem(args[1]); err != nil { + if err := ns.FromStackItem(args[1]); err != nil { panic(err) } @@ -326,8 +324,9 @@ func (s *Designate) DesignateAsRole(ic *interop.Context, r noderoles.Role, pubs return ErrAlreadyDesignated } sort.Sort(pubs) + nl := NodeList(pubs) s.rolesChangedFlag.Store(true) - err := ic.DAO.PutStorageItem(s.ID, key, NodeList(pubs).Bytes()) + err := putConvertibleToDAO(s.ID, ic.DAO, key, &nl) if err != nil { return err } diff --git a/pkg/core/native/management.go b/pkg/core/native/management.go index 032bf3571..a5eb71c52 100644 --- a/pkg/core/native/management.go +++ b/pkg/core/native/management.go @@ -16,7 +16,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/storage" "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" @@ -155,7 +154,7 @@ func (m *Management) GetContract(d dao.DAO, hash util.Uint160) (*state.Contract, func (m *Management) getContractFromDAO(d dao.DAO, hash util.Uint160) (*state.Contract, error) { contract := new(state.Contract) key := makeContractKey(hash) - err := getSerializableFromDAO(m.ID, d, key, contract) + err := getConvertibleFromDAO(m.ID, d, key, contract) if err != nil { return nil, err } @@ -487,14 +486,12 @@ func (m *Management) InitializeCache(d dao.DAO) error { var initErr error d.Seek(m.ID, []byte{prefixContract}, func(_, v []byte) { - var cs state.Contract - r := io.NewBinReaderFromBuf(v) - cs.DecodeBinary(r) - if r.Err != nil { - initErr = r.Err + var cs = new(state.Contract) + initErr = stackitem.DeserializeConvertible(v, cs) + if initErr != nil { return } - m.contracts[cs.Hash] = &cs + m.contracts[cs.Hash] = cs }) return initErr } @@ -529,7 +526,7 @@ func (m *Management) Initialize(ic *interop.Context) error { // PutContractState saves given contract state into given DAO. func (m *Management) PutContractState(d dao.DAO, cs *state.Contract) error { key := makeContractKey(cs.Hash) - if err := putSerializableToDAO(m.ID, d, key, cs); err != nil { + if err := putConvertibleToDAO(m.ID, d, key, cs); err != nil { return err } m.markUpdated(cs.Hash) diff --git a/pkg/core/native/native_neo.go b/pkg/core/native/native_neo.go index b5e433d14..3726d9063 100644 --- a/pkg/core/native/native_neo.go +++ b/pkg/core/native/native_neo.go @@ -697,7 +697,7 @@ func (n *NEO) RegisterCandidateInternal(ic *interop.Context, pub *keys.PublicKey c = new(candidate).FromBytes(si) c.Registered = true } - return ic.DAO.PutStorageItem(n.ID, key, c.Bytes()) + return putConvertibleToDAO(n.ID, ic.DAO, key, c) } func (n *NEO) unregisterCandidate(ic *interop.Context, args []stackitem.Item) stackitem.Item { @@ -726,7 +726,7 @@ func (n *NEO) UnregisterCandidateInternal(ic *interop.Context, pub *keys.PublicK if ok { return err } - return ic.DAO.PutStorageItem(n.ID, key, c.Bytes()) + return putConvertibleToDAO(n.ID, ic.DAO, key, c) } func (n *NEO) vote(ic *interop.Context, args []stackitem.Item) stackitem.Item { @@ -819,7 +819,7 @@ func (n *NEO) ModifyAccountVotes(acc *state.NEOBalanceState, d dao.DAO, value *b } } n.validators.Store(keys.PublicKeys(nil)) - return d.PutStorageItem(n.ID, key, cd.Bytes()) + return putConvertibleToDAO(n.ID, d, key, cd) } return nil } diff --git a/pkg/core/native/native_neo_candidate.go b/pkg/core/native/native_neo_candidate.go index f0802d512..8a1d6ca84 100644 --- a/pkg/core/native/native_neo_candidate.go +++ b/pkg/core/native/native_neo_candidate.go @@ -11,41 +11,35 @@ type candidate struct { Votes big.Int } -// Bytes marshals c to byte array. -func (c *candidate) Bytes() []byte { - 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 { - item, err := stackitem.Deserialize(data) + err := stackitem.DeserializeConvertible(data, c) if err != nil { panic(err) } - return c.fromStackItem(item) + return c } -func (c *candidate) toStackItem() stackitem.Item { +// ToStackItem implements stackitem.Convertible. It never returns an error. +func (c *candidate) ToStackItem() (stackitem.Item, error) { return stackitem.NewStruct([]stackitem.Item{ stackitem.NewBool(c.Registered), stackitem.NewBigInteger(&c.Votes), - }) + }), nil } -func (c *candidate) fromStackItem(item stackitem.Item) *candidate { +// FromStackItem implements stackitem.Convertible. +func (c *candidate) FromStackItem(item stackitem.Item) error { arr := item.(*stackitem.Struct).Value().([]stackitem.Item) vs, err := arr[1].TryInteger() if err != nil { - panic(err) + return err } - c.Registered, err = arr[0].TryBool() + reg, err := arr[0].TryBool() if err != nil { - panic(err) + return err } + c.Registered = reg c.Votes = *vs - return c + return nil } diff --git a/pkg/core/native/native_neo_test.go b/pkg/core/native/native_neo_test.go index bccd3bd42..ebeb30742 100644 --- a/pkg/core/native/native_neo_test.go +++ b/pkg/core/native/native_neo_test.go @@ -4,7 +4,7 @@ import ( "math/big" "testing" - "github.com/stretchr/testify/require" + "github.com/nspcc-dev/neo-go/internal/testserdes" ) func TestCandidate_Bytes(t *testing.T) { @@ -12,7 +12,6 @@ func TestCandidate_Bytes(t *testing.T) { Registered: true, Votes: *big.NewInt(0x0F), } - data := expected.Bytes() - actual := new(candidate).FromBytes(data) - require.Equal(t, expected, actual) + actual := new(candidate) + testserdes.ToFromStackItem(t, expected, actual) } diff --git a/pkg/core/native/notary.go b/pkg/core/native/notary.go index e29224a0c..36d268a3e 100644 --- a/pkg/core/native/notary.go +++ b/pkg/core/native/notary.go @@ -414,7 +414,7 @@ func (n *Notary) setMaxNotValidBeforeDelta(ic *interop.Context, args []stackitem func (n *Notary) GetDepositFor(dao dao.DAO, acc util.Uint160) *state.Deposit { key := append([]byte{prefixDeposit}, acc.BytesBE()...) deposit := new(state.Deposit) - err := getSerializableFromDAO(n.ID, dao, key, deposit) + err := getConvertibleFromDAO(n.ID, dao, key, deposit) if err == nil { return deposit } @@ -427,7 +427,7 @@ func (n *Notary) GetDepositFor(dao dao.DAO, acc util.Uint160) *state.Deposit { // putDepositFor puts deposit on the balance of the specified account in the storage. func (n *Notary) putDepositFor(dao dao.DAO, deposit *state.Deposit, acc util.Uint160) error { key := append([]byte{prefixDeposit}, acc.BytesBE()...) - return putSerializableToDAO(n.ID, dao, key, deposit) + return putConvertibleToDAO(n.ID, dao, key, deposit) } // removeDepositFor removes deposit from the storage. diff --git a/pkg/core/native/oracle.go b/pkg/core/native/oracle.go index 9b640b226..0b4e927b7 100644 --- a/pkg/core/native/oracle.go +++ b/pkg/core/native/oracle.go @@ -170,7 +170,7 @@ func (o *Oracle) PostPersist(ic *interop.Context) error { } reqKey := makeRequestKey(resp.ID) req := new(state.OracleRequest) - if err := o.getSerializableFromDAO(ic.DAO, reqKey, req); err != nil { + if err := o.getConvertibleFromDAO(ic.DAO, reqKey, req); err != nil { continue } if err := ic.DAO.DeleteStorageItem(o.ID, reqKey); err != nil { @@ -182,7 +182,7 @@ func (o *Oracle) PostPersist(ic *interop.Context) error { idKey := makeIDListKey(req.URL) idList := new(IDList) - if err := o.getSerializableFromDAO(ic.DAO, idKey, idList); err != nil { + if err := o.getConvertibleFromDAO(ic.DAO, idKey, idList); err != nil { return err } if !idList.Remove(resp.ID) { @@ -193,7 +193,7 @@ func (o *Oracle) PostPersist(ic *interop.Context) error { if len(*idList) == 0 { err = ic.DAO.DeleteStorageItem(o.ID, idKey) } else { - err = ic.DAO.PutStorageItem(o.ID, idKey, idList.Bytes()) + err = putConvertibleToDAO(o.ID, ic.DAO, idKey, idList) } if err != nil { return err @@ -398,7 +398,7 @@ func (o *Oracle) RequestInternal(ic *interop.Context, url string, filter *string // PutRequestInternal puts oracle request with the specified id to d. func (o *Oracle) PutRequestInternal(id uint64, req *state.OracleRequest, d dao.DAO) error { reqKey := makeRequestKey(id) - if err := d.PutStorageItem(o.ID, reqKey, req.Bytes()); err != nil { + if err := putConvertibleToDAO(o.ID, d, reqKey, req); err != nil { return err } o.newRequests[id] = req @@ -406,14 +406,14 @@ func (o *Oracle) PutRequestInternal(id uint64, req *state.OracleRequest, d dao.D // Add request ID to the id list. lst := new(IDList) key := makeIDListKey(req.URL) - if err := o.getSerializableFromDAO(d, key, lst); err != nil && !errors.Is(err, storage.ErrKeyNotFound) { + if err := o.getConvertibleFromDAO(d, key, lst); err != nil && !errors.Is(err, storage.ErrKeyNotFound) { return err } if len(*lst) >= maxRequestsCount { return fmt.Errorf("there are too many pending requests for %s url", req.URL) } *lst = append(*lst, id) - return d.PutStorageItem(o.ID, key, lst.Bytes()) + return putConvertibleToDAO(o.ID, d, key, lst) } // GetScriptHash returns script hash or oracle nodes. @@ -431,14 +431,14 @@ func (o *Oracle) GetOracleNodes(d dao.DAO) (keys.PublicKeys, error) { func (o *Oracle) GetRequestInternal(d dao.DAO, id uint64) (*state.OracleRequest, error) { key := makeRequestKey(id) req := new(state.OracleRequest) - return req, o.getSerializableFromDAO(d, key, req) + return req, o.getConvertibleFromDAO(d, key, req) } // GetIDListInternal returns request by ID and key under which it is stored. func (o *Oracle) GetIDListInternal(d dao.DAO, url string) (*IDList, error) { key := makeIDListKey(url) idList := new(IDList) - return idList, o.getSerializableFromDAO(d, key, idList) + return idList, o.getConvertibleFromDAO(d, key, idList) } func (o *Oracle) verify(ic *interop.Context, _ []stackitem.Item) stackitem.Item { @@ -493,11 +493,10 @@ func (o *Oracle) getRequests(d dao.DAO) (map[uint64]*state.OracleRequest, error) if len(k) != 8 { return nil, errors.New("invalid request ID") } - r := io.NewBinReaderFromBuf(si) req := new(state.OracleRequest) - req.DecodeBinary(r) - if r.Err != nil { - return nil, r.Err + err = stackitem.DeserializeConvertible(si, req) + if err != nil { + return nil, err } id := binary.BigEndian.Uint64([]byte(k)) reqs[id] = req @@ -516,8 +515,8 @@ func makeIDListKey(url string) []byte { return append(prefixIDList, hash.Hash160([]byte(url)).BytesBE()...) } -func (o *Oracle) getSerializableFromDAO(d dao.DAO, key []byte, item io.Serializable) error { - return getSerializableFromDAO(o.ID, d, key, item) +func (o *Oracle) getConvertibleFromDAO(d dao.DAO, key []byte, item stackitem.Convertible) error { + return getConvertibleFromDAO(o.ID, d, key, item) } // updateCache updates cached Oracle values if they've been changed. diff --git a/pkg/core/native/oracle_types.go b/pkg/core/native/oracle_types.go index 985897e72..b678f67db 100644 --- a/pkg/core/native/oracle_types.go +++ b/pkg/core/native/oracle_types.go @@ -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" ) @@ -16,36 +15,17 @@ type IDList []uint64 // NodeList represents list or oracle nodes. type NodeList keys.PublicKeys -// Bytes return l serizalized to a byte-slice. -func (l IDList) Bytes() []byte { - w := io.NewBufBinWriter() - l.EncodeBinary(w.BinWriter) - return w.Bytes() -} - -// EncodeBinary implements io.Serializable. -func (l IDList) EncodeBinary(w *io.BinWriter) { - stackitem.EncodeBinary(l.toStackItem(), w) -} - -// DecodeBinary implements io.Serializable. -func (l *IDList) DecodeBinary(r *io.BinReader) { - item := stackitem.DecodeBinary(r) - if r.Err != nil || item == nil { - return - } - r.Err = l.fromStackItem(item) -} - -func (l IDList) toStackItem() stackitem.Item { +// ToStackItem implements stackitem.Convertible. It never returns an error. +func (l IDList) ToStackItem() (stackitem.Item, error) { arr := make([]stackitem.Item, len(l)) for i := range l { arr[i] = stackitem.NewBigInteger(new(big.Int).SetUint64(l[i])) } - return stackitem.NewArray(arr) + return stackitem.NewArray(arr), nil } -func (l *IDList) fromStackItem(it stackitem.Item) error { +// FromStackItem implements stackitem.Convertible. +func (l *IDList) FromStackItem(it stackitem.Item) error { arr, ok := it.Value().([]stackitem.Item) if !ok { return errors.New("not an array") @@ -75,36 +55,17 @@ func (l *IDList) Remove(id uint64) bool { return false } -// Bytes return l serizalized to a byte-slice. -func (l NodeList) Bytes() []byte { - w := io.NewBufBinWriter() - l.EncodeBinary(w.BinWriter) - return w.Bytes() -} - -// EncodeBinary implements io.Serializable. -func (l NodeList) EncodeBinary(w *io.BinWriter) { - stackitem.EncodeBinary(l.toStackItem(), w) -} - -// DecodeBinary implements io.Serializable. -func (l *NodeList) DecodeBinary(r *io.BinReader) { - item := stackitem.DecodeBinary(r) - if r.Err != nil || item == nil { - return - } - r.Err = l.fromStackItem(item) -} - -func (l NodeList) toStackItem() stackitem.Item { +// ToStackItem implements stackitem.Convertible. It never returns an error. +func (l NodeList) ToStackItem() (stackitem.Item, error) { arr := make([]stackitem.Item, len(l)) for i := range l { arr[i] = stackitem.NewByteArray(l[i].Bytes()) } - return stackitem.NewArray(arr) + return stackitem.NewArray(arr), nil } -func (l *NodeList) fromStackItem(it stackitem.Item) error { +// FromStackItem implements stackitem.Convertible. +func (l *NodeList) FromStackItem(it stackitem.Item) error { arr, ok := it.Value().([]stackitem.Item) if !ok { return errors.New("not an array") diff --git a/pkg/core/native/oracle_types_test.go b/pkg/core/native/oracle_types_test.go index c58323dd9..cba0e20b1 100644 --- a/pkg/core/native/oracle_types_test.go +++ b/pkg/core/native/oracle_types_test.go @@ -5,32 +5,26 @@ import ( "github.com/nspcc-dev/neo-go/internal/testserdes" "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/stretchr/testify/require" ) -func getInvalidTestFunc(actual io.Serializable, value interface{}) func(t *testing.T) { +func getInvalidTestFunc(actual stackitem.Convertible, value interface{}) func(t *testing.T) { return func(t *testing.T) { - w := io.NewBufBinWriter() it := stackitem.Make(value) - stackitem.EncodeBinary(it, w.BinWriter) - require.NoError(t, w.Err) - require.Error(t, testserdes.DecodeBinary(w.Bytes(), actual)) + require.Error(t, actual.FromStackItem(it)) } } -func TestIDList_EncodeBinary(t *testing.T) { +func TestIDListToFromSI(t *testing.T) { t.Run("Valid", func(t *testing.T) { l := &IDList{1, 4, 5} - testserdes.EncodeDecodeBinary(t, l, new(IDList)) + var l2 = new(IDList) + testserdes.ToFromStackItem(t, l, l2) }) t.Run("Invalid", func(t *testing.T) { t.Run("NotArray", getInvalidTestFunc(new(IDList), []byte{})) t.Run("InvalidElement", getInvalidTestFunc(new(IDList), []stackitem.Item{stackitem.Null{}})) - t.Run("NotStackItem", func(t *testing.T) { - require.Error(t, testserdes.DecodeBinary([]byte{0x77}, new(IDList))) - }) }) } @@ -50,22 +44,20 @@ func TestIDList_Remove(t *testing.T) { require.Equal(t, IDList{1}, l) } -func TestNodeList_EncodeBinary(t *testing.T) { +func TestNodeListToFromSI(t *testing.T) { priv, err := keys.NewPrivateKey() require.NoError(t, err) pub := priv.PublicKey() t.Run("Valid", func(t *testing.T) { l := &NodeList{pub} - testserdes.EncodeDecodeBinary(t, l, new(NodeList)) + var l2 = new(NodeList) + testserdes.ToFromStackItem(t, l, l2) }) t.Run("Invalid", func(t *testing.T) { t.Run("NotArray", getInvalidTestFunc(new(NodeList), []byte{})) t.Run("InvalidElement", getInvalidTestFunc(new(NodeList), []stackitem.Item{stackitem.Null{}})) t.Run("InvalidKey", getInvalidTestFunc(new(NodeList), []stackitem.Item{stackitem.NewByteArray([]byte{0x9})})) - t.Run("NotStackItem", func(t *testing.T) { - require.Error(t, testserdes.DecodeBinary([]byte{0x77}, new(NodeList))) - }) }) } diff --git a/pkg/core/native/util.go b/pkg/core/native/util.go index c882762cb..afb20728c 100644 --- a/pkg/core/native/util.go +++ b/pkg/core/native/util.go @@ -8,30 +8,26 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/dao" "github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/encoding/bigint" - "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" ) var intOne = big.NewInt(1) -func getSerializableFromDAO(id int32, d dao.DAO, key []byte, item io.Serializable) error { +func getConvertibleFromDAO(id int32, d dao.DAO, key []byte, conv stackitem.Convertible) error { si := d.GetStorageItem(id, key) if si == nil { return storage.ErrKeyNotFound } - r := io.NewBinReaderFromBuf(si) - item.DecodeBinary(r) - return r.Err + return stackitem.DeserializeConvertible(si, conv) } -func putSerializableToDAO(id int32, d dao.DAO, key []byte, item io.Serializable) error { - w := io.NewBufBinWriter() - item.EncodeBinary(w.BinWriter) - if w.Err != nil { - return w.Err +func putConvertibleToDAO(id int32, d dao.DAO, key []byte, conv stackitem.Convertible) error { + data, err := stackitem.SerializeConvertible(conv) + if err != nil { + return err } - return d.PutStorageItem(id, key, w.Bytes()) + return d.PutStorageItem(id, key, data) } func setIntWithKey(id int32, dao dao.DAO, key []byte, value int64) error { diff --git a/pkg/core/state/contract.go b/pkg/core/state/contract.go index e6ee4e873..b5a526cbc 100644 --- a/pkg/core/state/contract.go +++ b/pkg/core/state/contract.go @@ -35,25 +35,6 @@ type NativeContract struct { UpdateHistory []uint32 `json:"updatehistory"` } -// DecodeBinary implements Serializable interface. -func (c *Contract) DecodeBinary(r *io.BinReader) { - si := stackitem.DecodeBinary(r) - if r.Err != nil { - return - } - r.Err = c.FromStackItem(si) -} - -// EncodeBinary implements Serializable interface. -func (c *Contract) EncodeBinary(w *io.BinWriter) { - si, err := c.ToStackItem() - if err != nil { - w.Err = err - return - } - stackitem.EncodeBinary(si, w) -} - // ToStackItem converts state.Contract to stackitem.Item. func (c *Contract) ToStackItem() (stackitem.Item, error) { rawNef, err := c.NEF.Bytes() diff --git a/pkg/core/state/contract_test.go b/pkg/core/state/contract_test.go index d2c7987ec..3f872974a 100644 --- a/pkg/core/state/contract_test.go +++ b/pkg/core/state/contract_test.go @@ -14,7 +14,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestEncodeDecodeContractState(t *testing.T) { +func TestContractStateToFromSI(t *testing.T) { script := []byte("testscript") h := hash.Hash160(script) @@ -52,9 +52,9 @@ func TestEncodeDecodeContractState(t *testing.T) { } contract.NEF.Checksum = contract.NEF.CalculateChecksum() - t.Run("Serializable", func(t *testing.T) { + t.Run("Convertible", func(t *testing.T) { contractDecoded := new(Contract) - testserdes.EncodeDecodeBinary(t, contract, contractDecoded) + testserdes.ToFromStackItem(t, contract, contractDecoded) }) t.Run("JSON", func(t *testing.T) { contractDecoded := new(Contract) diff --git a/pkg/core/state/deposit.go b/pkg/core/state/deposit.go index 879b562b3..3efd76d7a 100644 --- a/pkg/core/state/deposit.go +++ b/pkg/core/state/deposit.go @@ -6,7 +6,6 @@ import ( "math" "math/big" - "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" ) @@ -16,28 +15,17 @@ type Deposit struct { Till uint32 } -// EncodeBinary implements io.Serializable interface. -func (d *Deposit) EncodeBinary(w *io.BinWriter) { - stackitem.EncodeBinary(d.toStackItem(), w) -} - -// DecodeBinary implements io.Serializable interface. -func (d *Deposit) DecodeBinary(r *io.BinReader) { - si := stackitem.DecodeBinary(r) - if r.Err != nil { - return - } - r.Err = d.fromStackItem(si) -} - -func (d *Deposit) toStackItem() stackitem.Item { +// ToStackItem implements stackitem.Convertible interface. It never returns an +// error. +func (d *Deposit) ToStackItem() (stackitem.Item, error) { return stackitem.NewStruct([]stackitem.Item{ stackitem.NewBigInteger(d.Amount), stackitem.Make(d.Till), - }) + }), nil } -func (d *Deposit) fromStackItem(it stackitem.Item) error { +// FromStackItem implements stackitem.Convertible interface. +func (d *Deposit) FromStackItem(it stackitem.Item) error { items, ok := it.Value().([]stackitem.Item) if !ok { return errors.New("not a struct") diff --git a/pkg/core/state/deposit_test.go b/pkg/core/state/deposit_test.go index e928bc3a7..0b19052e6 100644 --- a/pkg/core/state/deposit_test.go +++ b/pkg/core/state/deposit_test.go @@ -11,49 +11,44 @@ import ( func TestEncodeDecodeDeposit(t *testing.T) { d := &Deposit{Amount: big.NewInt(100500), Till: 888} - depo := new(Deposit) - testserdes.EncodeDecodeBinary(t, d, depo) - item := stackitem.Make(42) - data, err := stackitem.Serialize(item) - require.NoError(t, err) - require.Error(t, testserdes.DecodeBinary(data, depo)) + testserdes.ToFromStackItem(t, d, depo) } func TestDepositFromStackItem(t *testing.T) { var d Deposit item := stackitem.Make(42) - require.Error(t, d.fromStackItem(item)) + require.Error(t, d.FromStackItem(item)) item = stackitem.NewStruct(nil) - require.Error(t, d.fromStackItem(item)) + require.Error(t, d.FromStackItem(item)) item = stackitem.NewStruct([]stackitem.Item{ stackitem.NewStruct(nil), stackitem.NewStruct(nil), }) - require.Error(t, d.fromStackItem(item)) + require.Error(t, d.FromStackItem(item)) item = stackitem.NewStruct([]stackitem.Item{ stackitem.Make(777), stackitem.NewStruct(nil), }) - require.Error(t, d.fromStackItem(item)) + require.Error(t, d.FromStackItem(item)) item = stackitem.NewStruct([]stackitem.Item{ stackitem.Make(777), stackitem.Make(-1), }) - require.Error(t, d.fromStackItem(item)) + require.Error(t, d.FromStackItem(item)) item = stackitem.NewStruct([]stackitem.Item{ stackitem.Make(777), stackitem.Make("somenonu64value"), }) - require.Error(t, d.fromStackItem(item)) + require.Error(t, d.FromStackItem(item)) item = stackitem.NewStruct([]stackitem.Item{ stackitem.Make(777), stackitem.Make(888), }) - require.NoError(t, d.fromStackItem(item)) + require.NoError(t, d.FromStackItem(item)) } diff --git a/pkg/core/state/native_state.go b/pkg/core/state/native_state.go index 75baee89d..a4f652e3f 100644 --- a/pkg/core/state/native_state.go +++ b/pkg/core/state/native_state.go @@ -7,7 +7,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" ) @@ -26,99 +25,81 @@ type NEOBalanceState struct { // NEP17BalanceStateFromBytes converts serialized NEP17BalanceState to structure. func NEP17BalanceStateFromBytes(b []byte) (*NEP17BalanceState, error) { balance := new(NEP17BalanceState) - if len(b) == 0 { - return balance, nil - } - r := io.NewBinReaderFromBuf(b) - balance.DecodeBinary(r) - if r.Err != nil { - return nil, r.Err + err := balanceFromBytes(b, balance) + if err != nil { + return nil, err } return balance, nil } // Bytes returns serialized NEP17BalanceState. func (s *NEP17BalanceState) Bytes() []byte { - w := io.NewBufBinWriter() - s.EncodeBinary(w.BinWriter) - if w.Err != nil { - panic(w.Err) + return balanceToBytes(s) +} + +func balanceFromBytes(b []byte, item stackitem.Convertible) error { + if len(b) == 0 { + return nil } - return w.Bytes() + return stackitem.DeserializeConvertible(b, item) } -func (s *NEP17BalanceState) toStackItem() stackitem.Item { - return stackitem.NewStruct([]stackitem.Item{stackitem.NewBigInteger(&s.Balance)}) -} - -func (s *NEP17BalanceState) fromStackItem(item stackitem.Item) { - s.Balance = *item.(*stackitem.Struct).Value().([]stackitem.Item)[0].Value().(*big.Int) -} - -// EncodeBinary implements io.Serializable interface. -func (s *NEP17BalanceState) EncodeBinary(w *io.BinWriter) { - si := s.toStackItem() - stackitem.EncodeBinary(si, w) -} - -// DecodeBinary implements io.Serializable interface. -func (s *NEP17BalanceState) DecodeBinary(r *io.BinReader) { - si := stackitem.DecodeBinary(r) - if r.Err != nil { - return +func balanceToBytes(item stackitem.Convertible) []byte { + data, err := stackitem.SerializeConvertible(item) + if err != nil { + panic(err) } - s.fromStackItem(si) + return data +} + +// ToStackItem implements stackitem.Convertible. It never returns an error. +func (s *NEP17BalanceState) ToStackItem() (stackitem.Item, error) { + return stackitem.NewStruct([]stackitem.Item{stackitem.NewBigInteger(&s.Balance)}), nil +} + +// FromStackItem implements stackitem.Convertible. +func (s *NEP17BalanceState) FromStackItem(item stackitem.Item) error { + items, ok := item.Value().([]stackitem.Item) + if !ok { + return errors.New("not a struct") + } + if len(items) < 1 { + return errors.New("no balance value") + } + balance, err := items[0].TryInteger() + if err != nil { + return fmt.Errorf("invalid balance: %w", err) + } + s.Balance = *balance + return nil } // NEOBalanceStateFromBytes converts serialized NEOBalanceState to structure. func NEOBalanceStateFromBytes(b []byte) (*NEOBalanceState, error) { balance := new(NEOBalanceState) - if len(b) == 0 { - return balance, nil - } - r := io.NewBinReaderFromBuf(b) - balance.DecodeBinary(r) - - if r.Err != nil { - return nil, r.Err + err := balanceFromBytes(b, balance) + if err != nil { + return nil, err } return balance, nil } // Bytes returns serialized NEOBalanceState. func (s *NEOBalanceState) Bytes() []byte { - w := io.NewBufBinWriter() - s.EncodeBinary(w.BinWriter) - if w.Err != nil { - panic(w.Err) - } - return w.Bytes() + return balanceToBytes(s) } -// EncodeBinary implements io.Serializable interface. -func (s *NEOBalanceState) EncodeBinary(w *io.BinWriter) { - si := s.toStackItem() - stackitem.EncodeBinary(si, w) -} - -// DecodeBinary implements io.Serializable interface. -func (s *NEOBalanceState) DecodeBinary(r *io.BinReader) { - si := stackitem.DecodeBinary(r) - if r.Err != nil { - return - } - r.Err = s.FromStackItem(si) -} - -func (s *NEOBalanceState) toStackItem() stackitem.Item { - result := s.NEP17BalanceState.toStackItem().(*stackitem.Struct) +// ToStackItem implements stackitem.Convertible interface. It never returns an error. +func (s *NEOBalanceState) ToStackItem() (stackitem.Item, error) { + resItem, _ := s.NEP17BalanceState.ToStackItem() + result := resItem.(*stackitem.Struct) result.Append(stackitem.NewBigInteger(big.NewInt(int64(s.BalanceHeight)))) if s.VoteTo != nil { result.Append(stackitem.NewByteArray(s.VoteTo.Bytes())) } else { result.Append(stackitem.Null{}) } - return result + return result, nil } // FromStackItem converts stackitem.Item to NEOBalanceState. diff --git a/pkg/core/state/oracle.go b/pkg/core/state/oracle.go index 96efde901..861a5a1db 100644 --- a/pkg/core/state/oracle.go +++ b/pkg/core/state/oracle.go @@ -5,7 +5,6 @@ import ( "math/big" "unicode/utf8" - "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" ) @@ -21,28 +20,9 @@ type OracleRequest struct { UserData []byte } -// Bytes return o serizalized to a byte-slice. -func (o *OracleRequest) Bytes() []byte { - w := io.NewBufBinWriter() - o.EncodeBinary(w.BinWriter) - return w.Bytes() -} - -// EncodeBinary implements io.Serializable. -func (o *OracleRequest) EncodeBinary(w *io.BinWriter) { - stackitem.EncodeBinary(o.toStackItem(), w) -} - -// DecodeBinary implements io.Serializable. -func (o *OracleRequest) DecodeBinary(r *io.BinReader) { - item := stackitem.DecodeBinary(r) - if r.Err != nil || item == nil { - return - } - r.Err = o.fromStackItem(item) -} - -func (o *OracleRequest) toStackItem() stackitem.Item { +// ToStackItem implements stackitem.Convertible interface. It never returns an +// error. +func (o *OracleRequest) ToStackItem() (stackitem.Item, error) { filter := stackitem.Item(stackitem.Null{}) if o.Filter != nil { filter = stackitem.Make(*o.Filter) @@ -55,10 +35,11 @@ func (o *OracleRequest) toStackItem() stackitem.Item { stackitem.NewByteArray(o.CallbackContract.BytesBE()), stackitem.Make(o.CallbackMethod), stackitem.NewByteArray(o.UserData), - }) + }), nil } -func (o *OracleRequest) fromStackItem(it stackitem.Item) error { +// FromStackItem implements stackitem.Convertible interface. +func (o *OracleRequest) FromStackItem(it stackitem.Item) error { arr, ok := it.Value().([]stackitem.Item) if !ok || len(arr) < 7 { return errors.New("not an array of needed length") diff --git a/pkg/core/state/oracle_test.go b/pkg/core/state/oracle_test.go index bb17ee6bc..07e2ce1f6 100644 --- a/pkg/core/state/oracle_test.go +++ b/pkg/core/state/oracle_test.go @@ -6,12 +6,11 @@ import ( "github.com/nspcc-dev/neo-go/internal/random" "github.com/nspcc-dev/neo-go/internal/testserdes" - "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/stretchr/testify/require" ) -func TestOracleRequest_EncodeBinary(t *testing.T) { +func TestOracleRequestToFromSI(t *testing.T) { t.Run("Valid", func(t *testing.T) { r := &OracleRequest{ OriginalTxID: random.Uint256(), @@ -21,25 +20,19 @@ func TestOracleRequest_EncodeBinary(t *testing.T) { CallbackMethod: "method", UserData: []byte{1, 2, 3}, } - testserdes.EncodeDecodeBinary(t, r, new(OracleRequest)) + testserdes.ToFromStackItem(t, r, new(OracleRequest)) t.Run("WithFilter", func(t *testing.T) { s := "filter" r.Filter = &s - testserdes.EncodeDecodeBinary(t, r, new(OracleRequest)) + testserdes.ToFromStackItem(t, r, new(OracleRequest)) }) }) t.Run("Invalid", func(t *testing.T) { - w := io.NewBufBinWriter() + var res = new(OracleRequest) t.Run("NotArray", func(t *testing.T) { - w.Reset() it := stackitem.NewByteArray([]byte{}) - stackitem.EncodeBinary(it, w.BinWriter) - require.Error(t, testserdes.DecodeBinary(w.Bytes(), new(OracleRequest))) - }) - t.Run("NotStackItem", func(t *testing.T) { - w.Reset() - require.Error(t, testserdes.DecodeBinary([]byte{0x77}, new(OracleRequest))) + require.Error(t, res.FromStackItem(it)) }) items := []stackitem.Item{ @@ -54,12 +47,10 @@ func TestOracleRequest_EncodeBinary(t *testing.T) { arrItem := stackitem.NewArray(items) runInvalid := func(i int, elem stackitem.Item) func(t *testing.T) { return func(t *testing.T) { - w.Reset() before := items[i] items[i] = elem - stackitem.EncodeBinary(arrItem, w.BinWriter) + require.Error(t, res.FromStackItem(arrItem)) items[i] = before - require.Error(t, testserdes.DecodeBinary(w.Bytes(), new(OracleRequest))) } } t.Run("TxID", func(t *testing.T) { diff --git a/pkg/util/uint160_test.go b/pkg/util/uint160_test.go index 7445242b1..f6bc855a2 100644 --- a/pkg/util/uint160_test.go +++ b/pkg/util/uint160_test.go @@ -1,21 +1,22 @@ -package util +package util_test import ( "encoding/hex" "testing" "github.com/nspcc-dev/neo-go/internal/testserdes" + "github.com/nspcc-dev/neo-go/pkg/util" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestUint160UnmarshalJSON(t *testing.T) { str := "0263c1de100292813b5e075e585acc1bae963b2d" - expected, err := Uint160DecodeStringLE(str) + expected, err := util.Uint160DecodeStringLE(str) assert.NoError(t, err) // UnmarshalJSON decodes hex-strings - var u1, u2 Uint160 + var u1, u2 util.Uint160 assert.NoError(t, u1.UnmarshalJSON([]byte(`"`+str+`"`))) assert.True(t, expected.Equals(u1)) @@ -27,25 +28,25 @@ func TestUint160UnmarshalJSON(t *testing.T) { func TestUInt160DecodeString(t *testing.T) { hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302" - val, err := Uint160DecodeStringBE(hexStr) + val, err := util.Uint160DecodeStringBE(hexStr) assert.NoError(t, err) assert.Equal(t, hexStr, val.String()) - valLE, err := Uint160DecodeStringLE(hexStr) + valLE, err := util.Uint160DecodeStringLE(hexStr) assert.NoError(t, err) assert.Equal(t, val, valLE.Reverse()) - _, err = Uint160DecodeStringBE(hexStr[1:]) + _, err = util.Uint160DecodeStringBE(hexStr[1:]) assert.Error(t, err) - _, err = Uint160DecodeStringLE(hexStr[1:]) + _, err = util.Uint160DecodeStringLE(hexStr[1:]) assert.Error(t, err) hexStr = "zz3b96ae1bcc5a585e075e3b81920210dec16302" - _, err = Uint160DecodeStringBE(hexStr) + _, err = util.Uint160DecodeStringBE(hexStr) assert.Error(t, err) - _, err = Uint160DecodeStringLE(hexStr) + _, err = util.Uint160DecodeStringLE(hexStr) assert.Error(t, err) } @@ -54,18 +55,18 @@ func TestUint160DecodeBytes(t *testing.T) { b, err := hex.DecodeString(hexStr) require.NoError(t, err) - val, err := Uint160DecodeBytesBE(b) + val, err := util.Uint160DecodeBytesBE(b) assert.NoError(t, err) assert.Equal(t, hexStr, val.String()) - valLE, err := Uint160DecodeBytesLE(b) + valLE, err := util.Uint160DecodeBytesLE(b) assert.NoError(t, err) assert.Equal(t, val, valLE.Reverse()) - _, err = Uint160DecodeBytesLE(b[1:]) + _, err = util.Uint160DecodeBytesLE(b[1:]) assert.Error(t, err) - _, err = Uint160DecodeBytesBE(b[1:]) + _, err = util.Uint160DecodeBytesBE(b[1:]) assert.Error(t, err) } @@ -73,10 +74,10 @@ func TestUInt160Equals(t *testing.T) { a := "2d3b96ae1bcc5a585e075e3b81920210dec16302" b := "4d3b96ae1bcc5a585e075e3b81920210dec16302" - ua, err := Uint160DecodeStringBE(a) + ua, err := util.Uint160DecodeStringBE(a) require.NoError(t, err) - ub, err := Uint160DecodeStringBE(b) + ub, err := util.Uint160DecodeStringBE(b) require.NoError(t, err) assert.False(t, ua.Equals(ub), "%s and %s cannot be equal", ua, ub) assert.True(t, ua.Equals(ua), "%s and %s must be equal", ua, ua) @@ -86,11 +87,11 @@ func TestUInt160Less(t *testing.T) { a := "2d3b96ae1bcc5a585e075e3b81920210dec16302" b := "2d3b96ae1bcc5a585e075e3b81920210dec16303" - ua, err := Uint160DecodeStringBE(a) + ua, err := util.Uint160DecodeStringBE(a) assert.Nil(t, err) - ua2, err := Uint160DecodeStringBE(a) + ua2, err := util.Uint160DecodeStringBE(a) assert.Nil(t, err) - ub, err := Uint160DecodeStringBE(b) + ub, err := util.Uint160DecodeStringBE(b) assert.Nil(t, err) assert.Equal(t, true, ua.Less(ub)) assert.Equal(t, false, ua.Less(ua2)) @@ -101,7 +102,7 @@ func TestUInt160String(t *testing.T) { hexStr := "b28427088a3729b2536d10122960394e8be6721f" hexRevStr := "1f72e68b4e39602912106d53b229378a082784b2" - val, err := Uint160DecodeStringBE(hexStr) + val, err := util.Uint160DecodeStringBE(hexStr) assert.Nil(t, err) assert.Equal(t, hexStr, val.String()) @@ -110,7 +111,7 @@ func TestUInt160String(t *testing.T) { func TestUint160_Reverse(t *testing.T) { hexStr := "b28427088a3729b2536d10122960394e8be6721f" - val, err := Uint160DecodeStringBE(hexStr) + val, err := util.Uint160DecodeStringBE(hexStr) require.NoError(t, err) assert.Equal(t, hexStr, val.Reverse().StringLE()) diff --git a/pkg/util/uint256_test.go b/pkg/util/uint256_test.go index e0e3d2291..b1f5d2c3e 100644 --- a/pkg/util/uint256_test.go +++ b/pkg/util/uint256_test.go @@ -1,21 +1,22 @@ -package util +package util_test import ( "encoding/hex" "testing" "github.com/nspcc-dev/neo-go/internal/testserdes" + "github.com/nspcc-dev/neo-go/pkg/util" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestUint256UnmarshalJSON(t *testing.T) { str := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d" - expected, err := Uint256DecodeStringLE(str) + expected, err := util.Uint256DecodeStringLE(str) require.NoError(t, err) // UnmarshalJSON decodes hex-strings - var u1, u2 Uint256 + var u1, u2 util.Uint256 require.NoError(t, u1.UnmarshalJSON([]byte(`"`+str+`"`))) assert.True(t, expected.Equals(u1)) @@ -28,33 +29,33 @@ func TestUint256UnmarshalJSON(t *testing.T) { func TestUint256DecodeString(t *testing.T) { hexStr := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d" - val, err := Uint256DecodeStringLE(hexStr) + val, err := util.Uint256DecodeStringLE(hexStr) require.NoError(t, err) assert.Equal(t, hexStr, val.StringLE()) - valBE, err := Uint256DecodeStringBE(hexStr) + valBE, err := util.Uint256DecodeStringBE(hexStr) require.NoError(t, err) assert.Equal(t, val, valBE.Reverse()) bs, err := hex.DecodeString(hexStr) require.NoError(t, err) - val1, err := Uint256DecodeBytesBE(bs) + val1, err := util.Uint256DecodeBytesBE(bs) assert.NoError(t, err) assert.Equal(t, hexStr, val1.String()) assert.Equal(t, val, val1.Reverse()) - _, err = Uint256DecodeStringLE(hexStr[1:]) + _, err = util.Uint256DecodeStringLE(hexStr[1:]) assert.Error(t, err) - _, err = Uint256DecodeStringBE(hexStr[1:]) + _, err = util.Uint256DecodeStringBE(hexStr[1:]) assert.Error(t, err) hexStr = "zzz7308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d" - _, err = Uint256DecodeStringLE(hexStr) + _, err = util.Uint256DecodeStringLE(hexStr) assert.Error(t, err) - _, err = Uint256DecodeStringBE(hexStr) + _, err = util.Uint256DecodeStringBE(hexStr) assert.Error(t, err) } @@ -63,11 +64,11 @@ func TestUint256DecodeBytes(t *testing.T) { b, err := hex.DecodeString(hexStr) require.NoError(t, err) - val, err := Uint256DecodeBytesLE(b) + val, err := util.Uint256DecodeBytesLE(b) require.NoError(t, err) assert.Equal(t, hexStr, val.StringLE()) - _, err = Uint256DecodeBytesBE(b[1:]) + _, err = util.Uint256DecodeBytesBE(b[1:]) assert.Error(t, err) } @@ -75,10 +76,10 @@ func TestUInt256Equals(t *testing.T) { a := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d" b := "e287c5b29a1b66092be6803c59c765308ac20287e1b4977fd399da5fc8f66ab5" - ua, err := Uint256DecodeStringLE(a) + ua, err := util.Uint256DecodeStringLE(a) require.NoError(t, err) - ub, err := Uint256DecodeStringLE(b) + ub, err := util.Uint256DecodeStringLE(b) require.NoError(t, err) assert.False(t, ua.Equals(ub), "%s and %s cannot be equal", ua, ub) assert.True(t, ua.Equals(ua), "%s and %s must be equal", ua, ua) @@ -86,11 +87,11 @@ func TestUInt256Equals(t *testing.T) { } func TestUint256_Serializable(t *testing.T) { - a := Uint256{ + a := util.Uint256{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, } - var b Uint256 + var b util.Uint256 testserdes.EncodeDecodeBinary(t, &a, &b) } diff --git a/pkg/vm/stackitem/item.go b/pkg/vm/stackitem/item.go index 4db99c9fc..1b84f4e51 100644 --- a/pkg/vm/stackitem/item.go +++ b/pkg/vm/stackitem/item.go @@ -52,6 +52,12 @@ type Item interface { Convert(Type) (Item, error) } +// Convertible is something that can be converted to/from Item. +type Convertible interface { + ToStackItem() (Item, error) + FromStackItem(Item) error +} + var ( // ErrInvalidConversion is returned on attempt to make an incorrect // conversion between item types. diff --git a/pkg/vm/stackitem/serialization.go b/pkg/vm/stackitem/serialization.go index 67929e7eb..a4271a4cc 100644 --- a/pkg/vm/stackitem/serialization.go +++ b/pkg/vm/stackitem/serialization.go @@ -254,3 +254,21 @@ func decodeBinary(r *io.BinReader, allowInvalid bool) Item { return nil } } + +// SerializeConvertible serializes Convertible into a slice of bytes. +func SerializeConvertible(conv Convertible) ([]byte, error) { + item, err := conv.ToStackItem() + if err != nil { + return nil, err + } + return Serialize(item) +} + +// DeserializeConvertible deserializes Convertible from a slice of bytes. +func DeserializeConvertible(data []byte, conv Convertible) error { + item, err := Deserialize(data) + if err != nil { + return err + } + return conv.FromStackItem(item) +}