Skip to content

Commit

Permalink
feat: add rpc methods
Browse files Browse the repository at this point in the history
  • Loading branch information
fearlessfe authored and GrapeBaBa committed Jan 28, 2024
1 parent c3b1fe2 commit 3936f0b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
53 changes: 53 additions & 0 deletions p2p/discover/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/p2p/discover/portalwire"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/portalnetwork/storage"
"github.com/holiman/uint256"
)

Expand Down Expand Up @@ -432,3 +433,55 @@ func (p *PortalAPI) HistoryRecursiveFindNodes(nodeId string) ([]string, error) {

return enrs, nil
}

func (p *PortalAPI) HistoryRecursiveFindContent(contentKeyHex string) (*ContentInfo, error) {
contentKey, err := hexutil.Decode(contentKeyHex)
if err != nil {
return nil, err
}
content, utpTransfer, err := p.portalProtocol.ContentLookup(contentKey)
if err != nil {
return nil, err
}

return &ContentInfo{
Content: hexutil.Encode(content),
UtpTransfer: utpTransfer,
}, err
}

func (p *PortalAPI) HistoryLocalContent(contentKeyHex string) (string, error) {
contentKey, err := hexutil.Decode(contentKeyHex)
if err != nil {
return "", err
}
contentId := p.portalProtocol.ToContentId(contentKey)
content, err := p.portalProtocol.Get(contentId)
if errors.Is(err, storage.ErrContentNotFound) {
return "0x", nil
}
if err != nil {
return "", err
}
return hexutil.Encode(content), nil
}

func (p *PortalAPI) HistoryStore(contentKeyHex string, contextHex string) (bool, error) {
contentKey, err := hexutil.Decode(contentKeyHex)
if err != nil {
return false, err
}
contentId := p.portalProtocol.ToContentId(contentKey)
if !p.portalProtocol.InRange(contentId) {
return false, nil
}
content, err := hexutil.Decode(contextHex)
if err != nil {
return false, err
}
err = p.portalProtocol.Put(contentId, content)
if err != nil {
return false, err
}
return true, nil
}
24 changes: 18 additions & 6 deletions p2p/discover/portal_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ type OfferRequestWithNode struct {
Node *enode.Node
}

type ContentInfoRes struct {
Content []byte
UtpTransfer bool
}

type PortalProtocolOption func(p *PortalProtocol)

type PortalProtocolConfig struct {
Expand Down Expand Up @@ -1402,22 +1407,23 @@ func (p *PortalProtocol) collectTableNodes(rip net.IP, distances []uint, limit i
return nodes
}

func (p *PortalProtocol) ContentLookup(contentKey []byte) ([]byte, error) {
func (p *PortalProtocol) ContentLookup(contentKey []byte) ([]byte, bool, error) {
lookupContext, cancel := context.WithCancel(context.Background())
defer cancel()
resChan := make(chan []byte, 1)
resChan := make(chan *ContentInfoRes, 1)
defer close(resChan)
newLookup(lookupContext, p.table, p.Self().ID(), func(n *node) ([]*node, error) {
return p.contentLookupWorker(unwrapNode(n), contentKey, resChan)
}).run()

if len(resChan) > 0 {
return <-resChan, nil
res := <-resChan
return res.Content, res.UtpTransfer, nil
}
return nil, ContentNotFound
return nil, false, ContentNotFound
}

func (p *PortalProtocol) contentLookupWorker(n *enode.Node, contentKey []byte, resChan chan<- []byte) ([]*node, error) {
func (p *PortalProtocol) contentLookupWorker(n *enode.Node, contentKey []byte, resChan chan<- *ContentInfoRes) ([]*node, error) {
wrapedNode := make([]*node, 0)
flag, content, err := p.findContent(n, contentKey)
if err != nil {
Expand All @@ -1429,7 +1435,13 @@ func (p *PortalProtocol) contentLookupWorker(n *enode.Node, contentKey []byte, r
if !ok {
return wrapedNode, fmt.Errorf("failed to assert to raw content, value is: %v", content)
}
resChan <- content
res := &ContentInfoRes{
Content: content,
}
if flag == portalwire.ContentConnIdSelector {
res.UtpTransfer = true
}
resChan <- res
return wrapedNode, err
case portalwire.ContentEnrsSelector:
nodes, ok := content.([]*enode.Node)
Expand Down
4 changes: 2 additions & 2 deletions p2p/discover/portal_protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,11 @@ func TestContentLookup(t *testing.T) {
err = node3.storage.Put(contentId, content)
assert.NoError(t, err)

res, err := node1.ContentLookup(contentKey)
res, _, err := node1.ContentLookup(contentKey)
assert.NoError(t, err)
assert.Equal(t, res, content)

res, err = node1.ContentLookup([]byte{0x2, 0x4})
res, _, err = node1.ContentLookup([]byte{0x2, 0x4})
assert.Equal(t, ContentNotFound, err)
assert.Nil(t, res)
}
8 changes: 4 additions & 4 deletions portalnetwork/history/history_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (h *HistoryNetwork) GetBlockHeader(blockHash []byte) (*types.Header, error)
// no content in local storage
for retries := 0; retries < requestRetries; retries++ {
// TODO log the err and continue
content, err := h.portalProtocol.ContentLookup(contentKey)
content, _, err := h.portalProtocol.ContentLookup(contentKey)
if err != nil {
continue
}
Expand Down Expand Up @@ -165,7 +165,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)
if err != nil {
continue
}
Expand Down Expand Up @@ -215,7 +215,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)
if err != nil {
continue
}
Expand Down Expand Up @@ -245,7 +245,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)
if err != nil {
continue
}
Expand Down

0 comments on commit 3936f0b

Please sign in to comment.