Commit b0fd3011 authored by Michel Schudel's avatar Michel Schudel

Begun node management.

parent 2b672a56
......@@ -10,6 +10,7 @@ buildscript {
}
}
plugins {
id 'org.springframework.boot' version '1.5.9.RELEASE'
}
......@@ -18,7 +19,7 @@ apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'nl.craftsmen.blockchain'
group = 'nl.craftsmen.craftsmencoinnode'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
......@@ -26,9 +27,17 @@ repositories {
mavenCentral()
}
if(!project.hasProperty("port"))
project.ext.set("port", 8080)
bootRun {
args += ["--server.port=${project.port}"]
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile 'commons-codec:commons-codec:1.10'
testCompile('org.springframework.boot:spring-boot-starter-test')
compile("org.springframework.boot:spring-boot-devtools")
}
......@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-bin.zip
......@@ -33,11 +33,11 @@ DEFAULT_JVM_OPTS=""
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn ( ) {
warn () {
echo "$*"
}
die ( ) {
die () {
echo
echo "$*"
echo
......@@ -155,7 +155,7 @@ if $cygwin ; then
fi
# Escape application args
save ( ) {
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
......
package nl.craftsmen.blockchain.blockchain;
package nl.craftsmen.blockchain.craftsmencoinnode;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import java.util.ArrayList;
import java.util.List;
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
public class Block {
private final long index;
private final long timestamp;
private final List<Transaction> transactions;
private final long proof;
private final String previousHash;
private long index;
private long timestamp;
private List<Transaction> transactions;
private long proof;
private String previousHash;
//default constructor needed for json deserialization
public Block() {
}
public Block(long index, long timestamp, List<Transaction> transactions, long proof, String previousHash) {
this.index = index;
......
package nl.craftsmen.blockchain.blockchain;
package nl.craftsmen.blockchain.craftsmencoinnode;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
......@@ -13,10 +13,12 @@ public class BlockChain {
private List<Block> chain = new ArrayList<>();
//default constructor, needed for json deserialization
public BlockChain() {
}
public Block createNewBlock(long proof, String previousHash, List<Transaction> transactionsToBeIncluded) {
Block block = new Block(getIndexOfLastBlock(), Instant
.now()
......@@ -30,6 +32,7 @@ public class BlockChain {
chain.add(block);
}
public long getIndexOfLastBlock() {
Optional<Block> block = getLastBlock();
return block
......
package nl.craftsmen.blockchain.craftsmencoinnode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
@Component
public final class BlockChainRepository {
private InstanceInfo instanceInfo;
private static final Logger LOGGER = LoggerFactory.getLogger(BlockChainRepository.class);
@Autowired
public BlockChainRepository(InstanceInfo instanceInfo) {
this.instanceInfo = instanceInfo;
}
public BlockChain loadBlockChain() {
try {
File file = createBlockchainFileForThisNode();
if (file.exists()) {
LOGGER.info("existing local blockchain found, loading...");
ObjectMapper objectMapper = new ObjectMapper();
BlockChain blockChain = objectMapper.readValue(file, BlockChain.class);
LOGGER.info("blockchain succesfully loaded!");
return blockChain;
} else {
return null;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void saveBlockChain(BlockChain blockChain) {
try {
File file = createBlockchainFileForThisNode();
ObjectMapper objectMapper = new ObjectMapper();
LOGGER.info("saving blockchain");
objectMapper.writeValue(file, blockChain);
LOGGER.info("blockchain succesfully saved!");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private File createBlockchainFileForThisNode() {
return new File(System.getProperty("user.dir"), createBlockChainFileName(instanceInfo.getNode()));
}
private String createBlockChainFileName(String node) {
return node.replace(".", "").replace(":", "") + ".json";
}
}
package nl.craftsmen.blockchain.blockchain;
package nl.craftsmen.blockchain.craftsmencoinnode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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;
@Component
......@@ -19,33 +19,44 @@ public class BlockchainLogic {
private BlockChain blockChain;
private Network network;
private BlockChainRepository blockChainRepository;
@Autowired
public BlockchainLogic(Network network) {
this.blockChain = new BlockChain();
public BlockchainLogic(Network network, BlockChainRepository blockChainRepository) {
this.network = network;
this.blockChainRepository = blockChainRepository;
}
@PostConstruct
public void initFirstBlock() {
blockChain.createNewBlock(100, "1", this.currentTransactions);
public BlockChain getBlockChain() {
if (this.blockChain == null) {
this.blockChain = blockChainRepository.loadBlockChain();
if (this.blockChain == null) {
this.blockChain = new BlockChain();
this.blockChain.createNewBlock(100, "1", this.currentTransactions);
blockChainRepository.saveBlockChain(this.blockChain);
}
}
return this.blockChain;
}
public BlockChain getBlockchain() {
return blockChain;
public BlockChain retrieveBlockChain() {
return getBlockChain();
}
public long newTransaction(Transaction transaction) {
transaction.init();
transaction.resolveTransactionId();
LOGGER.info("received new transaction: {}", transaction);
currentTransactions.add(transaction);
//notify the other nodes of this transaction
network.notifyNewTransaction(transaction);
return blockChain.getIndexOfLastBlock();
return getBlockChain().getIndexOfLastBlock();
}
public Block mine() {
long lastProof = blockChain.getLastBlock()
long lastProof = getBlockChain().getLastBlock()
.get()
.getProof();
long newProof = proofOfWork(lastProof);
......@@ -54,8 +65,9 @@ public class BlockchainLogic {
Transaction transaction = new Transaction("0", "this-node", BigDecimal.valueOf(1));
currentTransactions.add(transaction);
String previousHash = HashUtil.hash(blockChain.getLastBlock().get());
Block newBlock = blockChain.createNewBlock(newProof, previousHash, this.currentTransactions);
String previousHash = HashUtil.hash(getBlockChain().getLastBlock().get());
Block newBlock = getBlockChain().createNewBlock(newProof, previousHash, this.currentTransactions);
blockChainRepository.saveBlockChain(this.blockChain);
currentTransactions.clear();
network.notifyNewBlock(newBlock);
return newBlock;
......@@ -72,16 +84,10 @@ public class BlockchainLogic {
return currentProof;
}
public void resolveConflicts() {
List<BlockChain> otherChains = network.getBlockchains();
for (BlockChain otherChain : otherChains) {
if (otherChain.getBlockHeight() > blockChain.getBlockHeight() && otherChain.isValid()) {
if (otherChain.getBlockHeight() > getBlockChain().getBlockHeight() && otherChain.isValid()) {
this.blockChain = otherChain;
break;
}
......@@ -89,16 +95,17 @@ public class BlockchainLogic {
}
public void insertNewBlock(Block block) {
blockChain.addBlock(block);
getBlockChain().addBlock(block);
blockChainRepository.saveBlockChain(blockChain);
}
public boolean isValid() {
return blockChain.isValid();
return getBlockChain().isValid();
}
public WalletDto getWallet(String walletName) {
BigDecimal balance = BigDecimal.ZERO;
List<Transaction> unconfirmedTransactions = new ArrayList<>();
List<Transaction> confirmedTransactions = blockChain.getTransactionsFor(walletName);
List<Transaction> confirmedTransactions = getBlockChain().getTransactionsFor(walletName);
for (Transaction transaction : confirmedTransactions) {
if (transaction.isRecepient(walletName)) {
balance = balance.add(transaction.getAmount());
......@@ -120,4 +127,7 @@ public class BlockchainLogic {
}
public List<Transaction> getPendingTransactions() {
return Collections.unmodifiableList(this.currentTransactions);
}
}
package nl.craftsmen.blockchain.blockchain;
package nl.craftsmen.blockchain.craftsmencoinnode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BlockchainApplication {
public class CraftsmenCoinApplication {
public static void main(String[] args) {
SpringApplication.run(BlockchainApplication.class, args);
SpringApplication.run(CraftsmenCoinApplication.class, args);
}
}
package nl.craftsmen.blockchain.blockchain;
package nl.craftsmen.blockchain.craftsmencoinnode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class BlockchainRestController {
public class CraftsmenCoinRestController {
@Autowired
private BlockchainLogic blockchainLogic;
......@@ -31,11 +33,15 @@ public class BlockchainRestController {
}
@GetMapping("blockchain")
public BlockChain blockchain() {
return blockchainLogic.getBlockchain();
public BlockChain getBlockchain() {
return blockchainLogic.retrieveBlockChain();
}
@PostMapping
@GetMapping("pendingtransactions")
public List<Transaction> getPendingTransactions() {
return blockchainLogic.getPendingTransactions();
}
@PostMapping("newblock")
public void newBlock(@RequestBody Block block) {
blockchainLogic.insertNewBlock(block);
}
......@@ -49,6 +55,7 @@ public class BlockchainRestController {
public boolean isValid() {
return blockchainLogic.isValid();
}
@GetMapping("wallet/{walletId}")
public WalletDto getWallet(@PathVariable String walletId) {
return blockchainLogic.getWallet(walletId);
......
package nl.craftsmen.blockchain.blockchain;
package nl.craftsmen.blockchain.craftsmencoinnode;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
......
package nl.craftsmen.blockchain.blockchain;
package nl.craftsmen.blockchain.craftsmencoinnode;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.context.ApplicationListener;
......@@ -27,6 +27,10 @@ public class InstanceInfo implements ApplicationListener<EmbeddedServletContaine
}
}
/**
* Gives the host and port the application is running on.
* @return the node address, for example: 192.168.1.250:8080
*/
public String getNode() {
return node;
}
......
package nl.craftsmen.blockchain.blockchain;
package nl.craftsmen.blockchain.craftsmencoinnode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
......@@ -14,6 +16,10 @@ import java.util.stream.Collectors;
@Component
public class Network {
private static final Logger LOGGER = LoggerFactory.getLogger(Network.class);
private String bootstrapNode = "localhost:8080";
private Set<String> nodes = new HashSet<>();
private RestTemplate restTemplate = new RestTemplate();
......@@ -26,12 +32,14 @@ public class Network {
}
public void notifyNewTransaction(Transaction transaction) {
LOGGER.info("notifying the following nodes of transaction {}: {}", transaction, nodes);
for (String node : nodes) {
restTemplate.postForEntity(node + "/newtransaction", transaction, Object.class);
}
}
public void notifyNewBlock(Block newBlock) {
LOGGER.info("notifying the following nodes of new block {}: {}", newBlock, nodes);
for (String node : nodes) {
restTemplate.postForEntity(node + "newblock", newBlock, null);
}
......@@ -54,8 +62,9 @@ public class Network {
public List<BlockChain> getBlockchains() {
List<BlockChain> blockchainList = new ArrayList<>();
LOGGER.info("asking the following nodes for their blockhain: {}", nodes);
for (String node : nodes) {
BlockChain otherChain = restTemplate.getForObject(node + "blockchain", BlockChain.class);
BlockChain otherChain = restTemplate.getForObject(node + "craftsmencoinnode", BlockChain.class);
blockchainList.add(otherChain);
}
return blockchainList;
......
package nl.craftsmen.blockchain.blockchain;
package nl.craftsmen.blockchain.craftsmencoinnode;
import java.math.BigDecimal;
import java.util.UUID;
......@@ -22,7 +22,7 @@ public class Transaction {
}
public void init() {
public void resolveTransactionId() {
if (this.getId() == null) {
this.id = UUID.randomUUID();
}
......
package nl.craftsmen.blockchain.blockchain;
package nl.craftsmen.blockchain.craftsmencoinnode;
import java.math.BigDecimal;
import java.util.List;
......
/ __ \ / _| | / __ \ (_)
| / \/_ __ __ _| |_| |_ ___ _ __ ___ ___ _ __ | / \/ ___ _ _ __
| | | '__/ _` | _| __/ __| '_ ` _ \ / _ \ '_ \| | / _ \| | '_ \
| \__/\ | | (_| | | | |_\__ \ | | | | | __/ | | | \__/\ (_) | | | | |
\____/_| \__,_|_| \__|___/_| |_| |_|\___|_| |_|\____/\___/|_|_| |_|
\ No newline at end of file
package nl.craftsmen.blockchain.blockchain;
package nl.craftsmen.blockchain.craftsmencoinnode;
import org.junit.Test;
import org.junit.runner.RunWith;
......
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