Search in sources :

Example 1 with EthereumException

use of org.eclipse.winery.accountability.exceptions.EthereumException in project winery by eclipse.

the class ProvenanceSmartContractWrapper method getProvenance.

public CompletableFuture<List<ModelProvenanceElement>> getProvenance(final String identifier) {
    // eventName, indexed parameters, unindexed parameters
    final Event event = new Event("ResourceVersion", Arrays.asList(new TypeReference<Utf8String>() {
    }, new TypeReference<Address>() {
    }, new TypeReference<Bytes>() {
    }));
    EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contract.getContractAddress()).addSingleTopic(EventEncoder.encode(event)).addOptionalTopics(Hash.sha3String(identifier)).addNullTopic();
    final CompletableFuture<List<ModelProvenanceElement>> result = new CompletableFuture<>();
    try {
        final int recordsCount = web3j.ethGetLogs(filter).send().getLogs().size();
        LOGGER.info(recordsCount + " provenance elements detected.");
        if (recordsCount > 0) {
            final List<ModelProvenanceElement> provenanceElements = new ArrayList<>();
            final Disposable subscription = ((Provenance) contract).resourceVersionEventFlowable(filter).subscribe(resourceVersionEventResponse -> {
                try {
                    final ModelProvenanceElement currentElement = generateProvenanceElement(resourceVersionEventResponse);
                    provenanceElements.add(currentElement);
                    if (provenanceElements.size() == recordsCount) {
                        result.complete(provenanceElements);
                    }
                } catch (EthereumException e) {
                    result.completeExceptionally(e);
                }
            });
            // unsubscribe the observable when the CompletableFuture completes (this frees threads)
            result.whenComplete((r, e) -> subscription.dispose());
        } else {
            // empty result
            result.complete(new ArrayList<>());
        }
    } catch (IOException e) {
        final String msg = "Failed detecting the number of provenance elements for the collaboration resource. Reason: " + e.getMessage();
        LOGGER.error(msg);
        result.completeExceptionally(new EthereumException(msg, e));
    }
    return result;
}
Also used : Disposable(io.reactivex.disposables.Disposable) EthFilter(org.web3j.protocol.core.methods.request.EthFilter) ModelProvenanceElement(org.eclipse.winery.accountability.model.ModelProvenanceElement) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Utf8String(org.web3j.abi.datatypes.Utf8String) CompletableFuture(java.util.concurrent.CompletableFuture) Event(org.web3j.abi.datatypes.Event) EthereumException(org.eclipse.winery.accountability.exceptions.EthereumException) ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(org.web3j.abi.TypeReference)

Example 2 with EthereumException

use of org.eclipse.winery.accountability.exceptions.EthereumException in project winery by eclipse.

the class SmartContractProvider method validateSmartContract.

private static void validateSmartContract(Contract contract, String address) throws EthereumException {
    try {
        if (!contract.isValid()) {
            final String msg = "Contract at address " + address + " doesn't match the desired contract.";
            log.error(msg);
            throw new EthereumException(msg);
        }
    } catch (IOException e) {
        final String msg = "Error while checking the validity of referenced smart contract. Reason: " + e.getMessage();
        log.error(msg);
        throw new EthereumException(msg, e);
    }
}
Also used : EthereumException(org.eclipse.winery.accountability.exceptions.EthereumException) IOException(java.io.IOException)

Example 3 with EthereumException

use of org.eclipse.winery.accountability.exceptions.EthereumException in project winery by eclipse.

the class AuthorizationSmartContractWrapper method getAuthorizationTree.

/**
 * Retrieves the {@link AuthorizationInfo} from the blockchain.
 * If no authorization data can be retrieved, the completable future returns <code>null</code>.
 *
 * @param identifier The process identifier identifying the collaboration process.
 * @return A completable future containing the authorization information.
 */
public CompletableFuture<AuthorizationInfo> getAuthorizationTree(final String identifier) {
    // eventName, indexed parameters, unindexed parameters
    final Event event = new Event("Authorized", Arrays.asList(new TypeReference<Utf8String>() {
    }, new TypeReference<Address>() {
    }, new TypeReference<Address>() {
    }, new TypeReference<Utf8String>() {
    }));
    EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contract.getContractAddress()).addSingleTopic(EventEncoder.encode(event)).addOptionalTopics(Hash.sha3String(identifier)).addNullTopic().addNullTopic();
    final CompletableFuture<AuthorizationInfo> result = new CompletableFuture<>();
    try {
        final int recordsCount = web3j.ethGetLogs(filter).send().getLogs().size();
        LOGGER.info(recordsCount + " authorization elements detected.");
        if (recordsCount > 0) {
            final List<AuthorizationElement> authorizationElements = new ArrayList<>();
            final Disposable subscription = ((Authorization) contract).authorizedEventFlowable(filter).subscribe(authorizedEventResponse -> {
                try {
                    final AuthorizationElement currentElement = generateAuthorizationElement(authorizedEventResponse);
                    authorizationElements.add(currentElement);
                    if (authorizationElements.size() == recordsCount) {
                        final AuthorizationTree tree = new AuthorizationTree(authorizationElements);
                        result.complete(tree);
                    }
                } catch (EthereumException e) {
                    result.completeExceptionally(e);
                }
            });
            // unsubscribe the observable when the CompletableFuture completes (this frees threads)
            result.whenComplete((r, e) -> subscription.dispose());
        } else {
            // empty result
            result.complete(null);
        }
    } catch (IOException e) {
        final String msg = "Failed detecting the number of authorization elements for the collaboration resource. Reason: " + e.getMessage();
        LOGGER.error(msg);
        result.completeExceptionally(new EthereumException(msg, e));
    }
    return result;
}
Also used : Disposable(io.reactivex.disposables.Disposable) EthFilter(org.web3j.protocol.core.methods.request.EthFilter) ArrayList(java.util.ArrayList) AuthorizationElement(org.eclipse.winery.accountability.model.authorization.AuthorizationElement) IOException(java.io.IOException) Utf8String(org.web3j.abi.datatypes.Utf8String) AuthorizationInfo(org.eclipse.winery.accountability.model.authorization.AuthorizationInfo) CompletableFuture(java.util.concurrent.CompletableFuture) AuthorizationTree(org.eclipse.winery.accountability.model.authorization.AuthorizationTree) Event(org.web3j.abi.datatypes.Event) EthereumException(org.eclipse.winery.accountability.exceptions.EthereumException) TypeReference(org.web3j.abi.TypeReference)

Example 4 with EthereumException

use of org.eclipse.winery.accountability.exceptions.EthereumException in project winery by eclipse.

the class AuthorizationSmartContractWrapper method generateAuthorizationElement.

private AuthorizationElement generateAuthorizationElement(final Authorization.AuthorizedEventResponse event) throws EthereumException {
    try {
        final Log log = event.log;
        final AuthorizationElement result = new AuthorizationElement();
        result.setTransactionHash(log.getTransactionHash());
        // get the timestamp of the block that includes the tx that includes the authorization record
        result.setUnixTimestamp(web3j.ethGetBlockByHash(log.getBlockHash(), false).send().getBlock().getTimestamp().longValue());
        result.setAuthorizerBlockchainAddress(event._authorizer);
        result.setAuthorizedBlockchainAddress(event._authorized);
        result.setAuthorizedIdentity(event.realWorldIdentity);
        return result;
    } catch (IOException e) {
        final String msg = "Error while fetching block timestamp. Reason: " + e.getMessage();
        LOGGER.error(msg);
        throw new EthereumException(msg, e);
    }
}
Also used : Log(org.web3j.protocol.core.methods.response.Log) EthereumException(org.eclipse.winery.accountability.exceptions.EthereumException) AuthorizationElement(org.eclipse.winery.accountability.model.authorization.AuthorizationElement) IOException(java.io.IOException) Utf8String(org.web3j.abi.datatypes.Utf8String)

Example 5 with EthereumException

use of org.eclipse.winery.accountability.exceptions.EthereumException in project winery by eclipse.

the class ProvenanceSmartContractWrapper method generateProvenanceElement.

private ModelProvenanceElement generateProvenanceElement(final Provenance.ResourceVersionEventResponse event) throws EthereumException {
    try {
        final Log log = event.log;
        final ModelProvenanceElement result = new ModelProvenanceElement();
        result.setTransactionHash(log.getTransactionHash());
        result.setAuthorAddress(event._creator);
        // decompress the state
        final byte[] compressedState = event._compressedResource;
        result.setFingerprint(new String(CompressionUtils.decompress(compressedState), StandardCharsets.UTF_8));
        // get the timestamp of the block that includes the tx that includes the state change
        result.setUnixTimestamp(web3j.ethGetBlockByHash(log.getBlockHash(), false).send().getBlock().getTimestamp().longValue());
        return result;
    } catch (IOException e) {
        final String msg = "Error while fetching block timestamp. Reason: " + e.getMessage();
        LOGGER.error(msg);
        throw new EthereumException(msg, e);
    }
}
Also used : Log(org.web3j.protocol.core.methods.response.Log) ModelProvenanceElement(org.eclipse.winery.accountability.model.ModelProvenanceElement) EthereumException(org.eclipse.winery.accountability.exceptions.EthereumException) Utf8String(org.web3j.abi.datatypes.Utf8String) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)5 EthereumException (org.eclipse.winery.accountability.exceptions.EthereumException)5 Utf8String (org.web3j.abi.datatypes.Utf8String)4 Disposable (io.reactivex.disposables.Disposable)2 ArrayList (java.util.ArrayList)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ModelProvenanceElement (org.eclipse.winery.accountability.model.ModelProvenanceElement)2 AuthorizationElement (org.eclipse.winery.accountability.model.authorization.AuthorizationElement)2 TypeReference (org.web3j.abi.TypeReference)2 Event (org.web3j.abi.datatypes.Event)2 EthFilter (org.web3j.protocol.core.methods.request.EthFilter)2 Log (org.web3j.protocol.core.methods.response.Log)2 List (java.util.List)1 AuthorizationInfo (org.eclipse.winery.accountability.model.authorization.AuthorizationInfo)1 AuthorizationTree (org.eclipse.winery.accountability.model.authorization.AuthorizationTree)1