-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmining.hpp
More file actions
36 lines (31 loc) · 941 Bytes
/
mining.hpp
File metadata and controls
36 lines (31 loc) · 941 Bytes
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
#pragma once
#ifndef MINING_HPP
#define MINING_HPP
#include<iostream>
#include "block.hpp"
#include<string>
#include <openssl/sha.h>
#include <cstring>
#include <iomanip>
#include <sstream>
#include"utils.hpp"
using namespace std;
pair<long long,string> mine_block(const Block& block,int difficulty=4){
long long nonce = 0;
string prefix(difficulty, '0');
string hash;
string calculated_hash;
while(true){
// Hash calculation
hash = to_string(block.getIndex()) + block.getDateTime() + block.getLastHash() + block.getData() + to_string(nonce);
calculated_hash = sha256(hash);
// Simple hash simulation: take the first 'difficulty' characters
string simulated_hash = calculated_hash.substr(0, difficulty);
if(simulated_hash == prefix){
break;
}
nonce++;
}
return pair<long long,string>(nonce, calculated_hash);
}
#endif // MINING_HPP