1 /******************************************************************************
2 * Copyright © 2014-2018 The SuperNET Developers. *
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. *
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 *
12 * Removal or modification of this copyright notice is prohibited. *
14 ******************************************************************************/
21 // start of consensus code
23 int64_t IsPricesvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v)
26 if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 )
28 if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 )
29 return(tx.vout[v].nValue);
34 bool PricesExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee)
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++)
42 //fprintf(stderr,"vini.%d\n",i);
43 if ( (*cp->ismyvin)(tx.vin[i].scriptSig) != 0 )
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");
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 )
58 for (i=0; i<numvouts; i++)
60 //fprintf(stderr,"i.%d of numvouts.%d\n",i,numvouts);
61 if ( (assetoshis= IsPricesvout(cp,tx,i)) != 0 )
62 outputs += assetoshis;
64 if ( inputs != outputs+txfee )
66 fprintf(stderr,"inputs %llu vs outputs %llu\n",(long long)inputs,(long long)outputs);
67 return eval->Invalid("mismatched inputs != outputs + txfee");
72 bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx)
74 int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,numblocks; bool retval; uint256 txid; uint8_t hash[32]; char str[65],destaddr[64];
76 std::vector<std::pair<CAddressIndexKey, CAmount> > txids;
77 numvins = tx.vin.size();
78 numvouts = tx.vout.size();
79 preventCCvins = preventCCvouts = -1;
81 return eval->Invalid("no vouts");
84 for (i=0; i<numvins; i++)
86 if ( IsCCInput(tx.vin[0].scriptSig) == 0 )
88 return eval->Invalid("illegal normal vini");
91 //fprintf(stderr,"check amounts\n");
92 if ( PricesExactAmounts(cp,eval,tx,1,10000) == false )
94 fprintf(stderr,"Pricesget invalid amount\n");
100 memcpy(hash,&txid,sizeof(hash));
101 retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts);
103 fprintf(stderr,"Pricesget validated\n");
104 else fprintf(stderr,"Pricesget invalid\n");
109 // end of consensus code
111 // helper functions for rpc calls in rpcwallet.cpp
113 int64_t AddPricesInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,int64_t total,int32_t maxinputs)
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++)
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 )
126 if ( (nValue= IsPricesvout(cp,vintx,vout)) > 1000000 && myIsutxo_spentinmempool(txid,vout) == 0 )
128 if ( total != 0 && maxinputs != 0 )
129 mtx.vin.push_back(CTxIn(txid,vout,CScript()));
130 nValue = it->second.satoshis;
131 totalinputs += nValue;
133 if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) )
141 std::string PricesGet(uint64_t txfee,int64_t nValue)
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);
147 Pricespk = GetUnspendable(cp,0);
148 mypk = pubkey2pk(Mypubkey());
149 if ( (inputs= AddPricesInputs(cp,mtx,Pricespk,nValue+txfee,60)) > 0 )
151 if ( inputs > nValue )
152 CCchange = (inputs - nValue - txfee);
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++)
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 )
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 )
169 fprintf(stderr,"found valid txid after %d iterations %u\n",i,(uint32_t)time(NULL));
172 //fprintf(stderr,"%02x%02x ",hash.bytes[0],hash.bytes[31]);
175 fprintf(stderr,"couldnt generate valid txid %u\n",(uint32_t)time(NULL));
177 } else fprintf(stderr,"cant find Prices inputs\n");
181 std::string PricesFund(uint64_t txfee,int64_t funds)
183 CMutableTransaction mtx; CPubKey mypk,Pricespk; CScript opret; struct CCcontract_info *cp,C;
184 cp = CCinit(&C,EVAL_PRICES);
187 mypk = pubkey2pk(Mypubkey());
188 Pricespk = GetUnspendable(cp,0);
189 if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 )
191 mtx.vout.push_back(MakeCC1vout(EVAL_PRICES,funds,Pricespk));
192 return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret));
197 UniValue PricesInfo()
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));