-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.cpp
More file actions
83 lines (68 loc) · 1.97 KB
/
block.cpp
File metadata and controls
83 lines (68 loc) · 1.97 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
//Isaac Mejía F.
#include "block.hpp"
#include <windows.h>
block::block(const std::string data, std::string prevHash){
auto time = std::chrono::system_clock::now();
int timestamp = std::chrono::duration_cast<std::chrono::seconds>(time.time_since_epoch()).count();
this->data = data;
this->prevHash = prevHash;
this->timestamp = timestamp;
this -> ID_BLOCK = genIDBlock(this->data, this->prevHash, this->timestamp);
this->ValidHash = mineValidHash(this->ID_BLOCK);
}
std::string block::getHash(){
return this->ValidHash;
}
std::ostream& operator<<(std::ostream& os, block b){
os<< "[data: "<<b.data<<std::endl;
os<< " MAKER_ROOT: "<<b.ID_BLOCK<<std::endl;
os<< " prevhash: "<<b.prevHash<<std::endl;
os<< " hash: "<<b.ValidHash<<"]";
return os;
}
bool isValidHash(std::string hash){
bool isValid = false;
if(hash.find("00") == 0){
isValid=true;
}
return isValid;
}
std::string mineValidHash(std::string ID_BLOCK){
int magicValue = 0;
std::string hash;
ID_BLOCK.pop_back();
std::cout<<"INCOGNITA: ";
cpos incog;
std::cout<<"\nHASH: ";
cpos jash;
while(true){
ID_BLOCK.push_back(magicValue);
hash = sha256(ID_BLOCK);
if(isValidHash(hash)){
break;
}
else{
ID_BLOCK.pop_back();
++magicValue;
}
incog.putxy();
std::cout<<magicValue;
jash.putxy();
std::cout<<hash<<"\n";
}
incog.putxy();
std::cout<<magicValue;
jash.putxy();
std::cout<<hash<<"\n";
std::cout<<std::endl;
return hash;
}
std::string genIDBlock(const std::string data, std::string prevHash, int timestamp){
std::string ID_BLOCK;
std::string headers = "";
headers += data;
headers += prevHash;
headers += timestamp;
ID_BLOCK = sha256(headers);
return ID_BLOCK;
}