mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-31 09:03:46 +00:00
feat:add more disc/portal api
Signed-off-by: Chen Kai <281165273grape@gmail.com>
This commit is contained in:
parent
555b58c26c
commit
2b0315e3ab
1 changed files with 161 additions and 0 deletions
|
|
@ -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"
|
||||
|
|
@ -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()
|
||||
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue