From 84924972d0d752e2ad70aa1a69c5717e50d53989 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 1 Jun 2022 14:25:30 +0300 Subject: [PATCH] core: adjust notary-related attributes encoding --- pkg/core/transaction/attribute_test.go | 21 +++------------------ pkg/core/transaction/conflicts.go | 13 ++----------- pkg/core/transaction/not_valid_before.go | 17 ++--------------- pkg/core/transaction/notary_assisted.go | 14 ++------------ 4 files changed, 9 insertions(+), 56 deletions(-) diff --git a/pkg/core/transaction/attribute_test.go b/pkg/core/transaction/attribute_test.go index b38c881813..f35a797328 100644 --- a/pkg/core/transaction/attribute_test.go +++ b/pkg/core/transaction/attribute_test.go @@ -52,14 +52,9 @@ func TestAttribute_EncodeBinary(t *testing.T) { } testserdes.EncodeDecodeBinary(t, attr, new(Attribute)) }) - t.Run("bad format: too long", func(t *testing.T) { - bw := io.NewBufBinWriter() - bw.WriteVarBytes([]byte{1, 2, 3, 4, 5}) - require.Error(t, testserdes.DecodeBinary(bw.Bytes(), new(NotValidBefore))) - }) t.Run("bad format: too short", func(t *testing.T) { bw := io.NewBufBinWriter() - bw.WriteVarBytes([]byte{1, 2, 3}) + bw.WriteBytes([]byte{1, 2, 3}) require.Error(t, testserdes.DecodeBinary(bw.Bytes(), new(NotValidBefore))) }) }) @@ -96,14 +91,9 @@ func TestAttribute_EncodeBinary(t *testing.T) { } testserdes.EncodeDecodeBinary(t, attr, new(Attribute)) }) - t.Run("negative: too long", func(t *testing.T) { - bw := io.NewBufBinWriter() - bw.WriteVarBytes(make([]byte, util.Uint256Size+1)) - require.Error(t, testserdes.DecodeBinary(bw.Bytes(), new(Conflicts))) - }) t.Run("negative: bad uint256", func(t *testing.T) { bw := io.NewBufBinWriter() - bw.WriteVarBytes(make([]byte, util.Uint256Size-1)) + bw.WriteBytes(make([]byte, util.Uint256Size-1)) require.Error(t, testserdes.DecodeBinary(bw.Bytes(), new(Conflicts))) }) }) @@ -117,14 +107,9 @@ func TestAttribute_EncodeBinary(t *testing.T) { } testserdes.EncodeDecodeBinary(t, attr, new(Attribute)) }) - t.Run("bad format: too long", func(t *testing.T) { - bw := io.NewBufBinWriter() - bw.WriteVarBytes(make([]byte, 2)) - require.Error(t, testserdes.DecodeBinary(bw.Bytes(), new(NotaryAssisted))) - }) t.Run("bad format: too short", func(t *testing.T) { bw := io.NewBufBinWriter() - bw.WriteVarBytes([]byte{}) + bw.WriteBytes([]byte{}) require.Error(t, testserdes.DecodeBinary(bw.Bytes(), new(NotaryAssisted))) }) }) diff --git a/pkg/core/transaction/conflicts.go b/pkg/core/transaction/conflicts.go index 77eff8c64e..01b62e441e 100644 --- a/pkg/core/transaction/conflicts.go +++ b/pkg/core/transaction/conflicts.go @@ -12,21 +12,12 @@ type Conflicts struct { // DecodeBinary implements the io.Serializable interface. func (c *Conflicts) DecodeBinary(br *io.BinReader) { - bytes := br.ReadVarBytes(util.Uint256Size) - if br.Err != nil { - return - } - hash, err := util.Uint256DecodeBytesBE(bytes) - if err != nil { - br.Err = err - return - } - c.Hash = hash + c.Hash.DecodeBinary(br) } // EncodeBinary implements the io.Serializable interface. func (c *Conflicts) EncodeBinary(w *io.BinWriter) { - w.WriteVarBytes(c.Hash.BytesBE()) + c.Hash.EncodeBinary(w) } func (c *Conflicts) toJSONMap(m map[string]interface{}) { diff --git a/pkg/core/transaction/not_valid_before.go b/pkg/core/transaction/not_valid_before.go index 46868dc2b8..232009f3db 100644 --- a/pkg/core/transaction/not_valid_before.go +++ b/pkg/core/transaction/not_valid_before.go @@ -1,9 +1,6 @@ package transaction import ( - "encoding/binary" - "fmt" - "github.com/nspcc-dev/neo-go/pkg/io" ) @@ -14,22 +11,12 @@ type NotValidBefore struct { // DecodeBinary implements the io.Serializable interface. func (n *NotValidBefore) DecodeBinary(br *io.BinReader) { - bytes := br.ReadVarBytes(4) - if br.Err != nil { - return - } - if len(bytes) != 4 { - br.Err = fmt.Errorf("expected 4 bytes, got %d", len(bytes)) - return - } - n.Height = binary.LittleEndian.Uint32(bytes) + n.Height = br.ReadU32LE() } // EncodeBinary implements the io.Serializable interface. func (n *NotValidBefore) EncodeBinary(w *io.BinWriter) { - bytes := make([]byte, 4) - binary.LittleEndian.PutUint32(bytes, n.Height) - w.WriteVarBytes(bytes) + w.WriteU32LE(n.Height) } func (n *NotValidBefore) toJSONMap(m map[string]interface{}) { diff --git a/pkg/core/transaction/notary_assisted.go b/pkg/core/transaction/notary_assisted.go index 436c4f42c9..5a7de29a4f 100644 --- a/pkg/core/transaction/notary_assisted.go +++ b/pkg/core/transaction/notary_assisted.go @@ -1,8 +1,6 @@ package transaction import ( - "fmt" - "github.com/nspcc-dev/neo-go/pkg/io" ) @@ -13,20 +11,12 @@ type NotaryAssisted struct { // DecodeBinary implements the io.Serializable interface. func (n *NotaryAssisted) DecodeBinary(br *io.BinReader) { - bytes := br.ReadVarBytes(1) - if br.Err != nil { - return - } - if len(bytes) != 1 { - br.Err = fmt.Errorf("expected 1 byte, got %d", len(bytes)) - return - } - n.NKeys = bytes[0] + n.NKeys = br.ReadB() } // EncodeBinary implements the io.Serializable interface. func (n *NotaryAssisted) EncodeBinary(w *io.BinWriter) { - w.WriteVarBytes([]byte{n.NKeys}) + w.WriteB(n.NKeys) } func (n *NotaryAssisted) toJSONMap(m map[string]interface{}) {