Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 ###

####################

c.

CLASSES where you add code:

d. Miner

i. 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.

a.

Hint: use a while loop here that keeps looping while the length of this string youre creating is

less than the target difficulty length.

2. Then, this method should have another while loop that keeps looping until a valid nonce is found.

a.

There needs to be an if --- else in this while loop.

i. 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:

1. Set bAbortPoW back to false.

2. Print out something to the user like:

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

3. Return false so the method ends.

ii. 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 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).

a.

So remember that on oBlock you have to call:

i. Set nonce.

ii. Compute hash.

iii. Set hash to whatever came back from compute hash.

b.

Then check if hash on oBlock now begins with your leading zeroes string you

created above.

i. If it does, then return true.

e. 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.

f.

BlockchainUtil

i. generateHash:

1. This method generates a SHA-256 hash of the string passed in.

2. Code for this was reviewed in class and doesnt need to be changed.

g.

P2PMessageQueue

i. enqueue:

1. This adds a node to the queue.

2. Code will be reviewed in class.

ii. dequeue:

1. This removes and returns a node from the queue.

2. Code will be reviewed in class.

iii. hasNodes:

1. This returns true or false depending on if any nodes exist in queue.

2. Code will be reviewed in class.

h. P2PUtil

i. 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.

3. Testing Your Code

a.

You need to run two instances of this app so that they can communicate with each other.

i. One app can be run from your IDE like you normally do.

ii. The other app must be ran from a PowerShell window or other command line tool as explained below.

b.

Running a separate instance of your app:

i.

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.

ii. Running your JAR file

1. Navigate to your JAR file in file explorer.

a.

Shortcut if using IntelliJ:

i. Find it in your left side file explorer in your IntelliJ IDE:

1. ...out > artifacts > BlockchainBasics_jar > BlockchainBasics.jar

ii. 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

5. Then make sure a different port is chosen for your IDE instance and your command line instance:

a.

For instance, choose 8800 for one and 8888 or the other.

4. NOT PART OF PROJECT GRADING: Incorporating your code into larger in-class Griffin Blockchain app.

a.

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.

b. Details will be provided in class, and this apps code will be made available later.

Block.java:

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); } } }

This is the BlockchainBasics code: https://drive.google.com/open?id=152ErnSJJa6F2LRECI_0K4D2XnUfhul1I

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Real Time Database Systems Architecture And Techniques

Authors: Kam-Yiu Lam ,Tei-Wei Kuo

1st Edition

1475784023, 978-1475784022

More Books

Students also viewed these Databases questions