, create two files: Block.h (class definition) and Block.cpp (class implementati
ID: 3868389 • Letter: #
Question
, create two files: Block.h (class definition) and Block.cpp
(class implementation). The following UML describes the class:
Member Variables:
• The id variable of the Block is initialized through the input parameter of
the Block constructor.
• txCount is a variable that keeps track of the number of Transaction objects
added to the Block’s transaction list, and has an initial value of zero.
• MAX_TX refers to the maximum number of transaction objects that a Block
can accept and is set to a constant value of 5 for this assignment.
• txList is a one-dimensional dynamically allocated array that contains pointers
to Transaction objects. The array is instantiated to contain MAX_TX objects
during Block initialization.
• blockHash holds the final hash value (described in a later section) of the
block contents, and is initialized to an empty string.
• prevBlockHash contains the hash value of the contents of the last block
in the blockchain, upon which the current block builds. The variable is
initialized to the value of the second argument of the Block constructor.
• minerName refers to the name of the block’s owner. A block can only be
created by a miner. This value is set through the constructor via the last
argument.
6
Block
- id : int
- txCount : int
- MAX_TX : int
- txList : Transaction**
- blockHash : string
- prevBlockHash : string
- minerName : string
- confirmed : bool
+ Block(i : int, prevh : string, n : string):
+ Block(other: Block&)
+ ~Block():
+ getId(): int
+ canAddTransaction(): bool
+ addTransaction(t : Transaction*): void
+ addTransaction(f : string, t : string, s : string, a : float ): void
+ getTransaction(i : int): Transaction*
+ getNumTransactions(): int
+ setBlockHash(h : string): void
+ getBlockHash(): string
+ getPreviousBlockHash(): string
+ getMinerName(): string
+ confirm(): void
+ isConfirmed(): bool
+ printBlockSummary(): void
• confirmed is a variable that indicates if a block has been added to the
blockchain, thus confirmed. It is initially set to false.
Member Functions:
• Block constructors: both the normal and copy constructor should dynamically
allocate memory for the Transaction list array, txList, that will contain
the dynamic Transaction objects associated with the block. The rest
of the variables should be initialized as mentioned above or based on the
copied object (in the case of the copy constructor).
• ~Block() should deallocate all dynamically created memory.
• getId() returns the Block id.
• canAddTransaction() - checks if a Transaction pointer can still be added to
the transaction list (i.e. if the number of transactions does not supersede
the allocated space)
• addTransaction(Transaction* t) - creates a deep copy of the given Transaction
* object and adds the copy to the first available position of the block’s
7
transaction list. If the transaction list is full, the function should not copy
or add the object but print the following statement instead: "Maximum
entries of transactions reached. The block cannot accept any more
transactions."
• addTransaction(string f, string t, string s, float a) - dynamically allocates
a Transaction object with the given parameters and stores the new
object in the next available position of the transaction list. If the transaction
list is full, the function should not create or add the object but
print the following statement instead: "Maximum entries of transactions
reached. The block cannot accept any more transactions."
• getTransaction(int i) - retrieves a Transaction* object from the block’s
transaction list at the given index i. If the index is out of bounds, the
function should print out "Block transaction retrieval unsuccessful:
index out of bounds." and return a null pointer.
• getNumTransactions() - returns the number of transactions currently in the
block.
• setBlockHash(string h) and getBlockHash() - store and return the block’s
hash string variable respectively
• getPreviousBlockHash() - returns the hash string of the previous block as
recorded from the constructor.
• getMinerName() - returns the name of the miner associated with the block.
• confirm() - sets the confirmed status of the block to true. It then cascades
the newly confirmed status to the transactions it holds, by setting the confirm
status of every transaction in its list to true. It would typically be
called upon when a block has been added to the blockchain.
• isConfirmed() - returns the confirmed status of the block
• printBlockSummary() - prints out certain properties of the block and the
transactions it holds. The content and format of the summary should be as
shown in the image below. Set the column widths as you see fit, but take
care to use the same labels as those in the figure.
Explanation / Answer
Block.h:
#include<bits/stdc++.h>
#ifndef BLOCK_H_
#define BLOCK_H_
using namespace std;
class Transaction{
string f;
string t;
string s;
float a;
Transaction(string f, string t, string s, float a);
};
class Block{
int id;
int txCount;
int MAX_TX;
Transaction** txList;
string blockHash;
string prevBlockHash;
string minorName;
bool confirmed;
Block(int i, string prevh, string n);
Block(Block& other);
~Block();
int getId();
bool canAddTransaction();
void addTransaction(Transaction* t);
void addTransaction(string f, string t, string s, float a);
Transaction* getTransaction(int i);
int getNumTransactions();
void setBlockHash(string h);
string getBlockHash();
string getPreviousBlockHash();
string getMinorName();
void confirm();
bool isConfirmed();
void printBlockSummary();
};
#endif /* BLOCK_H_ */
Block.cpp:
#include "Block.h"
Transaction::Transaction(string f1, string t1, string s1, float a1){
f = f1;
t = t1;
s = s1;
a = a1;
}
Block::Block(int i, string prevh, string n){
id= i;
prevBlockHash = prevh;
minorName =n;
txCount = 0;
MAX_TX = 5;
txList = new Transaction[MAX_TX];
blockHash ="";
confirmed = false;
}
Block::Block(Block& other){
id= other.id;
prevBlockHash = other.prevBlockHash;
minorName = other.minorName;
txCount = other.txCount;
MAX_TX = other.MAX_TX;
txList = other.txList;
blockHash = other.blockHash;
confirmed = other.confirmed;
}
Block::~Block(){
delete []txList;
}
int Block::getId(){
return Block::id;
}
bool Block::canAddTransaction(){
if(Block::txCount>=5){
return false;
}
return true;
}
void Block::addTransaction(Transaction* t){
if(canAddTransaction()){
Transaction new_t = new Transaction(t->f,t->t,t->s,t->a);
txList[txCount] = new_t;
txCount++;
}
else{
cout<<"Maximum entries of transactions reached. The block cannot accept any more transactions."<<endl;
}
}
void Block::addTransaction(string f, string t, string s, float a){
if(canAddTransaction()){
Transaction new_t = new Transaction(f,t,s,a);
txList[txCount] = new_t;
txCount++;
}
else{
cout<<"Maximum entries of transactions reached. The block cannot accept any more transactions."<<endl;
}
}
Transaction* Block::getTransaction(int i){
if(txCount>=5){
cout<< "Block transaction retrieval unsuccessful: index out of bounds."<<endl;
return NULL;
}
else{
return txList[i];
}
}
int Block::getNumTransactions(){
return txCount;
}
void Block::setBlockHash(string h){
blockHash = h;
}
string Block::getBlockHash(){
return blockHash;
}
string Block::getPreviousBlockHash(){
return prevBlockHash;
}
string Block::getMinorName(){
return minorName;
}
void Block::confirm(){
confirmed = true;
}
bool Block::isConfirmed(){
return confirmed;
}
void Block::printBlockSummary(){
for(int i =0; i<txCount; i++){
cout << txList[txCount]->f << endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.