-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlockChain.cpp
210 lines (196 loc) · 5.77 KB
/
BlockChain.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
#include <iostream>
#include <string>
#include <time.h>
#include <fstream>
#include "sha256.h"
using namespace std;
string timestamp();
struct Block{
string data;
string prev_hashKey;
string hashKey;
int BlockNum;
Block* next;
Block* prev;
long long nonce;
};
class chain{
public:
Block* GenesisBlock = NULL;
void insert(string data)
{
data += timestamp();
//data+= " ";
int length = data.length();
Block* NewBlock = new Block;
Block* pntr = GenesisBlock;
Block* previous_ptr = GenesisBlock;
if (GenesisBlock == NULL) {
NewBlock->hashKey = sha256(data);
string temp;
while (!(NewBlock->hashKey[0] == '0' && NewBlock->hashKey[1] == '0' && NewBlock->hashKey[2] == '0' && NewBlock->hashKey[3] == '0' && NewBlock->hashKey[4] == '0'))
{
temp = data;
NewBlock->nonce += 1;
std::string s = std::to_string(NewBlock->nonce);
temp += s;
std::cout << "data = "<<temp << '\n';
std::cout << "nonce = "<< NewBlock->nonce << '\n';
NewBlock->hashKey = sha256(temp);
}
std::cout << "nonce = "<< NewBlock->nonce << '\n';
NewBlock->prev_hashKey = "0000000000000000000000000000000000000000000000000000000000000000";
NewBlock->data = data;
NewBlock->BlockNum = 0;
NewBlock->next = NULL;
GenesisBlock = NewBlock;
}
else {
int counter = 0;
while(pntr != NULL) {
previous_ptr = pntr;
pntr = pntr->next;
counter++;
}
previous_ptr->next = NewBlock;
NewBlock->hashKey = sha256(data);
string temp;
while (!(NewBlock->hashKey[0] == '0' && NewBlock->hashKey[1] == '0' && NewBlock->hashKey[2] == '0' && NewBlock->hashKey[3] == '0'&& NewBlock->hashKey[4] == '0'))
{
temp = data;
NewBlock->nonce += 1;
std::cout << "nonce = "<< NewBlock->nonce << '\n';
std::string s = std::to_string(NewBlock->nonce);
temp += s;
std::cout << "data = "<<temp << '\n';
NewBlock->hashKey = sha256(temp);
}
std::cout << "nonce = "<< NewBlock->nonce << '\n';
NewBlock->prev_hashKey = previous_ptr->hashKey;
NewBlock->prev = previous_ptr;
NewBlock->data = data;
NewBlock->BlockNum = counter;
NewBlock->next = NULL;
}
}
void print(){
Block* curr = GenesisBlock;
if (curr == NULL)
{
cout<<"-------------------------------------"<<endl;
std::cout << "Nothing to print" << '\n';
cout<<"-------------------------------------"<<endl;
}
else
{
cout<<"BlockNum: "<<curr->BlockNum<<endl;
cout<<"Data: "<<curr->data<<endl;
cout<<"Hash Key: "<<curr->hashKey<<endl;
cout<<"Previous Hash Key: "<<curr->prev_hashKey<<endl;
cout<<"Nonce :"<<curr->nonce<<endl;
if (curr->next != NULL) {
do{
curr = curr->next;
cout<<"-------------------------------------"<<endl;
cout<<"BlockNum: "<<curr->BlockNum<<endl<<endl;
cout<<"Data: "<<curr->data<<endl;
cout<<"Hash Key: "<<curr->hashKey<<endl;
cout<<"Previous Hash Key: "<<curr->prev_hashKey<<endl;
cout<<"Nonce :"<<curr->nonce<<endl;
cout<<"-------------------------------------"<<endl;
}while(curr->next != NULL);
}
}
}
void printBlock(int num)
{
Block* curr = GenesisBlock;
if (curr == NULL)
{
cout<<"-------------------------------------"<<endl;
std::cout << "Nothing to print" << '\n';
cout<<"-------------------------------------"<<endl;
}
else
{
for (size_t i = 1; i < num; i++) {
curr = curr->next;
}
cout<<"-------------------------------------"<<endl;
cout<<"BlockNum: "<<curr->BlockNum<<endl<<endl;
cout<<"Data: "<<curr->data<<endl;
cout<<"Hash Key: "<<curr->hashKey<<endl;
cout<<"Previous Hash Key: "<<curr->prev_hashKey<<endl;
cout<<"Nonce :"<<curr->nonce<<endl;
cout<<"-------------------------------------"<<endl;
}
}
void writetofile()
{
Block* curr = GenesisBlock;
if (curr == NULL)
{
cout<<"-------------------------------------"<<endl;
std::cout << "Nothing to Write" << '\n';
cout<<"-------------------------------------"<<endl;
}
else
{
fstream file;
file.open("blocks.txt", ios::out);
file<<"BlockNum: "<<curr->BlockNum<<endl;
file<<"Data: "<<curr->data<<endl;
file<<"Hash Key: "<<curr->hashKey<<endl;
file<<"Previous Hash Key: "<<curr->prev_hashKey<<endl;
file<<"Nonce :"<<curr->nonce<<endl;
if (curr->next != NULL) {
do{
curr = curr->next;
file<<"-------------------------------------"<<endl;
file<<"BlockNum: "<<curr->BlockNum<<endl<<endl;
file<<"Data: "<<curr->data<<endl;
file<<"Hash Key: "<<curr->hashKey<<endl;
file<<"Previous Hash Key: "<<curr->prev_hashKey<<endl;
file<<"Nonce :"<<curr->nonce<<endl;
file<<"-------------------------------------"<<endl;
}while(curr->next != NULL);
}
file.close();
}
}
void readFromFile() {
fstream file;
string str;
file.open("blocks.txt",ios::in);
if (file.is_open()) {
while (!file.eof()) {
getline(file, str);
std::cout << str << '\n';
}
}
}
};
string timestamp()
{
time_t now = time(0);
char* dt = ctime(&now);
return dt;
}
int main(){
chain ch;
int var,x;
string data;
// cout<<"-------------------------------------"<<endl;
// cout<<"-------------------------------------"<<endl;
// cout<<"-------------------------------------"<<endl;
// cout<<"---------WELCOME TO OURCOIN----------" <<endl;
// cout<<"-------------------------------------"<<endl;
// cout<<"-------------------------------------"<<endl;
// cout<<"-------------------------------------"<<endl;
ch.insert("give me 50 ourcoins ");
ch.insert("give ahmed 50 ourcoins ");
//ch.print();
//ch.printBlock(1);
ch.writetofile();
ch.readFromFile();
}