-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
443 lines (417 loc) · 15.7 KB
/
main.cpp
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include <thread>
#include <chrono>
#include "stdio.h"
#include "connection.h"
#include "parser.h"
#include "structs.h"
#include "keyUtils.h"
#include "K12AndKeyUtil.h"
#include "logger.h"
#include <stdexcept>
#define ARBITRATOR "AFZPUAIYVPNUYGJRQVLUKOPPVLHAZQTGLYAAUUNBXFTVTAMSBKQBLEIEPCVJ"
#define DEBUG 0
static uint64_t gLastProcessedLogId = 0;
template<typename T>
T charToNumber(char *a) {
T retVal = 0;
char *endptr = nullptr;
retVal = strtoull(a, &endptr, 10);
return retVal;
}
static void printDebug(const char *fmt, ...)
{
#if DEBUG
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
#endif
}
void getTickData(QCPtr &qc, const uint32_t tick, TickData &result) {
memset(&result, 0, sizeof(TickData));
static struct {
RequestResponseHeader header;
RequestTickData requestTickData;
} packet;
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(REQUEST_TICK_DATA);
packet.requestTickData.requestedTickData.tick = tick;
qc->sendData((uint8_t *) &packet, packet.header.size());
result = qc->receivePacketAs<TickData>();
return;
}
bool getLogFromNodeChunk(QCPtr &qc, uint64_t *passcode, uint64_t fromId, uint64_t toId) {
struct {
RequestResponseHeader header;
unsigned long long passcode[4];
unsigned long long fromid;
unsigned long long toid;
} packet;
memset(&packet, 0, sizeof(packet));
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(RequestLog::type());
memcpy(packet.passcode, passcode, 4 * sizeof(uint64_t));
packet.fromid = fromId;
packet.toid = toId;
qc->sendData((uint8_t *) &packet, packet.header.size());
std::vector<uint8_t> buffer;
qc->receiveAFullPacket(buffer);
uint8_t *data = buffer.data();
int recvByte = buffer.size();
int ptr = 0;
unsigned long long retLogId = -1; // max uint64
while (ptr < recvByte) {
auto header = (RequestResponseHeader *) (data + ptr);
if (header->type() == RespondLog::type()) {
auto logBuffer = (uint8_t *) (data + ptr + sizeof(RequestResponseHeader));
retLogId = printQubicLog(logBuffer, header->size() - sizeof(RequestResponseHeader));
fflush(stdout);
}
ptr += header->size();
}
if (retLogId < toId) {
// round buffer case, only the first half returned, call one more time to print out another half
return getLogFromNodeChunk(qc, passcode, retLogId + 1, toId);
}
if (retLogId == -1) {
LOG("[0] WARNING: Unexpected value for retLogId\n");
return false;
}
return true;
}
void getLogFromNodeOneByOne(QCPtr &qc, uint64_t *passcode, uint64_t _fromId, uint64_t _toId)
{
struct {
RequestResponseHeader header;
unsigned long long passcode[4];
unsigned long long fromid;
unsigned long long toid;
} packet;
for (uint64_t l_id = _fromId; l_id <= _toId; l_id++)
{
if (l_id < gLastProcessedLogId) continue;
memset(&packet, 0, sizeof(packet));
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(RequestLog::type());
memcpy(packet.passcode, passcode, 4 * sizeof(uint64_t));
packet.fromid = l_id;
packet.toid = l_id;
qc->sendData((uint8_t *) &packet, packet.header.size());
std::vector<uint8_t> buffer;
qc->receiveAFullPacket(buffer);
uint8_t *data = buffer.data();
int recvByte = buffer.size();
int ptr = 0;
unsigned long long retLogId = -1; // max uint64
while (ptr < recvByte) {
auto header = (RequestResponseHeader *) (data + ptr);
if (header->type() == RespondLog::type()) {
auto logBuffer = (uint8_t *) (data + ptr + sizeof(RequestResponseHeader));
retLogId = printQubicLog(logBuffer, header->size() - sizeof(RequestResponseHeader));
gLastProcessedLogId = retLogId;
fflush(stdout);
}
ptr += header->size();
}
if (retLogId == -1) {
LOG("[1] WARNING: Unexpected value for retLogId\n");
}
}
}
void getLogFromNode(QCPtr &qc, uint64_t *passcode, uint64_t fromId, uint64_t toId)
{
bool finish = getLogFromNodeChunk(qc, passcode, fromId, toId);
while (!finish)
{
std::this_thread::sleep_for(std::chrono::seconds(3));
LOG("Failed to get logging content, retry in 3 seconds...\n");
finish = getLogFromNodeChunk(qc, passcode, fromId, toId);
}
//getLogFromNodeOneByOne(qc, passcode, fromId, toId);
}
void getLogIdRange(QCPtr &qc, uint64_t *passcode, uint32_t requestedTick, uint32_t txsId, long long &fromId,
long long &toId) {
struct {
RequestResponseHeader header;
unsigned long long passcode[4];
unsigned int tick;
unsigned int txId;
} packet;
memset(&packet, 0, sizeof(packet));
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(RequestLogIdRange::type());
memcpy(packet.passcode, passcode, 4 * sizeof(uint64_t));
packet.tick = requestedTick;
packet.txId = txsId;
qc->sendData((uint8_t *) &packet, packet.header.size());
auto result = qc->receivePacketAs<ResponseLogIdRange>();
if (result.length != -1) {
fromId = result.fromLogId;
toId = fromId + result.length - 1;
} else {
fromId = -1;
toId = -1;
}
}
ResponseAllLogIdRangesFromTick getAllLogIdRangesFromTick(QCPtr &qc, uint64_t *passcode, uint32_t requestedTick) {
struct {
RequestResponseHeader header;
unsigned long long passcode[4];
unsigned int tick;
} packet;
memset(&packet, 0, sizeof(packet));
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(RequestAllLogIdRangesFromTick::type());
memcpy(packet.passcode, passcode, 4 * sizeof(uint64_t));
packet.tick = requestedTick;
qc->sendData((uint8_t *) &packet, packet.header.size());
auto result = qc->receivePacketAs<ResponseAllLogIdRangesFromTick>();
return result;
}
static CurrentTickInfo getTickInfoFromNode(QCPtr &qc) {
CurrentTickInfo result;
memset(&result, 0, sizeof(CurrentTickInfo));
struct {
RequestResponseHeader header;
} packet;
memset(&packet, 0, sizeof(packet));
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(REQUEST_CURRENT_TICK_INFO);
qc->sendData((uint8_t *) &packet, packet.header.size());
std::vector<uint8_t> buffer;
qc->receiveAFullPacket(buffer);
uint8_t *data = buffer.data();
int recvByte = buffer.size();
int ptr = 0;
while (ptr < recvByte) {
auto header = (RequestResponseHeader *) (data + ptr);
if (header->type() == RESPOND_CURRENT_TICK_INFO) {
auto curTickInfo = (CurrentTickInfo *) (data + ptr + sizeof(RequestResponseHeader));
result = *curTickInfo;
}
ptr += header->size();
}
return result;
}
#if DEBUG
static void getTickTransactions(QCPtr &qc, const uint32_t requestedTick, int requestedTxId,
Transaction &txs, //out
extraDataStruct &extraData // out
) {
struct {
RequestResponseHeader header;
RequestedTickTransactions txs;
} packet;
memset(&packet, 0, sizeof(packet));
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(REQUEST_TICK_TRANSACTIONS); // REQUEST_TICK_TRANSACTIONS
packet.txs.tick = requestedTick;
for (int i = 0; i < (1024 + 7) / 8; i++) packet.txs.transactionFlags[i] = 0xff;
{
packet.txs.transactionFlags[requestedTxId >> 3] &= ~(1 << (requestedTxId & 7));
}
qc->sendData((uint8_t *) &packet, packet.header.size());
{
std::vector<uint8_t> buffer;
qc->receiveAFullPacket(buffer);
uint8_t *data = buffer.data();
int recvByte = buffer.size();
int ptr = 0;
while (ptr < recvByte) {
auto header = (RequestResponseHeader *) (data + ptr);
if (header->type() == BROADCAST_TRANSACTION) {
auto tx = (Transaction *) (data + ptr + sizeof(RequestResponseHeader));
txs = *tx;
extraDataStruct ed;
ed.vecU8.resize(tx->inputSize);
if (tx->inputSize != 0) {
memcpy(ed.vecU8.data(), reinterpret_cast<const uint8_t *>(tx) + sizeof(Transaction), tx->inputSize);
}
extraData = ed;
}
ptr += header->size();
}
}
{
// receive END OF transmission
std::vector<uint8_t> buffer;
qc->receiveAFullPacket(buffer);
}
}
void
printReceipt(Transaction &tx, const char *txHash = nullptr, const uint8_t *extraData = nullptr, int moneyFlew = -1) {
char sourceIdentity[128] = {0};
char dstIdentity[128] = {0};
char txHashClean[128] = {0};
bool isLowerCase = false;
getIdentityFromPublicKey(tx.sourcePublicKey, sourceIdentity, isLowerCase);
getIdentityFromPublicKey(tx.destinationPublicKey, dstIdentity, isLowerCase);
LOG("~~~~~RECEIPT~~~~~\n");
if (txHash != nullptr) {
memcpy(txHashClean, txHash, 60);
LOG("TxHash: %s\n", txHashClean);
}
LOG("From: %s\n", sourceIdentity);
LOG("To: %s\n", dstIdentity);
LOG("Input type: %u\n", tx.inputType);
LOG("Amount: %lu\n", tx.amount);
LOG("Tick: %u\n", tx.tick);
LOG("Extra data size: %u\n", tx.inputSize);
if (extraData != nullptr && tx.inputSize) {
char hex_tring[1024 * 2] = {0};
for (int i = 0; i < tx.inputSize; i++)
sprintf(hex_tring + i * 2, "%02x", extraData[i]);
LOG("Extra data: %s\n", hex_tring);
}
if (moneyFlew != -1) {
if (moneyFlew) LOG("MoneyFlew: Yes\n");
else LOG("MoneyFlew: No\n");
} else {
LOG("MoneyFlew: N/A\n");
}
LOG("~~~~~END-RECEIPT~~~~~\n");
}
#endif
uint32_t getTickNumberFromNode(QCPtr &qc, char *isFirstTick = NULL) {
auto curTickInfo = getTickInfoFromNode(qc);
if (isFirstTick) {
*isFirstTick = curTickInfo.initialTick == curTickInfo.tick;
}
return curTickInfo.tick;
}
//TickData td;
void checkSystemLog(QCPtr &qc, uint64_t *passcode, unsigned int tick, unsigned int systemEventID,
std::string systemEventName) {
long long fromId = 0, toId = 0;
getLogIdRange(qc, passcode, tick, systemEventID, fromId, toId);
if (fromId < 0 || toId < 0) {}
else {
printf("Tick %u %s has log from %lld to %lld\n", tick, systemEventName.c_str(), fromId, toId);
getLogFromNode(qc, passcode, fromId, toId);
}
}
unsigned int SC_INITIALIZE_TX = NUMBER_OF_TRANSACTIONS_PER_TICK + 0;
unsigned int SC_BEGIN_EPOCH_TX = NUMBER_OF_TRANSACTIONS_PER_TICK + 1;
unsigned int SC_BEGIN_TICK_TX = NUMBER_OF_TRANSACTIONS_PER_TICK + 2;
unsigned int SC_END_TICK_TX = NUMBER_OF_TRANSACTIONS_PER_TICK + 3;
unsigned int SC_END_EPOCH_TX = NUMBER_OF_TRANSACTIONS_PER_TICK + 4;
bool isValidRange(long long start, long long length)
{
return start >= 0 && length > 0;
}
void printTxMapTable(ResponseAllLogIdRangesFromTick& txmap)
{
LOG("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
LOG("Event\t\tFromId\tToId\n");
if (isValidRange(txmap.fromLogId[SC_INITIALIZE_TX], txmap.length[SC_INITIALIZE_TX]))
{
LOG("SC_INIT\t\t%u\t%u\n", txmap.fromLogId[SC_INITIALIZE_TX], txmap.fromLogId[SC_INITIALIZE_TX] + txmap.length[SC_INITIALIZE_TX] - 1);
}
if (isValidRange(txmap.fromLogId[SC_BEGIN_EPOCH_TX], txmap.length[SC_BEGIN_EPOCH_TX]))
{
LOG("BEGIN_EPOCH\t\t%u\t%u\n", txmap.fromLogId[SC_BEGIN_EPOCH_TX], txmap.fromLogId[SC_BEGIN_EPOCH_TX] + txmap.length[SC_BEGIN_EPOCH_TX] - 1);
}
if (isValidRange(txmap.fromLogId[SC_BEGIN_TICK_TX], txmap.length[SC_BEGIN_TICK_TX]))
{
LOG("BEGIN_TICK\t\t%u\t%u\n", txmap.fromLogId[SC_BEGIN_TICK_TX], txmap.fromLogId[SC_BEGIN_TICK_TX] + txmap.length[SC_BEGIN_TICK_TX] - 1);
}
for (int i = 0; i < NUMBER_OF_TRANSACTIONS_PER_TICK; i++)
{
if (isValidRange(txmap.fromLogId[i], txmap.length[i]))
{
LOG("Tx #%d\t\t%u\t%u\n", i, txmap.fromLogId[i], txmap.fromLogId[i] + txmap.length[i] - 1);
}
}
if (isValidRange(txmap.fromLogId[SC_END_TICK_TX], txmap.length[SC_END_TICK_TX]))
{
LOG("END_TICK\t\t%u\t%u\n", txmap.fromLogId[SC_END_TICK_TX], txmap.fromLogId[SC_END_TICK_TX] + txmap.length[SC_END_TICK_TX] - 1);
}
if (isValidRange(txmap.fromLogId[SC_END_EPOCH_TX], txmap.length[SC_END_EPOCH_TX]))
{
LOG("END_EPOCH\t\t%u\t%u\n", txmap.fromLogId[SC_END_EPOCH_TX], txmap.fromLogId[SC_END_EPOCH_TX] + txmap.length[SC_END_EPOCH_TX] - 1);
}
LOG("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
int run(int argc, char *argv[]) {
if (argc != 8) {
printf("./qlogging [nodeip] [nodeport] [passcode u64 x 4] [tick to start]\n");
return 0;
}
uint8_t arbPubkey[32];
getPublicKeyFromIdentity(ARBITRATOR, arbPubkey);
char *nodeIp = argv[1];
int nodePort = charToNumber<int>(argv[2]);
uint64_t passcode[4] = {charToNumber<unsigned long long>(argv[3]), charToNumber<unsigned long long>(argv[4]),
charToNumber<unsigned long long>(argv[5]), charToNumber<unsigned long long>(argv[6])};
unsigned int tick = charToNumber<unsigned int>(argv[7]);
QCPtr qc;
uint32_t currentTick = 0;
bool needReconnect = true;
char isFirstTick = 0;
while (1) {
try {
if (needReconnect) {
qc = make_qc(nodeIp, nodePort);
// do the handshake stuff
std::vector<uint8_t> buff;
qc->receiveAFullPacket(buff);
needReconnect = false;
}
if (currentTick == 0 || currentTick <= tick) {
isFirstTick = 0;
currentTick = getTickNumberFromNode(qc, &isFirstTick);
}
if (currentTick <= tick) {
printDebug("Current tick %u vs local tick %u | sleep 3s\n", currentTick, tick);
std::this_thread::sleep_for(std::chrono::seconds(3));
continue;
}
//ResponseAllLogIdRangesFromTick getAllLogIdRangesFromTick(QCPtr &qc, uint64_t *passcode, uint32_t requestedTick) {
auto all_ranges = getAllLogIdRangesFromTick(qc, passcode, tick);
long long fromId = INT64_MAX;
long long toId = -1;
for (int i = 0; i < LOG_TX_PER_TICK; i++)
{
if (isValidRange(all_ranges.fromLogId[i], all_ranges.length[i]))
{
fromId = std::min(fromId, all_ranges.fromLogId[i]);
toId = std::max(toId, all_ranges.fromLogId[i] + all_ranges.length[i] - 1);
}
}
if (fromId <= toId && fromId >= 0)
{
// print the txId <-> logId map table here
printTxMapTable(all_ranges);
getLogFromNode(qc, passcode, fromId, toId);
}
else
{
LOG("Tick %u doesn't generate any log\n", tick);
}
tick++;
fflush(stdout);
}
catch (std::logic_error &ex) {
printf("%s\n", ex.what());
fflush(stdout);
needReconnect = true;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
}
int main(int argc, char *argv[]) {
try {
return run(argc, argv);
}
catch (std::exception &ex) {
printf("%s\n", ex.what());
return -1;
}
}