
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

/**
 * Created by Kaan on 18-Feb-17.
 */
public class BlockchainManager
{
    private Blockchain blockchain;

    public BlockchainManager()
    {
        blockchain = new Blockchain();
    }

    public Blockchain getBlockchain()
    {
        return blockchain;
    }

    public boolean addBlockToBlockchain(Block block)
    {
        return blockchain.addBlock(block);
    }

    public int getBlockchainLength()
    {
        return blockchain.getLength();
    }

    public ArrayList<Block> getLongestChain()
    {
        ArrayList<String> hashChain = blockchain.getBlockchain();
        ArrayList<Block> blocks = new ArrayList<Block>();

        for (int i = 0; i < hashChain.size(); i++)
            blocks.add(blockchain.getBlock(hashChain.get(i)));
        return blocks;
    }

    public String mineBlock(String prevHash, long timestamp, long maxNonce,
                            ArrayList<Transaction> transactions)
    {
        BlockMiner miner = new BlockMiner(prevHash, timestamp, maxNonce, transactions);
        return miner.mineBlock();
    }

    /**
     * This class is used for mining a block which means finding a
     * convenient hash key before creating it. The hash key must
     * include zeros in its 8 most significant digit. The purpose is
     * to find a minimum value which is called nonce to make the hash
     * key. The score should be below a target after doing this.
     */
    private class BlockMiner
    {
        private long blockId;
        private MerkleTree data;
        private String prevHash;
        private long timestamp;
        private long maxNonce;

        public BlockMiner(String prevHash, long timestamp, long maxNonce,
                          ArrayList<Transaction> transactions)
        {
            this.prevHash = prevHash;
            this.timestamp = timestamp;
            this.maxNonce = maxNonce;

            ArrayList<String> stringTransactions = new ArrayList<String>();
            for (int i = 0; i < transactions.size(); i++)
                stringTransactions.add(transactions.get(i).getStringFormat());
            data = new MerkleTree(stringTransactions);
        }

        public String mineBlock()
        {
            long score = Long.MAX_VALUE;
            int bestNonce = -1;
            try
            {
                MessageDigest md = MessageDigest.getInstance("SHA-256");
                String blockData = "{" + timestamp + ":" + blockId + ":" + prevHash + ":" +
                        data.getRoot();

                // Try all values as brute-force
                for (int i = 0; i < maxNonce; i++)
                {
                    String dataWithNonce = blockData + ":" + i + "}";
                    byte[] hash = md.digest(dataWithNonce.getBytes("UTF-8"));

                    long tempScore = 0L;
                    for (int j = 0; j < 8; j++)
                        tempScore = (hash[j] & 0xff) + (tempScore << 8);

                    if (tempScore < score && tempScore > 0)
                        score = tempScore;

                    // Check if most significant 8 digits are zero
                    if ((hash[0] & 0xff) == 0x00)
                    {
                        bestNonce = i;
                        break;
                    }
                }
            }
            catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {}
            return bestNonce + ":" + score;
        }
    }
}

@@ -1,20 +1,309 @@
+package Blockchain_MVN.BlockchainManager.src.main.java;%0A%0Aimport Blockchain_MVN.Blockchain.src.main.java.Block;%0Aimport Blockchain_MVN.Blockchain.src.main.java.Blockchain;%0Aimport Blockchain_MVN.Blockchain.src.main.java.MerkleTree;%0Aimport Blockchain_MVN.Blockchain.src.main.java.Transaction;%0A
 %0Aimport java.io.Unsu
@@ -579,16 +579,29 @@
 Manager(
+Block genesis
 )%0A    %7B%0A
@@ -632,24 +632,31 @@
  Blockchain(
+genesis
 );%0A    %7D%0A%0A  
@@ -921,32 +921,34 @@
 ength();%0A    %7D%0A%0A
+//
     public Array
@@ -973,30 +973,34 @@
 gestChain()%0A
+//
     %7B%0A
+//
         Arra
@@ -1049,24 +1049,26 @@
 ockchain();%0A
+//
         Arra
@@ -1109,25 +1109,29 @@
 t%3CBlock%3E();%0A
-%0A
+//%0A//
         for 
@@ -1161,32 +1161,34 @@
 in.size(); i++)%0A
+//
             bloc
@@ -1230,24 +1230,26 @@
 n.get(i)));%0A
+//
         retu
@@ -1259,16 +1259,18 @@
 blocks;%0A
+//
     %7D%0A%0A 
@@ -2916,24 +2916,8 @@
 :%22 +
- blockId + %22:%22 +
  pre
@@ -2930,16 +2930,28 @@
 + %22:%22 +%0A
+            
         

Add new blockchain