Skip to content

Commit

Permalink
feat: change method params
Browse files Browse the repository at this point in the history
  ContentLookup and TraceContentLookup method
  • Loading branch information
fearlessfe authored and GrapeBaBa committed Mar 27, 2024
1 parent 021ce2e commit 923ec25
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
13 changes: 9 additions & 4 deletions p2p/discover/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
12 changes: 4 additions & 8 deletions p2p/discover/portal_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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{
Expand Down
7 changes: 4 additions & 3 deletions p2p/discover/portal_protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions portalnetwork/history/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions portalnetwork/history/history_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 923ec25

Please sign in to comment.