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;
}
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);
}
}
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;
}
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);
}
}
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);
}
}
Aggregations