Commit 8ab8f875 authored by Michel Schudel's avatar Michel Schudel

added more javadoc.

parent 15141349
...@@ -278,7 +278,7 @@ Schrijf een methode `void reachConsensus()` method op de `BlockChainService` bea ...@@ -278,7 +278,7 @@ Schrijf een methode `void reachConsensus()` method op de `BlockChainService` bea
- Ophalen blockchains van alle peers middels `network.retrieveBlockchainsFromPeers()` - Ophalen blockchains van alle peers middels `network.retrieveBlockchainsFromPeers()`
- Itereren: als er een blockchain is die beter is dan de huidige blockchain, dan de eigen blockchain overschrijven met de blockchain van de peer. - Itereren: als er een blockchain is die beter is dan de huidige blockchain, dan de eigen blockchain overschrijven met de blockchain van de peer.
- Alle transacties *die in de nieuwe blockchain* aanwezig zijn, verwijderen uit de transaction pool. (**note: ** in het geval van een collision lopen we hier wel het risico dat er transacties een putje ingaan. Het gaat hier wat te ver om hier iets aan te doen.) - Alle transacties *die in de nieuwe blockchain* aanwezig zijn, verwijderen uit de transaction pool, door implementatie en aanroep van de method `clearTransactions(..)` op de `TransactionPool`. (**note: ** in het geval van een collision lopen we hier wel het risico dat er transacties een putje ingaan. Het gaat hier wat te ver om hier iets aan te doen.)
- Blockchain daarna meteen opslaan middels `blockchainRepository.saveBlockchain(..)`. - Blockchain daarna meteen opslaan middels `blockchainRepository.saveBlockchain(..)`.
- Roep de `reachConsensus` method aan vanuit de `init()` method op de `BlockChainService` Dit moet uiteraard plaatsvinden *na* het initialiseren (of laden) van de eigen blockchain. - Roep de `reachConsensus` method aan vanuit de `init()` method op de `BlockChainService` Dit moet uiteraard plaatsvinden *na* het initialiseren (of laden) van de eigen blockchain.
......
...@@ -18,6 +18,7 @@ public class Blockchain { ...@@ -18,6 +18,7 @@ public class Blockchain {
/** /**
* Factory method to create a new blockchain. * Factory method to create a new blockchain.
*
* @return a new blockchain containing a genesis block. * @return a new blockchain containing a genesis block.
*/ */
public static Blockchain create() { public static Blockchain create() {
...@@ -34,8 +35,10 @@ public class Blockchain { ...@@ -34,8 +35,10 @@ public class Blockchain {
public Blockchain() { public Blockchain() {
} }
/** /**
* Mines a new block. * Mines a new block. Adds all the transactions,
* calculates the proof and puts a hash of the previous block in the new block.
*
* @param transactionsToBeIncluded the transactions the be included in the block. * @param transactionsToBeIncluded the transactions the be included in the block.
* @return the new block. * @return the new block.
*/ */
...@@ -43,13 +46,14 @@ public class Blockchain { ...@@ -43,13 +46,14 @@ public class Blockchain {
String previousHash = HashUtil.hash(this.getLastBlock()); String previousHash = HashUtil.hash(this.getLastBlock());
Block block = new Block(getIndexOfLastBlock() + 1, Instant Block block = new Block(getIndexOfLastBlock() + 1, Instant
.now() .now()
.toEpochMilli(), transactionsToBeIncluded, this.proofOfWork(),previousHash); .toEpochMilli(), transactionsToBeIncluded, this.proofOfWork(), previousHash);
chain.add(block); chain.add(block);
return block; return block;
} }
/** /**
* Adds block to the blockchain. * Adds block to the blockchain.
*
* @param block the block to be added. * @param block the block to be added.
*/ */
public void addBlock(Block block) { public void addBlock(Block block) {
...@@ -59,6 +63,7 @@ public class Blockchain { ...@@ -59,6 +63,7 @@ public class Blockchain {
/** /**
* Gets the index of the block height of the next block. * Gets the index of the block height of the next block.
*
* @return index of the block height of the next block. * @return index of the block height of the next block.
*/ */
public long getIndexOfNextBlock() { public long getIndexOfNextBlock() {
...@@ -72,6 +77,7 @@ public class Blockchain { ...@@ -72,6 +77,7 @@ public class Blockchain {
/** /**
* Gets the most recent block in the blockchain. * Gets the most recent block in the blockchain.
*
* @return the most recent block in the blockchain. * @return the most recent block in the blockchain.
*/ */
private Block getLastBlock() { private Block getLastBlock() {
...@@ -80,6 +86,7 @@ public class Blockchain { ...@@ -80,6 +86,7 @@ public class Blockchain {
/** /**
* Returns the blockheight. * Returns the blockheight.
*
* @return the blockheight. * @return the blockheight.
*/ */
private long getBlockHeight() { private long getBlockHeight() {
...@@ -88,6 +95,7 @@ public class Blockchain { ...@@ -88,6 +95,7 @@ public class Blockchain {
/** /**
* Gets all transactions in the blockchain. * Gets all transactions in the blockchain.
*
* @return all transactions in the blockchain. * @return all transactions in the blockchain.
*/ */
public List<Transaction> getAllTransactions() { public List<Transaction> getAllTransactions() {
...@@ -102,6 +110,7 @@ public class Blockchain { ...@@ -102,6 +110,7 @@ public class Blockchain {
/** /**
* Gets transactions in the blockchain for a specific wallet. * Gets transactions in the blockchain for a specific wallet.
*
* @param walletId the walletId. * @param walletId the walletId.
* @return transactions in the blockchain for a specific wallet. * @return transactions in the blockchain for a specific wallet.
*/ */
...@@ -119,6 +128,8 @@ public class Blockchain { ...@@ -119,6 +128,8 @@ public class Blockchain {
/** /**
* Checks if this blockchain is valid. * Checks if this blockchain is valid.
* A blockchain is valid if all the proofs are correct and all previous hashes point to the hash of the last block.
*
* @return if this blockchain is valid. * @return if this blockchain is valid.
*/ */
public boolean isValid() { public boolean isValid() {
...@@ -135,6 +146,8 @@ public class Blockchain { ...@@ -135,6 +146,8 @@ public class Blockchain {
/** /**
* Checks if a new block is valid with respect to this blockchain. * Checks if a new block is valid with respect to this blockchain.
* A new block is valid if the proof of the new block is correct and the previoushash matches the hash of the block before.
*
* @param newBlock the new block. * @param newBlock the new block.
* @return true if the new block is valid, false otherwise. * @return true if the new block is valid, false otherwise.
*/ */
...@@ -145,12 +158,13 @@ public class Blockchain { ...@@ -145,12 +158,13 @@ public class Blockchain {
/** /**
* Find the proof of work for the next block. * Find the proof of work for the next block.
*/ * Simple Proof of Work Algorithm:
* - Find a number p' such that hash(p + p') contains leading 4 zeroes, where p is the previous p'
* - p is the previous proof, and p' is the new proof
*/
private long proofOfWork() { private long proofOfWork() {
long lastProof = getLastBlock().getProof(); long lastProof = getLastBlock().getProof();
/*Simple Proof of Work Algorithm:
- Find a number p' such that hash(pp') contains leading 4 zeroes, where p is the previous p'
- p is the previous proof, and p' is the new proof*/
long currentProof = 0; long currentProof = 0;
while (!validProof(lastProof, currentProof)) { while (!validProof(lastProof, currentProof)) {
currentProof++; currentProof++;
...@@ -158,7 +172,7 @@ public class Blockchain { ...@@ -158,7 +172,7 @@ public class Blockchain {
return currentProof; return currentProof;
} }
private boolean validProof( long previousProof, long currentProof) { private boolean validProof(long previousProof, long currentProof) {
String hash = HashUtil.createHash(previousProof + "" + currentProof); String hash = HashUtil.createHash(previousProof + "" + currentProof);
return hash.startsWith("0000"); return hash.startsWith("0000");
} }
......
...@@ -66,6 +66,7 @@ public class BlockchainService { ...@@ -66,6 +66,7 @@ public class BlockchainService {
/** /**
* Retrieves the blockchain of this node for API usage. * Retrieves the blockchain of this node for API usage.
*
* @return the blockchain of this node. * @return the blockchain of this node.
*/ */
public Blockchain retrieveBlockChain() { public Blockchain retrieveBlockChain() {
...@@ -73,14 +74,15 @@ public class BlockchainService { ...@@ -73,14 +74,15 @@ public class BlockchainService {
} }
/** /**
* Creates a new transaction. * Creates a new transaction and adds it to the transaction pool.
* @param from where the money comes from *
* @param to where the money should be sent * @param from where the money comes from
* @param to where the money should be sent
* @param amount how much * @param amount how much
* @return the block height of the block that this transaction will be mined in. * @return the block height of the block that this transaction will be mined in.
*/ */
public long createTransaction(String from, String to, BigDecimal amount) { public long createTransaction(String from, String to, BigDecimal amount) {
LOGGER.info("creating new transaction: from: {}, to: {}, amount: {}",from, to, amount); LOGGER.info("creating new transaction: from: {}, to: {}, amount: {}", from, to, amount);
Transaction transaction = new Transaction(from, to, amount); Transaction transaction = new Transaction(from, to, amount);
transactionPool.addTransaction(transaction); transactionPool.addTransaction(transaction);
network.notifyPeersOfNewTransaction(transaction, instanceInfo.getNode()); network.notifyPeersOfNewTransaction(transaction, instanceInfo.getNode());
...@@ -89,6 +91,7 @@ public class BlockchainService { ...@@ -89,6 +91,7 @@ public class BlockchainService {
/** /**
* Gets pending transactions for API usage. * Gets pending transactions for API usage.
*
* @return the pending transactions from the transaction pool. * @return the pending transactions from the transaction pool.
*/ */
public Set<Transaction> getPendingTransactions() { public Set<Transaction> getPendingTransactions() {
...@@ -96,7 +99,8 @@ public class BlockchainService { ...@@ -96,7 +99,8 @@ public class BlockchainService {
} }
/** /**
* Mines a new block. * Mines a new block with all transactions in the transaction pool.
*
* @return the new block that has just been mined. * @return the new block that has just been mined.
*/ */
public Block mine() { public Block mine() {
...@@ -115,6 +119,7 @@ public class BlockchainService { ...@@ -115,6 +119,7 @@ public class BlockchainService {
/** /**
* Establishes consensus over the current state of the blockchain. * Establishes consensus over the current state of the blockchain.
* If another node has a blockchain that is 'better', the blockchain of the current node will be replaced.
*/ */
private void reachConsensus() { private void reachConsensus() {
List<Blockchain> otherChains = network.retrieveBlockchainsFromPeers(); List<Blockchain> otherChains = network.retrieveBlockchainsFromPeers();
...@@ -132,9 +137,11 @@ public class BlockchainService { ...@@ -132,9 +137,11 @@ public class BlockchainService {
} }
/** /**
* Adds a newly received transaction to the pool and propagates it further into the network. * Adds a newly received transaction that has not yet been received
* to the transaction pool and propagates it further into the network.
*
* @param transaction the new transaction. * @param transaction the new transaction.
* @param sourcePeer the peer that sent the notification. * @param sourcePeer the peer that sent the notification.
*/ */
public void newTransactionReceived(Transaction transaction, String sourcePeer) { public void newTransactionReceived(Transaction transaction, String sourcePeer) {
LOGGER.info("received transaction: {} from source peer {}", transaction, sourcePeer); LOGGER.info("received transaction: {} from source peer {}", transaction, sourcePeer);
...@@ -146,8 +153,9 @@ public class BlockchainService { ...@@ -146,8 +153,9 @@ public class BlockchainService {
} }
/** /**
* Adds a newly received block to the blockchain and propagates it further into the network. * Adds a newly received block (if valid) to the blockchain and propagates it further into the network.
* @param block the new block. *
* @param block the new block.
* @param sourcePeer the peer that sent the notification. * @param sourcePeer the peer that sent the notification.
*/ */
public void newBlockReceived(Block block, String sourcePeer) { public void newBlockReceived(Block block, String sourcePeer) {
...@@ -157,7 +165,7 @@ public class BlockchainService { ...@@ -157,7 +165,7 @@ public class BlockchainService {
this.blockchain.addBlock(block); this.blockchain.addBlock(block);
this.network.notifyPeersOfNewBlock(block, sourcePeer); this.network.notifyPeersOfNewBlock(block, sourcePeer);
blockchainRepository.saveBlockChain(blockchain); blockchainRepository.saveBlockChain(blockchain);
clearConfirmedTransactions(); clearConfirmedTransactions();
} else { } else {
LOGGER.info("block is not valid with respect to my blockchain, discarding."); LOGGER.info("block is not valid with respect to my blockchain, discarding.");
...@@ -172,6 +180,7 @@ public class BlockchainService { ...@@ -172,6 +180,7 @@ public class BlockchainService {
/** /**
* Returns if the blockchain on this node is valid. * Returns if the blockchain on this node is valid.
*
* @return if the blockchain on this node is valid. * @return if the blockchain on this node is valid.
*/ */
public boolean isValid() { public boolean isValid() {
......
...@@ -42,8 +42,8 @@ public class TransactionPool { ...@@ -42,8 +42,8 @@ public class TransactionPool {
* @param transactions the supplied transactions list. * @param transactions the supplied transactions list.
*/ */
public void clearTransactions(List<Transaction> transactions) { public void clearTransactions(List<Transaction> transactions) {
LOGGER.info("the following transactions have been purged: {}", transactions);
this.transactions.removeAll(transactions); this.transactions.removeAll(transactions);
LOGGER.info("the following transactions have been purged: {}", transactions);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment