]> Git Repo - VerusCoin.git/blob - src/cc/payments.cpp
Merge pull request #55 from monkins1010/mike_dev_arm
[VerusCoin.git] / src / cc / payments.cpp
1 /******************************************************************************
2  * Copyright © 2014-2018 The SuperNET Developers.                             *
3  *                                                                            *
4  * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at                  *
5  * the top-level directory of this distribution for the individual copyright  *
6  * holder information and the developer policies on copyright and licensing.  *
7  *                                                                            *
8  * Unless otherwise agreed in a custom licensing agreement, no part of the    *
9  * SuperNET software, including this file may be copied, modified, propagated *
10  * or distributed except according to the terms contained in the LICENSE file *
11  *                                                                            *
12  * Removal or modification of this copyright notice is prohibited.            *
13  *                                                                            *
14  ******************************************************************************/
15
16 #include "CCPayments.h"
17
18 /*
19  Payments CC is a catchall CC, supported invoices, zpayments, automated funds allocation, including token based revshare
20  
21  
22 */
23
24 // start of consensus code
25
26 int64_t IsPaymentsvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v)
27 {
28     char destaddr[64];
29     if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 )
30     {
31         if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 )
32             return(tx.vout[v].nValue);
33     }
34     return(0);
35 }
36
37 bool PaymentsExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee)
38 {
39     static uint256 zerohash;
40     CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; int64_t inputs=0,outputs=0,assetoshis;
41     numvins = tx.vin.size();
42     numvouts = tx.vout.size();
43     for (i=0; i<numvins; i++)
44     {
45         //fprintf(stderr,"vini.%d\n",i);
46         if ( (*cp->ismyvin)(tx.vin[i].scriptSig) != 0 )
47         {
48             //fprintf(stderr,"vini.%d check mempool\n",i);
49             if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 )
50                 return eval->Invalid("cant find vinTx");
51             else
52             {
53                 //fprintf(stderr,"vini.%d check hash and vout\n",i);
54                 if ( hashBlock == zerohash )
55                     return eval->Invalid("cant Payments from mempool");
56                 if ( (assetoshis= IsPaymentsvout(cp,vinTx,tx.vin[i].prevout.n)) != 0 )
57                     inputs += assetoshis;
58             }
59         }
60     }
61     for (i=0; i<numvouts; i++)
62     {
63         //fprintf(stderr,"i.%d of numvouts.%d\n",i,numvouts);
64         if ( (assetoshis= IsPaymentsvout(cp,tx,i)) != 0 )
65             outputs += assetoshis;
66     }
67     if ( inputs != outputs+txfee )
68     {
69         fprintf(stderr,"inputs %llu vs outputs %llu\n",(long long)inputs,(long long)outputs);
70         return eval->Invalid("mismatched inputs != outputs + txfee");
71     }
72     else return(true);
73 }
74
75 bool PaymentsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn)
76 {
77     int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,numblocks; bool retval; uint256 txid; uint8_t hash[32]; char str[65],destaddr[64];
78     return(false);
79     std::vector<std::pair<CAddressIndexKey, CAmount> > txids;
80     numvins = tx.vin.size();
81     numvouts = tx.vout.size();
82     preventCCvins = preventCCvouts = -1;
83     if ( numvouts < 1 )
84         return eval->Invalid("no vouts");
85     else
86     {
87         for (i=0; i<numvins; i++)
88         {
89             if ( IsCCInput(tx.vin[0].scriptSig) == 0 )
90             {
91                 return eval->Invalid("illegal normal vini");
92             }
93         }
94         //fprintf(stderr,"check amounts\n");
95         if ( PaymentsExactAmounts(cp,eval,tx,1,10000) == false )
96         {
97             fprintf(stderr,"Paymentsget invalid amount\n");
98             return false;
99         }
100         else
101         {
102             txid = tx.GetHash();
103             memcpy(hash,&txid,sizeof(hash));
104             retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts);
105             if ( retval != 0 )
106                 fprintf(stderr,"Paymentsget validated\n");
107             else fprintf(stderr,"Paymentsget invalid\n");
108             return(retval);
109         }
110     }
111 }
112 // end of consensus code
113
114 // helper functions for rpc calls in rpcwallet.cpp
115
116 int64_t AddPaymentsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,int64_t total,int32_t maxinputs)
117 {
118     char coinaddr[64]; int64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector<uint8_t> origpubkey; CTransaction vintx; int32_t vout,n = 0;
119     std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs;
120     GetCCaddress(cp,coinaddr,pk);
121     SetCCunspents(unspentOutputs,coinaddr);
122     for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++)
123     {
124         txid = it->first.txhash;
125         vout = (int32_t)it->first.index;
126         // no need to prevent dup
127         if ( GetTransaction(txid,vintx,hashBlock,false) != 0 )
128         {
129             if ( (nValue= IsPaymentsvout(cp,vintx,vout)) > 1000000 && myIsutxo_spentinmempool(txid,vout) == 0 )
130             {
131                 if ( total != 0 && maxinputs != 0 )
132                     mtx.vin.push_back(CTxIn(txid,vout,CScript()));
133                 nValue = it->second.satoshis;
134                 totalinputs += nValue;
135                 n++;
136                 if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) )
137                     break;
138             }
139         }
140     }
141     return(totalinputs);
142 }
143
144 std::string PaymentsGet(uint64_t txfee,int64_t nValue)
145 {
146     CMutableTransaction mtx,tmpmtx; CPubKey mypk,Paymentspk; int64_t inputs,CCchange=0; struct CCcontract_info *cp,C; std::string rawhex; uint32_t j; int32_t i,len; uint8_t buf[32768]; bits256 hash;
147     cp = CCinit(&C,EVAL_PAYMENTS);
148     if ( txfee == 0 )
149         txfee = 10000;
150     Paymentspk = GetUnspendable(cp,0);
151     mypk = pubkey2pk(Mypubkey());
152     if ( (inputs= AddPaymentsInputs(cp,mtx,Paymentspk,nValue+txfee,60)) > 0 )
153     {
154         if ( inputs > nValue )
155             CCchange = (inputs - nValue - txfee);
156         if ( CCchange != 0 )
157             mtx.vout.push_back(MakeCC1vout(EVAL_PAYMENTS,CCchange,Paymentspk));
158         mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG));
159         fprintf(stderr,"start at %u\n",(uint32_t)time(NULL));
160         j = rand() & 0xfffffff;
161         for (i=0; i<1000000; i++,j++)
162         {
163             tmpmtx = mtx;
164             rawhex = FinalizeCCTx(-1LL,cp,tmpmtx,mypk,txfee,CScript() << OP_RETURN << E_MARSHAL(ss << (uint8_t)EVAL_PAYMENTS << (uint8_t)'G' << j));
165             if ( (len= (int32_t)rawhex.size()) > 0 && len < 65536 )
166             {
167                 len >>= 1;
168                 decode_hex(buf,len,(char *)rawhex.c_str());
169                 hash = bits256_doublesha256(0,buf,len);
170                 if ( (hash.bytes[0] & 0xff) == 0 && (hash.bytes[31] & 0xff) == 0 )
171                 {
172                     fprintf(stderr,"found valid txid after %d iterations %u\n",i,(uint32_t)time(NULL));
173                     return(rawhex);
174                 }
175                 //fprintf(stderr,"%02x%02x ",hash.bytes[0],hash.bytes[31]);
176             }
177         }
178         fprintf(stderr,"couldnt generate valid txid %u\n",(uint32_t)time(NULL));
179         return("");
180     } else fprintf(stderr,"cant find Payments inputs\n");
181     return("");
182 }
183
184 std::string PaymentsFund(uint64_t txfee,int64_t funds)
185 {
186     CMutableTransaction mtx; CPubKey mypk,Paymentspk; CScript opret; struct CCcontract_info *cp,C;
187     cp = CCinit(&C,EVAL_PAYMENTS);
188     if ( txfee == 0 )
189         txfee = 10000;
190     mypk = pubkey2pk(Mypubkey());
191     Paymentspk = GetUnspendable(cp,0);
192     if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 )
193     {
194         mtx.vout.push_back(MakeCC1vout(EVAL_PAYMENTS,funds,Paymentspk));
195         return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret));
196     }
197     return("");
198 }
199
200 UniValue PaymentsInfo()
201 {
202     UniValue result(UniValue::VOBJ); char numstr[64];
203     CMutableTransaction mtx; CPubKey Paymentspk; struct CCcontract_info *cp,C; int64_t funding;
204     result.push_back(Pair("result","success"));
205     result.push_back(Pair("name","Payments"));
206     cp = CCinit(&C,EVAL_PAYMENTS);
207     Paymentspk = GetUnspendable(cp,0);
208     funding = AddPaymentsInputs(cp,mtx,Paymentspk,0,0);
209     sprintf(numstr,"%.8f",(double)funding/COIN);
210     result.push_back(Pair("funding",numstr));
211     return(result);
212 }
213
This page took 0.035023 seconds and 4 git commands to generate.