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))
})
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)))
})
})

View file

@ -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{}) {

View file

@ -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{}) {

View file

@ -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{}) {