-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathqutil.cpp
More file actions
904 lines (822 loc) · 30.9 KB
/
qutil.cpp
File metadata and controls
904 lines (822 loc) · 30.9 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
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <cstring>
#include <cinttypes>
#include "qutil.h"
#include "key_utils.h"
#include "structs.h"
#include "defines.h"
#include "logger.h"
#include "node_utils.h"
#include "k12_and_key_utils.h"
#include "connection.h"
#include "wallet_utils.h"
#include "sanity_check.h"
constexpr int QUTIL_CONTRACT_ID = 4;
// TODO: use value from core source code
constexpr unsigned long long CONTRACT_ACTION_TRACKER_SIZE = 16 * 1024 * 1024;
struct SendToManyV1_input
{
uint8_t addresses[25][32];
int64_t amounts[25];
};
struct BurnQubic_input
{
long long amount;
};
struct BurnQubicForContract_input
{
uint32_t contractIndexBurnedFor;
};
struct QueryFeeReserve_input
{
uint32_t contractIndex;
};
struct QueryFeeReserve_output
{
int64_t reserveAmount;
};
struct SendToManyBenchmark_input
{
int64_t dstCount;
int64_t numTransfersEach;
};
void readPayoutList(const char* payoutListFile, std::vector<std::string>& addresses, std::vector<int64_t>& amounts)
{
addresses.resize(0);
amounts.resize(0);
std::ifstream infile(payoutListFile);
std::string line;
while (std::getline(infile, line))
{
std::istringstream iss(line);
std::string a;
int64_t b;
if (!(iss >> a >> b)) { break; } // error
addresses.push_back(a);
amounts.push_back(b);
}
}
static std::string assetNameFromInt64(unsigned long long assetName)
{
char buffer[8];
memcpy(buffer, &assetName, sizeof(assetName));
buffer[7] = '\0';
return std::string(buffer);
}
long long getSendToManyV1Fee(QCPtr qc)
{
struct {
RequestResponseHeader header;
RequestContractFunction rcf;
} packet;
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(RequestContractFunction::type());
packet.rcf.inputSize = 0;
packet.rcf.inputType = qutilFunctionId::GetSendToManyV1Fee;
packet.rcf.contractIndex = QUTIL_CONTRACT_ID;
qc->sendData((uint8_t *) &packet, packet.header.size());
GetSendToManyV1Fee_output fee;
memset(&fee, 0, sizeof(GetSendToManyV1Fee_output));
try
{
fee = qc->receivePacketWithHeaderAs<GetSendToManyV1Fee_output>();
return fee.fee;
}
catch (std::logic_error& e)
{
LOG(e.what());
return -1;
}
}
struct GetFees_output
{
int64_t smt1InvocationFee;
int64_t pollCreationFee;
int64_t pollVoteFee;
int64_t distributeQuToShareholderFeePerShareholder;
int64_t shareholderProposalFee;
// Placeholder for future fees, preventing incompatibilities for some extensions
int64_t _futureFeePlaceholder0;
int64_t _futureFeePlaceholder1;
int64_t _futureFeePlaceholder2;
int64_t _futureFeePlaceholder3;
int64_t _futureFeePlaceholder4;
int64_t _futureFeePlaceholder5;
};
bool getFees(QCPtr qc, GetFees_output& fees)
{
if (!runContractFunction(nullptr, 0, QUTIL_CONTRACT_ID, qutilFunctionId::GetFees, nullptr, 0, &fees, sizeof(fees), &qc))
{
LOG("ERROR: Didn't receive valid response from GetFees!\n");
return false;
}
return true;
}
void qutilPrintFees(const char* nodeIp, int nodePort)
{
auto qc = make_qc(nodeIp, nodePort);
if (!qc)
{
LOG("Failed to connect to node.\n");
return;
}
GetFees_output fees;
if (!getFees(qc, fees))
return;
LOG("SendToManyV1 fee (var 0): %" PRIi64 "\n", fees.smt1InvocationFee);
LOG("Poll creation fee (var 1): %" PRIi64 "\n", fees.pollCreationFee);
LOG("Poll vote fee (var 2): %" PRIi64 "\n", fees.pollVoteFee);
LOG("DistributeQuToShareholders fee (var 3): %" PRIi64 " per shareholder\n", fees.distributeQuToShareholderFeePerShareholder);
LOG("Shareholder proposal fee (var 4): %" PRIi64 "\n", fees.shareholderProposalFee);
}
void qutilSendToManyV1(const char* nodeIp, int nodePort, const char* seed, const char* payoutListFile, uint32_t scheduledTickOffset)
{
auto qc = make_qc(nodeIp, nodePort);
std::vector<std::string> addresses;
std::vector<int64_t> amounts;
readPayoutList(payoutListFile, addresses, amounts);
if (addresses.size() > 25)
{
LOG("WARNING: payout list has more than 25 addresses, only the first 25 addresses will be paid\n");
}
uint8_t privateKey[32] = {0};
uint8_t sourcePublicKey[32] = {0};
uint8_t destPublicKey[32] = {0};
uint8_t subseed[32] = {0};
uint8_t digest[32] = {0};
uint8_t signature[64] = {0};
char publicIdentity[128] = {0};
char txHash[128] = {0};
getSubseedFromSeed((uint8_t*)seed, subseed);
getPrivateKeyFromSubSeed(subseed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
const bool isLowerCase = false;
getIdentityFromPublicKey(sourcePublicKey, publicIdentity, isLowerCase);
((uint64_t*)destPublicKey)[0] = QUTIL_CONTRACT_ID;
((uint64_t*)destPublicKey)[1] = 0;
((uint64_t*)destPublicKey)[2] = 0;
((uint64_t*)destPublicKey)[3] = 0;
struct {
RequestResponseHeader header;
Transaction transaction;
SendToManyV1_input stm;
unsigned char signature[64];
} packet;
memset(&packet.stm, 0, sizeof(SendToManyV1_input));
packet.transaction.amount = 0;
for (int i = 0; i < std::min(25, int(addresses.size())); i++)
{
getPublicKeyFromIdentity(addresses[i].data(), packet.stm.addresses[i]);
packet.stm.amounts[i] = amounts[i];
packet.transaction.amount += amounts[i];
}
long long fee = getSendToManyV1Fee(qc);
if (fee == -1)
return;
LOG("Send to many V1 fee: %lld\n", fee);
packet.transaction.amount += fee; // fee
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
uint32_t currentTick = getTickNumberFromNode(qc);
packet.transaction.tick = currentTick + scheduledTickOffset;
packet.transaction.inputType = qutilProcedureId::SendToManyV1;
packet.transaction.inputSize = sizeof(SendToManyV1_input);
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(packet.transaction) + sizeof(SendToManyV1_input),
digest,
32);
sign(subseed, sourcePublicKey, digest, signature);
memcpy(packet.signature, signature, 64);
packet.header.setSize(sizeof(packet));
packet.header.zeroDejavu();
packet.header.setType(BROADCAST_TRANSACTION);
qc->sendData((uint8_t *) &packet, packet.header.size());
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(packet.transaction) + sizeof(SendToManyV1_input) + SIGNATURE_SIZE,
digest,
32); // recompute digest for txhash
getTxHashFromDigest(digest, txHash);
LOG("SendToManyV1 tx has been sent!\n");
printReceipt(packet.transaction, txHash, nullptr);
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", currentTick + scheduledTickOffset, txHash);
LOG("to check your tx confirmation status\n");
}
void qutilBurnQubic(const char* nodeIp, int nodePort, const char* seed, long long amount, uint32_t scheduledTickOffset)
{
auto qc = make_qc(nodeIp, nodePort);
uint8_t privateKey[32] = {0};
uint8_t sourcePublicKey[32] = {0};
uint8_t destPublicKey[32] = {0};
uint8_t subseed[32] = {0};
uint8_t digest[32] = {0};
uint8_t signature[64] = {0};
char publicIdentity[128] = {0};
char txHash[128] = {0};
getSubseedFromSeed((uint8_t*)seed, subseed);
getPrivateKeyFromSubSeed(subseed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
const bool isLowerCase = false;
getIdentityFromPublicKey(sourcePublicKey, publicIdentity, isLowerCase);
((uint64_t*)destPublicKey)[0] = QUTIL_CONTRACT_ID;
((uint64_t*)destPublicKey)[1] = 0;
((uint64_t*)destPublicKey)[2] = 0;
((uint64_t*)destPublicKey)[3] = 0;
struct {
RequestResponseHeader header;
Transaction transaction;
BurnQubic_input bqi;
unsigned char signature[64];
} packet;
packet.bqi.amount = amount;
packet.transaction.amount = amount;
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
uint32_t currentTick = getTickNumberFromNode(qc);
packet.transaction.tick = currentTick + scheduledTickOffset;
packet.transaction.inputType = qutilProcedureId::BurnQubic;
packet.transaction.inputSize = sizeof(BurnQubic_input);
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(packet.transaction) + sizeof(BurnQubic_input),
digest,
32);
sign(subseed, sourcePublicKey, digest, signature);
memcpy(packet.signature, signature, 64);
packet.header.setSize(sizeof(packet));
packet.header.zeroDejavu();
packet.header.setType(BROADCAST_TRANSACTION);
qc->sendData((uint8_t *) &packet, packet.header.size());
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(packet.transaction) + sizeof(BurnQubic_input) + SIGNATURE_SIZE,
digest,
32); // recompute digest for txhash
getTxHashFromDigest(digest, txHash);
LOG("BurnQubic tx has been sent!\n");
printReceipt(packet.transaction, txHash, nullptr);
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", currentTick + scheduledTickOffset, txHash);
LOG("to check your tx confirmation status\n");
}
void qutilBurnQubicForContract(const char* nodeIp, int nodePort, const char* seed, long long amount, uint32_t contractIndex, uint32_t scheduledTickOffset)
{
BurnQubicForContract_input input{ contractIndex };
makeContractTransaction(nodeIp, nodePort, seed, QUTIL_CONTRACT_ID, qutilProcedureId::BurnQubicForContract, amount, sizeof(input), &input, scheduledTickOffset);
}
void qutilQueryFeeReserve(const char* nodeIp, int nodePort, uint32_t contractIndex)
{
QueryFeeReserve_input input{ contractIndex };
QueryFeeReserve_output output;
if (runContractFunction(nodeIp, nodePort, QUTIL_CONTRACT_ID, qutilFunctionId::QueryFeeReserve,
&input, sizeof(QueryFeeReserve_input), &output, sizeof(QueryFeeReserve_output)))
{
LOG("Fee reserve for contract %u: %" PRIi64 " Qu\n", contractIndex, output.reserveAmount);
}
else
{
LOG("Failed to query fee reserve for contract %u\n", contractIndex);
}
}
void qutilSendToManyBenchmark(const char* nodeIp, int nodePort, const char* seed, uint32_t destinationCount, uint32_t numTransfersEach, uint32_t scheduledTickOffset)
{
if (destinationCount <= 0 || numTransfersEach <= 0 || destinationCount * numTransfersEach + 2 > CONTRACT_ACTION_TRACKER_SIZE)
{
LOG("Invalid number of total transfers (0 or exceeds %llu)\n", CONTRACT_ACTION_TRACKER_SIZE - 2);
return;
}
auto qc = make_qc(nodeIp, nodePort);
uint8_t privateKey[32] = { 0 };
uint8_t sourcePublicKey[32] = { 0 };
uint8_t destPublicKey[32] = { 0 };
uint8_t subseed[32] = { 0 };
uint8_t digest[32] = { 0 };
uint8_t signature[64] = { 0 };
char publicIdentity[128] = { 0 };
char txHash[128] = { 0 };
getSubseedFromSeed((uint8_t*)seed, subseed);
getPrivateKeyFromSubSeed(subseed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
const bool isLowerCase = false;
getIdentityFromPublicKey(sourcePublicKey, publicIdentity, isLowerCase);
((uint64_t*)destPublicKey)[0] = QUTIL_CONTRACT_ID;
((uint64_t*)destPublicKey)[1] = 0;
((uint64_t*)destPublicKey)[2] = 0;
((uint64_t*)destPublicKey)[3] = 0;
struct {
RequestResponseHeader header;
Transaction transaction;
SendToManyBenchmark_input bm;
unsigned char signature[64];
} packet;
memset(&packet.bm, 0, sizeof(SendToManyBenchmark_input));
packet.bm.dstCount = destinationCount;
packet.bm.numTransfersEach = numTransfersEach;
packet.transaction.amount = destinationCount * numTransfersEach; // no fee at the moment
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
uint32_t currentTick = getTickNumberFromNode(qc);
packet.transaction.tick = currentTick + scheduledTickOffset;
packet.transaction.inputType = qutilProcedureId::SendToManyBenchmark;
packet.transaction.inputSize = sizeof(SendToManyBenchmark_input);
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(packet.transaction) + sizeof(SendToManyBenchmark_input),
digest,
32);
sign(subseed, sourcePublicKey, digest, signature);
memcpy(packet.signature, signature, 64);
packet.header.setSize(sizeof(packet));
packet.header.zeroDejavu();
packet.header.setType(BROADCAST_TRANSACTION);
qc->sendData((uint8_t*)&packet, packet.header.size());
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(packet.transaction) + sizeof(SendToManyBenchmark_input) + SIGNATURE_SIZE,
digest,
32); // recompute digest for txhash
getTxHashFromDigest(digest, txHash);
LOG("SendToManyBenchmark tx has been sent!\n");
printReceipt(packet.transaction, txHash, nullptr);
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", currentTick + scheduledTickOffset, txHash);
LOG("to check your tx confirmation status\n");
}
void qutilGetTotalNumberOfAssetShares(const char* nodeIp, int nodePort, const char* issuerIdentity, const char* assetName)
{
struct
{
uint8_t issuer[32];
uint64_t assetName;
} input;
uint64_t output;
sanityCheckIdentity(issuerIdentity);
getPublicKeyFromIdentity(issuerIdentity, input.issuer);
sanityCheckValidAssetName(assetName);
input.assetName = 0;
memcpy(&input.assetName, assetName, std::min(size_t(7), strlen(assetName)));
if (!runContractFunction(nodeIp, nodePort, QUTIL_CONTRACT_ID,
GetTotalNumberOfAssetShares, &input, sizeof(input), &output, sizeof(output)))
{
LOG("ERROR: Didn't receive valid response from GetTotalNumberOfAssetShares!\n");
return;
}
LOG("%" PRIu64 "\n", output);
}
void qutilDistributeQuToShareholders(const char* nodeIp, int nodePort, const char* seed,
const char* issuerIdentity, const char* assetName, long long amount,
uint32_t scheduledTickOffset)
{
struct
{
uint8_t issuer[32];
uint64_t assetName;
} input;
sanityCheckIdentity(issuerIdentity);
getPublicKeyFromIdentity(issuerIdentity, input.issuer);
sanityCheckValidAssetName(assetName);
input.assetName = 0;
memcpy(&input.assetName, assetName, std::min(size_t(7), strlen(assetName)));
LOG("\nSending transaction to distribute QUs among shareholders ...\n");
makeContractTransaction(nodeIp, nodePort, seed,
QUTIL_CONTRACT_ID, DistributeQuToShareholders, amount,
sizeof(input), &input, scheduledTickOffset);
}
// **********************
// *** Voting related ***
// **********************
// Helper function
std::vector<std::string> split(const std::string& s, char delimiter)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s);
while (std::getline(tokenStream, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
void qutilCreatePoll(const char* nodeIp, int nodePort, const char* seed,
const char* poll_name, uint64_t poll_type, uint64_t min_amount,
const char* github_link, const char* semicolon_separated_assets,
uint32_t scheduledTickOffset)
{
auto qc = make_qc(nodeIp, nodePort);
if (!qc)
{
LOG("Failed to connect to node.\n");
return;
}
CreatePoll_input input;
memset(&input, 0, sizeof(input));
strncpy((char*)input.poll_name, poll_name, 32);
input.poll_type = poll_type;
input.min_amount = min_amount;
strncpy((char*)input.github_link, github_link, 256);
if (poll_type == QUTIL_POLL_TYPE_ASSET && semicolon_separated_assets != nullptr)
{
std::string assets_str(semicolon_separated_assets);
std::vector<std::string> asset_entries = split(assets_str, ';');
if (asset_entries.empty() || asset_entries.size() > QUTIL_MAX_ASSETS_PER_POLL)
{
LOG("Invalid number of assets. Must be between 1 and %" PRIu64 ".\n", QUTIL_MAX_ASSETS_PER_POLL);
return;
}
input.num_assets = asset_entries.size();
for (size_t i = 0; i < asset_entries.size(); ++i)
{
std::vector<std::string> asset_pair = split(asset_entries[i], ',');
if (asset_pair.size() != 2)
{
LOG("Invalid asset format at index %zu: expected 'asset_name,issuer'.\n", i);
return;
}
const std::string& asset_name = asset_pair[0];
const std::string& issuer = asset_pair[1];
if (asset_name.empty() || issuer.empty())
{
LOG("Asset name or issuer cannot be empty at index %zu.\n", i);
return;
}
qpi::Asset& asset = input.allowed_assets[i];
memset(&asset, 0, sizeof(qpi::Asset));
getPublicKeyFromIdentity(issuer.c_str(), asset.issuer);
asset.assetName = assetNameFromString(asset_name.c_str());
}
}
else if (poll_type == QUTIL_POLL_TYPE_QUBIC)
{
input.num_assets = 0;
}
else
{
LOG("Invalid poll type: %" PRIu64 ". Must be 1 (Qubic) or 2 (Asset).\n", poll_type);
return;
}
GetFees_output fees;
if (!getFees(qc, fees))
return;
uint8_t subseed[32] = { 0 };
uint8_t privateKey[32] = { 0 };
uint8_t sourcePublicKey[32] = { 0 };
uint8_t destPublicKey[32] = { 0 };
uint8_t digest[32] = { 0 };
uint8_t signature[64] = { 0 };
char txHash[128] = { 0 };
getSubseedFromSeed((uint8_t*)seed, subseed);
getPrivateKeyFromSubSeed(subseed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
((uint64_t*)destPublicKey)[0] = QUTIL_CONTRACT_ID;
struct
{
RequestResponseHeader header;
Transaction transaction;
CreatePoll_input inputData;
unsigned char signature[64];
} packet;
memset(&packet, 0, sizeof(packet));
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
packet.transaction.amount = fees.pollCreationFee;
uint32_t currentTick = getTickNumberFromNode(qc);
packet.transaction.tick = currentTick + scheduledTickOffset;
packet.transaction.inputType = qutilProcedureId::CreatePoll;
packet.transaction.inputSize = sizeof(CreatePoll_input);
memcpy(&packet.inputData, &input, sizeof(input));
KangarooTwelve((uint8_t*)&packet.transaction,
sizeof(packet.transaction) + sizeof(input),
digest,
32);
sign(subseed, sourcePublicKey, digest, signature);
memcpy(packet.signature, signature, 64);
packet.header.setSize(sizeof(packet));
packet.header.zeroDejavu();
packet.header.setType(BROADCAST_TRANSACTION);
qc->sendData((uint8_t*)&packet, packet.header.size());
KangarooTwelve((uint8_t*)&packet.transaction,
sizeof(packet.transaction) + sizeof(input) + SIGNATURE_SIZE,
digest,
32);
getTxHashFromDigest(digest, txHash);
LOG("CreatePoll transaction sent.\n");
printReceipt(packet.transaction, txHash, nullptr);
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", currentTick + scheduledTickOffset, txHash);
LOG("to check your tx confirmation status\n");
}
void qutilVote(const char* nodeIp, int nodePort, const char* seed,
uint64_t poll_id, uint64_t amount, uint64_t chosen_option,
uint32_t scheduledTickOffset)
{
auto qc = make_qc(nodeIp, nodePort);
if (!qc)
{
LOG("Failed to connect to node.\n");
return;
}
GetFees_output fees;
if (!getFees(qc, fees))
return;
Vote_input input;
memset(&input, 0, sizeof(input));
input.poll_id = poll_id;
uint8_t subseed[32] = { 0 };
uint8_t privateKey[32] = { 0 };
uint8_t sourcePublicKey[32] = { 0 };
getSubseedFromSeed((uint8_t*)seed, subseed);
getPrivateKeyFromSubSeed(subseed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
memcpy(input.address, sourcePublicKey, 32);
input.amount = amount;
input.chosen_option = chosen_option;
uint8_t destPublicKey[32] = { 0 };
((uint64_t*)destPublicKey)[0] = QUTIL_CONTRACT_ID;
uint8_t digest[32] = { 0 };
uint8_t signature[64] = { 0 };
char txHash[128] = { 0 };
struct
{
RequestResponseHeader header;
Transaction transaction;
Vote_input inputData;
unsigned char signature[64];
} packet;
memset(&packet, 0, sizeof(packet));
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
packet.transaction.amount = fees.pollVoteFee;
uint32_t currentTick = getTickNumberFromNode(qc);
packet.transaction.tick = currentTick + scheduledTickOffset;
packet.transaction.inputType = qutilProcedureId::Vote;
packet.transaction.inputSize = sizeof(Vote_input);
memcpy(&packet.inputData, &input, sizeof(input));
KangarooTwelve((uint8_t*)&packet.transaction,
sizeof(packet.transaction) + sizeof(input),
digest,
32);
sign(subseed, sourcePublicKey, digest, signature);
memcpy(packet.signature, signature, 64);
packet.header.setSize(sizeof(packet));
packet.header.zeroDejavu();
packet.header.setType(BROADCAST_TRANSACTION);
qc->sendData((uint8_t*)&packet, packet.header.size());
KangarooTwelve((uint8_t*)&packet.transaction,
sizeof(packet.transaction) + sizeof(input) + SIGNATURE_SIZE,
digest,
32);
getTxHashFromDigest(digest, txHash);
LOG("Vote transaction sent.\n");
printReceipt(packet.transaction, txHash, nullptr);
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", currentTick + scheduledTickOffset, txHash);
LOG("to check your tx confirmation status\n");
}
void qutilGetCurrentResult(const char* nodeIp, int nodePort, uint64_t poll_id)
{
auto qc = make_qc(nodeIp, nodePort);
if (!qc)
{
LOG("Failed to connect to node.\n");
return;
}
struct
{
RequestResponseHeader header;
RequestContractFunction rcf;
GetCurrentResult_input input;
} req;
memset(&req, 0, sizeof(req));
req.rcf.contractIndex = QUTIL_CONTRACT_ID;
req.rcf.inputType = qutilFunctionId::GetCurrentResult;
req.rcf.inputSize = sizeof(GetCurrentResult_input);
req.input.poll_id = poll_id;
req.header.setSize(sizeof(req));
req.header.randomizeDejavu();
req.header.setType(RequestContractFunction::type());
qc->sendData((uint8_t*)&req, req.header.size());
GetCurrentResult_output output;
try
{
output = qc->receivePacketWithHeaderAs<GetCurrentResult_output>();
}
catch (std::logic_error& e)
{
LOG("Failed to get current result: %s\n", e.what());
return;
}
if (output.is_active != 0)
{
LOG("Poll %" PRIu64 " status is ACTIVE.\n", poll_id);
}
else
{
LOG("Poll %" PRIu64 " status is INACTIVE.\n", poll_id);
}
// Log the votes for each option with votes > 0
for (int i = 0; i < QUTIL_MAX_OPTIONS; i++)
{
if (output.votes[i] > 0)
{
LOG("Option %d: %" PRIu64 " votes\n", i, output.votes[i]);
}
}
}
void qutilGetPollsByCreator(const char* nodeIp, int nodePort, const char* creator_address)
{
auto qc = make_qc(nodeIp, nodePort);
if (!qc)
{
LOG("Failed to connect to node.\n");
return;
}
uint8_t creator_pubkey[32];
getPublicKeyFromIdentity(creator_address, creator_pubkey);
struct
{
RequestResponseHeader header;
RequestContractFunction rcf;
GetPollsByCreator_input input;
} req;
memset(&req, 0, sizeof(req));
req.rcf.contractIndex = QUTIL_CONTRACT_ID;
req.rcf.inputType = qutilFunctionId::GetPollsByCreator;
req.rcf.inputSize = sizeof(GetPollsByCreator_input);
memcpy(req.input.creator, creator_pubkey, 32);
req.header.setSize(sizeof(req));
req.header.randomizeDejavu();
req.header.setType(RequestContractFunction::type());
qc->sendData((uint8_t*)&req, req.header.size());
GetPollsByCreator_output output;
try
{
output = qc->receivePacketWithHeaderAs<GetPollsByCreator_output>();
}
catch (std::logic_error& e)
{
LOG("Failed to get polls by creator: %s\n", e.what());
return;
}
if (output.num_polls == 0)
{
LOG("No polls found for creator %s.\n", creator_address);
return;
}
LOG("Polls created by %s:\n", creator_address);
for (uint64_t i = 0; i < output.num_polls; i++)
{
LOG("Poll ID: %" PRIu64 "\n", output.poll_ids[i]);
}
}
void qutilGetCurrentPollId(const char* nodeIp, int nodePort) {
auto qc = make_qc(nodeIp, nodePort);
if (!qc) {
LOG("Failed to connect to node.\n");
return;
}
struct {
RequestResponseHeader header;
RequestContractFunction rcf;
} packet;
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(RequestContractFunction::type());
packet.rcf.inputSize = 0;
packet.rcf.inputType = qutilFunctionId::GetCurrentPollId;
packet.rcf.contractIndex = QUTIL_CONTRACT_ID;
qc->sendData((uint8_t*)&packet, packet.header.size());
GetCurrentPollId_output output;
try {
output = qc->receivePacketWithHeaderAs<GetCurrentPollId_output>();
}
catch (std::logic_error& e) {
LOG("Failed to get current poll ID: %s\n", e.what());
return;
}
LOG("Current Poll ID: %" PRIu64 "\n", output.current_poll_id);
LOG("Active Polls: %" PRIu64 "\n", output.active_count);
for (uint64_t i = 0; i < output.active_count; i++) {
LOG("Poll ID: %" PRIu64 "\n", output.active_poll_ids[i]);
}
}
void qutilGetPollInfo(const char* nodeIp, int nodePort, uint64_t poll_id)
{
auto qc = make_qc(nodeIp, nodePort);
if (!qc)
{
LOG("Failed to connect to node.\n");
return;
}
GetPollInfo_input input;
input.poll_id = poll_id;
struct
{
RequestResponseHeader header;
RequestContractFunction rcf;
GetPollInfo_input inputData;
} packet;
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(RequestContractFunction::type());
packet.rcf.inputSize = sizeof(GetPollInfo_input);
packet.rcf.inputType = qutilFunctionId::GetPollInfo;
packet.rcf.contractIndex = QUTIL_CONTRACT_ID;
memcpy(&packet.inputData, &input, sizeof(input));
qc->sendData((uint8_t*)&packet, packet.header.size());
try
{
GetPollInfo_output output = qc->receivePacketWithHeaderAs<GetPollInfo_output>();
if (output.found == 0)
{
LOG("Poll not found.\n");
}
else
{
char buf[128] = { 0 };
LOG("Poll Info:\n");
char poll_name[33] = { 0 };
memcpy(poll_name, output.poll_info.poll_name, 32);
poll_name[32] = '\0';
LOG("Poll Name: %s\n", poll_name);
LOG("Poll Type: %" PRIu64 " (%s)\n", output.poll_info.poll_type,
output.poll_info.poll_type == QUTIL_POLL_TYPE_QUBIC ? "Qubic" : "Asset");
LOG("Min Amount: %" PRIu64 "\n", output.poll_info.min_amount);
LOG("Is Active: %" PRIu64 "\n", output.poll_info.is_active);
memset(buf, 0, 128);
getIdentityFromPublicKey(output.poll_info.creator, buf, false);
LOG("Creator: %s\n", buf);
LOG("Num Assets: %" PRIu64 "\n", output.poll_info.num_assets);
if (output.poll_info.num_assets > 0)
{
LOG("Allowed Assets:\n");
for (uint64_t i = 0; i < output.poll_info.num_assets; i++)
{
memset(buf, 0, 128);
getIdentityFromPublicKey(output.poll_info.allowed_assets[i].issuer, buf, false);
std::string assetNameStr = assetNameFromInt64(output.poll_info.allowed_assets[i].assetName);
LOG("Asset %" PRIu64 ": Issuer %s, Name %s\n", i, buf, assetNameStr.c_str());
}
}
LOG("GitHub Link: %s\n", output.poll_link);
}
}
catch (std::logic_error& e)
{
LOG("Error receiving poll info: %s\n", e.what());
}
}
void qutilCancelPoll(const char* nodeIp, int nodePort, const char* seed, uint64_t poll_id, uint32_t scheduledTickOffset)
{
auto qc = make_qc(nodeIp, nodePort);
if (!qc)
{
LOG("Failed to connect to node.\n");
return;
}
GetFees_output fees;
if (!getFees(qc, fees))
return;
CancelPoll_input input;
input.poll_id = poll_id;
uint8_t subseed[32] = { 0 };
uint8_t privateKey[32] = { 0 };
uint8_t sourcePublicKey[32] = { 0 };
getSubseedFromSeed((uint8_t*)seed, subseed);
getPrivateKeyFromSubSeed(subseed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
uint8_t destPublicKey[32] = { 0 };
((uint64_t*)destPublicKey)[0] = QUTIL_CONTRACT_ID;
uint8_t digest[32] = { 0 };
uint8_t signature[64] = { 0 };
char txHash[128] = { 0 };
struct
{
RequestResponseHeader header;
Transaction transaction;
CancelPoll_input inputData;
unsigned char signature[64];
} packet;
memset(&packet, 0, sizeof(packet));
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
packet.transaction.amount = fees.pollCreationFee;
uint32_t currentTick = getTickNumberFromNode(qc);
packet.transaction.tick = currentTick + scheduledTickOffset;
packet.transaction.inputType = qutilProcedureId::CancelPoll;
packet.transaction.inputSize = sizeof(CancelPoll_input);
memcpy(&packet.inputData, &input, sizeof(input));
KangarooTwelve((uint8_t*)&packet.transaction,
sizeof(packet.transaction) + sizeof(CancelPoll_input),
digest,
32);
sign(subseed, sourcePublicKey, digest, signature);
memcpy(packet.signature, signature, 64);
packet.header.setSize(sizeof(packet));
packet.header.zeroDejavu();
packet.header.setType(BROADCAST_TRANSACTION);
qc->sendData((uint8_t*)&packet, packet.header.size());
KangarooTwelve((uint8_t*)&packet.transaction,
sizeof(packet.transaction) + sizeof(CancelPoll_input) + SIGNATURE_SIZE,
digest,
32);
getTxHashFromDigest(digest, txHash);
LOG("CancelPoll transaction sent.\n");
printReceipt(packet.transaction, txHash, nullptr);
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", currentTick + scheduledTickOffset, txHash);
LOG("to check your tx confirmation status\n");
}