Search in sources :

Example 1 with Web3

use of org.ethereum.rpc.Web3 in project rskj by rsksmart.

the class Web3HttpServerTest method smokeTest.

private void smokeTest(String contentType, String host, InetAddress rpcAddress, List<String> rpcHost, boolean excludeJsonRequestId) throws Exception {
    Web3 web3Mock = Mockito.mock(Web3.class);
    String mockResult = "output";
    Mockito.when(web3Mock.web3_sha3(Mockito.anyString())).thenReturn(mockResult);
    CorsConfiguration mockCorsConfiguration = Mockito.mock(CorsConfiguration.class);
    Mockito.when(mockCorsConfiguration.hasHeader()).thenReturn(true);
    Mockito.when(mockCorsConfiguration.getHeader()).thenReturn("*");
    // new ServerSocket(0).getLocalPort();
    int randomPort = 9999;
    List<ModuleDescription> filteredModules = Collections.singletonList(new ModuleDescription("web3", "1.0", true, Collections.emptyList(), Collections.emptyList()));
    JsonRpcWeb3FilterHandler filterHandler = new JsonRpcWeb3FilterHandler("*", rpcAddress, rpcHost);
    JsonRpcWeb3ServerHandler serverHandler = new JsonRpcWeb3ServerHandler(web3Mock, filteredModules);
    Web3HttpServer server = new Web3HttpServer(InetAddress.getLoopbackAddress(), randomPort, 0, Boolean.TRUE, mockCorsConfiguration, filterHandler, serverHandler);
    server.start();
    try {
        Response response = sendJsonRpcMessage(randomPort, contentType, host, excludeJsonRequestId);
        String strResponse = response.body().string();
        JsonNode jsonRpcResponse = OBJECT_MAPPER.readTree(strResponse);
        assertThat(response.code(), is(HttpResponseStatus.OK.code()));
        if (excludeJsonRequestId) {
            assertEquals(jsonRpcResponse.get("error").get("code").asLong(), -32700L);
            assertEquals(jsonRpcResponse.get("error").get("message").asText(), "missing request id");
        } else {
            assertThat(jsonRpcResponse.at("/result").asText(), is(mockResult));
        }
    } finally {
        server.stop();
    }
}
Also used : CorsConfiguration(co.rsk.rpc.CorsConfiguration) ModuleDescription(co.rsk.rpc.ModuleDescription) Web3(org.ethereum.rpc.Web3) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 2 with Web3

use of org.ethereum.rpc.Web3 in project rskj by rsksmart.

the class Web3HttpServerTest method smokeTest.

private void smokeTest(String contentType, String host, InetAddress rpcAddress, List<String> rpcHost) throws Exception {
    Web3 web3Mock = Mockito.mock(Web3.class);
    String mockResult = "output";
    Mockito.when(web3Mock.web3_sha3(Mockito.anyString())).thenReturn(mockResult);
    CorsConfiguration mockCorsConfiguration = Mockito.mock(CorsConfiguration.class);
    Mockito.when(mockCorsConfiguration.hasHeader()).thenReturn(true);
    Mockito.when(mockCorsConfiguration.getHeader()).thenReturn("*");
    // new ServerSocket(0).getLocalPort();
    int randomPort = 9999;
    List<ModuleDescription> filteredModules = Collections.singletonList(new ModuleDescription("web3", "1.0", true, Collections.emptyList(), Collections.emptyList()));
    JsonRpcWeb3FilterHandler filterHandler = new JsonRpcWeb3FilterHandler("*", rpcAddress, rpcHost);
    JsonRpcWeb3ServerHandler serverHandler = new JsonRpcWeb3ServerHandler(web3Mock, filteredModules);
    Web3HttpServer server = new Web3HttpServer(InetAddress.getLoopbackAddress(), randomPort, 0, Boolean.TRUE, mockCorsConfiguration, filterHandler, serverHandler);
    server.start();
    try {
        Response response = sendJsonRpcMessage(randomPort, contentType, host);
        JsonNode jsonRpcResponse = OBJECT_MAPPER.readTree(response.body().string());
        assertThat(response.code(), is(HttpResponseStatus.OK.code()));
        assertThat(jsonRpcResponse.at("/result").asText(), is(mockResult));
    } finally {
        server.stop();
    }
}
Also used : CorsConfiguration(co.rsk.rpc.CorsConfiguration) ModuleDescription(co.rsk.rpc.ModuleDescription) Web3(org.ethereum.rpc.Web3) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 3 with Web3

use of org.ethereum.rpc.Web3 in project rskj by rsksmart.

the class Web3WebSocketServerTest method smokeTest.

private void smokeTest(byte[] msg) throws Exception {
    Web3 web3Mock = mock(Web3.class);
    String mockResult = "output";
    when(web3Mock.web3_sha3(anyString())).thenReturn(mockResult);
    int randomPort = 9998;
    TestSystemProperties testSystemProperties = new TestSystemProperties();
    List<ModuleDescription> filteredModules = Collections.singletonList(new ModuleDescription("web3", "1.0", true, Collections.emptyList(), Collections.emptyList()));
    RskWebSocketJsonRpcHandler handler = new RskWebSocketJsonRpcHandler(null);
    JsonRpcWeb3ServerHandler serverHandler = new JsonRpcWeb3ServerHandler(web3Mock, filteredModules);
    int serverWriteTimeoutSeconds = testSystemProperties.rpcWebSocketServerWriteTimeoutSeconds();
    int maxFrameSize = testSystemProperties.rpcWebSocketMaxFrameSize();
    int maxAggregatedFrameSize = testSystemProperties.rpcWebSocketMaxAggregatedFrameSize();
    assertEquals(DEFAULT_WRITE_TIMEOUT_SECONDS, serverWriteTimeoutSeconds);
    assertEquals(DEFAULT_MAX_FRAME_SIZE, maxFrameSize);
    assertEquals(DEFAULT_MAX_AGGREGATED_FRAME_SIZE, maxAggregatedFrameSize);
    Web3WebSocketServer websocketServer = new Web3WebSocketServer(InetAddress.getLoopbackAddress(), randomPort, handler, serverHandler, serverWriteTimeoutSeconds, maxFrameSize, maxAggregatedFrameSize);
    websocketServer.start();
    OkHttpClient wsClient = new OkHttpClient();
    Request wsRequest = new Request.Builder().url("ws://localhost:" + randomPort + "/websocket").build();
    WebSocketCall wsCall = WebSocketCall.create(wsClient, wsRequest);
    CountDownLatch wsAsyncResultLatch = new CountDownLatch(1);
    CountDownLatch wsAsyncCloseLatch = new CountDownLatch(1);
    AtomicReference<Exception> failureReference = new AtomicReference<>();
    wsCall.enqueue(new WebSocketListener() {

        private WebSocket webSocket;

        @Override
        public void onOpen(WebSocket webSocket, Response response) {
            wsExecutor.submit(() -> {
                RequestBody body = RequestBody.create(WebSocket.TEXT, msg);
                try {
                    this.webSocket = webSocket;
                    this.webSocket.sendMessage(body);
                    this.webSocket.close(1000, null);
                } catch (IOException e) {
                    failureReference.set(e);
                }
            });
        }

        @Override
        public void onFailure(IOException e, Response response) {
            failureReference.set(e);
        }

        @Override
        public void onMessage(ResponseBody message) throws IOException {
            JsonNode jsonRpcResponse = OBJECT_MAPPER.readTree(message.bytes());
            assertThat(jsonRpcResponse.at("/result").asText(), is(mockResult));
            message.close();
            wsAsyncResultLatch.countDown();
        }

        @Override
        public void onPong(Buffer payload) {
        }

        @Override
        public void onClose(int code, String reason) {
            wsAsyncCloseLatch.countDown();
        }
    });
    if (!wsAsyncResultLatch.await(10, TimeUnit.SECONDS)) {
        fail("Result timed out");
    }
    if (!wsAsyncCloseLatch.await(10, TimeUnit.SECONDS)) {
        fail("Close timed out");
    }
    websocketServer.stop();
    Exception failure = failureReference.get();
    if (failure != null) {
        failure.printStackTrace();
        fail(failure.getMessage());
    }
}
Also used : WebSocketListener(com.squareup.okhttp.ws.WebSocketListener) OkHttpClient(com.squareup.okhttp.OkHttpClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) WebSocketCall(com.squareup.okhttp.ws.WebSocketCall) Web3(org.ethereum.rpc.Web3) RequestBody(com.squareup.okhttp.RequestBody) Buffer(okio.Buffer) Request(com.squareup.okhttp.Request) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WebSocket(com.squareup.okhttp.ws.WebSocket) ResponseBody(com.squareup.okhttp.ResponseBody) Response(com.squareup.okhttp.Response) ModuleDescription(co.rsk.rpc.ModuleDescription) TestSystemProperties(co.rsk.config.TestSystemProperties)

Aggregations

ModuleDescription (co.rsk.rpc.ModuleDescription)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 Web3 (org.ethereum.rpc.Web3)3 CorsConfiguration (co.rsk.rpc.CorsConfiguration)2 TestSystemProperties (co.rsk.config.TestSystemProperties)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 OkHttpClient (com.squareup.okhttp.OkHttpClient)1 Request (com.squareup.okhttp.Request)1 RequestBody (com.squareup.okhttp.RequestBody)1 Response (com.squareup.okhttp.Response)1 ResponseBody (com.squareup.okhttp.ResponseBody)1 WebSocket (com.squareup.okhttp.ws.WebSocket)1 WebSocketCall (com.squareup.okhttp.ws.WebSocketCall)1 WebSocketListener (com.squareup.okhttp.ws.WebSocketListener)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Buffer (okio.Buffer)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1