]> Git Repo - VerusCoin.git/blob - src/cc/auction.cpp
Ensure export finalization edge case
[VerusCoin.git] / src / cc / auction.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 "CCauction.h"
17 #include "../txmempool.h"
18
19 /*
20 */
21
22 // start of consensus code
23
24 int64_t IsAuctionvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v)
25 {
26     char destaddr[64];
27     if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 )
28     {
29         if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 )
30             return(tx.vout[v].nValue);
31     }
32     return(0);
33 }
34
35 bool AuctionExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee)
36 {
37     static uint256 zerohash;
38     CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; int64_t inputs=0,outputs=0,assetoshis;
39     numvins = tx.vin.size();
40     numvouts = tx.vout.size();
41     for (i=0; i<numvins; i++)
42     {
43         //fprintf(stderr,"vini.%d\n",i);
44         if ( (*cp->ismyvin)(tx.vin[i].scriptSig) != 0 )
45         {
46             //fprintf(stderr,"vini.%d check mempool\n",i);
47             if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 )
48                 return eval->Invalid("cant find vinTx");
49             else
50             {
51                 //fprintf(stderr,"vini.%d check hash and vout\n",i);
52                 if ( hashBlock == zerohash )
53                     return eval->Invalid("cant Auction from mempool");
54                 if ( (assetoshis= IsAuctionvout(cp,vinTx,tx.vin[i].prevout.n)) != 0 )
55                     inputs += assetoshis;
56             }
57         }
58     }
59     for (i=0; i<numvouts; i++)
60     {
61         //fprintf(stderr,"i.%d of numvouts.%d\n",i,numvouts);
62         if ( (assetoshis= IsAuctionvout(cp,tx,i)) != 0 )
63             outputs += assetoshis;
64     }
65     if ( inputs != outputs+COIN+txfee )
66     {
67         fprintf(stderr,"inputs %llu vs outputs %llu\n",(long long)inputs,(long long)outputs);
68         return eval->Invalid("mismatched inputs != outputs + COIN + txfee");
69     }
70     else return(true);
71 }
72
73 bool AuctionValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn, bool fulfilled)
74 {
75     int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; bool retval;
76     return(false); // reject any auction CC for now
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         //fprintf(stderr,"check vins\n");
85         for (i=0; i<numvins; i++)
86         {
87             if ( IsCCInput(tx.vin[0].scriptSig) == 0 )
88             {
89                 fprintf(stderr,"Auctionget invalid vini\n");
90                 return eval->Invalid("illegal normal vini");
91             }
92         }
93         //fprintf(stderr,"check amounts\n");
94         if ( AuctionExactAmounts(cp,eval,tx,1,10000) == false )
95         {
96             fprintf(stderr,"Auctionget invalid amount\n");
97             return false;
98         }
99         else
100         {
101             preventCCvouts = 1;
102             if ( IsAuctionvout(cp,tx,0) != 0 )
103             {
104                 preventCCvouts++;
105                 i = 1;
106             } else i = 0;
107             if ( tx.vout[i].nValue != COIN )
108                 return eval->Invalid("invalid Auction output");
109             retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts);
110             if ( retval != 0 )
111                 fprintf(stderr,"Auctionget validated\n");
112             else fprintf(stderr,"Auctionget invalid\n");
113             return(retval);
114         }
115     }
116 }
117 // end of consensus code
118
119 // helper functions for rpc calls in rpcwallet.cpp
120
121 int64_t AddAuctionInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,int64_t total,int32_t maxinputs)
122 {
123     char coinaddr[64]; int64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector<uint8_t> origpubkey; CTransaction vintx; int32_t n = 0;
124     std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs;
125     GetCCaddress(cp,coinaddr,pk);
126     SetCCunspents(unspentOutputs,coinaddr);
127     for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++)
128     {
129         txid = it->first.txhash;
130         // prevent dup
131         if ( it->second.satoshis < 1000000 )
132             continue;
133         if ( GetTransaction(txid,vintx,hashBlock,false) != 0 )
134         {
135             if ( (nValue= IsAuctionvout(cp,vintx,(int32_t)it->first.index)) > 0 )
136             {
137                 if ( total != 0 && maxinputs != 0 )
138                     mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript()));
139                 nValue = it->second.satoshis;
140                 totalinputs += nValue;
141                 n++;
142                 if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) )
143                     break;
144             }
145         }
146     }
147     return(totalinputs);
148 }
149
150 std::string AuctionBid(uint64_t txfee,uint256 itemhash,int64_t amount)
151 {
152     CMutableTransaction mtx; CPubKey mypk,Auctionpk; CScript opret; int64_t inputs,CCchange=0,nValue=COIN; struct CCcontract_info *cp,C;
153     cp = CCinit(&C,EVAL_AUCTION);
154     if ( txfee == 0 )
155         txfee = 10000;
156     Auctionpk = GetUnspendable(cp,0);
157     mypk = pubkey2pk(Mypubkey());
158     if ( (inputs= AddAuctionInputs(cp,mtx,Auctionpk,nValue+txfee,60)) > 0 )
159     {
160         if ( inputs > nValue )
161             CCchange = (inputs - nValue - txfee);
162         if ( CCchange != 0 )
163             mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,CCchange,Auctionpk));
164         mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG));
165         return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,opret));
166     } else fprintf(stderr,"cant find Auction inputs\n");
167     return("");
168 }
169
170 std::string AuctionDeliver(uint64_t txfee,uint256 itemhash,uint256 bidtxid)
171 {
172     CMutableTransaction mtx; CPubKey mypk,Auctionpk; CScript opret; int64_t inputs,CCchange=0,nValue=COIN; struct CCcontract_info *cp,C;
173     cp = CCinit(&C,EVAL_AUCTION);
174     if ( txfee == 0 )
175         txfee = 10000;
176     Auctionpk = GetUnspendable(cp,0);
177     mypk = pubkey2pk(Mypubkey());
178     if ( (inputs= AddAuctionInputs(cp,mtx,Auctionpk,nValue+txfee,60)) > 0 )
179     {
180         if ( inputs > nValue )
181             CCchange = (inputs - nValue - txfee);
182         if ( CCchange != 0 )
183             mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,CCchange,Auctionpk));
184         mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG));
185         return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,opret));
186     } else fprintf(stderr,"cant find Auction inputs\n");
187     return("");
188 }
189
190 std::string AuctionPost(uint64_t txfee,uint256 itemhash,int64_t minbid,char *title,char *description)
191 {
192     CMutableTransaction mtx; CPubKey mypk,Auctionpk; int64_t funds = 0; CScript opret; struct CCcontract_info *cp,C;
193     cp = CCinit(&C,EVAL_AUCTION);
194     if ( txfee == 0 )
195         txfee = 10000;
196     mypk = pubkey2pk(Mypubkey());
197     Auctionpk = GetUnspendable(cp,0);
198     if ( AddNormalinputs(mtx,mypk,txfee,64) > 0 )
199     {
200         mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,funds,Auctionpk));
201         return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret));
202     }
203     return("");
204 }
205
206
This page took 0.035449 seconds and 4 git commands to generate.