Skip to content

Commit 400dd66

Browse files
Fix unreadable message errors (#1585)
1 parent cdf86ae commit 400dd66

File tree

5 files changed

+174
-6
lines changed

5 files changed

+174
-6
lines changed

message/internal_msg_builder.go

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package message
66

77
import (
8+
"fmt"
89
"time"
910

1011
"github.com/ava-labs/avalanchego/ids"
@@ -15,56 +16,80 @@ import (
1516

1617
var (
1718
disconnected = &Disconnected{}
18-
timeout = &Timeout{}
1919
gossipRequest = &GossipRequest{}
20+
timeout = &Timeout{}
2021

22+
_ fmt.Stringer = (*GetStateSummaryFrontierFailed)(nil)
2123
_ chainIDGetter = (*GetStateSummaryFrontierFailed)(nil)
2224
_ requestIDGetter = (*GetStateSummaryFrontierFailed)(nil)
2325

26+
_ fmt.Stringer = (*GetAcceptedStateSummaryFailed)(nil)
2427
_ chainIDGetter = (*GetAcceptedStateSummaryFailed)(nil)
2528
_ requestIDGetter = (*GetAcceptedStateSummaryFailed)(nil)
2629

30+
_ fmt.Stringer = (*GetAcceptedFrontierFailed)(nil)
2731
_ chainIDGetter = (*GetAcceptedFrontierFailed)(nil)
2832
_ requestIDGetter = (*GetAcceptedFrontierFailed)(nil)
2933
_ engineTypeGetter = (*GetAcceptedFrontierFailed)(nil)
3034

35+
_ fmt.Stringer = (*GetAcceptedFailed)(nil)
3136
_ chainIDGetter = (*GetAcceptedFailed)(nil)
3237
_ requestIDGetter = (*GetAcceptedFailed)(nil)
3338
_ engineTypeGetter = (*GetAcceptedFailed)(nil)
3439

40+
_ fmt.Stringer = (*GetAncestorsFailed)(nil)
3541
_ chainIDGetter = (*GetAncestorsFailed)(nil)
3642
_ requestIDGetter = (*GetAncestorsFailed)(nil)
3743
_ engineTypeGetter = (*GetAncestorsFailed)(nil)
3844

45+
_ fmt.Stringer = (*GetFailed)(nil)
3946
_ chainIDGetter = (*GetFailed)(nil)
4047
_ requestIDGetter = (*GetFailed)(nil)
4148
_ engineTypeGetter = (*GetFailed)(nil)
4249

50+
_ fmt.Stringer = (*QueryFailed)(nil)
4351
_ chainIDGetter = (*QueryFailed)(nil)
4452
_ requestIDGetter = (*QueryFailed)(nil)
4553
_ engineTypeGetter = (*QueryFailed)(nil)
4654

55+
_ fmt.Stringer = (*AppRequestFailed)(nil)
4756
_ chainIDGetter = (*AppRequestFailed)(nil)
4857
_ requestIDGetter = (*AppRequestFailed)(nil)
4958

59+
_ fmt.Stringer = (*CrossChainAppRequest)(nil)
5060
_ sourceChainIDGetter = (*CrossChainAppRequest)(nil)
5161
_ chainIDGetter = (*CrossChainAppRequest)(nil)
5262
_ requestIDGetter = (*CrossChainAppRequest)(nil)
5363

64+
_ fmt.Stringer = (*CrossChainAppRequestFailed)(nil)
5465
_ sourceChainIDGetter = (*CrossChainAppRequestFailed)(nil)
5566
_ chainIDGetter = (*CrossChainAppRequestFailed)(nil)
5667
_ requestIDGetter = (*CrossChainAppRequestFailed)(nil)
5768

69+
_ fmt.Stringer = (*CrossChainAppResponse)(nil)
5870
_ sourceChainIDGetter = (*CrossChainAppResponse)(nil)
5971
_ chainIDGetter = (*CrossChainAppResponse)(nil)
6072
_ requestIDGetter = (*CrossChainAppResponse)(nil)
73+
74+
_ fmt.Stringer = (*Disconnected)(nil)
75+
76+
_ fmt.Stringer = (*GossipRequest)(nil)
77+
78+
_ fmt.Stringer = (*Timeout)(nil)
6179
)
6280

6381
type GetStateSummaryFrontierFailed struct {
6482
ChainID ids.ID `json:"chain_id,omitempty"`
6583
RequestID uint32 `json:"request_id,omitempty"`
6684
}
6785

86+
func (m *GetStateSummaryFrontierFailed) String() string {
87+
return fmt.Sprintf(
88+
"ChainID: %s RequestID: %d",
89+
m.ChainID, m.RequestID,
90+
)
91+
}
92+
6893
func (m *GetStateSummaryFrontierFailed) GetChainId() []byte {
6994
return m.ChainID[:]
7095
}
@@ -94,6 +119,13 @@ type GetAcceptedStateSummaryFailed struct {
94119
RequestID uint32 `json:"request_id,omitempty"`
95120
}
96121

122+
func (m *GetAcceptedStateSummaryFailed) String() string {
123+
return fmt.Sprintf(
124+
"ChainID: %s RequestID: %d",
125+
m.ChainID, m.RequestID,
126+
)
127+
}
128+
97129
func (m *GetAcceptedStateSummaryFailed) GetChainId() []byte {
98130
return m.ChainID[:]
99131
}
@@ -124,6 +156,13 @@ type GetAcceptedFrontierFailed struct {
124156
EngineType p2p.EngineType `json:"engine_type,omitempty"`
125157
}
126158

159+
func (m *GetAcceptedFrontierFailed) String() string {
160+
return fmt.Sprintf(
161+
"ChainID: %s RequestID: %d EngineType: %s",
162+
m.ChainID, m.RequestID, m.EngineType,
163+
)
164+
}
165+
127166
func (m *GetAcceptedFrontierFailed) GetChainId() []byte {
128167
return m.ChainID[:]
129168
}
@@ -160,6 +199,13 @@ type GetAcceptedFailed struct {
160199
EngineType p2p.EngineType `json:"engine_type,omitempty"`
161200
}
162201

202+
func (m *GetAcceptedFailed) String() string {
203+
return fmt.Sprintf(
204+
"ChainID: %s RequestID: %d EngineType: %s",
205+
m.ChainID, m.RequestID, m.EngineType,
206+
)
207+
}
208+
163209
func (m *GetAcceptedFailed) GetChainId() []byte {
164210
return m.ChainID[:]
165211
}
@@ -196,6 +242,13 @@ type GetAncestorsFailed struct {
196242
EngineType p2p.EngineType `json:"engine_type,omitempty"`
197243
}
198244

245+
func (m *GetAncestorsFailed) String() string {
246+
return fmt.Sprintf(
247+
"ChainID: %s RequestID: %d EngineType: %s",
248+
m.ChainID, m.RequestID, m.EngineType,
249+
)
250+
}
251+
199252
func (m *GetAncestorsFailed) GetChainId() []byte {
200253
return m.ChainID[:]
201254
}
@@ -232,6 +285,13 @@ type GetFailed struct {
232285
EngineType p2p.EngineType `json:"engine_type,omitempty"`
233286
}
234287

288+
func (m *GetFailed) String() string {
289+
return fmt.Sprintf(
290+
"ChainID: %s RequestID: %d EngineType: %s",
291+
m.ChainID, m.RequestID, m.EngineType,
292+
)
293+
}
294+
235295
func (m *GetFailed) GetChainId() []byte {
236296
return m.ChainID[:]
237297
}
@@ -268,6 +328,13 @@ type QueryFailed struct {
268328
EngineType p2p.EngineType `json:"engine_type,omitempty"`
269329
}
270330

331+
func (m *QueryFailed) String() string {
332+
return fmt.Sprintf(
333+
"ChainID: %s RequestID: %d EngineType: %s",
334+
m.ChainID, m.RequestID, m.EngineType,
335+
)
336+
}
337+
271338
func (m *QueryFailed) GetChainId() []byte {
272339
return m.ChainID[:]
273340
}
@@ -303,6 +370,13 @@ type AppRequestFailed struct {
303370
RequestID uint32 `json:"request_id,omitempty"`
304371
}
305372

373+
func (m *AppRequestFailed) String() string {
374+
return fmt.Sprintf(
375+
"ChainID: %s RequestID: %d",
376+
m.ChainID, m.RequestID,
377+
)
378+
}
379+
306380
func (m *AppRequestFailed) GetChainId() []byte {
307381
return m.ChainID[:]
308382
}
@@ -334,6 +408,13 @@ type CrossChainAppRequest struct {
334408
Message []byte `json:"message,omitempty"`
335409
}
336410

411+
func (m *CrossChainAppRequest) String() string {
412+
return fmt.Sprintf(
413+
"SourceChainID: %s DestinationChainID: %s RequestID: %d Message: 0x%x",
414+
m.SourceChainID, m.DestinationChainID, m.RequestID, m.Message,
415+
)
416+
}
417+
337418
func (m *CrossChainAppRequest) GetSourceChainID() ids.ID {
338419
return m.SourceChainID
339420
}
@@ -373,6 +454,13 @@ type CrossChainAppRequestFailed struct {
373454
RequestID uint32 `json:"request_id,omitempty"`
374455
}
375456

457+
func (m *CrossChainAppRequestFailed) String() string {
458+
return fmt.Sprintf(
459+
"SourceChainID: %s DestinationChainID: %s RequestID: %d",
460+
m.SourceChainID, m.DestinationChainID, m.RequestID,
461+
)
462+
}
463+
376464
func (m *CrossChainAppRequestFailed) GetSourceChainID() ids.ID {
377465
return m.SourceChainID
378466
}
@@ -410,6 +498,13 @@ type CrossChainAppResponse struct {
410498
Message []byte `json:"message,omitempty"`
411499
}
412500

501+
func (m *CrossChainAppResponse) String() string {
502+
return fmt.Sprintf(
503+
"SourceChainID: %s DestinationChainID: %s RequestID: %d Message: 0x%x",
504+
m.SourceChainID, m.DestinationChainID, m.RequestID, m.Message,
505+
)
506+
}
507+
413508
func (m *CrossChainAppResponse) GetSourceChainID() ids.ID {
414509
return m.SourceChainID
415510
}
@@ -446,6 +541,13 @@ type Connected struct {
446541
NodeVersion *version.Application `json:"node_version,omitempty"`
447542
}
448543

544+
func (m *Connected) String() string {
545+
return fmt.Sprintf(
546+
"NodeVersion: %s",
547+
m.NodeVersion,
548+
)
549+
}
550+
449551
func InternalConnected(nodeID ids.NodeID, nodeVersion *version.Application) InboundMessage {
450552
return &inboundMessage{
451553
nodeID: nodeID,
@@ -463,6 +565,13 @@ type ConnectedSubnet struct {
463565
SubnetID ids.ID `json:"subnet_id,omitempty"`
464566
}
465567

568+
func (m *ConnectedSubnet) String() string {
569+
return fmt.Sprintf(
570+
"SubnetID: %s",
571+
m.SubnetID,
572+
)
573+
}
574+
466575
// InternalConnectedSubnet returns a message that indicates the node with [nodeID] is
467576
// connected to the subnet with the given [subnetID].
468577
func InternalConnectedSubnet(nodeID ids.NodeID, subnetID ids.ID) InboundMessage {
@@ -478,6 +587,10 @@ func InternalConnectedSubnet(nodeID ids.NodeID, subnetID ids.ID) InboundMessage
478587

479588
type Disconnected struct{}
480589

590+
func (Disconnected) String() string {
591+
return ""
592+
}
593+
481594
func InternalDisconnected(nodeID ids.NodeID) InboundMessage {
482595
return &inboundMessage{
483596
nodeID: nodeID,
@@ -491,6 +604,13 @@ type VMMessage struct {
491604
Notification uint32 `json:"notification,omitempty"`
492605
}
493606

607+
func (m *VMMessage) String() string {
608+
return fmt.Sprintf(
609+
"Notification: %d",
610+
m.Notification,
611+
)
612+
}
613+
494614
func InternalVMMessage(
495615
nodeID ids.NodeID,
496616
notification uint32,
@@ -507,6 +627,10 @@ func InternalVMMessage(
507627

508628
type GossipRequest struct{}
509629

630+
func (GossipRequest) String() string {
631+
return ""
632+
}
633+
510634
func InternalGossipRequest(
511635
nodeID ids.NodeID,
512636
) InboundMessage {
@@ -520,6 +644,10 @@ func InternalGossipRequest(
520644

521645
type Timeout struct{}
522646

647+
func (Timeout) String() string {
648+
return ""
649+
}
650+
523651
func InternalTimeout(nodeID ids.NodeID) InboundMessage {
524652
return &inboundMessage{
525653
nodeID: nodeID,

message/messages.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ var (
3434

3535
// InboundMessage represents a set of fields for an inbound message
3636
type InboundMessage interface {
37+
fmt.Stringer
3738
// NodeID returns the ID of the node that sent this message
3839
NodeID() ids.NodeID
3940
// Op returns the op that describes this message type
4041
Op() Op
4142
// Message returns the message that was sent
42-
Message() any
43+
Message() fmt.Stringer
4344
// Expiration returns the time that the sender will have already timed out
4445
// this request
4546
Expiration() time.Time
@@ -54,7 +55,7 @@ type InboundMessage interface {
5455
type inboundMessage struct {
5556
nodeID ids.NodeID
5657
op Op
57-
message any
58+
message fmt.Stringer
5859
expiration time.Time
5960
onFinishedHandling func()
6061
bytesSavedCompression int
@@ -68,7 +69,7 @@ func (m *inboundMessage) Op() Op {
6869
return m.op
6970
}
7071

71-
func (m *inboundMessage) Message() any {
72+
func (m *inboundMessage) Message() fmt.Stringer {
7273
return m.message
7374
}
7475

@@ -86,6 +87,11 @@ func (m *inboundMessage) BytesSavedCompression() int {
8687
return m.bytesSavedCompression
8788
}
8889

90+
func (m *inboundMessage) String() string {
91+
return fmt.Sprintf("%s Op: %s Message: %s",
92+
m.nodeID, m.op, m.message)
93+
}
94+
8995
// OutboundMessage represents a set of fields for an outbound message that can
9096
// be serialized into a byte stream
9197
type OutboundMessage interface {

message/messages_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,40 @@ func TestMessage(t *testing.T) {
846846
}
847847
}
848848

849+
// Tests the Stringer interface on inbound messages
850+
func TestInboundMessageToString(t *testing.T) {
851+
t.Parallel()
852+
853+
require := require.New(t)
854+
855+
mb, err := newMsgBuilder(
856+
logging.NoLog{},
857+
"test",
858+
prometheus.NewRegistry(),
859+
5*time.Second,
860+
)
861+
require.NoError(err)
862+
863+
// msg that will become the tested InboundMessage
864+
msg := &p2p.Message{
865+
Message: &p2p.Message_Pong{
866+
Pong: &p2p.Pong{
867+
Uptime: 100,
868+
},
869+
},
870+
}
871+
msgBytes, err := proto.Marshal(msg)
872+
require.NoError(err)
873+
874+
inboundMsg, err := mb.parseInbound(msgBytes, ids.EmptyNodeID, func() {})
875+
require.NoError(err)
876+
877+
require.Equal("NodeID-111111111111111111116DBWJs Op: pong Message: uptime:100", inboundMsg.String())
878+
879+
internalMsg := InternalGetStateSummaryFrontierFailed(ids.EmptyNodeID, ids.Empty, 1)
880+
require.Equal("NodeID-111111111111111111116DBWJs Op: get_state_summary_frontier_failed Message: ChainID: 11111111111111111111111111111111LpoYY RequestID: 1", internalMsg.String())
881+
}
882+
849883
func TestEmptyInboundMessage(t *testing.T) {
850884
t.Parallel()
851885

message/ops.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func (op Op) String() string {
296296
}
297297
}
298298

299-
func Unwrap(m *p2p.Message) (interface{}, error) {
299+
func Unwrap(m *p2p.Message) (fmt.Stringer, error) {
300300
switch msg := m.GetMessage().(type) {
301301
// Handshake:
302302
case *p2p.Message_Ping:

0 commit comments

Comments
 (0)