Commit 830bf333 authored by mikael.beyene's avatar mikael.beyene
Browse files

Refactor App

parent 1bf6c328
......@@ -36,38 +36,33 @@ import java.util.TreeMap;
// https://www.baeldung.com/java-blockchain
public class App {
private static final int NUMBER_OF_TRIES = 1_000_000;
public static void main(String[] args) throws NoSuchAlgorithmException {
Options options = parseArguments(args);
if (options == null) return;
// Try different targets. The maximum target is Integer.MAX (i.e., 2^32 -1)
int target = (int) (Integer.MAX_VALUE * options.targetScaleFactor);
// this is the first block
Block genesis = new Block.Builder()
.setVersion(1)
.setPreviousHash("")
.setHash("0xDEADBEEF") // this hash is just an example value...
.setMerkleRoot("")
.setTimeStamp(Instant.now().toEpochMilli())
.setTarget(target)
.setNonce(1337)
.build();
Block genesis = makeGenesisBlock(target);
// This is NOT a real Merkle tree
Map<String, Object> merkleTree = new TreeMap<>();
// Add some random fake transactions to our fake Merkle tree
merkleTree.put("1", "TRANSACTION" + new Random().nextInt());
merkleTree.put("2", "TRANSACTION" + new Random().nextInt());
Map<String, Object> merkleTree = getFakeMerkleTree();
// Here we set up everything for the next block
Block.Builder blockBuilder = new Block.Builder()
.setVersion(1)
.setPreviousHash(genesis.getHash())
// Normally, we would need the Merkle tree root...
.setMerkleRoot(String.format("0x%08X", merkleTree.hashCode()))
.setTarget(target);
Block.Builder blockBuilder = createAndConfigureBlockBuilder(target, genesis, merkleTree);
boolean foundBlock = startMining(target, blockBuilder);
if (foundBlock) {
Block block = blockBuilder.build();
System.out.println("Hash =" + block.getHash());
System.out.println("Target=" + String.format("0x%08X", target));
System.out.println(block);
}
}
private static boolean startMining(int target, Block.Builder blockBuilder) throws NoSuchAlgorithmException {
// We'll use SHA-256 for hashing just like Bitcoin
MessageDigest md = MessageDigest.getInstance("SHA-256");
......@@ -99,20 +94,43 @@ public class App {
}
// we stop after a number of tries, since this is only an exercise
if (nonce > 1_000_000)
if (nonce > NUMBER_OF_TRIES)
break;
}
System.out.println("Found block=" + foundBlock);
System.out.println("Number of tries=" + NumberFormat.getInstance().format(nonce));
return foundBlock;
}
if (foundBlock) {
Block block = blockBuilder.build();
private static Block.Builder createAndConfigureBlockBuilder(int target, Block genesis, Map<String, Object> merkleTree) {
return new Block.Builder()
.setVersion(1)
.setPreviousHash(genesis.getHash())
// Normally, we would need the Merkle tree root...
.setMerkleRoot(String.format("0x%08X", merkleTree.hashCode()))
.setTarget(target);
}
System.out.println("Hash =" + block.getHash());
System.out.println("Target=" + String.format("0x%08X", target));
System.out.println(block);
}
private static Map<String, Object> getFakeMerkleTree() {
Map<String, Object> merkleTree = new TreeMap<>();
// Add some random fake transactions to our fake Merkle tree
merkleTree.put("1", "TRANSACTION" + new Random().nextInt());
merkleTree.put("2", "TRANSACTION" + new Random().nextInt());
return merkleTree;
}
private static Block makeGenesisBlock(int target) {
// this is the first block
return new Block.Builder()
.setVersion(1)
.setPreviousHash("")
.setHash("0xDEADBEEF") // this hash is just an example value...
.setMerkleRoot("")
.setTimeStamp(Instant.now().toEpochMilli())
.setTarget(target)
.setNonce(1337)
.build();
}
private static Options parseArguments(String[] args) {
......@@ -134,7 +152,6 @@ public class App {
cmd.printVersionHelp(System.out);
return null;
}
} catch (CommandLine.ParameterException ex) {
System.err.println(ex.getMessage());
if (!CommandLine.UnmatchedArgumentException.printSuggestions(ex, System.err)) {
......@@ -142,6 +159,7 @@ public class App {
}
return null;
}
return options;
}
}
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