-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.hpp
More file actions
103 lines (87 loc) · 3.86 KB
/
block.hpp
File metadata and controls
103 lines (87 loc) · 3.86 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
#ifndef BLOCK_HPP
#define BLOCK_HPP
#include <iostream>
#include <string>
#include <ctime>
#include <sstream>
#include <vector>
#include <fstream>
#include <filesystem>
#include "SHA1/sha1.hpp"
#include "Transaction.hpp"
class Block {
public:
int index; // Индекс блока
std::string previousHash; // Хэш предыдущего блока
std::string timestamp; // Временная метка создания блока
std::vector<Transaction> transactions; // Список транзакций
std::string hash; // Хэш текущего блока
int nonce; // Нонс для доказательства работы
static const int difficulty = 4; // Уровень сложности (количество ведущих нулей)
int ProofOfWorkCoinAddress; // Адрес. Добавляет транзакцию, отправляющую майнеру 3 монеты
Block(int idx, const std::string& prevHash, const std::vector<Transaction>& txs, int MinerAdrs)
: index(idx), previousHash(prevHash), transactions(txs), nonce(0), ProofOfWorkCoinAddress(MinerAdrs) {
timestamp = getCurrentTime();
hash = mineBlock();
}
std::string calculateHash(bool save) const {
std::stringstream ss;
ss << index << previousHash << timestamp << std::endl;
for (const auto& tx : transactions) {
ss << tx.publicKey << " " << tx.howMany << " " << tx.receiver_publicKey << " " << tx.signature << std::endl;
}
ss << nonce;
std::string does_hash = hashing(ss.str());
if (save == true) saveBlockAtFile(ss.str());
return does_hash;
}
void saveBlockAtFile(const std::string& BlockData) const {
std::string blocks_folder = "Blocks";
std::string file_name = "block" + std::to_string(index) + ".ast";
std::string file_path = blocks_folder + "/" + file_name;
std::filesystem::create_directory(blocks_folder);
std::string code_snippet = BlockData;
std::ofstream outFile(file_path);
if (outFile.is_open()) {
outFile << code_snippet;
outFile.close();
} else {
std::cerr << "Не удалось открыть файл для записи." << std::endl;
}
}
std::string getCurrentTime() const {
std::time_t now = std::time(nullptr);
std::tm* localTime = std::localtime(&now);
std::ostringstream oss;
oss << std::put_time(localTime, "%Y-%m-%d %H:%M:%S");
return oss.str();
}
static std::string hashing(const std::string& str) {
SHA1 checksum;
checksum.update(str);
return checksum.final();
}
std::string mineBlock() {
transactions.push_back(Transaction("0", 3, std::to_string(ProofOfWorkCoinAddress)));
std::string target(difficulty, '0');
do {
hash = calculateHash(false);
nonce++;
} while (hash.substr(0, difficulty) != target);
calculateHash(true); // для сохранения файла, да, не оптимизировано, но кому какое дело :)
return hash;
}
void displayBlock() const {
std::cout << "Блок #" << index << std::endl;
std::cout << "Временная метка: " << timestamp << std::endl;
std::cout << "Транзакции: " << std::endl;
// for (const auto& tx : transactions) {
// tx.displayTransaction();
// }
std::cout << "Предыдущий хэш: " << previousHash << std::endl;
std::cout << "Хэш: " << hash << std::endl;
std::cout << "Нонс: " << nonce << std::endl;
std::cout << "-----------------------------------" << std::endl;
}
};
#endif // BLOCK_HPP