Skip to content

Commit 2243736

Browse files
authored
feat: protocol debug logs using configured logger (#736)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent e65b6dd commit 2243736

File tree

12 files changed

+441
-214
lines changed

12 files changed

+441
-214
lines changed

protocol/blockfetch/client.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
6666
Name: ProtocolName,
6767
ProtocolId: ProtocolId,
6868
Muxer: protoOptions.Muxer,
69+
Logger: protoOptions.Logger,
6970
ErrorChan: protoOptions.ErrorChan,
7071
Mode: protoOptions.Mode,
7172
Role: protocol.ProtocolRoleClient,
@@ -80,6 +81,8 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
8081

8182
func (c *Client) Start() {
8283
c.onceStart.Do(func() {
84+
c.Protocol.Logger().
85+
Debug(fmt.Sprintf("starting protocol: %s", ProtocolName))
8386
c.Protocol.Start()
8487
// Start goroutine to cleanup resources on protocol shutdown
8588
go func() {
@@ -93,6 +96,8 @@ func (c *Client) Start() {
9396
func (c *Client) Stop() error {
9497
var err error
9598
c.onceStop.Do(func() {
99+
c.Protocol.Logger().
100+
Debug(fmt.Sprintf("stopping protocol: %s", ProtocolName))
96101
msg := NewMsgClientDone()
97102
err = c.SendMessage(msg)
98103
})
@@ -101,6 +106,8 @@ func (c *Client) Stop() error {
101106

102107
// GetBlockRange starts an async process to fetch all blocks in the specified range (inclusive)
103108
func (c *Client) GetBlockRange(start common.Point, end common.Point) error {
109+
c.Protocol.Logger().
110+
Debug(fmt.Sprintf("client called %s GetBlockRange(start: %+v, end: %+v)", ProtocolName, start, end))
104111
c.busyMutex.Lock()
105112
c.blockUseCallback = true
106113
msg := NewMsgRequestRange(start, end)
@@ -121,6 +128,8 @@ func (c *Client) GetBlockRange(start common.Point, end common.Point) error {
121128

122129
// GetBlock requests and returns a single block specified by the provided point
123130
func (c *Client) GetBlock(point common.Point) (ledger.Block, error) {
131+
c.Protocol.Logger().
132+
Debug(fmt.Sprintf("client called %s GetBlock(point: %+v)", ProtocolName, point))
124133
c.busyMutex.Lock()
125134
c.blockUseCallback = false
126135
msg := NewMsgRequestRange(point, point)
@@ -144,6 +153,8 @@ func (c *Client) GetBlock(point common.Point) (ledger.Block, error) {
144153
}
145154

146155
func (c *Client) messageHandler(msg protocol.Message) error {
156+
c.Protocol.Logger().
157+
Debug(fmt.Sprintf("handling client message for %s", ProtocolName))
147158
var err error
148159
switch msg.Type() {
149160
case MessageTypeStartBatch:
@@ -165,17 +176,23 @@ func (c *Client) messageHandler(msg protocol.Message) error {
165176
}
166177

167178
func (c *Client) handleStartBatch() error {
179+
c.Protocol.Logger().
180+
Debug(fmt.Sprintf("handling client start batch for %s", ProtocolName))
168181
c.startBatchResultChan <- nil
169182
return nil
170183
}
171184

172185
func (c *Client) handleNoBlocks() error {
186+
c.Protocol.Logger().
187+
Debug(fmt.Sprintf("handling client no blocks found for %s", ProtocolName))
173188
err := fmt.Errorf("block(s) not found")
174189
c.startBatchResultChan <- err
175190
return nil
176191
}
177192

178193
func (c *Client) handleBlock(msgGeneric protocol.Message) error {
194+
c.Protocol.Logger().
195+
Debug(fmt.Sprintf("handling client block found for %s", ProtocolName))
179196
msg := msgGeneric.(*MsgBlock)
180197
// Decode only enough to get the block type value
181198
var wrappedBlock WrappedBlock
@@ -201,6 +218,8 @@ func (c *Client) handleBlock(msgGeneric protocol.Message) error {
201218
}
202219

203220
func (c *Client) handleBatchDone() error {
221+
c.Protocol.Logger().
222+
Debug(fmt.Sprintf("handling client batch done for %s", ProtocolName))
204223
c.busyMutex.Unlock()
205224
return nil
206225
}

protocol/blockfetch/server.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func (s *Server) initProtocol() {
4747
Name: ProtocolName,
4848
ProtocolId: ProtocolId,
4949
Muxer: s.protoOptions.Muxer,
50+
Logger: s.protoOptions.Logger,
5051
ErrorChan: s.protoOptions.ErrorChan,
5152
Mode: s.protoOptions.Mode,
5253
Role: protocol.ProtocolRoleServer,
@@ -59,16 +60,22 @@ func (s *Server) initProtocol() {
5960
}
6061

6162
func (s *Server) NoBlocks() error {
63+
s.Protocol.Logger().
64+
Debug(fmt.Sprintf("server called %s NoBlocks()", ProtocolName))
6265
msg := NewMsgNoBlocks()
6366
return s.SendMessage(msg)
6467
}
6568

6669
func (s *Server) StartBatch() error {
70+
s.Protocol.Logger().
71+
Debug(fmt.Sprintf("server called %s StartBatch()", ProtocolName))
6772
msg := NewMsgStartBatch()
6873
return s.SendMessage(msg)
6974
}
7075

7176
func (s *Server) Block(blockType uint, blockData []byte) error {
77+
s.Protocol.Logger().
78+
Debug(fmt.Sprintf("server called %s Block(blockType: %+v, blockData: %x)", ProtocolName, blockType, blockData))
7279
wrappedBlock := WrappedBlock{
7380
Type: blockType,
7481
RawBlock: blockData,
@@ -82,11 +89,15 @@ func (s *Server) Block(blockType uint, blockData []byte) error {
8289
}
8390

8491
func (s *Server) BatchDone() error {
92+
s.Protocol.Logger().
93+
Debug(fmt.Sprintf("server called %s BatchDone()", ProtocolName))
8594
msg := NewMsgBatchDone()
8695
return s.SendMessage(msg)
8796
}
8897

8998
func (s *Server) messageHandler(msg protocol.Message) error {
99+
s.Protocol.Logger().
100+
Debug(fmt.Sprintf("handling server message for %s", ProtocolName))
90101
var err error
91102
switch msg.Type() {
92103
case MessageTypeRequestRange:
@@ -104,6 +115,8 @@ func (s *Server) messageHandler(msg protocol.Message) error {
104115
}
105116

106117
func (s *Server) handleRequestRange(msg protocol.Message) error {
118+
s.Protocol.Logger().
119+
Debug(fmt.Sprintf("handling server request range for %s", ProtocolName))
107120
if s.config == nil || s.config.RequestRangeFunc == nil {
108121
return fmt.Errorf(
109122
"received block-fetch RequestRange message but no callback function is defined",
@@ -118,6 +131,8 @@ func (s *Server) handleRequestRange(msg protocol.Message) error {
118131
}
119132

120133
func (s *Server) handleClientDone() error {
134+
s.Protocol.Logger().
135+
Debug(fmt.Sprintf("handling server client done for %s", ProtocolName))
121136
// Restart protocol
122137
s.Protocol.Stop()
123138
s.initProtocol()

protocol/chainsync/client.go

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func NewClient(
100100
Name: ProtocolName,
101101
ProtocolId: ProtocolId,
102102
Muxer: protoOptions.Muxer,
103+
Logger: protoOptions.Logger,
103104
ErrorChan: protoOptions.ErrorChan,
104105
Mode: protoOptions.Mode,
105106
Role: protocol.ProtocolRoleClient,
@@ -115,6 +116,8 @@ func NewClient(
115116

116117
func (c *Client) Start() {
117118
c.onceStart.Do(func() {
119+
c.Protocol.Logger().
120+
Debug(fmt.Sprintf("starting protocol: %s", ProtocolName))
118121
c.Protocol.Start()
119122
// Start goroutine to cleanup resources on protocol shutdown
120123
go func() {
@@ -124,33 +127,12 @@ func (c *Client) Start() {
124127
})
125128
}
126129

127-
func (c *Client) messageHandler(msg protocol.Message) error {
128-
var err error
129-
switch msg.Type() {
130-
case MessageTypeAwaitReply:
131-
err = c.handleAwaitReply()
132-
case MessageTypeRollForward:
133-
err = c.handleRollForward(msg)
134-
case MessageTypeRollBackward:
135-
err = c.handleRollBackward(msg)
136-
case MessageTypeIntersectFound:
137-
err = c.handleIntersectFound(msg)
138-
case MessageTypeIntersectNotFound:
139-
err = c.handleIntersectNotFound(msg)
140-
default:
141-
err = fmt.Errorf(
142-
"%s: received unexpected message type %d",
143-
ProtocolName,
144-
msg.Type(),
145-
)
146-
}
147-
return err
148-
}
149-
150130
// Stop transitions the protocol to the Done state. No more protocol operations will be possible afterward
151131
func (c *Client) Stop() error {
152132
var err error
153133
c.onceStop.Do(func() {
134+
c.Protocol.Logger().
135+
Debug(fmt.Sprintf("stopping protocol: %s", ProtocolName))
154136
c.busyMutex.Lock()
155137
defer c.busyMutex.Unlock()
156138
msg := NewMsgDone()
@@ -163,6 +145,8 @@ func (c *Client) Stop() error {
163145

164146
// GetCurrentTip returns the current chain tip
165147
func (c *Client) GetCurrentTip() (*Tip, error) {
148+
c.Protocol.Logger().
149+
Debug(fmt.Sprintf("client called %s GetCurrentTip()", ProtocolName))
166150
done := atomic.Bool{}
167151
requestResultChan := make(chan Tip, 1)
168152
requestErrorChan := make(chan error, 1)
@@ -220,6 +204,8 @@ func (c *Client) GetCurrentTip() (*Tip, error) {
220204
func (c *Client) GetAvailableBlockRange(
221205
intersectPoints []common.Point,
222206
) (common.Point, common.Point, error) {
207+
c.Protocol.Logger().
208+
Debug(fmt.Sprintf("client called %s GetAvailableBlockRange(intersectPoints: %+v)", ProtocolName, intersectPoints))
223209
c.busyMutex.Lock()
224210
defer c.busyMutex.Unlock()
225211

@@ -293,6 +279,8 @@ func (c *Client) GetAvailableBlockRange(
293279
// Sync begins a chain-sync operation using the provided intersect point(s). Incoming blocks will be delivered
294280
// via the RollForward callback function specified in the protocol config
295281
func (c *Client) Sync(intersectPoints []common.Point) error {
282+
c.Protocol.Logger().
283+
Debug(fmt.Sprintf("client called %s Sync(intersectPoints: %+v)", ProtocolName, intersectPoints))
296284
c.busyMutex.Lock()
297285
defer c.busyMutex.Unlock()
298286
// Use origin if no intersect points were specified
@@ -441,11 +429,40 @@ func (c *Client) requestFindIntersect(
441429
}
442430
}
443431

432+
func (c *Client) messageHandler(msg protocol.Message) error {
433+
c.Protocol.Logger().
434+
Debug(fmt.Sprintf("handling client message for %s", ProtocolName))
435+
var err error
436+
switch msg.Type() {
437+
case MessageTypeAwaitReply:
438+
err = c.handleAwaitReply()
439+
case MessageTypeRollForward:
440+
err = c.handleRollForward(msg)
441+
case MessageTypeRollBackward:
442+
err = c.handleRollBackward(msg)
443+
case MessageTypeIntersectFound:
444+
err = c.handleIntersectFound(msg)
445+
case MessageTypeIntersectNotFound:
446+
err = c.handleIntersectNotFound(msg)
447+
default:
448+
err = fmt.Errorf(
449+
"%s: received unexpected message type %d",
450+
ProtocolName,
451+
msg.Type(),
452+
)
453+
}
454+
return err
455+
}
456+
444457
func (c *Client) handleAwaitReply() error {
458+
c.Protocol.Logger().
459+
Debug(fmt.Sprintf("handling client await reply for %s", ProtocolName))
445460
return nil
446461
}
447462

448463
func (c *Client) handleRollForward(msgGeneric protocol.Message) error {
464+
c.Protocol.Logger().
465+
Debug(fmt.Sprintf("handling client roll forward for %s", ProtocolName))
449466
firstBlockChan := func() chan<- clientPointResult {
450467
select {
451468
case ch := <-c.wantFirstBlockChan:
@@ -554,6 +571,8 @@ func (c *Client) handleRollForward(msgGeneric protocol.Message) error {
554571
}
555572

556573
func (c *Client) handleRollBackward(msg protocol.Message) error {
574+
c.Protocol.Logger().
575+
Debug(fmt.Sprintf("handling client roll backward for %s", ProtocolName))
557576
msgRollBackward := msg.(*MsgRollBackward)
558577
c.sendCurrentTip(msgRollBackward.Tip)
559578
if len(c.wantFirstBlockChan) == 0 {
@@ -579,6 +598,8 @@ func (c *Client) handleRollBackward(msg protocol.Message) error {
579598
}
580599

581600
func (c *Client) handleIntersectFound(msg protocol.Message) error {
601+
c.Protocol.Logger().
602+
Debug(fmt.Sprintf("handling client intersect found for %s", ProtocolName))
582603
msgIntersectFound := msg.(*MsgIntersectFound)
583604
c.sendCurrentTip(msgIntersectFound.Tip)
584605

@@ -591,6 +612,8 @@ func (c *Client) handleIntersectFound(msg protocol.Message) error {
591612
}
592613

593614
func (c *Client) handleIntersectNotFound(msgGeneric protocol.Message) error {
615+
c.Protocol.Logger().
616+
Debug(fmt.Sprintf("handling client intersect not found for %s", ProtocolName))
594617
msgIntersectNotFound := msgGeneric.(*MsgIntersectNotFound)
595618
c.sendCurrentTip(msgIntersectNotFound.Tip)
596619

protocol/chainsync/server.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func (s *Server) initProtocol() {
6464
Name: ProtocolName,
6565
ProtocolId: ProtocolId,
6666
Muxer: s.protoOptions.Muxer,
67+
Logger: s.protoOptions.Logger,
6768
ErrorChan: s.protoOptions.ErrorChan,
6869
Mode: s.protoOptions.Mode,
6970
Role: protocol.ProtocolRoleServer,
@@ -77,16 +78,22 @@ func (s *Server) initProtocol() {
7778
}
7879

7980
func (s *Server) RollBackward(point common.Point, tip Tip) error {
81+
s.Protocol.Logger().
82+
Debug(fmt.Sprintf("server called %s RollBackward(point: %+v, tip: %+v)", ProtocolName, point, tip))
8083
msg := NewMsgRollBackward(point, tip)
8184
return s.SendMessage(msg)
8285
}
8386

8487
func (s *Server) AwaitReply() error {
88+
s.Protocol.Logger().
89+
Debug(fmt.Sprintf("server called %s AwaitReply()", ProtocolName))
8590
msg := NewMsgAwaitReply()
8691
return s.SendMessage(msg)
8792
}
8893

8994
func (s *Server) RollForward(blockType uint, blockData []byte, tip Tip) error {
95+
s.Protocol.Logger().
96+
Debug(fmt.Sprintf("server called %s Rollforward(blockType: %+v, blockData: %x, tip: %+v)", ProtocolName, blockType, blockData, tip))
9097
if s.Mode() == protocol.ProtocolModeNodeToNode {
9198
eraId := ledger.BlockToBlockHeaderTypeMap[blockType]
9299
msg := NewMsgRollForwardNtN(
@@ -107,6 +114,8 @@ func (s *Server) RollForward(blockType uint, blockData []byte, tip Tip) error {
107114
}
108115

109116
func (s *Server) messageHandler(msg protocol.Message) error {
117+
s.Protocol.Logger().
118+
Debug(fmt.Sprintf("handling server message for %s", ProtocolName))
110119
var err error
111120
switch msg.Type() {
112121
case MessageTypeRequestNext:
@@ -126,6 +135,10 @@ func (s *Server) messageHandler(msg protocol.Message) error {
126135
}
127136

128137
func (s *Server) handleRequestNext() error {
138+
// TODO: figure out why this one log message causes a panic (and only this one)
139+
// during tests
140+
// s.Protocol.Logger().
141+
// Debug(fmt.Sprintf("handling server request next for %s", ProtocolName))
129142
if s.config == nil || s.config.RequestNextFunc == nil {
130143
return fmt.Errorf(
131144
"received chain-sync RequestNext message but no callback function is defined",
@@ -135,6 +148,8 @@ func (s *Server) handleRequestNext() error {
135148
}
136149

137150
func (s *Server) handleFindIntersect(msg protocol.Message) error {
151+
s.Protocol.Logger().
152+
Debug(fmt.Sprintf("handling server find intersect for %s", ProtocolName))
138153
if s.config == nil || s.config.FindIntersectFunc == nil {
139154
return fmt.Errorf(
140155
"received chain-sync FindIntersect message but no callback function is defined",
@@ -163,6 +178,8 @@ func (s *Server) handleFindIntersect(msg protocol.Message) error {
163178
}
164179

165180
func (s *Server) handleDone() error {
181+
s.Protocol.Logger().
182+
Debug(fmt.Sprintf("handling server done for %s", ProtocolName))
166183
// Restart protocol
167184
s.Protocol.Stop()
168185
s.initProtocol()

0 commit comments

Comments
 (0)