Skip to content
This repository was archived by the owner on Apr 26, 2024. 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Special thanks to all of the [IOTA Contributors](https://github.com/iotaledger/i
## Developers

- Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
- Please read the [helix-1.0-specifications](https://github.com/HelixNetwork/helix-specs/blob/master/specs/helix-1.0.md) before contributing.
- Please read the [helix-1.0-specifications](https://github.com/HelixNetwork/helix-specs/tree/master/specs/1.0) before contributing.

## Installing
Make sure you have [**Maven**](https://maven.apache.org/) and [**Java 8**](https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) installed on your computer.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.helix.hlx.controllers;

import net.helix.hlx.model.Hash;
import net.helix.hlx.model.AddressHash;
import net.helix.hlx.model.persistables.Address;
import net.helix.hlx.storage.Indexable;
import net.helix.hlx.storage.Persistable;
Expand Down Expand Up @@ -107,7 +108,7 @@ public void delete(Tangle tangle) throws Exception {
* @return <code> AddressViewModel </code>
*/
public static AddressViewModel first(Tangle tangle) throws Exception {
Pair<Indexable, Persistable> bundlePair = tangle.getFirst(Address.class, Hash.class);
Pair<Indexable, Persistable> bundlePair = tangle.getFirst(Address.class, AddressHash.class);
if(bundlePair != null && bundlePair.hi != null) {
return new AddressViewModel((Address) bundlePair.hi, (Hash) bundlePair.low);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.helix.hlx.controllers;

import net.helix.hlx.model.Hash;
import net.helix.hlx.model.TransactionHash;
import net.helix.hlx.model.persistables.Approvee;
import net.helix.hlx.storage.Indexable;
import net.helix.hlx.storage.Persistable;
Expand Down Expand Up @@ -121,7 +122,7 @@ public void delete(Tangle tangle) throws Exception {
* @return <code> ApproveeViewModel </code>
*/
public static ApproveeViewModel first(Tangle tangle) throws Exception {
Pair<Indexable, Persistable> bundlePair = tangle.getFirst(Approvee.class, Hash.class);
Pair<Indexable, Persistable> bundlePair = tangle.getFirst(Approvee.class, TransactionHash.class);
if(bundlePair != null && bundlePair.hi != null) {
return new ApproveeViewModel((Approvee) bundlePair.hi, (Hash) bundlePair.low);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/helix/hlx/controllers/BundleViewModel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.helix.hlx.controllers;

import net.helix.hlx.model.Hash;
import net.helix.hlx.model.BundleHash;
import net.helix.hlx.model.persistables.Bundle;
import net.helix.hlx.storage.Indexable;
import net.helix.hlx.storage.Persistable;
Expand Down Expand Up @@ -129,7 +130,7 @@ public void delete(Tangle tangle) throws Exception {
* @return <code> bundleViewModel </code>
*/
public static BundleViewModel first(Tangle tangle) throws Exception {
Pair<Indexable, Persistable> bundlePair = tangle.getFirst(Bundle.class, Hash.class);
Pair<Indexable, Persistable> bundlePair = tangle.getFirst(Bundle.class, BundleHash.class);
if(bundlePair != null && bundlePair.hi != null) {
return new BundleViewModel((Bundle) bundlePair.hi, (Hash) bundlePair.low);
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/net/helix/hlx/controllers/TagViewModel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.helix.hlx.controllers;

import net.helix.hlx.model.Hash;
import net.helix.hlx.model.TagHash;
import net.helix.hlx.model.persistables.Tag;
import net.helix.hlx.storage.Indexable;
import net.helix.hlx.storage.Persistable;
Expand Down Expand Up @@ -34,6 +35,10 @@ public static TagViewModel load(Tangle tangle, Indexable hash) throws Exception
return load(tangle, hash, Tag.class);
}

public static TagViewModel loadBundleNonce(Tangle tangle, Indexable hash) throws Exception {
return load(tangle, hash, net.helix.hlx.model.persistables.BundleNonce.class);
}

public static Map.Entry<Indexable, Persistable> getEntry(Hash hash, Hash hashToMerge) throws Exception {
Tag hashes = new Tag();
hashes.set.add(hashToMerge);
Expand Down Expand Up @@ -64,7 +69,7 @@ public void delete(Tangle tangle) throws Exception {
tangle.delete(Tag.class,hash);
}
public static TagViewModel first(Tangle tangle) throws Exception {
Pair<Indexable, Persistable> bundlePair = tangle.getFirst(Tag.class, Hash.class);
Pair<Indexable, Persistable> bundlePair = tangle.getFirst(Tag.class, TagHash.class);
if(bundlePair != null && bundlePair.hi != null) {
return new TagViewModel((Tag) bundlePair.hi, (Hash) bundlePair.low);
}
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/net/helix/hlx/controllers/TransactionViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.helix.hlx.storage.Tangle;
import net.helix.hlx.utils.Converter;
import net.helix.hlx.utils.Pair;
import org.bouncycastle.util.encoders.Hex;

import java.util.*;

Expand Down Expand Up @@ -252,7 +251,7 @@ public List<Pair<Indexable, Persistable>> getSaveBatch() throws Exception {
* @return <code> TransactionViewModel </code>
*/
public static TransactionViewModel first(Tangle tangle) throws Exception {
Pair<Indexable, Persistable> transactionPair = tangle.getFirst(Transaction.class, Hash.class);
Pair<Indexable, Persistable> transactionPair = tangle.getFirst(Transaction.class, TransactionHash.class);
if(transactionPair != null && transactionPair.hi != null) {
return new TransactionViewModel((Transaction) transactionPair.hi, (Hash) transactionPair.low);
}
Expand Down Expand Up @@ -298,6 +297,20 @@ public boolean store(Tangle tangle, Snapshot initialSnapshot) throws Exception {
return tangle.saveBatch(batch);
}

/**
* Creates a copy of the underlying {@link Transaction} object.
*
* @return the transaction object
*/
public Transaction getTransaction() {
Transaction t = new Transaction();

//if the supplied array to the call != null the transaction bytes are copied over from the buffer.
t.read(getBytes());
t.readMetadata(transaction.metadata());
return t;
}

/**
* Gets the {@link ApproveeViewModel} of a {@link Transaction}. If the current {@link ApproveeViewModel} is null, a
* new one is created using the transaction {@link Hash} identifier.
Expand Down Expand Up @@ -452,7 +465,7 @@ public Hash getBranchTransactionHash() {
*/
public Hash getTagValue() {
if(transaction.tag == null) {
transaction.tag = HashFactory.TAG.create(getBytes(), TAG_OFFSET);
transaction.tag = HashFactory.TAG.create(getBytes(), TAG_OFFSET, TAG_SIZE);
}
return transaction.tag;
}
Expand Down
129 changes: 78 additions & 51 deletions src/main/java/net/helix/hlx/model/AbstractHash.java
Original file line number Diff line number Diff line change
@@ -1,55 +1,93 @@
package net.helix.hlx.model;

import java.io.Serializable;
import java.util.Arrays;

import org.bouncycastle.util.encoders.Hex;

import net.helix.hlx.utils.Converter;
import net.helix.hlx.model.persistables.Transaction;
import net.helix.hlx.model.safe.ByteSafe;
import net.helix.hlx.storage.Indexable;

import org.bouncycastle.util.encoders.Hex;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Objects;

public abstract class AbstractHash implements Hash, Serializable {
/**
* Base implementation of a hash object.
*/
public abstract class AbstractHash implements Hash, Serializable {

private byte[] data;
private Integer hashCode;
private final Object lock = new Object();
private ByteSafe byteSafe;

/**
* AbstractHash constructor for byte array with offset.
* @param source byte array
* @param sourceOffset offset length
* @param sourceSize length of byte array
* Empty Constructor for a placeholder hash identifier object. Creates a hash identifier object with no properties.
*/
public AbstractHash() {
}

/**
* Constructor for a hash object using a byte source array.
*
* @param source A byte array containing the source information in byte format
* @param sourceOffset The offset defining the start point for the hash object in the source
* @param sourceSize The size of the hash object that will be created
*/
public AbstractHash(byte[] source, int sourceOffset, int sourceSize) {
byte[] dest = new byte[SIZE_IN_BYTES];
System.arraycopy(source, sourceOffset, dest, 0, sourceSize - sourceOffset > source.length ? source.length - sourceOffset : sourceSize);
this.byteSafe = new ByteSafe(dest);
read(source, sourceOffset, sourceSize + sourceOffset > source.length ? source.length - sourceOffset : sourceSize);
}

/**
* Private method for reading in the byte array.
* @param src byte array
* @throws IllegalStateException in case bytes are already initialized.
* Assigns the input byte data to the hash object. Each hash object can only be initialized with data
* once. If the byte array is not null, an <tt>IllegalStateException</tt> is thrown.
*
* @param source A byte array containing the source bytes
*/
private void fullRead(byte[] src) {
if (src != null) {
@Override
public void read(byte[] source) {
if (source != null) {
synchronized (lock) {
if (byteSafe != null) {
if (data != null) {
throw new IllegalStateException("I cannot be initialized with data twice.");
}
byte[] dest = new byte[SIZE_IN_BYTES];
System.arraycopy(src, 0, dest, 0, Math.min(dest.length, src.length));
byteSafe = new ByteSafe(dest);
read(source, 0, source.length);
}
}
}

/**
* Counting number of zeros in a byte array
* Private method for reading in the byte array.
*
* @param source byte array
* @param offset The offset defining the start point for the hash object in the source
* @param length The length of the hash object that will be created
* @throws IllegalStateException in case bytes are already initialized
*/
private void read(byte[] source, int offset, int length) {
data = new byte[SIZE_IN_BYTES];
System.arraycopy(source, offset, data, 0, Math.min(data.length, length));
hashCode = Arrays.hashCode(data);
}

/**
* Checks if the hash object is storing a byte array. If there
* is no byte array present, a <tt>NullPointerException</tt> will be thrown.
*
* @return The stored byte array containing the hash values
*/
@Override
public byte[] bytes() {
if (data == null) {
throw new NullPointerException("No bytes initialized, please use read(byte[]) to read in the byte array");
}
return data;
}

/**
* Counting number of zeros in a byte array.
*
* @return zeros <code> int </code>
*/
@Override
public int trailingZeros() {
final byte[] bytes = bytes();
int index = SIZE_IN_BYTES;
Expand All @@ -61,9 +99,11 @@ public int trailingZeros() {
}

/**
* Counting number of zeros in a byte array
* Counting number of zeros in a byte array.
*
* @return zeros <code> int </code>
*/
@Override
public int leadingZeros() {
final byte[] bytes = bytes();
int index = 0;
Expand All @@ -77,7 +117,9 @@ public int leadingZeros() {


/**
* Check Equality of hash and <code> object </code> o
* Check equality of hash and <code> object </code> o.
*
* @param o the reference object with which to compare
* @return <code> boolean </code> equality
*/
@Override
Expand All @@ -93,40 +135,24 @@ public boolean equals(Object o) {
}

/**
* Get hash code of bytes from byteSafe.
* @return <code> boolean </code> equality
* Get hash code of bytes.
*
* @return a hash code value for this object
*/
@Override
public int hashCode() {
bytes();
return byteSafe.getHashcode();
}

/**
* Get bytes.
* @return <code> byte[] </code> bytes
*/
public byte[] bytes() {
ByteSafe safe = byteSafe;
if (safe == null) {
Objects.requireNonNull(byteSafe, "No bytes initialized, Please use fullRead(byte[]) to read in the byte array");
}
return safe.getData();
return hashCode;
}

/**
* Convert to hex string
* Convert to hex string.
*
* @return <code> string </code> string in hex representation
*/
public String toString() { return Hex.toHexString(bytes()); }

/**
* Reading byte array. @see #fullRead(byte[])
* @param src byte array
*/
@Override
public void read(byte[] src) {
fullRead(src);
public String toString() {
return Hex.toHexString(bytes());
}

@Override
Expand All @@ -141,6 +167,7 @@ public Indexable decremented() {

/**
* Get difference between Hash and <code> Indexable </code>. Returns 0 if they are equal.
*
* @param indexable
* @return <code> int </code> difference
*/
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/helix/hlx/model/AddressHash.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package net.helix.hlx.model;

public class AddressHash extends AbstractHash {

public AddressHash() {
}

protected AddressHash(byte[] bytes, int offset, int sizeInBytes) {
super(bytes, offset, sizeInBytes);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/helix/hlx/model/BundleHash.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package net.helix.hlx.model;

public class BundleHash extends AbstractHash {

public BundleHash() {
}

protected BundleHash(byte[] bytes, int offset, int sizeInBytes) {
super(bytes, offset, sizeInBytes);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/helix/hlx/model/BundleNonceHash.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package net.helix.hlx.model;

public class BundleNonceHash extends AbstractHash {

public BundleNonceHash() {
}

protected BundleNonceHash(byte[] tagBytes, int offset, int tagSizeInBytes) {
super(tagBytes, offset, tagSizeInBytes);
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/net/helix/hlx/model/Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.helix.hlx.crypto.Sha3;
import net.helix.hlx.storage.Indexable;
//import net.helix.hlx.utils.Converter;


/**
Expand All @@ -11,6 +10,11 @@
* and the inner classes <code> ByteSafe </code>
*/
public interface Hash extends Indexable, HashId {

/**
* Creates a null transaction hash with from a byte array of length {@value Sha3#HASH_LENGTH}.
* This is used as a reference hash for the genesis transaction.
*/
Hash NULL_HASH = HashFactory.TRANSACTION.create(new byte[Sha3.HASH_LENGTH]);

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/helix/hlx/model/HashFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Hash create(byte[] source) {
* @return the hash of the correct type
*/
public Hash create(Class<?> modelClass, byte[] source) {
return create(modelClass, source, 0, AbstractHash.SIZE_IN_BYTES);
return create(modelClass, source, 0, Hash.SIZE_IN_BYTES);
}

/**
Expand Down
Loading