@@ -167,10 +167,7 @@ func DefaultPortalProtocolConfig() *PortalProtocolConfig {
167167}
168168
169169type 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-
428386func (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
932888func (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
939897func (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 )
0 commit comments