Search in sources :

Example 11 with Context

use of com.quorum.gauge.common.Context in project quorum-acceptance-tests by ConsenSys.

the class MultiTenancy method canNotReadContract.

@Step("`<clientNames>` can __NOT__ read <contractName> on `<node>`")
public void canNotReadContract(String clientNames, String contractName, Node node) {
    Contract existingContract = mustHaveValue(DataStoreFactory.getScenarioDataStore(), contractName, Contract.class);
    Observable.fromIterable(Arrays.stream(clientNames.split(",")).map(String::trim).collect(Collectors.toList())).forEach(clientName -> {
        obtainAccessToken(clientName);
        Optional<String> accessToken = haveValue(DataStoreFactory.getScenarioDataStore(), "access_token", String.class);
        accessToken.ifPresent(Context::storeAccessToken);
        assertThatThrownBy(() -> contractService.readSimpleContractValue(node, existingContract.getContractAddress()).doOnTerminate(Context::removeAccessToken).blockingSubscribe()).as(clientName).hasMessageContaining("Empty value (0x) returned from contract");
    });
}
Also used : Context(com.quorum.gauge.common.Context) Contract(org.web3j.tx.Contract) Step(com.thoughtworks.gauge.Step)

Example 12 with Context

use of com.quorum.gauge.common.Context in project quorum-acceptance-tests by ConsenSys.

the class ContractExtension method extensionCompleted.

@Step("Wait for <contractName> to disappear from active extension in <node>")
public void extensionCompleted(final String contractName, final QuorumNetworkProperty.Node node) {
    final Contract contract = mustHaveValue(contractName, Contract.class);
    Optional<String> accessToken = haveValue(DataStoreFactory.getScenarioDataStore(), "access_token", String.class);
    accessToken.ifPresent(Context::storeAccessToken);
    try {
        int count = 0;
        while (true) {
            count++;
            final QuorumActiveExtensionContracts result = this.extensionService.getExtensionContracts(node).blockingFirst();
            final Optional<Map<Object, Object>> first = result.getResult().stream().filter(contractStatus -> contractStatus.containsValue(contract.getContractAddress())).findFirst();
            if ((!first.isPresent()) || (count > 25)) {
                break;
            } else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        final DataStore store = DataStoreFactory.getScenarioDataStore();
        final String contractAddress = mustHaveValue(store, contractName + "extensionAddress", String.class);
        String status = extensionService.getExtensionStatus(node, contractAddress);
        int i = 0;
        if (!status.equals("DONE")) {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i++;
                status = extensionService.getExtensionStatus(node, contractAddress);
                if ((i > 25) || status.equals("DONE")) {
                    break;
                }
            }
        }
        assertThat(status).describedAs("Extension must be successfully completed").isEqualTo("DONE");
    } finally {
        Context.removeAccessToken();
    }
}
Also used : Context(com.quorum.gauge.common.Context) Contract(org.web3j.tx.Contract) AbstractSpecImplementation(com.quorum.gauge.core.AbstractSpecImplementation) Step(com.thoughtworks.gauge.Step) Logger(org.slf4j.Logger) PrivacyFlag(com.quorum.gauge.common.PrivacyFlag) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) QuorumNetworkProperty(com.quorum.gauge.common.QuorumNetworkProperty) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ExtensionService(com.quorum.gauge.services.ExtensionService) TransactionReceipt(org.web3j.protocol.core.methods.response.TransactionReceipt) Stream(java.util.stream.Stream) DataStore(com.thoughtworks.gauge.datastore.DataStore) Context(com.quorum.gauge.common.Context) Service(org.springframework.stereotype.Service) com.quorum.gauge.ext.contractextension(com.quorum.gauge.ext.contractextension) Map(java.util.Map) Optional(java.util.Optional) DataStoreFactory(com.thoughtworks.gauge.datastore.DataStoreFactory) DataStore(com.thoughtworks.gauge.datastore.DataStore) Contract(org.web3j.tx.Contract) Map(java.util.Map) Step(com.thoughtworks.gauge.Step)

Example 13 with Context

use of com.quorum.gauge.common.Context in project quorum-acceptance-tests by ConsenSys.

the class OAuth2Service method requestAccessToken.

public Observable<String> requestAccessToken(String clientId, List<String> targetAud, List<String> requestScopes) {
    Context.removeAccessToken();
    ClientCredentials cred = clientCredentials.get(clientId);
    if (cred == null) {
        throw new AccessControlException(clientId + " OAuth2 Client has not been setup");
    }
    RequestBody body = new FormBody.Builder().add("grant_type", "client_credentials").add("client_id", cred.clientId).add("client_secret", cred.clientSecret).add("scope", String.join(" ", requestScopes)).add("audience", String.join(" ", targetAud)).build();
    Request req = new Request.Builder().url(oAuth2ServerProperty().getClientEndpoint() + "/oauth2/token").post(body).build();
    return Observable.fromCallable(() -> httpClient.newCall(req).execute()).doOnError(e -> logger.error("Failed to retrieve access token", e)).map(r -> {
        try {
            assert r.body() != null;
            if (!r.isSuccessful()) {
                throw new RuntimeException("unsuccessful request: " + r.message() + ": " + r.body().string());
            }
            TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {
            };
            Map<String, String> map = new ObjectMapper().readValue(r.body().byteStream(), typeRef);
            return map.get("token_type") + " " + map.get("access_token");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            r.close();
        }
    }).onExceptionResumeNext(Observable.just("no_access_token_retrieved")).doOnNext(Context::storeAccessToken);
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) HashMap(java.util.HashMap) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) okhttp3(okhttp3) Context(com.quorum.gauge.common.Context) Service(org.springframework.stereotype.Service) Map(java.util.Map) AccessControlException(java.security.AccessControlException) Observable(io.reactivex.Observable) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Context(com.quorum.gauge.common.Context) HashMap(java.util.HashMap) AccessControlException(java.security.AccessControlException) IOException(java.io.IOException) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 14 with Context

use of com.quorum.gauge.common.Context in project quorum-acceptance-tests by ConsenSys.

the class MultiTenancy method deploySimpleStorageContractWithFlag.

@Step("`<clientName>` deploys a <privacyFlag> `SimpleStorage` contract on `<node>` private from `<privateFrom>` using `<ethAccount>`, named <contractName>, private for <anotherClient>'s `<privateFor>`")
public void deploySimpleStorageContractWithFlag(String clientName, PrivacyFlag privacyFlag, Node node, String privateFrom, String ethAccount, String contractName, String anotherClient, String privateFor) {
    List<String> privateForList = Arrays.stream(privateFor.split(",")).map(String::trim).collect(Collectors.toList());
    obtainAccessToken(clientName);
    Optional<String> accessToken = haveValue(DataStoreFactory.getScenarioDataStore(), "access_token", String.class);
    accessToken.ifPresent(Context::storeAccessToken);
    Contract contract = contractService.createSimpleContract(42, node, ethAccount, privateFrom, privateForList, List.of(privacyFlag)).doOnTerminate(Context::removeAccessToken).blockingFirst();
    DataStoreFactory.getScenarioDataStore().put(contractName, contract);
    DataStoreFactory.getScenarioDataStore().put(contractName + "_privateFrom", privacyService.id(privateFrom));
    DataStoreFactory.getScenarioDataStore().put(contractName + "_privacyFlag", privacyFlag);
}
Also used : Context(com.quorum.gauge.common.Context) Contract(org.web3j.tx.Contract) Step(com.thoughtworks.gauge.Step)

Example 15 with Context

use of com.quorum.gauge.common.Context in project quorum-acceptance-tests by ConsenSys.

the class MultiTenancy method deployPrivateContract.

private Observable<Optional<Contract>> deployPrivateContract(String clientName, QuorumNode node, String privateFrom, String privateFor, String contractId, WalletData wallet, String ethAccount) {
    List<String> privateForList = Arrays.stream(privateFor.split(",")).map(String::trim).collect(Collectors.toList());
    return requestAccessToken(clientName).flatMap(t -> {
        String realContractId = contractId;
        String delegateContractAddress = "";
        if (realContractId.startsWith("SimpleStorageDelegate")) {
            realContractId = "SimpleStorageDelegate";
            String delegateContractName = contractId.replaceAll("^.+\\((.+)\\)$", "$1");
            Contract delegateContractInstance = mustHaveValue(DataStoreFactory.getScenarioDataStore(), delegateContractName, Contract.class);
            delegateContractAddress = delegateContractInstance.getContractAddress();
        }
        if (realContractId.startsWith("SneakyWrapper")) {
            realContractId = "SneakyWrapper";
            String delegateContractName = contractId.replaceAll("^.+\\((.+)\\)$", "$1");
            Contract delegateContractInstance = mustHaveValue(DataStoreFactory.getScenarioDataStore(), delegateContractName, Contract.class);
            delegateContractAddress = delegateContractInstance.getContractAddress();
        }
        Node source = networkProperty.getNode(node.name());
        switch(realContractId) {
            case "SimpleStorage":
                if (wallet == null) {
                    return contractService.createSimpleContract(42, source, ethAccount, privateFrom, privateForList, null);
                } else {
                    return rawContractService.createRawSimplePrivateContract(42, wallet, node, privateFrom, privateForList);
                }
            case "ClientReceipt":
                if (wallet == null) {
                    return contractService.createClientReceiptPrivateSmartContract(source, ethAccount, privateFrom, privateForList);
                } else {
                    return rawContractService.createRawClientReceiptPrivateContract(wallet, node, privateFrom, privateForList);
                }
            case "SimpleStorageDelegate":
                if (wallet == null) {
                    return contractService.createSimpleDelegatePrivateContract(delegateContractAddress, source, ethAccount, privateFrom, privateForList);
                } else {
                    return rawContractService.createRawSimpleDelegatePrivateContract(delegateContractAddress, wallet, node, privateFrom, privateForList);
                }
            case "SneakyWrapper":
                if (wallet != null) {
                    return rawContractService.createRawSneakyWrapperPrivateContract(delegateContractAddress, wallet, node, privateFrom, privateForList);
                }
            case "ContractCodeReader":
                if (wallet == null) {
                    return contractCodeReaderService.createPrivateContract(source, ethAccount, privateFrom, privateForList);
                }
            default:
                return Observable.error(new UnsupportedOperationException(contractId + " is not supported"));
        }
    }).map(Optional::of).doOnError(e -> logger.debug("Deploying private contract: {}", e.getMessage(), e)).onErrorResumeNext(o -> {
        return Observable.just(Optional.empty());
    }).doOnTerminate(Context::removeAccessToken);
}
Also used : AbstractSpecImplementation(com.quorum.gauge.core.AbstractSpecImplementation) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) EthChainId(com.quorum.gauge.ext.EthChainId) TransactionException(org.web3j.protocol.exceptions.TransactionException) PrivacyFlag(com.quorum.gauge.common.PrivacyFlag) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Response(org.web3j.protocol.core.Response) AtomicReference(java.util.concurrent.atomic.AtomicReference) Credentials(org.web3j.crypto.Credentials) ExtensionService(com.quorum.gauge.services.ExtensionService) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) DataStore(com.thoughtworks.gauge.datastore.DataStore) Context(com.quorum.gauge.common.Context) Service(org.springframework.stereotype.Service) WalletUtils(org.web3j.crypto.WalletUtils) Observable(io.reactivex.Observable) BigInteger(java.math.BigInteger) DataStoreFactory(com.thoughtworks.gauge.datastore.DataStoreFactory) Node(com.quorum.gauge.common.QuorumNetworkProperty.Node) Contract(org.web3j.tx.Contract) Step(com.thoughtworks.gauge.Step) Logger(org.slf4j.Logger) QuorumNode(com.quorum.gauge.common.QuorumNode) QuorumNetworkProperty(com.quorum.gauge.common.QuorumNetworkProperty) Ints(com.google.common.primitives.Ints) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Stream(java.util.stream.Stream) WalletData(com.quorum.gauge.common.config.WalletData) org.web3j.protocol.core.methods.response(org.web3j.protocol.core.methods.response) Table(com.thoughtworks.gauge.Table) Context(com.quorum.gauge.common.Context) Node(com.quorum.gauge.common.QuorumNetworkProperty.Node) QuorumNode(com.quorum.gauge.common.QuorumNode) Contract(org.web3j.tx.Contract)

Aggregations

Context (com.quorum.gauge.common.Context)28 Contract (org.web3j.tx.Contract)26 Step (com.thoughtworks.gauge.Step)23 PrivacyFlag (com.quorum.gauge.common.PrivacyFlag)18 DataStore (com.thoughtworks.gauge.datastore.DataStore)17 BigInteger (java.math.BigInteger)17 Collectors (java.util.stream.Collectors)17 Logger (org.slf4j.Logger)17 LoggerFactory (org.slf4j.LoggerFactory)17 Autowired (org.springframework.beans.factory.annotation.Autowired)17 Service (org.springframework.stereotype.Service)17 QuorumNetworkProperty (com.quorum.gauge.common.QuorumNetworkProperty)16 AbstractSpecImplementation (com.quorum.gauge.core.AbstractSpecImplementation)16 ExtensionService (com.quorum.gauge.services.ExtensionService)16 DataStoreFactory (com.thoughtworks.gauge.datastore.DataStoreFactory)16 Stream (java.util.stream.Stream)16 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)16 Observable (io.reactivex.Observable)15 StringUtils (org.apache.commons.lang.StringUtils)15 Ints (com.google.common.primitives.Ints)14