Skip to content

Commit d34a772

Browse files
thinkAfCodGrapeBaBa
authored andcommitted
fix: check lint
1 parent 81477ca commit d34a772

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

p2p/discover/portal_protocol.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ func DefaultPortalProtocolConfig() *PortalProtocolConfig {
169169
type PortalProtocol struct {
170170
table *Table
171171
cachedIdsLock sync.Mutex
172-
cachedIds map[string]*enode.Node
172+
cachedNodes map[string]*enode.Node
173+
cachedIds map[string]enode.ID
173174

174175
protocolId string
175176
protocolName string
@@ -213,7 +214,8 @@ func NewPortalProtocol(config *PortalProtocolConfig, protocolId portalwire.Proto
213214
closeCtx, cancelCloseCtx := context.WithCancel(context.Background())
214215

215216
protocol := &PortalProtocol{
216-
cachedIds: make(map[string]*enode.Node),
217+
cachedNodes: make(map[string]*enode.Node),
218+
cachedIds: make(map[string]enode.ID),
217219
protocolId: string(protocolId),
218220
protocolName: protocolId.Name(),
219221
ListenAddr: config.ListenAddr,
@@ -330,11 +332,17 @@ func (p *PortalProtocol) setupUDPListening() error {
330332

331333
p.cachedIdsLock.Lock()
332334
defer p.cachedIdsLock.Unlock()
333-
if n, ok := p.cachedIds[addr.String()]; ok {
335+
if n, ok := p.cachedNodes[addr.String()]; ok {
334336
//_, err := p.DiscV5.TalkRequestToID(id, addr, string(portalwire.UTPNetwork), buf)
335337
req := &v5wire.TalkRequest{Protocol: string(portalwire.Utp), Message: buf}
336338
p.DiscV5.sendFromAnotherThreadWithNode(n, netip.AddrPortFrom(netutil.IPToAddr(addr.IP), uint16(addr.Port)), req)
337339

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+
338346
return len(buf), err
339347
} else {
340348
p.Log.Warn("not found target node info", "ip", addr.IP.To4().String(), "port", addr.Port, "bufLength", len(buf))
@@ -392,14 +400,23 @@ func (p *PortalProtocol) cacheNode(node *enode.Node) {
392400
p.cachedIdsLock.Lock()
393401
defer p.cachedIdsLock.Unlock()
394402
addr := &net.UDPAddr{IP: node.IP(), Port: node.UDP()}
395-
if _, ok := p.cachedIds[addr.String()]; !ok {
396-
p.cachedIds[addr.String()] = node
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
397413
}
398414
}
399415

400416
func (p *PortalProtocol) cacheNodeById(id enode.ID, addr *net.UDPAddr) {
401417
go func() {
402-
if _, ok := p.cachedIds[addr.String()]; !ok {
418+
p.cacheNodeId(id, addr)
419+
if _, ok := p.cachedNodes[addr.String()]; !ok {
403420
n := p.ResolveNodeId(id)
404421
if n != nil {
405422
p.cacheNode(n)

p2p/discover/v5_udp.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -604,12 +604,17 @@ func (t *UDPv5) dispatch() {
604604
c.ch = make(chan v5wire.Packet, 1)
605605
c.err = make(chan error, 1)
606606
// Assign request ID.
607-
crand.Read(c.reqid)
608-
c.packet.SetRequestID(c.reqid)
609-
newNonce, _ := t.send(c.id, c.addr, c.packet, nil)
610-
c.nonce = newNonce
611-
t.activeCallByAuth[newNonce] = c
607+
if tq, ok := r.msg.(*v5wire.TalkRequest); ok {
608+
if len(tq.ReqID) == 0 {
609+
crand.Read(c.reqid)
610+
c.packet.SetRequestID(c.reqid)
611+
}
612+
}
613+
nonce, _ := t.send(c.id, c.addr, c.packet, nil)
614+
c.nonce = nonce
615+
t.activeCallByAuth[nonce] = c
612616
t.startResponseTimeout(c)
617+
//t.send(r.destID, r.destAddr, r.msg, nil)
613618

614619
case p := <-t.packetInCh:
615620
t.handlePacket(p.Data, p.Addr)

portalnetwork/history/history_network_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ 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+
143148
// get content from historyNetwork1
144149
header, err = historyNetwork2.GetBlockHeader(headerEntry.key[1:])
145150
require.NoError(t, err)

portalnetwork/history/storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ func (p *ContentStorage) deleteContentOutOfRadius(radius *uint256.Int) error {
452452
return err
453453
}
454454
count, _ := res.RowsAffected()
455-
p.log.Trace("delete %d items", count)
455+
p.log.Trace("delete items", "count", count)
456456
return err
457457
}
458458

0 commit comments

Comments
 (0)