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

made nice lamba for loading blockchain.

parent ace90e02
......@@ -13,9 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.*;
/**
* Manages the blockchain on this node.
......@@ -127,16 +125,12 @@ public class BlockchainService {
private void reachConsensus() {
List<Blockchain> otherChains = network.retrieveBlockchainsFromPeers();
LOGGER.info("{} blockchains from peers received. Checking...", otherChains.size());
for (Blockchain otherChain : otherChains) {
if (this.blockchain == null || this.blockchain.isInferiorTo(otherChain)) {
LOGGER.info("received a blockchain that is better than my currect chain. Replacing current chain.");
this.blockchain = otherChain;
clearConfirmedTransactions();
genericRepository.save(this.blockchain);
break;
}
}
Optional<Blockchain> betterChain = otherChains.stream().filter(b -> this.blockchain.isInferiorTo(b)).max(Comparator.comparing(Blockchain::getIndexOfNextBlock));
betterChain.ifPresent(d -> {
this.blockchain = d;
clearConfirmedTransactions();
genericRepository.save(this.blockchain);
});
LOGGER.info("Finished reaching consensus.");
}
......
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