-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBanu Prasath Sivakumar
More file actions
151 lines (128 loc) · 4.35 KB
/
Banu Prasath Sivakumar
File metadata and controls
151 lines (128 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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