Commit bf40f730 authored by Schudel, MJ (Michel)'s avatar Schudel, MJ (Michel)

Moved more code around to keep it simple

parent 0787ce2e
......@@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.Set;
@RestController
public class CraftsCoinRestController {
public class ApiRestController {
private final BlockchainService blockchainService;
......@@ -22,7 +22,7 @@ public class CraftsCoinRestController {
private final Network network;
@Autowired
public CraftsCoinRestController(BlockchainService blockchainService, WalletService walletService, Network network) {
public ApiRestController(BlockchainService blockchainService, WalletService walletService, Network network) {
this.blockchainService = blockchainService;
this.walletService = walletService;
this.network = network;
......@@ -66,15 +66,6 @@ public class CraftsCoinRestController {
return blockchainService.retrieveBlockChain();
}
/**
* Called when the network notifies this node of a new peer.
* @param peer the new peer
* @return the list of known peers by this node (excluding the peer that just arrived)
*/
@PostMapping("/api/registernode")
public Set<String> registerPeer(@RequestBody String peer) {
return network.registerNewPeer(peer);
}
/**
* Returns the list of known peers for this node.
......@@ -85,26 +76,6 @@ public class CraftsCoinRestController {
return network.getPeers();
}
/**
* Called when the network notifies this node of a new block.
* @param peer the peer that initiated the request.
* @param block the new block.
*/
@PostMapping("/api/addblock")
public void newBlock(@RequestHeader(value = "peer", required = false) String peer, @RequestBody Block block) {
blockchainService.newBlockReceived(block, peer);
}
/**
* Processes a transaction received from a peer in the network.
* @param peer the peer (optional)
* @param transaction the received transaction
*/
@PostMapping("/api/addtransaction")
public void addTransaction(@RequestHeader(value = "peer", required = false) String peer, @RequestBody Transaction transaction) {
blockchainService.newTransactionReceived(transaction, peer);
}
/**
* Checks the validity of the blockchain of this node
* @return true or false
......
......@@ -34,6 +34,7 @@ class ApplicationStartedListener implements ApplicationListener<ApplicationStart
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
LOGGER.info("======== Application started ==============");
Integer port = environment.getProperty("local.server.port", Integer.class);
LOGGER.info("running port is: " + port);
instanceInfo.setPort(port);
......
......@@ -28,6 +28,10 @@ public class Network {
private static final Logger LOGGER = LoggerFactory.getLogger(Network.class);
static final String API_REGISTERNODE = "/api/registernode";
static final String API_ADDBLOCK = "/api/addblock";
static final String API_ADDTRANSACTION = "/api/addtransaction";
private final String bootstrapPeerHost;
private final int bootstrapPeerPort;
......@@ -91,7 +95,7 @@ public class Network {
Set<String> newPeers = new HashSet<>();
Set<String> peersToRemove = new HashSet<>();
for (String peer : peersWithout(newPeer)) {
ResponseEntity<Set<String>> listResponseEntity = post(peer, "/registernode", newPeer, new ParameterizedTypeReference<Set<String>>() {
ResponseEntity<Set<String>> listResponseEntity = post(peer, API_REGISTERNODE, newPeer, new ParameterizedTypeReference<Set<String>>() {
}, peersToRemove);
if (listResponseEntity != null) {
Set<String> list = listResponseEntity.getBody();
......@@ -108,7 +112,7 @@ public class Network {
}
private void registerThroughBootstrapNode() {
ResponseEntity<Set<String>> listResponseEntity = post(createHostEndpoint(), "/registernode", instanceInfo.getNode(), new ParameterizedTypeReference<Set<String>>() {
ResponseEntity<Set<String>> listResponseEntity = post(createHostEndpoint(), API_REGISTERNODE, instanceInfo.getNode(), new ParameterizedTypeReference<Set<String>>() {
}, new HashSet<>());
if (listResponseEntity != null) {
Set<String> list = listResponseEntity.getBody();
......@@ -128,7 +132,7 @@ public class Network {
* @param newPeer the new peer.
* @return a list of known peers so the new peer can add them to its peers list.
*/
public Set<String> registerNewPeer(String newPeer) {
Set<String> registerNewPeer(String newPeer) {
LOGGER.info("a new peer has connected to the network: {}", newPeer);
if (!peers.getPeers().contains(newPeer)) {
LOGGER.info("peer {} is not previously known to this node. Peer registration will be forwarded to known peer: {}", newPeer, peers);
......@@ -154,7 +158,7 @@ public class Network {
* @param sourcePeer the peer that sent the transsaction.
*/
public void notifyPeersOfNewTransaction(Transaction transaction, String sourcePeer) {
notifyPeersOf("transaction", transaction, sourcePeer);
notifyPeersOf(API_ADDTRANSACTION, transaction, sourcePeer);
}
/**
......@@ -164,16 +168,16 @@ public class Network {
* @param sourcePeer the peer that sent the new block.
*/
public void notifyPeersOfNewBlock(Block newBlock, String sourcePeer) {
notifyPeersOf("block", newBlock, sourcePeer);
notifyPeersOf(API_ADDBLOCK, newBlock, sourcePeer);
}
private <T> void notifyPeersOf(String artifactType, T t, String sourcePeer) {
private <T> void notifyPeersOf(String operation, T t, String sourcePeer) {
Set<String> peersWithoutSourcePeer = peersWithout(sourcePeer);
LOGGER.info("notifying the following peers of new {} {}: {}", artifactType, t, peersWithoutSourcePeer);
LOGGER.info("notifying the following peers of new {} {}: {}", t.getClass().getSimpleName(), t, peersWithoutSourcePeer);
Set<String> peersToRemove = new HashSet<>();
for (String node : peersWithoutSourcePeer) {
HttpEntity<T> entity = new HttpEntity<>(t, createHttpHeaders());
post(node, "/add" + artifactType, entity, new ParameterizedTypeReference<Object>() {
post(node, operation, entity, new ParameterizedTypeReference<Object>() {
}, peersToRemove);
}
peers.getPeers().removeAll(peersToRemove);
......@@ -221,7 +225,7 @@ public class Network {
} else {
httpEntity = new HttpEntity<>(object);
}
return restTemplate.exchange("http://" + peer + "/api" + action, HttpMethod.POST, httpEntity, typeRef);
return restTemplate.exchange("http://" + peer + action, HttpMethod.POST, httpEntity, typeRef);
} catch (ResourceAccessException e) {
LOGGER.warn("peer {} not found, removing from peer list.", peer);
peersToRemove.add(peer);
......
package nl.craftsmen.blockchain.craftscoinnode.network;
import nl.craftsmen.blockchain.craftscoinnode.blockchain.Block;
import nl.craftsmen.blockchain.craftscoinnode.blockchain.BlockchainService;
import nl.craftsmen.blockchain.craftscoinnode.network.Network;
import nl.craftsmen.blockchain.craftscoinnode.transaction.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import java.util.Set;
import static nl.craftsmen.blockchain.craftscoinnode.network.Network.API_ADDBLOCK;
import static nl.craftsmen.blockchain.craftscoinnode.network.Network.API_ADDTRANSACTION;
import static nl.craftsmen.blockchain.craftscoinnode.network.Network.API_REGISTERNODE;
@RestController
public class NetworkRestController {
private final BlockchainService blockchainService;
private final Network network;
@Autowired
public NetworkRestController(BlockchainService blockchainService, Network network) {
this.blockchainService = blockchainService;
this.network = network;
}
/**
* Called when the network notifies this node of a new peer.
*
* @param peer the new peer
* @return the list of known peers by this node (excluding the peer that just arrived)
*/
@PostMapping(API_REGISTERNODE)
public Set<String> registerPeer(@RequestBody String peer) {
return network.registerNewPeer(peer);
}
/**
* Called when the network notifies this node of a new block.
*
* @param peer the peer that initiated the request.
* @param block the new block.
*/
@PostMapping(API_ADDBLOCK)
public void newBlock(@RequestHeader(value = "peer", required = false) String peer, @RequestBody Block block) {
blockchainService.newBlockReceived(block, peer);
}
/**
* Processes a transaction received from a peer in the network.
*
* @param peer the peer (optional)
* @param transaction the received transaction
*/
@PostMapping(API_ADDTRANSACTION)
public void addTransaction(@RequestHeader(value = "peer", required = false) String peer, @RequestBody Transaction transaction) {
blockchainService.newTransactionReceived(transaction, peer);
}
}
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