Commit 43c19d59 authored by Kirill Smelkov's avatar Kirill Smelkov

fixup! proto.NotPrimaryMaster: Fix .Primary data type (2)

Change .Promary type from int8 back to int32.

5d93e434 says that .Primary type is not NodeID. That is true, but
changing it to int8 was a mistake:

1. PSignedNull is explicitly defined to come with '!l' struct code, which
   according to https://docs.python.org/3/library/struct.html#module-struct
   comes as 4-bytes integer on the wire:

   https://lab.nexedi.com/nexedi/neoppod/blob/v1.12-13-gf2ea4be2/neo/lib/protocol.py#L560-562

2. verifying this via serializing NotPrimaryMaster on NEO/py also
   confirms that .Primary occupies 4 bytes, not one:

   In [1]: from neo.lib.protocol import NotPrimaryMaster

   In [2]: NotPrimaryMaster(0x01020304, [('m111', 111), ('m222', 222)])._body
   Out[2]: '\x01\x02\x03\x04\x00\x00\x00\x02\x00\x00\x00\x04m111\x00o\x00\x00\x00\x04m222\x00\xde'
            ^^^^^^^^^^^^^^^^
            NOTE  NOTE  NOTE

-> change .Primary type back to being 4-bytes integer, but to int32
instead of NodeID because, as 5d93e434 correctly says, .Primary comes as
array index, not a node ID. The following place of NEO/py code
explicitly confirms this:

https://lab.nexedi.com/nexedi/neoppod/blob/v1.12-13-gf2ea4be2/neo/master/handlers/identification.py#L155-159

Add corresponding test.
parent 3cb82317
// Copyright (C) 2006-2021 Nexedi SA and Contributors.
// Copyright (C) 2006-2023 Nexedi SA and Contributors.
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
......@@ -457,7 +457,7 @@ type AnswerPrimary struct {
//
//neo:nodes SM -> *
type NotPrimaryMaster struct {
Primary int8
Primary int32 // index of PM in KnownMasterList
KnownMasterList []struct {
Address
}
......
// Copyright (C) 2016-2021 Nexedi SA and Contributors.
// Copyright (C) 2016-2023 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
......@@ -356,6 +356,18 @@ func TestMsgMarshal(t *testing.T) {
hex("90"),
},
// NotPrimaryMaster (.Primary used to have wrong type)
{&NotPrimaryMaster{0x01020304, []struct{Address}{{Address{"m111", 111}}, {Address{"m222", 222}}}},
// N
u32(0x01020304) + u32(2) + u32(4)+"m111"+u16(111) + u32(4)+"m222"+u16(222),
// M
hex("92") +
hex("d2" + "01020304") +
hex("92") +
hex("9192") + hex("c4")+u8(4)+"m111" + u8(111) +
hex("9192") + hex("c4")+u8(4)+"m222" + hex("ccde"),
},
// TODO we need tests for:
// []varsize + trailing
// map[]varsize + trailing
......
......@@ -783,15 +783,15 @@ func (p *NotPrimaryMaster) neoMsgEncodedLenN() int {
a := &p.KnownMasterList[i]
size += (*a).neoEncodedLenN()
}
return 5 + size
return 8 + size
}
func (p *NotPrimaryMaster) neoMsgEncodeN(data []byte) {
(data[0:])[0] = uint8(p.Primary)
binary.BigEndian.PutUint32(data[0:], uint32(p.Primary))
{
l := len(p.KnownMasterList)
binary.BigEndian.PutUint32(data[1:], uint32(l))
data = data[5:]
binary.BigEndian.PutUint32(data[4:], uint32(l))
data = data[8:]
for i := 0; i < l; i++ {
a := &p.KnownMasterList[i]
{
......@@ -804,13 +804,13 @@ func (p *NotPrimaryMaster) neoMsgEncodeN(data []byte) {
func (p *NotPrimaryMaster) neoMsgDecodeN(data []byte) (int, error) {
var nread uint64
if len(data) < 5 {
if len(data) < 8 {
goto overflow
}
p.Primary = int8((data[0 : 0+1])[0])
p.Primary = int32(binary.BigEndian.Uint32(data[0 : 0+4]))
{
l := binary.BigEndian.Uint32(data[1 : 1+4])
data = data[5:]
l := binary.BigEndian.Uint32(data[4 : 4+4])
data = data[8:]
p.KnownMasterList = make([]struct{ Address }, l)
for i := 0; uint32(i) < l; i++ {
a := &p.KnownMasterList[i]
......@@ -824,7 +824,7 @@ func (p *NotPrimaryMaster) neoMsgDecodeN(data []byte) (int, error) {
}
}
}
return 5 + int(nread), nil
return 8 + int(nread), nil
overflow:
return 0, ErrDecodeOverflow
......@@ -836,13 +836,13 @@ func (p *NotPrimaryMaster) neoMsgEncodedLenM() int {
a := &p.KnownMasterList[i]
size += msgpack.BinHeadSize(len((*a).Address.Host)) + len((*a).Address.Host) + msgpack.Uint16Size((*a).Address.Port)
}
return 1 + msgpack.Int8Size(p.Primary) + msgpack.ArrayHeadSize(len(p.KnownMasterList)) + len(p.KnownMasterList)*2 + size
return 1 + msgpack.Int32Size(p.Primary) + msgpack.ArrayHeadSize(len(p.KnownMasterList)) + len(p.KnownMasterList)*2 + size
}
func (p *NotPrimaryMaster) neoMsgEncodeM(data []byte) {
data[0] = byte(msgpack.FixArray_4 | 2)
{
n := msgpack.PutInt8(data[1:], p.Primary)
n := msgpack.PutInt32(data[1:], p.Primary)
data = data[1+n:]
}
{
......@@ -878,7 +878,7 @@ func (p *NotPrimaryMaster) neoMsgDecodeM(data []byte) (int, error) {
}
data = data[1:]
{
v, tail, err := msgp.ReadInt8Bytes(data)
v, tail, err := msgp.ReadInt32Bytes(data)
if err != nil {
return 0, mdecodeErr("NotPrimaryMaster.Primary", err)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment