-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.cpp
294 lines (268 loc) · 8.41 KB
/
block.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include<bits/stdc++.h>
#include "sha256.h"
#include "des.h"
#include "user.h"
#include <iostream>
#include <fstream>
using namespace std;
bool isChainValid(vector<Block> blockchain, int diff)
{
Block curr, prev;
char test[diff+1];
for(int i = 0; i < diff; i++)
test[i] = '0';
test[diff] = '\0';
string str = string(test);
for(int i = 1; i < blockchain.size(); i++)
{
curr = blockchain[i];
prev = blockchain[i-1];
string testhash = curr.calculateHash();
if(curr.hash != testhash)
{
cout<<"Hash of current block does not match for block "<<i;
return false;
}
if(prev.hash != curr.prevHash)
{
cout<<"Hash of previous block does not match for block "<<i;
return false;
}
if(curr.hash.substr(0,diff) != str)
{
cout<<"Block "<<i<<" hasn't been mined";
return false;
}
}
return true;
}
void saveSession(vector<User> users){
ofstream outFile;
outFile.open ("users.txt");
for(auto user:users){
outFile << user.username <<"\n";
outFile << user.getPassword()<<"\n";
outFile << user.num_coins <<"\n";
outFile << user.transactions.size() << endl;
for(auto t: user.transactions){
outFile << t << endl;
}
}
outFile.close();
}
void loadSession(vector<User> &users){
string curLine;
ifstream prevSession("users.txt");
string username;
int numCoins;
int numTransactions;
while (getline (prevSession, curLine)) {
//cout << curLine <<"\n";
username = curLine;
getline (prevSession, curLine);
int pass = (atoi(curLine.c_str()));
getline (prevSession, curLine);
numCoins = (atoi(curLine.c_str()));
getline (prevSession, curLine);
numTransactions = (atoi(curLine.c_str()));
vector<string> transactions;
for(int i=0; i<numTransactions; i++){
getline (prevSession, curLine);
transactions.push_back(curLine);
}
User temp = User(username, pass, numCoins, transactions);
users.push_back(temp);
}
prevSession.close();
}
void saveChain(vector<Block> blockchain){
ofstream outFile;
outFile.open ("blockchain.txt");
for(auto block:blockchain){
outFile << block.hash <<"\n";
outFile << block.prevHash<<"\n";
outFile << block.data <<"\n";
outFile << block.nonce <<"\n";
}
outFile.close();
}
void loadChain(vector<Block> &blockchain){
string curLine;
ifstream prevSession("blockchain.txt");
string hash,prevHash,data;
int nonce;
while (getline (prevSession, curLine)) {
hash = curLine;
getline (prevSession, curLine);
prevHash = curLine;
getline (prevSession, curLine);
data = curLine;
getline (prevSession, curLine);
int nonce = (atoi(curLine.c_str()));
Block temp = Block(data, prevHash, hash, nonce);
blockchain.push_back(temp);
}
prevSession.close();
}
int32_t main()
{
vector<User> users;
//Comment these two lines to save into files
loadSession(users);
loadChain(blockchain);
int diff = 2;
if(blockchain.empty()){
Block a("grape","zero");
a.mineBlock(diff);
blockchain.push_back(a);
Block b("apple",blockchain[blockchain.size()-1].hash);
b.mineBlock(diff);
blockchain.push_back(b);
}
isChainValid(blockchain,diff);
if(users.empty()){ //users.txt is empty
User u = User("Arpan", 12345678);
if(!u.addCoins(4, blockchain[blockchain.size()-1].hash, 12345678)){
cout << "Could not add coins.\n";
}
if(!u.withdrawCoins(1, blockchain[blockchain.size()-1].hash, 12345678)){
cout << "Could not withdraw coins.\n";
}
// u.viewUser(12345678);
users.push_back(u);
saveSession(users);
saveChain(blockchain);
}
else {
//for(auto user:users){
//cout<<user.username<<" "<<user.getPassword()<<" "<<user.num_coins<<"\n";
//}
//for(auto block:blockchain){
//cout<<block.hash<<" "<<block.prevHash<<" "<<block.data<<" "<<block.nonce<<"\n";
//}
}
cout << "\n1. Create Account\n2. Add Coins\n3. Withdraw Coins\n4. Show details\n5. Exit\n";
int choice;
do {
cout << "\nEnter choice: ";
cin >> choice;
switch(choice){
case 1: {
string username;
int password;
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
User u(username, password);
users.push_back(u);
cout << "Account created"<<endl;
break;
}
case 2: {
string username;
int password;
int numCoins;
cout << "Enter username: ";
cin >> username;
int flag = 0;
User u1(username, 123);
for(auto u: users){
if(u.username == username){
flag = 1;
u1 = u;
break;
}
}
if(flag == 0) {
cout << "No such user exists. Exiting...\n";
break;
}
cout << "Enter number of coins: ";
cin >> numCoins;
cout << "Enter password: ";
cin >> password;
if(!u1.addCoins(numCoins, blockchain[blockchain.size()-1].hash, password)){
cout << "Could not add coins."<<endl;
} else {
cout << "Successful"<<endl;
for(int i = 0; i < users.size(); i++){
if(users[i].username == username)
users[i] = u1;
}
saveChain(blockchain);
saveSession(users);
break;
}
}
case 3: {
string username;
int password;
int numCoins;
cout << "Enter username: ";
cin >> username;
int flag = 0;
User u1(username, 123);
for(auto u: users){
if(u.username == username){
flag = 1;
u1 = u;
break;
}
}
if(flag == 0) {
cout << "No such user exists. Exiting...\n";
break;
}
cout << "Enter number of coins: ";
cin >> numCoins;
cout << "Enter password: ";
cin >> password;
if(!u1.withdrawCoins(numCoins, blockchain[blockchain.size()-1].hash, password)){
cout << "Could not withdraw coins."<<endl;
} else {
cout << "Successful"<<endl;
for(int i = 0; i < users.size(); i++){
if(users[i].username == username)
users[i] = u1;
}
}
saveChain(blockchain);
saveSession(users);
break;
}
case 4: {
string username;
int password;
cout << "Enter username: ";
cin >> username;
User u1(username, 123);
int flag = 0;
for(auto u: users){
if(u.username == username){
flag = 1;
u1 = u;
break;
}
}
if(flag == 0) {
cout << "No such user exists. Exiting...\n";
break;
}
cout << "Enter password: ";
cin >> password;
if(!u1.viewUser(password)){
cout << "Wrong password.";
}
break;
}
case 5: break;
default:
cout << "Wrong choice...\n";
break;
}
} while(choice!=5);
saveChain(blockchain);
saveSession(users);
return 0;
}