└── improvement /improvement: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class Block { 12 | public: 13 | int index; 14 | string previousHash; 15 | string timestamp; 16 | string data; // Транзакції чи інші дані 17 | string hash; 18 | int nonce; // Параметр для майнінгу 19 | int difficulty; 20 | 21 | // Обчислення хешу блоку 22 | string calculateHash() const { 23 | stringstream ss; 24 | ss << index << previousHash << timestamp << data << nonce; 25 | return sha256(ss.str()); 26 | } 27 | 28 | Block(int idx, string prevHash, string data, int difficulty) 29 | : index(idx), previousHash(prevHash), data(data), difficulty(difficulty) { 30 | timestamp = currentDateTime(); 31 | nonce = 0; // початковий nonce 32 | hash = mineBlock(); 33 | } 34 | 35 | // Функція для майнінгу блоку (Proof of Work) 36 | string mineBlock() { 37 | string target(difficulty, '0'); // Ціль для хешу, повинна починатись з певної кількості нулів 38 | while (hash.substr(0, difficulty) != target) { 39 | nonce++; 40 | hash = calculateHash(); 41 | } 42 | return hash; 43 | } 44 | 45 | private: 46 | // Обчислення хешу за допомогою SHA256 47 | string sha256(const string& str) const { 48 | unsigned char hash[SHA256_DIGEST_LENGTH]; 49 | SHA256_CTX sha256_ctx; 50 | SHA256_Init(&sha256_ctx); 51 | SHA256_Update(&sha256_ctx, str.c_str(), str.length()); 52 | SHA256_Final(hash, &sha256_ctx); 53 | 54 | stringstream ss; 55 | for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { 56 | ss << hex << setw(2) << setfill('0') << (int)hash[i]; 57 | } 58 | return ss.str(); 59 | } 60 | 61 | // Поточний час 62 | string currentDateTime() const { 63 | time_t now = time(0); 64 | struct tm tstruct; 65 | char buf[80]; 66 | tstruct = *localtime(&now); 67 | strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct); 68 | return string(buf); 69 | } 70 | }; 71 | 72 | class Blockchain { 73 | private: 74 | vector chain; 75 | int difficulty; 76 | 77 | public: 78 | Blockchain(int difficultyLevel) : difficulty(difficultyLevel) { 79 | // Створюємо перший блок (генезис) 80 | chain.push_back(Block(0, "0", "Genesis Block", difficulty)); 81 | } 82 | 83 | void addBlock(const string& data) { 84 | Block newBlock(chain.back().index + 1, chain.back().hash, data, difficulty); 85 | 86 | if (isValidNewBlock(newBlock, chain.back())) { 87 | chain.push_back(newBlock); 88 | cout << "Block added: " << newBlock.hash << endl; 89 | } else { 90 | cout << "Failed to add block" << endl; 91 | } 92 | } 93 | 94 | bool isValidNewBlock(const Block& newBlock, const Block& prevBlock) { 95 | if (prevBlock.index + 1 != newBlock.index) { 96 | cout << "Invalid index" << endl; 97 | return false; 98 | } 99 | if (prevBlock.hash != newBlock.previousHash) { 100 | cout << "Invalid previous hash" << endl; 101 | return false; 102 | } 103 | if (newBlock.hash != newBlock.calculateHash()) { 104 | cout << "Invalid hash" << endl; 105 | return false; 106 | } 107 | return true; 108 | } 109 | 110 | bool isChainValid() { 111 | for (int i = 1; i < chain.size(); i++) { 112 | if (!isValidNewBlock(chain[i], chain[i - 1])) { 113 | return false; 114 | } 115 | } 116 | return true; 117 | } 118 | 119 | void printBlockchain() { 120 | for (const Block& block : chain) { 121 | cout << "Block " << block.index << " [Hash: " << block.hash << "]" << endl; 122 | cout << "Timestamp: " << block.timestamp << endl; 123 | cout << "Data: " << block.data << endl; 124 | cout << "Previous Hash: " << block.previousHash << endl; 125 | cout << "Nonce: " << block.nonce << endl; 126 | cout << "Difficulty: " << block.difficulty << endl; 127 | cout << "---------------------------------" << endl; 128 | } 129 | } 130 | 131 | bool validateBlockchain() { 132 | return isChainValid(); 133 | } 134 | 135 | const vector& getChain() const { 136 | return chain; 137 | } 138 | }; 139 | 140 | int main() { 141 | Blockchain myBlockchain(4); // Створюємо блокчейн з рівнем складності 4 (хеш починається з 4 нулів) 142 | 143 | myBlockchain.addBlock("First Block after Genesis"); 144 | myBlockchain.addBlock("Second Block with transactions"); 145 | 146 | myBlockchain.printBlockchain(); 147 | 148 | if (myBlockchain.validateBlockchain()) { 149 | cout << "Blockchain is valid!" << endl; 150 | } else { 151 | cout << "Blockchain is invalid!" << endl; 152 | } 153 | 154 | return 0; 155 | } 156 | --------------------------------------------------------------------------------