]> Git Repo - VerusCoin.git/blob - src/cc/prices.cpp
Rest of second wave CC contract stubs
[VerusCoin.git] / src / cc / prices.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 "CCPrices.h"
17
18 /*
19 */
20
21 // start of consensus code
22
23 int64_t IsPricesvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v)
24 {
25     char destaddr[64];
26     if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 )
27     {
28         if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 )
29             return(tx.vout[v].nValue);
30     }
31     return(0);
32 }
33
34 bool PricesExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee)
35 {
36     static uint256 zerohash;
37     CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; int64_t inputs=0,outputs=0,assetoshis;
38     numvins = tx.vin.size();
39     numvouts = tx.vout.size();
40     for (i=0; i<numvins; i++)
41     {
42         //fprintf(stderr,"vini.%d\n",i);
43         if ( (*cp->ismyvin)(tx.vin[i].scriptSig) != 0 )
44         {
45             //fprintf(stderr,"vini.%d check mempool\n",i);
46             if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 )
47                 return eval->Invalid("cant find vinTx");
48             else
49             {
50                 //fprintf(stderr,"vini.%d check hash and vout\n",i);
51                 if ( hashBlock == zerohash )
52                     return eval->Invalid("cant Prices from mempool");
53                 if ( (assetoshis= IsPricesvout(cp,vinTx,tx.vin[i].prevout.n)) != 0 )
54                     inputs += assetoshis;
55             }
56         }
57     }
58     for (i=0; i<numvouts; i++)
59     {
60         //fprintf(stderr,"i.%d of numvouts.%d\n",i,numvouts);
61         if ( (assetoshis= IsPricesvout(cp,tx,i)) != 0 )
62             outputs += assetoshis;
63     }
64     if ( inputs != outputs+txfee )
65     {
66         fprintf(stderr,"inputs %llu vs outputs %llu\n",(long long)inputs,(long long)outputs);
67         return eval->Invalid("mismatched inputs != outputs + txfee");
68     }
69     else return(true);
70 }
71
72 bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx)
73 {
74     int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,numblocks; bool retval; uint256 txid; uint8_t hash[32]; char str[65],destaddr[64];
75     return(false);
76     std::vector<std::pair<CAddressIndexKey, CAmount> > txids;
77     numvins = tx.vin.size();
78     numvouts = tx.vout.size();
79     preventCCvins = preventCCvouts = -1;
80     if ( numvouts < 1 )
81         return eval->Invalid("no vouts");
82     else
83     {
84         for (i=0; i<numvins; i++)
85         {
86             if ( IsCCInput(tx.vin[0].scriptSig) == 0 )
87             {
88                 return eval->Invalid("illegal normal vini");
89             }
90         }
91         //fprintf(stderr,"check amounts\n");
92         if ( PricesExactAmounts(cp,eval,tx,1,10000) == false )
93         {
94             fprintf(stderr,"Pricesget invalid amount\n");
95             return false;
96         }
97         else
98         {
99             txid = tx.GetHash();
100             memcpy(hash,&txid,sizeof(hash));
101             retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts);
102             if ( retval != 0 )
103                 fprintf(stderr,"Pricesget validated\n");
104             else fprintf(stderr,"Pricesget invalid\n");
105             return(retval);
106         }
107     }
108 }
109 // end of consensus code
110
111 // helper functions for rpc calls in rpcwallet.cpp
112
113 int64_t AddPricesInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,int64_t total,int32_t maxinputs)
114 {
115     char coinaddr[64]; int64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector<uint8_t> origpubkey; CTransaction vintx; int32_t vout,n = 0;
116     std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs;
117     GetCCaddress(cp,coinaddr,pk);
118     SetCCunspents(unspentOutputs,coinaddr);
119     for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++)
120     {
121         txid = it->first.txhash;
122         vout = (int32_t)it->first.index;
123         // no need to prevent dup
124         if ( GetTransaction(txid,vintx,hashBlock,false) != 0 )
125         {
126             if ( (nValue= IsPricesvout(cp,vintx,vout)) > 1000000 && myIsutxo_spentinmempool(txid,vout) == 0 )
127             {
128                 if ( total != 0 && maxinputs != 0 )
129                     mtx.vin.push_back(CTxIn(txid,vout,CScript()));
130                 nValue = it->second.satoshis;
131                 totalinputs += nValue;
132                 n++;
133                 if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) )
134                     break;
135             }
136         }
137     }
138     return(totalinputs);
139 }
140
141 std::string PricesGet(uint64_t txfee,int64_t nValue)
142 {
143     CMutableTransaction mtx,tmpmtx; CPubKey mypk,Pricespk; 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;
144     cp = CCinit(&C,EVAL_PRICES);
145     if ( txfee == 0 )
146         txfee = 10000;
147     Pricespk = GetUnspendable(cp,0);
148     mypk = pubkey2pk(Mypubkey());
149     if ( (inputs= AddPricesInputs(cp,mtx,Pricespk,nValue+txfee,60)) > 0 )
150     {
151         if ( inputs > nValue )
152             CCchange = (inputs - nValue - txfee);
153         if ( CCchange != 0 )
154             mtx.vout.push_back(MakeCC1vout(EVAL_PRICES,CCchange,Pricespk));
155         mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG));
156         fprintf(stderr,"start at %u\n",(uint32_t)time(NULL));
157         j = rand() & 0xfffffff;
158         for (i=0; i<1000000; i++,j++)
159         {
160             tmpmtx = mtx;
161             rawhex = FinalizeCCTx(-1LL,cp,tmpmtx,mypk,txfee,CScript() << OP_RETURN << E_MARSHAL(ss << (uint8_t)EVAL_PRICES << (uint8_t)'G' << j));
162             if ( (len= (int32_t)rawhex.size()) > 0 && len < 65536 )
163             {
164                 len >>= 1;
165                 decode_hex(buf,len,(char *)rawhex.c_str());
166                 hash = bits256_doublesha256(0,buf,len);
167                 if ( (hash.bytes[0] & 0xff) == 0 && (hash.bytes[31] & 0xff) == 0 )
168                 {
169                     fprintf(stderr,"found valid txid after %d iterations %u\n",i,(uint32_t)time(NULL));
170                     return(rawhex);
171                 }
172                 //fprintf(stderr,"%02x%02x ",hash.bytes[0],hash.bytes[31]);
173             }
174         }
175         fprintf(stderr,"couldnt generate valid txid %u\n",(uint32_t)time(NULL));
176         return("");
177     } else fprintf(stderr,"cant find Prices inputs\n");
178     return("");
179 }
180
181 std::string PricesFund(uint64_t txfee,int64_t funds)
182 {
183     CMutableTransaction mtx; CPubKey mypk,Pricespk; CScript opret; struct CCcontract_info *cp,C;
184     cp = CCinit(&C,EVAL_PRICES);
185     if ( txfee == 0 )
186         txfee = 10000;
187     mypk = pubkey2pk(Mypubkey());
188     Pricespk = GetUnspendable(cp,0);
189     if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 )
190     {
191         mtx.vout.push_back(MakeCC1vout(EVAL_PRICES,funds,Pricespk));
192         return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret));
193     }
194     return("");
195 }
196
197 UniValue PricesInfo()
198 {
199     UniValue result(UniValue::VOBJ); char numstr[64];
200     CMutableTransaction mtx; CPubKey Pricespk; struct CCcontract_info *cp,C; int64_t funding;
201     result.push_back(Pair("result","success"));
202     result.push_back(Pair("name","Prices"));
203     cp = CCinit(&C,EVAL_PRICES);
204     Pricespk = GetUnspendable(cp,0);
205     funding = AddPricesInputs(cp,mtx,Pricespk,0,0);
206     sprintf(numstr,"%.8f",(double)funding/COIN);
207     result.push_back(Pair("funding",numstr));
208     return(result);
209 }
210
This page took 0.035225 seconds and 4 git commands to generate.