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/05/27 02:20:56 UTC
[incubator-tuweni] branch jsonrpc_ethgetBlockByNumber created (now 0efc5d9c2)
This is an automated email from the ASF dual-hosted git repository.
toulmean pushed a change to branch jsonrpc_ethgetBlockByNumber
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git
at 0efc5d9c2 Support eth_getBlockByNumber from JSONRPC client
This branch includes the following new commits:
new 0efc5d9c2 Support eth_getBlockByNumber from JSONRPC client
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org
[incubator-tuweni] 01/01: Support eth_getBlockByNumber from JSONRPC client
Posted by to...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
toulmean pushed a commit to branch jsonrpc_ethgetBlockByNumber
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git
commit 0efc5d9c2ea76f6c6dad32a7cdd65adf2ad83412
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Fri May 26 19:20:41 2023 -0700
Support eth_getBlockByNumber from JSONRPC client
---
.../org/apache/tuweni/jsonrpc/JSONRPCClient.kt | 26 ++++++++++++++++++++--
.../org/apache/tuweni/jsonrpc/JSONRPCClientTest.kt | 22 +++++++++++++++---
2 files changed, 43 insertions(+), 5 deletions(-)
diff --git a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt
index 7c9e20581..4c28abfff 100644
--- a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt
+++ b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt
@@ -16,6 +16,7 @@
*/
package org.apache.tuweni.jsonrpc
+import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import io.vertx.core.Vertx
import io.vertx.core.buffer.Buffer
@@ -68,7 +69,7 @@ class JSONRPCClient(
val authorizationHeader = "Basic " + Base64.getEncoder()
.encode((basicAuthenticationUsername + ":" + basicAuthenticationPassword).toByteArray())
- suspend fun sendRequest(request: JSONRPCRequest): Deferred<JSONRPCResponse> {
+ fun sendRequest(request: JSONRPCRequest): Deferred<JSONRPCResponse> {
val deferred = CompletableDeferred<JSONRPCResponse>()
val httpRequest = client.postAbs(endpointUrl)
.putHeader("Content-Type", "application/json")
@@ -81,7 +82,6 @@ class JSONRPCClient(
if (response.failed()) {
deferred.completeExceptionally(response.cause())
} else {
- println(response.result().bodyAsString())
val jsonResponse = mapper.readValue(response.result().bodyAsString(), JSONRPCResponse::class.java)
deferred.complete(jsonResponse)
}
@@ -151,6 +151,28 @@ class JSONRPCClient(
}
}
+ /**
+ * Gets block data by block number
+ * @param blockNumber the block number
+ * @param includeTransactions whether to include transactions detail
+ * @return the whole block information as a JSON document.
+ */
+ suspend fun getBlockByBlockNumber(blockNumber: Int, includeTransactions: Boolean): Map<*, *> {
+ val body = JSONRPCRequest(
+ StringOrLong(nextId()),
+ "eth_getBlockByNumber",
+ arrayOf(blockNumber, includeTransactions)
+ )
+ val jsonResponse = sendRequest(body).await()
+ val err = jsonResponse.error
+ if (err != null) {
+ val errorMessage = "Code ${err.code}: ${err.message}"
+ throw ClientRequestException(errorMessage)
+ } else {
+ return jsonResponse.result as Map<*, *>
+ }
+ }
+
override fun close() {
client.close()
}
diff --git a/jsonrpc/src/test/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClientTest.kt b/jsonrpc/src/test/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClientTest.kt
index 8c54b1e45..6c892e34a 100644
--- a/jsonrpc/src/test/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClientTest.kt
+++ b/jsonrpc/src/test/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClientTest.kt
@@ -50,7 +50,7 @@ import java.util.concurrent.atomic.AtomicReference
class JSONRPCClientTest {
companion object {
- val handler = AtomicReference<Handler<JSONRPCRequest>>()
+ val handler = AtomicReference<(JSONRPCRequest)->JSONRPCResponse>()
var server: JSONRPCServer? = null
@JvmStatic
@@ -64,8 +64,7 @@ class JSONRPCClientTest {
vertx,
port = 0,
methodHandler = {
- handler.get().handle(it)
- JSONRPCResponse(StringOrLong(3), "")
+ handler.get()(it)
},
coroutineContext = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
)
@@ -119,4 +118,21 @@ class JSONRPCClientTest {
}
}
}
+
+ @Test
+ fun testGetBlockByNumber(@VertxInstance vertx: Vertx) {
+ JSONRPCClient(vertx, "http://localhost:" + server!!.port()).use {
+ val sent = CompletableDeferred<Any>()
+ handler.set { request ->
+ sent.complete(request.params.get(0))
+ JSONRPCResponse(request.id, mapOf(Pair("foo", "bar")))
+ }
+
+ runBlocking {
+ val block = it.getBlockByBlockNumber(32, true)
+ assertEquals(block["foo"], "bar")
+ sent.await()
+ }
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org