Skip to content

Conversation

@gzliudan
Copy link
Collaborator

Proposed changes

Ref: ethereum#16898

Types of changes

What types of changes does your code introduce to XDC network?
Put an in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)
  • Regular KTLO or any of the maintaince work. e.g code style
  • CICD Improvement

Impacted Components

Which part of the codebase this PR will touch base on,

Put an in the boxes that apply

  • Consensus
  • Account
  • Network
  • Geth
  • Smart Contract
  • External components
  • Not sure (Please specify below)

Checklist

Put an in the boxes once you have confirmed below actions (or provide reasons on not doing so) that

  • This PR has sufficient test coverage (unit/integration test) OR I have provided reason in the PR description for not having test coverage
  • Provide an end-to-end test plan in the PR description on how to manually test it on the devnet/testnet.
  • Tested the backwards compatibility.
  • Tested with XDC nodes running this version co-exist with those running the previous version.
  • Relevant documentation has been updated as part of this PR
  • N/A

Copilot AI review requested due to automatic review settings February 11, 2026 08:37
@coderabbitai
Copy link

coderabbitai bot commented Feb 11, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces changes necessary for the swarm-network-rewrite effort (ethereum#16898), focusing on improving the p2p simulation infrastructure. The changes enhance network simulation capabilities by making pipe connections configurable, improving node configuration handling, and standardizing logging patterns.

Changes:

  • Introduced a new pipes package to centralize TCP and net.Pipe implementations for simulation testing
  • Enhanced node configuration with Port field and made pipe connections configurable (NetPipe vs TCPPipe)
  • Standardized error logging to use structured key-value format throughout the simulation code
  • Added metrics tracking for peer message sending in the protocols package

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
p2p/simulations/pipes/pipes.go New package providing NetPipe and TCPPipe functions for simulation connections
p2p/simulations/network_test.go Updated to use RandomNodeConfig for node creation
p2p/simulations/network.go Moved GetNodes method for better code organization
p2p/simulations/mocker.go Improved error handling and updated logging to structured format
p2p/simulations/http_test.go Updated tests to use RandomNodeConfig before creating nodes
p2p/simulations/http.go Changed CreateNode to accept configurable NodeConfig instead of always generating random config
p2p/simulations/adapters/types.go Added Port field to NodeConfig and assignTCPPort function for port allocation
p2p/simulations/adapters/inproc_test.go New comprehensive test suite for TCPPipe and NetPipe functionality
p2p/simulations/adapters/inproc.go Made pipe connections configurable, added NewTCPAdapter, fixed service registration error handling
p2p/simulations/adapters/exec.go Refactored ExternalIP into reusable function with better error handling
p2p/simulations/adapters/docker.go Extracted ErrLinuxOnly as package variable and added Port configuration
p2p/rlpx_test.go Migrated tcpPipe function to shared pipes package
p2p/protocols/protocol.go Added metrics for tracking peer message sending
Comments suppressed due to low confidence (1)

p2p/simulations/adapters/inproc.go:63

  • The NewTCPAdapter function should have a documentation comment explaining its purpose and how it differs from NewSimAdapter. This helps developers understand when to use TCPPipe vs NetPipe for simulations.
func NewTCPAdapter(services LifecycleConstructors) *SimAdapter {
	return &SimAdapter{
		pipe:       pipes.TCPPipe,
		nodes:      make(map[discover.NodeID]*SimNode),
		lifecycles: services,
	}
}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if err != nil {
return 0, err
}
l.Close()
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The listener should be closed before attempting to parse the port to avoid a potential race condition. If another process binds to the same port between l.Close() and when the port is actually used, there could be a conflict. Consider keeping the listener open longer or document this limitation.

Suggested change
l.Close()
defer l.Close()

Copilot uses AI. Check for mistakes.
Comment on lines +337 to +351

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
}
Copy link

Copilot AI Feb 11, 2026

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.

Suggested change
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
}

Copilot uses AI. Check for mistakes.
return nil, err
}
// this is simulated 'listening'
// asynchronously call the dialed destintion node's p2p server
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in comment: 'destintion' should be 'destination'.

Suggested change
// asynchronously call the dialed destintion node's p2p server
// asynchronously call the dialed destination node's p2p server

Copilot uses AI. Check for mistakes.
panic("Could not startup node network for mocker")
select {
case <-quit:
//error may be due to abortion of mocking; so the quit channel is closed
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment should follow standard Go comment conventions - it should be a complete sentence starting with a capital letter and ending with a period. Consider: "Error may be due to abortion of mocking; so the quit channel is closed."

Suggested change
//error may be due to abortion of mocking; so the quit channel is closed
// Error may be due to abortion of mocking, so the quit channel is closed.

Copilot uses AI. Check for mistakes.
}
}
log.Warn("unable to determine explicit IP address, falling back to loopback")
return net.IP{127, 0, 0, 1}
Copy link

Copilot AI Feb 11, 2026

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)

Suggested change
return net.IP{127, 0, 0, 1}
return net.IPv4(127, 0, 0, 1)

Copilot uses AI. Check for mistakes.
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}
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ExternalIP function should prefer IPv4 addresses or be more specific about which IP version to return. Currently, it may return an IPv6 address when callers expect IPv4, which could cause issues with P2P.ListenAddr formatting. Consider filtering for IPv4 addresses specifically or handling both IP versions appropriately.

Suggested change
// 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)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant