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.
This commit is contained in:
Roman Khimov 2021-07-17 18:37:33 +03:00
parent 2d993d0da5
commit aab18c3083
23 changed files with 223 additions and 339 deletions

View file

@ -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()