Question: 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

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.

public String sUsername;

/**

* PoW is where miner keeps trying incrementing nonce until hash begins with as many 0s as the difficulty specifies.

* @param oBlock

* @return

*/

public boolean doProofOfWork(Block oBlock) {

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

### ADD CODE HERE ###

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

}

/**

* This thread monitors incoming messages, monitors the transaction queue, and mines Block if enough transactions collected.

* Called as part of Runnable interface and shouldn't be called directly by code.

*/

public void run() {

BlockchainUtil u = new BlockchainUtil();

u.p("Miner thread started.");

// *****************************

// *** Eternal Mining Loop *****

// Because miner always checking for next block to immediately work on.

while(true){

u.sleep(500);

while(oIncomingMessageQueue.hasNodes()){

P2PMessage oMessage = oIncomingMessageQueue.dequeue();

lstTransactionPool.add(oMessage.getMessage());

}

// Check if transaction pool full and lock if it is.

if (lstTransactionPool.size() >= iBlockTxSize) {

Block oBlock = new Block();

oBlock.setMinerUsername(sUsername);

oBlock.computeHash();

String sMerkleRoot = oBlock.computeMerkleRoot(lstTransactionPool);

oBlock.setMerkleRoot(sMerkleRoot);

boolean bMined = doProofOfWork(oBlock);

if(bMined){

// Notify connected node.

if(BlockchainBasics.sRemoteMinerIP != null){

P2PUtil.connectForOneMessage(BlockchainBasics.sRemoteMinerIP, BlockchainBasics.iRemoteMinerPort,

"mined");

}

u.p("");

u.p("***********");

u.p("BLOCK MINED");

u.p("nonce: " + oBlock.getNonce());

u.p("hash: " + oBlock.getHash());

u.p("");

u.p("Transactions:");

for(int x=0; x < lstTransactionPool.size(); x++){

u.p("Tx " + x + ": " + lstTransactionPool.get(x));

}

u.p("***********");

}

else{

u.p("[miner] Mining block failed.") // Clear tx pool.

lstTransactionPool.clear();

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!