refactored

This commit is contained in:
pinpong
2017-02-15 23:52:46 +01:00
parent a9a47e054d
commit 73799607cb
6 changed files with 57 additions and 38 deletions
+9 -9
View File
@@ -151,7 +151,7 @@ public class IotaAPI extends IotaAPICore {
for (Transaction trx : trxs) {
// Sort tail and nonTails
if (Long.parseLong(trx.getCurrentIndex()) == 0) {
if (trx.getCurrentIndex() == 0) {
tailTransactions.add(trx.getHash());
} else {
if (nonTailBundleHashes.indexOf(trx.getBundle()) == -1) {
@@ -163,7 +163,7 @@ public class IotaAPI extends IotaAPICore {
List<Transaction> bundleObjects = findTransactionObjectsByBundle(nonTailBundleHashes.toArray(new String[nonTailBundleHashes.size()]));
for (Transaction trx : bundleObjects) {
// Sort tail and nonTails
if (Long.parseLong(trx.getCurrentIndex()) == 0) {
if (trx.getCurrentIndex() == 0) {
if (tailTransactions.indexOf(trx.getHash()) == -1) {
tailTransactions.add(trx.getHash());
}
@@ -628,10 +628,10 @@ public class IotaAPI extends IotaAPICore {
for (int i = 0; i < bundle.getTransactions().size(); i++) {
Transaction trx = bundle.getTransactions().get(i);
Long bundleValue = Long.parseLong(trx.getValue());
Long bundleValue = trx.getValue();
totalSum += bundleValue;
if (i != Integer.parseInt(bundle.getTransactions().get(i).getCurrentIndex())) {
if (i != bundle.getTransactions().get(i).getCurrentIndex()) {
throw new ArgumentException("Invalid Bundle");
}
@@ -651,7 +651,7 @@ public class IotaAPI extends IotaAPICore {
Transaction newBundleTx = bundle.getTransactions().get(i + 1);
// Check if new tx is part of the signature fragment
if (newBundleTx.getAddress().equals(address) && Long.parseLong(newBundleTx.getValue()) == 0) {
if (newBundleTx.getAddress().equals(address) && newBundleTx.getValue() == 0) {
if (sig.getSignatureFragments().indexOf(newBundleTx.getSignatureFragments()) == -1)
sig.getSignatureFragments().add(newBundleTx.getSignatureFragments());
}
@@ -670,7 +670,7 @@ public class IotaAPI extends IotaAPICore {
if (!bundleFromTxString.equals(bundleHash)) throw new InvalidBundleException("Invalid Bundle Hash");
// Last tx in the bundle should have currentIndex === lastIndex
bundle.setLength(bundle.getTransactions().size());
if (!bundle.getTransactions().get(bundle.getLength() - 1).getCurrentIndex().equals(bundle.getTransactions().get(bundle.getLength() - 1).getLastIndex()))
if (!(bundle.getTransactions().get(bundle.getLength() - 1).getCurrentIndex() == (bundle.getTransactions().get(bundle.getLength() - 1).getLastIndex())))
throw new InvalidBundleException("Invalid Bundle");
// Validate the signatures
@@ -806,7 +806,7 @@ public class IotaAPI extends IotaAPICore {
throw new ArgumentException("Invalid trytes, could not create object");
}
// If first transaction to search is not a tail, return error
if (bundleHash == null && Integer.parseInt(trx.getCurrentIndex()) != 0) {
if (bundleHash == null && trx.getCurrentIndex() != 0) {
throw new ArgumentException("Invalid tail transaction supplied.");
}
// If no bundle hash, define it
@@ -818,7 +818,7 @@ public class IotaAPI extends IotaAPICore {
return bundle;
}
// If only one bundle element, return
if (Integer.parseInt(trx.getLastIndex()) == 0 && Integer.parseInt(trx.getCurrentIndex()) == 0) {
if (trx.getLastIndex() == 0 && trx.getCurrentIndex() == 0) {
return new Bundle(Collections.singletonList(trx), 1);
}
// Define new trunkTransaction for search
@@ -1027,7 +1027,7 @@ public class IotaAPI extends IotaAPICore {
if (trx.getBundle() == null) {
throw new ArgumentException("Invalid trytes, could not create object");
}
if (Integer.parseInt(trx.getCurrentIndex()) == 0) return trx.getHash();
if (trx.getCurrentIndex() == 0) return trx.getHash();
else return findTailTransactionHash(trx.getBundle());
}
+4 -4
View File
@@ -78,7 +78,7 @@ public class Bundle implements Comparable<Bundle> {
}
for (int i = 0; i < signatureMessageLength; i++) {
Transaction trx = new Transaction(address, String.valueOf(i == 0 ? value : 0), tag, String.valueOf(timestamp));
Transaction trx = new Transaction(address, i == 0 ? value : 0, tag, timestamp);
getTransactions().add(trx);
}
}
@@ -99,9 +99,9 @@ public class Bundle implements Comparable<Bundle> {
int[] timestampTrits = Converter.trits(this.getTransactions().get(i).getTimestamp(), 27);
int[] currentIndexTrits = Converter.trits(this.getTransactions().get(i).setCurrentIndex("" + i), 27);
int[] currentIndexTrits = Converter.trits(i + (this.getTransactions().get(i).getCurrentIndex()), 27);
int[] lastIndexTrits = Converter.trits(this.getTransactions().get(i).setLastIndex("" + (this.getTransactions().size() - 1)), 27);
int[] lastIndexTrits = Converter.trits(this.getTransactions().get(i).getLastIndex() - 1, 27);
int[] t = Converter.trits(this.getTransactions().get(i).getAddress() + Converter.trytes(valueTrits) + this.getTransactions().get(i).getTag() + Converter.trytes(timestampTrits) + Converter.trytes(currentIndexTrits) + Converter.trytes(lastIndexTrits));
@@ -198,6 +198,6 @@ public class Bundle implements Comparable<Bundle> {
*/
@Override
public int compareTo(Bundle o) {
return Long.compare(Long.parseLong(this.getTransactions().get(0).getTimestamp()), Long.parseLong(o.getTransactions().get(0).getTimestamp()));
return Long.compare(this.getTransactions().get(0).getTimestamp(), o.getTransactions().get(0).getTimestamp());
}
}
+22 -22
View File
@@ -24,11 +24,11 @@ public class Transaction {
private String hash;
private String signatureFragments;
private String address;
private String value;
private long value;
private String tag;
private String timestamp;
private String currentIndex;
private String lastIndex;
private long timestamp;
private long currentIndex;
private long lastIndex;
private String bundle;
private String trunkTransaction;
private String branchTransaction;
@@ -67,7 +67,7 @@ public class Transaction {
/**
* Initializes a new instance of the Signature class.
*/
public Transaction(String signatureFragments, String currentIndex, String lastIndex, String nonce, String hash, String tag, String timestamp, String trunkTransaction, String branchTransaction, String address, String value, String bundle) {
public Transaction(String signatureFragments, long currentIndex, long lastIndex, String nonce, String hash, String tag, long timestamp, String trunkTransaction, String branchTransaction, String address, long value, String bundle) {
this.hash = hash;
this.tag = tag;
@@ -86,7 +86,7 @@ public class Transaction {
/**
* Initializes a new instance of the Signature class.
*/
public Transaction(String address, String value, String tag, String timestamp) {
public Transaction(String address, long value, String tag, long timestamp) {
this.address = address;
this.value = value;
this.tag = tag;
@@ -135,8 +135,8 @@ public class Transaction {
*
* @param signatureFragments The signature fragments.
*/
public String setSignatureFragments(String signatureFragments) {
return this.signatureFragments = signatureFragments;
public void setSignatureFragments(String signatureFragments) {
this.signatureFragments = signatureFragments;
}
/**
@@ -162,7 +162,7 @@ public class Transaction {
*
* @return The value.
*/
public String getValue() {
public long getValue() {
return value;
}
@@ -171,7 +171,7 @@ public class Transaction {
*
* @param value The value.
*/
public void setValue(String value) {
public void setValue(long value) {
this.value = value;
}
@@ -198,7 +198,7 @@ public class Transaction {
*
* @return The timestamp.
*/
public String getTimestamp() {
public long getTimestamp() {
return timestamp;
}
@@ -207,7 +207,7 @@ public class Transaction {
*
* @param timestamp The timestamp.
*/
public void setTimestamp(String timestamp) {
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
@@ -216,7 +216,7 @@ public class Transaction {
*
* @return The current index.
*/
public String getCurrentIndex() {
public long getCurrentIndex() {
return currentIndex;
}
@@ -224,8 +224,8 @@ public class Transaction {
* Set the current index.
* @param currentIndex The current index.
*/
public String setCurrentIndex(String currentIndex) {
return this.currentIndex = currentIndex;
public void setCurrentIndex(long currentIndex) {
this.currentIndex = currentIndex;
}
/**
@@ -233,7 +233,7 @@ public class Transaction {
*
* @return The last index.
*/
public String getLastIndex() {
public long getLastIndex() {
return lastIndex;
}
@@ -242,8 +242,8 @@ public class Transaction {
*
* @param lastIndex The last index.
*/
public String setLastIndex(String lastIndex) {
return this.lastIndex = lastIndex;
public void setLastIndex(long lastIndex) {
this.lastIndex = lastIndex;
}
/**
@@ -400,11 +400,11 @@ public class Transaction {
this.setHash(Converter.trytes(hash));
this.setSignatureFragments(trytes.substring(0, 2187));
this.setAddress(trytes.substring(2187, 2268));
this.setValue("" + Converter.longValue(Arrays.copyOfRange(transactionTrits, 6804, 6837)));
this.setValue(Converter.longValue(Arrays.copyOfRange(transactionTrits, 6804, 6837)));
this.setTag(trytes.substring(2295, 2322));
this.setTimestamp("" + Converter.longValue(Arrays.copyOfRange(transactionTrits, 6966, 6993)));
this.setCurrentIndex("" + Converter.longValue(Arrays.copyOfRange(transactionTrits, 6993, 7020)));
this.setLastIndex("" + Converter.longValue(Arrays.copyOfRange(transactionTrits, 7020, 7047)));
this.setTimestamp(Converter.longValue(Arrays.copyOfRange(transactionTrits, 6966, 6993)));
this.setCurrentIndex(Converter.longValue(Arrays.copyOfRange(transactionTrits, 6993, 7020)));
this.setLastIndex(Converter.longValue(Arrays.copyOfRange(transactionTrits, 7020, 7047)));
this.setBundle(trytes.substring(2349, 2430));
this.setTrunkTransaction(trytes.substring(2430, 2511));
this.setBranchTransaction(trytes.substring(2511, 2592));
+20
View File
@@ -120,6 +120,26 @@ public class Converter {
return convertToIntArray(tritsList);
}
/**
* Converts the specified trinary encoded string into a trits array of the specified length.
* @param trytes The trytes.
* @param length The length.
* @return A trits array.
*/
public static int[] trits(final long trytes, int length) {
int[] trits = trits(String.valueOf(trytes));
List<Integer> tritsList = new LinkedList<>();
for (int i : trits)
tritsList.add(i);
while (tritsList.size() < length)
tritsList.add(0);
return convertToIntArray(tritsList);
}
/**
* Converts the specified trinary encoded trytes string to trits.
*
+2 -2
View File
@@ -57,7 +57,7 @@ public class IotaAPIUtils {
// Iterate over all bundle transactions, find the inputs
// Get the corresponding private key and calculate the signatureFragment
for (int i = 0; i < bundle.getTransactions().size(); i++) {
if (Long.parseLong(bundle.getTransactions().get(i).getValue()) < 0) {
if (bundle.getTransactions().get(i).getValue() < 0) {
String thisAddress = bundle.getTransactions().get(i).getAddress();
// Get the corresponding keyIndex of the address
@@ -98,7 +98,7 @@ public class IotaAPIUtils {
// find the second transaction to add the remainder of the signature
for (int k = 0; k < bundle.getTransactions().size(); k++) {
// Same address as well as value = 0 (as we already spent the input)
if (bundle.getTransactions().get(k).getAddress().equals(thisAddress) && Long.parseLong(bundle.getTransactions().get(k).getValue()) == 0) {
if (bundle.getTransactions().get(k).getAddress().equals(thisAddress) && bundle.getTransactions().get(k).getValue() == 0) {
// Use the second 6562 trits
int[] secondFragment = Arrays.copyOfRange(key, 6561, 6561 * 2);
-1
View File
@@ -129,7 +129,6 @@ public class IotaCoreApiTest {
@Test
public void shouldGetBalances() {
GetBalancesResponse res = proxy.getBalances(100, new String[]{TEST_ADDRESS_WITH_CHECKSUM});
System.err.println(res);
assertThat(res.getBalances(), IsNull.notNullValue());
assertThat(res.getMilestone(), IsNull.notNullValue());
assertThat(res.getMilestoneIndex(), IsNull.notNullValue());