Question: Griffin Blockchain 1. SUMMARY This project will involve the creation of a Blockchain miner that does all the following: Receives transactions from user or remote
Griffin Blockchain
1. 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
2. DETAILS
a. Download BlockchainBasics.zip
i. This includes the following classes:
1. BlockchainBasics.java
2. Miner.java
3. Block.java
4. MerkleNode.java
5. BlockchainUtil.java
6. P2PServer.java
7. P2PMessageQueue.java
8. P2PMessage.java
9. P2PUtil.java
b. You only add code where you see this:
####################
### ADD CODE HERE ###
####################
Block
i. computeMerkleRoot:
1. Choose which approach you want to take:
a.
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%.
b. The
100% approach
is much trickier, and you must figure out on your own.
2.
90% Project Score Logic
: This logic needs to account for possibly
2 or 4 items
in lstItems that are
passed in.
a.
This means your code should have
2 or 4 Merkle Tree leaves
depending on how many items
are passed in.
3.
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.
a.
IMPORTANT
: If you choose this route, youll have to show me your logic in class
one week
before
the project is due
.
4.
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()
ii. 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 nodes hash.
This is block code:
import java.util.ArrayList; /** * This holds the values for a Block in the Blockchain, and it has methods to compute the Merkle Root and generate a hash. */ public class Block { private String sMerkleRoot; private int iDifficulty = 5; // Mining seconds in testing 5: 6,10,15,17,20,32 | testing 6: 12,289,218 private String sNonce; private String sMinerUsername; private String sHash; /** * This computes the Merkle Root. It either accepts 2 or 4 items, or if made to be dynamic, then accepts any * multiple of 2 (2,4,8.16.32,etc.) items. * @param lstItems * @return */ public synchronized String computeMerkleRoot(ArrayList lstItems) { ##################### ### ADD CODE HERE ### ##################### } /** * This method populates a Merkle node's left, right, and hash variables. * @param oNode * @param oLeftNode * @param oRightNode */ private void populateMerkleNode(MerkleNode oNode, MerkleNode oLeftNode, MerkleNode oRightNode){ ##################### ### ADD CODE HERE ### ##################### } // Hash this block, and hash will also be next block's previous hash. /** * This generates the hash for this block by combining the properties and hashing them. * @return */ public String computeHash() { return new BlockchainUtil().generateHash(sMerkleRoot + iDifficulty + sMinerUsername + sNonce); } public int getDifficulty() { return iDifficulty; } public String getNonce() { return sNonce; } public void setNonce(String nonce) { this.sNonce = nonce; } public void setMinerUsername(String sMinerUsername) { this.sMinerUsername = sMinerUsername; } public String getHash() { return sHash; } public void setHash(String h) { this.sHash = h; } public synchronized void setMerkleRoot(String merkleRoot) { this.sMerkleRoot = merkleRoot; } /** * Run this to test your merkle tree logic. * @param args */ public static void main(String[] args){ ArrayList lstItems = new ArrayList<>(); Block oBlock = new Block(); String sMerkleRoot; // These merkle root hashes based on "t1","t2" for two items, and then "t3","t4" added for four items. String sExpectedMerkleRoot_2Items = "3269f5f93615478d3d2b4a32023126ff1bf47ebc54c2c96651d2ac72e1c5e235"; String sExpectedMerkleRoot_4Items = "e08f7b0331197112ff8aa7acdb4ecab1cfb9497cbfb84fb6d54f1cfdb0579d69"; lstItems.add("t1"); lstItems.add("t2"); // *** BEGIN TEST 2 ITEMS *** sMerkleRoot = oBlock.computeMerkleRoot(lstItems); if(sMerkleRoot.equals(sExpectedMerkleRoot_2Items)){ System.out.println("Merkle root method for 2 items worked!"); } else{ System.out.println("Merkle root method for 2 items failed!"); System.out.println("Expected: " + sExpectedMerkleRoot_2Items); System.out.println("Received: " + sMerkleRoot); } // *** BEGIN TEST 4 ITEMS *** lstItems.add("t3"); lstItems.add("t4"); sMerkleRoot = oBlock.computeMerkleRoot(lstItems); if(sMerkleRoot.equals(sExpectedMerkleRoot_4Items)){ System.out.println("Merkle root method for 4 items worked!"); } else{ System.out.println("Merkle root method for 4 items failed!"); System.out.println("Expected: " + sExpectedMerkleRoot_4Items); System.out.println("Received: " + sMerkleRoot); } } } Use IntellJ please. Because there only 1 question per post so I posted 1 by 1. Thank you. I posted anthoer 4 questions by seperated.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
