Implemented serialization
This commit is contained in:
@@ -8,9 +8,11 @@ public final class Main {
|
|||||||
System.out.print("Enter how many zeros the hash must start with: ");
|
System.out.print("Enter how many zeros the hash must start with: ");
|
||||||
final var hashZeroes = sc.nextInt();
|
final var hashZeroes = sc.nextInt();
|
||||||
final var blockChain = new BlockChain(hashZeroes);
|
final var blockChain = new BlockChain(hashZeroes);
|
||||||
|
blockChain.load();
|
||||||
|
|
||||||
IntStream.range(0, 5).forEach(blockChain::addBlock);
|
IntStream.range(0, 5).forEach(block -> blockChain.addBlock());
|
||||||
|
|
||||||
System.out.println(blockChain);
|
System.out.println(blockChain);
|
||||||
|
blockChain.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,18 +2,20 @@ package model;
|
|||||||
|
|
||||||
import static java.lang.String.valueOf;
|
import static java.lang.String.valueOf;
|
||||||
|
|
||||||
|
import util.HashFunction;
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import util.HashFunction;
|
|
||||||
|
|
||||||
public class Block {
|
public class Block implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
private final String previousBlockHash;
|
private final String previousBlockHash;
|
||||||
private final int id;
|
private final int id;
|
||||||
private final int hashZeroes;
|
private final int hashZeroes;
|
||||||
private final long timeStamp;
|
private final long timeStamp;
|
||||||
private final String blockHash;
|
private final String blockHash;
|
||||||
private final Random random = new Random();
|
private final transient Random random = new Random();
|
||||||
private int magicNumber;
|
private int magicNumber;
|
||||||
private float generationSecs = 0;
|
private float generationSecs = 0;
|
||||||
|
|
||||||
@@ -38,7 +40,7 @@ public class Block {
|
|||||||
var hash = "";
|
var hash = "";
|
||||||
do {
|
do {
|
||||||
hash = HashFunction.applySha256(this.id + valueOf(this.timeStamp) + calculateMagicNumber());
|
hash = HashFunction.applySha256(this.id + valueOf(this.timeStamp) + calculateMagicNumber());
|
||||||
} while (!hash.matches("(?s)0{"+hashZeroes+"}([^0].*)?"));
|
} while (!hash.matches("(?s)0{" + hashZeroes + "}([^0].*)?"));
|
||||||
final long end = System.currentTimeMillis();
|
final long end = System.currentTimeMillis();
|
||||||
generationSecs = (end - start) / 1000F;
|
generationSecs = (end - start) / 1000F;
|
||||||
return hash;
|
return hash;
|
||||||
@@ -51,11 +53,11 @@ public class Block {
|
|||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Block: " + "\n"
|
return "Block: " + "\n"
|
||||||
+"Id: " + this.id + "\n"
|
+ "Id: " + this.id + "\n"
|
||||||
+"Timestamp: " + this.timeStamp + "\n"
|
+ "Timestamp: " + this.timeStamp + "\n"
|
||||||
+"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" + "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package model;
|
package model;
|
||||||
|
|
||||||
|
import util.FileManagement;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -12,9 +13,10 @@ public class BlockChain {
|
|||||||
this.hashZeroes = hashZeroes;
|
this.hashZeroes = hashZeroes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addBlock(final int id) {
|
public void addBlock() {
|
||||||
final String previousBlockHash = (id > 0) ? blockList.get(id - 1).getBlockHash() : "0";
|
final int nextId = blockList.size();
|
||||||
blockList.add(new Block(previousBlockHash, id, hashZeroes));
|
final String previousBlockHash = (nextId > 0) ? blockList.get(nextId - 1).getBlockHash() : "0";
|
||||||
|
blockList.add(new Block(previousBlockHash, nextId, hashZeroes));
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean validateBlockchain() {
|
public boolean validateBlockchain() {
|
||||||
@@ -41,4 +43,12 @@ public class BlockChain {
|
|||||||
}
|
}
|
||||||
return blockList.stream().map(Block::toString).collect(Collectors.joining("\n"));
|
return blockList.stream().map(Block::toString).collect(Collectors.joining("\n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void load() {
|
||||||
|
FileManagement.loadBlockchain(blockList);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save() {
|
||||||
|
FileManagement.saveBlockchain(blockList);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
42
src/util/FileManagement.java
Normal file
42
src/util/FileManagement.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package util;
|
||||||
|
|
||||||
|
import model.Block;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public final class FileManagement {
|
||||||
|
|
||||||
|
private static final String FILE = "blockChain.txt";
|
||||||
|
|
||||||
|
public static void saveBlockchain(final List<Block> blockList) {
|
||||||
|
final var file = new File(FILE);
|
||||||
|
try (final var fileOut = new FileOutputStream(file);
|
||||||
|
final var objectOut = new ObjectOutputStream(fileOut)) {
|
||||||
|
for (final var block : blockList) {
|
||||||
|
objectOut.writeObject(block);
|
||||||
|
}
|
||||||
|
} catch (final IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void loadBlockchain(final List<Block> blockList) {
|
||||||
|
final var file = new File(FILE);
|
||||||
|
if (file.exists()) {
|
||||||
|
try (final var fileIn = new FileInputStream(file);
|
||||||
|
final var objectIn = new ObjectInputStream(fileIn)) {
|
||||||
|
while (fileIn.available() > 0) {
|
||||||
|
final var object = objectIn.readObject();
|
||||||
|
blockList.add((Block) object);
|
||||||
|
}
|
||||||
|
} catch (final IOException | ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user