Question
SUMMARY This project will involve the creation of a Blockchain miner that does all the following: Receives transactions from user or remote node. Sends transactions
SUMMARY
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 doesnt 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
DETAILS
Download BlockchainBasics.zip
This includes the following classes:
BlockchainBasics.java
Miner.java
Block.java
MerkleNode.java
BlockchainUtil.java
P2PServer.java
P2PMessageQueue.java
P2PMessage.java
P2PUtil.java
You only add code where you see this:
####################
### ADD CODE HERE ###
####################
CLASSES where you add code:
Miner
doProofOfWork method: The logic behind this will be reviewed in class.
This method should first create a string that has as many zeros in it as oBlock.getDifficulty() equals.
Hint: use a while loop here that keeps looping while the length of this string youre creating is less than the target difficulty length.
Then, this method should have another while loop that keeps looping until a valid nonce is found.
There needs to be an if --- else in this while loop.
If bAbortPoW (an instance variable youll 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:
Set bAbortPoW back to false.
Print out something to the user like:
"[miner] Aborted mining block, probably because another confirmed block received."
Return false so the method ends.
Else perform another attempt at a valid nonce:
The nonce should start at 0 before the while loop and increment each loop.
A valid nonce, after its added to the blocks properties and the block is hashed, should result in a hash that has as many leading zeros as the string you created above (use Strings startsWith method for this check).
So remember that on oBlock you have to call:
Set nonce.
Compute hash.
Set hash to whatever came back from compute hash.
Then check if hash on oBlock now begins with your leading zeroes string you created above.
If it does, then return true.
Block
computeMerkleRoot:
Choose which approach you want to take:
The 90% approach is much easier and is based on what weve done in class but your max score for the project can only reach 90%.
The 100% approach is much trickier, and you must figure out on your own.
90% Project Score Logic: This logic needs to account for possibly 2 or 4 items in lstItems that are passed in.
This means your code should have 2 or 4 Merkle Tree leaves depending on how many items are passed in.
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.
IMPORTANT: If you choose this route, youll have to show me your logic in class one week before the project is due.
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()
populateMerkleNode:
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 nodes hash.
BlockchainUtil
generateHash:
This method generates a SHA-256 hash of the string passed in.
Code for this was reviewed in class and doesnt need to be changed.
P2PMessageQueue
enqueue:
This adds a node to the queue.
Code will be reviewed in class.
dequeue:
This removes and returns a node from the queue.
Code will be reviewed in class.
hasNodes:
This returns true or false depending on if any nodes exist in queue.
Code will be reviewed in class.
P2PUtil
connectForOneMessage:
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.
Code for the Socket communication will be reviewed in class.
Testing Your Code
You need to run two instances of this app so that they can communicate with each other.
One app can be run from your IDE like you normally do.
The other app must be ran from a PowerShell window or other command line tool as explained below.
Running a separate instance of your app:
Jar file generation
You first need to build your code into a JAR file.
Instructions are included with the project online and will be reviewed in class.
Running your JAR file
Navigate to your JAR file in file explorer.
Shortcut if using IntelliJ:
Find it in your left side file explorer in your IntelliJ IDE:
out > artifacts > BlockchainBasics_jar > BlockchainBasics.jar
Right click on it and pic Show in Explorer.
Make sure that no file is highlighted by LEFT clicking in right space.
SHIFT + RIGHT click in white space as shown in class to get extra menu item of Open PowerShell Window here
In PowerShell or other command line tool, type the following to run:
java -jar BlockchainBasics.jar
Then make sure a different port is chosen for your IDE instance and your command line instance:
For instance, choose 8800 for one and 8888 or the other.
NOT PART OF PROJECT GRADING: Incorporating your code into larger in-class Griffin Blockchain app.
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.
Details will be provided in class, and this apps code will be made available later..
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started