Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Banu Prasath Sivakumar #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions Banu Prasath Sivakumar
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
CODE:

#include <iostream>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <ctime>

//Transaction Structure
struct Transaction {
std::string Sender;
std::string Receiver;
double Amount;

Transaction(const std::string& Sender, const std::string& Receiver, double Amount) : Sender(Sender), Receiver(Receiver), Amount(Amount) {}
};

//Block Structure
struct Block {
std::string hash; //Current Block,
std::string previousHash; //Previous Block.
std::vector<Transaction> transactions;//Vector of Transactions.

Block(const std::vector<Transaction>& transactions, const std::string& previousHash) : transactions(transactions), previousHash(previousHash) {
hash = generateRandomHash();//Random Hash for Current Block.
}

std::string generateRandomHash() {
const std::string characters = "0123456789ABCDEF";
const int length = 13;
std::string hash;
for (int i = 0; i < length; ++i) {
int index = std::rand() % characters.length();
hash += characters[index];
}
return hash;
}
};

// Blockchain class
class Blockchain {
private:
std::vector<Block> chain;

public:
Blockchain() {
std::vector<Transaction> genesisTransactions;//Genesis Block
Block genesisBlock(genesisTransactions, "0");
chain.push_back(genesisBlock);
}

void addBlock(const std::vector<Transaction>& transactions) {
const std::string& previousHash = chain.back().hash;
Block newBlock(transactions, previousHash);
chain.push_back(newBlock);
}

bool validateChain() {
for (size_t i = 1; i < chain.size(); ++i) {
const std::string& previousHash = chain[i - 1].hash;
if (previousHash != chain[i].previousHash) {
return false;
}
}
return true;
}

void printChain() {
for (const Block& block : chain) {
std::cout << "Block Hash: " << block.hash << std::endl;
std::cout << "Previous Hash: " << block.previousHash << std::endl;
std::cout << "Transactions: " << std::endl;
for (const Transaction& transaction : block.transactions) {
std::cout << "Sender: " << transaction.Sender << ", ";
std::cout << "Receiver: " << transaction.Receiver << ", ";
std::cout << "Amount: " << transaction.Amount << std::endl;
}
std::cout << "--------------------------" << std::endl;
}
}
};

int main() {
std::srand(static_cast<unsigned int>(std::time(nullptr))); // Random number generator.

// Create blockchain
Blockchain blockchain;

// Add some transactions
std::vector<Transaction> transactions1 = {
{"Banu", "Prasath", 10000.0},
{"Prasath", "Sivakumar", 8000.0},
{"Sivakumar", "Banu", 9000.0}
};
blockchain.addBlock(transactions1);

std::vector<Transaction> transactions2 = {
{"Radha", "Logesh", 3000.0},
{"Logesh", "Parvatha", 1000.0},
{"Parvatha", "Banu", 5000.0}
};
blockchain.addBlock(transactions2);

std::vector<Transaction> transactions3 = {
{"Raja", "Whisley", 800.0},
{"Whisley", "Nirmal", 500.0},
{"Nirmal", "Sathish", 900.0}
};
blockchain.addBlock(transactions3);

// Print the blockchain
blockchain.printChain();

// Validate the blockchain
bool isValid = blockchain.validateChain();
std::cout << "Is Valid: " << (isValid ? "Yes" : "No") << std::endl;

return 0;
}

OUTPUT:

/tmp/w0VR9Wl4Ea.o
Block Hash: CFBE079BD5233
Previous Hash: 0
Transactions:
--------------------------
Block Hash: 0F7480C7A5549
Previous Hash: CFBE079BD5233
Transactions:
Sender: Banu, Receiver: Prasath, Amount: 10000
Sender: Prasath, Receiver: Sivakumar, Amount: 8000
Sender: Sivakumar, Receiver: Banu, Amount: 9000
--------------------------
Block Hash: D5321F1CD236D
Previous Hash: 0F7480C7A5549
Transactions:
Sender: Radha, Receiver: Logesh, Amount: 3000
Sender: Logesh, Receiver: Parvatha, Amount: 1000
Sender: Parvatha, Receiver: Banu, Amount: 5000
--------------------------
Block Hash: 0BF3E0354B512
Previous Hash: D5321F1CD236D
Transactions:
Sender: Raja, Receiver: Whisley, Amount: 800
Sender: Whisley, Receiver: Nirmal, Amount: 500
Sender: Nirmal, Receiver: Sathish, Amount: 900
--------------------------
Is Valid: Yes


98 changes: 98 additions & 0 deletions Detailed Report
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
Introduction:
This report presents the implementation of a basic blockchain data structure using C++. The
blockchain consists of blocks, each with unique hash and pointer to the previous block in the chain.
Each block contains a list of transactions represented as a linked list.

Blockchain Implementation:
Transaction Structure: A structure defining the properties of a transaction, including the
Sender, Receiver and Amount.

Block Structure: A structure defining the properties of a block, including a vector of
transaction(Amount), the previous block's Hash (previoushash) and current hash (hash).

Blockchain Class: A class representing the blockchain, which manages the chain of blocks and
provide functionality to add new blocks(addBlock()) and validate the chain(validateChain()).

Functionality:

Initialization: The blockchain is initialized with a genesis block containing no transactions
and a hash value "0".

Adding Blocks: The blockchain allows the addition of new blocks by providing a list of
transactions. The new block's hash is calculated based on the previous block'c hash and it is
added to the chain.

Validation: The blockchain includes a function to validate the integrity of the chain by
verifying that each block's previous hash matches the hash of the previous block.

Security Implications: The use of blockchain for financial transactions offers several security
benefits:

Immutability: Transactions added to the blockchain are difficult to tamper with, ensuring the
integrity of the transaction history.

Decentralization: The decentralized nature of blockchain networks enhances security by
removing single points of failure and requiring consensus among multiple nodes.

Cryptographic Security: Blockchain networks employ cryptographic algorithms for secure
transaction verification, digital signatures, and tamper-evident record keeping.

Transparency: Blockchain transactions are transparent, allowing for auditing and accountability.

Conclusion:
The implemented basic blockchain data structure in C++ provides a foundation for building
more complex blockchain applications. The code demonstrates the creation of blocks, linking them
together, and validating the chain. It serves as a starting point for further exploration and
development of blockchain systems.



Code Explanation:

1) Includes and Namespace - The necessary header files,
#include <iostream>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <ctime>
The code also uses the "std" namespace.

2) Transaction Structure - The "Transaction" structure represents a single transaction in the
blockchain and the three members are Sender name (sender name), Receiver (receiver name) and
Amount(transaction amount).

3) Block Structure - It represents a single transaction in th blockchain. It contains the hash of
the current block (hash), hash of the previous block (previous block) and vector of transaction
(transactions). The "Block" constructor initilizes these values by taking the transactions and
the previous hash as parameters. It also includes a helper function "generateRandomHash()" that
generates a random hash value for the block.

4) Blockchain class - The "Blockchain" class manages the chain of blocks. It has a private members
"chain", which is a vector of "Block" objects.

Blockchain() - is the constructor that initializes the blockchain with a genesis block the first
block with no transactions and a hash value of 0.

addBlock() - adds a new block to the chain. It takes a vector of transactions as input,
calculates the previous block's hash, and creates a new block with the provided transactions
and the previous hash. The new block is then added to the chain.

validateChain() - validates the integrity of the blockchain by traversing the chain and checking
if each block's previous hash matches the hash of the previous block. It returns "true" if the
chain is valid and "false" otherwise.

printChain() - prints the details of each block in the chain, including the both hash, previous
hash, and transactions.

5)Main Function:
In the "main()" function, the "srand()" function is used to seed the random number generator.
Thenm an instance of the "Blockchain" class is created.

Some sample transaction("transactions1", "transactions2" and "transactions3") are created
as vectors of "Transaction" objects, representing two set of transactions. These transactions
are added to the blockchain using the "addBlock()" function.

Finally, the blockchain is printed using the "printChain()" function, and the validity of the
blockchain is checked using the "validateChain()" function. The result is printed as "Yes" if
the chain is valid and "No" otherwise.