From 45777275890f56738e231544bad1978c57f5a0b0 Mon Sep 17 00:00:00 2001 From: "bengtson.lennart" Date: Fri, 21 Aug 2020 12:23:38 +0200 Subject: [PATCH 1/5] Add generic deployment for smart contracts. Requires testing. --- .../las2peer/registry/ReadWriteRegistryClient.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/core/src/main/java/i5/las2peer/registry/ReadWriteRegistryClient.java b/core/src/main/java/i5/las2peer/registry/ReadWriteRegistryClient.java index 6bf1776de..c1072561e 100644 --- a/core/src/main/java/i5/las2peer/registry/ReadWriteRegistryClient.java +++ b/core/src/main/java/i5/las2peer/registry/ReadWriteRegistryClient.java @@ -14,6 +14,7 @@ import org.web3j.protocol.core.methods.response.EthSendTransaction; import org.web3j.protocol.core.methods.response.TransactionReceipt; import org.web3j.protocol.exceptions.TransactionException; +import org.web3j.tx.Contract; import org.web3j.tx.FastRawTransactionManager; import org.web3j.tx.Transfer; import org.web3j.utils.Convert; @@ -513,5 +514,16 @@ public String sendEtherFromCoinbase(String recipientAddress, BigInteger valueInW return sendEtherManaged(coinbase, recipientAddress, valueInWei); } + public T deploySmartContract(Class contractClass, String contractBinary) { + T contract = null; + try { + // TODO Add binary! + contract = T.deployRemoteCall(contractClass, web3j, credentials, gasPrice, gasLimit, contractBinary, "", BigInteger.ZERO).send(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return contract; + } } From 79ab7e4621f9ad7dc1021e3ac4007f178f9cca58 Mon Sep 17 00:00:00 2001 From: "bengtson.lennart" Date: Wed, 26 Aug 2020 11:22:36 +0200 Subject: [PATCH 2/5] Updated web3j version --- .classpath | 2 +- core/ivy.xml | 2 +- .../main/java/i5/las2peer/registry/ReadWriteRegistryClient.java | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.classpath b/.classpath index b19e9cbbb..1eed87618 100644 --- a/.classpath +++ b/.classpath @@ -15,7 +15,6 @@ - @@ -73,5 +72,6 @@ + diff --git a/core/ivy.xml b/core/ivy.xml index 9dc3ce6cc..4d54ee9cb 100644 --- a/core/ivy.xml +++ b/core/ivy.xml @@ -29,7 +29,7 @@ - + diff --git a/core/src/main/java/i5/las2peer/registry/ReadWriteRegistryClient.java b/core/src/main/java/i5/las2peer/registry/ReadWriteRegistryClient.java index c1072561e..c35889822 100644 --- a/core/src/main/java/i5/las2peer/registry/ReadWriteRegistryClient.java +++ b/core/src/main/java/i5/las2peer/registry/ReadWriteRegistryClient.java @@ -517,7 +517,6 @@ public String sendEtherFromCoinbase(String recipientAddress, BigInteger valueInW public T deploySmartContract(Class contractClass, String contractBinary) { T contract = null; try { - // TODO Add binary! contract = T.deployRemoteCall(contractClass, web3j, credentials, gasPrice, gasLimit, contractBinary, "", BigInteger.ZERO).send(); } catch (Exception e) { // TODO Auto-generated catch block From ea1e8563512d03e01d7274cedaa10153862b80ed Mon Sep 17 00:00:00 2001 From: "bengtson.lennart" Date: Wed, 2 Sep 2020 09:34:18 +0200 Subject: [PATCH 3/5] Read multiple XML elements with same tag --- .../i5/las2peer/serialization/XmlTools.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/core/src/main/java/i5/las2peer/serialization/XmlTools.java b/core/src/main/java/i5/las2peer/serialization/XmlTools.java index ba6201cbc..01f43eab8 100644 --- a/core/src/main/java/i5/las2peer/serialization/XmlTools.java +++ b/core/src/main/java/i5/las2peer/serialization/XmlTools.java @@ -5,6 +5,8 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -162,6 +164,35 @@ public static Element getOptionalElement(Element parent, String tagName) throws return null; } } + + /** + * Gets all elements with the given child tag from given parent XML element. + * + * @param parent The parent XML element. + * @param tagName The tag name of the child elements. CASE SENSITIVE + * @return Returns the child elements with the given tag name or null if no child matches the given tag name. + * @throws MalformedXMLException If it's not a node itself. + */ + public static List getElementList(Element parent, String tagName) throws MalformedXMLException { + NodeList nodeList = parent.getElementsByTagName(tagName); + int len = nodeList.getLength(); + if (len > 0) { + ArrayList elements = new ArrayList<>(); + for (int i = 0; i < len; i++) { + org.w3c.dom.Node firstNode = nodeList.item(i); + short nodeType = firstNode.getNodeType(); + if (nodeType != org.w3c.dom.Node.ELEMENT_NODE) { + throw new MalformedXMLException( + "Node type (" + nodeType + ") is not type element (" + org.w3c.dom.Node.ELEMENT_NODE + ")"); + } + elements.add((Element) firstNode); + } + return elements; + } else { + // no child elements with tag name found + return null; + } + } public static String escapeString(String str) { return str.replace("&", "&").replace("'", "'").replace("<", "<").replace(">", ">"); From fc22b50311428559afd390e63321709c7f3f9670 Mon Sep 17 00:00:00 2001 From: "bengtson.lennart" Date: Thu, 24 Sep 2020 14:17:49 +0200 Subject: [PATCH 4/5] Add loading smart contract with given address --- .../registry/ReadWriteRegistryClient.java | 56 +++++++++++++------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/i5/las2peer/registry/ReadWriteRegistryClient.java b/core/src/main/java/i5/las2peer/registry/ReadWriteRegistryClient.java index c35889822..db0142048 100644 --- a/core/src/main/java/i5/las2peer/registry/ReadWriteRegistryClient.java +++ b/core/src/main/java/i5/las2peer/registry/ReadWriteRegistryClient.java @@ -1,6 +1,7 @@ package i5.las2peer.registry; import java.io.IOException; +import java.lang.reflect.Constructor; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Arrays; @@ -9,6 +10,7 @@ import org.web3j.abi.datatypes.Function; import org.web3j.crypto.Credentials; +import org.web3j.protocol.Web3j; import org.web3j.protocol.core.Response; import org.web3j.protocol.core.methods.request.Transaction; import org.web3j.protocol.core.methods.response.EthSendTransaction; @@ -17,6 +19,8 @@ import org.web3j.tx.Contract; import org.web3j.tx.FastRawTransactionManager; import org.web3j.tx.Transfer; +import org.web3j.tx.gas.ContractGasProvider; +import org.web3j.tx.gas.StaticGasProvider; import org.web3j.utils.Convert; import org.web3j.utils.Convert.Unit; @@ -226,7 +230,7 @@ public String addUserRating(EthereumAgent receivingAgent, Integer rating) throws } public boolean hasReputationProfile(String profileName) - throws EthereumException { + throws EthereumException { Boolean hasProfile = Boolean.FALSE; try { hasProfile = contracts.reputationRegistry.hasProfile(profileName).sendAsync().get(); @@ -246,7 +250,7 @@ public boolean hasReputationProfile(String profileName) * @param agent Ethereum agent of the service author */ public void registerService(String serviceName, EthereumAgent agent) throws EthereumException, - AgentLockedException { + AgentLockedException { byte[] authorName = Util.padAndConvertString(agent.getLoginName(), 32); final Function function = new Function(ServiceRegistry.FUNC_REGISTER, @@ -258,7 +262,7 @@ public void registerService(String serviceName, EthereumAgent agent) throws Ethe try { contracts.serviceRegistry.delegatedRegister(serviceName, authorName, consentee, signature).sendAsync() - .get(); + .get(); } catch (Exception e) { throw new EthereumException("Failed to register service", e); } @@ -348,12 +352,12 @@ public void releaseService(String serviceName, int versionMajor, int versionMino * @param nodeId identifier of the node running the deployment */ public void announceDeployment(String servicePackageName, String serviceClassName, - int versionMajor, int versionMinor, int versionPatch, - String nodeId) throws EthereumException { + int versionMajor, int versionMinor, int versionPatch, + String nodeId) throws EthereumException { try { contracts.serviceRegistry.announceDeployment(servicePackageName, serviceClassName, - BigInteger.valueOf(versionMajor), BigInteger.valueOf(versionMinor), BigInteger.valueOf(versionPatch), - nodeId).sendAsync().get(); + BigInteger.valueOf(versionMajor), BigInteger.valueOf(versionMinor), BigInteger.valueOf(versionPatch), + nodeId).sendAsync().get(); } catch (Exception e) { throw new EthereumException("Failed to submit service deployment announcement (" + "DEBUG: " + serviceClassName + ", " + e.getMessage() + ")", e); @@ -375,12 +379,12 @@ public void announceDeployment(String servicePackageName, String serviceClassNam // TODO: is there a more elegant way? referencing the tx is possible of course // but not really preferable public void announceDeploymentEnd(String servicePackageName, String serviceClassName, - int versionMajor, int versionMinor, int versionPatch, - String nodeId) throws EthereumException { + int versionMajor, int versionMinor, int versionPatch, + String nodeId) throws EthereumException { try { contracts.serviceRegistry.announceDeploymentEnd(servicePackageName, serviceClassName, - BigInteger.valueOf(versionMajor), BigInteger.valueOf(versionMinor), BigInteger.valueOf(versionPatch), - nodeId).sendAsync().get(); + BigInteger.valueOf(versionMajor), BigInteger.valueOf(versionMinor), BigInteger.valueOf(versionPatch), + nodeId).sendAsync().get(); } catch (Exception e) { throw new EthereumException("Failed to submit service deployment *end* announcement (" + "DEBUG: " + serviceClassName + ", " + e.getMessage() + ")", e); @@ -393,10 +397,10 @@ public String sendEtherManaged(String senderAddress, String recipientAddress, Bi BigInteger gasPrice = GAS_PRICE; BigInteger gasLimit = GAS_LIMIT_ETHER_TX; logger.info("[ETH] Preparing raw transaction between accounts..."); - + Transfer transfer = new Transfer(this.web3j, this.contracts.transactionManager); - + @@ -444,7 +448,7 @@ public String sendEtherManaged(String senderAddress, String recipientAddress, Bi @Deprecated public TransactionReceipt sendEther(String senderAddress, String recipientAddress, BigInteger valueInWei) throws EthereumException { - + TransactionReceipt txR; String txHash; BigInteger nonce = BigInteger.valueOf(-1); @@ -469,12 +473,12 @@ public TransactionReceipt sendEther(String senderAddress, String recipientAddres throw new EthereumException("Eth Transaction Error [" + error.getCode() + "]: " + error.getMessage()); } txHash = ethSendTransaction.getTransactionHash(); - + if (txHash.length() < 2) { throw new EthereumException("Could not create ethereum transaction"); } - + logger.info("[ETH] waiting for receipt on [" + txHash + "]... "); txR = waitForReceipt(txHash); @@ -513,7 +517,7 @@ public String sendEtherFromCoinbase(String recipientAddress, BigInteger valueInW logger.info("[ETH] Sending Faucet Transaction"); return sendEtherManaged(coinbase, recipientAddress, valueInWei); } - + public T deploySmartContract(Class contractClass, String contractBinary) { T contract = null; try { @@ -524,5 +528,21 @@ public T deploySmartContract(Class contractClass, String } return contract; } - + + public T loadSmartContract(Class contractClass, String contractAddress) { + T contract = null; + try { + Constructor constructor = contractClass.getDeclaredConstructor( + String.class, + Web3j.class, + Credentials.class, + ContractGasProvider.class); + constructor.setAccessible(true); + contract = constructor.newInstance(contractAddress, web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return contract; + } } From ce7b1615ae4eecd89e9756bbf1c699c2cd476753 Mon Sep 17 00:00:00 2001 From: BorisJov Date: Thu, 3 Dec 2020 20:12:28 +0100 Subject: [PATCH 5/5] Version updates for release --- bundle/build.xml | 2 +- core/build.xml | 2 +- restmapper/build.xml | 2 +- webconnector/build.xml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bundle/build.xml b/bundle/build.xml index 650dab60a..42969ba49 100644 --- a/bundle/build.xml +++ b/bundle/build.xml @@ -4,7 +4,7 @@ - + diff --git a/core/build.xml b/core/build.xml index ca9dd66fb..b8abbef63 100644 --- a/core/build.xml +++ b/core/build.xml @@ -4,7 +4,7 @@ - + diff --git a/restmapper/build.xml b/restmapper/build.xml index 29f5b82d7..59bbc4576 100644 --- a/restmapper/build.xml +++ b/restmapper/build.xml @@ -4,7 +4,7 @@ - + diff --git a/webconnector/build.xml b/webconnector/build.xml index bd993433d..eea2e9d7b 100644 --- a/webconnector/build.xml +++ b/webconnector/build.xml @@ -4,7 +4,7 @@ - +