Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/main/java/model/Entity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package model;

import java.io.Serializable;

import crypto.Sha3256Hasher;
import model.codec.EncodedEntity;
import model.crypto.Hash;
Expand All @@ -10,7 +12,7 @@
* Entity represents the unit of data model in LightChain. Everything meant to be sent over the network, stored
* in memory, or a database must extend the Entity class.
*/
public abstract class Entity {
public abstract class Entity implements Serializable {
/**
* Computes the collision resistant hash value of entity.
*
Expand Down
1 change: 1 addition & 0 deletions src/main/java/model/codec/EntityType.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class EntityType {
public static final String TYPE_VALIDATED_TRANSACTION = "type-lightchain-validated-transaction";
public static final String TYPE_VALIDATED_BLOCK = "type-lightchain-validated-block";
public static final String TYPE_ECDSA_SIGNATURE = "type-lightchain-ecdsa-signature";
public static final String TYPE_ACCOUNT = "type-lightchain-account";
}
4 changes: 3 additions & 1 deletion src/main/java/model/crypto/Hash.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package model.crypto;

import java.io.Serializable;

import model.lightchain.Identifier;

/**
* Represents abstract data type for the cryptographic hash function used in LightChain.
*/
public abstract class Hash {
public abstract class Hash implements Serializable {
/**
* Actual value of hash in bytes.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/model/crypto/Sha3256Hash.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package model.crypto;

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

import model.lightchain.Identifier;
Expand All @@ -8,7 +9,7 @@
* Represents SHA3-256 data type which extends abstract Hash data type for
* the cryptographic hash function used in LightChain.
*/
public class Sha3256Hash extends Hash {
public class Sha3256Hash extends Hash implements Serializable {
public static final int Size = 32;
private final byte[] hashBytes;

Expand Down
26 changes: 24 additions & 2 deletions src/main/java/model/lightchain/Account.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package model.lightchain;

import model.Entity;
import model.codec.EntityType;
import model.crypto.PublicKey;

/**
* Represents a LightChain account which is the constituent of the SnapShot.
*/
public class Account {
public class Account extends Entity {
/**
* Unique identifier of the account.
*/
Expand Down Expand Up @@ -72,4 +74,24 @@ public Identifier getLastBlockId() {
public int getStake() {
return this.stake;
}
}

/**
* Type of this entity.
*
* @return type of this entity.
*/
@Override
public String type() {
return EntityType.TYPE_ACCOUNT;
}

/**
* Identifier of this account.
*
* @return identifier of this Account
*/
@Override
public Identifier id() {
return this.identifier;
}
}
3 changes: 2 additions & 1 deletion src/main/java/model/lightchain/Identifier.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package model.lightchain;

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

import io.ipfs.multibase.Multibase;

/**
* Represents a 32-byte unique identifier for an entity. Normally is computed as the hash value of the entity.
*/
public class Identifier {
public class Identifier implements Serializable {
public static final int Size = 32;
private final byte[] value;

Expand Down
16 changes: 15 additions & 1 deletion src/main/java/modules/ads/AuthenticatedDataStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,23 @@
* Models AuthenticatedDataStructure (ADS) and a key-value store of entities supported with membership proofs.
*/
public interface AuthenticatedDataStructure {

/**
* Puts the given entity into the merkle tree and return AuthenticationEntity.
*
* @param e the entity to be put into the merkle tree
*
* @return AuthenticationEntity of the given entity
*/
AuthenticatedEntity put(Entity e);

/**
* Return the AuthenticationEntity of the given identifier.
*
* @param id the identifier whose AuthenticationEntity is to be returned
*
* @return the AuthenticationEntity of the given identifier
*/
AuthenticatedEntity get(Identifier id);

int size();
}
4 changes: 3 additions & 1 deletion src/main/java/modules/ads/AuthenticatedEntity.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package modules.ads;

import java.io.Serializable;

import model.Entity;

/**
* AuthenticatedEntity is a wrapper model around the Entity type that also contains a membership Merkle Proof for
* that entity against a root identifier.
*/
public abstract class AuthenticatedEntity extends Entity {
public abstract class AuthenticatedEntity extends Entity implements Serializable {
public abstract Entity getEntity();

public abstract MembershipProof getMembershipProof();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/modules/ads/MembershipProof.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package modules.ads;

import java.io.Serializable;
import java.util.ArrayList;

import model.crypto.Sha3256Hash;

/**
* Represents a Merkle Proof of membership against a certain root identifier.
*/
public interface MembershipProof {
public interface MembershipProof extends Serializable {
/**
* Root of the authenticated data structure that this proof belongs to.
*
Expand Down
80 changes: 79 additions & 1 deletion src/main/java/modules/ads/merkletree/MerkleNode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package modules.ads.merkletree;

import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

import com.google.gson.Gson;
import crypto.Sha3256Hasher;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import model.Entity;
Expand All @@ -8,7 +13,7 @@
/**
* A node in the Merkle tree.
*/
public class MerkleNode {
public class MerkleNode implements Serializable {
private static final Sha3256Hasher hasher = new Sha3256Hasher();
private MerkleNode left;
private MerkleNode right;
Expand Down Expand Up @@ -70,34 +75,67 @@ public MerkleNode(Sha3256Hash hash, MerkleNode left, MerkleNode right) {
this.hash = hash;
}

/**
* Returns the left child of the node.
*
* @return the left child of the node
*/
@SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "internal representation is intentionally returned")
public MerkleNode getLeft() {
return left;
}

/**
* Returns the right child of the node.
*
* @return the right child of the node
*/
@SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "internal representation is intentionally returned")
public MerkleNode getRight() {
return right;
}

/**
* Returns the parent node of the node.
*
* @return the parent node of the node
*/
@SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "internal representation is intentionally returned")
public MerkleNode getParent() {
return parent;
}

/**
* Sets the parent node of the node.
*/
@SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "parent is intentionally mutable externally")
public void setParent(MerkleNode parent) {
this.parent = parent;
}

/**
* Returns the hash of the node.
*
* @return the hash of the node
*/
public Sha3256Hash getHash() {
return hash;
}

/**
* Returns the isLeft boolean of the node.
*
* @return the isLeft boolean of the node
*/
public boolean isLeft() {
return isLeft;
}

/**
* Sets the isLeft of the node.
*
* @param isLeft isLeft boolean of the node
*/
public void setLeft(boolean isLeft) {
this.isLeft = isLeft;
}
Expand All @@ -114,4 +152,44 @@ public MerkleNode getSibling() {
return parent.getLeft();
}
}

/**
* Returns if o is equal to this node.
*
* @param o object to compare
*
* @return true if o is equal to this node
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MerkleNode that = (MerkleNode) o;
return hash.equals(that.hash);
}

/**
* Returns the hash code of the node.
*
* @return the hash code of the node
*/
@Override
public int hashCode() {
return Objects.hash(left, right, parent, isLeft, hash);
}

/**
* Returns the byte array representation of the node.
*
* @return the byte array representation of the node
*/
public byte[] getBytes() {
Gson gson = new Gson();
byte[] bytes = gson.toJson(this).getBytes(StandardCharsets.UTF_8);
return bytes;
}
}
3 changes: 2 additions & 1 deletion src/main/java/modules/ads/merkletree/MerkleProof.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package modules.ads.merkletree;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
Expand All @@ -11,7 +12,7 @@
/**
* A proof of membership in a Merkle tree.
*/
public class MerkleProof implements MembershipProof {
public class MerkleProof implements MembershipProof, Serializable {
private ArrayList<Sha3256Hash> path;
private final ArrayList<Boolean> isLeftNode;
private final Sha3256Hash root;
Expand Down
Loading