Skip to content

Commit

Permalink
Add 'inbound' field to admin_peers JSON-RPC Call (hyperledger#7461)
Browse files Browse the repository at this point in the history
* Add 'inbound' field to admin_peers JSON-RPC Call

Signed-off-by: 7suyash7 <suyashnyn1@gmail.com>

* added changelog entry

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

---------

Signed-off-by: 7suyash7 <suyashnyn1@gmail.com>
Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com>
Signed-off-by: gconnect <agatevureglory@gmail.com>
  • Loading branch information
2 people authored and gconnect committed Aug 26, 2024
1 parent 2795655 commit b9bcd4a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -40,6 +43,11 @@ public String getRemoteAddress() {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ static PeerResult fromEthPeer(final EthPeer peer) {
.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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit b9bcd4a

Please sign in to comment.