Search in sources :

Example 1 with Request

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

the class PluginSecurity method invokeMultipleWithExpectation.

@Step("`<clientId>` sends a batch `<apis>` to `<node>` and expect: <table>")
public void invokeMultipleWithExpectation(String clientId, String apis, QuorumNetworkProperty.Node node, Table table) {
    String token = mustHaveValue(DataStoreFactory.getScenarioDataStore(), clientId, String.class);
    Context.storeAccessToken(token);
    BatchRequest.Collector collector = BatchRequest.Collector.create();
    Arrays.stream(apis.split(",")).map(String::trim).forEach(n -> collector.add(n, Collections.emptyList()));
    Map<String, String> expect = table.getTableRows().stream().collect(Collectors.toMap(r -> StringUtils.removeEnd(StringUtils.removeStart(r.getCell("callApi"), "`"), "`"), r -> r.getCell("expectation")));
    rpcService.call(node, collector).blockingForEach(batchResponse -> {
        assertThat(batchResponse.getResponses()).as("Number of returned responses").hasSize(collector.size());
        for (ObjectResponse res : batchResponse.getResponses()) {
            assertThat(res.getId()).as("Response must have id").isNotZero();
            Request r = collector.getByID(res.getId());
            String policy = Optional.ofNullable(expect.get(r.getMethod())).orElseThrow(() -> new IllegalStateException("no such method in expectation table: " + r.getMethod()));
            boolean expectAuthorized = "success".equalsIgnoreCase(policy);
            String description = policy + ": " + r.getMethod() + "@" + node.getName();
            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 : AbstractSpecImplementation(com.quorum.gauge.core.AbstractSpecImplementation) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) Step(com.thoughtworks.gauge.Step) Logger(org.slf4j.Logger) ObjectResponse(com.quorum.gauge.ext.ObjectResponse) QuorumNode(com.quorum.gauge.common.QuorumNode) BatchRequest(com.quorum.gauge.ext.BatchRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) QuorumNetworkProperty(com.quorum.gauge.common.QuorumNetworkProperty) LoggerFactory(org.slf4j.LoggerFactory) Response(org.web3j.protocol.core.Response) Collectors(java.util.stream.Collectors) Request(org.web3j.protocol.core.Request) Stream(java.util.stream.Stream) Context(com.quorum.gauge.common.Context) Service(org.springframework.stereotype.Service) Table(com.thoughtworks.gauge.Table) DataStoreFactory(com.thoughtworks.gauge.datastore.DataStoreFactory) ObjectResponse(com.quorum.gauge.ext.ObjectResponse) Response(org.web3j.protocol.core.Response) BatchRequest(com.quorum.gauge.ext.BatchRequest) BatchRequest(com.quorum.gauge.ext.BatchRequest) Request(org.web3j.protocol.core.Request) ObjectResponse(com.quorum.gauge.ext.ObjectResponse) Step(com.thoughtworks.gauge.Step)

Example 2 with Request

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

the class PermissionService method NodeInfo.

public String NodeInfo(QuorumNetworkProperty.Node node) {
    Request<?, NodeInfo> nodeInfoRequest = new Request<>("admin_nodeInfo", null, connectionFactory().getWeb3jService(node), NodeInfo.class);
    NodeInfo nodeInfo = nodeInfoRequest.flowable().toObservable().blockingFirst();
    return nodeInfo.getEnode();
}
Also used : NodeInfo(com.quorum.gauge.ext.NodeInfo) Request(org.web3j.protocol.core.Request)

Example 3 with Request

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

the class RawContractService method signTransaction.

public Observable<FillTransactionResponse> signTransaction(QuorumNode from, PrivateFillTransaction tx) {
    Web3j client = connectionFactory().getConnection(from);
    Request<?, FillTransactionResponse> request = new Request<>("eth_signTransaction", Collections.singletonList(tx), connectionFactory().getWeb3jService(from), FillTransactionResponse.class);
    return request.flowable().toObservable();
}
Also used : Web3j(org.web3j.protocol.Web3j) Request(org.web3j.protocol.core.Request) FillTransactionResponse(com.quorum.gauge.ext.filltx.FillTransactionResponse)

Example 4 with Request

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

the class ContractService method readSimpleContractValue.

/**
 * Need to use EthCall to manipulate the error as webj3 doesn't
 *
 * @param node
 * @param contractAddress
 * @return
 */
public Observable<BigInteger> readSimpleContractValue(Node node, String contractAddress) {
    Quorum client = connectionFactory().getConnection(node);
    Function function = new Function(FUNC_GET, Arrays.<Type>asList(), Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {
    }));
    return client.ethCoinbase().flowable().toObservable().map(Response::getResult).flatMap(address -> {
        Request<?, EthCall> req = client.ethCall(Transaction.createEthCallTransaction(address, contractAddress, FunctionEncoder.encode(function)), DefaultBlockParameterName.LATEST);
        return req.flowable().toObservable();
    }).map(ec -> {
        if (ec.hasError()) {
            throw new ContractCallException(ec.getError().getMessage());
        }
        List<Type> values = FunctionReturnDecoder.decode(ec.getValue(), function.getOutputParameters());
        Type result;
        if (!values.isEmpty()) {
            result = values.get(0);
        } else {
            throw new ContractCallException("Empty value (0x) returned from contract");
        }
        Object value = result.getValue();
        if (BigInteger.class.isAssignableFrom(value.getClass())) {
            return (BigInteger) value;
        } else {
            throw new ContractCallException("Unable to convert response: " + value + " to expected type: " + BigInteger.class.getSimpleName());
        }
    });
}
Also used : Response(org.web3j.protocol.core.Response) Transaction(org.web3j.protocol.core.methods.request.Transaction) Arrays(java.util.Arrays) EthChainId(com.quorum.gauge.ext.EthChainId) PrivacyFlag(com.quorum.gauge.common.PrivacyFlag) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) FunctionEncoder(org.web3j.abi.FunctionEncoder) Response(org.web3j.protocol.core.Response) Request(org.web3j.protocol.core.Request) FUNC_GET(com.quorum.gauge.sol.SimpleStorage.FUNC_GET) ReadonlyTransactionManager(org.web3j.tx.ReadonlyTransactionManager) Uint256(org.web3j.abi.datatypes.generated.Uint256) Charset(java.nio.charset.Charset) Service(org.springframework.stereotype.Service) Observable(io.reactivex.Observable) BigInteger(java.math.BigInteger) ClientTransactionManager(org.web3j.quorum.tx.ClientTransactionManager) FunctionReturnDecoder(org.web3j.abi.FunctionReturnDecoder) com.quorum.gauge.sol(com.quorum.gauge.sol) Type(org.web3j.abi.datatypes.Type) Node(com.quorum.gauge.common.QuorumNetworkProperty.Node) StreamUtils(org.springframework.util.StreamUtils) Quorum(org.web3j.quorum.Quorum) EthSendTransactionAsync(com.quorum.gauge.ext.EthSendTransactionAsync) Contract(org.web3j.tx.Contract) Logger(org.slf4j.Logger) EthStorageRoot(com.quorum.gauge.ext.EthStorageRoot) TypeReference(org.web3j.abi.TypeReference) Collections.emptyList(java.util.Collections.emptyList) QuorumNode(com.quorum.gauge.common.QuorumNode) QuorumNetworkProperty(com.quorum.gauge.common.QuorumNetworkProperty) PrivateTransactionAsync(com.quorum.gauge.ext.PrivateTransactionAsync) DefaultBlockParameterName(org.web3j.protocol.core.DefaultBlockParameterName) ContractCallException(org.web3j.tx.exceptions.ContractCallException) DefaultBlockParameter(org.web3j.protocol.core.DefaultBlockParameter) IOException(java.io.IOException) PrivateClientTransactionManager(com.quorum.gauge.ext.PrivateClientTransactionManager) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Web3j(org.web3j.protocol.Web3j) CollectionUtils(org.springframework.util.CollectionUtils) EthFilter(org.web3j.protocol.core.methods.request.EthFilter) TransactionManager(org.web3j.tx.TransactionManager) Function(org.web3j.abi.datatypes.Function) org.web3j.protocol.core.methods.response(org.web3j.protocol.core.methods.response) Collections(java.util.Collections) InputStream(java.io.InputStream) Function(org.web3j.abi.datatypes.Function) Quorum(org.web3j.quorum.Quorum) Type(org.web3j.abi.datatypes.Type) ContractCallException(org.web3j.tx.exceptions.ContractCallException) Request(org.web3j.protocol.core.Request) BigInteger(java.math.BigInteger) TypeReference(org.web3j.abi.TypeReference)

Example 5 with Request

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

the class ExtensionService method initiateContractExtension.

public Observable<QuorumExtendContract> initiateContractExtension(final QuorumNetworkProperty.Node node, final String addressToExtend, final QuorumNetworkProperty.Node newParty, final PrivacyFlag privacyFlag) {
    String recipientKey = accountService.getDefaultAccountAddress(newParty).blockingFirst();
    final List<String> privateFor = Stream.of(newParty).map(n -> privacyService.id(n)).collect(Collectors.toList());
    final EnhancedPrivateTransaction transactionArgs = new EnhancedPrivateTransaction(accountService.getDefaultAccountAddress(node).blockingFirst(), null, null, null, BigInteger.ZERO, null, privacyService.id(node), privateFor, singletonList(privacyFlag));
    final List<Object> arguments = Stream.of(addressToExtend, privacyService.id(newParty), recipientKey, transactionArgs).collect(Collectors.toList());
    final Request<?, QuorumExtendContract> request = new Request<>("quorumExtension_extendContract", arguments, connectionFactory().getWeb3jService(node), QuorumExtendContract.class);
    return request.flowable().toObservable();
}
Also used : PrivacyFlag(com.quorum.gauge.common.PrivacyFlag) PrivateTransaction(org.web3j.quorum.methods.request.PrivateTransaction) Collections.emptyList(java.util.Collections.emptyList) QuorumNetworkProperty(com.quorum.gauge.common.QuorumNetworkProperty) Autowired(org.springframework.beans.factory.annotation.Autowired) Set(java.util.Set) Collectors(java.util.stream.Collectors) Request(org.web3j.protocol.core.Request) Collections.singletonList(java.util.Collections.singletonList) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Service(org.springframework.stereotype.Service) com.quorum.gauge.ext.contractextension(com.quorum.gauge.ext.contractextension) EnhancedPrivateTransaction(com.quorum.gauge.ext.EnhancedPrivateTransaction) Observable(io.reactivex.Observable) BigInteger(java.math.BigInteger) EnhancedPrivateTransaction(com.quorum.gauge.ext.EnhancedPrivateTransaction) Request(org.web3j.protocol.core.Request)

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