use of org.web3j.abi.datatypes.Utf8String 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.web3j.abi.datatypes.Utf8String 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;
}
Aggregations