Skip to content

Commit

Permalink
message: rename structs in preparation of proto modes (ava-labs#1974)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuho authored Sep 12, 2022
1 parent 2dae25c commit de50c8b
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 185 deletions.
16 changes: 8 additions & 8 deletions message/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func init() {
panic(err)
}
TestCodec = codec
UncompressingBuilder = NewOutboundBuilder(codec, false /*compress*/)
TestInboundMsgBuilder = NewInboundBuilder(codec)
UncompressingBuilder = NewOutboundBuilderWithPacker(codec, false /*compress*/)
TestInboundMsgBuilder = NewInboundBuilderWithPacker(codec)
}

func TestBuildVersion(t *testing.T) {
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestBuildPut(t *testing.T) {
container := []byte{2}

for _, compress := range []bool{false, true} {
builder := NewOutboundBuilder(TestCodec, compress)
builder := NewOutboundBuilderWithPacker(TestCodec, compress)
msg, err := builder.Put(chainID, requestID, containerID, container)
require.NoError(t, err)
require.NotNil(t, msg)
Expand All @@ -215,7 +215,7 @@ func TestBuildPushQuery(t *testing.T) {
container := []byte{2}

for _, compress := range []bool{false, true} {
builder := NewOutboundBuilder(TestCodec, compress)
builder := NewOutboundBuilderWithPacker(TestCodec, compress)
msg, err := builder.PushQuery(chainID, requestID, time.Duration(deadline), containerID, container)
require.NoError(t, err)
require.NotNil(t, msg)
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestBuildAncestors(t *testing.T) {
containers := [][]byte{container[:], container2[:]}

for _, compress := range []bool{false, true} {
builder := NewOutboundBuilder(TestCodec, compress)
builder := NewOutboundBuilderWithPacker(TestCodec, compress)
msg, err := builder.Ancestors(chainID, requestID, containers)
require.NoError(t, err)
require.NotNil(t, msg)
Expand All @@ -335,7 +335,7 @@ func TestBuildAppRequestMsg(t *testing.T) {
deadline := uint64(time.Now().Unix())

for _, compress := range []bool{false, true} {
builder := NewOutboundBuilder(TestCodec, compress)
builder := NewOutboundBuilderWithPacker(TestCodec, compress)
msg, err := builder.AppRequest(chainID, 1, time.Duration(deadline), appRequestBytes)
require.NoError(t, err)
require.NotNil(t, msg)
Expand All @@ -355,7 +355,7 @@ func TestBuildAppResponseMsg(t *testing.T) {
appResponseBytes[len(appResponseBytes)-1] = 1

for _, compress := range []bool{false, true} {
builder := NewOutboundBuilder(TestCodec, compress)
builder := NewOutboundBuilderWithPacker(TestCodec, compress)
msg, err := builder.AppResponse(chainID, 1, appResponseBytes)
require.NoError(t, err)
require.NotNil(t, msg)
Expand All @@ -378,7 +378,7 @@ func TestBuildAppGossipMsg(t *testing.T) {
appGossipBytes[len(appGossipBytes)-1] = 1

for _, compress := range []bool{false, true} {
testBuilder := NewOutboundBuilder(TestCodec, compress)
testBuilder := NewOutboundBuilderWithPacker(TestCodec, compress)
msg, err := testBuilder.AppGossip(chainID, appGossipBytes)
require.NoError(t, err)
require.NotNil(t, msg)
Expand Down
16 changes: 9 additions & 7 deletions message/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,14 @@ func (c *codec) Parse(bytes []byte, nodeID ids.NodeID, onFinishedHandling func()
expirationTime = c.clock.Time().Add(deadlineDuration)
}

return &inboundMessage{
op: op,
fields: fieldValues,
bytesSavedCompression: bytesSaved,
nodeID: nodeID,
expirationTime: expirationTime,
onFinishedHandling: onFinishedHandling,
return &inboundMessageWithPacker{
inboundMessage: inboundMessage{
op: op,
bytesSavedCompression: bytesSaved,
nodeID: nodeID,
expirationTime: expirationTime,
onFinishedHandling: onFinishedHandling,
},
fields: fieldValues,
}, nil
}
72 changes: 52 additions & 20 deletions message/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ func TestDeadlineOverride(t *testing.T) {
require.NoError(t, err)

id := ids.GenerateTestID()
m := inboundMessage{
op: PushQuery,
m := inboundMessageWithPacker{
inboundMessage: inboundMessage{
op: PushQuery,
},
fields: map[Field]interface{}{
ChainID: id[:],
RequestID: uint32(1337),
Expand All @@ -82,7 +84,7 @@ func TestDeadlineOverride(t *testing.T) {
unpackedIntf, err := c.Parse(packedIntf.Bytes(), dummyNodeID, dummyOnFinishedHandling)
require.NoError(t, err, "failed to parse w/ compression on operation %s", m.op)

unpacked := unpackedIntf.(*inboundMessage)
unpacked := unpackedIntf.(*inboundMessageWithPacker)
require.NotEqual(t, unpacked.ExpirationTime(), time.Now().Add(1337*time.Hour))
require.True(t, time.Since(unpacked.ExpirationTime()) <= 10*time.Second)
}
Expand All @@ -98,9 +100,11 @@ func TestCodecPackParseGzip(t *testing.T) {
require.NoError(t, err)
cert := tlsCert.Leaf

msgs := []inboundMessage{
msgs := []inboundMessageWithPacker{
{
op: Version,
inboundMessage: inboundMessage{
op: Version,
},
fields: map[Field]interface{}{
NetworkID: uint32(0),
NodeID: uint32(1337),
Expand All @@ -113,7 +117,9 @@ func TestCodecPackParseGzip(t *testing.T) {
},
},
{
op: PeerList,
inboundMessage: inboundMessage{
op: PeerList,
},
fields: map[Field]interface{}{
Peers: []ips.ClaimedIPPort{
{
Expand All @@ -126,33 +132,43 @@ func TestCodecPackParseGzip(t *testing.T) {
},
},
{
op: Ping,
inboundMessage: inboundMessage{
op: Ping,
},
fields: map[Field]interface{}{},
},
{
op: Pong,
inboundMessage: inboundMessage{
op: Pong,
},
fields: map[Field]interface{}{
Uptime: uint8(80),
},
},
{
op: GetAcceptedFrontier,
inboundMessage: inboundMessage{
op: GetAcceptedFrontier,
},
fields: map[Field]interface{}{
ChainID: id[:],
RequestID: uint32(1337),
Deadline: uint64(time.Now().Unix()),
},
},
{
op: AcceptedFrontier,
inboundMessage: inboundMessage{
op: AcceptedFrontier,
},
fields: map[Field]interface{}{
ChainID: id[:],
RequestID: uint32(1337),
ContainerIDs: [][]byte{id[:]},
},
},
{
op: GetAccepted,
inboundMessage: inboundMessage{
op: GetAccepted,
},
fields: map[Field]interface{}{
ChainID: id[:],
RequestID: uint32(1337),
Expand All @@ -161,23 +177,29 @@ func TestCodecPackParseGzip(t *testing.T) {
},
},
{
op: Accepted,
inboundMessage: inboundMessage{
op: Accepted,
},
fields: map[Field]interface{}{
ChainID: id[:],
RequestID: uint32(1337),
ContainerIDs: [][]byte{id[:]},
},
},
{
op: Ancestors,
inboundMessage: inboundMessage{
op: Ancestors,
},
fields: map[Field]interface{}{
ChainID: id[:],
RequestID: uint32(1337),
MultiContainerBytes: [][]byte{id[:]},
},
},
{
op: Get,
inboundMessage: inboundMessage{
op: Get,
},
fields: map[Field]interface{}{
ChainID: id[:],
RequestID: uint32(1337),
Expand All @@ -186,7 +208,9 @@ func TestCodecPackParseGzip(t *testing.T) {
},
},
{
op: Put,
inboundMessage: inboundMessage{
op: Put,
},
fields: map[Field]interface{}{
ChainID: id[:],
RequestID: uint32(1337),
Expand All @@ -195,7 +219,9 @@ func TestCodecPackParseGzip(t *testing.T) {
},
},
{
op: PushQuery,
inboundMessage: inboundMessage{
op: PushQuery,
},
fields: map[Field]interface{}{
ChainID: id[:],
RequestID: uint32(1337),
Expand All @@ -205,7 +231,9 @@ func TestCodecPackParseGzip(t *testing.T) {
},
},
{
op: PullQuery,
inboundMessage: inboundMessage{
op: PullQuery,
},
fields: map[Field]interface{}{
ChainID: id[:],
RequestID: uint32(1337),
Expand All @@ -214,15 +242,19 @@ func TestCodecPackParseGzip(t *testing.T) {
},
},
{
op: Chits,
inboundMessage: inboundMessage{
op: Chits,
},
fields: map[Field]interface{}{
ChainID: id[:],
RequestID: uint32(1337),
ContainerIDs: [][]byte{id[:]},
},
},
{
op: ChitsV2,
inboundMessage: inboundMessage{
op: ChitsV2,
},
fields: map[Field]interface{}{
ChainID: id[:],
RequestID: uint32(1337),
Expand All @@ -238,7 +270,7 @@ func TestCodecPackParseGzip(t *testing.T) {
unpackedIntf, err := c.Parse(packedIntf.Bytes(), dummyNodeID, dummyOnFinishedHandling)
require.NoError(t, err, "failed to parse w/ compression on operation %s", m.op)

unpacked := unpackedIntf.(*inboundMessage)
unpacked := unpackedIntf.(*inboundMessageWithPacker)

require.EqualValues(t, len(m.fields), len(unpacked.fields))
}
Expand Down
4 changes: 2 additions & 2 deletions message/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func NewCreator(metrics prometheus.Registerer, compressionEnabled bool, parentNa
return nil, err
}
return &creator{
OutboundMsgBuilder: NewOutboundBuilder(codec, compressionEnabled),
InboundMsgBuilder: NewInboundBuilder(codec),
OutboundMsgBuilder: NewOutboundBuilderWithPacker(codec, compressionEnabled),
InboundMsgBuilder: NewInboundBuilderWithPacker(codec),
InternalMsgBuilder: NewInternalBuilder(),
}, nil
}
Loading

0 comments on commit de50c8b

Please sign in to comment.