Skip to content

Commit

Permalink
Merge pull request #74 from bartdejonge1996/improve-test-coverage
Browse files Browse the repository at this point in the history
Improve test coverage
  • Loading branch information
Taeir authored Feb 2, 2018
2 parents 2578aeb + 773799c commit a0539b4
Show file tree
Hide file tree
Showing 26 changed files with 1,012 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ public class Application {
public Application(boolean isProduction) {
this.isProduction = isProduction;
}

/**
* Constructor used for testing.
* @param localStore - the localStore to use
* @param serverThread - the serverThread to use
* @param transactionSender - the transactionSender to use
*/
protected Application(LocalStore localStore, Thread serverThread, TransactionSender transactionSender) {
this.localStore = localStore;
this.serverThread = serverThread;
this.transactionSender = transactionSender;
this.isProduction = false;
}

/**
* Initializes the application.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* Class to store information related to our own node.
*/
public class LocalStore {

@Getter
private final Application application;

Expand Down Expand Up @@ -104,13 +105,15 @@ public Transaction getTransactionFromNode(int nodeId, int transactionId) {
if (nodeId == Transaction.GENESIS_SENDER) {
// It's a genesis transaction
Block genesisBlock = this.ownNode.getChain().getGenesisBlock();
return genesisBlock.getTransactions().get(transactionId);
}

Node node = getNode(nodeId);
for (Block block : node.getChain().getBlocks()) {
for (Transaction transaction : block.getTransactions()) {
if (transaction.getNumber() == transactionId) return transaction;
if (transactionId < genesisBlock.getTransactions().size()) {
return genesisBlock.getTransactions().get(transactionId);
}
} else {
Node node = getNode(nodeId);
for (Block block : node.getChain().getBlocks()) {
for (Transaction transaction : block.getTransactions()) {
if (transaction.getNumber() == transactionId) return transaction;
}
}
}

Expand All @@ -128,17 +131,19 @@ public Transaction getTransactionFromNode(int nodeId, int blockId, int transacti
if (nodeId == Transaction.GENESIS_SENDER) {
// It's a genesis transaction
Block genesisBlock = this.ownNode.getChain().getGenesisBlock();
return genesisBlock.getTransactions().get(transactionId);
}

Node node = getNode(nodeId);
try {
Block block = node.getChain().getBlocks().get(blockId);
for (Transaction transaction : block.getTransactions()) {
if (transaction.getNumber() == transactionId) return transaction;
if (transactionId < genesisBlock.getTransactions().size()) {
return genesisBlock.getTransactions().get(transactionId);
}
} else {
Node node = getNode(nodeId);
try {
Block block = node.getChain().getBlocks().get(blockId);
for (Transaction transaction : block.getTransactions()) {
if (transaction.getNumber() == transactionId) return transaction;
}
} catch (IndexOutOfBoundsException ex) {
throw new IllegalStateException("Block with id " + blockId + " from node " + nodeId + " not found.");
}
} catch (IndexOutOfBoundsException ex) {
throw new IllegalStateException("Block with id " + blockId + " from node " + nodeId + " not found.");
}

throw new IllegalStateException("Transaction with id " + transactionId + " in block " + blockId + " from node " + nodeId + " not found.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package nl.tudelft.blockchain.scaleoutdistributedledger;

/**
* Exception for indicating that updating the running state of a node on the tracker failed.
*/
public class NodeStateChangeFailedException extends RuntimeException {
/**
* Constructor
* @param id - the ID of the node for which it failed
* @param running - the running state that it could not be updated to
*/
public NodeStateChangeFailedException(int id, boolean running) {
super("Failed to set the running state of node " + id + "to" + running);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
* Helper class for interacting with the tracker.
*/
public final class TrackerHelper {

private volatile static Queue<TransactionRegistration> transactionsToBeRegistered = new LinkedList<>();
private static final CloseableHttpClient client = HttpClientBuilder.create().build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Exception for indicating that the user doesn't have enough money.
*/
public class NotEnoughMoneyException extends RuntimeException {

private static final long serialVersionUID = -5115214404574584898L;

/**
Expand All @@ -13,10 +14,4 @@ public NotEnoughMoneyException() {
super();
}

/**
* @param msg - the message
*/
public NotEnoughMoneyException(String msg) {
super(msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public BlockAbstract calculateBlockAbstract() {
if (!(this.owner instanceof OwnNode)) {
throw new UnsupportedOperationException("You cannot calculate the block abstract of a block you do not own!");
}

// Convert attributes of abstract into an array of bytes, for the signature
// Important to keep the order of writings
byte[] attrInBytes;
Expand Down Expand Up @@ -151,10 +151,10 @@ public BlockAbstract calculateBlockAbstract() {
* @param localStore - the local store
*/
public synchronized void commit(LocalStore localStore) {
if (finalized) {
if (this.finalized) {
throw new IllegalStateException("This block has already been committed!");
}

Log.log(Level.FINER, "Committing block " + getNumber(), getOwner().getId());

//Commit to the main chain, and set the last committed block
Expand All @@ -169,7 +169,7 @@ public synchronized void commit(LocalStore localStore) {
prev = prev.getPreviousBlock();
}

finalized = true;
this.finalized = true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.io.Serializable;
import java.io.ByteArrayOutputStream;
import java.security.SignatureException;
import java.util.Arrays;
import java.util.Objects;
import java.util.logging.Level;

/**
Expand Down Expand Up @@ -105,18 +107,36 @@ public boolean checkBlockHash(Block block) {
*/
public boolean checkSignature(byte[] signatureKey) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(Utils.intToByteArray(this.ownerNodeId));
outputStream.write(Utils.intToByteArray(this.blockNumber));
outputStream.write(this.blockHash.getBytes());
byte[] attrInBytes = outputStream.toByteArray();
byte[] attrInBytes = BlockAbstract.calculateBytesForSignature(this.ownerNodeId, this.blockNumber, this.blockHash);

return Ed25519Key.verify(attrInBytes, this.signature, signatureKey);
} catch (IOException | SignatureException e) {
} catch (SignatureException e) {
return false;
}
}

/**
* Convert attributes of abstract into an array of bytes, for the signature.
* Important to keep the order of writings.
* @param ownerId - id of the owner
* @param blockNumber - number of the block
* @param hash - hash of the block
* @return array of bytes
*/
public static byte[] calculateBytesForSignature(int ownerId, int blockNumber, Sha256Hash hash) {
byte[] attrInBytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
outputStream.write(Utils.intToByteArray(ownerId));
outputStream.write(Utils.intToByteArray(blockNumber));
outputStream.write(hash.getBytes());
attrInBytes = outputStream.toByteArray();
} catch (IOException ex) {
throw new IllegalStateException("Unable to write to outputstream", ex);
}

return attrInBytes;
}

private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
}
Expand All @@ -126,4 +146,32 @@ private void readObject(ObjectInputStream stream) throws IOException,
stream.defaultReadObject();
}

@Override
public int hashCode() {
int hash = 7;
hash = 47 * hash + this.ownerNodeId;
hash = 47 * hash + this.blockNumber;
hash = 47 * hash + Objects.hashCode(this.blockHash);
hash = 47 * hash + Arrays.hashCode(this.signature);
hash = 47 * hash + Objects.hashCode(this.abstractHash);
return hash;
}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof BlockAbstract)) return false;

BlockAbstract other = (BlockAbstract) obj;
if (ownerNodeId != other.ownerNodeId) return false;
if (blockNumber != other.blockNumber) return false;
if (!blockHash.equals(other.blockHash)) return false;
if (!Arrays.equals(signature, other.signature)) return false;
if (this.abstractHash == null) {
if (other.abstractHash != null) return false;
} else if (other.abstractHash == null || this.abstractHash.equals(other.abstractHash)) return false;

return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ChainView(Chain chain, List<Block> updates, boolean trim) {
* true if this view is valid, false otherwise
*/
public boolean isValid() {
if (this.valid != null) return this.valid.booleanValue();
if (this.valid != null) return this.valid;

return checkIntegrity();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ public boolean verify(byte[] message, byte[] signature) throws SignatureExceptio
* @param message - array of bytes of the message
* @param privateKey - private ED25519 key
* @return signature of the message
* @throws java.lang.Exception - exception while signing
* @throws java.security.SignatureException - invalid signature
* @throws java.security.InvalidKeyException - invalid key
* @throws java.security.NoSuchAlgorithmException - no algorithm found for Ed25519
*/
public static byte[] sign(byte[] message, byte[] privateKey) throws Exception {
public static byte[] sign(byte[] message, byte[] privateKey) throws SignatureException, InvalidKeyException, NoSuchAlgorithmException {
// Get seed
byte[] seed = Arrays.copyOf(privateKey, 32);
// Sign
Expand All @@ -120,9 +122,11 @@ public static byte[] sign(byte[] message, byte[] privateKey) throws Exception {
* Sign an array of bytes with a private key.
* @param message - array of bytes of the message
* @return signature of the message
* @throws java.lang.Exception - exception while signing
* @throws java.security.SignatureException - invalid signature
* @throws java.security.InvalidKeyException - invalid key
* @throws java.security.NoSuchAlgorithmException - no algorithm found for Ed25519
*/
public byte[] sign(byte[] message) throws Exception {
public byte[] sign(byte[] message) throws SignatureException, InvalidKeyException, NoSuchAlgorithmException {
return sign(message, this.privateKey);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nl.tudelft.blockchain.scaleoutdistributedledger.model;

import java.security.SignatureException;
import lombok.Getter;
import lombok.Setter;

Expand Down Expand Up @@ -59,9 +60,9 @@ public Node(int id, byte[] publicKey, String address, int port) {
* @param message - message to be verified
* @param signature - signature of the message
* @return - the signature
* @throws Exception - See {@link Ed25519Key#verify(byte[], byte[], byte[])}
* @throws SignatureException - See {@link Ed25519Key#verify(byte[], byte[], byte[])}
*/
public boolean verify(byte[] message, byte[] signature) throws Exception {
public boolean verify(byte[] message, byte[] signature) throws SignatureException {
return Ed25519Key.verify(message, signature, this.publicKey);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Class to represent our own node.
*/
public class OwnNode extends Node {

/**
* Only used by the node himself.
* @return private key
Expand Down Expand Up @@ -44,4 +45,5 @@ public byte[] sign(byte[] message) throws Exception {
public String toString() {
return "(OwnNode) " + super.toString();
}

}
Empty file.
Loading

0 comments on commit a0539b4

Please sign in to comment.