-
Notifications
You must be signed in to change notification settings - Fork 23
/
structs.h
728 lines (634 loc) · 16.9 KB
/
structs.h
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#pragma once
#include "defines.h"
#include "utils.h"
#include <cstddef>
#include <cstring>
enum COMMAND
{
SHOW_KEYS = 0,
GET_CURRENT_TICK = 1,
GET_BALANCE = 2,
GET_ASSET = 3,
SEND_COIN = 4,
SEND_CUSTOM_TX = 5,
SEND_SPECIAL_COMMAND = 6,
SEND_RAW_PACKET = 7,
PUBLISH_PROPOSAL = 8,
VOTE_PROPOSAL = 9,
GET_QUORUM_TICK = 10,
GET_TICK_DATA=11,
READ_TICK_DATA=12,
CHECK_TX_ON_TICK=13,
CHECK_TX_ON_FILE=14,
GET_COMP_LIST=15,
QX_ISSUE_ASSET = 16,
QX_TRANSFER_ASSET = 17,
GET_NODE_IP_LIST=18,
GET_LOG_FROM_NODE = 19,
DUMP_SPECTRUM_FILE = 20,
DUMP_UNIVERSE_FILE = 21,
PRINT_QX_FEE =22,
MAKE_IPO_BID=23,
GET_IPO_STATUS=24,
QUOTTERY_ISSUE_BET=25,
QUOTTERY_JOIN_BET=26,
QUOTTERY_GET_BET_INFO=27,
QUOTTERY_GET_BET_DETAIL=28,
QUOTTERY_GET_ACTIVE_BET=29,
QUOTTERY_GET_ACTIVE_BET_BY_CREATOR=30,
QUOTTERY_GET_BASIC_INFO=31,
QUOTTERY_PUBLISH_RESULT=32,
QUOTTERY_CANCEL_BET=33,
TOOGLE_MAIN_AUX = 34,
SET_SOLUTION_THRESHOLD=35,
REFRESH_PEER_LIST=36,
FORCE_NEXT_TICK=37,
REISSUE_VOTE=38,
QUTIL_SEND_TO_MANY_V1=39,
SYNC_TIME = 40,
GET_SYSTEM_INFO = 41,
QX_ORDER=42,
QX_GET_ORDER=43,
GET_MINING_SCORE_RANKING=44,
SEND_COIN_IN_TICK = 45,
QUTIL_BURN_QUBIC=46,
GET_VOTE_COUNTER_TX=47,
GQMPROP_SET_PROPOSAL = 48,
GQMPROP_CLEAR_PROPOSAL = 49,
GQMPROP_GET_PROPOSALS = 50,
GQMPROP_VOTE = 51,
GQMPROP_GET_VOTE = 52,
GQMPROP_GET_VOTING_RESULTS = 53,
GQMPROP_GET_REV_DONATION = 54,
CCF_SET_PROPOSAL = 55,
CCF_CLEAR_PROPOSAL = 56,
CCF_GET_PROPOSALS = 57,
CCF_VOTE = 58,
CCF_GET_VOTE = 59,
CCF_GET_VOTING_RESULTS = 60,
CCF_GET_LATEST_TRANSFERS = 61,
DUMP_CONTRACT_FILE = 62,
GET_TX_INFO = 63,
UPLOAD_FILE = 64,
DOWNLOAD_FILE = 65,
TOTAL_COMMAND, // DO NOT CHANGE THIS
};
struct RequestResponseHeader {
private:
uint8_t _size[3];
uint8_t _type;
unsigned int _dejavu;
public:
inline unsigned int size() {
if (((*((unsigned int*)_size)) & 0xFFFFFF)==0) return INT32_MAX; // size is never zero, zero means broken packets
return (*((unsigned int*)_size)) & 0xFFFFFF;
}
inline void setSize(unsigned int size) {
_size[0] = (uint8_t)size;
_size[1] = (uint8_t)(size >> 8);
_size[2] = (uint8_t)(size >> 16);
}
inline bool isDejavuZero()
{
return !_dejavu;
}
inline void zeroDejavu()
{
_dejavu = 0;
}
inline void randomizeDejavu()
{
rand32(&_dejavu);
if (!_dejavu)
{
_dejavu = 1;
}
}
inline uint8_t type()
{
return _type;
}
inline void setType(const uint8_t type)
{
_type = type;
}
};
typedef struct
{
unsigned char publicKey[32];
} RequestedEntity;
struct Tick
{
unsigned short computorIndex;
unsigned short epoch;
unsigned int tick;
unsigned short millisecond;
unsigned char second;
unsigned char minute;
unsigned char hour;
unsigned char day;
unsigned char month;
unsigned char year;
unsigned long long prevResourceTestingDigest;
unsigned long long saltedResourceTestingDigest;
uint8_t prevSpectrumDigest[32];
uint8_t prevUniverseDigest[32];
uint8_t prevComputerDigest[32];
uint8_t saltedSpectrumDigest[32];
uint8_t saltedUniverseDigest[32];
uint8_t saltedComputerDigest[32];
uint8_t transactionDigest[32];
uint8_t expectedNextTickTransactionDigest[32];
unsigned char signature[SIGNATURE_SIZE];
static constexpr unsigned char type()
{
return 3;
}
};
struct Entity
{
unsigned char publicKey[32];
long long incomingAmount, outgoingAmount;
unsigned int numberOfIncomingTransfers, numberOfOutgoingTransfers;
unsigned int latestIncomingTransferTick, latestOutgoingTransferTick;
};
typedef struct
{
Entity entity;
unsigned int tick;
int spectrumIndex;
unsigned char siblings[SPECTRUM_DEPTH][32];
} RespondedEntity;
typedef struct
{
unsigned char sourcePublicKey[32];
unsigned char destinationPublicKey[32];
long long amount;
unsigned int tick;
unsigned short inputType;
unsigned short inputSize;
} Transaction;
typedef struct
{
unsigned short tickDuration;
unsigned short epoch;
unsigned int tick;
unsigned short numberOfAlignedVotes;
unsigned short numberOfMisalignedVotes;
unsigned int initialTick;
} CurrentTickInfo;
#pragma pack(push, 1)
typedef struct
{
short version;
unsigned short epoch;
unsigned int tick;
unsigned int initialTick;
unsigned int latestCreatedTick;
unsigned short initialMillisecond;
unsigned char initialSecond;
unsigned char initialMinute;
unsigned char initialHour;
unsigned char initialDay;
unsigned char initialMonth;
unsigned char initialYear;
unsigned int numberOfEntities;
unsigned int numberOfTransactions;
uint8_t randomMiningSeed[32];
int solutionThreshold;
unsigned long long totalSpectrumAmount;
// Entity balances less or euqal this value will be burned if number of entites rises to 75% of spectrum capacity.
// Starts to be meaningful if >50% of spectrum is filled but may still change after that.
unsigned long long currentEntityBalanceDustThreshold;
} CurrentSystemInfo;
#pragma pack(pop)
typedef struct
{
unsigned short computorIndex;
unsigned short epoch;
unsigned int tick;
unsigned short millisecond;
unsigned char second;
unsigned char minute;
unsigned char hour;
unsigned char day;
unsigned char month;
unsigned char year;
unsigned char timelock[32];
unsigned char transactionDigests[NUMBER_OF_TRANSACTIONS_PER_TICK][32];
long long contractFees[1024];
unsigned char signature[SIGNATURE_SIZE];
} TickData;
typedef struct
{
unsigned int tick;
} RequestedTickData;
typedef struct
{
RequestedTickData requestedTickData;
enum {
type = 16,
};
} RequestTickData;
typedef struct
{
unsigned int tick;
unsigned char voteFlags[(676 + 7) / 8];
enum {
type = 14,
};
} RequestedQuorumTick;
typedef struct
{
unsigned int tick;
unsigned char transactionFlags[NUMBER_OF_TRANSACTIONS_PER_TICK / 8];
} RequestedTickTransactions;
typedef struct
{
unsigned char transactionDigest[32];
} RequestedTransactionInfo;
typedef struct
{
uint8_t sig[SIGNATURE_SIZE];
} SignatureStruct;
typedef struct
{
char hash[60];
} TxhashStruct;
typedef struct
{
uint8_t ptr[32];
} TxHash32Struct;
typedef struct
{
std::vector<uint8_t> vecU8;
} extraDataStruct;
struct SpecialCommand
{
unsigned long long everIncreasingNonceAndCommandType;
static constexpr unsigned char type()
{
return 255;
}
};
#define EMPTY 0
#define ISSUANCE 1
#define OWNERSHIP 2
#define POSSESSION 3
struct Asset
{
union
{
struct
{
unsigned char publicKey[32];
unsigned char type;
char name[7]; // Capital letters + digits
char numberOfDecimalPlaces;
char unitOfMeasurement[7]; // Powers of the corresponding SI base units going in alphabetical order
} issuance;
struct
{
unsigned char publicKey[32];
unsigned char type;
char padding[1];
unsigned short managingContractIndex;
unsigned int issuanceIndex;
long long numberOfUnits;
} ownership;
struct
{
unsigned char publicKey[32];
unsigned char type;
char padding[1];
unsigned short managingContractIndex;
unsigned int ownershipIndex;
long long numberOfUnits;
} possession;
} varStruct;
};
typedef struct
{
unsigned char publicKey[32];
} RequestIssuedAssets;
typedef struct
{
Asset asset;
unsigned int tick;
unsigned int universeIndex;
unsigned char siblings[ASSETS_DEPTH][32];
} RespondIssuedAssets;
typedef struct
{
unsigned char publicKey[32];
} RequestOwnedAssets;
typedef struct
{
Asset asset;
Asset issuanceAsset;
unsigned int tick;
unsigned int universeIndex;
unsigned char siblings[ASSETS_DEPTH][32];
} RespondOwnedAssets;
typedef struct
{
unsigned char publicKey[32];
} RequestPossessedAssets;
typedef struct
{
Asset asset;
Asset ownershipAsset;
Asset issuanceAsset;
unsigned int tick;
unsigned int universeIndex;
unsigned char siblings[ASSETS_DEPTH][32];
} RespondPossessedAssets;
typedef struct
{
// TODO: Padding
unsigned short epoch;
unsigned char publicKeys[NUMBER_OF_COMPUTORS][32];
unsigned char signature[SIGNATURE_SIZE];
} Computors;
typedef struct
{
Computors computors;
} BroadcastComputors;
typedef struct
{
unsigned char peers[4][4];
} ExchangePublicPeers;
struct RequestLog // Fetches log
{
unsigned long long passcode[4];
static constexpr unsigned char type()
{
return 44;
}
};
struct RespondLog // Returns buffered log; clears the buffer; make sure you fetch log quickly enough, if the buffer is overflown log stops being written into it till the node restart
{
// Variable-size log;
static constexpr unsigned char type()
{
return 45;
}
};
struct RequestContractFunction // Invokes contract function
{
unsigned int contractIndex;
unsigned short inputType;
unsigned short inputSize;
// Variable-size input
static constexpr unsigned char type()
{
return 42;
}
};
struct RespondContractFunction // Returns result of contract function invocation
{
// Variable-size output; the size must be 0 if the invocation has failed for whatever reason (e.g. no a function registered for [inputType], or the function has timed out)
static constexpr unsigned char type()
{
return 43;
}
};
struct QxFees_output
{
uint32_t assetIssuanceFee; // Amount of qus
uint32_t transferFee; // Amount of qus
uint32_t tradeFee; // Number of billionths
};
struct GetSendToManyV1Fee_output
{
long long fee; // Number of billionths
static constexpr unsigned char type()
{
return 43;
}
};
struct ContractIPOBid
{
long long price;
unsigned short quantity;
};
#define REQUEST_CONTRACT_IPO 33
typedef struct
{
unsigned int contractIndex;
} RequestContractIPO;
#define RESPOND_CONTRACT_IPO 34
typedef struct
{
unsigned int contractIndex;
unsigned int tick;
uint8_t publicKeys[NUMBER_OF_COMPUTORS][32];
long long prices[NUMBER_OF_COMPUTORS];
} RespondContractIPO;
struct SpecialCommandToggleMainModeResquestAndResponse
{
unsigned long long everIncreasingNonceAndCommandType;
unsigned char mainModeFlag;
unsigned char padding[7];
static constexpr unsigned char type()
{
return 255;
}
};
struct SpecialCommandSetSolutionThresholdResquestAndResponse
{
unsigned long long everIncreasingNonceAndCommandType;
unsigned int epoch;
int threshold;
static constexpr unsigned char type()
{
return 255;
}
};
struct UtcTime
{
unsigned short year; // 1900 - 9999
unsigned char month; // 1 - 12
unsigned char day; // 1 - 31
unsigned char hour; // 0 - 23
unsigned char minute; // 0 - 59
unsigned char second; // 0 - 59
unsigned char pad1;
unsigned int nanosecond; // 0 - 999,999,999
};
struct SpecialCommandSendTime
{
unsigned long long everIncreasingNonceAndCommandType;
UtcTime utcTime;
static constexpr unsigned char type()
{
return 255;
}
};
#pragma pack(push, 1)
struct SpecialCommandGetMiningScoreRanking
{
struct ScoreEntry
{
unsigned char minerPublicKey[32];
unsigned int minerScore;
};
unsigned long long everIncreasingNonceAndCommandType;
unsigned int numRankings;
std::vector<ScoreEntry> rankings;
static constexpr unsigned char type()
{
return 255;
}
};
#pragma pack(pop)
#define REQUEST_TX_STATUS 201
struct RequestTxStatus
{
unsigned int tick;
};
static_assert(sizeof(RequestTxStatus) == 4, "unexpected size");
#define RESPOND_TX_STATUS 202
#pragma pack(push, 1)
struct RespondTxStatus
{
unsigned int currentTickOfNode;
unsigned int tick;
unsigned int txCount;
unsigned char moneyFlew[(NUMBER_OF_TRANSACTIONS_PER_TICK + 7) / 8];
// only txCount digests are sent with this message, so only read the first txCount digests when using this as a view to the received data
uint8_t txDigests[NUMBER_OF_TRANSACTIONS_PER_TICK][32];
// return size of this struct to be sent (last txDigests are 0 and do not need to be sent)
unsigned int size() const
{
return offsetof(RespondTxStatus, txDigests) + txCount * 32;
}
};
#pragma pack(pop)
enum SCType
{
SC_TYPE_Contract0State = 0,
SC_TYPE_QX = 1,
SC_TYPE_QTRY = 2,
SC_TYPE_RANDOM = 3,
SC_TYPE_QUTIL = 4,
SC_TYPE_MLM = 5,
SC_TYPE_GQMPROP = 6,
SC_TYPE_SWATCH = 7,
SC_TYPE_CCF = 8,
SC_TYPE_MAX
};
// Generate contract related structs
constexpr uint64_t X_MULTIPLIER = 1ULL;
struct ContractDescription
{
char assetName[8];
// constructionEpoch needs to be set to after IPO (IPO is before construction)
uint16_t constructionEpoch, destructionEpoch;
uint64_t stateSize;
};
struct ContractBase
{
};
template <typename T, uint64_t L>
struct array
{
static_assert(L && !(L & (L - 1)),
"The capacity of the array must be 2^N."
);
T _values[L];
};
static const int64_t COLLECTION_NULL_INDEX = -1LL;
// Simplified collection from core
template <typename T, uint64_t L>
struct collection
{
public:
// Return maximum number of elements that may be stored.
static constexpr uint64_t capacity() { return L; }
// Return total populations.
uint64_t population() { return _population; }
// Return element value at elementIndex.
inline T element(int64_t elementIndex) const { return _elements[elementIndex & (L - 1)].value; }
// Return overall number of elements.
inline uint64_t population() const { return _population; }
// Return point of view elementIndex belongs to (or 0 id if unused).
void pov(int64_t elementIndex, uint8_t* povID) const
{
memcpy(povID, _povs[_elements[elementIndex & (L - 1)].povIndex].value, 32);
}
// Return priority of elementIndex (or 0 id if unused).
int64_t priority(int64_t elementIndex) const
{
return _elements[elementIndex & (L - 1)].priority;
}
private:
static_assert(L && !(L & (L - 1)), "The capacity of the collection must be 2^N.");
static constexpr int64_t _nEncodedFlags = L > 32 ? 32 : L;
// Hash map of point of views = element filters, each with one priority queue (or empty)
struct PoV
{
uint8_t value[32];
uint64_t population;
int64_t headIndex, tailIndex;
int64_t bstRootIndex;
} _povs[L];
// 2 bits per element of _povs: 0b00 = not occupied; 0b01 = occupied; 0b10 = occupied but marked
// for removal; 0b11 is unused The state "occupied but marked for removal" is needed for finding
// the index of a pov in the hash map. Setting an entry to "not occupied" in remove() would
// potentially undo a collision, create a gap, and mess up the entry search.
uint64_t _povOccupationFlags[(L * 2 + 63) / 64];
// Array of elements (filled sequentially), each belongs to one PoV / priority queue (or is
// empty) Elements of a POV entry will be stored as a binary search tree (BST); so this
// structure has some properties related to BST (bstParentIndex, bstLeftIndex, bstRightIndex).
struct Element
{
T value;
int64_t priority;
int64_t povIndex;
int64_t bstParentIndex;
int64_t bstLeftIndex;
int64_t bstRightIndex;
} _elements[L];
uint64_t _population;
uint64_t _markRemovalCounter;
};
struct FileHeaderTransaction : public Transaction
{
static constexpr unsigned char transactionType()
{
return 3; // TODO: Set actual value
}
static constexpr long long minAmount()
{
return 0;
}
static constexpr unsigned short minInputSize()
{
return sizeof(fileSize)
+ sizeof(numberOfFragments)
+ sizeof(fileFormat);
}
unsigned long long fileSize;
unsigned long long numberOfFragments;
unsigned char fileFormat[8];
unsigned char signature[SIGNATURE_SIZE];
};
struct FileFragmentTransactionPrefix : public Transaction
{
static constexpr unsigned char transactionType()
{
return 4; // TODO: Set actual value
}
static constexpr long long minAmount()
{
return 0;
}
static constexpr unsigned short minInputSize()
{
return sizeof(fragmentIndex)
+ sizeof(prevFileFragmentTransactionDigest);
}
unsigned long long fragmentIndex;
uint8_t prevFileFragmentTransactionDigest[32];
};