feat: beacon start up

This commit is contained in:
fearlessfe 2024-04-16 08:20:58 +08:00 committed by Chen Kai
parent 5dc91e20ba
commit 9c0a5ccb74
4 changed files with 36 additions and 18 deletions

View file

@ -114,14 +114,14 @@ func startPortalRpcServer(config Config, conn discover.UDPConn, logger log.Logge
return err return err
} }
if slices.Contains(config.Networks, "history") { if slices.Contains(config.Networks, portalwire.HistoryNetworkName) {
err = initHistory(config, server, conn, localNode, discV5) err = initHistory(config, server, conn, localNode, discV5)
if err != nil { if err != nil {
return err return err
} }
} }
if slices.Contains(config.Networks, "beacon") { if slices.Contains(config.Networks, portalwire.BeaconNetworkName) {
err = initBeacon(config, server, conn, localNode, discV5) err = initBeacon(config, server, conn, localNode, discV5)
if err != nil { if err != nil {
return err return err
@ -229,7 +229,7 @@ func initBeacon(config Config, server *rpc.Server, conn discover.UDPConn, localN
} }
contentQueue := make(chan *discover.ContentElement, 50) contentQueue := make(chan *discover.ContentElement, 50)
protocol, err := discover.NewPortalProtocol(config.Protocol, string(portalwire.HistoryNetwork), config.PrivateKey, conn, localNode, discV5, contentStorage, contentQueue) protocol, err := discover.NewPortalProtocol(config.Protocol, string(portalwire.BeaconLightClientNetwork), config.PrivateKey, conn, localNode, discV5, contentStorage, contentQueue)
if err != nil { if err != nil {
return err return err
@ -237,7 +237,7 @@ func initBeacon(config Config, server *rpc.Server, conn discover.UDPConn, localN
portalApi := discover.NewPortalAPI(protocol) portalApi := discover.NewPortalAPI(protocol)
beaconAPI := beacon.NewBeaconNetworkAPI(portalApi) beaconAPI := beacon.NewBeaconNetworkAPI(portalApi)
err = server.RegisterName("beacon", beaconAPI) err = server.RegisterName("portal", beaconAPI)
if err != nil { if err != nil {
return err return err
} }

View file

@ -66,6 +66,7 @@ import (
"github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/miner"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover/portalwire"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/nat" "github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/p2p/netutil" "github.com/ethereum/go-ethereum/p2p/netutil"
@ -1019,7 +1020,7 @@ Please note that --` + MetricsHTTPFlag.Name + ` must be set to start the server.
Name: "networks", Name: "networks",
Usage: "portal sub networks: history, beacon, state", Usage: "portal sub networks: history, beacon, state",
Category: flags.PortalNetworkCategory, Category: flags.PortalNetworkCategory,
Value: cli.NewStringSlice("history", "beacon", "state"), Value: cli.NewStringSlice(portalwire.HistoryNetworkName, portalwire.BeaconNetworkName, portalwire.StateNetworkName),
} }
) )

View file

@ -156,7 +156,8 @@ func DefaultPortalProtocolConfig() *PortalProtocolConfig {
type PortalProtocol struct { type PortalProtocol struct {
table *Table table *Table
protocolId string protocolId string
protocolName string
nodeRadius *uint256.Int nodeRadius *uint256.Int
DiscV5 *UDPv5 DiscV5 *UDPv5
@ -192,6 +193,7 @@ func NewPortalProtocol(config *PortalProtocolConfig, protocolId string, privateK
protocol := &PortalProtocol{ protocol := &PortalProtocol{
protocolId: protocolId, protocolId: protocolId,
protocolName: portalwire.NetworkNameMap[protocolId],
ListenAddr: config.ListenAddr, ListenAddr: config.ListenAddr,
Log: log.New("protocol", protocolId), Log: log.New("protocol", protocolId),
PrivateKey: privateKey, PrivateKey: privateKey,
@ -376,7 +378,7 @@ func (p *PortalProtocol) pingInner(node *enode.Node) (*portalwire.Pong, error) {
CustomPayload: customPayloadBytes, CustomPayload: customPayloadBytes,
} }
p.Log.Trace("Sending ping request", "protocol", p.protocolId, "ip", p.Self().IP().String(), "source", p.Self().ID(), "target", node.ID(), "ping", pingRequest) p.Log.Trace("Sending ping request", "protocol", p.protocolName, "ip", p.Self().IP().String(), "source", p.Self().ID(), "target", node.ID(), "ping", pingRequest)
pingRequestBytes, err := pingRequest.MarshalSSZ() pingRequestBytes, err := pingRequest.MarshalSSZ()
if err != nil { if err != nil {
return nil, err return nil, err
@ -719,6 +721,9 @@ func (p *PortalProtocol) filterNodes(target *enode.Node, enrs [][]byte, distance
} }
func (p *PortalProtocol) processPong(target *enode.Node, resp []byte) (*portalwire.Pong, error) { func (p *PortalProtocol) processPong(target *enode.Node, resp []byte) (*portalwire.Pong, error) {
if len(resp) == 0 {
return nil, fmt.Errorf("empty resp")
}
if resp[0] != portalwire.PONG { if resp[0] != portalwire.PONG {
return nil, fmt.Errorf("invalid pong response") return nil, fmt.Errorf("invalid pong response")
} }
@ -771,7 +776,7 @@ func (p *PortalProtocol) handleTalkRequest(id enode.ID, addr *net.UDPAddr, msg [
return nil return nil
} }
p.Log.Trace("received ping request", "protocol", p.protocolId, "source", id, "pingRequest", pingRequest) p.Log.Trace("received ping request", "protocol", p.protocolName, "source", id, "pingRequest", pingRequest)
resp, err := p.handlePing(id, pingRequest) resp, err := p.handlePing(id, pingRequest)
if err != nil { if err != nil {
p.Log.Error("failed to handle ping request", "err", err) p.Log.Error("failed to handle ping request", "err", err)
@ -787,7 +792,7 @@ func (p *PortalProtocol) handleTalkRequest(id enode.ID, addr *net.UDPAddr, msg [
return nil return nil
} }
p.Log.Trace("received find nodes request", "protocol", p.protocolId, "source", id, "findNodesRequest", findNodesRequest) p.Log.Trace("received find nodes request", "protocol", p.protocolName, "source", id, "findNodesRequest", findNodesRequest)
resp, err := p.handleFindNodes(addr, findNodesRequest) resp, err := p.handleFindNodes(addr, findNodesRequest)
if err != nil { if err != nil {
p.Log.Error("failed to handle find nodes request", "err", err) p.Log.Error("failed to handle find nodes request", "err", err)
@ -803,7 +808,7 @@ func (p *PortalProtocol) handleTalkRequest(id enode.ID, addr *net.UDPAddr, msg [
return nil return nil
} }
p.Log.Trace("received find content request", "protocol", p.protocolId, "source", id, "findContentRequest", findContentRequest) p.Log.Trace("received find content request", "protocol", p.protocolName, "source", id, "findContentRequest", findContentRequest)
resp, err := p.handleFindContent(id, addr, findContentRequest) resp, err := p.handleFindContent(id, addr, findContentRequest)
if err != nil { if err != nil {
p.Log.Error("failed to handle find content request", "err", err) p.Log.Error("failed to handle find content request", "err", err)
@ -819,7 +824,7 @@ func (p *PortalProtocol) handleTalkRequest(id enode.ID, addr *net.UDPAddr, msg [
return nil return nil
} }
p.Log.Trace("received offer request", "protocol", p.protocolId, "source", id, "offerRequest", offerRequest) p.Log.Trace("received offer request", "protocol", p.protocolName, "source", id, "offerRequest", offerRequest)
resp, err := p.handleOffer(id, addr, offerRequest) resp, err := p.handleOffer(id, addr, offerRequest)
if err != nil { if err != nil {
p.Log.Error("failed to handle offer request", "err", err) p.Log.Error("failed to handle offer request", "err", err)
@ -860,7 +865,7 @@ func (p *PortalProtocol) handlePing(id enode.ID, ping *portalwire.Ping) ([]byte,
CustomPayload: pongCustomPayloadBytes, CustomPayload: pongCustomPayloadBytes,
} }
p.Log.Trace("Sending pong response", "protocol", p.protocolId, "source", id, "pong", pong) p.Log.Trace("Sending pong response", "protocol", p.protocolName, "source", id, "pong", pong)
pongBytes, err := pong.MarshalSSZ() pongBytes, err := pong.MarshalSSZ()
if err != nil { if err != nil {
@ -893,7 +898,7 @@ func (p *PortalProtocol) handleFindNodes(fromAddr *net.UDPAddr, request *portalw
Enrs: enrs, Enrs: enrs,
} }
p.Log.Trace("Sending nodes response", "protocol", p.protocolId, "source", fromAddr, "nodes", nodesMsg) p.Log.Trace("Sending nodes response", "protocol", p.protocolName, "source", fromAddr, "nodes", nodesMsg)
nodesMsgBytes, err := nodesMsg.MarshalSSZ() nodesMsgBytes, err := nodesMsg.MarshalSSZ()
if err != nil { if err != nil {
return nil, err return nil, err
@ -942,7 +947,7 @@ func (p *PortalProtocol) handleFindContent(id enode.ID, addr *net.UDPAddr, reque
Enrs: enrs, Enrs: enrs,
} }
p.Log.Trace("Sending enrs content response", "protocol", p.protocolId, "source", addr, "enrs", enrsMsg) p.Log.Trace("Sending enrs content response", "protocol", p.protocolName, "source", addr, "enrs", enrsMsg)
var enrsMsgBytes []byte var enrsMsgBytes []byte
enrsMsgBytes, err = enrsMsg.MarshalSSZ() enrsMsgBytes, err = enrsMsg.MarshalSSZ()
if err != nil { if err != nil {
@ -963,7 +968,7 @@ func (p *PortalProtocol) handleFindContent(id enode.ID, addr *net.UDPAddr, reque
Content: content, Content: content,
} }
p.Log.Trace("Sending raw content response", "protocol", p.protocolId, "source", addr, "content", rawContentMsg) p.Log.Trace("Sending raw content response", "protocol", p.protocolName, "source", addr, "content", rawContentMsg)
var rawContentMsgBytes []byte var rawContentMsgBytes []byte
rawContentMsgBytes, err = rawContentMsg.MarshalSSZ() rawContentMsgBytes, err = rawContentMsg.MarshalSSZ()
@ -1042,7 +1047,7 @@ func (p *PortalProtocol) handleFindContent(id enode.ID, addr *net.UDPAddr, reque
Id: idBuffer, Id: idBuffer,
} }
p.Log.Trace("Sending connection id content response", "protocol", p.protocolId, "source", addr, "connId", connIdMsg) p.Log.Trace("Sending connection id content response", "protocol", p.protocolName, "source", addr, "connId", connIdMsg)
var connIdMsgBytes []byte var connIdMsgBytes []byte
connIdMsgBytes, err = connIdMsg.MarshalSSZ() connIdMsgBytes, err = connIdMsg.MarshalSSZ()
if err != nil { if err != nil {
@ -1070,7 +1075,7 @@ func (p *PortalProtocol) handleOffer(id enode.ID, addr *net.UDPAddr, request *po
ContentKeys: contentKeyBitlist, ContentKeys: contentKeyBitlist,
} }
p.Log.Trace("Sending accept response", "protocol", p.protocolId, "source", addr, "accept", acceptMsg) p.Log.Trace("Sending accept response", "protocol", p.protocolName, "source", addr, "accept", acceptMsg)
var acceptMsgBytes []byte var acceptMsgBytes []byte
acceptMsgBytes, err = acceptMsg.MarshalSSZ() acceptMsgBytes, err = acceptMsg.MarshalSSZ()
if err != nil { if err != nil {
@ -1156,7 +1161,7 @@ func (p *PortalProtocol) handleOffer(id enode.ID, addr *net.UDPAddr, request *po
ContentKeys: []byte(contentKeyBitlist), ContentKeys: []byte(contentKeyBitlist),
} }
p.Log.Trace("Sending accept response", "protocol", p.protocolId, "source", addr, "accept", acceptMsg) p.Log.Trace("Sending accept response", "protocol", p.protocolName, "source", addr, "accept", acceptMsg)
var acceptMsgBytes []byte var acceptMsgBytes []byte
acceptMsgBytes, err = acceptMsg.MarshalSSZ() acceptMsgBytes, err = acceptMsg.MarshalSSZ()
if err != nil { if err != nil {

View file

@ -48,6 +48,18 @@ var (
Rendezvous = []byte{0x72, 0x65, 0x6e} Rendezvous = []byte{0x72, 0x65, 0x6e}
) )
const (
HistoryNetworkName = "history"
BeaconNetworkName = "beacon"
StateNetworkName = "state"
)
var NetworkNameMap = map[string]string{
string(StateNetwork): StateNetworkName,
string(HistoryNetwork): HistoryNetworkName,
string(StateNetwork): StateNetworkName,
}
type ContentKV struct { type ContentKV struct {
ContentKey []byte ContentKey []byte
Content []byte Content []byte