Skip to content

Commit 8c88bf4

Browse files
authored
perf(protocol): fix chainsync rollforward debug log (#1907)
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
1 parent c7e54c7 commit 8c88bf4

2 files changed

Lines changed: 70 additions & 13 deletions

File tree

protocol/chainsync/server.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package chainsync
1717
import (
1818
"errors"
1919
"fmt"
20+
"log/slog"
2021
"sync"
2122

2223
"github.com/blinklabs-io/gouroboros/ledger"
@@ -151,19 +152,13 @@ func (s *Server) AwaitReply() error {
151152

152153
func (s *Server) RollForward(blockType uint, blockData []byte, tip Tip) error {
153154
p := s.ProtocolInstance()
154-
p.Logger().
155-
Debug(
156-
fmt.Sprintf("calling RollForward(blockType: %+x, blockData: %x, tip: {Point: {Slot: %d, Hash: %x}, BlockNumber: %d})",
157-
blockType,
158-
blockData,
159-
tip.Point.Slot, tip.Point.Hash,
160-
tip.BlockNumber,
161-
),
162-
"component", "network",
163-
"protocol", ProtocolName,
164-
"role", "server",
165-
"connection_id", s.callbackContext.ConnectionId.String(),
166-
)
155+
logRollForward(
156+
p.Logger(),
157+
blockType,
158+
blockData,
159+
tip,
160+
s.callbackContext.ConnectionId.String(),
161+
)
167162
if p.Mode() == protocol.ProtocolModeNodeToNode {
168163
eraId := ledger.BlockToBlockHeaderTypeMap[blockType]
169164
msg, err := NewMsgRollForwardNtN(
@@ -197,6 +192,27 @@ func (s *Server) RollForward(blockType uint, blockData []byte, tip Tip) error {
197192
}
198193
}
199194

195+
func logRollForward(
196+
logger *slog.Logger,
197+
blockType uint,
198+
blockData []byte,
199+
tip Tip,
200+
connectionId string,
201+
) {
202+
logger.Debug(
203+
"calling RollForward",
204+
"block_type", blockType,
205+
"block_size", len(blockData),
206+
"tip_slot", tip.Point.Slot,
207+
"tip_hash", tip.Point.Hash,
208+
"tip_block_number", tip.BlockNumber,
209+
"component", "network",
210+
"protocol", ProtocolName,
211+
"role", "server",
212+
"connection_id", connectionId,
213+
)
214+
}
215+
200216
func (s *Server) messageHandler(msg protocol.Message) error {
201217
var err error
202218
switch msg.Type() {

protocol/chainsync/server_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
package chainsync
1616

1717
import (
18+
"bytes"
19+
"log/slog"
1820
"testing"
1921

22+
pcommon "github.com/blinklabs-io/gouroboros/protocol/common"
2023
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
2125
)
2226

2327
func TestHandleRequestNext_Callback(t *testing.T) {
@@ -52,3 +56,40 @@ func TestHandleRequestNext_NilCallback(t *testing.T) {
5256
assert.Error(t, err, "expected an error due to nil callback")
5357
assert.EqualError(t, err, expectedError)
5458
}
59+
60+
func TestLogRollForwardDoesNotLogBlockData(t *testing.T) {
61+
var logOutput bytes.Buffer
62+
logger := slog.New(slog.NewTextHandler(
63+
&logOutput,
64+
&slog.HandlerOptions{Level: slog.LevelDebug},
65+
))
66+
tip := Tip{
67+
Point: pcommon.Point{
68+
Slot: 42,
69+
Hash: []byte{0xca, 0xfe},
70+
},
71+
BlockNumber: 24,
72+
}
73+
74+
logRollForward(
75+
logger,
76+
5,
77+
[]byte{0xde, 0xad, 0xbe, 0xef},
78+
tip,
79+
"connection-id",
80+
)
81+
82+
output := logOutput.String()
83+
require.NotEmpty(t, output)
84+
assert.Contains(t, output, "msg=\"calling RollForward\"")
85+
assert.Contains(t, output, "block_type=5")
86+
assert.Contains(t, output, "block_size=4")
87+
assert.Contains(t, output, "tip_slot=42")
88+
assert.Contains(t, output, "tip_block_number=24")
89+
assert.NotContains(
90+
t,
91+
output,
92+
"deadbeef",
93+
"serialized block data should not be logged",
94+
)
95+
}

0 commit comments

Comments
 (0)