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

no postconstructs anymore, wait for runtime port and then do initialization.

parent 43ffdb0a
package nl.craftsmen.blockchain.craftscoinnode;
import nl.craftsmen.blockchain.craftscoinnode.blockchain.BlockchainService;
import nl.craftsmen.blockchain.craftscoinnode.network.Network;
import nl.craftsmen.blockchain.craftscoinnode.transaction.SignatureService;
import nl.craftsmen.blockchain.craftscoinnode.util.InstanceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
class ApplicationStartedListener implements ApplicationListener<ApplicationStartedEvent> {
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationStartedListener.class);
private final Environment environment;
private final InstanceInfo instanceInfo;
private final Network network;
private final BlockchainService blockchainService;
private final SignatureService signatureService;
@Autowired
public ApplicationStartedListener(Environment environment, InstanceInfo instanceInfo, Network network, BlockchainService blockchainService, SignatureService signatureService) {
this.environment = environment;
this.instanceInfo = instanceInfo;
this.network = network;
this.blockchainService = blockchainService;
this.signatureService = signatureService;
}
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
Integer port = environment.getProperty("local.server.port", Integer.class);
LOGGER.info("running port is: " + port);
instanceInfo.setPort(port);
network.init();
network.connectToNetwork();
blockchainService.init();
signatureService.init();
}
}
......@@ -11,7 +11,6 @@ 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.Collections;
import java.util.List;
......@@ -51,9 +50,8 @@ public class BlockchainService {
/**
* Initializes and updates the blockchain on startup.
*/
@PostConstruct
public void init() {
network.connectToNetwork();
LOGGER.info("intializing");
initializeBlockchain();
reachConsensus();
}
......
......@@ -17,7 +17,6 @@ import org.springframework.stereotype.Component;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;
......@@ -50,7 +49,6 @@ public class Network {
}
@PostConstruct
public void init() {
peers.addAll(peersRepository.loadPeers());
}
......
......@@ -6,7 +6,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.security.*;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.InvalidKeySpecException;
......@@ -30,18 +29,23 @@ public class SignatureService {
this.keyRepository = keyRepository;
}
@PostConstruct
public void init() throws GeneralSecurityException {
Security.addProvider(new BouncyCastleProvider());
Optional<Keys> keys = keyRepository.loadKeys();
if (keys.isPresent()) {
LOGGER.info("existing key material found, loading...");
loadExistingKeyData(keys.get());
} else {
LOGGER.info("no key material found, generating new keypair.");
generateNewKeyPair();
Keys serializedKeys = new Keys(new String(Base64.getEncoder().encode(keyPair.getPrivate().getEncoded())), new String(Base64.getEncoder().encode(keyPair.getPublic().getEncoded())));
keyRepository.saveKeys(serializedKeys);
public void init() {
try {
Security.addProvider(new BouncyCastleProvider());
Optional<Keys> keys = keyRepository.loadKeys();
if (keys.isPresent()) {
LOGGER.info("existing key material found, loading...");
loadExistingKeyData(keys.get());
LOGGER.info("finished loading key material.");
} else {
LOGGER.info("no key material found, generating new keypair.");
generateNewKeyPair();
Keys serializedKeys = new Keys(new String(Base64.getEncoder().encode(keyPair.getPrivate().getEncoded())), new String(Base64.getEncoder().encode(keyPair.getPublic().getEncoded())));
keyRepository.saveKeys(serializedKeys);
LOGGER.info("new keypair generated and saved.");
}
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
......
......@@ -4,7 +4,6 @@ import nl.craftsmen.blockchain.craftscoinnode.CraftsCoinConfigurationProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
......@@ -16,15 +15,12 @@ public class InstanceInfo {
private static final Logger LOGGER = LoggerFactory.getLogger(InstanceInfo.class);
private final ServerProperties serverProperties;
private String thisIpAdress;
private int port;
private String node;
@Autowired
public InstanceInfo(ServerProperties serverProperties, CraftsCoinConfigurationProperties configuration) {
this.serverProperties = serverProperties;
public InstanceInfo(CraftsCoinConfigurationProperties configuration) {
this.thisIpAdress = configuration.getIpAddress();
}
......@@ -35,9 +31,6 @@ public class InstanceInfo {
LOGGER.info("property 'thisIpAddress' not set in application.properties, determining ip address...");
this.thisIpAdress = InetAddress.getLocalHost().getHostAddress();
}
this.port = serverProperties.getPort();
this.node = this.thisIpAdress + ":" + this.port;
LOGGER.info("node name of this node is {}", node);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
......@@ -50,9 +43,13 @@ public class InstanceInfo {
* @return the node address, for example: 192.168.1.250:8080
*/
public String getNode() {
return node;
return this.thisIpAdress + ":" + this.port;
}
public void setPort(int aPort) {
this.port = aPort;
LOGGER.info("node name of this node is {}", getNode());
}
public int getPort() {
return port;
......
......@@ -22,18 +22,18 @@ public class InstanceInfoTest {
}
@Test
public void nonConfiguredIpShouldResultInAutomaticIpDiscovery() {
instanceInfo = new InstanceInfo(serverProperties, new CraftsCoinConfigurationProperties());
instanceInfo = new InstanceInfo(new CraftsCoinConfigurationProperties());
instanceInfo.init();
assertThat(instanceInfo.getPort()).isEqualTo(8080);
assertThat(instanceInfo.getPort()).isEqualTo(0);
}
@Test
public void configuredIpShouldProduceThatIpForThisNode() {
CraftsCoinConfigurationProperties craftsCoinConfigurationProperties = new CraftsCoinConfigurationProperties();
craftsCoinConfigurationProperties.setIpAddress("192.168.0.170");
instanceInfo = new InstanceInfo(serverProperties, craftsCoinConfigurationProperties);
instanceInfo = new InstanceInfo(craftsCoinConfigurationProperties);
instanceInfo.init();
assertThat(instanceInfo.getPort()).isEqualTo(8080);
assertThat(instanceInfo.getPort()).isEqualTo(0);
assertThat(instanceInfo.getNode()).isEqualTo("192.168.0.170:8080");
}
}
\ No newline at end of file
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