Skip to content

Commit cfbffc5

Browse files
thinkAfCodGrapeBaBa
authored andcommitted
fix: cache id and addr in discv5
revert
1 parent d34a772 commit cfbffc5

File tree

4 files changed

+35
-83
lines changed

4 files changed

+35
-83
lines changed

p2p/discover/portal_protocol.go

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,7 @@ func DefaultPortalProtocolConfig() *PortalProtocolConfig {
167167
}
168168

169169
type PortalProtocol struct {
170-
table *Table
171-
cachedIdsLock sync.Mutex
172-
cachedNodes map[string]*enode.Node
173-
cachedIds map[string]enode.ID
170+
table *Table
174171

175172
protocolId string
176173
protocolName string
@@ -214,8 +211,6 @@ func NewPortalProtocol(config *PortalProtocolConfig, protocolId portalwire.Proto
214211
closeCtx, cancelCloseCtx := context.WithCancel(context.Background())
215212

216213
protocol := &PortalProtocol{
217-
cachedNodes: make(map[string]*enode.Node),
218-
cachedIds: make(map[string]enode.ID),
219214
protocolId: string(protocolId),
220215
protocolName: protocolId.Name(),
221216
ListenAddr: config.ListenAddr,
@@ -330,19 +325,11 @@ func (p *PortalProtocol) setupUDPListening() error {
330325
func(buf []byte, addr *net.UDPAddr) (int, error) {
331326
p.Log.Info("will send to target data", "ip", addr.IP.To4().String(), "port", addr.Port, "bufLength", len(buf))
332327

333-
p.cachedIdsLock.Lock()
334-
defer p.cachedIdsLock.Unlock()
335-
if n, ok := p.cachedNodes[addr.String()]; ok {
328+
if n, ok := p.DiscV5.cachedAddrNode[addr.String()]; ok {
336329
//_, err := p.DiscV5.TalkRequestToID(id, addr, string(portalwire.UTPNetwork), buf)
337330
req := &v5wire.TalkRequest{Protocol: string(portalwire.Utp), Message: buf}
338331
p.DiscV5.sendFromAnotherThreadWithNode(n, netip.AddrPortFrom(netutil.IPToAddr(addr.IP), uint16(addr.Port)), req)
339332

340-
return len(buf), err
341-
} else if id, ok := p.cachedIds[addr.String()]; ok {
342-
//_, err := p.DiscV5.TalkRequestToID(id, addr, string(portalwire.UTPNetwork), buf)
343-
req := &v5wire.TalkRequest{Protocol: string(portalwire.Utp), Message: buf}
344-
p.DiscV5.sendFromAnotherThread(id, netip.AddrPortFrom(netutil.IPToAddr(addr.IP), uint16(addr.Port)), req)
345-
346333
return len(buf), err
347334
} else {
348335
p.Log.Warn("not found target node info", "ip", addr.IP.To4().String(), "port", addr.Port, "bufLength", len(buf))
@@ -396,35 +383,6 @@ func (p *PortalProtocol) setupDiscV5AndTable() error {
396383
return nil
397384
}
398385

399-
func (p *PortalProtocol) cacheNode(node *enode.Node) {
400-
p.cachedIdsLock.Lock()
401-
defer p.cachedIdsLock.Unlock()
402-
addr := &net.UDPAddr{IP: node.IP(), Port: node.UDP()}
403-
if _, ok := p.cachedNodes[addr.String()]; !ok && node != nil {
404-
p.cachedNodes[addr.String()] = node
405-
}
406-
}
407-
408-
func (p *PortalProtocol) cacheNodeId(id enode.ID, addr *net.UDPAddr) {
409-
p.cachedIdsLock.Lock()
410-
defer p.cachedIdsLock.Unlock()
411-
if (id != enode.ID{}) {
412-
p.cachedIds[addr.String()] = id
413-
}
414-
}
415-
416-
func (p *PortalProtocol) cacheNodeById(id enode.ID, addr *net.UDPAddr) {
417-
go func() {
418-
p.cacheNodeId(id, addr)
419-
if _, ok := p.cachedNodes[addr.String()]; !ok {
420-
n := p.ResolveNodeId(id)
421-
if n != nil {
422-
p.cacheNode(n)
423-
}
424-
}
425-
}()
426-
}
427-
428386
func (p *PortalProtocol) ping(node *enode.Node) (uint64, error) {
429387
pong, err := p.pingInner(node)
430388
if err != nil {
@@ -586,7 +544,6 @@ func (p *PortalProtocol) processOffer(target *enode.Node, resp []byte, request *
586544
}
587545

588546
p.Log.Info("will process Offer", "id", target.ID(), "ip", target.IP().To4().String(), "port", target.UDP())
589-
p.cacheNode(target)
590547

591548
accept := &portalwire.Accept{}
592549
err = accept.UnmarshalSSZ(resp[1:])
@@ -724,7 +681,6 @@ func (p *PortalProtocol) processContent(target *enode.Node, resp []byte) (byte,
724681
}
725682

726683
p.Log.Info("will process content", "id", target.ID(), "ip", target.IP().To4().String(), "port", target.UDP())
727-
p.cacheNode(target)
728684

729685
switch resp[1] {
730686
case portalwire.ContentRawSelector:
@@ -930,14 +886,18 @@ func (p *PortalProtocol) processPong(target *enode.Node, resp []byte) (*portalwi
930886
}
931887

932888
func (p *PortalProtocol) handleUtpTalkRequest(id enode.ID, addr *net.UDPAddr, msg []byte) []byte {
933-
p.cacheNodeById(id, addr)
889+
if n := p.DiscV5.getNode(id); n != nil {
890+
p.table.addInboundNode(n)
891+
}
934892
p.Log.Trace("receive utp data", "addr", addr, "msg-length", len(msg))
935893
p.packetRouter.ReceiveMessage(msg, addr)
936894
return []byte("")
937895
}
938896

939897
func (p *PortalProtocol) handleTalkRequest(id enode.ID, addr *net.UDPAddr, msg []byte) []byte {
940-
p.cacheNodeById(id, addr)
898+
if n := p.DiscV5.getNode(id); n != nil {
899+
p.table.addInboundNode(n)
900+
}
941901

942902
msgCode := msg[0]
943903

@@ -1120,8 +1080,6 @@ func (p *PortalProtocol) handleFindContent(id enode.ID, addr *net.UDPAddr, reque
11201080
return nil, err
11211081
}
11221082

1123-
p.cacheNodeById(id, addr)
1124-
11251083
if errors.Is(err, ContentNotFound) {
11261084
closestNodes := p.findNodesCloseToContent(contentId, portalFindnodesResultLimit)
11271085
for i, n := range closestNodes {
@@ -1316,8 +1274,6 @@ func (p *PortalProtocol) handleOffer(id enode.ID, addr *net.UDPAddr, request *po
13161274
}
13171275
}
13181276

1319-
p.cacheNodeById(id, addr)
1320-
13211277
idBuffer := make([]byte, 2)
13221278
if contentKeyBitlist.Count() != 0 {
13231279
connId := p.connIdGen.GenCid(id, false)

p2p/discover/portal_protocol_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,6 @@ func TestPortalWireProtocolUdp(t *testing.T) {
114114
assert.NoError(t, err)
115115
time.Sleep(12 * time.Second)
116116

117-
node1.cacheNode(node2.localNode.Node())
118-
node1.cacheNode(node3.localNode.Node())
119-
120-
node2.cacheNode(node1.localNode.Node())
121-
node2.cacheNode(node3.localNode.Node())
122-
123-
node3.cacheNode(node1.localNode.Node())
124-
node3.cacheNode(node2.localNode.Node())
125-
126117
udpAddrStr1 := fmt.Sprintf("%s:%d", node1.localNode.Node().IP(), node1.localNode.Node().UDP())
127118
udpAddrStr2 := fmt.Sprintf("%s:%d", node2.localNode.Node().IP(), node2.localNode.Node().UDP())
128119

p2p/discover/v5_udp.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,17 @@ type codecV5 interface {
6262
// UDPv5 is the implementation of protocol version 5.
6363
type UDPv5 struct {
6464
// static fields
65-
conn UDPConn
66-
tab *Table
67-
netrestrict *netutil.Netlist
68-
priv *ecdsa.PrivateKey
69-
localNode *enode.LocalNode
70-
db *enode.DB
71-
log log.Logger
72-
clock mclock.Clock
73-
validSchemes enr.IdentityScheme
65+
conn UDPConn
66+
tab *Table
67+
cachedIds map[enode.ID]*enode.Node
68+
cachedAddrNode map[string]*enode.Node
69+
netrestrict *netutil.Netlist
70+
priv *ecdsa.PrivateKey
71+
localNode *enode.LocalNode
72+
db *enode.DB
73+
log log.Logger
74+
clock mclock.Clock
75+
validSchemes enr.IdentityScheme
7476

7577
// misc buffers used during message handling
7678
logcontext []interface{}
@@ -151,14 +153,16 @@ func newUDPv5(conn UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv5, error) {
151153
cfg = cfg.withDefaults()
152154
t := &UDPv5{
153155
// static fields
154-
conn: newMeteredConn(conn),
155-
localNode: ln,
156-
db: ln.Database(),
157-
netrestrict: cfg.NetRestrict,
158-
priv: cfg.PrivateKey,
159-
log: cfg.Log,
160-
validSchemes: cfg.ValidSchemes,
161-
clock: cfg.Clock,
156+
conn: newMeteredConn(conn),
157+
cachedAddrNode: make(map[string]*enode.Node),
158+
cachedIds: make(map[enode.ID]*enode.Node),
159+
localNode: ln,
160+
db: ln.Database(),
161+
netrestrict: cfg.NetRestrict,
162+
priv: cfg.PrivateKey,
163+
log: cfg.Log,
164+
validSchemes: cfg.ValidSchemes,
165+
clock: cfg.Clock,
162166
// channels into dispatch
163167
packetInCh: make(chan ReadPacket, 1),
164168
readNextCh: make(chan struct{}, 1),
@@ -724,6 +728,10 @@ func (t *UDPv5) send(toID enode.ID, toAddr netip.AddrPort, packet v5wire.Packet,
724728
t.log.Warn(">> "+packet.Name(), t.logcontext...)
725729
return nonce, err
726730
}
731+
if c != nil && c.Node != nil {
732+
t.cachedIds[toID] = c.Node
733+
t.cachedAddrNode[toAddr.String()] = c.Node
734+
}
727735

728736
_, err = t.conn.WriteToUDPAddrPort(enc, toAddr)
729737
t.log.Trace(">> "+packet.Name(), t.logcontext...)
@@ -785,6 +793,8 @@ func (t *UDPv5) handlePacket(rawpacket []byte, fromAddr netip.AddrPort) error {
785793
if fromNode != nil {
786794
// Handshake succeeded, add to table.
787795
t.tab.addInboundNode(fromNode)
796+
t.cachedIds[fromID] = fromNode
797+
t.cachedAddrNode[fromAddr.String()] = fromNode
788798
}
789799
if packet.Kind() != v5wire.WhoareyouPacket {
790800
// WHOAREYOU logged separately to report errors.

portalnetwork/history/history_network_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,6 @@ func TestGetContentByKey(t *testing.T) {
140140
contentId := historyNetwork1.portalProtocol.ToContentId(headerEntry.key)
141141
err = historyNetwork1.portalProtocol.Put(headerEntry.key, contentId, headerEntry.value)
142142
require.NoError(t, err)
143-
144-
header, err = historyNetwork1.GetBlockHeader(headerEntry.key[1:])
145-
require.NoError(t, err)
146-
require.NotNil(t, header)
147-
148143
// get content from historyNetwork1
149144
header, err = historyNetwork2.GetBlockHeader(headerEntry.key[1:])
150145
require.NoError(t, err)

0 commit comments

Comments
 (0)