From 3868faaf4036eb61eea039d0d3214c6ce9d7bb2d Mon Sep 17 00:00:00 2001 From: AZ Date: Sat, 10 Dec 2016 18:44:28 +0100 Subject: [PATCH] bundlesfromaddresses and latestinclusion added --- src/main/java/jota/IotaAPIProxy.java | 86 ++++++++++++++++------- src/main/java/jota/model/Transaction.java | 9 +++ src/main/java/jota/model/Transfer.java | 33 ++++++++- 3 files changed, 100 insertions(+), 28 deletions(-) diff --git a/src/main/java/jota/IotaAPIProxy.java b/src/main/java/jota/IotaAPIProxy.java index d8122d5..bf65728 100644 --- a/src/main/java/jota/IotaAPIProxy.java +++ b/src/main/java/jota/IotaAPIProxy.java @@ -351,41 +351,75 @@ public class IotaAPIProxy { return null; } - public Bundle[] bundlesFromAddresses(String[] addresses, Boolean inclusionStates) { - return null; + public Bundle[] bundlesFromAddresses(String[] addresses, Boolean inclusionStates) throws ArgumentException { Transaction[] trxs = findTransactionObjects(addresses); // set of tail transactions - var tailTransactions = new Set(); - var nonTailBundleHashes = new Set(); - - transactionObjects.forEach(function(thisTransaction) { + List tailTransactions = new ArrayList<>(); + List nonTailBundleHashes = new ArrayList<>(); + for (Transaction trx : trxs) { // Sort tail and nonTails - if (thisTransaction.currentIndex === 0) { - - tailTransactions.add(thisTransaction.hash); + if (Long.parseLong(trx.getCurrentIndex()) == 0) { + tailTransactions.add(trx.getHash()); } else { - - nonTailBundleHashes.add(thisTransaction.bundle) + nonTailBundleHashes.add(trx.getBundle()); } - }) -/* - // Get tail transactions for each nonTail via the bundle hash - self.findTransactionObjects({'bundles': Array.from(nonTailBundleHashes)}, function(error, bundleObjects) { + } + if (nonTailBundleHashes.isEmpty()) return null; - if (error) return callback(error); + Transaction[] bundleObjects = findTransactionObjects(addresses); + for (Transaction trx : bundleObjects) { + // Sort tail and nonTails + if (Long.parseLong(trx.getCurrentIndex()) == 0) { + tailTransactions.add(trx.getHash()); + } + } - bundleObjects.forEach(function(thisTransaction) { + List finalBundles = new ArrayList<>(); + String[] tailTxArray = tailTransactions.toArray(new String[tailTransactions.size()]); - if (thisTransaction.currentIndex === 0) { - - tailTransactions.add(thisTransaction.hash); + // If inclusionStates, get the confirmation status + // of the tail transactions, and thus the bundles + if (inclusionStates) { + GetInclusionStateResponse gisr = getLatestInclusion(tailTxArray); + if (gisr == null || gisr.getStates() == null || gisr.getStates().length == 0) return null; + for (String trx : tailTxArray) { + GetBundleResponse gbr = getBundle(trx); + if (gbr != null && gbr.getTransactions() != null) { + if (inclusionStates) { + boolean thisInclusion = gisr.getStates()[Arrays.asList(tailTxArray).indexOf(trx)]; + for (Transaction t : gbr.getTransactions()) { + t.setPersistence(thisInclusion); + } + } + finalBundles.add(gbr); } - }) + } + } + Collections.sort(finalBundles, new Comparator() { + public int compare(GetBundleResponse c1, GetBundleResponse c2) { + if (Long.parseLong(c1.getTransactions().get(0).getTimestamp()) > Long.parseLong(c2.getTransactions().get(0).getTimestamp())) + return -1; + if (Long.parseLong(c1.getTransactions().get(0).getTimestamp()) < Long.parseLong(c2.getTransactions().get(0).getTimestamp())) + return 1; + return 0; + } + }); + Bundle[] returnValue = new Bundle[finalBundles.size()]; + for (int i = 0; i < finalBundles.size(); i++) { + returnValue[i] = new Bundle(finalBundles.get(i).getTransactions(), finalBundles.get(i).getTransactions().size()); + } + return returnValue; + } - var finalBundles = []; - var tailTxArray = Array.from(tailTransactions);*/ + public GetInclusionStateResponse getLatestInclusion(String[] hashes) { + GetNodeInfoResponse getNodeInfoResponse = getNodeInfo(); + if (getNodeInfoResponse == null) return null; + + String[] latestMilestone = {getNodeInfoResponse.getLatestSolidSubtangleMilestone()}; + + return getInclusionStates(hashes, latestMilestone); } public Transaction[] findTransactionObjects(String[] input) throws ArgumentException { @@ -420,12 +454,14 @@ public class IotaAPIProxy { return transactionObjects.toArray(new Transaction[transactionObjects.size()]); } - public Transaction[] sendTransfer(String seed, int depth, int minWeightMagnitude, Transfer[] transfers, Input[] inputs, String address) throws NotEnoughBalanceException, ArgumentException { + public Transaction[] sendTransfer(String seed, int depth, int minWeightMagnitude, Transfer[] transfers, Input[] + inputs, String address) throws NotEnoughBalanceException, ArgumentException { String[] trytes = prepareTransfers(seed, transfers, inputs, address); return sendTrytes(trytes, depth, minWeightMagnitude); } - public String[] prepareTransfers(String seed, Transfer[] transfers, Input[] inputs, String remainderAddress) throws NotEnoughBalanceException, ArgumentException { + public String[] prepareTransfers(String seed, Transfer[] transfers, Input[] inputs, String remainderAddress) throws + NotEnoughBalanceException, ArgumentException { //InputValidator.checkTransferArray(transfers); // If message or tag is not supplied, provide it diff --git a/src/main/java/jota/model/Transaction.java b/src/main/java/jota/model/Transaction.java index 9a19ed3..c67bf53 100644 --- a/src/main/java/jota/model/Transaction.java +++ b/src/main/java/jota/model/Transaction.java @@ -19,6 +19,7 @@ public class Transaction { private String trunkTransaction; private String branchTransaction; private String nonce; + private Boolean persistence; public Transaction() { @@ -140,4 +141,12 @@ public class Transaction { public void setNonce(String nonce) { this.nonce = nonce; } + + public Boolean getPersistence() { + return persistence; + } + + public void setPersistence(Boolean persistence) { + this.persistence = persistence; + } } diff --git a/src/main/java/jota/model/Transfer.java b/src/main/java/jota/model/Transfer.java index dabbf6e..4fdaa8c 100644 --- a/src/main/java/jota/model/Transfer.java +++ b/src/main/java/jota/model/Transfer.java @@ -12,12 +12,12 @@ public class Transfer { private String timestamp; private String address; private String hash; - private Integer persistence; + private Boolean persistence; private long value; private String message; private String tag; - public Transfer(String timestamp, String address, String hash, Integer persistence, long value, String message, String tag) { + public Transfer(String timestamp, String address, String hash, Boolean persistence, long value, String message, String tag) { this.timestamp = timestamp; this.address = address; @@ -51,7 +51,7 @@ public class Transfer { return hash; } - public Integer getPersistence() { + public Boolean getPersistence() { return persistence; } @@ -71,4 +71,31 @@ public class Transfer { return tag; } + public void setTimestamp(String timestamp) { + this.timestamp = timestamp; + } + + public void setAddress(String address) { + this.address = address; + } + + public void setHash(String hash) { + this.hash = hash; + } + + public void setPersistence(Boolean persistence) { + this.persistence = persistence; + } + + public void setValue(long value) { + this.value = value; + } + + public void setMessage(String message) { + this.message = message; + } + + public void setTag(String tag) { + this.tag = tag; + } }