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 4a8760d  Add more JSON-RPC methods
     new df0197a  Merge pull request #386 from atoulme/more_rpc_methods
4a8760d is described below

commit 4a8760d86588d7dacbe1bc9b1274505ffccebf7e
Author: Antoine Toulme <[email protected]>
AuthorDate: Sun Mar 20 16:09:44 2022 -0700

    Add more JSON-RPC methods
---
 .../tuweni/jsonrpc/methods/MethodsHandler.kt       | 19 ++++++++++++
 .../tuweni/jsonrpc/methods/{Web3.kt => Net.kt}     | 35 +++++++++++-----------
 .../org/apache/tuweni/jsonrpc/methods/Web3.kt      | 11 ++++---
 .../org/apache/tuweni/jsonrpc/methods/NetTest.kt}  | 28 +++++++----------
 4 files changed, 51 insertions(+), 42 deletions(-)

diff --git 
a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/MethodsHandler.kt 
b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/MethodsHandler.kt
index 45a18c3..2feab0d 100644
--- 
a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/MethodsHandler.kt
+++ 
b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/MethodsHandler.kt
@@ -208,3 +208,22 @@ class CachingPollingHandler(
     }
   }
 }
+
+class ConstantStringResult(val result: String) {
+  suspend fun handle(request: JSONRPCRequest): JSONRPCResponse {
+    return JSONRPCResponse(id = request.id, result = result)
+  }
+}
+
+class ConstantBooleanResult(val result: Boolean) {
+  suspend fun handle(request: JSONRPCRequest): JSONRPCResponse {
+    return JSONRPCResponse(id = request.id, result = result)
+  }
+}
+
+class FunctionCallResult(val result: () -> Any?) {
+
+  suspend fun handle(request: JSONRPCRequest): JSONRPCResponse {
+    return JSONRPCResponse(id = request.id, result = result())
+  }
+}
diff --git a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/Web3.kt 
b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/Net.kt
similarity index 60%
copy from jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/Web3.kt
copy to jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/Net.kt
index 218bbe7..9061f6e 100644
--- a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/Web3.kt
+++ b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/Net.kt
@@ -17,25 +17,24 @@
 package org.apache.tuweni.jsonrpc.methods
 
 import org.apache.tuweni.bytes.Bytes
-import org.apache.tuweni.eth.Hash
 import org.apache.tuweni.eth.JSONRPCRequest
 import org.apache.tuweni.eth.JSONRPCResponse
-import org.apache.tuweni.eth.invalidParams
 
-suspend fun sha3(request: JSONRPCRequest): JSONRPCResponse {
-  if (request.params.size != 1) {
-    return invalidParams.copy(id = request.id)
-  }
-  try {
-    val input = Bytes.fromHexString(request.params[0])
-    return JSONRPCResponse(id = request.id, result = 
Hash.hash(input).toHexString())
-  } catch (e: IllegalArgumentException) {
-    return invalidParams.copy(id = request.id)
-  }
-}
-
-class ClientVersion(val clientId: String) {
-  suspend fun handle(request: JSONRPCRequest): JSONRPCResponse {
-    return JSONRPCResponse(id = request.id, result = clientId)
-  }
+fun registerNet(
+  networkId: String,
+  listeningToConnections: Boolean = true,
+  peerCount: () -> Int,
+): Map<String, suspend (JSONRPCRequest) -> JSONRPCResponse> {
+  val version = ConstantStringResult(networkId)
+  val listening = ConstantBooleanResult(listeningToConnections)
+  return mapOf(
+    Pair("net_version", version::handle),
+    Pair("net_listening", listening::handle),
+    Pair(
+      "net_peerCount",
+      FunctionCallResult {
+        Bytes.ofUnsignedInt(peerCount().toLong()).toQuantityHexString()
+      }::handle
+    )
+  )
 }
diff --git a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/Web3.kt 
b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/Web3.kt
index 218bbe7..aa75ec7 100644
--- a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/Web3.kt
+++ b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/Web3.kt
@@ -22,6 +22,11 @@ import org.apache.tuweni.eth.JSONRPCRequest
 import org.apache.tuweni.eth.JSONRPCResponse
 import org.apache.tuweni.eth.invalidParams
 
+fun registerWeb3(clientVersion: String): Map<String, suspend (JSONRPCRequest) 
-> JSONRPCResponse> {
+  val version = ConstantStringResult(clientVersion)
+  return mapOf(Pair("web3_sha3", ::sha3), Pair("web3_clientVersion", 
version::handle))
+}
+
 suspend fun sha3(request: JSONRPCRequest): JSONRPCResponse {
   if (request.params.size != 1) {
     return invalidParams.copy(id = request.id)
@@ -33,9 +38,3 @@ suspend fun sha3(request: JSONRPCRequest): JSONRPCResponse {
     return invalidParams.copy(id = request.id)
   }
 }
-
-class ClientVersion(val clientId: String) {
-  suspend fun handle(request: JSONRPCRequest): JSONRPCResponse {
-    return JSONRPCResponse(id = request.id, result = clientId)
-  }
-}
diff --git a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/Web3.kt 
b/jsonrpc/src/test/kotlin/org/apache/tuweni/jsonrpc/methods/NetTest.kt
similarity index 55%
copy from jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/Web3.kt
copy to jsonrpc/src/test/kotlin/org/apache/tuweni/jsonrpc/methods/NetTest.kt
index 218bbe7..b9421ad 100644
--- a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/Web3.kt
+++ b/jsonrpc/src/test/kotlin/org/apache/tuweni/jsonrpc/methods/NetTest.kt
@@ -16,26 +16,18 @@
  */
 package org.apache.tuweni.jsonrpc.methods
 
-import org.apache.tuweni.bytes.Bytes
-import org.apache.tuweni.eth.Hash
+import kotlinx.coroutines.runBlocking
 import org.apache.tuweni.eth.JSONRPCRequest
-import org.apache.tuweni.eth.JSONRPCResponse
-import org.apache.tuweni.eth.invalidParams
+import org.junit.jupiter.api.Assertions.assertEquals
+import org.junit.jupiter.api.Test
 
-suspend fun sha3(request: JSONRPCRequest): JSONRPCResponse {
-  if (request.params.size != 1) {
-    return invalidParams.copy(id = request.id)
-  }
-  try {
-    val input = Bytes.fromHexString(request.params[0])
-    return JSONRPCResponse(id = request.id, result = 
Hash.hash(input).toHexString())
-  } catch (e: IllegalArgumentException) {
-    return invalidParams.copy(id = request.id)
-  }
-}
+class NetTest {
 
-class ClientVersion(val clientId: String) {
-  suspend fun handle(request: JSONRPCRequest): JSONRPCResponse {
-    return JSONRPCResponse(id = request.id, result = clientId)
+  @Test
+  fun testNetCheck() = runBlocking {
+    val net = registerNet("2", true, { 2 })
+    assertEquals("2", net["net_version"]?.invoke(JSONRPCRequest(1, "", 
arrayOf()))?.result)
+    assertEquals(true, net["net_listening"]?.invoke(JSONRPCRequest(1, "", 
arrayOf()))?.result)
+    assertEquals("0x2", net["net_peerCount"]?.invoke(JSONRPCRequest(1, "", 
arrayOf()))?.result)
   }
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to