Skip to content

Commit 9fe2530

Browse files
committed
Merge branch 'gethmaster' into gethintegration
2 parents b21d178 + d2176f4 commit 9fe2530

File tree

6 files changed

+54
-34
lines changed

6 files changed

+54
-34
lines changed

p2p/discover/v5_talk.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ func (t *talkSystem) register(protocol string, handler TalkRequestHandler) {
7272

7373
// handleRequest handles a talk request.
7474
func (t *talkSystem) handleRequest(id enode.ID, addr netip.AddrPort, req *v5wire.TalkRequest) {
75-
var n *enode.Node
76-
if n = t.transport.codec.SessionNode(id, addr.String()); n == nil {
77-
log.Error("Got a TALKREQ from a node that has not completed the handshake", "id", id, "addr", addr)
78-
return
75+
n := t.transport.codec.SessionNode(id, addr.String())
76+
if n == nil {
77+
// The node must be contained in the session here, since we wouldn't have
78+
// received the request otherwise.
79+
panic("missing node in session")
7980
}
8081
t.mutex.Lock()
8182
handler, ok := t.handlers[req.Protocol]

p2p/discover/v5_udp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type codecV5 interface {
6767
CurrentChallenge(id enode.ID, addr string) *v5wire.Whoareyou
6868

6969
// SessionNode returns a node that has completed the handshake.
70-
SessionNode(enode.ID, string) *enode.Node
70+
SessionNode(id enode.ID, addr string) *enode.Node
7171
}
7272

7373
// UDPv5 is the implementation of protocol version 5.
@@ -941,7 +941,7 @@ func (t *UDPv5) handleWhoareyou(p *v5wire.Whoareyou, fromID enode.ID, fromAddr n
941941
c.err <- errors.New("remote wants handshake, but call has no ENR")
942942
return
943943
}
944-
944+
// Resend the call that was answered by WHOAREYOU.
945945
t.log.Trace("<< "+p.Name(), "id", c.node.ID(), "addr", fromAddr)
946946
if _, ok := t.noRespCallByAuth[p.Nonce]; !ok {
947947
// Resend the call that was answered by WHOAREYOU.

p2p/discover/v5_udp_test.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,29 +181,35 @@ func TestUDPv5_handshakeRepeatChallenge(t *testing.T) {
181181
nonce1 := v5wire.Nonce{1}
182182
nonce2 := v5wire.Nonce{2}
183183
nonce3 := v5wire.Nonce{3}
184-
check := func(p *v5wire.Whoareyou, wantNonce v5wire.Nonce) {
184+
var firstAuthTag *v5wire.Nonce
185+
check := func(p *v5wire.Whoareyou, authTag, wantNonce v5wire.Nonce) {
185186
t.Helper()
186187
if p.Nonce != wantNonce {
187-
t.Error("wrong nonce in WHOAREYOU:", p.Nonce, wantNonce)
188+
t.Error("wrong nonce in WHOAREYOU:", p.Nonce, "want:", wantNonce)
189+
}
190+
if firstAuthTag == nil {
191+
firstAuthTag = &authTag
192+
} else if authTag != *firstAuthTag {
193+
t.Error("wrong auth tag in WHOAREYOU header:", authTag, "want:", *firstAuthTag)
188194
}
189195
}
190196

191197
// Unknown packet from unknown node.
192198
test.packetIn(&v5wire.Unknown{Nonce: nonce1})
193-
test.waitPacketOut(func(p *v5wire.Whoareyou, addr netip.AddrPort, _ v5wire.Nonce) {
194-
check(p, nonce1)
199+
test.waitPacketOut(func(p *v5wire.Whoareyou, addr netip.AddrPort, authTag v5wire.Nonce) {
200+
check(p, authTag, nonce1)
195201
})
196202

197203
// Second unknown packet. Here we expect the response to reference the
198204
// first unknown packet.
199205
test.packetIn(&v5wire.Unknown{Nonce: nonce2})
200-
test.waitPacketOut(func(p *v5wire.Whoareyou, addr netip.AddrPort, _ v5wire.Nonce) {
201-
check(p, nonce1)
206+
test.waitPacketOut(func(p *v5wire.Whoareyou, addr netip.AddrPort, authTag v5wire.Nonce) {
207+
check(p, authTag, nonce1)
202208
})
203209
// Third unknown packet. This should still return the first nonce.
204210
test.packetIn(&v5wire.Unknown{Nonce: nonce3})
205-
test.waitPacketOut(func(p *v5wire.Whoareyou, addr netip.AddrPort, _ v5wire.Nonce) {
206-
check(p, nonce1)
211+
test.waitPacketOut(func(p *v5wire.Whoareyou, addr netip.AddrPort, authTag v5wire.Nonce) {
212+
check(p, authTag, nonce1)
207213
})
208214
}
209215

@@ -766,20 +772,30 @@ type testCodecFrame struct {
766772
}
767773

768774
func (c *testCodec) Encode(toID enode.ID, addr string, p v5wire.Packet, _ *v5wire.Whoareyou) ([]byte, v5wire.Nonce, error) {
775+
// To match the behavior of v5wire.Codec, we return the cached encoding of
776+
// WHOAREYOU challenges.
777+
if wp, ok := p.(*v5wire.Whoareyou); ok && len(wp.Encoded) > 0 {
778+
return wp.Encoded, wp.Nonce, nil
779+
}
780+
769781
c.ctr++
770782
var authTag v5wire.Nonce
771783
binary.BigEndian.PutUint64(authTag[:], c.ctr)
784+
penc, _ := rlp.EncodeToBytes(p)
785+
frame, err := rlp.EncodeToBytes(testCodecFrame{c.id, authTag, p.Kind(), penc})
786+
if err != nil {
787+
return frame, authTag, err
788+
}
772789

790+
// Store recently sent challenges.
773791
if w, ok := p.(*v5wire.Whoareyou); ok {
774-
// Store recently sent Whoareyou challenges.
792+
w.Nonce = authTag
793+
w.Encoded = frame
775794
if c.sentChallenges == nil {
776795
c.sentChallenges = make(map[enode.ID]*v5wire.Whoareyou)
777796
}
778797
c.sentChallenges[toID] = w
779798
}
780-
781-
penc, _ := rlp.EncodeToBytes(p)
782-
frame, err := rlp.EncodeToBytes(testCodecFrame{c.id, authTag, p.Kind(), penc})
783799
return frame, authTag, err
784800
}
785801

p2p/discover/v5wire/encoding.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,10 @@ func (c *Codec) Encode(id enode.ID, addr string, packet Packet, challenge *Whoar
189189
)
190190
switch {
191191
case packet.Kind() == WhoareyouPacket:
192-
if sentWhoareyou := c.sc.getHandshake(id, addr); sentWhoareyou != nil {
193-
return sentWhoareyou.encoded, sentWhoareyou.Nonce, nil
192+
// just send the WHOAREYOU packet raw again, rather than the re-encoded challenge data
193+
w := packet.(*Whoareyou)
194+
if len(w.Encoded) > 0 {
195+
return w.Encoded, w.Nonce, nil
194196
}
195197
head, err = c.encodeWhoareyou(id, packet.(*Whoareyou))
196198
case challenge != nil:
@@ -218,25 +220,26 @@ func (c *Codec) Encode(id enode.ID, addr string, packet Packet, challenge *Whoar
218220
// Encode header data.
219221
c.writeHeaders(&head)
220222

221-
var enc []byte
222223
// Store sent WHOAREYOU challenges.
223224
if challenge, ok := packet.(*Whoareyou); ok {
224225
challenge.ChallengeData = bytesCopy(&c.buf)
225-
enc, err = c.EncodeRaw(id, head, msgData)
226+
enc, err := c.EncodeRaw(id, head, msgData)
226227
if err != nil {
227228
return nil, Nonce{}, err
228229
}
229-
challenge.encoded = enc
230+
challenge.Encoded = bytes.Clone(enc)
230231
c.sc.storeSentHandshake(id, addr, challenge)
231232
return enc, head.Nonce, err
232-
} else if msgData == nil {
233+
}
234+
235+
if msgData == nil {
233236
headerData := c.buf.Bytes()
234237
msgData, err = c.encryptMessage(session, packet, &head, headerData)
235238
if err != nil {
236239
return nil, Nonce{}, err
237240
}
238241
}
239-
enc, err = c.EncodeRaw(id, head, msgData)
242+
enc, err := c.EncodeRaw(id, head, msgData)
240243
return enc, head.Nonce, err
241244
}
242245

@@ -260,12 +263,6 @@ func (c *Codec) CurrentChallenge(id enode.ID, addr string) *Whoareyou {
260263
return c.sc.getHandshake(id, addr)
261264
}
262265

263-
// SessionNode returns the node associated.
264-
// This will return nil while a handshake is in progress.
265-
func (c *Codec) SessionNode(id enode.ID, addr string) *enode.Node {
266-
return c.sc.readNode(id, addr)
267-
}
268-
269266
func (c *Codec) writeHeaders(head *Header) {
270267
c.buf.Reset()
271268
c.buf.Write(head.IV[:])
@@ -659,6 +656,10 @@ func (c *Codec) decryptMessage(input, nonce, headerData, readKey []byte) (Packet
659656
return DecodeMessage(msgdata[0], msgdata[1:])
660657
}
661658

659+
func (c *Codec) SessionNode(id enode.ID, addr string) *enode.Node {
660+
return c.sc.readNode(id, addr)
661+
}
662+
662663
// checkValid performs some basic validity checks on the header.
663664
// The packetLen here is the length remaining after the static header.
664665
func (h *StaticHeader) checkValid(packetLen int, protocolID [6]byte) error {

p2p/discover/v5wire/msg.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ type (
7272
// This must be set by the caller of Encode.
7373
Node *enode.Node
7474

75-
sent mclock.AbsTime // for handshake GC.
76-
encoded []byte // Encoded packet raw data for sending out
75+
sent mclock.AbsTime // for handshake GC.
76+
77+
// Encoded is packet raw data for sending out, but should not be include in the RLP encoding.
78+
Encoded []byte `rlp:"-"`
7779
}
7880

7981
// PING is sent during liveness checks.

p2p/discover/v5wire/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (sc *SessionCache) readNode(id enode.ID, addr string) *enode.Node {
114114
// storeNewSession stores new encryption keys in the cache.
115115
func (sc *SessionCache) storeNewSession(id enode.ID, addr string, s *session, n *enode.Node) {
116116
if n == nil {
117-
panic("session must caches a non-nil node")
117+
panic("nil node in storeNewSession")
118118
}
119119
s.node = n
120120
sc.sessions.Add(sessionID{id, addr}, s)

0 commit comments

Comments
 (0)