This repository was archived by the owner on Feb 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.cpp
More file actions
143 lines (108 loc) · 5.08 KB
/
block.cpp
File metadata and controls
143 lines (108 loc) · 5.08 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
#include <iostream>
#include <chrono>
using namespace std;
#define DEC 10
class Block{
private:
bool nonce_calculated = false;
string transaction_time;
int transaction_time_hash;
long long nonce;
long long sender;
long long reciever;
double amount;
int current_block_hash;
int prev_block_hash;
void initialize_components(long long _sender, long long _reciever, double _amount, long long _prev_block_hash){
auto now = chrono::system_clock::now();
time_t time = chrono::system_clock::to_time_t(now);
transaction_time = ctime(&time);
transaction_time.erase(transaction_time.size() - 1, 1);
sender = _sender;
reciever = _reciever;
amount = _amount;
prev_block_hash = _prev_block_hash;
nonce = 0; // default nonce
}
public:
Block(){
initialize_components(0, 0, 0, 0);
}
Block(long long _sender, long long _reciever, double _amount, long long _prev_block_hash){
initialize_components(_sender, _reciever, _amount, _prev_block_hash);
}
Block(long long _sender, long long _reciever, double _amount, long long _nonce, long long _prev_block_hash){
initialize_components(_sender, _reciever, _amount, _prev_block_hash);
this->nonce = _nonce;
nonce_calculated = true;
}
void set_current_block_hash(long long _current_block_hash){
current_block_hash = _current_block_hash;
}
/*
Информация о формате записи одного блока в файл. Один блок должен содержать в себе
___________________________________________
| -->Block<-- |
|-----------------------------------------|
|отправитель монет (id или публичный ключ)|
|-----------------------------------------|
| получатель монет |
|-----------------------------------------|
| количество монет |
|-----------------------------------------|
| время транзакции |
|-----------------------------------------|
| нонс |
|-----------------------------------------|
| хэш предыдущего блока |
|-----------------------------------------|
| хэш текущего блока |
|_________________________________________|
В формате "Block{sender, reciever, amount, transaction_time, nonce, prev_block_hash, current_block_hash};\n"
*/
string toString(){
string result = "Block{";
result.append(to_string(sender));
result.append(", ");
result.append(to_string(reciever));
result.append(", ");
result.append(to_string(amount));
result.append(", ");
result.append(transaction_time);
result.append(", ");
result.append(to_string(nonce));
result.append(", ");
result.append(to_string(prev_block_hash));
result.append(", ");
result.append(to_string(current_block_hash));
result.append("};\n");
return result;
}
static Block* stringToBlock(string str){
string sender_s, reciever_s, amount_s, transaction_time_s, nonce_s;
string prev_block_hash_s, current_block_hash_s;
int start_position = str.find("Block{");
start_position += ((new string("Block"))->size()) + 1;
int end_position = str.find(",", start_position);
sender_s = str.substr(start_position, end_position - start_position);
start_position = end_position + 2;
end_position = str.find(",", start_position);
reciever_s = str.substr(start_position, end_position - start_position);
start_position = end_position + 2; // skip ", " (2 symbols)
end_position = str.find(",", start_position);
amount_s = str.substr(start_position, end_position - start_position);
start_position = end_position + 2;
end_position = str.find(",", start_position);
transaction_time_s = str.substr(start_position, end_position - start_position);
start_position = end_position + 2;
end_position = str.find(",", start_position);
nonce_s = str.substr(start_position, end_position - start_position);
start_position = end_position + 2;
end_position = str.find(",", start_position);
prev_block_hash_s = str.substr(start_position, end_position - start_position);
start_position = end_position + 2;
end_position = str.find("}", start_position);
current_block_hash_s = str.substr(start_position, end_position - start_position);
return (new Block(stoll(sender_s, nullptr, DEC), stoll(reciever_s, nullptr, DEC),stod(amount_s), stoll(nonce_s, nullptr, DEC), stoll(prev_block_hash_s, nullptr, DEC)));
}
};