diff --git a/README.md b/README.md new file mode 100644 index 0000000..3730174 --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# Brief Review + +## Intro:- +Blockchain Technology is a method of recording information under a decentralised system which is impossible to change, manipulate or hacked. Blockchain is a distributed ledger that duplicates and distribute the transactional information to all the connected networks. It is a structure that stores all records of transaction in a single block which is connected to the next block of transaction in a chain manner which is termed as Blockchain. This storage is known as Digital Ledger. It is highly secured, distributed, immutable, decentralised and transparent. + +Every transaction in a ledger is authorised by the digital signature of the owner which authenticates the transaction and safeguards from tampering. Hence, its highly secured. + +Blockchain network connects all the peer-to-peer network via internet on a blockchain platform like Ethereum and Solana where we can create DApps. Each and every transactional details is visible on all other user's device except their profile. When the user commit a transaction, it attains finality and it can't be changed by anyone. Once if anyone tries to tamper the network or any transaction, it applies to their network alone and all other devices remains unchanged. This feature ensures the integrity of the data and prevents double spending, providing a high level of security and trust in Blockchain types and sustainability. + +## Structure of Blockchain:- + +### A Block consists of three major components: +1) **Header** - contains metadata such as timpestamp and previous block's hash. +2) **Data section** - contains the actual information such as transactions and smart contracts. +3) **Hash** - its a unique cryptographic value of the entire block for verification purposes. + +The decentralised structure of a blockchain is a way to record transactions. Every block has a specific number of transactions within it, making it a node. Every transaction has a distinct hash value along with a digital signature, and each block is linked to the next block by the hash values of the previous block. Blocks are treated as linked lists in this instance. + +## Process of Transaction:- + +When two people want to perform a transaction using their private and public keys, respectively, the first party would attach the transaction data to the second party's public key. The block includes a timestamp, a digital signature, and other crucial details. It should be emphasised that the block does not contain information on the parties to the transaction's identities. The block is then sent around the entire network, and when the correct user uses his private key to match it with the block, the transaction is successfully completed. + +The Blockchain is able to record financial transactions as well as prperties, cars, and so on which is known as smart contracts. + +## Implementation of a simple Blockchain:- + +### A simple Blockchain class is defined. + +- **index:** An integer to keep track of the block's position in the chain. +- **data:** A string representing the data to be stored in the block. +- **timestamp:** A string representing the time when the block is created. +- **previous_hash:** A string representing the hash of the previous block in the chain. +- **hash:** A string representing the hash of the current block. The hash is calculated based on the block's data and other properties. + +1. Two essential structures are defined: Transaction and Block. + + **Transaction:** Represents a single transaction in the blockchain, with attributes for the sender, receiver, amount, and timestamp when the transaction occurred. + + **Block:** Represents a block in the blockchain, containing an index, timestamp, a vector of Transaction objects, a previous block's hash, and its own hash. + +2. Blockchain contains a private member variable chain, which is a vector of Block objects. The constructor initializes the chain with the genesis block (block with index 0 and empty transactions). + +4. The ***createGenesisBlock()*** method creates and returns the genesis block. + +5. The ***addBlock()*** method is used to add a new block to the blockchain. It takes a vector of transactions as input and creates a new block with the provided transactions, linking it to the last block in the chain. + +6. The ***validateChain()*** method iterates through the entire blockchain and checks if each block's hash matches the calculated hash and if the previous block's hash matches the stored previous hash. If any inconsistency is found, the method returns false, indicating that the blockchain is invalid. Otherwise, it returns true, indicating a valid blockchain. + +7. In the ***main()*** function, an instance of the Blockchain class named blockchain is created. + +8. Two blocks with different transactions are added to the blockchain using the ***addBlock()*** method. + +9. The ***validateChain()*** method is called to check the validity of the blockchain. + +10. The result of the validation is printed on the console. + + + + diff --git a/assignments/Assignment b/assignments/Assignment new file mode 100644 index 0000000..08e4502 --- /dev/null +++ b/assignments/Assignment @@ -0,0 +1,31 @@ +# Introduction +In this assignment, you will be using your knowledge of the C++ programming language to implement a simple blockchain. +A blockchain is a digital ledger of transactions that is decentralized and secure. Each block in the chain contains a cryptographic +hash of the previous block, along with the current transactions. + +# Requirements +Implement a basic blockchain data structure in C++. The blockchain should consist of blocks, each with a unique hash and a pointer +to the previous block in the chain. +Each block should contain a list of transactions, represented as a linked list. +Implement a function to add a new block to the chain. This function should take a list of transactions as input, calculate the hash +of the previous block, and add the new block to the chain. +Implement a function to validate the blockchain. This function should traverse the chain and verify that each block's hash matches +the previous block's hash. +Test your blockchain implementation by adding some transactions and validating the chain. +Research and write a brief report on the security implications of using a blockchain for financial transactions. +Submission Guidelines +Fork this repository and create a new branch for your work. +Create a folder with your name and place all your files inside it. +Write all your code in C++ language. +Include a README.md file explaining how to compile and run your code. +Write a brief report on the security implications of using a blockchain for financial transactions and include it in your folder. +Submit a pull request to the master branch of the original repository. + +# Evaluation +Your assignment will be evaluated based on the following criteria: + +Correctness and completeness of the blockchain implementation +Clarity and readability of the code +Correctness and completeness of the validation function +Quality and completeness of the report on blockchain security + diff --git a/assignments/Code Implementation b/assignments/Code Implementation new file mode 100644 index 0000000..f08187f --- /dev/null +++ b/assignments/Code Implementation @@ -0,0 +1,207 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +struct Transaction { + string sender; + string receiver; + double amount; + time_t timestamp; + + Transaction(string _sender, string _receiver, double _amount) : sender(_sender), receiver(_receiver), amount(_amount) { + timestamp = time(nullptr); + } +}; + +struct Block { + int index; + time_t timestamp; + vector transactions; + string previousHash; + string hash; + string Data, data; + + Block(int _index, vector _transactions, string _previousHash) : index(_index), transactions(_transactions), previousHash(_previousHash) { + timestamp = time(nullptr); + hash = calculateHash(); + } + + string calculateHash() { + Data = ""; + data = to_string(index) + to_string(timestamp) + previousHash; + for (const auto& transaction : transactions) { + Data += "Sender: " + transaction.sender + "\nReceiver: " + transaction.receiver + "\nAmount: " + to_string(transaction.amount) + "\n\n"; + data += transaction.sender + transaction.receiver + to_string(transaction.amount) + to_string(transaction.timestamp); + } + + size_t hashValue = std::hash{}(data); + + stringstream ss; + ss << hex << hashValue; + return ss.str(); + } +}; + +class Blockchain { +private: + vector chain; + +public: + Blockchain() { + chain.push_back(createGenesisBlock()); + } + + Block createGenesisBlock() { + vector transactions; + return Block(0, transactions, "0"); + } + + void addBlock(vector transactions) { + Block previousBlock = chain.back(); + int newIndex = previousBlock.index + 1; + string previousHash = previousBlock.hash; + Block newBlock(newIndex, transactions, previousHash); + chain.push_back(newBlock); + } + + void displayChain() { + for (const auto& block : chain) { + cout<<"-------------------------------------------------\n"; + cout << "Block #" << block.index << endl; + cout<<"-------------------------------------------------\n"; + cout << "Timestamp: " << block.timestamp << endl << endl; + cout << block.Data; + cout << "Previous Hash: " << block.previousHash << endl; + cout << "Hash: " << block.hash << endl; + cout << endl << endl; + } + } + + bool validateChain() { + for (int i = 1; i < chain.size(); i++) { + Block currentBlock = chain[i]; + Block previousBlock = chain[i - 1]; + + if (currentBlock.hash != currentBlock.calculateHash()) { + return false; + } + + if (currentBlock.previousHash != previousBlock.hash) { + return false; + } + } + return true; + } +}; + +int main() { + + Blockchain blockchain; + + //Block1 Transaction + vector block1Transactions; + block1Transactions.push_back(Transaction("Ram", "Sita", 1000)); + block1Transactions.push_back(Transaction("Naruto", "Hinata", 4500)); + blockchain.addBlock(block1Transactions); + + //Block2 Transaction + vector block2Transactions; + block2Transactions.push_back(Transaction("Aryan", "Karthik", 9500)); + block2Transactions.push_back(Transaction("Alya", "Krish", 45000)); + blockchain.addBlock(block2Transactions); + + //Block3 Transaction + vector block3Transactions; + block3Transactions.push_back(Transaction("Krish", "Karthik", 19500)); + block3Transactions.push_back(Transaction("Ram", "Mrithi", 4000)); + blockchain.addBlock(block3Transactions); + + //Display Blocks + blockchain.displayChain(); + + //Validation + cout<<"-------------------------------------------------\n"; + bool isValid = blockchain.validateChain(); + cout << "Is blockchain valid? " << (isValid ? "Yes" : "No") << endl; + + return 0; +} + + +/* + +OUTPUT: + +------------------------------------------------- +Block #0 +------------------------------------------------- +Timestamp: 1689491193 + +Previous Hash: 0 +Hash: 572d4f397483c4bd + + +------------------------------------------------- +Block #1 +------------------------------------------------- +Timestamp: 1689491193 + +Sender: Ram +Receiver: Sita +Amount: 1000.000000 + +Sender: Naruto +Receiver: Hinata +Amount: 4500.000000 + +Previous Hash: 572d4f397483c4bd +Hash: 659fae22fb1b4c98 + + +------------------------------------------------- +Block #2 +------------------------------------------------- +Timestamp: 1689491193 + +Sender: Aryan +Receiver: Karthik +Amount: 9500.000000 + +Sender: Alya +Receiver: Krish +Amount: 45000.000000 + +Previous Hash: 659fae22fb1b4c98 +Hash: e910d5c1c8669065 + + +------------------------------------------------- +Block #3 +------------------------------------------------- +Timestamp: 1689491193 + +Sender: Krish +Receiver: Karthik +Amount: 19500.000000 + +Sender: Ram +Receiver: Mrithi +Amount: 4000.000000 + +Sender: Ram +Receiver: Mrithi +Amount: 4000.000000 + +Previous Hash: e910d5c1c8669065 +Hash: 37bdce5c4b4b6a4c + + +------------------------------------------------- +Is blockchain valid? Yes + +*/