From b2b55a54fb663649f089eab773d36354132cec73 Mon Sep 17 00:00:00 2001 From: Suyash Nayan <89125422+7suyash7@users.noreply.github.com> Date: Fri, 16 Aug 2024 06:07:19 +0530 Subject: [PATCH] Add 'inbound' field to admin_peers JSON-RPC Call (#7461) * Add 'inbound' field to admin_peers JSON-RPC Call Signed-off-by: 7suyash7 * added changelog entry Signed-off-by: Sally MacFarlane --------- Signed-off-by: 7suyash7 Signed-off-by: Sally MacFarlane Co-authored-by: Sally MacFarlane --- CHANGELOG.md | 1 + .../jsonrpc/internal/results/NetworkResult.java | 10 +++++++++- .../api/jsonrpc/internal/results/PeerResult.java | 6 +++++- .../internal/results/NetworkResultTest.java | 15 ++++++++++++++- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a16284015..183ca24e55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - Added support for tracing private transactions using `priv_traceTransaction` API. [#6161](https://github.com/hyperledger/besu/pull/6161) - Wrap WorldUpdater into EVMWorldupdater [#7434](https://github.com/hyperledger/besu/pull/7434) - Bump besu-native to 0.9.4 [#7456](https://github.com/hyperledger/besu/pull/7456) +- Add 'inbound' field to admin_peers JSON-RPC Call [#7461](https://github.com/hyperledger/besu/pull/7461) ### Bug fixes diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/NetworkResult.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/NetworkResult.java index e5176c55b2..2a48201dcc 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/NetworkResult.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/NetworkResult.java @@ -24,10 +24,13 @@ public class NetworkResult { private final String localAddress; private final String remoteAddress; + private final boolean inbound; - public NetworkResult(final SocketAddress localAddress, final SocketAddress remoteAddress) { + public NetworkResult( + final SocketAddress localAddress, final SocketAddress remoteAddress, final boolean inbound) { this.localAddress = removeTrailingSlash(localAddress.toString()); this.remoteAddress = removeTrailingSlash(remoteAddress.toString()); + this.inbound = inbound; } @JsonGetter(value = "localAddress") @@ -40,6 +43,11 @@ public class NetworkResult { return remoteAddress; } + @JsonGetter(value = "inbound") + public boolean isInbound() { + return inbound; + } + private String removeTrailingSlash(final String address) { if (address != null && address.startsWith("/")) { return address.substring(1); diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/PeerResult.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/PeerResult.java index c420990d65..1bc2283acc 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/PeerResult.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/PeerResult.java @@ -45,7 +45,11 @@ public interface PeerResult { .map(Capability::toString) .map(TextNode::new) .collect(Collectors.toList())) - .network(new NetworkResult(connection.getLocalAddress(), connection.getRemoteAddress())) + .network( + new NetworkResult( + connection.getLocalAddress(), + connection.getRemoteAddress(), + connection.inboundInitiated())) .port(Quantity.create(peerInfo.getPort())) .id(peerInfo.getNodeId().toString()) .protocols(Map.of(peer.getProtocolName(), ProtocolsResult.fromEthPeer(peer))) diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/NetworkResultTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/NetworkResultTest.java index 876e795468..63f71c435f 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/NetworkResultTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/NetworkResultTest.java @@ -26,9 +26,22 @@ public class NetworkResultTest { @Test public void localAndRemoteAddressShouldNotStartWithForwardSlash() { final SocketAddress socketAddress = new InetSocketAddress("1.2.3.4", 7890); - final NetworkResult networkResult = new NetworkResult(socketAddress, socketAddress); + final NetworkResult networkResult = new NetworkResult(socketAddress, socketAddress, true); assertThat(networkResult.getLocalAddress()).isEqualTo("1.2.3.4:7890"); assertThat(networkResult.getRemoteAddress()).isEqualTo("1.2.3.4:7890"); + assertThat(networkResult.isInbound()).isTrue(); + } + + @Test + public void inboundFieldShouldReflectConnectionDirection() { + final SocketAddress localAddress = new InetSocketAddress("192.168.0.1", 30303); + final SocketAddress remoteAddress = new InetSocketAddress("10.0.0.1", 30303); + + final NetworkResult inboundConnection = new NetworkResult(localAddress, remoteAddress, true); + assertThat(inboundConnection.isInbound()).isTrue(); + + final NetworkResult outboundConnection = new NetworkResult(localAddress, remoteAddress, false); + assertThat(outboundConnection.isInbound()).isFalse(); } }