Dynamic hash zeroes
This commit is contained in:
@@ -1,27 +1,30 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.IntStream;
|
||||
import model.BlockChain;
|
||||
import model.Miner;
|
||||
|
||||
public final class Main {
|
||||
|
||||
public static void main(final String[] args) throws InterruptedException {
|
||||
final var sc = new Scanner(System.in);
|
||||
System.out.print("Enter how many zeros the hash must start with: ");
|
||||
final var hashZeroes = sc.nextInt();
|
||||
final var blockChain = new BlockChain(hashZeroes);
|
||||
final var blockChain = new BlockChain();
|
||||
blockChain.load();
|
||||
|
||||
final List<Miner> miners = new ArrayList<>();
|
||||
IntStream.range(0, 5).forEach(minerId -> miners.add(new Miner(minerId, blockChain)));
|
||||
miners.forEach(Thread::start);
|
||||
final var nThreads = Runtime.getRuntime().availableProcessors();
|
||||
final var executor = Executors.newFixedThreadPool(nThreads);
|
||||
|
||||
for (final Miner miner : miners) {
|
||||
miner.join();
|
||||
IntStream.range(0, 5)
|
||||
.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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -37,6 +37,10 @@ public class Block implements Serializable {
|
||||
return previousBlockHash;
|
||||
}
|
||||
|
||||
public float getGenerationSecs() {
|
||||
return generationSecs;
|
||||
}
|
||||
|
||||
private String calculateBlockHash() {
|
||||
final long start = System.currentTimeMillis();
|
||||
var hash = "";
|
||||
@@ -61,7 +65,7 @@ public class Block implements Serializable {
|
||||
+ "Magic number: " + this.magicNumber + "\n"
|
||||
+ "Hash of the previous block: " + "\n" + this.previousBlockHash + "\n"
|
||||
+ "Hash of the block: \n" + this.blockHash + "\n"
|
||||
+ "Block was generating for: " + this.generationSecs + " seconds" + "\n";
|
||||
+ "Block was generating for " + this.generationSecs + " seconds";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,17 +6,32 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class BlockChain {
|
||||
private final int hashZeroes;
|
||||
private static final List<Block> blockList = new LinkedList<>();
|
||||
|
||||
public BlockChain(final int hashZeroes) {
|
||||
this.hashZeroes = hashZeroes;
|
||||
}
|
||||
private int hashZeroes;
|
||||
private static final List<Block> blockList = new LinkedList<>();
|
||||
|
||||
public synchronized void addBlock(final int minerId) {
|
||||
final int nextId = blockList.size();
|
||||
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() {
|
||||
@@ -51,4 +66,5 @@ public class BlockChain {
|
||||
public void save() {
|
||||
FileManagement.saveBlockchain(blockList);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package model;
|
||||
|
||||
public class Miner extends Thread {
|
||||
public class Miner implements Runnable {
|
||||
|
||||
private final int minerId;
|
||||
private final BlockChain blockChain;
|
||||
@@ -14,4 +14,5 @@ public class Miner extends Thread {
|
||||
public void run() {
|
||||
blockChain.addBlock(minerId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,4 +39,5 @@ public final class FileManagement {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user