Commit 754122dc authored by Levin Zimmermann's avatar Levin Zimmermann

go/neo/proto: Fix KnownMasterList nesting

Before this patch, the 'KnownMasterList' field of the 'NotPrimaryMaster'
was expected to be structured in the following way:

	ArrayHeader (KnownMasterList)

	    ArrayHeader (KnownMaster)

		ArrayHeader (Address)

		    Host (string)
		    Port (uint16)

However NEO/py sends the following structure:

	ArrayHeader (KnownMasterList)

	    ArrayHeader (Address)

		Host (string)
		Port (uint16)

This also makes sense, as 'KnownMaster' doesn't need to add another
nesting, because it only includes the address.

This patch amends the NEO/go protocol definition to transparently
represent the nesting as it's send by NEO/py. See also
18287612 for a similar issue.
parent 6d75eb3b
...@@ -138,7 +138,7 @@ func (node *_MasteredNode) TalkMaster(ctx context.Context, f func(context.Contex ...@@ -138,7 +138,7 @@ func (node *_MasteredNode) TalkMaster(ctx context.Context, f func(context.Contex
} else { } else {
// TODO update masterRegistry from received KnownMasterList // TODO update masterRegistry from received KnownMasterList
primary := notPrimaryMaster.KnownMasterList[p] primary := notPrimaryMaster.KnownMasterList[p]
maddr = primary.Address.String() maddr = primary.String()
log.Info(ctx, "switching to try %s as primary master", maddr) log.Info(ctx, "switching to try %s as primary master", maddr)
} }
} }
......
...@@ -37,7 +37,7 @@ func TestNotPrimaryMasterError(t *testing.T) { ...@@ -37,7 +37,7 @@ func TestNotPrimaryMasterError(t *testing.T) {
"not primary master; primary: ? ; known masters: []", "not primary master; primary: ? ; known masters: []",
}, },
{&NotPrimaryMaster{1, []struct{Address}{{Address{"α",111}}, {Address{"β",222}}, {Address{"γ",333}}}}, {&NotPrimaryMaster{1, []Address{{"α",111}, {"β",222}, {"γ",333}}},
"not primary master; primary: β:222 ; known masters: [α:111 β:222 γ:333]", "not primary master; primary: β:222 ; known masters: [α:111 β:222 γ:333]",
}, },
} }
......
...@@ -457,9 +457,7 @@ type AnswerPrimary struct { ...@@ -457,9 +457,7 @@ type AnswerPrimary struct {
//neo:nodes SM -> * //neo:nodes SM -> *
type NotPrimaryMaster struct { type NotPrimaryMaster struct {
Primary int32 // index of PM in KnownMasterList Primary int32 // index of PM in KnownMasterList
KnownMasterList []struct { KnownMasterList []Address
Address
}
} }
// Notify information about one or more nodes. // Notify information about one or more nodes.
......
...@@ -359,15 +359,15 @@ func TestMsgMarshal(t *testing.T) { ...@@ -359,15 +359,15 @@ func TestMsgMarshal(t *testing.T) {
}, },
// NotPrimaryMaster (.Primary used to have wrong type) // NotPrimaryMaster (.Primary used to have wrong type)
{&NotPrimaryMaster{0x01020304, []struct{Address}{{Address{"m111", 111}}, {Address{"m222", 222}}}}, {&NotPrimaryMaster{0x01020304, []Address{{"m111", 111}, {"m222", 222}}},
// N // N
u32(0x01020304) + u32(2) + u32(4)+"m111"+u16(111) + u32(4)+"m222"+u16(222), u32(0x01020304) + u32(2) + u32(4)+"m111"+u16(111) + u32(4)+"m222"+u16(222),
// M // M
hex("92") + hex("92") +
hex("d2" + "01020304") + hex("d2" + "01020304") +
hex("92") + hex("92") +
hex("9192") + hex("c4")+u8(4)+"m111" + u8(111) + hex("92") + hex("c4")+u8(4)+"m111" + u8(111) +
hex("9192") + hex("c4")+u8(4)+"m222" + hex("ccde"), hex("92") + hex("c4")+u8(4)+"m222" + hex("ccde"),
}, },
// TODO we need tests for: // TODO we need tests for:
......
...@@ -823,7 +823,7 @@ func (p *NotPrimaryMaster) neoMsgDecodeN(data []byte) (int, error) { ...@@ -823,7 +823,7 @@ func (p *NotPrimaryMaster) neoMsgDecodeN(data []byte) (int, error) {
{ {
l := binary.BigEndian.Uint32(data[4 : 4+4]) l := binary.BigEndian.Uint32(data[4 : 4+4])
data = data[8:] data = data[8:]
p.KnownMasterList = make([]struct{ Address }, l) p.KnownMasterList = make([]Address, l)
for i := 0; uint32(i) < l; i++ { for i := 0; uint32(i) < l; i++ {
a := &p.KnownMasterList[i] a := &p.KnownMasterList[i]
{ {
...@@ -846,7 +846,7 @@ func (p *NotPrimaryMaster) neoMsgEncodedLenM() int { ...@@ -846,7 +846,7 @@ func (p *NotPrimaryMaster) neoMsgEncodedLenM() int {
var size int var size int
for i := 0; i < len(p.KnownMasterList); i++ { for i := 0; i < len(p.KnownMasterList); i++ {
a := &p.KnownMasterList[i] a := &p.KnownMasterList[i]
size += msgpack.ArrayHeadSize(reflect.TypeOf((*a)).NumField()) + msgpack.ArrayHeadSize(reflect.TypeOf((*a).Address).NumField()) + msgpack.BinHeadSize(len((*a).Address.Host)) + len((*a).Address.Host) + msgpack.Uint16Size((*a).Address.Port) size += msgpack.ArrayHeadSize(reflect.TypeOf((*a)).NumField()) + msgpack.BinHeadSize(len((*a).Host)) + len((*a).Host) + msgpack.Uint16Size((*a).Port)
} }
return msgpack.ArrayHeadSize(reflect.TypeOf((*p)).NumField()) + msgpack.Int32Size(p.Primary) + msgpack.ArrayHeadSize(len(p.KnownMasterList)) + size return msgpack.ArrayHeadSize(reflect.TypeOf((*p)).NumField()) + msgpack.Int32Size(p.Primary) + msgpack.ArrayHeadSize(len(p.KnownMasterList)) + size
} }
...@@ -863,17 +863,16 @@ func (p *NotPrimaryMaster) neoMsgEncodeM(data []byte) { ...@@ -863,17 +863,16 @@ func (p *NotPrimaryMaster) neoMsgEncodeM(data []byte) {
data = data[0+n:] data = data[0+n:]
for i := 0; i < l; i++ { for i := 0; i < l; i++ {
a := &p.KnownMasterList[i] a := &p.KnownMasterList[i]
data[0] = byte(msgpack.FixArray_4 | 1) data[0] = byte(msgpack.FixArray_4 | 2)
data[1] = byte(msgpack.FixArray_4 | 2)
{ {
l := len((*a).Address.Host) l := len((*a).Host)
n := msgpack.PutBinHead(data[2:], l) n := msgpack.PutBinHead(data[1:], l)
data = data[2+n:] data = data[1+n:]
copy(data, (*a).Address.Host) copy(data, (*a).Host)
data = data[l:] data = data[l:]
} }
{ {
n := msgpack.PutUint16(data[0:], (*a).Address.Port) n := msgpack.PutUint16(data[0:], (*a).Port)
data = data[0+n:] data = data[0+n:]
} }
} }
...@@ -906,7 +905,7 @@ func (p *NotPrimaryMaster) neoMsgDecodeM(data []byte) (int, error) { ...@@ -906,7 +905,7 @@ func (p *NotPrimaryMaster) neoMsgDecodeM(data []byte) (int, error) {
} }
nread += uint64(len(data) - len(tail)) nread += uint64(len(data) - len(tail))
data = tail data = tail
p.KnownMasterList = make([]struct{ Address }, l) p.KnownMasterList = make([]Address, l)
for i := 0; uint32(i) < l; i++ { for i := 0; uint32(i) < l; i++ {
a := &p.KnownMasterList[i] a := &p.KnownMasterList[i]
{ {
...@@ -917,29 +916,21 @@ func (p *NotPrimaryMaster) neoMsgDecodeM(data []byte) (int, error) { ...@@ -917,29 +916,21 @@ func (p *NotPrimaryMaster) neoMsgDecodeM(data []byte) (int, error) {
nread += uint64(len(data) - len(tail)) nread += uint64(len(data) - len(tail))
data = tail data = tail
} }
{
_, tail, err := msgp.ReadArrayHeaderBytes(data)
if err != nil {
return 0, mdecodeErr("NotPrimaryMaster.Address", err)
}
nread += uint64(len(data) - len(tail))
data = tail
}
{ {
b, tail, err := msgp.ReadBytesZC(data) b, tail, err := msgp.ReadBytesZC(data)
if err != nil { if err != nil {
return 0, mdecodeErr("NotPrimaryMaster.Address.Host", err) return 0, mdecodeErr("NotPrimaryMaster.Host", err)
} }
(*a).Address.Host = string(b) (*a).Host = string(b)
nread += uint64(len(data) - len(tail)) nread += uint64(len(data) - len(tail))
data = tail data = tail
} }
{ {
v, tail, err := msgp.ReadUint16Bytes(data) v, tail, err := msgp.ReadUint16Bytes(data)
if err != nil { if err != nil {
return 0, mdecodeErr("NotPrimaryMaster.Address.Port", err) return 0, mdecodeErr("NotPrimaryMaster.Port", err)
} }
(*a).Address.Port = v (*a).Port = v
nread += uint64(len(data) - len(tail)) nread += uint64(len(data) - len(tail))
data = tail data = tail
} }
......
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