From 8d07e0ba603865f36df784fa50671f4cac01f693 Mon Sep 17 00:00:00 2001 From: pinpong Date: Mon, 6 Nov 2017 22:31:34 +0100 Subject: [PATCH] refactored --- jota/src/main/java/jota/IotaAPI.java | 49 ++++++------------- jota/src/main/java/jota/model/Bundle.java | 16 +++--- .../src/main/java/jota/model/Transaction.java | 3 +- jota/src/main/java/jota/pow/Kerl.java | 4 +- jota/src/main/java/jota/utils/Checksum.java | 3 +- 5 files changed, 28 insertions(+), 47 deletions(-) diff --git a/jota/src/main/java/jota/IotaAPI.java b/jota/src/main/java/jota/IotaAPI.java index 35e456a..e18c57e 100644 --- a/jota/src/main/java/jota/IotaAPI.java +++ b/jota/src/main/java/jota/IotaAPI.java @@ -108,7 +108,7 @@ public class IotaAPI extends IotaAPICore { * @throws InvalidSecurityLevelException is thrown when the specified security level is not valid. */ public GetTransferResponse getTransfers(String seed, int security, Integer start, Integer end, Boolean inclusionStates) throws ArgumentException, InvalidBundleException, InvalidSignatureException, NoNodeInfoException, NoInclusionStatesException, InvalidSecurityLevelException, InvalidAddressException { - StopWatch stopWatch = new StopWatch(); + // validate seed if ((!InputValidator.isValidSeed(seed))) { throw new IllegalStateException("Invalid Seed"); @@ -118,12 +118,12 @@ public class IotaAPI extends IotaAPICore { throw new InvalidSecurityLevelException(); } - start = start == null ? 0 : start; if (start > end || end > (start + 500)) { - throw new ArgumentException(); + throw new ArgumentException("Invalid inputs provided"); } - StopWatch sw = new StopWatch(); + + StopWatch stopWatch = new StopWatch(); GetNewAddressResponse gnr = getNewAddress(seed, security, start, false, end, true); if (gnr != null && gnr.getAddresses() != null) { @@ -510,7 +510,6 @@ public class IotaAPI extends IotaAPICore { * @throws InvalidSecurityLevelException is thrown when the specified security level is not valid. **/ public GetBalancesAndFormatResponse getInputs(String seed, int security, int start, int end, long threshold) throws InvalidSecurityLevelException, InvalidAddressException { - StopWatch stopWatch = new StopWatch(); // validate the seed if ((!InputValidator.isValidSeed(seed))) { @@ -527,6 +526,8 @@ public class IotaAPI extends IotaAPICore { throw new IllegalStateException("Invalid inputs provided"); } + StopWatch stopWatch = new StopWatch(); + // Case 1: start and end // // If start and end is defined by the user, simply iterate through the keys @@ -541,7 +542,7 @@ public class IotaAPI extends IotaAPICore { allAddresses.add(address); } - return getBalanceAndFormat(allAddresses, threshold, start, end, stopWatch, security); + return getBalanceAndFormat(allAddresses, threshold, start, stopWatch, security); } // Case 2: iterate till threshold || end // @@ -550,7 +551,7 @@ public class IotaAPI extends IotaAPICore { // We then do getBalance, format the output and return it else { final GetNewAddressResponse res = getNewAddress(seed, security, start, false, 0, true); - return getBalanceAndFormat(res.getAddresses(), threshold, start, end, stopWatch, security); + return getBalanceAndFormat(res.getAddresses(), threshold, start, stopWatch, security); } } @@ -560,13 +561,12 @@ public class IotaAPI extends IotaAPICore { * @param addresses The addresses. * @param threshold Min balance required. * @param start Starting key index. - * @param end Ending key index. * @param stopWatch the stopwatch. * @param security The security level of private key / seed. * @return Inputs object. * @throws InvalidSecurityLevelException is thrown when the specified security level is not valid. **/ - public GetBalancesAndFormatResponse getBalanceAndFormat(final List addresses, long threshold, int start, int end, StopWatch stopWatch, int security) throws InvalidSecurityLevelException { + public GetBalancesAndFormatResponse getBalanceAndFormat(final List addresses, long threshold, int start, StopWatch stopWatch, int security) throws InvalidSecurityLevelException { if (security < 1) { throw new InvalidSecurityLevelException(); @@ -618,13 +618,14 @@ public class IotaAPI extends IotaAPICore { * @throws InvalidSignatureException is thrown when an invalid signature is encountered. */ public GetBundleResponse getBundle(String transaction) throws ArgumentException, InvalidBundleException, InvalidSignatureException { - StopWatch stopWatch = new StopWatch(); Bundle bundle = traverseBundle(transaction, null, new Bundle()); if (bundle == null) { throw new ArgumentException("Unknown Bundle"); } + StopWatch stopWatch = new StopWatch(); + long totalSum = 0; String bundleHash = bundle.getTransactions().get(0).getBundle(); @@ -718,6 +719,10 @@ public class IotaAPI extends IotaAPICore { throws InvalidBundleException, ArgumentException, InvalidSignatureException, InvalidTrytesException, InvalidSecurityLevelException, InvalidAddressException, NoInclusionStatesException, NoNodeInfoException { + if (start > end || end > (start + 1000)) { + throw new ArgumentException("Invalid inputs provided"); + } + StopWatch stopWatch = new StopWatch(); GetNewAddressResponse gna = getNewAddress(seed, security, index, checksum, total, returnAll); @@ -1044,29 +1049,7 @@ public class IotaAPI extends IotaAPICore { } } - - /** - * @param hash The hash. - * @return A bundle. - * @throws ArgumentException is thrown when an invalid argument is provided. - */ - public String findTailTransactionHash(String hash) throws ArgumentException { - GetTrytesResponse gtr = getTrytes(hash); - - if (gtr == null) throw new ArgumentException("Invalid hash"); - - if (gtr.getTrytes().length == 0) { - throw new ArgumentException("Bundle transactions not visible"); - } - - Transaction trx = new Transaction(gtr.getTrytes()[0], customCurl.clone()); - if (trx.getBundle() == null) { - throw new ArgumentException("Invalid trytes, could not create object"); - } - if (trx.getCurrentIndex() == 0) return trx.getHash(); - else return findTailTransactionHash(trx.getBundle()); - } - + /** * @param seed Tryte-encoded seed. * @param security The security level of private key / seed. diff --git a/jota/src/main/java/jota/model/Bundle.java b/jota/src/main/java/jota/model/Bundle.java index 40c0e42..3ed3840 100644 --- a/jota/src/main/java/jota/model/Bundle.java +++ b/jota/src/main/java/jota/model/Bundle.java @@ -123,14 +123,14 @@ public class Bundle implements Comparable { normalizedBundleValue = normalizedBundle(hashInTrytes); boolean foundValue = false; - for (int i = 0; i < normalizedBundleValue.length; i++) { - if (normalizedBundleValue[i] == 13) { - foundValue = true; - obsoleteTagTrits = Converter.trits(this.getTransactions().get(0).getObsoleteTag()); - Converter.increment(obsoleteTagTrits, 81); - this.getTransactions().get(0).setObsoleteTag(Converter.trytes(obsoleteTagTrits)); + for (int aNormalizedBundleValue : normalizedBundleValue) { + if (aNormalizedBundleValue == 13) { + foundValue = true; + obsoleteTagTrits = Converter.trits(this.getTransactions().get(0).getObsoleteTag()); + Converter.increment(obsoleteTagTrits, 81); + this.getTransactions().get(0).setObsoleteTag(Converter.trytes(obsoleteTagTrits)); + } } - } valid = !foundValue; } while (!valid); @@ -148,7 +148,7 @@ public class Bundle implements Comparable { public void addTrytes(List signatureFragments) { String emptySignatureFragment = ""; String emptyHash = EMPTY_HASH; - long emptyTimestamp = 999999999l; + long emptyTimestamp = 999999999L; emptySignatureFragment = StringUtils.rightPad(emptySignatureFragment, 2187, '9'); diff --git a/jota/src/main/java/jota/model/Transaction.java b/jota/src/main/java/jota/model/Transaction.java index cedce1f..716c5f0 100644 --- a/jota/src/main/java/jota/model/Transaction.java +++ b/jota/src/main/java/jota/model/Transaction.java @@ -423,7 +423,7 @@ public class Transaction { this.tag = this.tag != null && !this.tag.isEmpty() ? this.tag : this.obsoleteTag; - String trx = this.getSignatureFragments() + return this.getSignatureFragments() + this.getAddress() + Converter.trytes(valueTrits) + this.getObsoleteTag() @@ -438,7 +438,6 @@ public class Transaction { + Converter.trytes(attachmentTimestampLowerBoundTrits) + Converter.trytes(attachmentTimestampUpperBoundTrits) + this.getNonce(); - return trx; } /** diff --git a/jota/src/main/java/jota/pow/Kerl.java b/jota/src/main/java/jota/pow/Kerl.java index c916060..9c4cdef 100644 --- a/jota/src/main/java/jota/pow/Kerl.java +++ b/jota/src/main/java/jota/pow/Kerl.java @@ -40,8 +40,8 @@ public class Kerl extends JCurl { private static int sum(int[] toSum) { int sum = 0; - for (int i = 0; i < toSum.length; i++) { - sum += toSum[i]; + for (int aToSum : toSum) { + sum += aToSum; } return sum; } diff --git a/jota/src/main/java/jota/utils/Checksum.java b/jota/src/main/java/jota/utils/Checksum.java index 08107ca..464131d 100644 --- a/jota/src/main/java/jota/utils/Checksum.java +++ b/jota/src/main/java/jota/utils/Checksum.java @@ -90,7 +90,6 @@ public class Checksum { int[] checksumTrits = new int[JCurl.HASH_LENGTH]; curl.squeeze(checksumTrits); String checksum = Converter.trytes(checksumTrits); - String checksumPrt = checksum.substring(72, 81); - return checksumPrt; + return checksum.substring(72, 81); } }