forked from qubic/core-bob
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOProcessor.cpp
More file actions
366 lines (346 loc) · 14.2 KB
/
IOProcessor.cpp
File metadata and controls
366 lines (346 loc) · 14.2 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include <atomic>
#include <chrono>
#include <thread>
#include <vector>
#include <map>
#include <cstring>
#include "connection/connection.h"
#include "structs.h"
#include "GlobalVar.h"
#include "Logger.h"
#include "database/db.h"
#include "K12AndKeyUtil.h"
#include "commonFunctions.h"
#include "Profiler.h"
#include "shim.h"
using namespace std::chrono_literals;
// verify if:
// - have tick data
// - have enough txs
// - quorum reach in tick votes
bool verifyQuorum(uint32_t tick, TickData& td, std::vector<TickVote>& votes)
{
// check and fetch more votes
int count = 0;
for (int i = 0; i < 676; i++)
{
auto& vote = votes[i];
if (vote.tick != tick)
{
db_get_tick_vote(tick, i, vote);
}
if (vote.tick == tick && vote.epoch == gCurrentProcessingEpoch) count++;
}
if (count < 225) {
return false;
}
// NOTE: this is not fully verification, state digest are not yet verified
struct ConsensusData
{
unsigned int prevResourceTestingDigest;
unsigned int prevTransactionBodyDigest;
m256i prevSpectrumDigest;
m256i prevUniverseDigest;
m256i prevComputerDigest;
m256i transactionDigest;
bool operator<(const ConsensusData &other) const {
return memcmp(transactionDigest.m256i_u8, other.transactionDigest.m256i_u8, 32) < 0;
}
};
std::map<ConsensusData, int> digestCount;
for (const auto &vote: votes) {
if (vote.tick == tick && vote.epoch == gCurrentProcessingEpoch)
{
ConsensusData cd{};
cd.prevResourceTestingDigest = vote.prevResourceTestingDigest;
cd.prevTransactionBodyDigest = vote.prevTransactionBodyDigest;
cd.prevSpectrumDigest = vote.prevSpectrumDigest;
cd.prevUniverseDigest = vote.prevUniverseDigest;
cd.prevComputerDigest = vote.prevComputerDigest;
cd.transactionDigest = vote.transactionDigest;
digestCount[cd]++;
}
}
int maxCount = 0;
m256i maxDigest;
bool reachConsensus = false;
for (const auto &pair: digestCount) {
if (pair.first.transactionDigest == m256i::zero() && pair.second >= 226) // empty case
{
maxCount = pair.second;
maxDigest = pair.first.transactionDigest;
reachConsensus = true;
break;
}
if (pair.first.transactionDigest != m256i::zero() && pair.second >= 451) // non-empty case
{
maxCount = pair.second;
maxDigest = pair.first.transactionDigest;
reachConsensus = true;
break;
}
}
if (!reachConsensus) return false;
if (maxDigest == m256i::zero()) return true;
if (td.tick != tick || td.epoch != gCurrentProcessingEpoch)
{
if (!db_get_tick_data(tick, td))
{
return false;
}
}
if (td.tick != tick || td.epoch != gCurrentProcessingEpoch)
{
return false;
}
uint8_t tdHash[32];
KangarooTwelve((uint8_t*)&td, sizeof(TickData), tdHash, 32);
if (memcmp(tdHash, maxDigest.m256i_u8, 32) != 0)
{
Logger::get()->critical("Consensus error: tickData {} is mismatched (there are potentially 2 tick data). Delete the current one in DB.", td.tick);
db_delete_tick_data(tick);
return false;
}
for (int i= 0; i < NUMBER_OF_TRANSACTIONS_PER_TICK; i++)
{
if (!(td.transactionDigests[i] == m256i::zero()))
{
char qhash[64] = {0};
getIdentityFromPublicKey(td.transactionDigests[i].m256i_u8, qhash, true);
std::string hash_str(qhash);
if (!db_check_transaction_exist(hash_str))
{
return false;
}
}
}
return true; // quorum reach
}
// Requester thread: periodically evaluates what to request next and sends requests over the connection.
// Placeholders (TODO) are included where the request conditions and payloads will be implemented.
void IORequestThread(ConnectionPool& conn_pool, std::chrono::milliseconds requestCycle, uint32_t futureOffset)
{
// Optional: pacing/tuning knobs
auto idleBackoff = 10ms; // Backoff when there's nothing immediate to request
const auto errorBackoff = 2000ms; // Backoff after an exception
auto requestClock = std::chrono::high_resolution_clock::now() - requestCycle;
while (!gStopFlag.load(std::memory_order_relaxed)) {
if (gIsEndEpoch) break;
try {
if (refetchTickVotes != -1)
{
RequestedQuorumTick rqt{};
rqt.tick = refetchTickVotes;
memset(rqt.voteFlags, 0, sizeof(rqt.voteFlags));
int count = 0;
auto tvs = db_get_tick_votes(refetchTickVotes);
for (auto& tv: tvs) {
int i = tv.computorIndex;
rqt.voteFlags[i >> 3] |= (1 << (i & 7)); // turn on the flag if the vote exists
count++;
}
if (count < 676)
{
conn_pool.sendToMany((uint8_t *) &rqt, sizeof(rqt), 3, RequestedQuorumTick::type, true);
}
refetchTickVotes = -1;
}
/* Don't need to fetch too far if not yet verifying*/
if (gCurrentFetchingTick > gCurrentVerifyLoggingTick + 1000)
{
SLEEP(idleBackoff);
continue;
}
auto now = std::chrono::high_resolution_clock::now();
if (now - requestClock >= requestCycle)
{
requestClock = now;
for (uint32_t offset = 0; offset < futureOffset; offset++) {
bool have_next_td = false;
{
if (!db_has_tick_data(gCurrentFetchingTick + offset))
{
RequestTickData rtd;
rtd.tick = gCurrentFetchingTick + offset;
conn_pool.sendToMany((uint8_t *) &rtd, sizeof(rtd), 1, RequestTickData::type, true);
} else {
have_next_td = true;
}
}
{
// tick votes
RequestedQuorumTick rqt{};
rqt.tick = gCurrentFetchingTick + offset;
memset(rqt.voteFlags, 0, sizeof(rqt.voteFlags));
int count = 0;
auto tvs = db_get_tick_votes(gCurrentFetchingTick + offset);
for (auto& tv: tvs) {
int i = tv.computorIndex;
rqt.voteFlags[i >> 3] |= (1 << (i & 7)); // turn on the flag if the vote exists
count++;
}
if (count < 676)
{
conn_pool.sendToMany((uint8_t *) &rqt, sizeof(rqt), 1, RequestedQuorumTick::type, true);
}
}
{
// transactions: requires to have tickdata
if (have_next_td) {
TickData td{};
db_get_tick_data(gCurrentFetchingTick + offset, td);
RequestedTickTransactions rtt;
rtt.tick = gCurrentFetchingTick + offset;
memset(rtt.flag, 0, sizeof(rtt.flag));
int count = 0;
for (unsigned int i = 0; i < NUMBER_OF_TRANSACTIONS_PER_TICK; i++) {
if (td.transactionDigests[i] == m256i::zero()) continue;
char qhash[64] = {0};
getIdentityFromPublicKey(td.transactionDigests[i].m256i_u8, qhash, true);
std::string hash_str(qhash);
if (db_check_transaction_exist(hash_str)) {
rtt.flag[i >> 3] |= (1 << (i & 7)); // turn on the flag if the tx exists
} else
{
count++;
}
}
if (count) conn_pool.sendToMany((uint8_t *) &rtt, sizeof(rtt), 1, RequestedTickTransactions::type, true);
}
}
}
}
SLEEP(idleBackoff);
} catch (const std::exception& ex) {
Logger::get()->warn("IORequestThread exception: {}", ex.what());
std::this_thread::sleep_for(errorBackoff);
} catch (...) {
Logger::get()->warn("IORequestThread unknown exception.");
std::this_thread::sleep_for(errorBackoff);
}
}
}
// this pre-verify tick votes, not fully verifying all digests
void IOVerifyThread()
{
const auto idleBackoff = 10ms;
TickData td{};
std::vector<TickVote> votes;
votes.resize(676);
memset((void*)votes.data(), 0, votes.size() * sizeof(TickVote));
while (!gStopFlag.load())
{
if (gIsEndEpoch) break;
if (!verifyQuorum(gCurrentFetchingTick, td, votes))
{
std::this_thread::sleep_for(idleBackoff);
}
else
{
auto current_tick = gCurrentFetchingTick.load();
db_update_latest_tick_and_epoch(gCurrentFetchingTick, gCurrentProcessingEpoch);
Logger::get()->trace("Progress ticking from {} to {}", gCurrentFetchingTick.load(), gCurrentFetchingTick.load() + 1);
uint32_t tmp_tick;
uint16_t tmp_epoch;
db_get_latest_tick_and_epoch(tmp_tick, tmp_epoch);
if (current_tick == tmp_tick) gCurrentFetchingTick++;
}
}
}
static bool checkAllowedTypeForNonTrusted(int type)
{
if (type == RespondLog::type()) return false;
if (type == LogRangesPerTxInTick::type()) return false;
return true;
}
static bool isRequestType(int type)
{
if (type == REQUEST_COMPUTOR_LIST) return true; // request computor list
if (type == RequestedQuorumTick::type) return true; // request vote
if (type == RequestTickData::type) return true; // request tickdata
if (type == REQUEST_CURRENT_TICK_INFO) return true; // REQUEST_CURRENT_TICK_INFO
if (type == RequestedTickTransactions::type) return true; // request tx
if (type == RequestLog::type()) return true; // request log
if (type == RequestAllLogIdRangesFromTick::type()) return true; // request log range
return false;
}
static bool isDataType(int type)
{
if (type == TickVote::type()) return true; // vote
if (type == TickData::type()) return true; // tickdata
if (type == BROADCAST_TRANSACTION) return true; // tx
if (type == RespondLog::type()) return true; // log
if (type == LogRangesPerTxInTick::type()) return true; // logrange
if (type == RespondContractFunction::type) return true;
return false;
}
// Receiver thread: continuously receives full packets and enqueues them into the global round buffer (MRB).
void connReceiver(QCPtr conn, const bool isTrustedNode)
{
using namespace std::chrono_literals;
const auto errorBackoff = 1000ms;
std::vector<uint8_t> packet;
packet.reserve(64 * 1024); // Optional: initial capacity to minimize reallocations
while (!gStopFlag.load(std::memory_order_relaxed)) {
try {
// Blocking receive of a complete packet from the connection.
RequestResponseHeader hdr{};
conn->receiveAFullPacket(hdr, packet);
if (packet.empty()) {
// Defensive check; shouldn't happen if receiveAFullPacket succeeds.
if (!conn->isReconnectable()) return;
Logger::get()->trace("connReceiver error on : {}. Disconnecting", conn->getNodeIp());
conn->disconnect();
SLEEP(errorBackoff);
conn->reconnect();
continue;
}
if (!isTrustedNode)
{
if (!gAllowReceiveLogFromIncomingConnection) // if operator already allowed to receive, no need to block
{
if (!checkAllowedTypeForNonTrusted(hdr.type()))
{
continue; //drop
}
}
}
// trusted conn allowed all packets
if (isDataType(hdr.type()))
{
// Enqueue the packet into the global MutexRoundBuffer.
bool ok = MRB_Data.EnqueuePacket(packet.data());
if (!ok) {
Logger::get()->warn("connReceiver: failed to enqueue packet (size={}, type={}). Dropped.",
packet.size(),
static_cast<unsigned>(hdr.type()));
}
}
if (isRequestType(hdr.type()))
{
bool ok = MRB_Request.EnqueuePacket(packet.data());
if (!ok) {
Logger::get()->warn("connReceiver: failed to enqueue packet (size={}, type={}). Dropped.",
packet.size(),
static_cast<unsigned>(hdr.type()));
}
else
{
requestMapperTo.add(hdr.getDejavu(), nullptr, 0, conn);
}
}
} catch (const std::logic_error& ex) {
if (!conn->isReconnectable()) return;
Logger::get()->trace("connReceiver error on : {}. Disconnecting", conn->getNodeIp());
conn->disconnect();
SLEEP(errorBackoff);
conn->reconnect();
} catch (...) {
if (!conn->isReconnectable()) return;
Logger::get()->trace("connReceiver unknown exception from ip {}", conn->getNodeIp());
conn->disconnect();
SLEEP(errorBackoff);
conn->reconnect();
}
}
}