Skip to content
This repository was archived by the owner on Aug 23, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main/java/com/iota/iri/BundleValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public static List<List<TransactionViewModel>> validate(Tangle tangle, Hash tail
final Sponge curlInstance = SpongeFactory.create(SpongeFactory.Mode.KERL);
final Sponge addressInstance = SpongeFactory.create(SpongeFactory.Mode.KERL);

final int[] addressTrits = new int[TransactionViewModel.ADDRESS_TRINARY_SIZE];
final int[] bundleHashTrits = new int[TransactionViewModel.BUNDLE_TRINARY_SIZE];
final int[] normalizedBundle = new int[Curl.HASH_LENGTH / ISS.TRYTE_WIDTH];
final int[] digestTrits = new int[Curl.HASH_LENGTH];
final byte[] addressTrits = new byte[TransactionViewModel.ADDRESS_TRINARY_SIZE];
final byte[] bundleHashTrits = new byte[TransactionViewModel.BUNDLE_TRINARY_SIZE];
final byte[] normalizedBundle = new byte[Curl.HASH_LENGTH / ISS.TRYTE_WIDTH];
final byte[] digestTrits = new byte[Curl.HASH_LENGTH];

MAIN_LOOP:
while (true) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/iota/iri/Milestone.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ private Validity validateMilestone(SpongeFactory.Mode mode, TransactionViewModel
&& transactionViewModel.getBranchTransactionHash().equals(transactionViewModel2.getTrunkTransactionHash())
&& transactionViewModel.getBundleHash().equals(transactionViewModel2.getBundleHash())) {

final int[] trunkTransactionTrits = transactionViewModel.getTrunkTransactionHash().trits();
final int[] signatureFragmentTrits = Arrays.copyOfRange(transactionViewModel.trits(), TransactionViewModel.SIGNATURE_MESSAGE_FRAGMENT_TRINARY_OFFSET, TransactionViewModel.SIGNATURE_MESSAGE_FRAGMENT_TRINARY_OFFSET + TransactionViewModel.SIGNATURE_MESSAGE_FRAGMENT_TRINARY_SIZE);
final byte[] trunkTransactionTrits = transactionViewModel.getTrunkTransactionHash().trits();
final byte[] signatureFragmentTrits = Arrays.copyOfRange(transactionViewModel.trits(), TransactionViewModel.SIGNATURE_MESSAGE_FRAGMENT_TRINARY_OFFSET, TransactionViewModel.SIGNATURE_MESSAGE_FRAGMENT_TRINARY_OFFSET + TransactionViewModel.SIGNATURE_MESSAGE_FRAGMENT_TRINARY_SIZE);

final int[] merkleRoot = ISS.getMerkleRoot(mode, ISS.address(mode, ISS.digest(mode,
final byte[] merkleRoot = ISS.getMerkleRoot(mode, ISS.address(mode, ISS.digest(mode,
Arrays.copyOf(ISS.normalizedBundle(trunkTransactionTrits),
ISS.NUMBER_OF_FRAGMENT_CHUNKS),
signatureFragmentTrits)),
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/iota/iri/SignedFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
public class SignedFiles {

public static boolean isFileSignatureValid(String filename, String signatureFilename, String publicKey, int depth, int index) throws IOException {
int[] signature = digestFile(filename, SpongeFactory.create(SpongeFactory.Mode.KERL));
byte[] signature = digestFile(filename, SpongeFactory.create(SpongeFactory.Mode.KERL));
return validateSignature(signatureFilename, publicKey, depth, index, signature);
}

private static boolean validateSignature(String signatureFilename, String publicKey, int depth, int index, int[] digest) throws IOException {
private static boolean validateSignature(String signatureFilename, String publicKey, int depth, int index, byte[] digest) throws IOException {
//validate signature
SpongeFactory.Mode mode = SpongeFactory.Mode.CURLP81;
int[] digests = new int[0];
int[] bundle = ISS.normalizedBundle(digest);
int[] root;
byte[] digests = new byte[0];
byte[] bundle = ISS.normalizedBundle(digest);
byte[] root;
int i;

try (InputStream inputStream = SignedFiles.class.getResourceAsStream(signatureFilename);
Expand All @@ -31,33 +31,33 @@ private static boolean validateSignature(String signatureFilename, String public

String line;
for (i = 0; i < 3 && (line = reader.readLine()) != null; i++) {
int[] lineTrits = Converter.allocateTritsForTrytes(line.length());
byte[] lineTrits = Converter.allocateTritsForTrytes(line.length());
Converter.trits(line, lineTrits, 0);
int[] normalizedBundleFragment = Arrays.copyOfRange(bundle, i * ISS.NORMALIZED_FRAGMENT_LENGTH, (i + 1) * ISS.NORMALIZED_FRAGMENT_LENGTH);
int[] issDigest = ISS.digest(mode, normalizedBundleFragment, lineTrits);
byte[] normalizedBundleFragment = Arrays.copyOfRange(bundle, i * ISS.NORMALIZED_FRAGMENT_LENGTH, (i + 1) * ISS.NORMALIZED_FRAGMENT_LENGTH);
byte[] issDigest = ISS.digest(mode, normalizedBundleFragment, lineTrits);
digests = ArrayUtils.addAll(digests, issDigest);
}

if ((line = reader.readLine()) != null) {
int[] lineTrits = Converter.allocateTritsForTrytes(line.length());
byte[] lineTrits = Converter.allocateTritsForTrytes(line.length());
Converter.trits(line, lineTrits, 0);
root = ISS.getMerkleRoot(mode, ISS.address(mode, digests), lineTrits, 0, index, depth);
} else {
root = ISS.address(mode, digests);
}

int[] pubkeyTrits = Converter.allocateTritsForTrytes(publicKey.length());
byte[] pubkeyTrits = Converter.allocateTritsForTrytes(publicKey.length());
Converter.trits(publicKey, pubkeyTrits, 0);
return Arrays.equals(pubkeyTrits, root); // valid
}
}

private static int[] digestFile(String filename, Sponge curl) throws IOException {
private static byte[] digestFile(String filename, Sponge curl) throws IOException {
try (InputStream inputStream = SignedFiles.class.getResourceAsStream(filename);
BufferedReader reader = new BufferedReader((inputStream == null)
? new FileReader(filename) : new InputStreamReader(inputStream))) {

int[] buffer = new int[Curl.HASH_LENGTH * 3];
byte[] buffer = new byte[Curl.HASH_LENGTH * 3];

reader.lines().forEach(line -> {
String trytes = Converter.asciiToTrytes(line); // can return a null
Expand All @@ -66,10 +66,10 @@ private static int[] digestFile(String filename, Sponge curl) throws IOException
}
Converter.trits(trytes, buffer, 0);
curl.absorb(buffer, 0, buffer.length);
Arrays.fill(buffer, 0);
Arrays.fill(buffer, (byte) 0);
});

int[] signature = new int[Curl.HASH_LENGTH];
byte[] signature = new byte[Curl.HASH_LENGTH];
curl.squeeze(signature, 0, Curl.HASH_LENGTH);
return signature;
} catch (UncheckedIOException e) {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/iota/iri/TransactionValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public TransactionValidator(Tangle tangle, TipsViewModel tipsViewModel, Transact

public void init(boolean testnet, int mwm) {
MIN_WEIGHT_MAGNITUDE = mwm;

//lowest allowed MWM encoded in 46 bytes.
if (!testnet && MIN_WEIGHT_MAGNITUDE<13){
MIN_WEIGHT_MAGNITUDE = 13;
Expand Down Expand Up @@ -104,17 +104,17 @@ public static void runValidation(TransactionViewModel transactionViewModel, fina
}
}

public static TransactionViewModel validate(final int[] trits, int minWeightMagnitude) {
public static TransactionViewModel validateTrits(final byte[] trits, int minWeightMagnitude) {
TransactionViewModel transactionViewModel = new TransactionViewModel(trits, Hash.calculate(trits, 0, trits.length, SpongeFactory.create(SpongeFactory.Mode.CURLP81)));
runValidation(transactionViewModel, minWeightMagnitude);
return transactionViewModel;
}
public static TransactionViewModel validate(final byte[] bytes, int minWeightMagnitude) {
return validate(bytes, minWeightMagnitude, SpongeFactory.create(SpongeFactory.Mode.CURLP81));

public static TransactionViewModel validateBytes(final byte[] bytes, int minWeightMagnitude) {
return validateBytes(bytes, minWeightMagnitude, SpongeFactory.create(SpongeFactory.Mode.CURLP81));
}

public static TransactionViewModel validate(final byte[] bytes, int minWeightMagnitude, Sponge curl) {
public static TransactionViewModel validateBytes(final byte[] bytes, int minWeightMagnitude, Sponge curl) {
TransactionViewModel transactionViewModel = new TransactionViewModel(bytes, Hash.calculate(bytes, TransactionViewModel.TRINARY_SIZE, curl));
runValidation(transactionViewModel, minWeightMagnitude);
return transactionViewModel;
Expand Down Expand Up @@ -186,7 +186,7 @@ private Runnable spawnSolidTransactionsPropagation() {
for(Hash h: approvers) {
TransactionViewModel tx = TransactionViewModel.fromHash(tangle, h);
if(quietQuickSetSolid(tx)) {
tx.update(tangle, "solid");
tx.update(tangle, "solid");
} else {
if (transaction.isSolid()) {
addSolidTransaction(hash);
Expand Down
54 changes: 27 additions & 27 deletions src/main/java/com/iota/iri/controllers/TransactionViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class TransactionViewModel {
public final static int PREFILLED_SLOT = 1; // means that we know only hash of the tx, the rest is unknown yet: only another tx references that hash
public final static int FILLED_SLOT = -1; // knows the hash only coz another tx references that hash

private int[] trits;
private byte[] trits;
public int weightMagnitude;

public static void fillMetadata(Tangle tangle, TransactionViewModel transactionViewModel) throws Exception {
Expand Down Expand Up @@ -85,29 +85,29 @@ public TransactionViewModel(final Transaction transaction, final Hash hash) {
weightMagnitude = this.hash.trailingZeros();
}

public TransactionViewModel(final int[] trits, Hash hash) {
transaction = new com.iota.iri.model.Transaction();
this.trits = new int[trits.length];
System.arraycopy(trits, 0, this.trits, 0, trits.length);
transaction.bytes = Converter.allocateBytesForTrits(trits.length);
Converter.bytes(trits, 0, transaction.bytes, 0, trits.length);
this.hash = hash;
public TransactionViewModel(final byte[] trits, Hash hash) {
if(trits.length == 8019) {
transaction = new com.iota.iri.model.Transaction();
this.trits = new byte[trits.length];
System.arraycopy(trits, 0, this.trits, 0, trits.length);
transaction.bytes = Converter.allocateBytesForTrits(trits.length);
Converter.bytes(trits, 0, transaction.bytes, 0, trits.length);
this.hash = hash;

transaction.type = FILLED_SLOT;
transaction.type = FILLED_SLOT;

weightMagnitude = this.hash.trailingZeros();
transaction.validity = 0;
transaction.arrivalTime = 0;
}


public TransactionViewModel(final byte[] bytes, Hash hash) throws RuntimeException {
transaction = new Transaction();
transaction.bytes = new byte[SIZE];
System.arraycopy(bytes, 0, transaction.bytes, 0, SIZE);
this.hash = hash;
weightMagnitude = this.hash.trailingZeros();
transaction.type = FILLED_SLOT;
weightMagnitude = this.hash.trailingZeros();
transaction.validity = 0;
transaction.arrivalTime = 0;
}
else {
transaction = new Transaction();
transaction.bytes = new byte[SIZE];
System.arraycopy(trits, 0, transaction.bytes, 0, SIZE);
this.hash = hash;
weightMagnitude = this.hash.trailingZeros();
transaction.type = FILLED_SLOT;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would create two static factory methods:
createFromBytes and createFromTrits and make the constructor private.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll open an issue on this. but it shouldn't block the goodness from this PR imo.

}

public static int getNumberOfStoredTransactions(Tangle tangle) throws Exception {
Expand Down Expand Up @@ -143,16 +143,16 @@ public TransactionViewModel getTrunkTransaction(Tangle tangle) throws Exception
return trunk;
}

public static int[] trits(byte[] transactionBytes) {
int[] trits;
trits = new int[TRINARY_SIZE];
public static byte[] trits(byte[] transactionBytes) {
byte[] trits;
trits = new byte[TRINARY_SIZE];
if(transactionBytes != null) {
Converter.getTrits(transactionBytes, trits);
}
return trits;
}

public synchronized int[] trits() {
public synchronized byte[] trits() {
return (trits == null) ? (trits = trits(transaction.bytes)) : trits;
}

Expand Down Expand Up @@ -329,7 +329,7 @@ public long getCurrentIndex() {
return transaction.currentIndex;
}

public int[] getSignature() {
public byte[] getSignature() {
return Arrays.copyOfRange(trits(), SIGNATURE_MESSAGE_FRAGMENT_TRINARY_OFFSET, SIGNATURE_MESSAGE_FRAGMENT_TRINARY_SIZE);
}

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/iota/iri/hash/Curl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ public class Curl implements Sponge {
private static final int STATE_LENGTH = 3 * HASH_LENGTH;
private static final int HALF_LENGTH = 364;

private static final int[] TRUTH_TABLE = {1, 0, -1, 2, 1, -1, 0, 2, -1, 1, 0};
private static final byte[] TRUTH_TABLE = {1, 0, -1, 2, 1, -1, 0, 2, -1, 1, 0};
/*
private static final IntPair[] TRANSFORM_INDICES = IntStream.range(0, STATE_LENGTH)
.mapToObj(i -> new IntPair(i == 0 ? 0 : (((i - 1) % 2) + 1) * HALF_LENGTH - ((i - 1) >> 1),
((i % 2) + 1) * HALF_LENGTH - ((i) >> 1)))
.toArray(IntPair[]::new);
*/

private final int[] state;
private final byte[] state;
private final long[] stateLow;
private final long[] stateHigh;

private final int[] scratchpad = new int[STATE_LENGTH];
private final byte[] scratchpad = new byte[STATE_LENGTH];


protected Curl(SpongeFactory.Mode mode) {
Expand All @@ -45,7 +45,7 @@ protected Curl(SpongeFactory.Mode mode) {
} break;
default: throw new NoSuchElementException("Only Curl-P-27 and Curl-P-81 are supported.");
}
state = new int[STATE_LENGTH];
state = new byte[STATE_LENGTH];
stateHigh = null;
stateLow = null;
}
Expand All @@ -66,7 +66,7 @@ public Curl(boolean pair, SpongeFactory.Mode mode) {
state = null;
set();
} else {
state = new int[STATE_LENGTH];
state = new byte[STATE_LENGTH];
stateHigh = null;
stateLow = null;
}
Expand All @@ -76,7 +76,7 @@ private void setMode(SpongeFactory.Mode mode) {

}

public void absorb(final int[] trits, int offset, int length) {
public void absorb(final byte[] trits, int offset, int length) {

do {
System.arraycopy(trits, offset, state, 0, length < HASH_LENGTH ? length : HASH_LENGTH);
Expand All @@ -86,7 +86,7 @@ public void absorb(final int[] trits, int offset, int length) {
}


public void squeeze(final int[] trits, int offset, int length) {
public void squeeze(final byte[] trits, int offset, int length) {

do {
System.arraycopy(state, 0, trits, offset, length < HASH_LENGTH ? length : HASH_LENGTH);
Expand All @@ -113,7 +113,7 @@ private void transform() {
}
}
public void reset() {
Arrays.fill(state, 0);
Arrays.fill(state, (byte) 0);
}
public void reset(boolean pair) {
if(pair) {
Expand Down
Loading