Dynamic hash zeroes

This commit is contained in:
2021-07-13 09:23:39 +02:00
parent 3856ff7e48
commit d433dbf721
5 changed files with 48 additions and 23 deletions

View File

@@ -1,27 +1,30 @@
import java.util.ArrayList; import java.util.concurrent.Executors;
import java.util.List; import java.util.concurrent.TimeUnit;
import java.util.Scanner;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import model.BlockChain; import model.BlockChain;
import model.Miner; import model.Miner;
public final class Main { public final class Main {
public static void main(final String[] args) throws InterruptedException { public static void main(final String[] args) throws InterruptedException {
final var sc = new Scanner(System.in); final var blockChain = new BlockChain();
System.out.print("Enter how many zeros the hash must start with: ");
final var hashZeroes = sc.nextInt();
final var blockChain = new BlockChain(hashZeroes);
blockChain.load(); blockChain.load();
final List<Miner> miners = new ArrayList<>(); final var nThreads = Runtime.getRuntime().availableProcessors();
IntStream.range(0, 5).forEach(minerId -> miners.add(new Miner(minerId, blockChain))); final var executor = Executors.newFixedThreadPool(nThreads);
miners.forEach(Thread::start);
for (final Miner miner : miners) { IntStream.range(0, 5)
miner.join(); .mapToObj(minerId -> new Miner(minerId, blockChain))
.forEach(executor::submit);
executor.shutdown();
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
executor.shutdownNow();
} }
System.out.println(blockChain);
blockChain.save(); blockChain.save();
} }
} }

View File

@@ -37,6 +37,10 @@ public class Block implements Serializable {
return previousBlockHash; return previousBlockHash;
} }
public float getGenerationSecs() {
return generationSecs;
}
private String calculateBlockHash() { private String calculateBlockHash() {
final long start = System.currentTimeMillis(); final long start = System.currentTimeMillis();
var hash = ""; var hash = "";
@@ -61,7 +65,7 @@ public class Block implements Serializable {
+ "Magic number: " + this.magicNumber + "\n" + "Magic number: " + this.magicNumber + "\n"
+ "Hash of the previous block: " + "\n" + this.previousBlockHash + "\n" + "Hash of the previous block: " + "\n" + this.previousBlockHash + "\n"
+ "Hash of the block: \n" + this.blockHash + "\n" + "Hash of the block: \n" + this.blockHash + "\n"
+ "Block was generating for: " + this.generationSecs + " seconds" + "\n"; + "Block was generating for " + this.generationSecs + " seconds";
} }
}
}

View File

@@ -6,17 +6,32 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class BlockChain { public class BlockChain {
private final int hashZeroes;
private static final List<Block> blockList = new LinkedList<>();
public BlockChain(final int hashZeroes) { private int hashZeroes;
this.hashZeroes = hashZeroes; private static final List<Block> blockList = new LinkedList<>();
}
public synchronized void addBlock(final int minerId) { public synchronized void addBlock(final int minerId) {
final int nextId = blockList.size(); final int nextId = blockList.size();
final String previousBlockHash = (nextId > 0) ? blockList.get(nextId - 1).getBlockHash() : "0"; final String previousBlockHash = (nextId > 0) ? blockList.get(nextId - 1).getBlockHash() : "0";
blockList.add(new Block(previousBlockHash, minerId, nextId, hashZeroes));
final var block = new Block(previousBlockHash, minerId, nextId, hashZeroes);
blockList.add(block);
final float generationTime = blockList.get(nextId).getGenerationSecs();
final String CASE;
if (generationTime < 10) {
hashZeroes += 1;
CASE = "N was increased to " + hashZeroes + "\n";
} else if (generationTime > 60) {
hashZeroes -= 1;
CASE = "N was decreased by 1" + "\n";
} else {
CASE = "N stays the same";
}
System.out.println(block);
System.out.println(CASE);
} }
public boolean validateBlockchain() { public boolean validateBlockchain() {
@@ -51,4 +66,5 @@ public class BlockChain {
public void save() { public void save() {
FileManagement.saveBlockchain(blockList); FileManagement.saveBlockchain(blockList);
} }
} }

View File

@@ -1,6 +1,6 @@
package model; package model;
public class Miner extends Thread { public class Miner implements Runnable {
private final int minerId; private final int minerId;
private final BlockChain blockChain; private final BlockChain blockChain;
@@ -14,4 +14,5 @@ public class Miner extends Thread {
public void run() { public void run() {
blockChain.addBlock(minerId); blockChain.addBlock(minerId);
} }
} }

View File

@@ -39,4 +39,5 @@ public final class FileManagement {
} }
} }
} }
} }