Skip to content

Commit a251bca

Browse files
authored
p2p/discover: add more packet information in logs (#26307)
* p2p/discover: add more packet information in logs This adds more fields to discv5 packet logs. These can be useful when debugging multi-packet interactions. The FINDNODE message also gets an additional field, OpID for debugging purposes. This field is not encoded onto the wire. I'm also removing topic system related message types in this change. These will come back in the future, where support for them will be guarded by a config flag. * p2p/discover/v5wire: rename 'Total' to 'RespCount' The new name captures the meaning of this field better.
1 parent 9e6a1c3 commit a251bca

File tree

6 files changed

+105
-112
lines changed

6 files changed

+105
-112
lines changed

cmd/devp2p/internal/v5test/discv5tests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func (bn *bystander) loop() {
355355
wasAdded = true
356356
bn.notifyAdded()
357357
case *v5wire.Findnode:
358-
bn.conn.write(bn.l, &v5wire.Nodes{ReqID: p.ReqID, Total: 1}, nil)
358+
bn.conn.write(bn.l, &v5wire.Nodes{ReqID: p.ReqID, RespCount: 1}, nil)
359359
wasAdded = true
360360
bn.notifyAdded()
361361
case *v5wire.TalkRequest:

cmd/devp2p/internal/v5test/framework.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ func (p *readError) Unwrap() error { return p.err }
4444
func (p *readError) RequestID() []byte { return nil }
4545
func (p *readError) SetRequestID([]byte) {}
4646

47+
func (p *readError) AppendLogInfo(ctx []interface{}) []interface{} { return ctx }
48+
4749
// readErrorf creates a readError with the given text.
4850
func readErrorf(format string, args ...interface{}) *readError {
4951
return &readError{fmt.Errorf(format, args...)}
@@ -171,16 +173,16 @@ func (tc *conn) findnode(c net.PacketConn, dists []uint) ([]*enode.Node, error)
171173
// Check total count. It should be greater than one
172174
// and needs to be the same across all responses.
173175
if first {
174-
if resp.Total == 0 || resp.Total > 6 {
175-
return nil, fmt.Errorf("invalid NODES response 'total' %d (not in (0,7))", resp.Total)
176+
if resp.RespCount == 0 || resp.RespCount > 6 {
177+
return nil, fmt.Errorf("invalid NODES response count %d (not in (0,7))", resp.RespCount)
176178
}
177-
total = resp.Total
179+
total = resp.RespCount
178180
n = int(total) - 1
179181
first = false
180182
} else {
181183
n--
182-
if resp.Total != total {
183-
return nil, fmt.Errorf("invalid NODES response 'total' %d (!= %d)", resp.Total, total)
184+
if resp.RespCount != total {
185+
return nil, fmt.Errorf("invalid NODES response count %d (!= %d)", resp.RespCount, total)
184186
}
185187
}
186188
// Check nodes.

p2p/discover/v5_udp.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ type UDPv5 struct {
7070
clock mclock.Clock
7171
validSchemes enr.IdentityScheme
7272

73+
// misc buffers used during message handling
74+
logcontext []interface{}
75+
7376
// talkreq handler registry
7477
trlock sync.Mutex
7578
trhandlers map[string]TalkRequestHandler
@@ -158,6 +161,7 @@ func newUDPv5(conn UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv5, error) {
158161
activeCallByNode: make(map[enode.ID]*callV5),
159162
activeCallByAuth: make(map[v5wire.Nonce]*callV5),
160163
callQueue: make(map[enode.ID][]*callV5),
164+
161165
// shutdown
162166
closeCtx: closeCtx,
163167
cancelCloseCtx: cancelCloseCtx,
@@ -385,7 +389,7 @@ func (t *UDPv5) waitForNodes(c *callV5, distances []uint) ([]*enode.Node, error)
385389
nodes = append(nodes, node)
386390
}
387391
if total == -1 {
388-
total = min(int(response.Total), totalNodesResponseLimit)
392+
total = min(int(response.RespCount), totalNodesResponseLimit)
389393
}
390394
if received++; received == total {
391395
return nodes, nil
@@ -601,13 +605,18 @@ func (t *UDPv5) sendResponse(toID enode.ID, toAddr *net.UDPAddr, packet v5wire.P
601605
// send sends a packet to the given node.
602606
func (t *UDPv5) send(toID enode.ID, toAddr *net.UDPAddr, packet v5wire.Packet, c *v5wire.Whoareyou) (v5wire.Nonce, error) {
603607
addr := toAddr.String()
608+
t.logcontext = append(t.logcontext[:0], "id", toID, "addr", addr)
609+
t.logcontext = packet.AppendLogInfo(t.logcontext)
610+
604611
enc, nonce, err := t.codec.Encode(toID, addr, packet, c)
605612
if err != nil {
606-
t.log.Warn(">> "+packet.Name(), "id", toID, "addr", addr, "err", err)
613+
t.logcontext = append(t.logcontext, "err", err)
614+
t.log.Warn(">> "+packet.Name(), t.logcontext...)
607615
return nonce, err
608616
}
617+
609618
_, err = t.conn.WriteToUDP(enc, toAddr)
610-
t.log.Trace(">> "+packet.Name(), "id", toID, "addr", addr)
619+
t.log.Trace(">> "+packet.Name(), t.logcontext...)
611620
return nonce, err
612621
}
613622

@@ -657,7 +666,9 @@ func (t *UDPv5) handlePacket(rawpacket []byte, fromAddr *net.UDPAddr) error {
657666
}
658667
if packet.Kind() != v5wire.WhoareyouPacket {
659668
// WHOAREYOU logged separately to report errors.
660-
t.log.Trace("<< "+packet.Name(), "id", fromID, "addr", addr)
669+
t.logcontext = append(t.logcontext[:0], "id", fromID, "addr", addr)
670+
t.logcontext = packet.AppendLogInfo(t.logcontext)
671+
t.log.Trace("<< "+packet.Name(), t.logcontext...)
661672
}
662673
t.handle(packet, fromID, fromAddr)
663674
return nil
@@ -712,7 +723,7 @@ func (t *UDPv5) handle(p v5wire.Packet, fromID enode.ID, fromAddr *net.UDPAddr)
712723
case *v5wire.Nodes:
713724
t.handleCallResponse(fromID, fromAddr, p)
714725
case *v5wire.TalkRequest:
715-
t.handleTalkRequest(p, fromID, fromAddr)
726+
t.handleTalkRequest(fromID, fromAddr, p)
716727
case *v5wire.TalkResponse:
717728
t.handleCallResponse(fromID, fromAddr, p)
718729
}
@@ -827,7 +838,7 @@ func (t *UDPv5) collectTableNodes(rip net.IP, distances []uint, limit int) []*en
827838
// packNodes creates NODES response packets for the given node list.
828839
func packNodes(reqid []byte, nodes []*enode.Node) []*v5wire.Nodes {
829840
if len(nodes) == 0 {
830-
return []*v5wire.Nodes{{ReqID: reqid, Total: 1}}
841+
return []*v5wire.Nodes{{ReqID: reqid, RespCount: 1}}
831842
}
832843

833844
// This limit represents the available space for nodes in output packets. Maximum
@@ -851,13 +862,13 @@ func packNodes(reqid []byte, nodes []*enode.Node) []*v5wire.Nodes {
851862
resp = append(resp, p)
852863
}
853864
for _, msg := range resp {
854-
msg.Total = uint8(len(resp))
865+
msg.RespCount = uint8(len(resp))
855866
}
856867
return resp
857868
}
858869

859870
// handleTalkRequest runs the talk request handler of the requested protocol.
860-
func (t *UDPv5) handleTalkRequest(p *v5wire.TalkRequest, fromID enode.ID, fromAddr *net.UDPAddr) {
871+
func (t *UDPv5) handleTalkRequest(fromID enode.ID, fromAddr *net.UDPAddr, p *v5wire.TalkRequest) {
861872
t.trlock.Lock()
862873
handler := t.trhandlers[p.Protocol]
863874
t.trlock.Unlock()

p2p/discover/v5_udp_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ func (test *udpV5Test) expectNodes(wantReqID []byte, wantTotal uint8, wantNodes
208208
if !bytes.Equal(p.ReqID, wantReqID) {
209209
test.t.Fatalf("wrong request ID %v in response, want %v", p.ReqID, wantReqID)
210210
}
211-
if p.Total != wantTotal {
212-
test.t.Fatalf("wrong total response count %d, want %d", p.Total, wantTotal)
211+
if p.RespCount != wantTotal {
212+
test.t.Fatalf("wrong total response count %d, want %d", p.RespCount, wantTotal)
213213
}
214214
for _, record := range p.Nodes {
215215
n, _ := enode.New(enode.ValidSchemesForTesting, record)
@@ -301,14 +301,14 @@ func TestUDPv5_findnodeCall(t *testing.T) {
301301
t.Fatalf("wrong distances in request: %v", p.Distances)
302302
}
303303
test.packetIn(&v5wire.Nodes{
304-
ReqID: p.ReqID,
305-
Total: 2,
306-
Nodes: nodesToRecords(nodes[:4]),
304+
ReqID: p.ReqID,
305+
RespCount: 2,
306+
Nodes: nodesToRecords(nodes[:4]),
307307
})
308308
test.packetIn(&v5wire.Nodes{
309-
ReqID: p.ReqID,
310-
Total: 2,
311-
Nodes: nodesToRecords(nodes[4:]),
309+
ReqID: p.ReqID,
310+
RespCount: 2,
311+
Nodes: nodesToRecords(nodes[4:]),
312312
})
313313
})
314314

@@ -409,16 +409,16 @@ func TestUDPv5_callTimeoutReset(t *testing.T) {
409409
test.waitPacketOut(func(p *v5wire.Findnode, addr *net.UDPAddr, _ v5wire.Nonce) {
410410
time.Sleep(respTimeout - 50*time.Millisecond)
411411
test.packetIn(&v5wire.Nodes{
412-
ReqID: p.ReqID,
413-
Total: 2,
414-
Nodes: nodesToRecords(nodes[:4]),
412+
ReqID: p.ReqID,
413+
RespCount: 2,
414+
Nodes: nodesToRecords(nodes[:4]),
415415
})
416416

417417
time.Sleep(respTimeout - 50*time.Millisecond)
418418
test.packetIn(&v5wire.Nodes{
419-
ReqID: p.ReqID,
420-
Total: 2,
421-
Nodes: nodesToRecords(nodes[4:]),
419+
ReqID: p.ReqID,
420+
RespCount: 2,
421+
Nodes: nodesToRecords(nodes[4:]),
422422
})
423423
})
424424
if err := <-done; err != nil {

p2p/discover/v5wire/encoding_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestHandshake(t *testing.T) {
9292
}
9393

9494
// A <- B NODES
95-
nodes, _ := net.nodeB.encode(t, net.nodeA, &Nodes{Total: 1})
95+
nodes, _ := net.nodeB.encode(t, net.nodeA, &Nodes{RespCount: 1})
9696
net.nodeA.expectDecode(t, NodesMsg, nodes)
9797
}
9898

@@ -150,7 +150,7 @@ func TestHandshake_norecord(t *testing.T) {
150150
net.nodeB.expectDecode(t, FindnodeMsg, findnode)
151151

152152
// A <- B NODES
153-
nodes, _ := net.nodeB.encode(t, net.nodeA, &Nodes{Total: 1})
153+
nodes, _ := net.nodeB.encode(t, net.nodeA, &Nodes{RespCount: 1})
154154
net.nodeA.expectDecode(t, NodesMsg, nodes)
155155
}
156156

@@ -190,7 +190,7 @@ func TestHandshake_rekey(t *testing.T) {
190190
net.nodeB.expectDecode(t, FindnodeMsg, findnode)
191191

192192
// A <- B NODES
193-
nodes, _ := net.nodeB.encode(t, net.nodeA, &Nodes{Total: 1})
193+
nodes, _ := net.nodeB.encode(t, net.nodeA, &Nodes{RespCount: 1})
194194
net.nodeA.expectDecode(t, NodesMsg, nodes)
195195
}
196196

@@ -225,7 +225,7 @@ func TestHandshake_rekey2(t *testing.T) {
225225
net.nodeB.expectDecode(t, FindnodeMsg, findnode)
226226

227227
// A <- B NODES
228-
nodes, _ := net.nodeB.encode(t, net.nodeA, &Nodes{Total: 1})
228+
nodes, _ := net.nodeB.encode(t, net.nodeA, &Nodes{RespCount: 1})
229229
net.nodeA.expectDecode(t, NodesMsg, nodes)
230230
}
231231

0 commit comments

Comments
 (0)