-
Notifications
You must be signed in to change notification settings - Fork 1
/
basic_methods.cpp
165 lines (139 loc) · 3.65 KB
/
basic_methods.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
#include "header/basic_methods.h"
using namespace std;
struct Node
{
char plotno[20];
string datetime;
char owner[25];
string curHASH;
string prevHASH;
struct Node *next;
};
struct Node* head = NULL;
int arr[101]; //stores nonce
int f = 0; // stores no of plot sold
int g = 0; // no idea
int barr = 900000; // no idea
string printtime()
{
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time (&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer,sizeof(buffer),"%d-%m-%Y %H:%M:%S",timeinfo);
std::string str(buffer);
return str;
}
string insert(char *pno, char *name, string phash)
{
struct Node* new_node = (struct Node*) calloc(1, sizeof(struct Node));
strcpy(new_node->plotno, pno);
strcpy(new_node->owner, name);
new_node->datetime = printtime();
string o(new_node->owner);
string pn(new_node->plotno);
int nonce = 0;
while (1)
{
char non[2] = { (char(nonce)) };
string nonc(non);
new_node->curHASH = createhash(o + new_node->prevHASH + pn + new_node->datetime + nonc);
if (new_node->curHASH[0] == '0' && new_node->curHASH[1] == '0' && new_node->curHASH[2] == '0' && new_node->curHASH[3] == '0' && new_node->curHASH[4] == '0')
{
arr[f] = nonce;
f++;
break;
}
nonce++;
}
new_node->prevHASH = phash;
new_node->next = head;
head = new_node;
return new_node->curHASH;
}
void display()
{
cout << "---------------------BLOCK DETAILS-----------------------" <<endl;
cout << "-----------------------------------------------------------------------------------------------------------" << endl;
int n = 0;
struct Node* ptr;
ptr = head;
while (ptr != NULL)
{
n++;
ptr = ptr->next;
}
ptr = head;
while (ptr != NULL)
{
cout << "\n";
cout << "Block : " << n << endl;
cout << "Owner : " << ptr->owner << endl;
cout << "Plot No : " << ptr->plotno << endl;
cout << "DateTime : " << ptr->datetime << endl;
cout << "PrevHASH : " << ptr->prevHASH << endl;
cout << "CurrHASH : " << ptr->curHASH << endl;
cout << "\n";
n--;
ptr = ptr->next;
}
cout << "-----------------------------------------------------------------------------------------------------------" << endl;
}
void createGenesis()
{
char pno[20] = "0", name[25] = "GENESIS BLOCK RIMAS";
string phash = "bf4b640926f1aec11da6359154e592fea9b4512f572934694b66706445f284d6";
phash = insert(pno, name, phash);
}
int askFunctionality()
{
cout << "------------------------For new transaction : press 1------------------------" << endl;
cout << "----------------------- For viewing block : press 2------------------------" << endl;
cout << "----------------------- For verifying block-chain : press 3------------------------" << endl;
cout << "----------------------- End Program : press 4------------------------" << endl;
cout << "ENTER YOUR CHOICE " << endl;
int n;
cin >> n;
return n;
}
int verify_bc()
{
g = f;
string calHASH;
struct Node* ptr;
ptr = head;
int nonce = 0;
while (ptr != NULL)
{
while (1)
{
char non[2] = { (char(nonce)) };
string nonc(non);
calHASH = createhash(ptr->owner + ptr->prevHASH + ptr->plotno + ptr->datetime + nonc);
if (calHASH[0] == '0' && calHASH[1] == '0' && calHASH[2] == '0' && calHASH[3] == '0' && calHASH[4] == '0')
{
if (nonce == barr)
{
g--;
break;
}
else if (nonce == arr[g])
{
g--;
break;
}
else
{
cout << " BLOCK VERIFIED";
cout << endl;
break;
}
}
nonce++;
}
ptr = ptr->next;
}
ptr = head;
return 2;
}