org.web3j.utils.Async Java Examples

The following examples show how to use org.web3j.utils.Async. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContractTest.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testInvalidTransactionReceipt() throws Throwable {
    prepareNonceRequest();
    prepareTransactionRequest();

    EthGetTransactionReceipt ethGetTransactionReceipt = new EthGetTransactionReceipt();
    ethGetTransactionReceipt.setError(new Response.Error(1, "Invalid transaction receipt"));

    Request<?, EthGetTransactionReceipt> getTransactionReceiptRequest = mock(Request.class);
    when(getTransactionReceiptRequest.sendAsync())
            .thenReturn(Async.run(() -> ethGetTransactionReceipt));
    when(web3j.ethGetTransactionReceipt(TRANSACTION_HASH))
            .thenReturn((Request) getTransactionReceiptRequest);
    assertThrows(RuntimeException.class, this::testErrorScenario);
}
 
Example #2
Source File: ContractTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test(expected = RuntimeException.class)
@SuppressWarnings("unchecked")
public void testInvalidTransactionReceipt() throws Throwable {
    prepareNonceRequest();
    prepareTransactionRequest();

    EthGetTransactionReceipt ethGetTransactionReceipt = new EthGetTransactionReceipt();
    ethGetTransactionReceipt.setError(new Response.Error(1, "Invalid transaction receipt"));

    Request<?, EthGetTransactionReceipt> getTransactionReceiptRequest = mock(Request.class);
    when(getTransactionReceiptRequest.sendAsync())
            .thenReturn(Async.run(() -> ethGetTransactionReceipt));
    when(web3j.ethGetTransactionReceipt(TRANSACTION_HASH))
            .thenReturn((Request) getTransactionReceiptRequest);

    testErrorScenario();
}
 
Example #3
Source File: ContractTest.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test(expected = RuntimeException.class)
@SuppressWarnings("unchecked")
public void testInvalidTransactionReceipt() throws Throwable {
    prepareNonceRequest();
    prepareTransactionRequest();

    PlatonGetTransactionReceipt ethGetTransactionReceipt = new PlatonGetTransactionReceipt();
    ethGetTransactionReceipt.setError(new Response.Error(1, "Invalid transaction receipt"));

    Request<?, PlatonGetTransactionReceipt> getTransactionReceiptRequest = mock(Request.class);
    when(getTransactionReceiptRequest.sendAsync())
            .thenReturn(Async.run(() -> ethGetTransactionReceipt));
    when(web3j.platonGetTransactionReceipt(TRANSACTION_HASH))
            .thenReturn((Request) getTransactionReceiptRequest);

    testErrorScenario();
}
 
Example #4
Source File: BesuNode.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
@Override
public void start() {
  LOG.info("Starting Besu Docker container: {}", besuContainerId);
  docker.startContainerCmd(besuContainerId).exec();

  LOG.info("Querying for the Docker dynamically allocated RPC port numbers");
  final InspectContainerResponse containerResponse =
      docker.inspectContainerCmd(besuContainerId).exec();
  final Ports ports = containerResponse.getNetworkSettings().getPorts();
  final int httpRpcPort = httpRpcPort(ports);
  final int wsRpcPort = wsRpcPort(ports);
  LOG.info("Http RPC port: {}, Web Socket RPC port: {}", httpRpcPort, wsRpcPort);

  final String httpRpcUrl = url(httpRpcPort);
  LOG.info("Besu Web3j service targeting: {} ", httpRpcUrl);

  final HttpService web3jHttpService = new HttpService(httpRpcUrl);
  this.jsonRpc =
      new JsonRpc2_0Web3j(web3jHttpService, pollingInterval, Async.defaultExecutorService());
  final RawJsonRpcRequestFactory requestFactory = new RawJsonRpcRequestFactory(web3jHttpService);
  final JsonRpc2_0Besu besuJsonRpc = new JsonRpc2_0Besu(web3jHttpService);
  final Eth eth = new Eth(jsonRpc);
  final Besu besu = new Besu(besuJsonRpc);
  final Eea eea = new Eea(requestFactory);
  this.accounts = new Accounts(eth);
  this.publicContracts = new PublicContracts(eth);
  this.privateContracts = new PrivateContracts(besu, eea);
  this.transactions = new Transactions(eth);
  this.ports = new NodePorts(httpRpcPort, wsRpcPort);
}
 
Example #5
Source File: ContractTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testInvalidTransactionResponse() throws IOException {
    prepareNonceRequest();

    EthSendTransaction ethSendTransaction = new EthSendTransaction();
    ethSendTransaction.setError(new Response.Error(1, "Invalid transaction"));

    Request<?, EthSendTransaction> rawTransactionRequest = mock(Request.class);
    when(rawTransactionRequest.sendAsync()).thenReturn(Async.run(() -> ethSendTransaction));
    when(web3j.ethSendRawTransaction(any(String.class)))
            .thenReturn((Request) rawTransactionRequest);
    assertThrows(RuntimeException.class, this::testErrorScenario);
}
 
Example #6
Source File: QueuingTransactionReceiptProcessor.java    From web3j with Apache License 2.0 5 votes vote down vote up
public QueuingTransactionReceiptProcessor(
        Web3j web3j, Callback callback, int pollingAttemptsPerTxHash, long pollingFrequency) {
    super(web3j);
    this.scheduledExecutorService = Async.defaultExecutorService();
    this.callback = callback;
    this.pendingTransactions = new LinkedBlockingQueue<>();
    this.pollingAttemptsPerTxHash = pollingAttemptsPerTxHash;

    scheduledExecutorService.scheduleAtFixedRate(
            this::sendTransactionReceiptRequests,
            pollingFrequency,
            pollingFrequency,
            TimeUnit.MILLISECONDS);
}
 
Example #7
Source File: ContractTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = RuntimeException.class)
@SuppressWarnings("unchecked")
public void testInvalidTransactionResponse() throws Throwable {
    prepareNonceRequest();

    EthSendTransaction ethSendTransaction = new EthSendTransaction();
    ethSendTransaction.setError(new Response.Error(1, "Invalid transaction"));

    Request<?, EthSendTransaction> rawTransactionRequest = mock(Request.class);
    when(rawTransactionRequest.sendAsync()).thenReturn(Async.run(() -> ethSendTransaction));
    when(web3j.ethSendRawTransaction(any(String.class)))
            .thenReturn((Request) rawTransactionRequest);

    testErrorScenario();
}
 
Example #8
Source File: QueuingTransactionReceiptProcessor.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
public QueuingTransactionReceiptProcessor(
        Web3j web3j, Callback callback,
        int pollingAttemptsPerTxHash, long pollingFrequency) {
    super(web3j);
    this.scheduledExecutorService = Async.defaultExecutorService();
    this.callback = callback;
    this.pendingTransactions = new LinkedBlockingQueue<>();
    this.pollingAttemptsPerTxHash = pollingAttemptsPerTxHash;

    scheduledExecutorService.scheduleAtFixedRate(
            this::sendTransactionReceiptRequests,
            pollingFrequency, pollingFrequency, TimeUnit.MILLISECONDS);
}
 
Example #9
Source File: Signer.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
public void start() {
  LOG.info("Starting EthSigner");
  runner.start(PROCESS_NAME);

  final String httpJsonRpcUrl = getUrl();

  LOG.info("Http requests being submitted to : {} ", httpJsonRpcUrl);

  final OkHttpClient httpClient = OkHttpClientHelpers.createOkHttpClient(clientTlsConfig);

  final HttpService web3jHttpService = new HttpService(httpJsonRpcUrl, httpClient);
  this.jsonRpc =
      new JsonRpc2_0Web3j(
          web3jHttpService, pollingInterval.toMillis(), Async.defaultExecutorService());
  final JsonRpc2_0Besu besuJsonRpc = new JsonRpc2_0Besu(web3jHttpService);

  final Eth eth = new Eth(jsonRpc);
  final RawJsonRpcRequestFactory requestFactory = new RawJsonRpcRequestFactory(web3jHttpService);
  this.transactions = new Transactions(eth);
  final Besu besu = new Besu(besuJsonRpc);
  final Eea eea = new Eea(requestFactory);
  this.publicContracts = new PublicContracts(eth);
  this.privateContracts = new PrivateContracts(besu, eea);
  this.accounts = new Accounts(eth);
  this.rawJsonRpcRequests = new RawJsonRpcRequests(web3jHttpService, requestFactory);
  this.rawHttpRequests = new HttpRequest(httpJsonRpcUrl, httpClient);
}
 
Example #10
Source File: ContractTest.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test(expected = RuntimeException.class)
@SuppressWarnings("unchecked")
public void testInvalidTransactionResponse() throws Throwable {
    prepareNonceRequest();

    PlatonSendTransaction ethSendTransaction = new PlatonSendTransaction();
    ethSendTransaction.setError(new Response.Error(1, "Invalid transaction"));

    Request<?, PlatonSendTransaction> rawTransactionRequest = mock(Request.class);
    when(rawTransactionRequest.sendAsync()).thenReturn(Async.run(() -> ethSendTransaction));
    when(web3j.platonSendRawTransaction(any(String.class)))
            .thenReturn((Request) rawTransactionRequest);

    testErrorScenario();
}
 
Example #11
Source File: QueuingTransactionReceiptProcessor.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
public QueuingTransactionReceiptProcessor(
        Web3j web3j, Callback callback,
        int pollingAttemptsPerTxHash, long pollingFrequency) {
    super(web3j);
    this.scheduledExecutorService = Async.defaultExecutorService();
    this.callback = callback;
    this.pendingTransactions = new LinkedBlockingQueue<>();
    this.pollingAttemptsPerTxHash = pollingAttemptsPerTxHash;

    scheduledExecutorService.scheduleAtFixedRate(
            this::sendTransactionReceiptRequests,
            pollingFrequency, pollingFrequency, TimeUnit.MILLISECONDS);
}
 
Example #12
Source File: AsyncTest.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
@Test(expected = ExecutionException.class)
public void testRunException() throws Exception {
    Async.run(() -> {
        throw new RuntimeException("");
    }).get();
}
 
Example #13
Source File: JsonRpc2_0Web3j.java    From web3j with Apache License 2.0 4 votes vote down vote up
public JsonRpc2_0Web3j(Web3jService web3jService) {
    this(web3jService, DEFAULT_BLOCK_TIME, Async.defaultExecutorService());
}
 
Example #14
Source File: Service.java    From web3j with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<BatchResponse> sendBatchAsync(BatchRequest batchRequest) {
    return Async.run(() -> sendBatch(batchRequest));
}
 
Example #15
Source File: Service.java    From web3j with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends Response> CompletableFuture<T> sendAsync(
        Request jsonRpc20Request, Class<T> responseType) {
    return Async.run(() -> send(jsonRpc20Request, responseType));
}
 
Example #16
Source File: Service.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends Response> CompletableFuture<T> sendAsync(
        Request jsonRpc20Request, Class<T> responseType) {
    return Async.run(() -> send(jsonRpc20Request, responseType));
}
 
Example #17
Source File: JsonRpc2_0Besu.java    From web3j with Apache License 2.0 4 votes vote down vote up
public JsonRpc2_0Besu(final Web3jService web3jService) {
    this(web3jService, DEFAULT_BLOCK_TIME, Async.defaultExecutorService());
}
 
Example #18
Source File: AsyncTest.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testRun() throws Exception {
    assertThat(Async.run(() -> "").get(), is(""));
}
 
Example #19
Source File: AsyncTest.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testRun() throws Exception {
    assertThat(Async.run(() -> "").get(), is(""));
}
 
Example #20
Source File: JsonRpc2_0Web3j.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
public JsonRpc2_0Web3j(Web3jService web3jService) {
    this(web3jService, DEFAULT_BLOCK_TIME, Async.defaultExecutorService());
}
 
Example #21
Source File: JsonRpc2_0Web3j.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
public JsonRpc2_0Web3j(Web3jService web3jService) {
    this(web3jService, DEFAULT_BLOCK_TIME, Async.defaultExecutorService());
}
 
Example #22
Source File: Service.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
@Override
public <T extends Response> CompletableFuture<T> sendAsync(
        Request jsonRpc20Request, Class<T> responseType) {
    return Async.run(() -> send(jsonRpc20Request, responseType));
}
 
Example #23
Source File: AsyncTest.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
@Test(expected = ExecutionException.class)
public void testRunException() throws Exception {
    Async.run(() -> {
        throw new RuntimeException("");
    }).get();
}
 
Example #24
Source File: NodeBeanRegistrationStrategy.java    From eventeum with Apache License 2.0 2 votes vote down vote up
private Web3j buildWeb3j(Node node, Web3jService web3jService) {

        return Web3j.build(web3jService, node.getPollingInterval(), Async.defaultExecutorService());
    }
 
Example #25
Source File: RemoteCall.java    From client-sdk-java with Apache License 2.0 2 votes vote down vote up
/**
 * Perform request asynchronously with a future.
 *
 * @return a future containing our function
 */
public CompletableFuture<T> sendAsync() {
    return Async.run(this::send);
}
 
Example #26
Source File: RemoteCall.java    From web3j with Apache License 2.0 2 votes vote down vote up
/**
 * Perform request asynchronously with a future.
 *
 * @return a future containing our function
 */
public CompletableFuture<T> sendAsync() {
    return Async.run(this::send);
}
 
Example #27
Source File: RemoteCall.java    From etherscan-explorer with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Perform request asynchronously with a future.
 *
 * @return a future containing our function
 */
public CompletableFuture<T> sendAsync() {
    return Async.run(this::send);
}