Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Programming excercise Create two files: Miner.h (class definition) and Miner.cpp (class implementation) and use the UML below to guide your implementation. Miner - name

C++ Programming excercise

Create two files: Miner.h (class definition) and Miner.cpp (class implementation) and use the UML below to guide your implementation. Miner - name : string - currentBlock : Block* - wallet : Wallet* - blockReward : float + Miner(): + Miner(n: string): + ~Miner(): + setName(n: string): void + getName(): string + createBlock(p: Transaction**, prev: Block*, reward: float): void + getBlock(): Block* + deleteBlock(): void + confirmBlock(): void + getWallet(): Wallet* + mine(nonce : int): string Member Variables: name - the name of an individual miner or a group of miners. This can also be left blank as some miners wish to remain anonymous. currentBlock - a pointer to a Block object that a miner is currently mining or has just created wallet - a pointer to a Wallet object which contains the account details of the miner, through which he/she can be paid his/her mining reward. blockReward - the amount that the miner gets for confirming a block. It is first set to zero and gets updated with every mining call. Member Functions: The Miner has a default constructor which initializes the miners name to an empty string and the blockReward to zero. It also has a constructor which initializes the miners name to the given input parameter. Both constructors dynamically create a Wallet object and initialize the currentBlock object to a null pointer. ~Miner() - should free memory used for the current block (possibly through the deleteBlock() function) and the wallet. setName(string n) and getName() - sets and retrieves the name of the miner createBlock(Transaction** p, Block* prev, float reward) - accepts onedimensional dynamic array with Transaction* objects, a Block pointer to the previous block (i.e. the last block in the blockchain) and the amount of the reward for mining the block. The process of creating a block is as follows: 1. Check if memory for the current block has been allocated already, if so, release the memory. 2. Instantiate a new Block object. 3. Check if the pointer to a previous block is defined (i.e. not a null pointer), if so, the current blocks id should be an increment of the previous blocks id, and store the previous blocks hash value 4. The miner should add a reward transaction to himself as the first transaction in the block. The fromAddress of the transaction should be empty. 5. The miner should then add as many transactions from the transaction pool as the block allows. Before adding, first check that the pointer to a transaction in a pool is not null (this is because transactions in the pool that have been confirmed and added to the blockchain are deallocated). 6. Update the removeFromPool status of a pool transaction to indicate that it can now be removed from the pool. getBlock() - returns a pointer to the current block deleteBlock() - deallocates the memory of the current block. If your implementation is correct, this should automatically call the destructors of any Transaction objects in the block. confirmBlock() - serves to mark the current block as confirmed (it should call the relevant block function that modifies this status). It is at this point that the miner can send the reward to his wallet. getWallet() - returns the miners wallet. mine(int nonce) - computes the blocks hash value based on the transaction ID hashes, the previous blocks hash value and the nonce value. All these components are joined using hyphens (-) and the result has a format similar to the one shown in the image below (prior to undergoing the final hash), where 163 is the input nonce value.

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

Databases And Python Programming MySQL MongoDB OOP And Tkinter

Authors: R. PANNEERSELVAM

1st Edition

9357011331, 978-9357011334

More Books

Students also viewed these Databases questions