diff --git a/pkg/network/payload/version.go b/pkg/network/payload/version.go index 6545b19f5..8f050d9f7 100644 --- a/pkg/network/payload/version.go +++ b/pkg/network/payload/version.go @@ -7,6 +7,8 @@ import ( "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 // List of Services offered by the node @@ -83,5 +85,5 @@ func (p *Version) EncodeBinary(w io.Writer) error { // Size implements the payloader interface. func (p *Version) Size() uint32 { - return uint32(minVersionSize + len(p.UserAgent)) + return uint32(minVersionSize + util.GetVarSize(p.UserAgent)) } diff --git a/pkg/network/payload/version_test.go b/pkg/network/payload/version_test.go index 098a5d05f..97a724256 100644 --- a/pkg/network/payload/version_test.go +++ b/pkg/network/payload/version_test.go @@ -14,6 +14,10 @@ func TestVersionEncodeDecode(t *testing.T) { t.Fatal(err) } + if int(version.Size()) != buf.Len() { + t.Fatalf("Expected version size of %d", buf.Len()) + } + versionDecoded := &Version{} if err := versionDecoded.DecodeBinary(buf); err != nil { 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) } - if version.Size() != uint32(minVersionSize+len(version.UserAgent)) { - t.Fatalf("Expected version size of %d", minVersionSize+len(version.UserAgent)) - } }