forked from zcash/zcash
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathpayments.cpp
More file actions
1569 lines (1505 loc) · 78.3 KB
/
payments.cpp
File metadata and controls
1569 lines (1505 loc) · 78.3 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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* Copyright © 2014-2019 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
#include "CCPayments.h"
/*
0) txidopret <- allocation, scriptPubKey, opret
1) create <- locked_blocks, minrelease, list of txidopret
2) fund createtxid amount opretflag to global CC address with opret or txidaddr without
3) release amount -> vout[i] will be scriptPubKeys[i] and (amount * allocations[i]) / sumallocations[] (only using vins that have been locked for locked_blocks+).
4) info txid -> display parameters, funds
5) list -> all txids
First step is to create txids with the info needed in their opreturns. this info is the weight, scriptPubKey and opret if needed. To do that txidopret is used:
./c is a script that invokes komodo-cli with the correct -ac_name
./c paymentstxidopret \"[9,%222102d6f13a8f745921cdb811e32237bb98950af1a5952be7b3d429abd9152f8e388dac%22]\" -> rawhex with txid 95d9fc8d8a3ef63693c7427e59ff5e177ef63b7345d5f6d6497ac262699a8def
./c paymentstxidopret \"[1,%2221039433dc3749aece1bd568f374a45da3b0bc6856990d7da3cd175399577940a775ac%22]\" -> rawhex txid 00469695a08b975ceaf7258896abbf1455eb0f383e8a98fc650deace4cbf02a1
now we have 2 txid with the required info in the opreturn. one of them has a 9 and the other a 1 for a 90%/10% split.
./c paymentscreate \"[0,0,%2295d9fc8d8a3ef63693c7427e59ff5e177ef63b7345d5f6d6497ac262699a8def%22,%2200469695a08b975ceaf7258896abbf1455eb0f383e8a98fc650deace4cbf02a1%22]\" -> created txid 318d827cc6d8f25f40517e7fb0982e3f707b4aa749d322483fc336686a87b28a that will be the createtxid that the other rpc calls will use.
lets see if this appears in the list
./c paymentslist ->
{
"result": "success",
"createtxids": [
"318d827cc6d8f25f40517e7fb0982e3f707b4aa749d322483fc336686a87b28a"
]
}
It appeared! now lets get more info on it:
./c paymentsinfo \"[%22318d827cc6d8f25f40517e7fb0982e3f707b4aa749d322483fc336686a87b28a%22]\"
{
"lockedblocks": 0,
"totalallocations": 10,
"minrelease": 0,
"RWRM36sC8jSctyFZtsu7CyDcHYPdZX7nPZ": 0.00000000,
"REpyKi7avsVduqZ3eimncK4uKqSArLTGGK": 0.00000000,
"totalfunds": 0.00000000,
"result": "success"
}
There are 2 possible places the funds for this createtxid can be, the first is the special address that is derived from combining the globalCC address with the txidaddr. txidaddr is a non-spendable markeraddress created by converting the txid into a 33 byte pubkey by prefixing 0x02 to the txid. It is a 1of2 address, so it doesnt matter that nobody knows the privkey for this txidaddr. the second address is the global CC address and only utxo to that address with an opreturn containing the createtxid are funds valid for this payments CC createtxid
next let us add some funds to it. the funds can be to either of the two addresses, controlled by useopret (defaults to 0)
./c paymentsfund \"[%22318d827cc6d8f25f40517e7fb0982e3f707b4aa749d322483fc336686a87b28a%22,1,0]\" -> txid 28f69b925bb7a21d2a3ba2327e85eb2031b014e976e43f5c2c6fb8a76767b221, which indeed sent funds to RWRM36sC8jSctyFZtsu7CyDcHYPdZX7nPZ without an opreturn and it appears on the payments info.
./c paymentsfund \"[%22318d827cc6d8f25f40517e7fb0982e3f707b4aa749d322483fc336686a87b28a%22,1,1]\" -> txid cc93330b5c951b724b246b3b138d00519c33f2a600a7c938bc9e51aff6e20e32, which indeed sent funds to REpyKi7avsVduqZ3eimncK4uKqSArLTGGK with an opreturn and it appears on the payments info.
./c paymentsrelease \"[%22318d827cc6d8f25f40517e7fb0982e3f707b4aa749d322483fc336686a87b28a%22,1.5]\" -> a8d5dbbb8ee94c05e75c4f3c5221091f59dcb86e0e9c4e1e3d2cf69e6fce6b81
it used both fund utxos
*/
// start of consensus code
void mpz_set_lli( mpz_t rop, long long op )
{
mpz_import(rop, 1, 1, sizeof(op), 0, 0, &op);
}
int64_t mpz_get_si2( mpz_t op )
{
int64_t ret = 0;
mpz_export(&ret, NULL, 1, sizeof(ret), 0, 0, op);
return ret;
}
uint64_t mpz_get_ui2( mpz_t op )
{
uint64_t ret = 0;
mpz_export(&ret, NULL, 1, sizeof(ret), 0, 0, op);
return ret;
}
CScript EncodePaymentsTxidOpRet(int64_t allocation,std::vector<uint8_t> scriptPubKey,std::vector<uint8_t> destopret)
{
CScript opret; uint8_t evalcode = EVAL_PAYMENTS;
opret << OP_RETURN << E_MARSHAL(ss << evalcode << 'T' << allocation << scriptPubKey << destopret);
return(opret);
}
uint8_t DecodePaymentsTxidOpRet(CScript scriptPubKey,int64_t &allocation,std::vector<uint8_t> &destscriptPubKey,std::vector<uint8_t> &destopret)
{
std::vector<uint8_t> vopret; uint8_t *script,e,f;
GetOpReturnData(scriptPubKey, vopret);
script = (uint8_t *)vopret.data();
if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> allocation; ss >> destscriptPubKey; ss >> destopret) != 0 )
{
if ( e == EVAL_PAYMENTS && f == 'T' )
return(f);
}
return(0);
}
CScript EncodePaymentsFundOpRet(uint256 checktxid)
{
CScript opret; uint8_t evalcode = EVAL_PAYMENTS;
opret << OP_RETURN << E_MARSHAL(ss << evalcode << 'F' << checktxid);
return(opret);
}
uint8_t DecodePaymentsFundOpRet(CScript scriptPubKey,uint256 &checktxid)
{
std::vector<uint8_t> vopret; uint8_t *script,e,f;
GetOpReturnData(scriptPubKey, vopret);
script = (uint8_t *)vopret.data();
if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> checktxid) != 0 )
{
if ( e == EVAL_PAYMENTS && f == 'F' )
return(f);
}
return(0);
}
CScript EncodePaymentsMergeOpRet(uint256 checktxid)
{
CScript opret; uint8_t evalcode = EVAL_PAYMENTS;
opret << OP_RETURN << E_MARSHAL(ss << evalcode << 'M' << checktxid);
return(opret);
}
uint8_t DecodePaymentsMergeOpRet(CScript scriptPubKey,uint256 &checktxid)
{
std::vector<uint8_t> vopret; uint8_t *script,e,f;
GetOpReturnData(scriptPubKey, vopret);
script = (uint8_t *)vopret.data();
if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> checktxid) != 0 )
{
if ( e == EVAL_PAYMENTS && f == 'M' )
return(f);
}
return(0);
}
CScript EncodePaymentsReleaseOpRet(uint256 checktxid, int64_t amountReleased)
{
CScript opret; uint8_t evalcode = EVAL_PAYMENTS;
opret << OP_RETURN << E_MARSHAL(ss << evalcode << 'R' << checktxid << amountReleased);
return(opret);
}
uint8_t DecodePaymentsReleaseOpRet(CScript scriptPubKey,uint256 &checktxid,int64_t &amountReleased)
{
std::vector<uint8_t> vopret; uint8_t *script,e,f;
GetOpReturnData(scriptPubKey, vopret);
script = (uint8_t *)vopret.data();
if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> checktxid; ss >> amountReleased) != 0 )
{
if ( e == EVAL_PAYMENTS && f == 'R' )
return(f);
}
return(0);
}
CScript EncodePaymentsOpRet(int32_t lockedblocks,int32_t minrelease,int64_t totalallocations,std::vector<uint256> txidoprets)
{
CScript opret; uint8_t evalcode = EVAL_PAYMENTS;
opret << OP_RETURN << E_MARSHAL(ss << evalcode << 'C' << lockedblocks << minrelease << totalallocations << txidoprets);
return(opret);
}
uint8_t DecodePaymentsOpRet(CScript scriptPubKey,int32_t &lockedblocks,int32_t &minrelease,int64_t &totalallocations,std::vector<uint256> &txidoprets)
{
std::vector<uint8_t> vopret; uint8_t *script,e,f;
GetOpReturnData(scriptPubKey, vopret);
script = (uint8_t *)vopret.data();
if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> lockedblocks; ss >> minrelease; ss >> totalallocations; ss >> txidoprets) != 0 )
{
if ( e == EVAL_PAYMENTS && f == 'C' && txidoprets.size() > 1 )
return(f);
}
return(0);
}
CScript EncodePaymentsSnapsShotOpRet(int32_t lockedblocks,int32_t minrelease,int32_t minimum,int32_t top,int32_t bottom,int8_t fixedAmount,std::vector<std::vector<uint8_t>> excludeScriptPubKeys)
{
CScript opret; uint8_t evalcode = EVAL_PAYMENTS;
opret << OP_RETURN << E_MARSHAL(ss << evalcode << 'S' << lockedblocks << minrelease << minimum << top << bottom << fixedAmount << excludeScriptPubKeys);
return(opret);
}
uint8_t DecodePaymentsSnapsShotOpRet(CScript scriptPubKey,int32_t &lockedblocks,int32_t &minrelease,int32_t &minimum,int32_t &top,int32_t &bottom,int8_t &fixedAmount,std::vector<std::vector<uint8_t>> &excludeScriptPubKeys)
{
std::vector<uint8_t> vopret; uint8_t *script,e,f;
GetOpReturnData(scriptPubKey, vopret);
script = (uint8_t *)vopret.data();
if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> lockedblocks; ss >> minrelease; ss >> minimum; ss >> top; ; ss >> bottom; ss >> fixedAmount; ss >> excludeScriptPubKeys) != 0 )
{
if ( e == EVAL_PAYMENTS && f == 'S' )
return(f);
}
return(0);
}
CScript EncodePaymentsTokensOpRet(int32_t lockedblocks,int32_t minrelease,int32_t minimum,int32_t top,int32_t bottom,int8_t fixedAmount,std::vector<std::vector<uint8_t>> excludeScriptPubKeys,uint256 tokenid)
{
CScript opret; uint8_t evalcode = EVAL_PAYMENTS;
opret << OP_RETURN << E_MARSHAL(ss << evalcode << 'O' << lockedblocks << minrelease << minimum << top << bottom << fixedAmount << excludeScriptPubKeys << tokenid);
return(opret);
}
uint8_t DecodePaymentsTokensOpRet(CScript scriptPubKey,int32_t &lockedblocks,int32_t &minrelease,int32_t &minimum,int32_t &top,int32_t &bottom,int8_t &fixedAmount,std::vector<std::vector<uint8_t>> &excludeScriptPubKeys, uint256 &tokenid)
{
std::vector<uint8_t> vopret; uint8_t *script,e,f;
GetOpReturnData(scriptPubKey, vopret);
script = (uint8_t *)vopret.data();
if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> lockedblocks; ss >> minimum; ss >> top; ; ss >> bottom; ss >> fixedAmount; ss >> excludeScriptPubKeys; ss >> tokenid) != 0 )
{
if ( e == EVAL_PAYMENTS && f == 'O' )
return(f);
}
return(0);
}
int64_t IsPaymentsvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v,char *cmpaddr, CScript &ccopret)
{
char destaddr[64];
if ( getCCopret(tx.vout[v].scriptPubKey, ccopret) )
{
if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && (cmpaddr[0] == 0 || strcmp(destaddr,cmpaddr) == 0) )
return(tx.vout[v].nValue);
}
return(0);
}
bool payments_game(int32_t &top, int32_t &bottom)
{
uint64_t x;
uint256 tmphash = chainActive[lastSnapShotHeight]->GetBlockHash();
memcpy(&x,&tmphash,sizeof(x));
bottom = ((x & 0xff) % 50);
if ( bottom == 0 ) bottom = 1;
top = (((x>>8) & 0xff) % 100);
if ( top < 50 ) top += 50;
bottom = (vAddressSnapshot.size()*bottom)/100;
top = (vAddressSnapshot.size()*top)/100;
//fprintf(stderr, "bottom.%i top.%i\n",bottom,top);
return true;
}
bool payments_lockedblocks(uint256 blockhash,int32_t lockedblocks,int32_t &blocksleft)
{
int32_t ht = chainActive.Height();
CBlockIndex* pblockindex = komodo_blockindex(blockhash);
if ( pblockindex == 0 || pblockindex->GetHeight()+lockedblocks > ht)
{
blocksleft = pblockindex->GetHeight()+lockedblocks - ht;
fprintf(stderr, "not elegible to be spent yet height.%i vs elegible_ht.%i blocksleft.%i\n",ht,(pblockindex!=0?pblockindex->GetHeight():0)+lockedblocks,blocksleft);
return false;
}
return true;
}
int32_t payments_getallocations(int32_t top, int32_t bottom, const std::vector<std::vector<uint8_t>> &excludeScriptPubKeys, mpz_t &mpzTotalAllocations, std::vector<CScript> &scriptPubKeys, std::vector<int64_t> &allocations)
{
mpz_t mpzAllocation; int32_t i =0;
for (int32_t j = bottom; j < vAddressSnapshot.size(); j++)
{
auto &address = vAddressSnapshot[j];
CScript scriptPubKey = GetScriptForDestination(address.second);
bool skip = false;
// skip excluded addresses.
for ( auto skipkey : excludeScriptPubKeys )
{
if ( scriptPubKey == CScript(skipkey.begin(), skipkey.end()) )
skip = true;
}
if ( !skip )
{
mpz_init(mpzAllocation);
i++;
//fprintf(stderr, "address: %s nValue.%li \n", CBitcoinAddress(address.second).ToString().c_str(), address.first);
scriptPubKeys.push_back(scriptPubKey);
allocations.push_back(address.first);
mpz_set_lli(mpzAllocation,address.first);
mpz_add(mpzTotalAllocations,mpzTotalAllocations,mpzAllocation);
mpz_clear(mpzAllocation);
}
if ( i+bottom == top )
break; // we reached top amount to pay, it can be less than this, if less address exist on chain, return the number we got.
}
return(i);
}
int32_t payments_gettokenallocations(int32_t top, int32_t bottom, const std::vector<std::vector<uint8_t>> &excludeScriptPubKeys, uint256 tokenid, mpz_t &mpzTotalAllocations, std::vector<CScript> &scriptPubKeys, std::vector<int64_t> &allocations)
{
/*
- check tokenid exists.
- iterate tokenid address and extract all pubkeys, add to map.
- rewind to last notarized height for balances? see main.cpp: line# 660.
- convert balances to mpz_t and add up totalallocations
- sort the map into a vector, then convert to the correct output.
*/
return(0);
}
bool PaymentsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn)
{
char temp[128], txidaddr[64]={0}; std::string scriptpubkey; uint256 createtxid, blockhash, tokenid; CTransaction plantx; int8_t funcid=0, fixedAmount=0;
int32_t i,lockedblocks,minrelease,blocksleft,dust = 0, top,bottom=0,minimum=10000; int64_t change,totalallocations,actualtxfee,amountReleased=0; std::vector<uint256> txidoprets; bool fHasOpret = false,fIsMerge = false; CPubKey txidpk,Paymentspk;
std::vector<std::vector<uint8_t>> excludeScriptPubKeys; bool fFixedAmount = false; CScript ccopret;
mpz_t mpzTotalAllocations,mpzAllocation,mpzCheckamount;
mpz_init(mpzCheckamount); mpz_init(mpzTotalAllocations);
// Check change is in vout[0], and also fetch the ccopret to determine what type of tx this is. txidaddr is unknown, recheck this later.
if ( (change= IsPaymentsvout(cp,tx,0,txidaddr,ccopret)) != 0 && ccopret.size() > 2 )
{
// get the checktxid and the amount released if doing release tx.
if ( DecodePaymentsMergeOpRet(ccopret,createtxid) == 'M' )
fIsMerge = true;
else if ( DecodePaymentsReleaseOpRet(ccopret,createtxid,amountReleased) != 'R' )
return(eval->Invalid("could not decode ccopret"));
if ( tx.vout.back().scriptPubKey.IsOpReturn() )
fHasOpret = true;
mpz_set_lli(mpzCheckamount,amountReleased);
} else return(eval->Invalid("could not decode ccopret"));
// use the createtxid to fetch the tx and all of the plans info.
if ( myGetTransaction(createtxid,plantx,blockhash) != 0 && plantx.vout.size() > 0 )
{
if ( ((funcid= DecodePaymentsOpRet(plantx.vout[plantx.vout.size()-1].scriptPubKey,lockedblocks,minrelease,totalallocations,txidoprets)) == 'C' || (funcid= DecodePaymentsSnapsShotOpRet(plantx.vout[plantx.vout.size()-1].scriptPubKey,lockedblocks,minrelease,minimum,top,bottom,fixedAmount,excludeScriptPubKeys)) == 'S' || (funcid= DecodePaymentsTokensOpRet(plantx.vout[plantx.vout.size()-1].scriptPubKey,lockedblocks,minrelease,minimum,top,bottom,fixedAmount,excludeScriptPubKeys,tokenid)) == 'O') )
{
if ( lockedblocks < 0 || minrelease < 0 || (totalallocations <= 0 && top <= 0 ) )
return(eval->Invalid("negative values"));
if ( minimum < 10000 )
return(eval->Invalid("minimum must be over 10000"));
Paymentspk = GetUnspendable(cp,0);
txidpk = CCtxidaddr(txidaddr,createtxid);
GetCCaddress1of2(cp,txidaddr,Paymentspk,txidpk);
//fprintf(stderr, "lockedblocks.%i minrelease.%i totalallocations.%i txidopret1.%s txidopret2.%s\n",lockedblocks, minrelease, totalallocations, txidoprets[0].ToString().c_str(), txidoprets[1].ToString().c_str() );
if ( !CheckTxFee(tx, PAYMENTS_TXFEE+1, chainActive.LastTip()->GetHeight(), chainActive.LastTip()->nTime, actualtxfee) )
return eval->Invalid("txfee is too high");
// Check that the change vout is playing the txid address.
if ( IsPaymentsvout(cp,tx,0,txidaddr,ccopret) == 0 )
return eval->Invalid("change pays wrong address");
if ( !fIsMerge )
{
if ( amountReleased < minrelease*COIN )
{
fprintf(stderr, "does not meet minrelease amount.%li minrelease.%li\n",amountReleased, (int64_t)minrelease*COIN);
return(eval->Invalid("amount is too small"));
}
// Get all the script pubkeys and allocations
std::vector<int64_t> allocations;
std::vector<CScript> scriptPubKeys;
i = 0;
if ( funcid == 'C' )
{
// normal payment
int64_t checkallocations = 0;
for ( auto txidopret : txidoprets)
{
CTransaction tx0; std::vector<uint8_t> scriptPubKey,opret; int64_t allocation;
if ( myGetTransaction(txidopret,tx0,blockhash) != 0 && tx0.vout.size() > 1 && DecodePaymentsTxidOpRet(tx0.vout[tx0.vout.size()-1].scriptPubKey,allocation,scriptPubKey,opret) == 'T' )
{
scriptPubKeys.push_back(CScript(scriptPubKey.begin(), scriptPubKey.end()));
allocations.push_back(allocation);
//fprintf(stderr, "i.%i scriptpubkey.%s allocation.%li\n",i,scriptPubKeys[i].ToString().c_str(),allocation);
checkallocations += allocation;
// if we have an op_return to pay to need to check it exists and is paying the correct opret.
if ( !opret.empty() )
{
if ( !fHasOpret )
{
fprintf(stderr, "missing opret.%s in payments release.\n",HexStr(opret.begin(), opret.end()).c_str());
return(eval->Invalid("missing opret in payments release"));
}
else if ( CScript(opret.begin(),opret.end()) != tx.vout[tx.vout.size()-1].scriptPubKey )
{
fprintf(stderr, "opret.%s vs opret.%s\n",HexStr(opret.begin(), opret.end()).c_str(), HexStr(tx.vout[tx.vout.size()-1].scriptPubKey.begin(), tx.vout[tx.vout.size()-1].scriptPubKey.end()).c_str());
return(eval->Invalid("pays incorrect opret"));
}
}
}
i++;
}
//fprintf(stderr, "totalallocations.%li checkallocations.%li\n",totalallocations, checkallocations);
if ( totalallocations != checkallocations )
return(eval->Invalid("allocation missmatch"));
mpz_set_lli(mpzTotalAllocations,totalallocations);
}
else if ( funcid == 'S' || funcid == 'O' )
{
// snapshot payment
if ( KOMODO_SNAPSHOT_INTERVAL == 0 )
return(eval->Invalid("snapshots not activated on this chain"));
if ( vAddressSnapshot.size() == 0 )
return(eval->Invalid("need first snapshot"));
if ( top > 3999 )
return(eval->Invalid("transaction too big"));
if ( fixedAmount == 7 )
{
// game setting, randomise bottom and top values
fFixedAmount = payments_game(top,bottom);
}
else if ( fixedAmount != 0 )
{
fFixedAmount = true;
}
if ( funcid == 'S' )
payments_getallocations(top, bottom, excludeScriptPubKeys, mpzTotalAllocations, scriptPubKeys, allocations);
else
{
// token snapshot
// payments_gettokenallocations(top, bottom, excludeScriptPubKeys, tokenid, mpzTotalAllocations, scriptPubKeys, allocations);
return(eval->Invalid("tokens not yet implemented"));
}
}
// sanity check to make sure we got all the required info, skip for merge type tx
//fprintf(stderr, " allocations.size().%li scriptPubKeys.size.%li\n",allocations.size(), scriptPubKeys.size());
if ( (allocations.size() == 0 || scriptPubKeys.size() == 0 || allocations.size() != scriptPubKeys.size()) )
return(eval->Invalid("missing data cannot validate"));
// Check vouts go to the right place and pay the right amounts.
int64_t amount = 0; int32_t n = 0;
// We place amount released into ccopret, so that these calcualtion are accurate!
// If you change the amount released in the RPC these calcs will be wrong, and validation will fail.
for (i = 1; i < (fHasOpret ? tx.vout.size()-1 : tx.vout.size()); i++)
{
int64_t test;
if ( scriptPubKeys[n] != tx.vout[i].scriptPubKey )
{
fprintf(stderr, "pays wrong destination destscriptPubKey.%s voutscriptPubKey.%s\n", HexStr(scriptPubKeys[n].begin(),scriptPubKeys[n].end()).c_str(), HexStr(tx.vout[i].scriptPubKey.begin(),tx.vout[i].scriptPubKey.end()).c_str());
return(eval->Invalid("pays wrong address"));
}
if ( fFixedAmount )
{
if ( (top-bottom) > 0 )
test = amountReleased / (top-bottom);
else
return(eval->Invalid("top/bottom range is illegal"));
}
else
{
mpz_init(mpzAllocation);
mpz_set_lli(mpzAllocation,allocations[n]);
mpz_mul(mpzAllocation,mpzAllocation,mpzCheckamount);
mpz_tdiv_q(mpzAllocation,mpzAllocation,mpzTotalAllocations);
test = mpz_get_si2(mpzAllocation);
mpz_clear(mpzAllocation);
}
//fprintf(stderr, "vout.%i test.%lli vs nVlaue.%lli\n",i, (long long)test, (long long)tx.vout[i].nValue);
if ( test != tx.vout[i].nValue )
{
fprintf(stderr, "vout.%i test.%lli vs nVlaue.%lli\n",i, (long long)test, (long long)tx.vout[i].nValue);
return(eval->Invalid("amounts do not match"));
}
if ( test < minimum )
{
// prevent anyone being paid the minimum.
fprintf(stderr, "vout.%i test.%li vs minimum.%i\n",i, test, minimum);
return(eval->Invalid("under minimum size"));
}
amount += tx.vout[i].nValue;
n++;
}
if ( allocations.size() > n )
{
// need to check that the next allocation was less than minimum, otherwise ppl can truncate the tx at any place not paying all elegible addresses.
mpz_init(mpzAllocation);
mpz_set_lli(mpzAllocation,allocations[n+1]);
mpz_mul(mpzAllocation,mpzAllocation,mpzCheckamount);
mpz_tdiv_q(mpzAllocation,mpzAllocation,mpzTotalAllocations);
int64_t test = mpz_get_si2(mpzAllocation);
//fprintf(stderr, "check next vout pays under min: test.%li > minimuim.%i\n", test, minimum);
if ( test > minimum )
return(eval->Invalid("next allocation was not under minimum"));
}
mpz_clear(mpzTotalAllocations); mpz_clear(mpzCheckamount);
}
// Check vins
i = 0;
for (auto vin : tx.vin)
{
CTransaction txin;
if ( myGetTransaction(vin.prevout.hash,txin,blockhash) )
{
// check the vin comes from the CC address's
char fromaddr[64]; int32_t mergeoffset = 0; CScript vinccopret; uint256 checktxid;
Getscriptaddress(fromaddr,txin.vout[vin.prevout.n].scriptPubKey);
if ( fIsMerge && txin.vout[vin.prevout.n].nValue < COIN )
dust++;
if ( IsPaymentsvout(cp,txin,vin.prevout.n,cp->unspendableCCaddr,vinccopret) != 0 )
{
// if from global payments address get ccopret to detemine pays correct plan.
uint256 checktxid;
if ( vinccopret.size() < 2 || DecodePaymentsFundOpRet(vinccopret,checktxid) != 'F' || checktxid != createtxid )
{
fprintf(stderr, "vin.%i is not a payments CC vout: txid.%s vout.%i\n", i, txin.GetHash().ToString().c_str(), vin.prevout.n);
return(eval->Invalid("vin is not paymentsCC type"));
}
}
else if ( IsPaymentsvout(cp,txin,vin.prevout.n,txidaddr,vinccopret) != 0 )
{
// if in txid address apply merge offset if applicable.
if ( fIsMerge && vinccopret.size() > 2 && DecodePaymentsMergeOpRet(vinccopret,checktxid) == 'M' )
{
// Apply merge offset to locked blocks, this prevents people spaming payments fund and payments merge to prevent release happening.
mergeoffset = PAYMENTS_MERGEOFSET;
}
}
else // not from global payments plan, or txid address.
return(eval->Invalid("utxo comes from incorrect address"));
// check the chain depth vs locked blocks requirement.
if ( !payments_lockedblocks(blockhash, lockedblocks+mergeoffset, blocksleft) )
{
fprintf(stderr, "vin.%i is not elegible for.%i blocks \n",i, blocksleft);
return(eval->Invalid("vin not elegible"));
}
i++;
} else return(eval->Invalid("cant get vin transaction"));
}
if ( fIsMerge )
{
if ( i < 2 )
return(eval->Invalid("must have at least 2 vins to carry out merge"));
else if ( i == dust+1 )
return(eval->Invalid("cannot merge only dust"));
}
} else return(eval->Invalid("cannot decode create transaction"));
} else return(eval->Invalid("could not get contract transaction"));
return(true);
}
// end of consensus code
// helper functions for rpc calls in rpcwallet.cpp
int64_t AddPaymentsInputs(bool fLockedBlocks,int8_t GetBalance,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey txidpk,int64_t total,int32_t maxinputs,uint256 createtxid,int32_t lockedblocks,int64_t minrelease,int32_t &blocksleft)
{
char coinaddr[64]; CPubKey Paymentspk; int64_t nValue,threshold,price,totalinputs = 0; uint256 txid,checktxid,hashBlock; std::vector<uint8_t> origpubkey; CTransaction vintx; int32_t iter,vout,ht,n = 0;
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs; CScript ccopret;
std::vector<std::pair<int32_t,CAmount> > blocksleft_balance;
if ( GetBalance == 0 )
{
if ( maxinputs > CC_MAXVINS )
maxinputs = CC_MAXVINS;
if ( maxinputs > 0 )
threshold = total/maxinputs;
else threshold = total;
}
else threshold = 0;
Paymentspk = GetUnspendable(cp,0);
for (iter=0; iter<2; iter++)
{
if ( GetBalance == 1 && iter == 1 )
continue; // getbalance of global paymentsCC address.
if ( GetBalance == 2 && iter == 0 )
continue; // get balance of txidpk address.
if ( iter == 0 )
GetCCaddress(cp,coinaddr,Paymentspk);
else GetCCaddress1of2(cp,coinaddr,Paymentspk,txidpk);
SetCCunspents(unspentOutputs,coinaddr,true);
for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++)
{
txid = it->first.txhash;
vout = (int32_t)it->first.index;
//fprintf(stderr,"iter.%d %s/v%d %s\n",iter,txid.GetHex().c_str(),vout,coinaddr);
if ( myGetTransaction(txid,vintx,hashBlock) != 0 )
{
if ( (nValue= IsPaymentsvout(cp,vintx,vout,coinaddr,ccopret)) > PAYMENTS_TXFEE && nValue >= threshold && myIsutxo_spentinmempool(ignoretxid,ignorevin,txid,vout) == 0 )
{
int32_t offset = 0;
if ( ccopret.size() > 2 )
{
if ( iter == 0 && (DecodePaymentsFundOpRet(ccopret,checktxid) != 'F' || checktxid != createtxid) )
{
// global address but not for this plan.
fprintf(stderr,"bad opret %s vs %s\n",checktxid.GetHex().c_str(),createtxid.GetHex().c_str());
continue;
}
// increase merge offset, if this is a merge tx, merging merged utxos.
if ( iter == 1 && GetBalance == 4 && DecodePaymentsMergeOpRet(ccopret,checktxid) == 'M' )
offset = PAYMENTS_MERGEOFSET;
}
int32_t tmpblocksleft = 0;
if ( fLockedBlocks && !payments_lockedblocks(hashBlock, lockedblocks+offset, tmpblocksleft) )
{
blocksleft_balance.push_back(std::make_pair(tmpblocksleft,nValue));
continue;
}
if ( (GetBalance == 0 && total != 0 && maxinputs != 0) || GetBalance == 4 )
mtx.vin.push_back(CTxIn(txid,vout,CScript()));
nValue = it->second.satoshis;
if ( GetBalance == 4 && nValue < COIN )
blocksleft++; // count dust with unused variable.
if ( GetBalance == 2 || GetBalance == 1 )
blocksleft++; // count all utxos with unused variable.
totalinputs += nValue;
n++;
//fprintf(stderr,"iter.%d %s/v%d %s %.8f\n",iter,txid.GetHex().c_str(),vout,coinaddr,(double)nValue/COIN);
if ( GetBalance == 0 && ((total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs)) )
break; // create tx. We have ebnough inputs to make it.
} //else fprintf(stderr,"nValue %.8f vs threshold %.8f\n",(double)nValue/COIN,(double)threshold/COIN);
}
}
}
if ( GetBalance == 3 && totalinputs < minrelease ) // return elegible balance to be spent, and blocks left until min release can be released.
{
int64_t lockedblocks_balance = totalinputs; // inputs that can be spent already.
// sort utxos by blocks until able to be spent, smallest at top.
std::sort(blocksleft_balance.begin(), blocksleft_balance.end());
// iterate the utxos blocks left vector, to get block height min release is able to be released.
for ( auto utxo : blocksleft_balance )
{
lockedblocks_balance += utxo.second;
if ( lockedblocks_balance >= minrelease )
{
blocksleft = utxo.first;
break;
}
}
}
return(totalinputs);
}
UniValue payments_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcastflag)
{
CTransaction tx;
if ( rawtx.size() > 0 )
{
result.push_back(Pair("hex",rawtx));
if ( DecodeHexTx(tx,rawtx) != 0 )
{
if ( broadcastflag != 0 && myAddtomempool(tx) != 0 )
RelayTransaction(tx);
result.push_back(Pair("txid",tx.GetHash().ToString()));
result.push_back(Pair("result","success"));
} else result.push_back(Pair("error","decode hex"));
} else result.push_back(Pair("error","couldnt finalize payments CCtx"));
return(result);
}
cJSON *payments_reparse(int32_t *nump,char *jsonstr)
{
cJSON *params=0; char *newstr; int32_t i,j;
*nump = 0;
if ( jsonstr != 0 )
{
if ( jsonstr[0] == '"' && jsonstr[strlen(jsonstr)-1] == '"' )
{
jsonstr[strlen(jsonstr)-1] = 0;
jsonstr++;
}
newstr = (char *)malloc(strlen(jsonstr)+1);
for (i=j=0; jsonstr[i]!=0; i++)
{
if ( jsonstr[i] == '%' && jsonstr[i+1] == '2' && jsonstr[i+2] == '2' )
{
newstr[j++] = '"';
i += 2;
}
else if ( jsonstr[i] == '\'' )
newstr[j++] = '"';
else newstr[j++] = jsonstr[i];
}
newstr[j] = 0;
params = cJSON_Parse(newstr);
if ( 0 && params != 0 )
printf("new.(%s) -> %s\n",newstr,jprint(params,0));
free(newstr);
*nump = cJSON_GetArraySize(params);
}
return(params);
}
uint256 payments_juint256(cJSON *obj)
{
uint256 tmp; bits256 t = jbits256(obj,0);
memcpy(&tmp,&t,sizeof(tmp));
return(revuint256(tmp));
}
int32_t payments_parsehexdata(std::vector<uint8_t> &hexdata,cJSON *item,int32_t len)
{
char *hexstr; int32_t val;
if ( (hexstr= jstr(item,0)) != 0 && ((val= is_hexstr(hexstr,0)) == len*2 || (val > 0 && len == 0)) )
{
val >>= 1;
hexdata.resize(val);
decode_hex(&hexdata[0],val,hexstr);
return(0);
} else return(-1);
}
UniValue PaymentsRelease(struct CCcontract_info *cp,char *jsonstr)
{
LOCK(cs_main);
CMutableTransaction tmpmtx,mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),komodo_nextheight()); UniValue result(UniValue::VOBJ); uint256 createtxid,hashBlock,tokenid;
CTransaction tx,txO; CPubKey mypk,txidpk,Paymentspk; int32_t i,n,m,numoprets=0,lockedblocks,minrelease; int64_t newamount,inputsum,amount,CCchange=0,totalallocations=0,checkallocations=0,allocation; CTxOut vout; CScript onlyopret,ccopret; char txidaddr[64],destaddr[64]; std::vector<uint256> txidoprets;
int32_t top,bottom=0,blocksleft=0,minimum=10000; std::vector<std::vector<uint8_t>> excludeScriptPubKeys; int8_t funcid,fixedAmount=0,skipminimum=0; bool fFixedAmount = false;
mpz_t mpzTotalAllocations, mpzAllocation; mpz_init(mpzTotalAllocations);
cJSON *params = payments_reparse(&n,jsonstr);
mypk = pubkey2pk(Mypubkey());
Paymentspk = GetUnspendable(cp,0);
if ( params != 0 && n >= 2 )
{
createtxid = payments_juint256(jitem(params,0));
amount = jdouble(jitem(params,1),0) * SATOSHIDEN + 0.0000000049;
if ( n == 3 )
skipminimum = juint(jitem(params,2),0);
if ( myGetTransaction(createtxid,tx,hashBlock) != 0 && tx.vout.size() > 0 )
{
if ( ((funcid= DecodePaymentsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,lockedblocks,minrelease,totalallocations,txidoprets)) == 'C' || (funcid= DecodePaymentsSnapsShotOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,lockedblocks,minrelease,minimum,top,bottom,fixedAmount,excludeScriptPubKeys)) == 'S' || (funcid= DecodePaymentsTokensOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,lockedblocks,minrelease,minimum,top,bottom,fixedAmount,excludeScriptPubKeys,tokenid)) == 'O') )
{
if ( lockedblocks < 0 || minrelease < 0 || (totalallocations <= 0 && top <= 0 ) )
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","negative parameter"));
if ( params != 0 )
free_json(params);
return(result);
}
// set minimum size to 10k sat otherwise the tx will be invalid.
if ( minimum < 10000 )
minimum = 10000;
if ( amount < minrelease*COIN )
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","amount too smal"));
result.push_back(Pair("amount",ValueFromAmount(amount)));
result.push_back(Pair("minrelease",ValueFromAmount(minrelease*COIN)));
if ( params != 0 )
free_json(params);
return(result);
}
txidpk = CCtxidaddr(txidaddr,createtxid);
ccopret = EncodePaymentsReleaseOpRet(createtxid, amount);
std::vector<std::vector<unsigned char>> vData = std::vector<std::vector<unsigned char>>();
if ( makeCCopret(ccopret, vData) )
mtx.vout.push_back(MakeCC1of2vout(EVAL_PAYMENTS,0,Paymentspk,txidpk,&vData));
//fprintf(stderr, "funcid.%i\n", funcid);
if ( funcid == 'C' )
{
// normal payments
m = txidoprets.size();
for (i=0; i<m; i++)
{
std::vector<uint8_t> scriptPubKey,opret;
vout.nValue = 0;
if ( myGetTransaction(txidoprets[i],txO,hashBlock) != 0 && txO.vout.size() > 1 && DecodePaymentsTxidOpRet(txO.vout[txO.vout.size()-1].scriptPubKey,allocation,scriptPubKey,opret) == 'T' )
{
vout.nValue = allocation;
vout.scriptPubKey.resize(scriptPubKey.size());
memcpy(&vout.scriptPubKey[0],&scriptPubKey[0],scriptPubKey.size());
checkallocations += allocation;
if ( opret.size() > 0 )
{
onlyopret.resize(opret.size());
memcpy(&onlyopret[0],&opret[0],opret.size());
numoprets++;
}
} else break;
mtx.vout.push_back(vout);
}
result.push_back(Pair("numoprets",(int64_t)numoprets));
if ( i != m )
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","invalid txidoprets[i]"));
result.push_back(Pair("txi",(int64_t)i));
if ( params != 0 )
free_json(params);
return(result);
}
else if ( checkallocations != totalallocations )
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","totalallocations mismatch"));
result.push_back(Pair("checkallocations",(int64_t)checkallocations));
result.push_back(Pair("totalallocations",(int64_t)totalallocations));
if ( params != 0 )
free_json(params);
return(result);
}
else if ( numoprets > 1 )
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","too many oprets"));
if ( params != 0 )
free_json(params);
return(result);
}
// set totalallocations to a mpz_t bignum, for amounts calculation later.
mpz_set_lli(mpzTotalAllocations,totalallocations);
}
else if ( funcid == 'S' || funcid == 'O' )
{
// normal snapshot
if ( vAddressSnapshot.size() == 0 )
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","first snapshot has not happened yet"));
if ( params != 0 )
free_json(params);
return(result);
}
if ( top > 3999 )
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","cannot pay more than 3999 addresses"));
if ( params != 0 )
free_json(params);
return(result);
}
if ( fixedAmount == 7 )
{
// game setting, randomise bottom and top values
fFixedAmount = payments_game(top,bottom);
}
else if ( fixedAmount != 0 )
{
fFixedAmount = true;
}
if ( (top-bottom) < 0 )
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","invalid range top/bottom"));
if ( params != 0 )
free_json(params);
return(result);
}
std::vector<int64_t> allocations;
std::vector<CScript> scriptPubKeys;
if ( funcid == 'S' )
m = payments_getallocations(top, bottom, excludeScriptPubKeys, mpzTotalAllocations, scriptPubKeys, allocations);
else
{
// token snapshot
// payments_gettokenallocations(top, bottom, excludeScriptPubKeys, tokenid, mpzTotalAllocations, scriptPubKeys, allocations);
}
if ( (allocations.size() == 0 || scriptPubKeys.size() == 0 || allocations.size() != scriptPubKeys.size()) )
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","mismatched allocations, scriptpubkeys"));
if ( params != 0 )
free_json(params);
return(result);
}
i = 0;
for ( auto allocation : allocations )
{
vout.nValue = allocation;
vout.scriptPubKey = scriptPubKeys[i];
mtx.vout.push_back(vout);
i++;
}
}
newamount = amount;
int64_t totalamountsent = 0;
mpz_t mpzAmount; mpz_init(mpzAmount); mpz_set_lli(mpzAmount,amount);
for (i=0; i<m; i++)
{
mpz_t mpzValue; mpz_init(mpzValue);
if ( fFixedAmount )
mtx.vout[i+1].nValue = amount / (top-bottom);
else
{
mpz_set_lli(mpzValue,mtx.vout[i+1].nValue);
mpz_mul(mpzValue,mpzValue,mpzAmount);
mpz_tdiv_q(mpzValue,mpzValue,mpzTotalAllocations);
if ( mpz_fits_slong_p(mpzValue) )
mtx.vout[i+1].nValue = mpz_get_si2(mpzValue);
else
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","value too big, try releasing a smaller amount"));
if ( params != 0 )
free_json(params);
return(result);
}
}
mpz_clear(mpzValue);
//fprintf(stderr, "[%i] nValue.%li minimum.%i scriptpubkey.%s\n", i, mtx.vout[i+1].nValue, minimum, HexStr(mtx.vout[i+1].scriptPubKey.begin(),mtx.vout[i+1].scriptPubKey.end()).c_str());
if ( mtx.vout[i+1].nValue < minimum )
{
if ( skipminimum == 0 )
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","value too small, try releasing a larger amount, or use skipminimum flag"));
if ( params != 0 )
free_json(params);
return(result);
}
else
{
// NOTE: should make this default behaviour.
// truncate off any vouts that are less than minimum.
mtx.vout.resize(i+1);
break;
}
}
totalamountsent += mtx.vout[i+1].nValue;
}
if ( totalamountsent < amount ) newamount = totalamountsent;
//int64_t temptst = mpz_get_si2(mpzTotalAllocations);
//fprintf(stderr, "checkamount RPC.%li totalallocations.%li\n",totalamountsent, temptst);
mpz_clear(mpzAmount); mpz_clear(mpzTotalAllocations);
}
else
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","couldnt decode paymentscreate txid opret"));
if ( params != 0 )
free_json(params);
return(result);
}
if ( (inputsum= AddPaymentsInputs(true,0,cp,mtx,txidpk,newamount+2*PAYMENTS_TXFEE,CC_MAXVINS/2,createtxid,lockedblocks,minrelease,blocksleft)) >= newamount+2*PAYMENTS_TXFEE )
{
std::string rawtx;
mtx.vout[0].nValue = inputsum - newamount - PAYMENTS_TXFEE; // only 1 txfee, so the minimum in this vout is a tx fee.
GetCCaddress1of2(cp,destaddr,Paymentspk,txidpk);
CCaddr1of2set(cp,Paymentspk,txidpk,cp->CCpriv,destaddr);
rawtx = FinalizeCCTx(0,cp,mtx,mypk,PAYMENTS_TXFEE,onlyopret);
if ( params != 0 )
free_json(params);
result.push_back(Pair("amount",ValueFromAmount(amount)));
result.push_back(Pair("newamount",ValueFromAmount(newamount)));
return(payments_rawtxresult(result,rawtx,1));
}
else
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","couldnt find enough locked funds"));
}
}
else
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","couldnt find paymentscreate txid"));
}
}
else
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","parameters error"));
}
if ( params != 0 )
free_json(params);
return(result);
}
UniValue PaymentsFund(struct CCcontract_info *cp,char *jsonstr)
{
CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); UniValue result(UniValue::VOBJ);
CPubKey Paymentspk,mypk,txidpk; uint256 txid,hashBlock; int64_t amount,totalallocations; CScript opret; CTransaction tx; char txidaddr[64]; std::string rawtx; int32_t n,useopret = 0,broadcast=0,lockedblocks,minrelease; std::vector<uint256> txidoprets;
int32_t top,bottom,minimum=10000; std::vector<std::vector<uint8_t>> excludeScriptPubKeys; // snapshot
uint256 tokenid; int8_t fixedAmount;
cJSON *params = payments_reparse(&n,jsonstr);
mypk = pubkey2pk(Mypubkey());
Paymentspk = GetUnspendable(cp,0);
if ( params != 0 && n > 1 && n <= 4 )
{
txid = payments_juint256(jitem(params,0));
amount = jdouble(jitem(params,1),0) * SATOSHIDEN + 0.0000000049;
if ( n == 3 )
useopret = jint(jitem(params,2),0) != 0;
if ( n == 4 )
broadcast = jint(jitem(params,3),0) != 0;
if ( myGetTransaction(txid,tx,hashBlock) == 0 || tx.vout.size() == 1 || (DecodePaymentsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,lockedblocks,minrelease,totalallocations,txidoprets) == 0 && DecodePaymentsSnapsShotOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,lockedblocks,minrelease,minimum,top,bottom,fixedAmount,excludeScriptPubKeys) == 0 && DecodePaymentsTokensOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,lockedblocks,minrelease,minimum,top,bottom,fixedAmount,excludeScriptPubKeys,tokenid) == 0) )
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","invalid createtxid"));
}
else if ( AddNormalinputs(mtx,mypk,amount+PAYMENTS_TXFEE,60) > 0 )
{
if ( lockedblocks < 0 || minrelease < 0 || (totalallocations <= 0 && top <= 0 ) )
{
result.push_back(Pair("result","error"));
result.push_back(Pair("error","negative parameter"));
if ( params != 0 )
free_json(params);
return(result);
}
if ( useopret == 0 )
{
txidpk = CCtxidaddr(txidaddr,txid);