Commit 4daaf707 authored by Michel Schudel's avatar Michel Schudel

Verfijning van de opdracht.

parent ff8f0ad2
......@@ -2,10 +2,11 @@ package nl.craftsmen.blockchain.craftscoinnode;
import nl.craftsmen.blockchain.craftscoinnode.blockchain.Block;
import nl.craftsmen.blockchain.craftscoinnode.blockchain.BlockChain;
import nl.craftsmen.blockchain.craftscoinnode.blockchain.BlockchainManager;
import nl.craftsmen.blockchain.craftscoinnode.blockchain.BlockchainService;
import nl.craftsmen.blockchain.craftscoinnode.network.Network;
import nl.craftsmen.blockchain.craftscoinnode.transaction.Transaction;
import nl.craftsmen.blockchain.craftscoinnode.wallet.WalletDto;
import nl.craftsmen.blockchain.craftscoinnode.wallet.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
......@@ -15,7 +16,10 @@ import java.util.Set;
public class CraftsCoinRestController {
@Autowired
private BlockchainManager blockchainManager;
private BlockchainService blockchainService;
@Autowired
private WalletService walletService;
@Autowired
private Network network;
......@@ -28,7 +32,7 @@ public class CraftsCoinRestController {
*/
@PostMapping("/api/newtransaction")
public String newTransaction(@RequestHeader(value = "peer", required = false) String peer, @RequestBody Transaction transaction) {
long l = blockchainManager.newTransaction(transaction, peer);
long l = blockchainService.newTransaction(transaction, peer);
return "{\"message\": \"new transaction will be added to block " + l + "\"}";
}
......@@ -38,7 +42,7 @@ public class CraftsCoinRestController {
*/
@GetMapping("/api/pendingtransactions")
public Set<Transaction> getPendingTransactions() {
return blockchainManager.getPendingTransactions();
return blockchainService.getPendingTransactions();
}
/**
......@@ -47,7 +51,7 @@ public class CraftsCoinRestController {
*/
@PostMapping("/api/mine")
public Block mine() {
return blockchainManager.mine();
return blockchainService.mine();
}
/**
......@@ -56,7 +60,7 @@ public class CraftsCoinRestController {
*/
@GetMapping("/api/blockchain")
public BlockChain getBlockchain() {
return blockchainManager.retrieveBlockChain();
return blockchainService.retrieveBlockChain();
}
/**
......@@ -85,7 +89,7 @@ public class CraftsCoinRestController {
*/
@PostMapping("/api/newblock")
public void newBlock(@RequestHeader(value = "peer", required = false) String peer, @RequestBody Block block) {
blockchainManager.newBlockReceived(block, peer);
blockchainService.newBlockReceived(block, peer);
}
/**
......@@ -94,7 +98,7 @@ public class CraftsCoinRestController {
*/
@GetMapping("/api/valid")
public boolean isValid() {
return blockchainManager.isValid();
return blockchainService.isValid();
}
/**
......@@ -104,7 +108,7 @@ public class CraftsCoinRestController {
*/
@GetMapping("/api/wallet/{walletId}")
public WalletDto getWallet(@PathVariable String walletId) {
return blockchainManager.getWallet(walletId);
return walletService.getWallet(walletId);
}
}
......@@ -12,15 +12,15 @@ import java.io.File;
import java.io.IOException;
@Component
final class BlockChainRepository {
final class BlockchainRepository {
private InstanceInfo instanceInfo;
private static final Logger LOGGER = LoggerFactory.getLogger(BlockChainRepository.class);
private static final Logger LOGGER = LoggerFactory.getLogger(BlockchainRepository.class);
@Autowired
BlockChainRepository(InstanceInfo instanceInfo) {
BlockchainRepository(InstanceInfo instanceInfo) {
this.instanceInfo = instanceInfo;
}
......
package nl.craftsmen.blockchain.craftscoinnode.blockchain;
import nl.craftsmen.blockchain.craftscoinnode.network.Network;
import nl.craftsmen.blockchain.craftscoinnode.transaction.Transaction;
import nl.craftsmen.blockchain.craftscoinnode.transaction.TransactionPool;
import nl.craftsmen.blockchain.craftscoinnode.wallet.WalletDto;
import nl.craftsmen.blockchain.craftscoinnode.network.Network;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -12,30 +11,29 @@ import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
@Component
public class BlockchainManager {
public class BlockchainService {
private static final Logger LOGGER = LoggerFactory.getLogger(BlockchainManager.class);
private static final Logger LOGGER = LoggerFactory.getLogger(BlockchainService.class);
private static final BigDecimal CRAFTSCOIN_MINING_REWARD = BigDecimal.TEN;
private BlockChain blockChain;
private Network network;
private BlockChainRepository blockChainRepository;
private BlockchainRepository blockchainRepository;
private TransactionPool transactionPool;
@Value("${miningWalletId}")
private String miningWalletId;
@Autowired
public BlockchainManager(Network network, BlockChainRepository blockChainRepository, TransactionPool transactionPool) {
public BlockchainService(Network network, BlockchainRepository blockchainRepository, TransactionPool transactionPool) {
this.network = network;
this.blockChainRepository = blockChainRepository;
this.blockchainRepository = blockchainRepository;
this.transactionPool = transactionPool;
}
......@@ -49,10 +47,10 @@ public class BlockchainManager {
private void initializeBlockchain() {
if (this.blockChain == null) {
this.blockChain = blockChainRepository.loadBlockChain();
this.blockChain = blockchainRepository.loadBlockChain();
if (this.blockChain == null) {
this.blockChain = BlockChain.create();
blockChainRepository.saveBlockChain(this.blockChain);
blockchainRepository.saveBlockChain(this.blockChain);
}
}
}
......@@ -83,17 +81,14 @@ public class BlockchainManager {
//reward the miner
Transaction transaction = new Transaction("0", miningWalletId, CRAFTSCOIN_MINING_REWARD);
transactionPool.addTransaction(transaction);
String previousHash = HashUtil.hash(this.blockChain.getLastBlock());
Block newBlock = this.blockChain.createNewBlock(newProof, previousHash, transactionPool.getCurrentTransactions());
blockChainRepository.saveBlockChain(this.blockChain);
blockchainRepository.saveBlockChain(this.blockChain);
transactionPool.clearTransactions();
network.notifyPeersOfNewBlock(newBlock, null);
return newBlock;
}
private void reachConsensus() {
List<BlockChain> otherChains = network.retrieveBlockchainsFromPeers();
LOGGER.info("blockchains from peers received. Checking...");
......@@ -103,7 +98,7 @@ public class BlockchainManager {
LOGGER.info("received a blockchain that is better than my currect chain. Replacing current chain.");
this.blockChain = otherChain;
clearConfirmedTransactions();
blockChainRepository.saveBlockChain(blockChain);
blockchainRepository.saveBlockChain(blockChain);
break;
}
}
......@@ -115,7 +110,7 @@ public class BlockchainManager {
LOGGER.info("block is valid, adding it to the blockchain.");
this.blockChain.addBlock(block);
this.network.notifyPeersOfNewBlock(block, sourcePeer);
blockChainRepository.saveBlockChain(blockChain);
blockchainRepository.saveBlockChain(blockChain);
clearConfirmedTransactions();
} else {
......@@ -132,32 +127,6 @@ public class BlockchainManager {
return this.blockChain.isValid();
}
public WalletDto getWallet(String walletName) {
BigDecimal balance = BigDecimal.ZERO;
List<Transaction> unconfirmedTransactions = new ArrayList<>();
List<Transaction> confirmedTransactions = this.blockChain.getTransactionsFor(walletName);
for (Transaction transaction : confirmedTransactions) {
if (transaction.isRecepient(walletName)) {
balance = balance.add(transaction.getAmount());
} else if (transaction.isSender(walletName)) {
balance = balance.subtract(transaction.getAmount());
}
}
//unconfirmed transactions
for (Transaction transaction : transactionPool.getCurrentTransactions()) {
if (!confirmedTransactions.contains(transaction) && transaction.getTo().equals(walletName)) {
balance = balance.add(transaction.getAmount());
unconfirmedTransactions.add(transaction);
} else if (!confirmedTransactions.contains(transaction) && transaction.getFrom().equals(walletName)) {
balance = balance.subtract(transaction.getAmount());
unconfirmedTransactions.add(transaction);
}
}
return new WalletDto(balance, confirmedTransactions, unconfirmedTransactions);
}
public Set<Transaction> getPendingTransactions() {
return Collections.unmodifiableSet(transactionPool.getCurrentTransactions());
}
......
package nl.craftsmen.blockchain.craftscoinnode.transaction;
import nl.craftsmen.blockchain.craftscoinnode.blockchain.BlockchainManager;
import nl.craftsmen.blockchain.craftscoinnode.blockchain.BlockchainService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
......@@ -13,7 +13,7 @@ import java.util.Set;
@Component
public class TransactionPool {
private static final Logger LOGGER = LoggerFactory.getLogger(BlockchainManager.class);
private static final Logger LOGGER = LoggerFactory.getLogger(BlockchainService.class);
private Set<Transaction> currentTransactions = new HashSet<>();
......
package nl.craftsmen.blockchain.craftscoinnode.wallet;
import nl.craftsmen.blockchain.craftscoinnode.blockchain.BlockchainService;
import nl.craftsmen.blockchain.craftscoinnode.transaction.Transaction;
import nl.craftsmen.blockchain.craftscoinnode.transaction.TransactionPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Component
public class WalletService {
private BlockchainService blockchainService;
private TransactionPool transactionPool;
@Autowired
public WalletService(BlockchainService blockchainService, TransactionPool transactionPool) {
this.blockchainService = blockchainService;
this.transactionPool = transactionPool;
}
public WalletDto getWallet(String walletName) {
BigDecimal balance = BigDecimal.ZERO;
List<Transaction> unconfirmedTransactions = new ArrayList<>();
List<Transaction> confirmedTransactions = blockchainService.retrieveBlockChain().getTransactionsFor(walletName);
for (Transaction transaction : confirmedTransactions) {
if (transaction.isRecepient(walletName)) {
balance = balance.add(transaction.getAmount());
} else if (transaction.isSender(walletName)) {
balance = balance.subtract(transaction.getAmount());
}
}
//unconfirmed transactions
for (Transaction transaction : transactionPool.getCurrentTransactions()) {
if (!confirmedTransactions.contains(transaction) && transaction.getTo().equals(walletName)) {
balance = balance.add(transaction.getAmount());
unconfirmedTransactions.add(transaction);
} else if (!confirmedTransactions.contains(transaction) && transaction.getFrom().equals(walletName)) {
balance = balance.subtract(transaction.getAmount());
unconfirmedTransactions.add(transaction);
}
}
return new WalletDto(balance, confirmedTransactions, unconfirmedTransactions);
}
}
......@@ -14,7 +14,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BlockchainManagerApplicationTests {
public class BlockchainServiceApplicationTests {
@Autowired
......
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