Skip to content

Commit

Permalink
Merge pull request #72 from bartdejonge1996/fix-proof-creation-and-me…
Browse files Browse the repository at this point in the history
…taknowledge

Fix proof creation and metaknowledge
  • Loading branch information
Bart de Jonge authored Feb 1, 2018
2 parents d6e397e + e72c892 commit 7c3dd5b
Show file tree
Hide file tree
Showing 64 changed files with 2,502 additions and 890 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ An in-depth explanation of the system is available as a technical report in the
Apply these configuration steps for every machine
- In the `SimulationMain`-class:
- Give each machine its sets of node by changing the `LOCAL_NODES_NUMBER`, `TOTAL_NODES_NUMBER`, `NODES_FROM_NUMBER` values
- Specify the parameters for the transaction sending behaviour of each node using the fields `MAX_BLOCKS_PENDING`, `INITIAL_SENDING_DELAY`, `SENDING_WAIT_TIME` and `REQUIRED_COMMITS`.
- Set `IS_MASTER` to `true` for the master machine and to `false` for all the others
- If the current machine is the master, also specify the simulation time in seconds.
- In the `Application`-class:
- Set `TRACKER_SERVER_ADDRESS` and `TRACKER_SERVER_PORT` to point to the server location

### Running
- Start the tracker server
- In folder `tracker-server` run `npm start`
- Start the master machine by calling the main method in `SimulationMain`.
- Start the other machines the same way as the master.
- A live visualization of the network can be seen by opening `<tracker-address>/demo` in a browser.

### Run Tests
From the root folder run `mvn test`.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
<archive>
<manifest>
<addClasspath>false</addClasspath>
<mainClass>nl.tudelft.blockchain.scaleoutdistributedledger.Main</mainClass>
<mainClass>nl.tudelft.blockchain.scaleoutdistributedledger.SimulationMain</mainClass>
</manifest>
<manifestEntries>
<Bundle-Name>${project.name}</Bundle-Name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Application {
@Getter
private LocalStore localStore;
private Thread executor;
private CancellableInfiniteRunnable transactionExecutable;
private CancellableInfiniteRunnable<LocalStore> transactionExecutable;
private final boolean isProduction;

@Getter
Expand Down Expand Up @@ -60,7 +60,6 @@ public void init(int nodePort, Block genesisBlock, Ed25519Key key, OwnNode ownNo

// Setup local store
localStore = new LocalStore(ownNode, this, genesisBlock, this.isProduction);
localStore.updateNodes();
localStore.initMainChain();

serverThread = new Thread(new SocketServer(nodePort, localStore));
Expand Down Expand Up @@ -133,7 +132,7 @@ public MainChain getMainChain() {
*/
public void finishTransactionSending() {
int nodeID = localStore.getOwnNode().getId();
transactionSender.stop();
//transactionSender.stop();
try {
transactionSender.waitUntilDone();
TrackerHelper.setRunning(nodeID, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package nl.tudelft.blockchain.scaleoutdistributedledger;

import java.util.logging.Level;

import nl.tudelft.blockchain.scaleoutdistributedledger.model.Proof;
import nl.tudelft.blockchain.scaleoutdistributedledger.utils.Log;
import nl.tudelft.blockchain.scaleoutdistributedledger.validation.ValidationException;

import java.io.IOException;
import java.util.logging.Level;

/**
* Helper class for communication.
*/
Expand All @@ -20,6 +21,8 @@ private CommunicationHelper() {
* @return true if the transaction was accepted, false otherwise
*/
public static boolean receiveTransaction(Proof proof, LocalStore localStore) {
Log.log(Level.FINE, "Received transaction: " + proof.getTransaction());

if (proof.getTransaction().getReceiver().getId() != localStore.getOwnNode().getId()) {
Log.log(Level.WARNING, "Received a transaction that isn't for us: " + proof.getTransaction());
return false;
Expand All @@ -28,13 +31,18 @@ public static boolean receiveTransaction(Proof proof, LocalStore localStore) {
try {
localStore.getVerification().validateNewMessage(proof, localStore);
} catch (ValidationException ex) {
Log.log(Level.WARNING, "Received an invalid transaction/proof.", ex);
Log.log(Level.WARNING, "Received an invalid transaction/proof " + proof.getTransaction() + ": " + ex.getMessage());
return false;
}

proof.applyUpdates();

Log.log(Level.INFO, "Received and validated transaction: " + proof.getTransaction());
Log.log(Level.FINE, "Transaction " + proof.getTransaction() + " is valid, applying updates...");
proof.applyUpdates(localStore);
try {
TrackerHelper.registerTransaction(proof);
} catch (IOException e) {
Log.log(Level.WARNING, "Transaction registration failed", e);
}

if (proof.getTransaction().getAmount() > 0) {
localStore.addUnspentTransaction(proof.getTransaction());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class LocalStore {
@Getter
private final Verification verification = new Verification();

@Getter
private final Set<Transaction> unspent = new HashSet<>();

@Getter
Expand Down Expand Up @@ -145,34 +144,47 @@ public Transaction getTransactionFromNode(int nodeId, int blockId, int transacti
throw new IllegalStateException("Transaction with id " + transactionId + " in block " + blockId + " from node " + nodeId + " not found.");
}

/**
* @return - a copy of the unspent transactions
*/
public Set<Transaction> getUnspent() {
synchronized (unspent) {
return new HashSet<>(unspent);
}
}

/**
* Adds the given transaction as unspent.
* @param transaction - the transaction to add
*/
public void addUnspentTransaction(Transaction transaction) {
if (!unspent.add(transaction)) return;

if (ownNode.equals(transaction.getReceiver())) {
availableMoney += transaction.getAmount();
}

if (ownNode.equals(transaction.getSender())) {
availableMoney += transaction.getRemainder();
synchronized (unspent) {
if (!unspent.add(transaction)) return;

if (ownNode.equals(transaction.getReceiver())) {
availableMoney += transaction.getAmount();
}

if (ownNode.equals(transaction.getSender())) {
availableMoney += transaction.getRemainder();
}
}
}

/**
* @param toRemove - the unspent transactions to remove
*/
public void removeUnspentTransactions(Collection<Transaction> toRemove) {
for (Transaction transaction : toRemove) {
if (!unspent.remove(transaction)) continue;

if (ownNode.equals(transaction.getReceiver())) {
availableMoney -= transaction.getAmount();
}
if (ownNode.equals(transaction.getSender())) {
availableMoney -= transaction.getRemainder();
synchronized (unspent) {
for (Transaction transaction : toRemove) {
if (!unspent.remove(transaction)) continue;

if (ownNode.equals(transaction.getReceiver())) {
availableMoney -= transaction.getAmount();
}
if (ownNode.equals(transaction.getSender())) {
availableMoney -= transaction.getRemainder();
}
}
}
}
Expand All @@ -197,7 +209,7 @@ public void initMainChain() {
private void normalizeGenesis() {
for (Transaction transaction : ownNode.getChain().getGenesisBlock().getTransactions()) {
Node receiver = getNode(transaction.getReceiver().getId());
if (receiver != transaction.getReceiver()) {
if (receiver != null && receiver != transaction.getReceiver()) {
transaction.setReceiver(receiver);
}
}
Expand Down

This file was deleted.

Loading

0 comments on commit 7c3dd5b

Please sign in to comment.