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 <anto...@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-unsubscr...@tuweni.apache.org For additional commands, e-mail: commits-h...@tuweni.apache.org