-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
192 lines (173 loc) · 4.86 KB
/
Copy pathmain.cpp
File metadata and controls
192 lines (173 loc) · 4.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <chrono>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
struct Block {
std::uint64_t index;
std::string timestamp;
std::string previousHash;
std::string hash;
std::uint32_t difficulty;
std::uint64_t nonce;
};
static std::string nowUtcIso() {
using namespace std::chrono;
auto tp = system_clock::now();
auto t = system_clock::to_time_t(tp);
std::tm tm{};
#if defined(_WIN32)
gmtime_s(&tm, &t);
#else
gmtime_r(&t, &tm);
#endif
std::ostringstream oss;
oss << std::put_time(&tm, "%Y-%m-%dT%H:%M:%SZ");
return oss.str();
}
static std::string simpleHash(const std::string& input) {
std::uint64_t a = 1469598103934665603ull;
for (unsigned char c : input) {
a ^= static_cast<std::uint64_t>(c);
a *= 1099511628211ull;
a ^= (a >> 13);
a *= 0xff51afd7ed558ccdULL;
a ^= (a >> 29);
}
std::ostringstream oss;
oss << std::hex << std::setw(16) << std::setfill('0') << a;
return oss.str();
}
static bool meetsDifficulty(const std::string& h, std::uint32_t difficulty) {
if (difficulty == 0) return true;
if (difficulty > h.size()) return false;
for (std::uint32_t i = 0; i < difficulty; ++i) {
if (h[i] != '0') return false;
}
return true;
}
static std::string computeBlockHash(std::uint64_t index,
const std::string& timestamp,
const std::string& previousHash,
const std::string& payload,
std::uint64_t nonce,
std::uint32_t difficulty) {
std::string seed = std::to_string(index) + "|" + timestamp + "|" + previousHash + "|" + payload + "|" + std::to_string(nonce) + "|" + std::to_string(difficulty);
return simpleHash(seed);
}
static Block createGenesis(std::uint32_t difficulty) {
Block b;
b.index = 0;
b.timestamp = nowUtcIso();
b.previousHash = std::string(16, '0');
b.difficulty = difficulty;
b.nonce = 0;
for (;;) {
b.hash = computeBlockHash(b.index, b.timestamp, b.previousHash, "genesis", b.nonce, b.difficulty);
if (meetsDifficulty(b.hash, b.difficulty)) break;
++b.nonce;
}
return b;
}
struct LedgerEntry {
Block block;
std::string payload;
};
class Ledger {
public:
Ledger(std::uint32_t difficulty = 3) : difficulty(difficulty) {
entries.reserve(64);
push("genesis");
}
const std::vector<LedgerEntry>& data() const { return entries; }
const LedgerEntry& latest() const { return entries.back(); }
void push(const std::string& payload) {
if (entries.empty()) {
Block g = createGenesis(difficulty);
entries.push_back({g, payload});
return;
}
const LedgerEntry& prev = entries.back();
Block b;
b.index = prev.block.index + 1;
b.timestamp = nowUtcIso();
b.previousHash = prev.block.hash;
b.difficulty = difficulty;
b.nonce = 0;
for (;;) {
b.hash = computeBlockHash(b.index, b.timestamp, b.previousHash, payload, b.nonce, b.difficulty);
if (meetsDifficulty(b.hash, b.difficulty)) break;
++b.nonce;
}
entries.push_back({b, payload});
}
bool validate(std::string* reason = nullptr) const {
if (entries.empty()) return true;
for (std::size_t i = 0; i < entries.size(); ++i) {
const LedgerEntry& e = entries[i];
std::string expect = computeBlockHash(e.block.index, e.block.timestamp, e.block.previousHash, e.payload, e.block.nonce, e.block.difficulty);
if (e.block.hash != expect) {
if (reason) *reason = "hash mismatch at index " + std::to_string(e.block.index);
return false;
}
if (!meetsDifficulty(e.block.hash, e.block.difficulty)) {
if (reason) *reason = "difficulty not satisfied at index " + std::to_string(e.block.index);
return false;
}
if (i > 0) {
const LedgerEntry& p = entries[i - 1];
if (e.block.previousHash != p.block.hash) {
if (reason) *reason = "previous link mismatch at index " + std::to_string(e.block.index);
return false;
}
}
}
return true;
}
void print(std::ostream& os) const {
os << "Ledger size: " << entries.size() << "\n";
for (const auto& e : entries) {
os << "#" << e.block.index << "\n";
os << "time: " << e.block.timestamp << "\n";
os << "prev: " << e.block.previousHash << "\n";
os << "hash: " << e.block.hash << "\n";
os << "difficulty: " << e.block.difficulty << "\n";
os << "nonce: " << e.block.nonce << "\n";
os << "data: " << e.payload << "\n";
os << "---\n";
}
}
private:
std::vector<LedgerEntry> entries;
std::uint32_t difficulty;
};
static void populate(Ledger& ledger) {
std::vector<std::string> payloads = {
"init",
"deposit:100",
"withdraw:40",
"transfer:25:alice->bob",
"fee:0.5",
"close"
};
for (const auto& p : payloads) ledger.push(p);
}
static void tamper(Ledger& ledger) {
(void)ledger;
}
int main() {
Ledger ledger(3);
populate(ledger);
std::string reason;
bool ok = ledger.validate(&reason);
if (!ok) {
std::cout << "invalid: " << reason << "\n";
return 1;
}
ledger.print(std::cout);
std::cout << "valid: true\n";
return 0;
}