Search in sources :

Example 21 with Request

use of org.web3j.protocol.core.Request in project quorum-acceptance-tests by ConsenSys.

the class ContractService method createClientReceiptContractAsync.

public Observable<EthSendTransactionAsync> createClientReceiptContractAsync(int initialValue, QuorumNode source, String sourceAccount, QuorumNode target, String callbackUrl) {
    InputStream binaryStream = ClientReceipt.class.getResourceAsStream("/com.quorum.gauge.sol/ClientReceipt.bin");
    if (binaryStream == null) {
        throw new IllegalStateException("Can't find resource ClientReceipt.bin");
    }
    return (sourceAccount != null ? Observable.just(sourceAccount) : accountService.getDefaultAccountAddress(source)).flatMap(fromAddress -> {
        try {
            String binary = StreamUtils.copyToString(binaryStream, Charset.defaultCharset());
            PrivateTransactionAsync tx = new PrivateTransactionAsync(fromAddress, null, DEFAULT_GAS_LIMIT, null, BigInteger.valueOf(0), binary, null, Arrays.asList(privacyService.id(target)), callbackUrl);
            Request<?, EthSendTransactionAsync> request = new Request<>("eth_sendTransactionAsync", Arrays.asList(tx), connectionFactory().getWeb3jService(source), EthSendTransactionAsync.class);
            return request.flowable().toObservable();
        } catch (IOException e) {
            logger.error("Unable to construct transaction arguments", e);
            throw new RuntimeException(e);
        }
    });
}
Also used : PrivateTransactionAsync(com.quorum.gauge.ext.PrivateTransactionAsync) EthSendTransactionAsync(com.quorum.gauge.ext.EthSendTransactionAsync) InputStream(java.io.InputStream) Request(org.web3j.protocol.core.Request) IOException(java.io.IOException)

Example 22 with Request

use of org.web3j.protocol.core.Request in project quorum-acceptance-tests by ConsenSys.

the class PluginSecurity method invokeMultiple.

@Step("`<clientId>` is responded with <policy> when trying to: <table>")
public void invokeMultiple(String clientId, String policy, Table table) {
    boolean expectAuthorized = "success".equalsIgnoreCase(policy);
    String token = mustHaveValue(DataStoreFactory.getScenarioDataStore(), clientId, String.class);
    Context.storeAccessToken(token);
    Map<String, QuorumNetworkProperty.Node> nodeMap = networkProperty.getNodes();
    table.getTableRows().stream().map(r -> new ApiCall(r.getCell("callApi"), r.getCell("targetNode"))).onClose(Context::removeAccessToken).forEach(a -> {
        if (a.isBatch) {
            rpcService.call(nodeMap.get(a.node), a.collector).blockingForEach(batchResponse -> {
                assertThat(batchResponse.getResponses()).as("Number of returned responses").hasSize(a.collector.size());
                for (ObjectResponse res : batchResponse.getResponses()) {
                    assertThat(res.getId()).as("Response must have id").isNotZero();
                    Request r = a.collector.getByID(res.getId());
                    String description = policy + ": " + r.getMethod() + "@" + a.node;
                    if (expectAuthorized) {
                        assertThat(Optional.ofNullable(res.getError()).orElse(new Response.Error()).getMessage()).as(description).isNullOrEmpty();
                    }
                    assertThat(res.hasError()).as(description).isNotEqualTo(expectAuthorized);
                    if (res.hasError()) {
                        assertThat(res.getError().getMessage()).as(description).endsWith(policy);
                    }
                }
            });
        } else {
            rpcService.call(nodeMap.get(a.node), a.name, Collections.emptyList(), ObjectResponse.class).blockingForEach(res -> {
                assertThat(res.getId()).as("Response must have ID").isNotZero();
                String description = policy + ": " + a.name + "@" + a.node;
                if (expectAuthorized) {
                    assertThat(Optional.ofNullable(res.getError()).orElse(new Response.Error()).getMessage()).as(description).isNullOrEmpty();
                }
                assertThat(res.hasError()).as(description).isNotEqualTo(expectAuthorized);
                if (res.hasError()) {
                    assertThat(res.getError().getMessage()).as(description).endsWith(policy);
                }
            });
        }
    });
}
Also used : ObjectResponse(com.quorum.gauge.ext.ObjectResponse) Response(org.web3j.protocol.core.Response) QuorumNode(com.quorum.gauge.common.QuorumNode) BatchRequest(com.quorum.gauge.ext.BatchRequest) Request(org.web3j.protocol.core.Request) ObjectResponse(com.quorum.gauge.ext.ObjectResponse) Step(com.thoughtworks.gauge.Step)

Example 23 with Request

use of org.web3j.protocol.core.Request in project quorum-acceptance-tests by ConsenSys.

the class RaftService method getLeaderWithLocalEnodeInfo.

/**
 * Retrieve the enode for the raft leader and compare it with the enode of
 * all the peers to convert it into a QuorumNode identity.
 */
public QuorumNode getLeaderWithLocalEnodeInfo(QuorumNode node) {
    Request<String, RaftLeader> request = new Request<>("raft_leader", null, connectionFactory().getWeb3jService(node), RaftLeader.class);
    RaftLeader response = request.flowable().toObservable().blockingFirst();
    String leaderEnode = response.getResult();
    logger.debug("Retrieved leader enode: {}", leaderEnode);
    Map<String, Node> nodes = connectionFactory().getNetworkProperty().getNodes();
    for (Map.Entry<String, Node> nodeEntry : nodes.entrySet()) {
        String thisEnode = nodeEntry.getValue().getEnodeUrl();
        logger.debug("Retrieved enode info: {}", thisEnode);
        if (thisEnode.contains(leaderEnode)) {
            return connectionFactory().getNetworkProperty().getQuorumNode(nodeEntry.getValue());
        }
    }
    throw new RuntimeException("Leader enode not found in peers: " + leaderEnode);
}
Also used : Node(com.quorum.gauge.common.QuorumNetworkProperty.Node) QuorumNode(com.quorum.gauge.common.QuorumNode) Request(org.web3j.protocol.core.Request) RaftLeader(com.quorum.gauge.ext.RaftLeader) Map(java.util.Map)

Example 24 with Request

use of org.web3j.protocol.core.Request in project quorum-acceptance-tests by ConsenSys.

the class RaftService method getLeader.

/**
 * Retrieve the enode for the raft leader and compare it with the enode of
 * all the peers to convert it into a QuorumNode identity.
 */
public QuorumNode getLeader(QuorumNode node) {
    Request<String, RaftLeader> request = new Request<>("raft_leader", null, connectionFactory().getWeb3jService(node), RaftLeader.class);
    RaftLeader response = request.flowable().toObservable().blockingFirst();
    String leaderEnode = response.getResult();
    logger.debug("Retrieved leader enode: {}", leaderEnode);
    Map<String, Node> nodes = connectionFactory().getNetworkProperty().getNodes();
    for (Node n : nodes.values()) {
        Request<?, NodeInfo> nodeInfoRequest = new Request<>("admin_nodeInfo", null, connectionFactory().getWeb3jService(n), NodeInfo.class);
        NodeInfo nodeInfo = nodeInfoRequest.flowable().toObservable().blockingFirst();
        String thisEnode = nodeInfo.getEnode();
        logger.debug("Retrieved enode info: {}", thisEnode);
        if (thisEnode.contains(leaderEnode)) {
            return connectionFactory().getNetworkProperty().getQuorumNode(n);
        }
    }
    throw new RuntimeException("Leader enode not found in peers: " + leaderEnode);
}
Also used : NodeInfo(com.quorum.gauge.ext.NodeInfo) Node(com.quorum.gauge.common.QuorumNetworkProperty.Node) QuorumNode(com.quorum.gauge.common.QuorumNode) Request(org.web3j.protocol.core.Request) RaftLeader(com.quorum.gauge.ext.RaftLeader)

Example 25 with Request

use of org.web3j.protocol.core.Request in project quorum-acceptance-tests by ConsenSys.

the class TransactionService method sendSignedPrivateTransaction.

public Observable<EthSendTransaction> sendSignedPrivateTransaction(String apiMethod, String txData, QuorumNode from, QuorumNode privateFor, String targetContract) {
    Quorum quorumClient = connectionFactory().getConnection(from);
    // sleep to allow time for previous tx to be minted so that nonce is updated
    try {
        TimeUnit.MILLISECONDS.sleep(100);
    } catch (InterruptedException e) {
        logger.error("sleep interrupted", e);
    }
    String fromAddress = accountService.getDefaultAccountAddress(from).blockingFirst();
    BigInteger transactionCount = quorumClient.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.LATEST).flowable().toObservable().blockingFirst().getTransactionCount();
    ExtendedPrivateTransaction tx = new ExtendedPrivateTransaction(fromAddress, transactionCount, BigInteger.ZERO, DEFAULT_GAS_LIMIT, targetContract, null, txData, null, Arrays.asList(privacyService.id(privateFor)));
    List<Object> params = new ArrayList<>(Collections.singletonList(tx));
    if ("personal_signTransaction".equals(apiMethod)) {
        // add empty password
        params.add("");
    }
    Request<?, EthSignTransaction> request = new Request<>(apiMethod, params, connectionFactory().getWeb3jService(from), EthSignTransaction.class);
    Observable<EthSignTransaction> ethSignTransaction = request.flowable().toObservable();
    String rawHexString = ethSignTransaction.blockingFirst().getRaw().orElseThrow();
    logger.debug("rawHexString", rawHexString);
    return quorumClient.ethSendRawPrivateTransaction(rawHexString, Arrays.asList(privacyService.id(privateFor))).flowable().toObservable();
}
Also used : Quorum(org.web3j.quorum.Quorum) Request(org.web3j.protocol.core.Request) BigInteger(java.math.BigInteger)

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