-
Notifications
You must be signed in to change notification settings - Fork 70
chore(p2p): changes necessary by swarm-network-rewrite #16898 #2051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package adapters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "bufio" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "crypto/ecdsa" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "encoding/json" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -103,9 +104,9 @@ func (e *ExecAdapter) NewNode(config *NodeConfig) (Node, error) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| conf.Stack.P2P.NoDiscovery = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| conf.Stack.P2P.NAT = nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // listen on a random localhost port (we'll get the actual port after | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // starting the node through the RPC admin.nodeInfo method) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| conf.Stack.P2P.ListenAddr = "127.0.0.1:0" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // listen on a localhost port, which we set when we | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // initialise NodeConfig (usually a random port) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| conf.Stack.P2P.ListenAddr = fmt.Sprintf(":%d", config.Port) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| node := &ExecNode{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ID: config.ID, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -189,9 +190,23 @@ func (n *ExecNode) Start(snapshots map[string][]byte) (err error) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| n.Cmd = cmd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // read the WebSocket address from the stderr logs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wsAddr, err := findWSAddr(stderrR, 10*time.Second) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("error getting WebSocket address: %s", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var wsAddr string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wsAddrC := make(chan string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| go func() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| s := bufio.NewScanner(stderrR) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for s.Scan() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if strings.Contains(s.Text(), "WebSocket endpoint opened") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wsAddrC <- wsAddrPattern.FindString(s.Text()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| select { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case wsAddr = <-wsAddrC: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if wsAddr == "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return errors.New("failed to read WebSocket address from stderr") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case <-time.After(10 * time.Second): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return errors.New("timed out waiting for WebSocket address on stderr") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // create the RPC client and load the node info | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -321,6 +336,21 @@ type execNodeConfig struct { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PeerAddrs map[string]string `json:"peer_addrs,omitempty"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ExternalIP gets an external IP address so that Enode URL is usable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func ExternalIP() net.IP { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| addrs, err := net.InterfaceAddrs() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.Crit("error getting IP address", "err", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, addr := range addrs { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() && !ip.IP.IsLinkLocalUnicast() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ip.IP | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.Warn("unable to determine explicit IP address, falling back to loopback") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return net.IP{127, 0, 0, 1} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+339
to
+351
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ExternalIP gets an external IP address so that Enode URL is usable | |
| func ExternalIP() net.IP { | |
| addrs, err := net.InterfaceAddrs() | |
| if err != nil { | |
| log.Crit("error getting IP address", "err", err) | |
| } | |
| for _, addr := range addrs { | |
| if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() && !ip.IP.IsLinkLocalUnicast() { | |
| return ip.IP | |
| } | |
| } | |
| log.Warn("unable to determine explicit IP address, falling back to loopback") | |
| return net.IP{127, 0, 0, 1} | |
| // ExternalIP gets an external IP address so that Enode URL is usable. | |
| // It prefers IPv4 addresses and falls back to IPv6 if no suitable IPv4 is found. | |
| func ExternalIP() net.IP { | |
| addrs, err := net.InterfaceAddrs() | |
| if err != nil { | |
| log.Crit("error getting IP address", "err", err) | |
| } | |
| var ipv6Candidate net.IP | |
| for _, addr := range addrs { | |
| ipNet, ok := addr.(*net.IPNet) | |
| if !ok { | |
| continue | |
| } | |
| ip := ipNet.IP | |
| if ip.IsLoopback() || ip.IsLinkLocalUnicast() { | |
| continue | |
| } | |
| // Prefer IPv4 addresses. | |
| if ip4 := ip.To4(); ip4 != nil { | |
| return ip4 | |
| } | |
| // Remember the first suitable IPv6 address as a fallback. | |
| if ipv6Candidate == nil { | |
| ipv6Candidate = ip | |
| } | |
| } | |
| if ipv6Candidate != nil { | |
| return ipv6Candidate | |
| } | |
| log.Warn("unable to determine explicit IP address, falling back to loopback") | |
| return net.IPv4(127, 0, 0, 1) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -28,13 +28,15 @@ import ( | |||||||||||||||||||||||||||||||
| "github.com/XinFinOrg/XDPoSChain/node" | ||||||||||||||||||||||||||||||||
| "github.com/XinFinOrg/XDPoSChain/p2p" | ||||||||||||||||||||||||||||||||
| "github.com/XinFinOrg/XDPoSChain/p2p/discover" | ||||||||||||||||||||||||||||||||
| "github.com/XinFinOrg/XDPoSChain/p2p/simulations/pipes" | ||||||||||||||||||||||||||||||||
| "github.com/XinFinOrg/XDPoSChain/rpc" | ||||||||||||||||||||||||||||||||
| "github.com/gorilla/websocket" | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // SimAdapter is a NodeAdapter which creates in-memory simulation nodes and | ||||||||||||||||||||||||||||||||
| // connects them using in-memory net.Pipe connections | ||||||||||||||||||||||||||||||||
| // connects them using net.Pipe | ||||||||||||||||||||||||||||||||
| type SimAdapter struct { | ||||||||||||||||||||||||||||||||
| pipe func() (net.Conn, net.Conn, error) | ||||||||||||||||||||||||||||||||
| mtx sync.RWMutex | ||||||||||||||||||||||||||||||||
| nodes map[discover.NodeID]*SimNode | ||||||||||||||||||||||||||||||||
| lifecycles LifecycleConstructors | ||||||||||||||||||||||||||||||||
|
|
@@ -43,10 +45,18 @@ type SimAdapter struct { | |||||||||||||||||||||||||||||||
| // NewSimAdapter creates a SimAdapter which is capable of running in-memory | ||||||||||||||||||||||||||||||||
| // simulation nodes running any of the given services (the services to run on a | ||||||||||||||||||||||||||||||||
| // particular node are passed to the NewNode function in the NodeConfig) | ||||||||||||||||||||||||||||||||
| // the adapter uses a net.Pipe for in-memory simulated network connections | ||||||||||||||||||||||||||||||||
| func NewSimAdapter(services LifecycleConstructors) *SimAdapter { | ||||||||||||||||||||||||||||||||
| return &SimAdapter{ | ||||||||||||||||||||||||||||||||
| // nodes: make(map[discover.NodeID]*SimNode), | ||||||||||||||||||||||||||||||||
| // lifecycles: lifecycles, | ||||||||||||||||||||||||||||||||
| pipe: pipes.NetPipe, | ||||||||||||||||||||||||||||||||
| nodes: make(map[discover.NodeID]*SimNode), | ||||||||||||||||||||||||||||||||
| lifecycles: services, | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| func NewTCPAdapter(services LifecycleConstructors) *SimAdapter { | ||||||||||||||||||||||||||||||||
| return &SimAdapter{ | ||||||||||||||||||||||||||||||||
| pipe: pipes.TCPPipe, | ||||||||||||||||||||||||||||||||
| nodes: make(map[discover.NodeID]*SimNode), | ||||||||||||||||||||||||||||||||
| lifecycles: services, | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
@@ -84,7 +94,7 @@ func (sa *SimAdapter) NewNode(config *NodeConfig) (Node, error) { | |||||||||||||||||||||||||||||||
| MaxPeers: math.MaxInt32, | ||||||||||||||||||||||||||||||||
| NoDiscovery: true, | ||||||||||||||||||||||||||||||||
| Dialer: sa, | ||||||||||||||||||||||||||||||||
| EnableMsgEvents: true, | ||||||||||||||||||||||||||||||||
| EnableMsgEvents: config.EnableMsgEvents, | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| Logger: log.New("node.id", id), | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
|
|
@@ -105,7 +115,7 @@ func (sa *SimAdapter) NewNode(config *NodeConfig) (Node, error) { | |||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // Dial implements the p2p.NodeDialer interface by connecting to the node using | ||||||||||||||||||||||||||||||||
| // an in-memory net.Pipe connection | ||||||||||||||||||||||||||||||||
| // an in-memory net.Pipe | ||||||||||||||||||||||||||||||||
| func (sa *SimAdapter) Dial(dest *discover.Node) (conn net.Conn, err error) { | ||||||||||||||||||||||||||||||||
| node, ok := sa.GetNode(dest.ID) | ||||||||||||||||||||||||||||||||
| if !ok { | ||||||||||||||||||||||||||||||||
|
|
@@ -118,7 +128,14 @@ func (sa *SimAdapter) Dial(dest *discover.Node) (conn net.Conn, err error) { | |||||||||||||||||||||||||||||||
| if srv == nil { | ||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("node not running: %s", dest.ID) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| pipe1, pipe2 := net.Pipe() | ||||||||||||||||||||||||||||||||
| // SimAdapter.pipe is net.Pipe (NewSimAdapter) | ||||||||||||||||||||||||||||||||
| pipe1, pipe2, err := sa.pipe() | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| // this is simulated 'listening' | ||||||||||||||||||||||||||||||||
| // asynchronously call the dialed destintion node's p2p server | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| // asynchronously call the dialed destintion node's p2p server | |
| // asynchronously call the dialed destination node's p2p server |
Copilot
AI
Feb 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The setSocketBuffer function is defined but never called. If this function is intended for future use, consider adding a comment explaining its purpose. Otherwise, consider removing it to avoid dead code.
| func setSocketBuffer(conn net.Conn, socketReadBuffer int, socketWriteBuffer int) error { | |
| switch v := conn.(type) { | |
| case *net.UnixConn: | |
| err := v.SetReadBuffer(socketReadBuffer) | |
| if err != nil { | |
| return err | |
| } | |
| err = v.SetWriteBuffer(socketWriteBuffer) | |
| if err != nil { | |
| return err | |
| } | |
| } | |
| return nil | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fallback IP address should use net.IPv4 for better clarity and consistency with Go conventions. Consider: return net.IPv4(127, 0, 0, 1)