Search in sources :

Example 11 with Request

use of org.web3j.protocol.core.Request in project web3j by web3j.

the class IpcServiceTest method testSend.

@Test
public void testSend() throws IOException {
    when(ioFacade.read()).thenReturn("{\"jsonrpc\":\"2.0\",\"id\":1," + "\"result\":\"Geth/v1.5.4-stable-b70acf3c/darwin/go1.7.3\"}\n");
    ipcService.send(new Request(), Web3ClientVersion.class);
    verify(ioFacade).write("{\"jsonrpc\":\"2.0\",\"method\":null,\"params\":null,\"id\":0}");
}
Also used : Request(org.web3j.protocol.core.Request) Test(org.junit.jupiter.api.Test)

Example 12 with Request

use of org.web3j.protocol.core.Request in project web3j by web3j.

the class UnixDomainSocketTest method testSlowResponse.

@Test
public void testSlowResponse() throws Exception {
    String response = "{\"jsonrpc\":\"2.0\",\"id\":1," + "\"result\":\"Geth/v1.5.4-stable-b70acf3c/darwin/go1.7.3\"}\n";
    unixDomainSocket = new UnixDomainSocket(reader, writer, response.length());
    final LinkedList<String> segments = new LinkedList<>();
    // 1st part of response
    segments.add(response.substring(0, 50));
    // rest of response
    segments.add(response.substring(50));
    doAnswer(invocation -> {
        String segment = segments.poll();
        if (segment == null) {
            return 0;
        } else {
            Object[] args = invocation.getArguments();
            ((CharBuffer) args[0]).append(segment);
            return segment.length();
        }
    }).when(reader).read(any(CharBuffer.class));
    IpcService ipcService = new IpcService() {

        @Override
        protected IOFacade getIO() {
            return unixDomainSocket;
        }
    };
    ipcService.send(new Request(), Web3ClientVersion.class);
}
Also used : CharBuffer(java.nio.CharBuffer) Request(org.web3j.protocol.core.Request) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 13 with Request

use of org.web3j.protocol.core.Request in project web3j by web3j.

the class WebSocketServiceTest method testBatchRequestReply.

@Test
public void testBatchRequestReply() throws Exception {
    BatchRequest request = new BatchRequest(service);
    request.add(new Request<>("web3_clientVersion", Collections.<String>emptyList(), service, Web3ClientVersion.class)).add(new Request<>("net_version", Collections.<String>emptyList(), service, NetVersion.class));
    request.getRequests().get(0).setId(1L);
    request.getRequests().get(1).setId(1L);
    CompletableFuture<BatchResponse> reply = service.sendBatchAsync(request);
    verify(webSocketClient).send("[" + "{\"jsonrpc\":\"2.0\",\"method\":\"web3_clientVersion\",\"params\":[],\"id\":0}," + "{\"jsonrpc\":\"2.0\",\"method\":\"net_version\",\"params\":[],\"id\":1}" + "]");
    sendClientNetVersionReply();
    assertTrue(reply.isDone());
    BatchResponse response = reply.get();
    assertEquals(response.getResponses().size(), 2);
    assertTrue(response.getResponses().get(0) instanceof Web3ClientVersion);
    Web3ClientVersion web3ClientVersion = (Web3ClientVersion) response.getResponses().get(0);
    assertEquals(web3ClientVersion.getWeb3ClientVersion(), "Mist/v0.9.3/darwin/go1.4.1");
    assertTrue(response.getResponses().get(1) instanceof NetVersion);
    NetVersion netVersion = (NetVersion) response.getResponses().get(1);
    assertEquals(netVersion.getNetVersion(), "59");
}
Also used : BatchRequest(org.web3j.protocol.core.BatchRequest) BatchResponse(org.web3j.protocol.core.BatchResponse) Request(org.web3j.protocol.core.Request) BatchRequest(org.web3j.protocol.core.BatchRequest) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Web3ClientVersion(org.web3j.protocol.core.methods.response.Web3ClientVersion) NetVersion(org.web3j.protocol.core.methods.response.NetVersion) Test(org.junit.jupiter.api.Test)

Example 14 with Request

use of org.web3j.protocol.core.Request in project web3j by web3j.

the class ClientTransactionManagerTest method sendCallErrorRevertByDataNull.

@Test
void sendCallErrorRevertByDataNull() throws IOException {
    EthCall lookupDataHex = new EthCall();
    Response.Error error = new Response.Error();
    error.setCode(3);
    error.setData(null);
    lookupDataHex.setError(error);
    Request request = mock(Request.class);
    when(request.send()).thenReturn(lookupDataHex);
    when(web3j.ethCall(any(Transaction.class), any(DefaultBlockParameter.class))).thenReturn(request);
    assertThrows(ContractCallException.class, () -> clientTransactionManager.sendCall("0xAdress", "data", DefaultBlockParameter.valueOf("latest")));
}
Also used : Response(org.web3j.protocol.core.Response) DefaultBlockParameter(org.web3j.protocol.core.DefaultBlockParameter) Transaction(org.web3j.protocol.core.methods.request.Transaction) Request(org.web3j.protocol.core.Request) EthCall(org.web3j.protocol.core.methods.response.EthCall) Test(org.junit.jupiter.api.Test)

Example 15 with Request

use of org.web3j.protocol.core.Request in project web3j by web3j.

the class ClientTransactionManagerTest method sendCallErrorResponseNotRevert.

/**
 * According to EIP 3668, error is a valid behavior in a new functionality.
 */
@Test
void sendCallErrorResponseNotRevert() throws IOException {
    EthCall lookupDataHex = new EthCall();
    Response.Error error = new Response.Error();
    error.setCode(3);
    error.setMessage("execution reverted");
    error.setData(responseData);
    lookupDataHex.setError(error);
    Request request = mock(Request.class);
    when(request.send()).thenReturn(lookupDataHex);
    when(web3j.ethCall(any(Transaction.class), any(DefaultBlockParameter.class))).thenReturn(request);
    String result = clientTransactionManager.sendCall("0xAdress", "data", DefaultBlockParameter.valueOf("latest"));
    assertEquals(responseData, result);
}
Also used : Response(org.web3j.protocol.core.Response) DefaultBlockParameter(org.web3j.protocol.core.DefaultBlockParameter) Transaction(org.web3j.protocol.core.methods.request.Transaction) Request(org.web3j.protocol.core.Request) EthCall(org.web3j.protocol.core.methods.response.EthCall) Test(org.junit.jupiter.api.Test)

Aggregations

Request (org.web3j.protocol.core.Request)25 Test (org.junit.jupiter.api.Test)8 Response (org.web3j.protocol.core.Response)8 BigInteger (java.math.BigInteger)7 Collectors (java.util.stream.Collectors)7 Transaction (org.web3j.protocol.core.methods.request.Transaction)7 QuorumNetworkProperty (com.quorum.gauge.common.QuorumNetworkProperty)6 List (java.util.List)6 Service (org.springframework.stereotype.Service)6 DefaultBlockParameter (org.web3j.protocol.core.DefaultBlockParameter)6 PrivacyFlag (com.quorum.gauge.common.PrivacyFlag)5 QuorumNode (com.quorum.gauge.common.QuorumNode)5 Observable (io.reactivex.Observable)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 EthCall (org.web3j.protocol.core.methods.response.EthCall)5 Node (com.quorum.gauge.common.QuorumNetworkProperty.Node)3 EnhancedPrivateTransaction (com.quorum.gauge.ext.EnhancedPrivateTransaction)3 com.quorum.gauge.ext.contractextension (com.quorum.gauge.ext.contractextension)3 IOException (java.io.IOException)3 Collections.emptyList (java.util.Collections.emptyList)3