diff --git a/p2p/discover/api.go b/p2p/discover/api.go index b6755f7e587c..707205eac510 100644 --- a/p2p/discover/api.go +++ b/p2p/discover/api.go @@ -458,7 +458,8 @@ func (p *PortalProtocolAPI) RecursiveFindContent(contentKeyHex string) (*Content if err != nil { return nil, err } - content, utpTransfer, err := p.portalProtocol.ContentLookup(contentKey) + contentId := p.portalProtocol.toContentId(contentKey) + content, utpTransfer, err := p.portalProtocol.ContentLookup(contentKey, contentId) if err != nil { return nil, err @@ -517,7 +518,11 @@ func (p *PortalProtocolAPI) Gossip(contentKeyHex, contentHex string) (int, error return p.portalProtocol.NeighborhoodGossip(&id, [][]byte{contentKey}, [][]byte{content}) } -// TODO -func (p *PortalProtocolAPI) TraceRecursiveFindContent(contentKeyHex string) { - +func (p *PortalProtocolAPI) TraceRecursiveFindContent(contentKeyHex string) (*TraceContentResult, error) { + contentKey, err := hexutil.Decode(contentKeyHex) + if err != nil { + return nil, err + } + contentId := p.portalProtocol.toContentId(contentKey) + return p.portalProtocol.TraceContentLookup(contentKey, contentId) } diff --git a/p2p/discover/portal_protocol.go b/p2p/discover/portal_protocol.go index 0ae514b69beb..f95636dbc4ef 100644 --- a/p2p/discover/portal_protocol.go +++ b/p2p/discover/portal_protocol.go @@ -1421,13 +1421,11 @@ func (p *PortalProtocol) collectTableNodes(rip net.IP, distances []uint, limit i return nodes } -func (p *PortalProtocol) ContentLookup(contentKey []byte) ([]byte, bool, error) { +func (p *PortalProtocol) ContentLookup(contentKey, contentId []byte) ([]byte, bool, error) { lookupContext, cancel := context.WithCancel(context.Background()) defer cancel() resChan := make(chan *ContentInfoResp, 1) defer close(resChan) - - contentId := p.ToContentId(contentKey) newLookup(lookupContext, p.table, enode.ID(contentId), func(n *node) ([]*node, error) { return p.contentLookupWorker(unwrapNode(n), contentKey, resChan) }).run() @@ -1469,7 +1467,7 @@ func (p *PortalProtocol) contentLookupWorker(n *enode.Node, contentKey []byte, r return wrapedNode, nil } -func (p *PortalProtocol) TraceContentLookup(contentKey []byte) (*TraceContentResult, error) { +func (p *PortalProtocol) TraceContentLookup(contentKey, contentId []byte) (*TraceContentResult, error) { lookupContext, cancel := context.WithCancel(context.Background()) defer cancel() requestNodeChan := make(chan *enode.Node, 3) @@ -1484,14 +1482,14 @@ func (p *PortalProtocol) TraceContentLookup(contentKey []byte) (*TraceContentRes trace := &Trace{ Origin: selfHexId, - TargetId: hexutil.Encode(p.ToContentId(contentKey)), + TargetId: hexutil.Encode(contentId), StartedAtMs: int(time.Now().UnixMilli()), Responses: make(map[string][]string), Metadata: make(map[string]*NodeMetadata), Cancelled: make([]string, 0), } - nodes := p.table.findnodeByID(enode.ID(p.ToContentId(contentKey)), bucketSize, false) + nodes := p.table.findnodeByID(enode.ID(contentId), bucketSize, false) localResponse := make([]string, 0, len(nodes.entries)) for _, node := range nodes.entries { @@ -1500,8 +1498,6 @@ func (p *PortalProtocol) TraceContentLookup(contentKey []byte) (*TraceContentRes } trace.Responses[selfHexId] = localResponse - contentId := p.ToContentId(contentKey) - dis := p.Distance(p.Self().ID(), enode.ID(contentId)) trace.Metadata[selfHexId] = &NodeMetadata{ diff --git a/p2p/discover/portal_protocol_test.go b/p2p/discover/portal_protocol_test.go index 271346c721f0..20f7bdb73ed8 100644 --- a/p2p/discover/portal_protocol_test.go +++ b/p2p/discover/portal_protocol_test.go @@ -455,11 +455,12 @@ func TestContentLookup(t *testing.T) { err = node3.storage.Put(nil, contentId, content) assert.NoError(t, err) - res, _, err := node1.ContentLookup(contentKey) + res, _, err := node1.ContentLookup(contentKey, contentId) assert.NoError(t, err) assert.Equal(t, res, content) - res, _, err = node1.ContentLookup([]byte{0x2, 0x4}) + nonExist := []byte{0x2, 0x4} + res, _, err = node1.ContentLookup(nonExist, node1.toContentId(nonExist)) assert.Equal(t, ContentNotFound, err) assert.Nil(t, res) } @@ -494,7 +495,7 @@ func TestTraceContentLookup(t *testing.T) { node2Id := hexutil.Encode(node2.Self().ID().Bytes()) node3Id := hexutil.Encode(node3.Self().ID().Bytes()) - res, err := node3.TraceContentLookup(contentKey) + res, err := node3.TraceContentLookup(contentKey, contentId) assert.NoError(t, err) assert.Equal(t, res.Content, hexutil.Encode(content)) assert.Equal(t, res.UtpTransfer, false) diff --git a/portalnetwork/history/api.go b/portalnetwork/history/api.go index 6a51cba7a4f4..926bca7cf9bc 100644 --- a/portalnetwork/history/api.go +++ b/portalnetwork/history/api.go @@ -64,8 +64,8 @@ func (p *API) HistoryGossip(contentKeyHex, contentHex string) (int, error) { return p.Gossip(contentKeyHex, contentHex) } -func (p *API) HistoryTraceRecursiveFindContent(contentKeyHex string) { - p.TraceRecursiveFindContent(contentKeyHex) +func (p *API) HistoryTraceRecursiveFindContent(contentKeyHex string) (*discover.TraceContentResult, error) { + return p.TraceRecursiveFindContent(contentKeyHex) } func NewHistoryNetworkAPI(historyAPI *discover.PortalProtocolAPI) *API { diff --git a/portalnetwork/history/history_network.go b/portalnetwork/history/history_network.go index 057da0f3352a..893a2cb6cc13 100644 --- a/portalnetwork/history/history_network.go +++ b/portalnetwork/history/history_network.go @@ -119,7 +119,7 @@ func (h *HistoryNetwork) GetBlockHeader(blockHash []byte) (*types.Header, error) } // no content in local storage for retries := 0; retries < requestRetries; retries++ { - content, _, err := h.portalProtocol.ContentLookup(contentKey) + content, _, err := h.portalProtocol.ContentLookup(contentKey, contentId) if err != nil { h.log.Error("getBlockHeader failed", "contentKey", hexutil.Encode(contentKey), "err", err) continue @@ -176,7 +176,7 @@ func (h *HistoryNetwork) GetBlockBody(blockHash []byte) (*types.Body, error) { // no content in local storage for retries := 0; retries < requestRetries; retries++ { - content, _, err := h.portalProtocol.ContentLookup(contentKey) + content, _, err := h.portalProtocol.ContentLookup(contentKey, contentId) if err != nil { h.log.Error("getBlockBody failed", "contentKey", hexutil.Encode(contentKey), "err", err) continue @@ -231,7 +231,7 @@ func (h *HistoryNetwork) GetReceipts(blockHash []byte) ([]*types.Receipt, error) // no content in local storage for retries := 0; retries < requestRetries; retries++ { - content, _, err := h.portalProtocol.ContentLookup(contentKey) + content, _, err := h.portalProtocol.ContentLookup(contentKey, contentId) if err != nil { h.log.Error("getReceipts failed", "contentKey", hexutil.Encode(contentKey), "err", err) continue @@ -265,7 +265,7 @@ func (h *HistoryNetwork) GetEpochAccumulator(epochHash []byte) (*EpochAccumulato return epochAccu, err } for retries := 0; retries < requestRetries; retries++ { - content, _, err := h.portalProtocol.ContentLookup(contentKey) + content, _, err := h.portalProtocol.ContentLookup(contentKey, contentId) if err != nil { h.log.Error("getEpochAccumulator failed", "contentKey", hexutil.Encode(contentKey), "err", err) continue