-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
53 lines (44 loc) · 1.23 KB
/
main.cpp
File metadata and controls
53 lines (44 loc) · 1.23 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
#include <bits/stdc++.h>
#include "include/block.h"
#include "include/sha256.h"
#include "include/merkle.h"
#include "include/utils.h"
#include "src/block.cpp"
#include "src/sha256.cpp"
#include "src/merkle.cpp"
#include "src/utils.cpp"
using namespace std;
/*
===================== BLOCK MINING SIMULATION =====================
*/
int main() {
vector<string> transactions = {
"Alice pays Bob 5 BTC",
"Bob pays Charlie 2 BTC",
"Charlie pays Dave 1 BTC"
};
// Compute Merkle root
vector<u8> root = merkle_root_raw(transactions);
// Create block header
Blockheader bh;
bh.version = 1;
bh.prev_hash = vector<u8>(32, 0);
bh.merkle_root = root;
bh.timestamp = time(nullptr);
bh.bits = 0x1f00ffff;
bh.nonce = 0;
cout << "Mining...\n";
// Proof-of-Work loop
while (true) {
vector<u8> header = serialize_header(bh);
vector<u8> hash = double_sha256_raw(header);
// Difficulty: 2 leading zero BYTES
if (has_leading_zero_bytes(hash, 2)) {
cout << "Block mined!\n";
cout << "Nonce: " << bh.nonce << "\n";
cout << "Hash: " << to_hex(hash) << endl;
break;
}
bh.nonce++;
}
}