Skip to content

Commit

Permalink
Balance checking, hash checking, updated blocks mining
Browse files Browse the repository at this point in the history
  • Loading branch information
nogalosa committed Nov 27, 2019
1 parent 92af831 commit f57cf98
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
39 changes: 31 additions & 8 deletions src/lt/nogalosa/nercoin/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,15 +19,36 @@ public Main() {

UserManager.i().generateUsers();
ArrayList<Transaction> 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<Block> 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<Transaction> 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();

}
Expand Down
9 changes: 9 additions & 0 deletions src/lt/nogalosa/nercoin/entities/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Block {
private int nonce;
private String difficulityTarget;
private TransactionManager transactionManager;
private boolean mined;

public Block() {
transactionManager = new TransactionManager();
Expand Down Expand Up @@ -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();
}
Expand Down
34 changes: 28 additions & 6 deletions src/lt/nogalosa/nercoin/entities/Miner.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
package lt.nogalosa.nercoin.entities;

import java.util.ArrayList;

public class Miner extends Thread{

Block block;
ArrayList<Block> blocks;

ArrayList<Block> mined;

public Miner(Block block) {
this.block = block;
public Miner(ArrayList<Block> 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!");
}
Expand Down

0 comments on commit f57cf98

Please sign in to comment.