Skip to content

Commit

Permalink
core: adjust notary-related attributes encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnaShaleva committed Jun 1, 2022
1 parent 1b80215 commit 3bec665
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 56 deletions.
21 changes: 3 additions & 18 deletions pkg/core/transaction/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
})
})
Expand Down Expand Up @@ -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)))
})
})
Expand All @@ -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)))
})
})
Expand Down
13 changes: 2 additions & 11 deletions pkg/core/transaction/conflicts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {
Expand Down
17 changes: 2 additions & 15 deletions pkg/core/transaction/not_valid_before.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package transaction

import (
"encoding/binary"
"fmt"

"github.com/nspcc-dev/neo-go/pkg/io"
)

Expand All @@ -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{}) {
Expand Down
14 changes: 2 additions & 12 deletions pkg/core/transaction/notary_assisted.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package transaction

import (
"fmt"

"github.com/nspcc-dev/neo-go/pkg/io"
)

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

0 comments on commit 3bec665

Please sign in to comment.