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: ");
|
||||
final var hashZeroes = sc.nextInt();
|
||||
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);
|
||||
blockChain.save();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,18 +2,20 @@ package model;
|
||||
|
||||
import static java.lang.String.valueOf;
|
||||
|
||||
import util.HashFunction;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
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 int id;
|
||||
private final int hashZeroes;
|
||||
private final long timeStamp;
|
||||
private final String blockHash;
|
||||
private final Random random = new Random();
|
||||
private final transient Random random = new Random();
|
||||
private int magicNumber;
|
||||
private float generationSecs = 0;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package model;
|
||||
|
||||
import util.FileManagement;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -12,9 +13,10 @@ public class BlockChain {
|
||||
this.hashZeroes = hashZeroes;
|
||||
}
|
||||
|
||||
public void addBlock(final int id) {
|
||||
final String previousBlockHash = (id > 0) ? blockList.get(id - 1).getBlockHash() : "0";
|
||||
blockList.add(new Block(previousBlockHash, id, hashZeroes));
|
||||
public void addBlock() {
|
||||
final int nextId = blockList.size();
|
||||
final String previousBlockHash = (nextId > 0) ? blockList.get(nextId - 1).getBlockHash() : "0";
|
||||
blockList.add(new Block(previousBlockHash, nextId, hashZeroes));
|
||||
}
|
||||
|
||||
public boolean validateBlockchain() {
|
||||
@@ -41,4 +43,12 @@ public class BlockChain {
|
||||
}
|
||||
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