payload: fix Size() calculation for version

UserAgent is variable-length-encoded. Fix associated test.
This commit is contained in:
Roman Khimov 2019-08-29 17:19:52 +03:00
parent fac87c7b87
commit c949d2ec53
2 changed files with 7 additions and 4 deletions

View file

@ -7,6 +7,8 @@ import (
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
) )
// Size of the payload not counting UserAgent encoding (which is at least 1 byte
// for zero-length string)
const minVersionSize = 27 const minVersionSize = 27
// List of Services offered by the node // List of Services offered by the node
@ -83,5 +85,5 @@ func (p *Version) EncodeBinary(w io.Writer) error {
// Size implements the payloader interface. // Size implements the payloader interface.
func (p *Version) Size() uint32 { func (p *Version) Size() uint32 {
return uint32(minVersionSize + len(p.UserAgent)) return uint32(minVersionSize + util.GetVarSize(p.UserAgent))
} }

View file

@ -14,6 +14,10 @@ func TestVersionEncodeDecode(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if int(version.Size()) != buf.Len() {
t.Fatalf("Expected version size of %d", buf.Len())
}
versionDecoded := &Version{} versionDecoded := &Version{}
if err := versionDecoded.DecodeBinary(buf); err != nil { if err := versionDecoded.DecodeBinary(buf); err != nil {
t.Fatal(err) t.Fatal(err)
@ -23,7 +27,4 @@ func TestVersionEncodeDecode(t *testing.T) {
t.Fatalf("expected both version payload to be equal: %+v and %+v", version, versionDecoded) t.Fatalf("expected both version payload to be equal: %+v and %+v", version, versionDecoded)
} }
if version.Size() != uint32(minVersionSize+len(version.UserAgent)) {
t.Fatalf("Expected version size of %d", minVersionSize+len(version.UserAgent))
}
} }