core: adjust notary-related attributes encoding

This commit is contained in:
Anna Shaleva 2022-06-01 14:25:30 +03:00
parent 1b80215415
commit 3bec6657f5
4 changed files with 9 additions and 56 deletions

View file

@ -52,14 +52,9 @@ func TestAttribute_EncodeBinary(t *testing.T) {
} }
testserdes.EncodeDecodeBinary(t, attr, new(Attribute)) 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) { t.Run("bad format: too short", func(t *testing.T) {
bw := io.NewBufBinWriter() bw := io.NewBufBinWriter()
bw.WriteVarBytes([]byte{1, 2, 3}) bw.WriteBytes([]byte{1, 2, 3})
require.Error(t, testserdes.DecodeBinary(bw.Bytes(), new(NotValidBefore))) 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)) 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) { t.Run("negative: bad uint256", func(t *testing.T) {
bw := io.NewBufBinWriter() 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))) 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)) 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) { t.Run("bad format: too short", func(t *testing.T) {
bw := io.NewBufBinWriter() bw := io.NewBufBinWriter()
bw.WriteVarBytes([]byte{}) bw.WriteBytes([]byte{})
require.Error(t, testserdes.DecodeBinary(bw.Bytes(), new(NotaryAssisted))) require.Error(t, testserdes.DecodeBinary(bw.Bytes(), new(NotaryAssisted)))
}) })
}) })

View file

@ -12,21 +12,12 @@ type Conflicts struct {
// DecodeBinary implements the io.Serializable interface. // DecodeBinary implements the io.Serializable interface.
func (c *Conflicts) DecodeBinary(br *io.BinReader) { func (c *Conflicts) DecodeBinary(br *io.BinReader) {
bytes := br.ReadVarBytes(util.Uint256Size) c.Hash.DecodeBinary(br)
if br.Err != nil {
return
}
hash, err := util.Uint256DecodeBytesBE(bytes)
if err != nil {
br.Err = err
return
}
c.Hash = hash
} }
// EncodeBinary implements the io.Serializable interface. // EncodeBinary implements the io.Serializable interface.
func (c *Conflicts) EncodeBinary(w *io.BinWriter) { func (c *Conflicts) EncodeBinary(w *io.BinWriter) {
w.WriteVarBytes(c.Hash.BytesBE()) c.Hash.EncodeBinary(w)
} }
func (c *Conflicts) toJSONMap(m map[string]interface{}) { func (c *Conflicts) toJSONMap(m map[string]interface{}) {

View file

@ -1,9 +1,6 @@
package transaction package transaction
import ( import (
"encoding/binary"
"fmt"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
) )
@ -14,22 +11,12 @@ type NotValidBefore struct {
// DecodeBinary implements the io.Serializable interface. // DecodeBinary implements the io.Serializable interface.
func (n *NotValidBefore) DecodeBinary(br *io.BinReader) { func (n *NotValidBefore) DecodeBinary(br *io.BinReader) {
bytes := br.ReadVarBytes(4) n.Height = br.ReadU32LE()
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)
} }
// EncodeBinary implements the io.Serializable interface. // EncodeBinary implements the io.Serializable interface.
func (n *NotValidBefore) EncodeBinary(w *io.BinWriter) { func (n *NotValidBefore) EncodeBinary(w *io.BinWriter) {
bytes := make([]byte, 4) w.WriteU32LE(n.Height)
binary.LittleEndian.PutUint32(bytes, n.Height)
w.WriteVarBytes(bytes)
} }
func (n *NotValidBefore) toJSONMap(m map[string]interface{}) { func (n *NotValidBefore) toJSONMap(m map[string]interface{}) {

View file

@ -1,8 +1,6 @@
package transaction package transaction
import ( import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
) )
@ -13,20 +11,12 @@ type NotaryAssisted struct {
// DecodeBinary implements the io.Serializable interface. // DecodeBinary implements the io.Serializable interface.
func (n *NotaryAssisted) DecodeBinary(br *io.BinReader) { func (n *NotaryAssisted) DecodeBinary(br *io.BinReader) {
bytes := br.ReadVarBytes(1) n.NKeys = br.ReadB()
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]
} }
// EncodeBinary implements the io.Serializable interface. // EncodeBinary implements the io.Serializable interface.
func (n *NotaryAssisted) EncodeBinary(w *io.BinWriter) { func (n *NotaryAssisted) EncodeBinary(w *io.BinWriter) {
w.WriteVarBytes([]byte{n.NKeys}) w.WriteB(n.NKeys)
} }
func (n *NotaryAssisted) toJSONMap(m map[string]interface{}) { func (n *NotaryAssisted) toJSONMap(m map[string]interface{}) {