You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2023/01/29 05:32:49 UTC

[incubator-tuweni] branch main updated: Expose an endpoint to see active peer info

This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git


The following commit(s) were added to refs/heads/main by this push:
     new 5a22cb317 Expose an endpoint to see active peer info
     new 04932a8d8 Merge pull request #498 from atoulme/add_peer_info
5a22cb317 is described below

commit 5a22cb31747c6447899da7146f8e4a1d12bb74fd
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Sat Jan 28 18:25:50 2023 -0800

    Expose an endpoint to see active peer info
---
 eth-client-ui/build.gradle                         |  1 +
 .../tuweni/ethclientui/controller/StateService.kt  | 39 ++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/eth-client-ui/build.gradle b/eth-client-ui/build.gradle
index b1f7d44d6..8151e0539 100644
--- a/eth-client-ui/build.gradle
+++ b/eth-client-ui/build.gradle
@@ -31,6 +31,7 @@ dependencies {
   implementation project(':config')
   implementation project(':concurrent-coroutines')
   implementation project(':crypto')
+  implementation project(':devp2p-eth')
   implementation project(':eth')
   implementation project(':eth-client')
   implementation project(':eth-repository')
diff --git a/eth-client-ui/src/main/kotlin/org/apache/tuweni/ethclientui/controller/StateService.kt b/eth-client-ui/src/main/kotlin/org/apache/tuweni/ethclientui/controller/StateService.kt
index 0a6e99c24..cb42d1420 100644
--- a/eth-client-ui/src/main/kotlin/org/apache/tuweni/ethclientui/controller/StateService.kt
+++ b/eth-client-ui/src/main/kotlin/org/apache/tuweni/ethclientui/controller/StateService.kt
@@ -19,17 +19,32 @@ package org.apache.tuweni.ethclientui.controller
 import kotlinx.coroutines.runBlocking
 import org.apache.tuweni.bytes.Bytes32
 import org.apache.tuweni.ethclient.EthereumClient
+import org.apache.tuweni.peer.repository.Identity
 import org.apache.tuweni.units.bigints.UInt256
 import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.http.HttpStatus
 import org.springframework.http.MediaType
 import org.springframework.web.bind.annotation.GetMapping
+import org.springframework.web.bind.annotation.PathVariable
 import org.springframework.web.bind.annotation.RequestMapping
 import org.springframework.web.bind.annotation.RestController
+import org.springframework.web.server.ResponseStatusException
+import java.time.Instant
+import kotlin.streams.toList
 
 data class BlockHashAndNumber(val hash: Bytes32, val number: UInt256)
 
 data class State(val peerCounts: Map<String, Long>, val bestBlocks: Map<String, BlockHashAndNumber>)
 
+data class Peer(
+  val id: Identity,
+  val networkID: UInt256?,
+  val bestHash: Bytes32?,
+  val totalDifficulty: UInt256?,
+  val lastContacted: Instant?
+)
+data class Peers(val peers: List<Peer>)
+
 @RestController
 @RequestMapping("/rest/state")
 class StateService {
@@ -50,4 +65,28 @@ class StateService {
     }
     return State(mapOf(*peerCounts.toTypedArray()), mapOf(*bestBlocks.toTypedArray()))
   }
+
+  @GetMapping(value = ["{id}/peers"], produces = [MediaType.APPLICATION_JSON_VALUE])
+  fun getPeers(@PathVariable id: String): Peers {
+    val repository = client!!.peerRepositories[id]
+      ?: throw ResponseStatusException(
+        HttpStatus.NOT_FOUND,
+        "entity not found"
+      )
+    val peers = Peers(
+      repository.activeConnections().map {
+        it.peer().id()
+        val peer = Peer(
+          it.peer().id(),
+          it.status()?.networkID,
+          it.status()?.bestHash,
+          it.status()?.totalDifficulty,
+          it.peer().lastContacted()
+        )
+        peer
+      }.toList()
+    )
+
+    return peers
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org