Skip to content

Commit

Permalink
feat:add more disc/portal api
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <281165273grape@gmail.com>
  • Loading branch information
GrapeBaBa committed Dec 12, 2023
1 parent 555b58c commit 2b0315e
Showing 1 changed file with 161 additions and 0 deletions.
161 changes: 161 additions & 0 deletions p2p/discover/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package discover
import (
"errors"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/p2p/discover/portalwire"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/holiman/uint256"
Expand Down Expand Up @@ -38,6 +39,15 @@ type PortalPongResp struct {
DataRadius string `json:"dataRadius"`
}

type ContentInfo struct {
Content string `json:"content"`
UtpTransfer bool `json:"utpTransfer"`
}

type Enrs struct {
Enrs []string `json:"enrs"`
}

func (d *DiscV5API) NodeInfo() *NodeInfo {
n := d.DiscV5.LocalNode().Node()

Expand Down Expand Up @@ -134,6 +144,53 @@ func (d *DiscV5API) Ping(enr string) (*DiscV5PongResp, error) {
}, nil
}

func (d *DiscV5API) FindNodes(enr string, distances []uint) ([]string, error) {
n, err := enode.Parse(enode.ValidSchemes, enr)
if err != nil {
return nil, err
}
findNodes, err := d.DiscV5.findnode(n, distances)
if err != nil {
return nil, err
}

enrs := make([]string, 0, len(findNodes))
for _, r := range findNodes {
enrs = append(enrs, r.String())
}

return enrs, nil
}

func (d *DiscV5API) TalkReq(enr string, protocol string, payload string) (string, error) {
n, err := enode.Parse(enode.ValidSchemes, enr)
if err != nil {
return "", err
}

req, err := hexutil.Decode(payload)
if err != nil {
return "", err
}

talkResp, err := d.DiscV5.TalkRequest(n, protocol, req)
if err != nil {
return "", err
}
return hexutil.Encode(talkResp), nil
}

func (d *DiscV5API) RecursiveFindNodes(nodeId string) ([]string, error) {
findNodes := d.DiscV5.Lookup(enode.HexID(nodeId))

enrs := make([]string, 0, len(findNodes))
for _, r := range findNodes {
enrs = append(enrs, r.String())
}

return enrs, nil
}

type PortalAPI struct {
*DiscV5API
portalProtocol *PortalProtocol
Expand Down Expand Up @@ -271,3 +328,107 @@ func (p *PortalAPI) Ping(enr string) (*PortalPongResp, error) {
DataRadius: nodeRadius.Hex(),
}, nil
}

func (p *PortalAPI) FindNodes(enr string, distances []uint) ([]string, error) {
n, err := enode.Parse(enode.ValidSchemes, enr)
if err != nil {
return nil, err
}
findNodes, err := p.portalProtocol.findNodes(n, distances)
if err != nil {
return nil, err
}

enrs := make([]string, 0, len(findNodes))
for _, r := range findNodes {
enrs = append(enrs, r.String())
}

return enrs, nil
}

func (p *PortalAPI) FindContent(enr string, contentKey string) (interface{}, error) {
n, err := enode.Parse(enode.ValidSchemes, enr)
if err != nil {
return nil, err
}

contentKeyBytes, err := hexutil.Decode(contentKey)
if err != nil {
return nil, err
}

flag, findContent, err := p.portalProtocol.findContent(n, contentKeyBytes)
if err != nil {
return nil, err
}

switch flag {
case portalwire.ContentRawSelector:
return &ContentInfo{
Content: hexutil.Encode(findContent.([]byte)),
UtpTransfer: false,
}, nil
case portalwire.ContentConnIdSelector:
return &ContentInfo{
Content: hexutil.Encode(findContent.([]byte)),
UtpTransfer: true,
}, nil
default:
enrs := make([]string, 0)
for _, r := range findContent.([]*enode.Node) {
enrs = append(enrs, r.String())
}

return &Enrs{
Enrs: enrs,
}, nil
}
}

func (p *PortalAPI) Offer(enr string, contentKey string, contentValue string) (string, error) {
n, err := enode.Parse(enode.ValidSchemes, enr)
if err != nil {
return "", err
}

contentKeyBytes, err := hexutil.Decode(contentKey)
if err != nil {
return "", err
}
contentValueBytes, err := hexutil.Decode(contentValue)
if err != nil {
return "", err
}

contentEntry := &ContentEntry{
ContentKey: contentKeyBytes,
Content: contentValueBytes,
}

transientOfferRequest := &TransientOfferRequest{
Contents: []*ContentEntry{contentEntry},
}

offerReq := &OfferRequest{
Kind: portalwire.OfferRequestDirect,
Request: transientOfferRequest,
}
accept, err := p.portalProtocol.offer(n, offerReq)
if err != nil {
return "", err
}

return hexutil.Encode(accept), nil
}

func (p *PortalAPI) RecursiveFindNodes(nodeId string) ([]string, error) {
findNodes := p.portalProtocol.Lookup(enode.HexID(nodeId))

enrs := make([]string, 0, len(findNodes))
for _, r := range findNodes {
enrs = append(enrs, r.String())
}

return enrs, nil
}

0 comments on commit 2b0315e

Please sign in to comment.