Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

Commit

Permalink
Refactoring conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
aaitor committed Feb 4, 2021
1 parent 063eec7 commit 36a5fce
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 54 deletions.
22 changes: 19 additions & 3 deletions src/main/java/io/keyko/nevermined/manager/ConditionsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
import org.web3j.tuples.generated.Tuple2;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ConditionsManager extends BaseManager {

Expand Down Expand Up @@ -130,10 +133,23 @@ public Boolean releaseReward(String agreementId) throws Exception {
try {
final Condition escrowRewardCondition = service.getConditionbyName(Condition.ConditionTypes.escrowReward.name());

List<BigInteger> _amounts = new ArrayList<>();
for (Object _someAmount: (ArrayList) escrowRewardCondition.getParameterByName("_amounts").value) {
if (_someAmount instanceof Integer || _someAmount instanceof Long)
_amounts.add(new BigInteger(String.valueOf(_someAmount)));
else if (_someAmount instanceof BigInteger)
_amounts.add((BigInteger) _someAmount);
else
_amounts.add(new BigInteger(_someAmount.toString()));
}

final List<String> _receivers = ((List<String>) escrowRewardCondition.getParameterByName("_receivers").value)
.stream().map(a -> Keys.toChecksumAddress(a))
.collect(Collectors.toList());
txReceipt = escrowReward.fulfill(EncodingHelper.hexStringToBytes(agreementId),
(List<BigInteger>) escrowRewardCondition.getParameterByName("_amounts").value,
(List<String>) escrowRewardCondition.getParameterByName("_receivers").value,
agreementData.component1(),
_amounts,
_receivers,
Keys.toChecksumAddress(ddo.proof.creator),
agreement.conditions.get(1),
agreement.conditions.get(0)).send();

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/keyko/nevermined/models/AssetRewards.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public String getReceiversArrayString() {
}

public String getAmountsArrayString() {
String str = "[" +
String.join("\",\"", rewards.values()) +
return "[" +
String.join(",", rewards.values()) +
"]";
return str.replace("[", "[\"")
.replace("]", "\"]");
// return str.replace("[", "[\"")
// .replace("]", "\"]");
}
}
2 changes: 1 addition & 1 deletion src/main/java/io/keyko/nevermined/models/DDO.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public Service getServiceByTemplate(String templateId) throws ServiceException {

// We assume there is only one service in the DDO with a specific templateId
return services.stream()
.filter(s -> s.templateId == templateId)
.filter(s -> s.templateId instanceof String && s.templateId.toLowerCase().equals(templateId.toLowerCase()))
.findFirst()
.orElseThrow(() -> new ServiceException("Service with template=" + templateId + " not found"));

Expand Down
35 changes: 22 additions & 13 deletions src/main/java/io/keyko/nevermined/models/service/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
import io.keyko.nevermined.models.service.attributes.ServiceAdditionalInformation;
import io.keyko.nevermined.models.service.attributes.ServiceCuration;
import io.keyko.nevermined.models.service.attributes.ServiceMain;
import org.web3j.abi.TypeEncoder;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.DynamicArray;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Hash;
import org.web3j.protocol.Web3j;
import org.web3j.utils.Numeric;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -130,7 +136,7 @@ public static class ServiceAgreementTemplate {
public List<String> fulfillmentOrder = Arrays.asList(
"lockReward.fulfill",
"accessSecretStore.fulfill",
"escrowReward.fulfillMultipleRewards");
"escrowReward.fulfill");

@JsonProperty
public ConditionDependency conditionDependency = new ConditionDependency();
Expand Down Expand Up @@ -318,30 +324,33 @@ public String generateEscrowRewardConditionId(String serviceAgreementId, String

String params = null;
if (amounts.value instanceof String) {
params = EthereumHelper.add0x(EthereumHelper.encodeParameterValue(amounts.type, amounts.value.toString())
+ EthereumHelper.encodeParameterValue(amounts.type, publisherAddress)
+ EthereumHelper.encodeParameterValue(sender.type, consumerAddress)
params = EthereumHelper.add0x(EthereumHelper.encodeParameterValue("uint256", amounts.value)
+ EthereumHelper.encodeParameterValue("address", publisherAddress)
+ EthereumHelper.encodeParameterValue("address", publisherAddress)
+ EthereumHelper.encodeParameterValue(lockCondition.type, lockConditionId)
+ EthereumHelper.encodeParameterValue(releaseCondition.type, releaseConditionId));

} else {
String encodedReceivers = "";
StringBuilder encodedReceivers = new StringBuilder();
for (String _receiver: (List<String>) receivers.value) {
encodedReceivers = encodedReceivers + EthereumHelper.encodeParameterValue("address", _receiver);
encodedReceivers.append(TypeEncoder.encode(new Address(_receiver)));
}
String encodedAmounts = "";
for (String _amount: (List<String>) amounts.value) {
encodedAmounts = encodedAmounts + EthereumHelper.encodeParameterValue("uint256", _amount);

StringBuilder encodedAmounts = new StringBuilder();
// List<Uint256> uintAmounts = new ArrayList<>();
for (Object _someAmount: (List) amounts.value) {
final Uint256 uint256 = new Uint256(new BigInteger(String.valueOf(_someAmount)));
// uintAmounts.add(uint256);
encodedAmounts.append(TypeEncoder.encode(uint256));
}

params = EthereumHelper.add0x(encodedAmounts
+ encodedReceivers
+ EthereumHelper.encodeParameterValue(sender.type, consumerAddress)
params = EthereumHelper.add0x(encodedAmounts.toString()
+ encodedReceivers.toString()
+ EthereumHelper.encodeParameterValue(sender.type, publisherAddress)
+ EthereumHelper.encodeParameterValue(lockCondition.type, lockConditionId)
+ EthereumHelper.encodeParameterValue(releaseCondition.type, releaseConditionId));
}


String valuesHash = Hash.sha3(params);

return Hash.sha3(
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/sla/access-sla-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,17 @@
"timelock": 0,
"timeout": 0,
"contractName": "EscrowReward",
"functionName": "fulfillMultipleRewards",
"functionName": "fulfill",
"parameters": [
{
"name": "_amounts",
"type": "uint256[]",
"value": ""
"value": []
},
{
"name": "_receivers",
"type": "address[]",
"value": ""
"value": []
},
{
"name": "_sender",
Expand All @@ -138,7 +138,7 @@
],
"events": [
{
"name": "FulfilledMultiple",
"name": "Fulfilled",
"actorType": "publisher",
"handler": {
"moduleName": "escrowRewardCondition",
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/sla/sla-access-conditions-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"timelock": 0,
"timeout": 0,
"contractName": "EscrowReward",
"functionName": "fulfillMultipleRewards",
"functionName": "fulfill",
"parameters": [
{
"name": "_amounts",
Expand All @@ -93,17 +93,17 @@
{
"name": "_lockCondition",
"type": "bytes32",
"value": "{contract.LockRewardCondition.address}"
"value": ""
},
{
"name": "_releaseCondition",
"type": "bytes32",
"value": "{contract.AccessSecretStoreCondition.address}"
"value": ""
}
],
"events": [
{
"name": "FulfilledMultiple",
"name": "Fulfilled",
"actorType": "publisher",
"handler": {
"moduleName": "escrowRewardCondition",
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/sla/sla-computing-conditions-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"timelock": 0,
"timeout": 0,
"contractName": "EscrowReward",
"functionName": "fulfillMultipleRewards",
"functionName": "fulfill",
"parameters": [
{
"name": "_amounts",
Expand All @@ -93,17 +93,17 @@
{
"name": "_lockCondition",
"type": "bytes32",
"value": "{contract.LockRewardCondition.address}"
"value": ""
},
{
"name": "_releaseCondition",
"type": "bytes32",
"value": "{contract.ComputeExecutionCondition.address}"
"value": ""
}
],
"events": [
{
"name": "FulfilledMultiple",
"name": "Fulfilled",
"actorType": "publisher",
"handler": {
"moduleName": "escrowRewardCondition",
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/io/keyko/nevermined/api/AssetsApiIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ public void create() throws Exception {
assertTrue(receivers.contains(neverminedAPI.getMainAccount().address));
assertTrue(receivers.contains(config.getString("provider.address")));

final List<String> _amounts = (List<String>) resolvedDDO.getAccessService()
final List<BigInteger> _amounts = (List<BigInteger>) resolvedDDO.getAccessService()
.getConditionbyName(Condition.ConditionTypes.escrowReward.name())
.getParameterByName("_amounts").value;
assertTrue(_amounts.contains("10"));
assertTrue(_amounts.contains("2"));
assertTrue(_amounts.contains(BigInteger.valueOf(10)));
assertTrue(_amounts.contains(BigInteger.valueOf(2)));

}

Expand Down

0 comments on commit 36a5fce

Please sign in to comment.