diff --git a/src/lt/nogalosa/nercoin/Main.java b/src/lt/nogalosa/nercoin/Main.java index b67609f..ff600be 100644 --- a/src/lt/nogalosa/nercoin/Main.java +++ b/src/lt/nogalosa/nercoin/Main.java @@ -3,10 +3,12 @@ import lt.nogalosa.nercoin.entities.Block; import lt.nogalosa.nercoin.entities.Miner; import lt.nogalosa.nercoin.entities.Transaction; +import lt.nogalosa.nercoin.hash.NerHash; import lt.nogalosa.nercoin.managers.TransactionManager; import lt.nogalosa.nercoin.managers.UserManager; import java.util.ArrayList; +import java.util.Iterator; import java.util.Random; public class Main { @@ -17,15 +19,36 @@ public Main() { UserManager.i().generateUsers(); ArrayList randomTransactions = TransactionManager.generateRandomTransactions(UserManager.i().getUsers()); - - Block block = new Block(); - for(int i = 0; i < 100; i++) { - Transaction transaction = randomTransactions.get(random.nextInt(randomTransactions.size())); - block.getTransactionManager().addTransaction(transaction); + ArrayList blocks = new ArrayList<>(); + + for (int x = 0; x < 5; x++) { + Block block = new Block(); + for (int i = 0; i < 100; i++) { + Transaction transaction = randomTransactions.get(random.nextInt(randomTransactions.size())); + block.getTransactionManager().addTransaction(transaction); + } + + Iterator transactionIterator = block.getTransactionManager().getTransactions().iterator(); + while (transactionIterator.hasNext()) { + Transaction transaction = transactionIterator.next(); + if (transaction.getFromUser().getBalance() < transaction.getAmount()) { + block.getTransactionManager().removeTransaction(transaction); + System.out.println("Removing " + transaction.getHash() + " #1"); + } + + NerHash nerHash = new NerHash(transaction.getFromUser().getPublicKey() + transaction.getToUser().getPublicKey() + transaction.getAmount() + transaction.getTimestamp()); + if (!nerHash.hash().equals(transaction.getHash())) { + block.getTransactionManager().removeTransaction(transaction); + System.out.println("Removing " + transaction.getHash() + " #2"); + } + } + + block.setDifficulityTarget("2"); + block.generateMerkelTreeHash(); + blocks.add(block); } - block.setDifficulityTarget("2"); - block.generateMerkelTreeHash(); - Miner blockMiner = new Miner(block); + System.out.println("Starting miner with "+blocks.size() + " blocks."); + Miner blockMiner = new Miner(blocks); blockMiner.start(); } diff --git a/src/lt/nogalosa/nercoin/entities/Block.java b/src/lt/nogalosa/nercoin/entities/Block.java index 2c49d32..4357dd1 100644 --- a/src/lt/nogalosa/nercoin/entities/Block.java +++ b/src/lt/nogalosa/nercoin/entities/Block.java @@ -15,6 +15,7 @@ public class Block { private int nonce; private String difficulityTarget; private TransactionManager transactionManager; + private boolean mined; public Block() { transactionManager = new TransactionManager(); @@ -84,6 +85,14 @@ public String getHash() { return hash.hash(); } + public boolean isMined() { + return mined; + } + + public void setMined(boolean mined) { + this.mined = mined; + } + public String toString() { return getHash(); } diff --git a/src/lt/nogalosa/nercoin/entities/Miner.java b/src/lt/nogalosa/nercoin/entities/Miner.java index 64844bd..5d51b88 100644 --- a/src/lt/nogalosa/nercoin/entities/Miner.java +++ b/src/lt/nogalosa/nercoin/entities/Miner.java @@ -1,18 +1,40 @@ package lt.nogalosa.nercoin.entities; +import java.util.ArrayList; + public class Miner extends Thread{ - Block block; + ArrayList blocks; + + ArrayList mined; - public Miner(Block block) { - this.block = block; + public Miner(ArrayList blocks) { + this.blocks = blocks; } @Override public void run() { - while(block.getHash().compareTo(block.getDifficulityTarget()) > 0) { - block.incrementNonce(); - System.out.println(block.getHash() + " > " + block.getNonce()); + int currentTries = 1; + int currentBlock = 0; + long lastTimestamp = System.currentTimeMillis(); + + while(blocks.size() > 0) { + if(lastTimestamp + currentTries * 1000 < System.currentTimeMillis() || currentBlock >= blocks.size()){ + currentBlock++; + if(currentBlock >= blocks.size()) { + currentBlock = 0; + currentTries++; + } + lastTimestamp = System.currentTimeMillis(); + } + if(blocks.get(currentBlock).getHash().compareTo(blocks.get(currentBlock).getDifficulityTarget()) > 0){ + blocks.get(currentBlock).incrementNonce(); + System.out.println(currentBlock + " > " + blocks.get(currentBlock).getHash() + " > " + blocks.get(currentBlock).getNonce()); + } else { + System.out.println("Mined: "+blocks.get(currentBlock).getHash()); + mined.add(blocks.get(currentBlock)); + blocks.remove(currentBlock); + } } System.out.println("Success!"); }