Essay Writer » Essay Blog » Java Assignment Project 1

Java Assignment Project 1


“Griffin Blockchain”




This project will involve the creation of a Blockchain miner that does all the following:


  • Receives transactions from user or remote node.
  • Sends transactions to a remote node.
  • Manages incoming remote node messages using a Queue.
  • Generates a Merkle Tree and an eventual Merkle Root as part of Transaction processing.
  • Creates Blockchain Blocks using Proof of Work algorithm.



For this project, you will create the main functionality inside of an existing code base called BlockchainBasics.  The BlockchainBasics code that you will begin with is provided along with this document.


BlockchainBasics, once completed, will allow you to run two instances of this app on two different ports that pass transactions to each other and mine blocks.


This app doesn’t save the blocks into a blockchain, but the central functionality you code into this app will be able to be plugged in directly into a larger Blockchain application (the code for this will be provided in the coming weeks) that fully manages a Blockchain, connects to multiple networked nodes, and allows you to trade items on the network.


However, you will only be turning in the BlockchainBasics code as specified at the end of this doc.


This project will involve the following:

– Socket programming

– Queue

– Multithreading

– Hashing: SHA-256

– Merkle Trees

– Blockchain Proof of Work





  1. Download


  1. This includes the following classes:
    1. java
    2. java
    3. java
    4. java
    5. java
    6. java
    7. java
    8. java
    9. java


  1. You only add code where you see this:







  1. CLASSES where you add code:


  1. Miner


  1. doProofOfWork method: The logic behind this will be reviewed in class.
    1. This method should first create a string that has as many zeros in it as oBlock.getDifficulty() equals.
      1. Hint: use a while loop here that keeps looping while the length of this string you’re creating is less than the target difficulty length.


  1. Then, this method should have another while loop that keeps looping until a valid nonce is found.
    1. There needs to be an if — else in this while loop.


  1. If bAbortPoW (an instance variable you’ll see at the top of the class) equals true, then this means another miner solved the block and this miner received it, so you need to:
    1. Set bAbortPoW back to false.
    2. Print out something to the user like:

“[miner] Aborted mining block, probably because another confirmed block received.”

  1. Return false so the method ends.


  1. Else perform another attempt at a valid nonce:
    1. The nonce should start at 0 before the while loop and increment each loop.
    2. A valid nonce, after it’s added to the block’s properties and the block is hashed, should result in a hash that has as many leading zeros as the string you created above (use String’s startsWith method for this check).
      1. So remember that on oBlock you have to call:
        1. Set nonce.
        2. Compute hash.
  • Set hash to whatever came back from compute hash.
  1. Then check if hash on oBlock now begins with your leading zeroes string you created above.
    1. If it does, then return true.













  1. Block


  1. computeMerkleRoot:


  1. Choose which approach you want to take:
    1. The 90% approach is much easier and is based on what we’ve done in class but your max score for the project can only reach 90%.
    2. The 100% approach is much trickier, and you must figure out on your own.


  1. 90% Project Score Logic: This logic needs to account for possibly 2 or 4 items in lstItems that are passed in.
    1. This means your code should have 2 or 4 Merkle Tree leaves depending on how many items are passed in.
  2. 100% Project Score Logic: You can only get 90% on this project if everything else is done perfectly.  To get 100%, you must make this method flexible to accept any power of 2 items.  So lstItems could be 2,4,8,16,32, etc. and your code would be able to compute the Merkle Root.
    1. IMPORTANT: If you choose this route, you’ll have to show me your logic in class one week beforethe project is due.


  1. NOTE: There is a main method at the bottom of this class that you can use to test 2 and 4 items simply by right clicking on the tab for this class and selecting “Run Block.main()”


  1. populateMerkleNode:
    1. This method will work just as we did in class… set the left and right nodes and then call generateHash in the BlockchainUtil class and set the node’s hash.




  1. BlockchainUtil


  1. generateHash:
    1. This method generates a SHA-256 hash of the string passed in.
    2. Code for this was reviewed in class and doesn’t need to be changed.


  1. P2PMessageQueue


  1. enqueue:
    1. This adds a node to the queue.
    2. Code will be reviewed in class.


  1. dequeue:
    1. This removes and returns a node from the queue.
    2. Code will be reviewed in class.


  • hasNodes:
    1. This returns true or false depending on if any nodes exist in queue.
    2. Code will be reviewed in class.





  1. P2PUtil


  1. connectForOneMessage:
    1. This method will connect to a given server via Socket for a one-time sending of a message to that server and it will return the reply from the server and disconnect.
    2. Code for the Socket communication will be reviewed in class.




  1. Testing Your Code
    1. You need to run two instances of this app so that they can communicate with each other.
      1. One app can be run from your IDE like you normally do.
      2. The other app must be ran from a PowerShell window or other command line tool as explained below.


  1. Running a separate instance of your app:
    1. Jar file generation
      1. You first need to build your code into a JAR file.
      2. Instructions are included with the project online and will be reviewed in class.
    2. Running your JAR file
      1. Navigate to your JAR file in file explorer.
        1. Shortcut if using IntelliJ:
          1. Find it in your left side file explorer in your IntelliJ IDE:
            1. …out > artifacts >BlockchainBasics_jar> BlockchainBasics.jar
          2. Right click on it and pic “Show in Explorer.”
        2. Make sure that no file is highlighted by LEFT clicking in right space.
        3. SHIFT + RIGHT click in white space as shown in class to get extra menu item of “Open PowerShell Window here”
        4. In PowerShell or other command line tool, type the following to run:

java -jar BlockchainBasics.jar


  1. Then make sure a different port is chosen for your IDE instance and your command line instance:
    1. For instance, choose 8800 for one and 8888 or the other.





  1. NOT PART OF PROJECT GRADING: Incorporating your code into larger in-class Griffin Blockchain app.
    1. All of the code you added to this BlockchainBasics project can also now be added to the Griffin Blockchain app to make it functional and partake in full Blockchain networking with others in class.
    2. Details will be provided in class, and this app’s code will be made available later.






IMPORTANT: DO NOT ZIP your files please.  Turn in just the 5 .java files that you added code to. ALSO, include a runnable .jar file of all the code if your code is functional.

Last Updated on February 27, 2019

Don`t copy text!
Scroll to Top