testserdes: implement helpers for encode/decode routines

Frequently one needs to check if struct serializes/deserializes
properly. This commit implements helpers for such cases including:
1. JSON
2. io.Serializable interface
This commit is contained in:
Evgenii Stratonikov 2020-03-26 17:43:24 +03:00
parent d4622768d1
commit 9abda40171
36 changed files with 201 additions and 476 deletions

View file

@ -10,6 +10,7 @@ import (
"github.com/nspcc-dev/dbft/payload" "github.com/nspcc-dev/dbft/payload"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/nspcc-dev/neo-go/pkg/vm/opcode"
@ -78,19 +79,15 @@ func TestConsensusPayload_Hash(t *testing.T) {
data, err := hex.DecodeString(dataHex) data, err := hex.DecodeString(dataHex)
require.NoError(t, err) require.NoError(t, err)
r := io.NewBinReaderFromBuf(data)
var p Payload var p Payload
p.DecodeBinary(r) require.NoError(t, testserdes.DecodeBinary(data, &p))
require.NoError(t, err)
require.Equal(t, p.Hash().String(), "45859759c8491597804f1922773947e0d37bf54484af82f80cd642f7b063aa56") require.Equal(t, p.Hash().String(), "45859759c8491597804f1922773947e0d37bf54484af82f80cd642f7b063aa56")
} }
func TestConsensusPayload_Serializable(t *testing.T) { func TestConsensusPayload_Serializable(t *testing.T) {
for _, mt := range messageTypes { for _, mt := range messageTypes {
p := randomPayload(t, mt) p := randomPayload(t, mt)
testSerializable(t, p, new(Payload)) testserdes.EncodeDecodeBinary(t, p, new(Payload))
data := p.MarshalUnsigned() data := p.MarshalUnsigned()
pu := new(Payload) pu := new(Payload)
@ -133,57 +130,49 @@ func TestConsensusPayload_DecodeBinaryInvalid(t *testing.T) {
buf[delimeterIndex] = 1 buf[delimeterIndex] = 1
buf[lenIndex] = 34 buf[lenIndex] = 34
buf[typeIndex] = byte(prepareResponseType) buf[typeIndex] = byte(prepareResponseType)
r := io.NewBinReaderFromBuf(buf)
p := new(Payload) p := new(Payload)
p.DecodeBinary(r) require.NoError(t, testserdes.DecodeBinary(buf, p))
require.NoError(t, r.Err)
require.Equal(t, expected, p) require.Equal(t, expected, p)
// invalid type // invalid type
buf[typeIndex] = 0xFF buf[typeIndex] = 0xFF
r = io.NewBinReaderFromBuf(buf) require.Error(t, testserdes.DecodeBinary(buf, new(Payload)))
new(Payload).DecodeBinary(r)
require.Error(t, r.Err)
// invalid format // invalid format
buf[delimeterIndex] = 0 buf[delimeterIndex] = 0
buf[typeIndex] = byte(prepareResponseType) buf[typeIndex] = byte(prepareResponseType)
r = io.NewBinReaderFromBuf(buf) require.Error(t, testserdes.DecodeBinary(buf, new(Payload)))
new(Payload).DecodeBinary(r)
require.Error(t, r.Err)
// invalid message length // invalid message length
buf[delimeterIndex] = 1 buf[delimeterIndex] = 1
buf[lenIndex] = 0xFF buf[lenIndex] = 0xFF
buf[typeIndex] = byte(prepareResponseType) buf[typeIndex] = byte(prepareResponseType)
r = io.NewBinReaderFromBuf(buf) require.Error(t, testserdes.DecodeBinary(buf, new(Payload)))
new(Payload).DecodeBinary(r)
require.Error(t, r.Err)
} }
func TestCommit_Serializable(t *testing.T) { func TestCommit_Serializable(t *testing.T) {
c := randomMessage(t, commitType) c := randomMessage(t, commitType)
testSerializable(t, c, new(commit)) testserdes.EncodeDecodeBinary(t, c, new(commit))
} }
func TestPrepareResponse_Serializable(t *testing.T) { func TestPrepareResponse_Serializable(t *testing.T) {
resp := randomMessage(t, prepareResponseType) resp := randomMessage(t, prepareResponseType)
testSerializable(t, resp, new(prepareResponse)) testserdes.EncodeDecodeBinary(t, resp, new(prepareResponse))
} }
func TestPrepareRequest_Serializable(t *testing.T) { func TestPrepareRequest_Serializable(t *testing.T) {
req := randomMessage(t, prepareRequestType) req := randomMessage(t, prepareRequestType)
testSerializable(t, req, new(prepareRequest)) testserdes.EncodeDecodeBinary(t, req, new(prepareRequest))
} }
func TestRecoveryRequest_Serializable(t *testing.T) { func TestRecoveryRequest_Serializable(t *testing.T) {
req := randomMessage(t, recoveryRequestType) req := randomMessage(t, recoveryRequestType)
testSerializable(t, req, new(recoveryRequest)) testserdes.EncodeDecodeBinary(t, req, new(recoveryRequest))
} }
func TestRecoveryMessage_Serializable(t *testing.T) { func TestRecoveryMessage_Serializable(t *testing.T) {
msg := randomMessage(t, recoveryMessageType) msg := randomMessage(t, recoveryMessageType)
testSerializable(t, msg, new(recoveryMessage)) testserdes.EncodeDecodeBinary(t, msg, new(recoveryMessage))
} }
func randomPayload(t *testing.T, mt messageType) *Payload { func randomPayload(t *testing.T, mt messageType) *Payload {
@ -318,16 +307,6 @@ func TestMessageType_String(t *testing.T) {
require.Equal(t, "UNKNOWN(0xff)", messageType(0xff).String()) require.Equal(t, "UNKNOWN(0xff)", messageType(0xff).String())
} }
func testSerializable(t *testing.T, expected, actual io.Serializable) {
w := io.NewBufBinWriter()
expected.EncodeBinary(w.BinWriter)
r := io.NewBinReaderFromBuf(w.Bytes())
actual.DecodeBinary(r)
require.Equal(t, expected, actual)
}
func fillRandom(t *testing.T, buf []byte) []byte { func fillRandom(t *testing.T, buf []byte) []byte {
r := rand.New(rand.NewSource(time.Now().Unix())) r := rand.New(rand.NewSource(time.Now().Unix()))
_, err := gio.ReadFull(r, buf) _, err := gio.ReadFull(r, buf)

View file

@ -7,6 +7,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -22,9 +23,7 @@ func TestDecodeBlock1(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
block := &Block{} block := &Block{}
r := io.NewBinReaderFromBuf(b) assert.NoError(t, testserdes.DecodeBinary(b, block))
block.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, uint32(data["index"].(float64)), block.Index) assert.Equal(t, uint32(data["index"].(float64)), block.Index)
assert.Equal(t, uint32(data["version"].(float64)), block.Version) assert.Equal(t, uint32(data["version"].(float64)), block.Version)
@ -132,9 +131,7 @@ func TestBinBlockDecodeEncode(t *testing.T) {
b := Block{} b := Block{}
r := io.NewBinReaderFromBuf(rawtxBytes) assert.NoError(t, testserdes.DecodeBinary(rawtxBytes, &b))
b.DecodeBinary(r)
assert.Nil(t, r.Err)
expected := map[string]bool{ // 18 trans expected := map[string]bool{ // 18 trans
"009f61f481f47eb7478e887871e4e744669d461b13d68e04250035260171d706": false, "009f61f481f47eb7478e887871e4e744669d461b13d68e04250035260171d706": false,
@ -188,12 +185,9 @@ func TestBinBlockDecodeEncode(t *testing.T) {
} }
assert.Equal(t, true, val) assert.Equal(t, true, val)
buf := io.NewBufBinWriter() data, err := testserdes.EncodeBinary(&b)
assert.NoError(t, err)
b.EncodeBinary(buf.BinWriter) assert.Equal(t, rawtx, hex.EncodeToString(data))
assert.Nil(t, buf.Err)
assert.Equal(t, rawtx, hex.EncodeToString(buf.Bytes()))
} }
func TestBlockSizeCalculation(t *testing.T) { func TestBlockSizeCalculation(t *testing.T) {
@ -207,10 +201,7 @@ func TestBlockSizeCalculation(t *testing.T) {
rawBlockBytes, _ := hex.DecodeString(rawBlock) rawBlockBytes, _ := hex.DecodeString(rawBlock)
b := Block{} b := Block{}
assert.NoError(t, testserdes.DecodeBinary(rawBlockBytes, &b))
r := io.NewBinReaderFromBuf(rawBlockBytes)
b.DecodeBinary(r)
assert.Nil(t, r.Err)
expected := []struct { expected := []struct {
ID string ID string
@ -273,11 +264,8 @@ func TestBlockSizeCalculation(t *testing.T) {
assert.Equal(t, "552102486fd15702c4490a26703112a5cc1d0923fd697a33406bd5a1c00e0013b09a7021024c7b7fb6c310fccf1ba33b082519d82964ea93868d676662d4a59ad548df0e7d2102aaec38470f6aad0042c6e877cfd8087d2676b0f516fddd362801b9bd3936399e2103b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c2103b8d9d5771d8f513aa0869b9cc8d50986403b78c6da36890638c3d46a5adce04a2102ca0e27697b9c248f6f16e085fd0061e26f44da85b58ee835c110caa5ec3ba5542102df48f60e8f3e01c48ff40b9b7f1310d7a8b2a193188befe1c2e3df740e89509357ae", hex.EncodeToString(b.Script.VerificationScript)) assert.Equal(t, "552102486fd15702c4490a26703112a5cc1d0923fd697a33406bd5a1c00e0013b09a7021024c7b7fb6c310fccf1ba33b082519d82964ea93868d676662d4a59ad548df0e7d2102aaec38470f6aad0042c6e877cfd8087d2676b0f516fddd362801b9bd3936399e2103b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c2103b8d9d5771d8f513aa0869b9cc8d50986403b78c6da36890638c3d46a5adce04a2102ca0e27697b9c248f6f16e085fd0061e26f44da85b58ee835c110caa5ec3ba5542102df48f60e8f3e01c48ff40b9b7f1310d7a8b2a193188befe1c2e3df740e89509357ae", hex.EncodeToString(b.Script.VerificationScript))
assert.Equal(t, "0006d3ff96e269f599eb1b5c5a527c218439e498dcc65b63794591bbcdc0516b", b.Hash().StringLE()) assert.Equal(t, "0006d3ff96e269f599eb1b5c5a527c218439e498dcc65b63794591bbcdc0516b", b.Hash().StringLE())
buf := io.NewBufBinWriter() benc, err := testserdes.EncodeBinary(&b)
assert.NoError(t, err)
b.EncodeBinary(buf.BinWriter)
assert.Nil(t, r.Err)
benc := buf.Bytes()
// test size of the block // test size of the block
assert.Equal(t, 7360, len(benc)) assert.Equal(t, 7360, len(benc))
assert.Equal(t, rawBlock, hex.EncodeToString(benc)) assert.Equal(t, rawBlock, hex.EncodeToString(benc))

View file

@ -6,7 +6,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -26,14 +26,10 @@ func TestHeaderEncodeDecode(t *testing.T) {
}, },
}} }}
buf := io.NewBufBinWriter() _ = header.Hash()
header.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
headerDecode := &Header{} headerDecode := &Header{}
r := io.NewBinReaderFromBuf(buf.Bytes()) testserdes.EncodeDecodeBinary(t, &header, headerDecode)
headerDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, header.Version, headerDecode.Version, "expected both versions to be equal") assert.Equal(t, header.Version, headerDecode.Version, "expected both versions to be equal")
assert.Equal(t, header.PrevHash, headerDecode.PrevHash, "expected both prev hashes to be equal") assert.Equal(t, header.PrevHash, headerDecode.PrevHash, "expected both prev hashes to be equal")
assert.Equal(t, header.MerkleRoot, headerDecode.MerkleRoot, "expected both merkle roots to be equal") assert.Equal(t, header.MerkleRoot, headerDecode.MerkleRoot, "expected both merkle roots to be equal")

View file

@ -7,7 +7,7 @@ import (
"io/ioutil" "io/ioutil"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -19,9 +19,7 @@ func getDecodedBlock(t *testing.T, i int) *Block {
require.NoError(t, err) require.NoError(t, err)
block := &Block{} block := &Block{}
r := io.NewBinReaderFromBuf(b) require.NoError(t, testserdes.DecodeBinary(b, block))
block.DecodeBinary(r)
require.NoError(t, r.Err)
return block return block
} }

View file

@ -15,6 +15,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash" "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/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
@ -119,9 +120,7 @@ func getDecodedBlock(t *testing.T, i int) *block.Block {
require.NoError(t, err) require.NoError(t, err)
block := &block.Block{} block := &block.Block{}
r := io.NewBinReaderFromBuf(b) require.NoError(t, testserdes.DecodeBinary(b, block))
block.DecodeBinary(r)
require.NoError(t, r.Err)
return block return block
} }
@ -383,9 +382,8 @@ func TestCreateBasicChain(t *testing.T) {
bh := bc.GetHeaderHash(i) bh := bc.GetHeaderHash(i)
b, err := bc.GetBlock(bh) b, err := bc.GetBlock(bh)
require.NoError(t, err) require.NoError(t, err)
buf := io.NewBufBinWriter() bytes, err := testserdes.EncodeBinary(b)
b.EncodeBinary(buf.BinWriter) require.NoError(t, err)
bytes := buf.Bytes()
writer.WriteU32LE(uint32(len(bytes))) writer.WriteU32LE(uint32(len(bytes)))
writer.WriteBytes(bytes) writer.WriteBytes(bytes)
require.NoError(t, writer.Err) require.NoError(t, writer.Err)
@ -397,10 +395,9 @@ func TestCreateBasicChain(t *testing.T) {
var blocks []*block.Block var blocks []*block.Block
blocks = append(blocks, bc.newBlock(), bc.newBlock(newMinerTX())) blocks = append(blocks, bc.newBlock(), bc.newBlock(newMinerTX()))
for _, b := range blocks { for _, b := range blocks {
buf := io.NewBufBinWriter() data, err := testserdes.EncodeBinary(b)
b.EncodeBinary(buf.BinWriter) require.NoError(t, err)
require.NoError(t, buf.Err) t.Log(hex.EncodeToString(data))
t.Log(hex.EncodeToString(buf.Bytes()))
} }
} }

View file

@ -5,7 +5,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/internal/random" "github.com/nspcc-dev/neo-go/pkg/internal/random"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -36,25 +36,10 @@ func TestDecodeEncodeAccountState(t *testing.T) {
IsFrozen: true, IsFrozen: true,
Votes: votes, Votes: votes,
Balances: balances, Balances: balances,
Unclaimed: UnclaimedBalances{Raw: []byte{}},
} }
buf := io.NewBufBinWriter() testserdes.EncodeDecodeBinary(t, a, new(Account))
a.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
aDecode := &Account{}
r := io.NewBinReaderFromBuf(buf.Bytes())
aDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, a.Version, aDecode.Version)
assert.Equal(t, a.ScriptHash, aDecode.ScriptHash)
assert.Equal(t, a.IsFrozen, aDecode.IsFrozen)
for i, vote := range a.Votes {
assert.Equal(t, vote.X, aDecode.Votes[i].X)
}
assert.Equal(t, a.Balances, aDecode.Balances)
} }
func TestAccountStateBalanceValues(t *testing.T) { func TestAccountStateBalanceValues(t *testing.T) {

View file

@ -6,7 +6,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/internal/random" "github.com/nspcc-dev/neo-go/pkg/internal/random"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -27,14 +27,7 @@ func TestEncodeDecodeAssetState(t *testing.T) {
IsFrozen: false, IsFrozen: false,
} }
buf := io.NewBufBinWriter() testserdes.EncodeDecodeBinary(t, asset, new(Asset))
asset.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
assetDecode := &Asset{}
r := io.NewBinReaderFromBuf(buf.Bytes())
assetDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, asset, assetDecode)
} }
func TestAssetState_GetName_NEO(t *testing.T) { func TestAssetState_GetName_NEO(t *testing.T) {

View file

@ -4,7 +4,7 @@ import (
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -25,14 +25,9 @@ func TestEncodeDecodeContractState(t *testing.T) {
} }
assert.Equal(t, hash.Hash160(script), contract.ScriptHash()) assert.Equal(t, hash.Hash160(script), contract.ScriptHash())
buf := io.NewBufBinWriter()
contract.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
contractDecoded := &Contract{} contractDecoded := &Contract{}
r := io.NewBinReaderFromBuf(buf.Bytes()) testserdes.EncodeDecodeBinary(t, contract, contractDecoded)
contractDecoded.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, contract, contractDecoded)
assert.Equal(t, contract.ScriptHash(), contractDecoded.ScriptHash()) assert.Equal(t, contract.ScriptHash(), contractDecoded.ScriptHash())
} }

View file

@ -6,6 +6,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -43,7 +44,7 @@ func TestNEP5Tracker_EncodeBinary(t *testing.T) {
LastUpdatedBlock: rand.Uint32(), LastUpdatedBlock: rand.Uint32(),
} }
testEncodeDecode(t, expected, new(NEP5Tracker)) testserdes.EncodeDecodeBinary(t, expected, new(NEP5Tracker))
} }
func TestNEP5Transfer_DecodeBinary(t *testing.T) { func TestNEP5Transfer_DecodeBinary(t *testing.T) {
@ -57,7 +58,7 @@ func TestNEP5Transfer_DecodeBinary(t *testing.T) {
Tx: util.Uint256{8, 5, 3}, Tx: util.Uint256{8, 5, 3},
} }
testEncodeDecode(t, expected, new(NEP5Transfer)) testserdes.EncodeDecodeBinary(t, expected, new(NEP5Transfer))
} }
func TestNEP5TransferSize(t *testing.T) { func TestNEP5TransferSize(t *testing.T) {
@ -84,14 +85,3 @@ func randomTransfer(t *testing.T, r *rand.Rand) *NEP5Transfer {
return tr return tr
} }
func testEncodeDecode(t *testing.T, expected, actual io.Serializable) {
w := io.NewBufBinWriter()
expected.EncodeBinary(w.BinWriter)
require.NoError(t, w.Err)
r := io.NewBinReaderFromBuf(w.Bytes())
actual.DecodeBinary(r)
require.NoError(t, r.Err)
require.Equal(t, expected, actual)
}

View file

@ -4,25 +4,18 @@ import (
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/internal/random" "github.com/nspcc-dev/neo-go/pkg/internal/random"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/stretchr/testify/assert" "github.com/nspcc-dev/neo-go/pkg/vm"
) )
func TestEncodeDecodeNotificationEvent(t *testing.T) { func TestEncodeDecodeNotificationEvent(t *testing.T) {
event := &NotificationEvent{ event := &NotificationEvent{
ScriptHash: random.Uint160(), ScriptHash: random.Uint160(),
Item: nil, Item: vm.NewBoolItem(true),
} }
buf := io.NewBufBinWriter() testserdes.EncodeDecodeBinary(t, event, new(NotificationEvent))
event.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
eventDecoded := &NotificationEvent{}
reader := io.NewBinReaderFromBuf(buf.Bytes())
eventDecoded.DecodeBinary(reader)
assert.Equal(t, event, eventDecoded)
} }
func TestEncodeDecodeAppExecResult(t *testing.T) { func TestEncodeDecodeAppExecResult(t *testing.T) {
@ -34,12 +27,6 @@ func TestEncodeDecodeAppExecResult(t *testing.T) {
Stack: []smartcontract.Parameter{}, Stack: []smartcontract.Parameter{},
Events: []NotificationEvent{}, Events: []NotificationEvent{},
} }
buf := io.NewBufBinWriter()
appExecResult.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
appExecResultDecoded := &AppExecResult{} testserdes.EncodeDecodeBinary(t, appExecResult, new(AppExecResult))
reader := io.NewBinReaderFromBuf(buf.Bytes())
appExecResultDecoded.DecodeBinary(reader)
assert.Equal(t, appExecResult, appExecResultDecoded)
} }

View file

@ -3,9 +3,7 @@ package state
import ( import (
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestEncodeDecodeStorageItem(t *testing.T) { func TestEncodeDecodeStorageItem(t *testing.T) {
@ -13,14 +11,6 @@ func TestEncodeDecodeStorageItem(t *testing.T) {
Value: []byte{}, Value: []byte{},
IsConst: false, IsConst: false,
} }
buf := io.NewBufBinWriter()
storageItem.EncodeBinary(buf.BinWriter)
require.NoError(t, buf.Err)
decodedStorageItem := &StorageItem{} testserdes.EncodeDecodeBinary(t, storageItem, new(StorageItem))
r := io.NewBinReaderFromBuf(buf.Bytes())
decodedStorageItem.DecodeBinary(r)
require.NoError(t, r.Err)
assert.Equal(t, storageItem, decodedStorageItem)
} }

View file

@ -7,18 +7,15 @@ import (
"testing" "testing"
"time" "time"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestUnclaimedBalance_Structure(t *testing.T) { func TestUnclaimedBalance_Structure(t *testing.T) {
b := randomUnclaimed(t) b := randomUnclaimed(t)
w := io.NewBufBinWriter() buf, err := testserdes.EncodeBinary(b)
b.EncodeBinary(w.BinWriter) require.NoError(t, err)
require.NoError(t, w.Err)
buf := w.Bytes()
require.Equal(t, UnclaimedBalanceSize, len(buf)) require.Equal(t, UnclaimedBalanceSize, len(buf))
require.Equal(t, b.Tx.BytesBE(), buf[:util.Uint256Size]) require.Equal(t, b.Tx.BytesBE(), buf[:util.Uint256Size])
require.Equal(t, b.Index, binary.LittleEndian.Uint16(buf[util.Uint256Size:])) require.Equal(t, b.Index, binary.LittleEndian.Uint16(buf[util.Uint256Size:]))

View file

@ -5,9 +5,8 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/internal/random" "github.com/nspcc-dev/neo-go/pkg/internal/random"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
) )
func TestDecodeEncodeUnspentCoin(t *testing.T) { func TestDecodeEncodeUnspentCoin(t *testing.T) {
@ -44,11 +43,5 @@ func TestDecodeEncodeUnspentCoin(t *testing.T) {
}, },
} }
buf := io.NewBufBinWriter() testserdes.EncodeDecodeBinary(t, unspent, new(UnspentCoin))
unspent.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
unspentDecode := &UnspentCoin{}
r := io.NewBinReaderFromBuf(buf.Bytes())
unspentDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
} }

View file

@ -5,7 +5,7 @@ import (
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -16,15 +16,8 @@ func TestValidatorState_DecodeEncodeBinary(t *testing.T) {
Registered: false, Registered: false,
Votes: util.Fixed8(10), Votes: util.Fixed8(10),
} }
buf := io.NewBufBinWriter()
state.EncodeBinary(buf.BinWriter)
require.NoError(t, buf.Err)
decodedState := &Validator{} testserdes.EncodeDecodeBinary(t, state, new(Validator))
reader := io.NewBinReaderFromBuf(buf.Bytes())
decodedState.DecodeBinary(reader)
require.NoError(t, reader.Err)
require.Equal(t, state, decodedState)
} }
func TestRegisteredAndHasVotes_Registered(t *testing.T) { func TestRegisteredAndHasVotes_Registered(t *testing.T) {

View file

@ -4,7 +4,7 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -30,9 +30,7 @@ func TestEncodeDecodeContract(t *testing.T) {
assert.Equal(t, "bdf6cc3b9af12a7565bda80933a75ee8cef1bc771d0d58effc08e4c8b436da79", tx.Hash().StringLE()) assert.Equal(t, "bdf6cc3b9af12a7565bda80933a75ee8cef1bc771d0d58effc08e4c8b436da79", tx.Hash().StringLE())
// Encode // Encode
buf := io.NewBufBinWriter() data, err := testserdes.EncodeBinary(tx)
assert.NoError(t, err)
tx.EncodeBinary(buf.BinWriter) assert.Equal(t, rawtx, hex.EncodeToString(data))
assert.Equal(t, nil, buf.Err)
assert.Equal(t, rawtx, hex.EncodeToString(buf.Bytes()))
} }

View file

@ -4,7 +4,7 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -16,9 +16,7 @@ func TestEncodeDecodeEnrollment(t *testing.T) {
assert.IsType(t, tx.Data, &EnrollmentTX{}) assert.IsType(t, tx.Data, &EnrollmentTX{})
assert.Equal(t, 0, int(tx.Version)) assert.Equal(t, 0, int(tx.Version))
buf := io.NewBufBinWriter() data, err := testserdes.EncodeBinary(tx)
tx.EncodeBinary(buf.BinWriter) assert.Equal(t, nil, err)
assert.Equal(t, rawtx, hex.EncodeToString(data))
assert.Equal(t, nil, buf.Err)
assert.Equal(t, rawtx, hex.EncodeToString(buf.Bytes()))
} }

View file

@ -4,7 +4,7 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -21,8 +21,6 @@ func decodeTransaction(rawTX string, t *testing.T) *Transaction {
b, err1 := hex.DecodeString(rawTX) b, err1 := hex.DecodeString(rawTX)
assert.Nil(t, err1) assert.Nil(t, err1)
tx := &Transaction{} tx := &Transaction{}
r := io.NewBinReaderFromBuf(b) assert.NoError(t, testserdes.DecodeBinary(b, tx))
tx.DecodeBinary(r)
assert.Nil(t, r.Err)
return tx return tx
} }

View file

@ -4,7 +4,7 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -16,18 +16,13 @@ func TestInvocationZeroScript(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
inv := &InvocationTX{Version: 1} inv := &InvocationTX{Version: 1}
r := io.NewBinReaderFromBuf(in) assert.Error(t, testserdes.DecodeBinary(in, inv))
inv.DecodeBinary(r)
assert.Error(t, r.Err)
// PUSH1 script. // PUSH1 script.
in, err = hex.DecodeString("01510000000000000000") in, err = hex.DecodeString("01510000000000000000")
require.NoError(t, err) require.NoError(t, err)
r = io.NewBinReaderFromBuf(in)
inv.DecodeBinary(r) assert.NoError(t, testserdes.DecodeBinary(in, inv))
assert.NoError(t, r.Err)
} }
func TestInvocationNegativeGas(t *testing.T) { func TestInvocationNegativeGas(t *testing.T) {
@ -36,18 +31,13 @@ func TestInvocationNegativeGas(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
inv := &InvocationTX{Version: 1} inv := &InvocationTX{Version: 1}
r := io.NewBinReaderFromBuf(in) assert.Error(t, testserdes.DecodeBinary(in, inv))
inv.DecodeBinary(r)
assert.Error(t, r.Err)
// Positive GAS. // Positive GAS.
in, err = hex.DecodeString("01510100000000000000") in, err = hex.DecodeString("01510100000000000000")
require.NoError(t, err) require.NoError(t, err)
r = io.NewBinReaderFromBuf(in)
inv.DecodeBinary(r) assert.NoError(t, testserdes.DecodeBinary(in, inv))
assert.NoError(t, r.Err)
assert.Equal(t, util.Fixed8(1), inv.Gas) assert.Equal(t, util.Fixed8(1), inv.Gas)
} }
@ -56,15 +46,9 @@ func TestInvocationVersionZero(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
inv := &InvocationTX{Version: 1} inv := &InvocationTX{Version: 1}
r := io.NewBinReaderFromBuf(in) assert.Error(t, testserdes.DecodeBinary(in, inv))
inv.DecodeBinary(r)
assert.Error(t, r.Err)
inv = &InvocationTX{Version: 0} inv = &InvocationTX{Version: 0}
r = io.NewBinReaderFromBuf(in) assert.NoError(t, testserdes.DecodeBinary(in, inv))
inv.DecodeBinary(r)
assert.NoError(t, r.Err)
assert.Equal(t, util.Fixed8(0), inv.Gas) assert.Equal(t, util.Fixed8(0), inv.Gas)
} }

View file

@ -4,7 +4,7 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -21,10 +21,7 @@ func TestEncodeDecodeMiner(t *testing.T) {
assert.Equal(t, "a1f219dc6be4c35eca172e65e02d4591045220221b1543f1a4b67b9e9442c264", tx.Hash().StringLE()) assert.Equal(t, "a1f219dc6be4c35eca172e65e02d4591045220221b1543f1a4b67b9e9442c264", tx.Hash().StringLE())
// Encode // Encode
buf := io.NewBufBinWriter() data, err := testserdes.EncodeBinary(tx)
assert.NoError(t, err)
tx.EncodeBinary(buf.BinWriter) assert.Equal(t, rawtx, hex.EncodeToString(data))
assert.Equal(t, nil, buf.Err)
assert.Equal(t, rawtx, hex.EncodeToString(buf.Bytes()))
} }

View file

@ -6,7 +6,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -24,21 +24,14 @@ func TestRegisterTX(t *testing.T) {
Precision: 8, Precision: 8,
Admin: someuint160, Admin: someuint160,
}, },
Attributes: []Attribute{},
Inputs: []Input{},
Outputs: []Output{},
Scripts: []Witness{},
} }
_ = tx.Hash()
buf := io.NewBufBinWriter() testserdes.EncodeDecodeBinary(t, tx, new(Transaction))
tx.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
b := buf.Bytes()
txDecode := &Transaction{}
r := io.NewBinReaderFromBuf(b)
txDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
txData := tx.Data.(*RegisterTX)
txDecodeData := txDecode.Data.(*RegisterTX)
assert.Equal(t, txData, txDecodeData)
assert.Equal(t, tx.Hash(), txDecode.Hash())
} }
func TestDecodeRegisterTXFromRawString(t *testing.T) { func TestDecodeRegisterTXFromRawString(t *testing.T) {
@ -47,9 +40,7 @@ func TestDecodeRegisterTXFromRawString(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
tx := &Transaction{} tx := &Transaction{}
r := io.NewBinReaderFromBuf(b) assert.NoError(t, testserdes.DecodeBinary(b, tx))
tx.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, RegisterType, tx.Type) assert.Equal(t, RegisterType, tx.Type)
txData := tx.Data.(*RegisterTX) txData := tx.Data.(*RegisterTX)
assert.Equal(t, GoverningToken, txData.AssetType) assert.Equal(t, GoverningToken, txData.AssetType)
@ -60,14 +51,5 @@ func TestDecodeRegisterTXFromRawString(t *testing.T) {
assert.Equal(t, "Abf2qMs1pzQb8kYk9RuxtUb9jtRKJVuBJt", address.Uint160ToString(txData.Admin)) assert.Equal(t, "Abf2qMs1pzQb8kYk9RuxtUb9jtRKJVuBJt", address.Uint160ToString(txData.Admin))
assert.Equal(t, "c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b", tx.Hash().StringLE()) assert.Equal(t, "c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b", tx.Hash().StringLE())
buf := io.NewBufBinWriter() testserdes.EncodeDecodeBinary(t, tx, new(Transaction))
tx.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
benc := buf.Bytes()
txDecode := &Transaction{}
encreader := io.NewBinReaderFromBuf(benc)
txDecode.DecodeBinary(encreader)
assert.Nil(t, encreader.Err)
assert.Equal(t, tx, txDecode)
} }

View file

@ -4,7 +4,7 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -30,9 +30,7 @@ func TestEncodeDecodeState(t *testing.T) {
assert.Equal(t, Validator, descriptor.Type) assert.Equal(t, Validator, descriptor.Type)
// Encode // Encode
buf := io.NewBufBinWriter() data, err := testserdes.EncodeBinary(tx)
tx.EncodeBinary(buf.BinWriter) assert.NoError(t, err)
assert.Equal(t, rawtx, hex.EncodeToString(data))
assert.Equal(t, nil, buf.Err)
assert.Equal(t, rawtx, hex.EncodeToString(buf.Bytes()))
} }

View file

@ -2,11 +2,10 @@ package transaction
import ( import (
"encoding/hex" "encoding/hex"
"encoding/json"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -29,19 +28,11 @@ func TestWitnessEncodeDecode(t *testing.T) {
VerificationScript: verif, VerificationScript: verif,
} }
buf := io.NewBufBinWriter()
wit.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
benc := buf.Bytes()
witDecode := &Witness{} witDecode := &Witness{}
encreader := io.NewBinReaderFromBuf(benc) testserdes.EncodeDecodeBinary(t, wit, witDecode)
witDecode.DecodeBinary(encreader)
assert.Nil(t, encreader.Err)
t.Log(len(witDecode.VerificationScript)) t.Log(len(witDecode.VerificationScript))
t.Log(len(witDecode.InvocationScript)) t.Log(len(witDecode.InvocationScript))
assert.Equal(t, wit, witDecode)
} }
func TestDecodeEncodeClaimTX(t *testing.T) { func TestDecodeEncodeClaimTX(t *testing.T) {
@ -62,10 +53,9 @@ func TestDecodeEncodeClaimTX(t *testing.T) {
assert.Equal(t, invoc, hex.EncodeToString(tx.Scripts[0].InvocationScript)) assert.Equal(t, invoc, hex.EncodeToString(tx.Scripts[0].InvocationScript))
assert.Equal(t, verif, hex.EncodeToString(tx.Scripts[0].VerificationScript)) assert.Equal(t, verif, hex.EncodeToString(tx.Scripts[0].VerificationScript))
buf := io.NewBufBinWriter() data, err := testserdes.EncodeBinary(tx)
tx.EncodeBinary(buf.BinWriter) assert.NoError(t, err)
assert.Nil(t, buf.Err) assert.Equal(t, rawClaimTX, hex.EncodeToString(data))
assert.Equal(t, rawClaimTX, hex.EncodeToString(buf.Bytes()))
hash := "2c6a45547b3898318e400e541628990a07acb00f3b9a15a8e966ae49525304da" hash := "2c6a45547b3898318e400e541628990a07acb00f3b9a15a8e966ae49525304da"
assert.Equal(t, hash, tx.hash.StringLE()) assert.Equal(t, hash, tx.hash.StringLE())
@ -90,11 +80,9 @@ func TestDecodeEncodeInvocationTX(t *testing.T) {
assert.Equal(t, invoc, hex.EncodeToString(tx.Scripts[0].InvocationScript)) assert.Equal(t, invoc, hex.EncodeToString(tx.Scripts[0].InvocationScript))
assert.Equal(t, verif, hex.EncodeToString(tx.Scripts[0].VerificationScript)) assert.Equal(t, verif, hex.EncodeToString(tx.Scripts[0].VerificationScript))
buf := io.NewBufBinWriter() data, err := testserdes.EncodeBinary(tx)
tx.EncodeBinary(buf.BinWriter) assert.NoError(t, err)
assert.Nil(t, buf.Err) assert.Equal(t, rawInvocationTX, hex.EncodeToString(data))
assert.Equal(t, rawInvocationTX, hex.EncodeToString(buf.Bytes()))
} }
func TestNewInvocationTX(t *testing.T) { func TestNewInvocationTX(t *testing.T) {
@ -104,16 +92,9 @@ func TestNewInvocationTX(t *testing.T) {
assert.Equal(t, InvocationType, tx.Type) assert.Equal(t, InvocationType, tx.Type)
assert.Equal(t, tx.Version, txData.Version) assert.Equal(t, tx.Version, txData.Version)
assert.Equal(t, script, txData.Script) assert.Equal(t, script, txData.Script)
buf := io.NewBufBinWriter()
// Update hash fields to match tx2 that is gonna autoupdate them on decode. // Update hash fields to match tx2 that is gonna autoupdate them on decode.
_ = tx.Hash() _ = tx.Hash()
tx.EncodeBinary(buf.BinWriter) testserdes.EncodeDecodeBinary(t, tx, new(Transaction))
assert.Nil(t, buf.Err)
var tx2 Transaction
r := io.NewBinReaderFromBuf(buf.Bytes())
tx2.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, *tx, tx2)
} }
func TestDecodePublishTX(t *testing.T) { func TestDecodePublishTX(t *testing.T) {
@ -164,17 +145,14 @@ func TestDecodePublishTX(t *testing.T) {
assert.Equal(t, expectedTX.Type, actualTX.Type) assert.Equal(t, expectedTX.Type, actualTX.Type)
assert.Equal(t, expectedTX.Version, actualTX.Version) assert.Equal(t, expectedTX.Version, actualTX.Version)
buf := io.NewBufBinWriter() data, err := testserdes.EncodeBinary(actualTX)
actualTX.EncodeBinary(buf.BinWriter) assert.NoError(t, err)
assert.Nil(t, buf.Err) assert.Equal(t, rawPublishTX, hex.EncodeToString(data))
assert.Equal(t, rawPublishTX, hex.EncodeToString(buf.Bytes()))
} }
func TestEncodingTXWithNoData(t *testing.T) { func TestEncodingTXWithNoData(t *testing.T) {
buf := io.NewBufBinWriter() _, err := testserdes.EncodeBinary(new(Transaction))
tx := &Transaction{} require.Error(t, err)
tx.EncodeBinary(buf.BinWriter)
require.Error(t, buf.Err)
} }
func TestMarshalUnmarshalJSON(t *testing.T) { func TestMarshalUnmarshalJSON(t *testing.T) {
@ -189,10 +167,5 @@ func TestMarshalUnmarshalJSON(t *testing.T) {
InvocationScript: []byte{5, 3, 1}, InvocationScript: []byte{5, 3, 1},
VerificationScript: []byte{2, 4, 6}, VerificationScript: []byte{2, 4, 6},
}} }}
data, err := json.Marshal(tx) testserdes.MarshalUnmarshalJSON(t, tx, new(Transaction))
require.NoError(t, err)
txNew := new(Transaction)
require.NoError(t, json.Unmarshal(data, txNew))
require.Equal(t, tx, txNew)
} }

View file

@ -7,16 +7,14 @@ import (
"sort" "sort"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestEncodeDecodeInfinity(t *testing.T) { func TestEncodeDecodeInfinity(t *testing.T) {
key := &PublicKey{} key := &PublicKey{}
buf := io.NewBufBinWriter() b, err := testserdes.EncodeBinary(key)
key.EncodeBinary(buf.BinWriter) require.NoError(t, err)
require.NoError(t, buf.Err)
b := buf.Bytes()
require.Equal(t, 1, len(b)) require.Equal(t, 1, len(b))
keyDecode := &PublicKey{} keyDecode := &PublicKey{}
@ -29,24 +27,13 @@ func TestEncodeDecodePublicKey(t *testing.T) {
k, err := NewPrivateKey() k, err := NewPrivateKey()
require.NoError(t, err) require.NoError(t, err)
p := k.PublicKey() p := k.PublicKey()
buf := io.NewBufBinWriter() testserdes.EncodeDecodeBinary(t, p, new(PublicKey))
p.EncodeBinary(buf.BinWriter)
require.NoError(t, buf.Err)
b := buf.Bytes()
pDecode := &PublicKey{}
require.NoError(t, pDecode.DecodeBytes(b))
require.Equal(t, p.X, pDecode.X)
} }
errCases := [][]byte{{}, {0x02}, {0x04}} errCases := [][]byte{{}, {0x02}, {0x04}}
for _, tc := range errCases { for _, tc := range errCases {
r := io.NewBinReaderFromBuf(tc) require.Error(t, testserdes.DecodeBinary(tc, new(PublicKey)))
var pDecode PublicKey
pDecode.DecodeBinary(r)
require.Error(t, r.Err)
} }
} }

View file

@ -0,0 +1,44 @@
package testserdes
import (
"encoding/json"
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/stretchr/testify/require"
)
// MarshalUnmarshalJSON checks if expected stays the same after
// marshal/unmarshal via JSON.
func MarshalUnmarshalJSON(t *testing.T, expected, actual interface{}) {
data, err := json.Marshal(expected)
require.NoError(t, err)
require.NoError(t, json.Unmarshal(data, actual))
require.Equal(t, expected, actual)
}
// EncodeDecodeBinary checks if expected stays the same after
// serializing/deserializing via io.Serializable methods.
func EncodeDecodeBinary(t *testing.T, expected, actual io.Serializable) {
data, err := EncodeBinary(expected)
require.NoError(t, err)
require.NoError(t, DecodeBinary(data, actual))
require.Equal(t, expected, actual)
}
// EncodeBinary serializes a to a byte slice.
func EncodeBinary(a io.Serializable) ([]byte, error) {
w := io.NewBufBinWriter()
a.EncodeBinary(w.BinWriter)
if w.Err != nil {
return nil, w.Err
}
return w.Bytes(), nil
}
// DecodeBinary deserializes a from a byte slice.
func DecodeBinary(data []byte, a io.Serializable) error {
r := io.NewBinReaderFromBuf(data)
a.DecodeBinary(r)
return r.Err
}

View file

@ -6,7 +6,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -15,7 +15,6 @@ func TestEncodeDecodeAddress(t *testing.T) {
e, _ = net.ResolveTCPAddr("tcp", "127.0.0.1:2000") e, _ = net.ResolveTCPAddr("tcp", "127.0.0.1:2000")
ts = time.Now() ts = time.Now()
addr = NewAddressAndTime(e, ts) addr = NewAddressAndTime(e, ts)
buf = io.NewBufBinWriter()
) )
assert.Equal(t, ts.UTC().Unix(), int64(addr.Timestamp)) assert.Equal(t, ts.UTC().Unix(), int64(addr.Timestamp))
@ -23,16 +22,8 @@ func TestEncodeDecodeAddress(t *testing.T) {
copy(aatip, addr.IP[:]) copy(aatip, addr.IP[:])
assert.Equal(t, e.IP, aatip) assert.Equal(t, e.IP, aatip)
assert.Equal(t, e.Port, int(addr.Port)) assert.Equal(t, e.Port, int(addr.Port))
addr.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
b := buf.Bytes() testserdes.EncodeDecodeBinary(t, addr, new(AddressAndTime))
r := io.NewBinReaderFromBuf(b)
addrDecode := &AddressAndTime{}
addrDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, addr, addrDecode)
} }
func TestEncodeDecodeAddressList(t *testing.T) { func TestEncodeDecodeAddressList(t *testing.T) {
@ -43,15 +34,5 @@ func TestEncodeDecodeAddressList(t *testing.T) {
addrList.Addrs[i] = NewAddressAndTime(e, time.Now()) addrList.Addrs[i] = NewAddressAndTime(e, time.Now())
} }
buf := io.NewBufBinWriter() testserdes.EncodeDecodeBinary(t, addrList, new(AddressList))
addrList.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
b := buf.Bytes()
r := io.NewBinReaderFromBuf(b)
addrListDecode := &AddressList{}
addrListDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, addrList, addrListDecode)
} }

View file

@ -4,9 +4,8 @@ import (
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
) )
func TestGetBlockEncodeDecode(t *testing.T) { func TestGetBlockEncodeDecode(t *testing.T) {
@ -18,16 +17,7 @@ func TestGetBlockEncodeDecode(t *testing.T) {
} }
p := NewGetBlocks(start, util.Uint256{}) p := NewGetBlocks(start, util.Uint256{})
buf := io.NewBufBinWriter() testserdes.EncodeDecodeBinary(t, p, new(GetBlocks))
p.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
b := buf.Bytes()
r := io.NewBinReaderFromBuf(b)
pDecode := &GetBlocks{}
pDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, p, pDecode)
} }
func TestGetBlockEncodeDecodeWithHashStop(t *testing.T) { func TestGetBlockEncodeDecodeWithHashStop(t *testing.T) {
@ -41,14 +31,5 @@ func TestGetBlockEncodeDecodeWithHashStop(t *testing.T) {
stop = hash.Sha256([]byte("e")) stop = hash.Sha256([]byte("e"))
) )
p := NewGetBlocks(start, stop) p := NewGetBlocks(start, stop)
buf := io.NewBufBinWriter() testserdes.EncodeDecodeBinary(t, p, new(GetBlocks))
p.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
b := buf.Bytes()
r := io.NewBinReaderFromBuf(b)
pDecode := &GetBlocks{}
pDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, p, pDecode)
} }

View file

@ -6,7 +6,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -44,21 +44,18 @@ func newTestHeaders(n int) *Headers {
} }
func testHeadersEncodeDecode(t *testing.T, headers *Headers, expected int, limit bool) { func testHeadersEncodeDecode(t *testing.T, headers *Headers, expected int, limit bool) {
buf := io.NewBufBinWriter() data, err := testserdes.EncodeBinary(headers)
headers.EncodeBinary(buf.BinWriter) assert.Nil(t, err)
assert.Nil(t, buf.Err)
b := buf.Bytes()
r := io.NewBinReaderFromBuf(b)
headersDecode := &Headers{} headersDecode := &Headers{}
headersDecode.DecodeBinary(r) rErr := testserdes.DecodeBinary(data, headersDecode)
var err error err = nil
if limit { if limit {
err = ErrTooManyHeaders err = ErrTooManyHeaders
} }
assert.Equal(t, err, r.Err) assert.Equal(t, err, rErr)
assert.Equal(t, expected, len(headersDecode.Hdrs)) assert.Equal(t, expected, len(headersDecode.Hdrs))
for i := 0; i < len(headersDecode.Hdrs); i++ { for i := 0; i < len(headersDecode.Hdrs); i++ {
@ -75,10 +72,7 @@ func TestBinEncodeDecode(t *testing.T) {
rawBlockBytes, _ := hex.DecodeString(rawBlockHeaders) rawBlockBytes, _ := hex.DecodeString(rawBlockHeaders)
r := io.NewBinReaderFromBuf(rawBlockBytes) assert.NoError(t, testserdes.DecodeBinary(rawBlockBytes, &headerMsg))
headerMsg.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, 1, len(headerMsg.Hdrs)) assert.Equal(t, 1, len(headerMsg.Hdrs))
header := headerMsg.Hdrs[0] header := headerMsg.Hdrs[0]
@ -86,9 +80,7 @@ func TestBinEncodeDecode(t *testing.T) {
assert.Equal(t, "f3c4ec44c07eccbda974f1ee34bc6654ab6d3f22cd89c2e5c593a16d6cc7e6e8", hash.StringLE()) assert.Equal(t, "f3c4ec44c07eccbda974f1ee34bc6654ab6d3f22cd89c2e5c593a16d6cc7e6e8", hash.StringLE())
buf := io.NewBufBinWriter() data, err := testserdes.EncodeBinary(&headerMsg)
assert.NoError(t, err)
headerMsg.EncodeBinary(buf.BinWriter) assert.Equal(t, hex.EncodeToString(rawBlockBytes), hex.EncodeToString(data))
assert.Equal(t, nil, buf.Err)
assert.Equal(t, hex.EncodeToString(rawBlockBytes), hex.EncodeToString(buf.Bytes()))
} }

View file

@ -4,7 +4,7 @@ import (
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
. "github.com/nspcc-dev/neo-go/pkg/util" . "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -16,24 +16,14 @@ func TestInventoryEncodeDecode(t *testing.T) {
} }
inv := NewInventory(BlockType, hashes) inv := NewInventory(BlockType, hashes)
buf := io.NewBufBinWriter() testserdes.EncodeDecodeBinary(t, inv, new(Inventory))
inv.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
b := buf.Bytes()
r := io.NewBinReaderFromBuf(b)
invDecode := &Inventory{}
invDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, inv, invDecode)
} }
func TestEmptyInv(t *testing.T) { func TestEmptyInv(t *testing.T) {
msgInv := NewInventory(TXType, []Uint256{}) msgInv := NewInventory(TXType, []Uint256{})
buf := io.NewBufBinWriter() data, err := testserdes.EncodeBinary(msgInv)
msgInv.EncodeBinary(buf.BinWriter) assert.Nil(t, err)
assert.Nil(t, buf.Err) assert.Equal(t, []byte{byte(TXType), 0}, data)
assert.Equal(t, []byte{byte(TXType), 0}, buf.Bytes())
assert.Equal(t, 0, len(msgInv.Hashes)) assert.Equal(t, 0, len(msgInv.Hashes))
} }

View file

@ -3,7 +3,7 @@ package payload
import ( import (
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -11,14 +11,8 @@ func TestEncodeDecodeBinary(t *testing.T) {
payload := NewPing(uint32(1), uint32(2)) payload := NewPing(uint32(1), uint32(2))
assert.NotEqual(t, 0, payload.Timestamp) assert.NotEqual(t, 0, payload.Timestamp)
bufBinWriter := io.NewBufBinWriter()
payload.EncodeBinary(bufBinWriter.BinWriter)
assert.Nil(t, bufBinWriter.Err)
binReader := io.NewBinReaderFromBuf(bufBinWriter.Bytes())
decodedPing := &Ping{} decodedPing := &Ping{}
decodedPing.DecodeBinary(binReader) testserdes.EncodeDecodeBinary(t, payload, decodedPing)
assert.Nil(t, binReader.Err)
assert.Equal(t, uint32(1), decodedPing.LastBlockIndex) assert.Equal(t, uint32(1), decodedPing.LastBlockIndex)
assert.Equal(t, uint32(2), decodedPing.Nonce) assert.Equal(t, uint32(2), decodedPing.Nonce)

View file

@ -3,7 +3,7 @@ package payload
import ( import (
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -15,17 +15,9 @@ func TestVersionEncodeDecode(t *testing.T) {
var relay = true var relay = true
version := NewVersion(id, port, useragent, height, relay) version := NewVersion(id, port, useragent, height, relay)
buf := io.NewBufBinWriter()
version.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
b := buf.Bytes()
assert.Equal(t, io.GetVarSize(version), len(b))
r := io.NewBinReaderFromBuf(b)
versionDecoded := &Version{} versionDecoded := &Version{}
versionDecoded.DecodeBinary(r) testserdes.EncodeDecodeBinary(t, version, versionDecoded)
assert.Nil(t, r.Err)
assert.Equal(t, versionDecoded.Nonce, id) assert.Equal(t, versionDecoded.Nonce, id)
assert.Equal(t, versionDecoded.Port, port) assert.Equal(t, versionDecoded.Port, port)
assert.Equal(t, versionDecoded.UserAgent, []byte(useragent)) assert.Equal(t, versionDecoded.UserAgent, []byte(useragent))

View file

@ -2,11 +2,11 @@ package context
import ( import (
"encoding/hex" "encoding/hex"
"encoding/json"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm" "github.com/nspcc-dev/neo-go/pkg/vm"
@ -137,12 +137,7 @@ func TestParameterContext_MarshalJSON(t *testing.T) {
}, },
} }
data, err = json.Marshal(expected) testserdes.MarshalUnmarshalJSON(t, expected, new(ParameterContext))
require.NoError(t, err)
actual := new(ParameterContext)
require.NoError(t, json.Unmarshal(data, actual))
require.Equal(t, expected, actual)
} }
func getPrivateKeys(t *testing.T, n int) ([]*keys.PrivateKey, []*keys.PublicKey) { func getPrivateKeys(t *testing.T, n int) ([]*keys.PrivateKey, []*keys.PublicKey) {

View file

@ -2,17 +2,15 @@ package context
import ( import (
"encoding/hex" "encoding/hex"
"encoding/json"
"io" "io"
"math/rand" "math/rand"
"testing" "testing"
"time" "time"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -56,12 +54,7 @@ func TestContextItem_MarshalJSON(t *testing.T) {
}, },
} }
data, err := json.Marshal(expected) testserdes.MarshalUnmarshalJSON(t, expected, new(Item))
require.NoError(t, err)
actual := new(Item)
require.NoError(t, json.Unmarshal(data, actual))
assert.Equal(t, expected, actual)
} }
func getRandomSlice(t *testing.T, n int) []byte { func getRandomSlice(t *testing.T, n int) []byte {

View file

@ -6,7 +6,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -446,25 +446,14 @@ func TestNewParameterFromString(t *testing.T) {
func TestEncodeDecodeBinary(t *testing.T) { func TestEncodeDecodeBinary(t *testing.T) {
for _, tc := range marshalJSONTestCases { for _, tc := range marshalJSONTestCases {
w := io.NewBufBinWriter() testserdes.EncodeDecodeBinary(t, &tc.input, new(Parameter))
tc.input.EncodeBinary(w.BinWriter)
require.NoError(t, w.Err)
r := io.NewBinReaderFromBuf(w.Bytes())
var p Parameter
p.DecodeBinary(r)
require.NoError(t, r.Err)
require.Equal(t, tc.input, p)
} }
t.Run("unknown", func(t *testing.T) { t.Run("unknown", func(t *testing.T) {
p := Parameter{Type: UnknownType} p := Parameter{Type: UnknownType}
w := io.NewBufBinWriter() _, err := testserdes.EncodeBinary(&p)
p.EncodeBinary(w.BinWriter) require.Error(t, err)
require.Error(t, w.Err)
r := io.NewBinReaderFromBuf([]byte{0xAA}) require.Error(t, testserdes.DecodeBinary([]byte{0xAA}, &p))
p.DecodeBinary(r)
require.Error(t, r.Err)
}) })
} }

View file

@ -7,7 +7,7 @@ import (
"testing" "testing"
"github.com/go-yaml/yaml" "github.com/go-yaml/yaml"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -175,12 +175,5 @@ func TestFixed8_Arith(t *testing.T) {
func TestFixed8_Serializable(t *testing.T) { func TestFixed8_Serializable(t *testing.T) {
a := Fixed8(0x0102030405060708) a := Fixed8(0x0102030405060708)
w := io.NewBufBinWriter() testserdes.EncodeDecodeBinary(t, &a, new(Fixed8))
a.EncodeBinary(w.BinWriter)
require.NoError(t, w.Err)
var b Fixed8
r := io.NewBinReaderFromBuf(w.Bytes())
b.DecodeBinary(r)
require.Equal(t, a, b)
} }

View file

@ -4,6 +4,7 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -19,12 +20,7 @@ func TestUint160UnmarshalJSON(t *testing.T) {
assert.NoError(t, u1.UnmarshalJSON([]byte(`"`+str+`"`))) assert.NoError(t, u1.UnmarshalJSON([]byte(`"`+str+`"`)))
assert.True(t, expected.Equals(u1)) assert.True(t, expected.Equals(u1))
s, err := expected.MarshalJSON() testserdes.MarshalUnmarshalJSON(t, &expected, &u2)
require.NoError(t, err)
// UnmarshalJSON decodes hex-strings prefixed by 0x
assert.NoError(t, u2.UnmarshalJSON(s))
assert.True(t, expected.Equals(u1))
assert.Error(t, u2.UnmarshalJSON([]byte(`123`))) assert.Error(t, u2.UnmarshalJSON([]byte(`123`)))
} }

View file

@ -4,7 +4,7 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -20,12 +20,7 @@ func TestUint256UnmarshalJSON(t *testing.T) {
require.NoError(t, u1.UnmarshalJSON([]byte(`"`+str+`"`))) require.NoError(t, u1.UnmarshalJSON([]byte(`"`+str+`"`)))
assert.True(t, expected.Equals(u1)) assert.True(t, expected.Equals(u1))
s, err := expected.MarshalJSON() testserdes.MarshalUnmarshalJSON(t, &expected, &u2)
require.NoError(t, err)
// UnmarshalJSON decodes hex-strings prefixed by 0x
require.NoError(t, u2.UnmarshalJSON(s))
assert.True(t, expected.Equals(u1))
// UnmarshalJSON does not accepts numbers // UnmarshalJSON does not accepts numbers
assert.Error(t, u2.UnmarshalJSON([]byte("123"))) assert.Error(t, u2.UnmarshalJSON([]byte("123")))
@ -96,12 +91,6 @@ func TestUint256_Serializable(t *testing.T) {
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
} }
w := io.NewBufBinWriter()
a.EncodeBinary(w.BinWriter)
require.NoError(t, w.Err)
var b Uint256 var b Uint256
r := io.NewBinReaderFromBuf(w.Bytes()) testserdes.EncodeDecodeBinary(t, &a, &b)
b.DecodeBinary(r)
require.Equal(t, a, b)
} }