diff --git a/src/Makefile.am b/src/Makefile.am index a0c474146..a44839781 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -264,6 +264,10 @@ libbitcoin_server_a_SOURCES = \ cc/assets.cpp \ cc/faucet.cpp \ cc/rewards.cpp \ + cc/dice.cpp \ + cc/lotto.cpp \ + cc/ponzi.cpp \ + cc/auction.cpp \ cc/betprotocol.cpp \ chain.cpp \ checkpoints.cpp \ diff --git a/src/cc/CCassets.h b/src/cc/CCassets.h index 0b4dd5578..0fbe4891c 100644 --- a/src/cc/CCassets.h +++ b/src/cc/CCassets.h @@ -25,10 +25,8 @@ #include "CCinclude.h" -extern const char *AssetsCCaddr; -extern char AssetsCChexstr[67]; // CCcustom -bool IsAssetsInput(CScript const& scriptSig); +bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); // CCassetsCore //CTxOut MakeAssetsVout(CAmount nValue,CPubKey pk); @@ -39,9 +37,9 @@ bool SetAssetOrigpubkey(std::vector &origpubkey,uint64_t &price,const C uint64_t IsAssetvout(uint64_t &price,std::vector &origpubkey,const CTransaction& tx,int32_t v,uint256 refassetid); bool ValidateAssetRemainder(int32_t sellflag,uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidprice,uint64_t totalprice); bool SetAssetFillamounts(int32_t sellflag,uint64_t &paid,uint64_t &remaining_price,uint64_t orig_nValue,uint64_t &received,uint64_t totalprice); -uint64_t AssetValidateBuyvin(Eval* eval,uint64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 refassetid); -uint64_t AssetValidateSellvin(Eval* eval,uint64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 assetid); -bool AssetExactAmounts(uint64_t &inputs,int32_t starti,uint64_t &outputs,Eval* eval,const CTransaction &tx,uint256 assetid); +uint64_t AssetValidateBuyvin(struct CCcontract_info *cp,Eval* eval,uint64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 refassetid); +uint64_t AssetValidateSellvin(struct CCcontract_info *cp,Eval* eval,uint64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 assetid); +bool AssetExactAmounts(struct CCcontract_info *cp,uint64_t &inputs,int32_t starti,uint64_t &outputs,Eval* eval,const CTransaction &tx,uint256 assetid); // CCassetstx uint64_t GetAssetBalance(CPubKey pk,uint256 tokenid); diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 7dc1e8a13..466db2d1b 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -185,13 +185,13 @@ bool SetAssetOrigpubkey(std::vector &origpubkey,uint64_t &price,const C else return(false); } -bool GetAssetorigaddrs(char *CCaddr,char *destaddr,const CTransaction& tx) +bool GetAssetorigaddrs(struct CCcontract_info *cp,char *CCaddr,char *destaddr,const CTransaction& tx) { uint256 assetid,assetid2; uint64_t price,nValue=0; int32_t n; uint8_t funcid; std::vector origpubkey; CScript script; n = tx.vout.size(); if ( n == 0 || (funcid= DecodeAssetOpRet(tx.vout[n-1].scriptPubKey,assetid,assetid2,price,origpubkey)) == 0 ) return(false); - if ( GetCCaddress(EVAL_ASSETS,CCaddr,pubkey2pk(origpubkey)) != 0 && Getscriptaddress(destaddr,CScript() << origpubkey << OP_CHECKSIG) != 0 ) + if ( GetCCaddress(cp,CCaddr,pubkey2pk(origpubkey)) != 0 && Getscriptaddress(destaddr,CScript() << origpubkey << OP_CHECKSIG) != 0 ) return(true); else return(false); } @@ -235,7 +235,7 @@ uint64_t IsAssetvout(uint64_t &price,std::vector &origpubkey,const CTra return(0); } -uint64_t AssetValidateCCvin(Eval* eval,char *CCaddr,char *origaddr,const CTransaction &tx,int32_t vini,CTransaction &vinTx) +uint64_t AssetValidateCCvin(struct CCcontract_info *cp,Eval* eval,char *CCaddr,char *origaddr,const CTransaction &tx,int32_t vini,CTransaction &vinTx) { uint256 hashBlock; char destaddr[64]; origaddr[0] = destaddr[0] = CCaddr[0] = 0; @@ -251,24 +251,24 @@ uint64_t AssetValidateCCvin(Eval* eval,char *CCaddr,char *origaddr,const CTransa fprintf(stderr," vini.%d\n",vini); return eval->Invalid("always should find CCvin, but didnt"); } - else if ( Getscriptaddress(destaddr,vinTx.vout[tx.vin[vini].prevout.n].scriptPubKey) == 0 || strcmp(destaddr,(char *)AssetsCCaddr) != 0 ) + else if ( Getscriptaddress(destaddr,vinTx.vout[tx.vin[vini].prevout.n].scriptPubKey) == 0 || strcmp(destaddr,(char *)cp->unspendableCCaddr) != 0 ) { - fprintf(stderr,"%s vs %s\n",destaddr,(char *)AssetsCCaddr); + fprintf(stderr,"%s vs %s\n",destaddr,(char *)cp->unspendableCCaddr); return eval->Invalid("invalid vin AssetsCCaddr"); } else if ( vinTx.vout[0].nValue < 10000 ) return eval->Invalid("invalid dust for buyvin"); - else if ( GetAssetorigaddrs(CCaddr,origaddr,vinTx) == 0 ) + else if ( GetAssetorigaddrs(cp,CCaddr,origaddr,vinTx) == 0 ) return eval->Invalid("couldnt get origaddr for buyvin"); fprintf(stderr,"Got %.8f to origaddr.(%s)\n",(double)vinTx.vout[tx.vin[vini].prevout.n].nValue/COIN,origaddr); return(vinTx.vout[0].nValue); } -uint64_t AssetValidateBuyvin(Eval* eval,uint64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 refassetid) +uint64_t AssetValidateBuyvin(struct CCcontract_info *cp,Eval* eval,uint64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 refassetid) { CTransaction vinTx; uint64_t nValue; uint256 assetid,assetid2; uint8_t funcid; CCaddr[0] = origaddr[0] = 0; - if ( (nValue= AssetValidateCCvin(eval,CCaddr,origaddr,tx,1,vinTx)) == 0 ) + if ( (nValue= AssetValidateCCvin(cp,eval,CCaddr,origaddr,tx,1,vinTx)) == 0 ) return(0); else if ( vinTx.vout[0].scriptPubKey.IsPayToCryptoCondition() == 0 ) return eval->Invalid("invalid normal vout0 for buyvin"); @@ -286,18 +286,18 @@ uint64_t AssetValidateBuyvin(Eval* eval,uint64_t &tmpprice,std::vector return(nValue); } -uint64_t AssetValidateSellvin(Eval* eval,uint64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 assetid) +uint64_t AssetValidateSellvin(struct CCcontract_info *cp,Eval* eval,uint64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 assetid) { CTransaction vinTx; uint64_t nValue,assetoshis; fprintf(stderr,"AssetValidateSellvin\n"); - if ( (nValue= AssetValidateCCvin(eval,CCaddr,origaddr,tx,1,vinTx)) == 0 ) + if ( (nValue= AssetValidateCCvin(cp,eval,CCaddr,origaddr,tx,1,vinTx)) == 0 ) return(0); if ( (assetoshis= IsAssetvout(tmpprice,tmporigpubkey,vinTx,0,assetid)) != 0 ) return eval->Invalid("invalid missing CC vout0 for sellvin"); else return(assetoshis); } -bool AssetExactAmounts(uint64_t &inputs,int32_t starti,uint64_t &outputs,Eval* eval,const CTransaction &tx,uint256 assetid) +bool AssetExactAmounts(struct CCcontract_info *cp,uint64_t &inputs,int32_t starti,uint64_t &outputs,Eval* eval,const CTransaction &tx,uint256 assetid) { CTransaction vinTx; uint256 hashBlock; int32_t i,numvins,numvouts; uint64_t assetoshis; std::vector tmporigpubkey; uint64_t tmpprice; numvins = tx.vin.size(); @@ -305,7 +305,7 @@ bool AssetExactAmounts(uint64_t &inputs,int32_t starti,uint64_t &outputs,Eval* e inputs = outputs = 0; for (i=starti; iismyvin)(tx.vin[i].scriptSig) != 0 ) { if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 ) { diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 29fb299c0..c52864218 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -15,11 +15,11 @@ #include "CCassets.h" -uint64_t AddAssetInputs(CMutableTransaction &mtx,CPubKey pk,uint256 assetid,uint64_t total,int32_t maxinputs) +uint64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint256 assetid,uint64_t total,int32_t maxinputs) { char coinaddr[64]; uint64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t n = 0; std::vector > unspentOutputs; - GetCCaddress(EVAL_ASSETS,coinaddr,pk); + GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); //std::sort(unspentOutputs.begin(), unspentOutputs.end(), heightSort); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) @@ -44,14 +44,16 @@ uint64_t AddAssetInputs(CMutableTransaction &mtx,CPubKey pk,uint256 assetid,uint uint64_t GetAssetBalance(CPubKey pk,uint256 tokenid) { - CMutableTransaction mtx; - return(AddAssetInputs(mtx,pk,tokenid,0,0)); + CMutableTransaction mtx; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); + return(AddAssetInputs(cp,mtx,pk,tokenid,0,0)); } UniValue AssetOrders(uint256 refassetid) { - uint64_t price; uint256 txid,hashBlock,assetid,assetid2; std::vector origpubkey; CTransaction vintx; UniValue result(UniValue::VARR); std::vector > unspentOutputs; uint8_t funcid; char funcidstr[16],origaddr[64],assetidstr[65]; - SetCCunspents(unspentOutputs,(char *)AssetsCCaddr); + uint64_t price; uint256 txid,hashBlock,assetid,assetid2; std::vector origpubkey; CTransaction vintx; UniValue result(UniValue::VARR); std::vector > unspentOutputs; uint8_t funcid; char funcidstr[16],origaddr[64],assetidstr[65]; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); + SetCCunspents(unspentOutputs,(char *)cp->unspendableCCaddr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; @@ -71,7 +73,7 @@ UniValue AssetOrders(uint256 refassetid) else item.push_back(Pair("askamount",(double)vintx.vout[0].nValue)); if ( origpubkey.size() == 33 ) { - GetCCaddress(EVAL_ASSETS,origaddr,pubkey2pk(origpubkey)); + GetCCaddress(cp,origaddr,pubkey2pk(origpubkey)); item.push_back(Pair("origaddress",origaddr)); } if ( assetid != zeroid ) @@ -93,7 +95,8 @@ UniValue AssetOrders(uint256 refassetid) std::string CreateAsset(uint64_t txfee,uint64_t assetsupply,std::string name,std::string description) { - CMutableTransaction mtx; CPubKey mypk; + CMutableTransaction mtx; CPubKey mypk; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( name.size() > 32 || description.size() > 4096 ) { fprintf(stderr,"name.%d or description.%d is too big\n",(int32_t)name.size(),(int32_t)description.size()); @@ -105,15 +108,16 @@ std::string CreateAsset(uint64_t txfee,uint64_t assetsupply,std::string name,std if ( AddNormalinputs(mtx,mypk,assetsupply+2*txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,assetsupply,mypk)); - mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(AssetsCChexstr) << OP_CHECKSIG)); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,EncodeAssetCreateOpRet('c',Mypubkey(),name,description))); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(cp->CChexstr) << OP_CHECKSIG)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetCreateOpRet('c',Mypubkey(),name,description))); } return(0); } std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector destpubkey,uint64_t total) { - CMutableTransaction mtx; CPubKey mypk; uint64_t CCchange=0,inputs=0; //int32_t i,n; + CMutableTransaction mtx; CPubKey mypk; uint64_t CCchange=0,inputs=0; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); @@ -124,7 +128,7 @@ std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector de { for (i=0; i 0 ) + if ( (inputs= AddAssetInputs(cp,mtx,mypk,assetid,total,60)) > 0 ) { if ( inputs > total ) CCchange = (inputs - total); @@ -132,7 +136,7 @@ std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector de mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,total,pubkey2pk(destpubkey))); if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,EncodeAssetOpRet('t',assetid,zeroid,0,Mypubkey()))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('t',assetid,zeroid,0,Mypubkey()))); } else fprintf(stderr,"not enough CC asset inputs for %.8f\n",(double)total/COIN); //} else fprintf(stderr,"numoutputs.%d != numamounts.%d\n",n,(int32_t)amounts.size()); } @@ -141,29 +145,31 @@ std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector de std::string CreateBuyOffer(uint64_t txfee,uint64_t bidamount,uint256 assetid,uint64_t pricetotal) { - CMutableTransaction mtx; CPubKey mypk; + CMutableTransaction mtx; CPubKey mypk; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); if ( AddNormalinputs(mtx,mypk,bidamount+txfee,64) > 0 ) { - mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,bidamount,GetUnspendable(EVAL_ASSETS,0))); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,EncodeAssetOpRet('b',assetid,zeroid,pricetotal,Mypubkey()))); + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,bidamount,GetUnspendable(cp,0))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('b',assetid,zeroid,pricetotal,Mypubkey()))); } return(0); } std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 assetid2,uint64_t pricetotal) { - CMutableTransaction mtx; CPubKey mypk; uint64_t inputs,CCchange; CScript opret; + CMutableTransaction mtx; CPubKey mypk; uint64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); if ( AddNormalinputs(mtx,mypk,txfee,1) > 0 ) { - if ( (inputs= AddAssetInputs(mtx,mypk,assetid,askamount,60)) > 0 ) + if ( (inputs= AddAssetInputs(cp,mtx,mypk,assetid,askamount,60)) > 0 ) { - mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount,GetUnspendable(EVAL_ASSETS,0))); + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount,GetUnspendable(cp,0))); if ( inputs > askamount ) CCchange = (inputs - askamount); if ( CCchange != 0 ) @@ -171,7 +177,7 @@ std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 if ( assetid2 == zeroid ) opret = EncodeAssetOpRet('s',assetid,zeroid,pricetotal,Mypubkey()); else opret = EncodeAssetOpRet('e',assetid,assetid2,pricetotal,Mypubkey()); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); } } return(0); @@ -179,7 +185,8 @@ std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 std::string CancelBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid) { - CMutableTransaction mtx; CTransaction vintx; uint256 hashBlock; uint64_t bidamount; CPubKey mypk; + CMutableTransaction mtx; CTransaction vintx; uint256 hashBlock; uint64_t bidamount; CPubKey mypk; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); @@ -190,7 +197,7 @@ std::string CancelBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid) bidamount = vintx.vout[0].nValue; mtx.vin.push_back(CTxIn(bidtxid,0,CScript())); mtx.vout.push_back(CTxOut(bidamount,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,EncodeAssetOpRet('o',assetid,zeroid,0,Mypubkey()))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('o',assetid,zeroid,0,Mypubkey()))); } } return(0); @@ -198,7 +205,8 @@ std::string CancelBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid) std::string CancelSell(uint64_t txfee,uint256 assetid,uint256 asktxid) { - CMutableTransaction mtx; CTransaction vintx; uint256 hashBlock; uint64_t askamount; CPubKey mypk; + CMutableTransaction mtx; CTransaction vintx; uint256 hashBlock; uint64_t askamount; CPubKey mypk; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); @@ -209,7 +217,7 @@ std::string CancelSell(uint64_t txfee,uint256 assetid,uint256 asktxid) askamount = vintx.vout[0].nValue; mtx.vin.push_back(CTxIn(asktxid,0,CScript())); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount,mypk)); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,EncodeAssetOpRet('x',assetid,zeroid,0,Mypubkey()))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('x',assetid,zeroid,0,Mypubkey()))); } } return(0); @@ -217,7 +225,8 @@ std::string CancelSell(uint64_t txfee,uint256 assetid,uint256 asktxid) std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t fillamount) { - CTransaction vintx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t bidvout=0; uint64_t origprice,bidamount,paid_amount,remaining_required,inputs,CCchange=0; + CTransaction vintx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t bidvout=0; uint64_t origprice,bidamount,paid_amount,remaining_required,inputs,CCchange=0; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); @@ -228,18 +237,18 @@ std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t bidamount = vintx.vout[bidvout].nValue; SetAssetOrigpubkey(origpubkey,origprice,vintx); mtx.vin.push_back(CTxIn(bidtxid,bidvout,CScript())); - if ( (inputs= AddAssetInputs(mtx,mypk,assetid,fillamount,60)) > 0 ) + if ( (inputs= AddAssetInputs(cp,mtx,mypk,assetid,fillamount,60)) > 0 ) { if ( inputs > fillamount ) CCchange = (inputs - fillamount); SetAssetFillamounts(0,paid_amount,remaining_required,bidamount,fillamount,origprice); - mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,bidamount - paid_amount,GetUnspendable(EVAL_ASSETS,0))); + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,bidamount - paid_amount,GetUnspendable(cp,0))); mtx.vout.push_back(CTxOut(paid_amount,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,fillamount,pubkey2pk(origpubkey))); if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); fprintf(stderr,"remaining %llu -> origpubkey\n",(long long)remaining_required); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,EncodeAssetOpRet('B',assetid,zeroid,remaining_required,origpubkey))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('B',assetid,zeroid,remaining_required,origpubkey))); } else fprintf(stderr,"filltx wasnt for assetid\n"); } } @@ -248,7 +257,8 @@ std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,uint64_t fillamount) { - CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t askvout=0; uint64_t totalunits,askamount,paid_amount,remaining_required,inputs,CCchange=0; + CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t askvout=0; uint64_t totalunits,askamount,paid_amount,remaining_required,inputs,CCchange=0; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); @@ -260,20 +270,20 @@ std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 ask SetAssetOrigpubkey(origpubkey,totalunits,vintx); mtx.vin.push_back(CTxIn(asktxid,askvout,CScript())); if ( assetid2 == zeroid ) - inputs = AddAssetInputs(mtx,mypk,assetid2,fillamount,60); + inputs = AddAssetInputs(cp,mtx,mypk,assetid2,fillamount,60); else inputs = AddNormalinputs(mtx,mypk,fillamount,60); if ( inputs > 0 ) { if ( assetid2 == zeroid && inputs > fillamount ) CCchange = (inputs - fillamount); SetAssetFillamounts(1,paid_amount,remaining_required,askamount,fillamount,totalunits); - mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount - paid_amount,GetUnspendable(EVAL_ASSETS,0))); + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount - paid_amount,GetUnspendable(cp,0))); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,paid_amount,mypk)); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,fillamount,pubkey2pk(origpubkey))); if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); fprintf(stderr,"remaining %llu -> origpubkey\n",(long long)remaining_required); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,EncodeAssetOpRet(assetid2==zeroid?'E':'S',assetid,assetid2,remaining_required,origpubkey))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet(assetid2==zeroid?'E':'S',assetid,assetid2,remaining_required,origpubkey))); } else fprintf(stderr,"filltx not enough utxos\n"); } } diff --git a/src/cc/CCauction.h b/src/cc/CCauction.h new file mode 100644 index 000000000..52cd03d14 --- /dev/null +++ b/src/cc/CCauction.h @@ -0,0 +1,30 @@ +/****************************************************************************** + * Copyright © 2014-2018 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. * + * * + ******************************************************************************/ + + +#ifndef CC_AUCTION_H +#define CC_AUCTION_H + +#include "CCinclude.h" + +#define EVAL_AUCTION 0xe8 + +bool AuctionValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); + +std::string AuctionPost(uint64_t txfee,uint256 itemhash,uint64_t minbid,char *title,char *description); +std::string AuctionBid(uint64_t txfee,uint256 itemhash,uint64_t amount); +std::string AuctionDeliver(uint64_t txfee,uint256 itemhash,uint256 bidtxid); + +#endif diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index a689c117f..db891c583 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -14,22 +14,30 @@ ******************************************************************************/ #include "CCinclude.h" +#include "CCassets.h" +#include "CCfaucet.h" +#include "CCrewards.h" +#include "CCdice.h" +#include "CCauction.h" +#include "CClotto.h" +#include "CCponzi.h" /* CCcustom has most of the functions that need to be extended to create a new CC contract. - EVAL_CONTRACT is the naming convention and it should be added to cc/eval.h - bool Eval::Dispatch() in cc/eval.h needs to add the validation function in the switch - A CC scriptPubKey can only be spent if it is properly signed and validated. By constraining the vins and vouts, it is possible to implement a variety of functionality. CC vouts have an otherwise non-standard form, but it is properly supported by the enhanced bitcoin protocol code as a "cryptoconditions" output and the same pubkey will create a different address. This allows creation of a special address(es) for each contract type, which has the privkey public. That allows anybody to properly sign and spend it, but with the constraints on what is allowed in the validation code, the contract functionality can be implemented. + + what needs to be done to add a new contract: + 1. add EVAL_CODE to eval.h + 2. initialize the variables in the CCinit function below + 3. write a Validate function to reject any unsanctioned usage of vin/vout + 4. make helper functions to create rawtx for RPC functions + 5. add rpc calls to rpcserver.cpp and rpcserver.h and in one of the rpc.cpp files + 6. add the new .cpp files to src/Makefile.am */ -//CC *MakeAssetCond(CPubKey pk); -//CC *MakeFaucetCond(CPubKey pk); -//CC *MakeRewardsCond(CPubKey pk); - //BTCD Address: RAssetsAtGnvwgK9gVHBbAU4sVTah1hAm5 //BTCD Privkey: UvtvQVgVScXEYm4J3r4nE4nbFuGXSVM5pKec8VWXwgG9dmpWBuDh //BTCD Address: RSavingsEYcivt2DFsxsKeCjqArV6oVtVZ @@ -38,7 +46,7 @@ // Assets, aka Tokens #define FUNCNAME IsAssetsInput #define EVALCODE EVAL_ASSETS -const char *AssetsCCaddr = "RGKRjeTBw4LYFotSDLT6RWzMHbhXri6BG6" ;//"RFYE2yL3KknWdHK6uNhvWacYsCUtwzjY3u"; +const char *AssetsCCaddr = "RGKRjeTBw4LYFotSDLT6RWzMHbhXri6BG6"; //"RFYE2yL3KknWdHK6uNhvWacYsCUtwzjY3u"; char AssetsCChexstr[67] = { "02adf84e0e075cf90868bd4e3d34a03420e034719649c41f371fc70d8e33aa2702" }; uint8_t AssetsCCpriv[32] = { 0x9b, 0x17, 0x66, 0xe5, 0x82, 0x66, 0xac, 0xb6, 0xba, 0x43, 0x83, 0x74, 0xf7, 0x63, 0x11, 0x3b, 0xf0, 0xf3, 0x50, 0x6f, 0xd9, 0x6b, 0x67, 0x85, 0xf9, 0x7a, 0xf0, 0x54, 0x4d, 0xb1, 0x30, 0x77 }; @@ -49,7 +57,7 @@ uint8_t AssetsCCpriv[32] = { 0x9b, 0x17, 0x66, 0xe5, 0x82, 0x66, 0xac, 0xb6, 0xb // Faucet #define FUNCNAME IsFaucetInput #define EVALCODE EVAL_FAUCET -const char *FaucetCCaddr = "R9zHrofhRbub7ER77B7NrVch3A63R39GuC" ;//"RKQV4oYs4rvxAWx1J43VnT73rSTVtUeckk"; +const char *FaucetCCaddr = "R9zHrofhRbub7ER77B7NrVch3A63R39GuC"; //"RKQV4oYs4rvxAWx1J43VnT73rSTVtUeckk"; char FaucetCChexstr[67] = { "03682b255c40d0cde8faee381a1a50bbb89980ff24539cb8518e294d3a63cefe12" }; uint8_t FaucetCCpriv[32] = { 0xd4, 0x4f, 0xf2, 0x31, 0x71, 0x7d, 0x28, 0x02, 0x4b, 0xc7, 0xdd, 0x71, 0xa0, 0x39, 0xc4, 0xbe, 0x1a, 0xfe, 0xeb, 0xc2, 0x46, 0xda, 0x76, 0xf8, 0x07, 0x53, 0x3d, 0x96, 0xb4, 0xca, 0xa0, 0xe9 }; @@ -60,89 +68,108 @@ uint8_t FaucetCCpriv[32] = { 0xd4, 0x4f, 0xf2, 0x31, 0x71, 0x7d, 0x28, 0x02, 0x4 // Rewards #define FUNCNAME IsRewardsInput #define EVALCODE EVAL_REWARDS -const char *RewardsCCaddr = "RJCqA4jQTFEZ841dZgxko8aYgUU3FRNGNm" ;//"RYRJGMAYEfLCZ6ZddbpxPiUZ1sens8vPYK"; +const char *RewardsCCaddr = "RJCqA4jQTFEZ841dZgxko8aYgUU3FRNGNm"; //"RYRJGMAYEfLCZ6ZddbpxPiUZ1sens8vPYK"; char RewardsCChexstr[67] = { "026f00fdc2f1ed0006d66e2ca1787633590581c2fc90e7cb7b01a6c1131b40e94d" }; uint8_t RewardsCCpriv[32] = { 0x9f, 0x0c, 0x57, 0xdc, 0x6f, 0x78, 0xae, 0xb0, 0xc7, 0x62, 0x9e, 0x7d, 0x2b, 0x90, 0x6b, 0xbd, 0x40, 0x78, 0x19, 0x5b, 0x3c, 0xb8, 0x82, 0x2d, 0x29, 0x84, 0x72, 0x7a, 0x59, 0x5a, 0x4b, 0x69 }; #include "CCcustom.inc" #undef FUNCNAME #undef EVALCODE -/*bool IsAssetsInput(CScript const& scriptSig) +// Dice +#define FUNCNAME IsDiceInput +#define EVALCODE EVAL_DICE +const char *DiceCCaddr = "REabWB7KjFN5C3LFMZ5odExHPenYzHLtVw"; //"RLEe8f7Eg3TDuXii9BmNiiiaVGraHUt25c"; +char DiceCChexstr[67] = { "039d966927cfdadab3ee6c56da63c21f17ea753dde4b3dfd41487103e24b27e94e" }; +uint8_t DiceCCpriv[32] = { 0x0e, 0xe8, 0xf5, 0xb4, 0x3d, 0x25, 0xcc, 0x35, 0xd1, 0xf1, 0x2f, 0x04, 0x5f, 0x01, 0x26, 0xb8, 0xd1, 0xac, 0x3a, 0x5a, 0xea, 0xe0, 0x25, 0xa2, 0x8f, 0x2a, 0x8e, 0x0e, 0xf9, 0x34, 0xfa, 0x77 }; +#include "CCcustom.inc" +#undef FUNCNAME +#undef EVALCODE + +// Lotto +#define FUNCNAME IsLottoInput +#define EVALCODE EVAL_LOTTO +const char *LottoCCaddr = "RNXZxgyWSAE6XS3qGnTaf5dVNCxnYzhPrg"; //"RLW6hhRqBZZMBndnyPv29Yg3krh6iBYCyg"; +char LottoCChexstr[67] = { "03f72d2c4db440df1e706502b09ca5fec73ffe954ea1883e4049e98da68690d98f" }; +uint8_t LottoCCpriv[32] = { 0xb4, 0xac, 0xc2, 0xd9, 0x67, 0x34, 0xd7, 0x58, 0x80, 0x4e, 0x25, 0x55, 0xc0, 0x50, 0x66, 0x84, 0xbb, 0xa2, 0xe7, 0xc0, 0x39, 0x17, 0xb4, 0xc5, 0x07, 0xb7, 0x3f, 0xca, 0x07, 0xb0, 0x9a, 0xeb }; +#include "CCcustom.inc" +#undef FUNCNAME +#undef EVALCODE + +// Ponzi +#define FUNCNAME IsPonziInput +#define EVALCODE EVAL_PONZI +const char *PonziCCaddr = "RUKTbLBeKgHkm3Ss4hKZP3ikuLW1xx7B2x"; //"RWSHRbxnJYLvDjpcQ2i8MekgP6h2ctTKaj"; +char PonziCChexstr[67] = { "039b52d294b413b07f3643c1a28c5467901a76562d8b39a785910ae0a0f3043810" }; +uint8_t PonziCCpriv[32] = { 0x11, 0xe1, 0xea, 0x3e, 0xdb, 0x36, 0xf0, 0xa8, 0xc6, 0x34, 0xe1, 0x21, 0xb8, 0x02, 0xb9, 0x4b, 0x12, 0x37, 0x8f, 0xa0, 0x86, 0x23, 0x50, 0xb2, 0x5f, 0xe4, 0xe7, 0x36, 0x0f, 0xda, 0xae, 0xfc }; +#include "CCcustom.inc" +#undef FUNCNAME +#undef EVALCODE + +// Auction +#define FUNCNAME IsAuctionInput +#define EVALCODE EVAL_AUCTION +const char *AuctionCCaddr = "RL4YPX7JYG3FnvoPqWF2pn3nQknH5NWEwx"; //"RFtVDNmdTZBTNZdmFRbfBgJ6LitgTghikL"; +char AuctionCChexstr[67] = { "037eefe050c14cb60ae65d5b2f69eaa1c9006826d729bc0957bdc3024e3ca1dbe6" }; +uint8_t AuctionCCpriv[32] = { 0x8c, 0x1b, 0xb7, 0x8c, 0x02, 0xa3, 0x9d, 0x21, 0x28, 0x59, 0xf5, 0xea, 0xda, 0xec, 0x0d, 0x11, 0xcd, 0x38, 0x47, 0xac, 0x0b, 0x6f, 0x19, 0xc0, 0x24, 0x36, 0xbf, 0x1c, 0x0a, 0x06, 0x31, 0xfb }; +#include "CCcustom.inc" +#undef FUNCNAME +#undef EVALCODE + +struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode) { - CC *cond; - if (!(cond = GetCryptoCondition(scriptSig))) - return false; - // Recurse the CC tree to find asset condition - auto findEval = [&] (CC *cond, struct CCVisitor _) { - bool r = cc_typeId(cond) == CC_Eval && cond->codeLength == 1 && cond->code[0] == EVAL_ASSETS; - // false for a match, true for continue - return r ? 0 : 1; - }; - CCVisitor visitor = {findEval, (uint8_t*)"", 0, NULL}; - bool out =! cc_visit(cond, visitor); - cc_free(cond); - return out; -} - -bool IsFaucetInput(CScript const& scriptSig) -{ - CC *cond; - if (!(cond = GetCryptoCondition(scriptSig))) - return false; - // Recurse the CC tree to find asset condition - auto findEval = [&] (CC *cond, struct CCVisitor _) { - bool r = cc_typeId(cond) == CC_Eval && cond->codeLength == 1 && cond->code[0] == EVAL_FAUCET; - // false for a match, true for continue - return r ? 0 : 1; - }; - CCVisitor visitor = {findEval, (uint8_t*)"", 0, NULL}; - bool out =! cc_visit(cond, visitor); - cc_free(cond); - return out; -} - -bool IsRewardsInput(CScript const& scriptSig) -{ - CC *cond; - if (!(cond = GetCryptoCondition(scriptSig))) - return false; - // Recurse the CC tree to find asset condition - auto findEval = [&] (CC *cond, struct CCVisitor _) { - bool r = cc_typeId(cond) == CC_Eval && cond->codeLength == 1 && cond->code[0] == EVAL_REWARDS; - // false for a match, true for continue - return r ? 0 : 1; - }; - CCVisitor visitor = {findEval, (uint8_t*)"", 0, NULL}; - bool out =! cc_visit(cond, visitor); - cc_free(cond); - return out; -}*/ - -uint64_t AddFaucetInputs(CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs); - -CPubKey GetUnspendable(uint8_t evalcode,uint8_t *unspendablepriv) -{ - static CPubKey nullpk; - if ( unspendablepriv != 0 ) - memset(unspendablepriv,0,32); - if ( evalcode == EVAL_ASSETS ) - { - if ( unspendablepriv != 0 ) - memcpy(unspendablepriv,AssetsCCpriv,32); - return(pubkey2pk(ParseHex(AssetsCChexstr))); - } - else if ( evalcode == EVAL_FAUCET ) - { - if ( unspendablepriv != 0 ) - memcpy(unspendablepriv,FaucetCCpriv,32); - return(pubkey2pk(ParseHex(FaucetCChexstr))); - } - else if ( evalcode == EVAL_REWARDS ) - { - if ( unspendablepriv != 0 ) - memcpy(unspendablepriv,RewardsCCpriv,32); - return(pubkey2pk(ParseHex(RewardsCChexstr))); - } - else return(nullpk); + cp->evalcode = evalcode; + switch ( evalcode ) + { + case EVAL_ASSETS: + strcpy(cp->unspendableCCaddr,AssetsCCaddr); + strcpy(cp->CChexstr,AssetsCChexstr); + memcpy(cp->CCpriv,AssetsCCpriv,32); + cp->validate = AssetsValidate; + cp->ismyvin = IsAssetsInput; + break; + case EVAL_FAUCET: + strcpy(cp->unspendableCCaddr,FaucetCCaddr); + strcpy(cp->CChexstr,FaucetCChexstr); + memcpy(cp->CCpriv,FaucetCCpriv,32); + cp->validate = FaucetValidate; + cp->ismyvin = IsFaucetInput; + break; + case EVAL_REWARDS: + strcpy(cp->unspendableCCaddr,RewardsCCaddr); + strcpy(cp->CChexstr,RewardsCChexstr); + memcpy(cp->CCpriv,RewardsCCpriv,32); + cp->validate = RewardsValidate; + cp->ismyvin = IsRewardsInput; + break; + case EVAL_DICE: + strcpy(cp->unspendableCCaddr,DiceCCaddr); + strcpy(cp->CChexstr,DiceCChexstr); + memcpy(cp->CCpriv,DiceCCpriv,32); + cp->validate = DiceValidate; + cp->ismyvin = IsDiceInput; + break; + case EVAL_LOTTO: + strcpy(cp->unspendableCCaddr,LottoCCaddr); + strcpy(cp->CChexstr,LottoCChexstr); + memcpy(cp->CCpriv,LottoCCpriv,32); + cp->validate = LottoValidate; + cp->ismyvin = IsLottoInput; + break; + case EVAL_PONZI: + strcpy(cp->unspendableCCaddr,PonziCCaddr); + strcpy(cp->CChexstr,PonziCChexstr); + memcpy(cp->CCpriv,PonziCCpriv,32); + cp->validate = PonziValidate; + cp->ismyvin = IsPonziInput; + break; + case EVAL_AUCTION: + strcpy(cp->unspendableCCaddr,AuctionCCaddr); + strcpy(cp->CChexstr,AuctionCChexstr); + memcpy(cp->CCpriv,AuctionCCpriv,32); + cp->validate = AuctionValidate; + cp->ismyvin = IsAuctionInput; + break; + } + return(cp); } diff --git a/src/cc/CCcustom.inc b/src/cc/CCcustom.inc index e88a813ac..a5a88f1eb 100644 --- a/src/cc/CCcustom.inc +++ b/src/cc/CCcustom.inc @@ -15,4 +15,3 @@ bool FUNCNAME(CScript const& scriptSig) cc_free(cond); return out; } - diff --git a/src/cc/CCdice.h b/src/cc/CCdice.h new file mode 100644 index 000000000..7c89d18d5 --- /dev/null +++ b/src/cc/CCdice.h @@ -0,0 +1,29 @@ +/****************************************************************************** + * Copyright © 2014-2018 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. * + * * + ******************************************************************************/ + + +#ifndef CC_DICE_H +#define CC_DICE_H + +#include "CCinclude.h" + +#define EVAL_DICE 0xe6 + +bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); + +std::string DiceFund(uint64_t txfee,uint64_t funds); +std::string DiceBet(uint64_t txfee,uint64_t amount,uint64_t odds); + +#endif diff --git a/src/cc/CCfaucet.h b/src/cc/CCfaucet.h index 42a097369..b478165b5 100644 --- a/src/cc/CCfaucet.h +++ b/src/cc/CCfaucet.h @@ -21,11 +21,9 @@ #define EVAL_FAUCET 0xe4 -extern const char *FaucetCCaddr; -extern char FaucetCChexstr[67]; +bool FaucetValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); // CCcustom -bool IsFaucetInput(CScript const& scriptSig); std::string FaucetFund(uint64_t txfee,uint64_t funds); std::string FaucetGet(uint64_t txfee); diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 4d1d1cf2c..f72fce237 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -28,6 +28,17 @@ #include #include +struct CCcontract_info +{ + uint256 prevtxid; + char unspendableCCaddr[64],CChexstr[72]; + uint8_t CCpriv[32]; + bool (*validate)(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); + bool (*ismyvin)(CScript const& scriptSig); + uint8_t evalcode,didinit; +}; +struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode); + #ifdef ENABLE_WALLET extern CWallet* pwalletMain; #endif @@ -36,18 +47,20 @@ bool GetAddressUnspent(uint160 addressHash, int type,std::vector pubkey); -bool GetCCaddress(uint8_t evalcode,char *destaddr,CPubKey pk); +bool GetCCaddress(struct CCcontract_info *cp,char *destaddr,CPubKey pk); bool ConstrainVout(CTxOut vout,int32_t CCflag,char *cmpaddr,uint64_t nValue); bool PreventCC(Eval* eval,const CTransaction &tx,int32_t preventCCvins,int32_t numvins,int32_t preventCCvouts,int32_t numvouts); bool Getscriptaddress(char *destaddr,const CScript &scriptPubKey); @@ -55,7 +68,7 @@ std::vector Mypubkey(); bool Myprivkey(uint8_t myprivkey[]); // CCtx -std::string FinalizeCCTx(uint8_t evalcode,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret); +std::string FinalizeCCTx(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret); void SetCCunspents(std::vector > &unspentOutputs,char *coinaddr); uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,int32_t maxinputs); diff --git a/src/cc/CClotto.h b/src/cc/CClotto.h new file mode 100644 index 000000000..a0354252e --- /dev/null +++ b/src/cc/CClotto.h @@ -0,0 +1,29 @@ +/****************************************************************************** + * Copyright © 2014-2018 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. * + * * + ******************************************************************************/ + + +#ifndef CC_LOTTO_H +#define CC_LOTTO_H + +#include "CCinclude.h" + +#define EVAL_LOTTO 0xe9 + +bool LottoValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); + +std::string LottoTicket(uint64_t txfee,uint64_t numtickets); +std::string LottoWinner(uint64_t txfee); + +#endif diff --git a/src/cc/CCponzi.h b/src/cc/CCponzi.h new file mode 100644 index 000000000..eb28737f7 --- /dev/null +++ b/src/cc/CCponzi.h @@ -0,0 +1,29 @@ +/****************************************************************************** + * Copyright © 2014-2018 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. * + * * + ******************************************************************************/ + + +#ifndef CC_PONZI_H +#define CC_PONZI_H + +#include "CCinclude.h" + +#define EVAL_PONZI 0xe7 + +bool PonziValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); + +std::string PonziBuy(uint64_t txfee,uint64_t amount); +std::string PonziClaim(uint64_t txfee); + +#endif diff --git a/src/cc/CCrewards.h b/src/cc/CCrewards.h new file mode 100644 index 000000000..b9addfb95 --- /dev/null +++ b/src/cc/CCrewards.h @@ -0,0 +1,30 @@ +/****************************************************************************** + * Copyright © 2014-2018 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. * + * * + ******************************************************************************/ + + +#ifndef CC_REWARDS_H +#define CC_REWARDS_H + +#include "CCinclude.h" + +#define EVAL_REWARDS 0xe5 + +bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); + +std::string RewardsFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit); +std::string RewardsLock(uint64_t txfee,char *planstr,uint64_t amount); +std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid); + +#endif diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index 5a8b3607a..b37e6f061 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -38,7 +38,7 @@ bool SignTx(CMutableTransaction &mtx,int32_t vini,uint64_t utxovalue,const CScri #endif } -std::string FinalizeCCTx(uint8_t evalcode,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret) +std::string FinalizeCCTx(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret) { auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); CTransaction vintx; std::string hex; uint256 hashBlock; uint64_t vinimask=0,utxovalues[64],change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n,err = 0; char myaddr[64],destaddr[64],unspendable[64]; uint8_t *privkey,myprivkey[32],unspendablepriv[32],*msg32 = 0; CC *mycond=0,*othercond=0,*cond; CPubKey unspendablepk; @@ -54,11 +54,11 @@ std::string FinalizeCCTx(uint8_t evalcode,CMutableTransaction &mtx,CPubKey mypk, return(0); } Myprivkey(myprivkey); - unspendablepk = GetUnspendable(evalcode,unspendablepriv); - GetCCaddress(evalcode,myaddr,mypk); - mycond = MakeCCcond1(evalcode,mypk); - GetCCaddress(evalcode,unspendable,unspendablepk); - othercond = MakeCCcond1(evalcode,unspendablepk); + unspendablepk = GetUnspendable(cp,unspendablepriv); + GetCCaddress(cp,myaddr,mypk); + mycond = MakeCCcond1(cp->evalcode,mypk); + GetCCaddress(cp,unspendable,unspendablepk); + othercond = MakeCCcond1(cp->evalcode,unspendablepk); //fprintf(stderr,"myCCaddr.(%s) %p vs unspendable.(%s) %p\n",myaddr,mycond,unspendable,othercond); memset(utxovalues,0,sizeof(utxovalues)); for (i=0; i>=8) + if ( (buf[i]= (char)(bits & 0xff)) == 0 ) + break; + buf[i] = 0; + return(i); +} + +uint64_t stringbits(char *str) +{ + uint64_t bits = 0; + if ( str == 0 ) + return(0); + int32_t i,n = (int32_t)strlen(str); + if ( n > 8 ) + n = 8; + for (i=n-1; i>=0; i--) + bits = (bits << 8) | (str[i] & 0xff); + //printf("(%s) -> %llx %llu\n",str,(long long)bits,(long long)bits); + return(bits); +} + uint256 revuint256(uint256 txid) { uint256 revtxid; int32_t i; @@ -106,13 +130,13 @@ bool Getscriptaddress(char *destaddr,const CScript &scriptPubKey) return(false); } -bool GetCCaddress(uint8_t evalcode,char *destaddr,CPubKey pk) +bool GetCCaddress(struct CCcontract_info *cp,char *destaddr,CPubKey pk) { CC *payoutCond; destaddr[0] = 0; if ( pk.size() == 0 ) - pk = GetUnspendable(evalcode,0); - if ( (payoutCond= MakeCCcond1(evalcode,pk)) != 0 ) + pk = GetUnspendable(cp,0); + if ( (payoutCond= MakeCCcond1(cp->evalcode,pk)) != 0 ) { Getscriptaddress(destaddr,CCPubKey(payoutCond)); cc_free(payoutCond); @@ -207,3 +231,31 @@ bool Myprivkey(uint8_t myprivkey[]) return(false); } +CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv) +{ + if ( unspendablepriv != 0 ) + memcpy(unspendablepriv,cp->CCpriv,32); + return(pubkey2pk(ParseHex(cp->CChexstr))); +} + +bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector paramsNull,const CTransaction &ctx, unsigned int nIn) +{ + CTransaction createTx; uint256 txid,assetid,assetid2,hashBlock; uint8_t funcid; int32_t i,n; uint64_t amount; std::vector origpubkey; + txid = ctx.GetHash(); + if ( txid == cp->prevtxid ) + return(true); + if ( paramsNull.size() != 0 ) // Don't expect params + return eval->Invalid("Cannot have params"); + else if ( ctx.vout.size() == 0 ) + return eval->Invalid("no-vouts"); + else if ( (*cp->validate)(cp,eval,ctx) != 0 ) + { + cp->prevtxid = txid; + return(true); + } + return(false); +} + + +uint64_t AddFaucetInputs(CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs); + diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 11d375698..8462c0b16 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -48,7 +48,7 @@ create vin.0: normal input vout.0: issuance assetoshis to CC - vout.1: tag sent to AssetsCCaddress + vout.1: tag sent to normal address of AssetsCCaddress vout.2: normal output for change (if any) vout.n-1: opreturn [EVAL_ASSETS] ['c'] [origpubkey] "" "" @@ -129,15 +129,22 @@ vout.n-1: opreturn [EVAL_ASSETS] ['E'] [assetid vin0+1] [assetid vin2] [remaining asset2 required] [origpubkey] */ -bool AssetValidate(Eval* eval,const CTransaction &tx,int32_t numvouts,uint8_t funcid,uint256 assetid,uint256 assetid2,uint64_t remaining_price,std::vector origpubkey) +bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { static uint256 zero; - CTxDestination address; const CTransaction vinTx; uint256 hashBlock; int32_t i,starti,numvins,preventCCvins,preventCCvouts; uint64_t nValue,assetoshis,outputs,inputs,tmpprice,totalunits,ignore; std::vector tmporigpubkey,ignorepubkey; char destaddr[64],origaddr[64],CCaddr[64]; - fprintf(stderr,"AssetValidate (%c)\n",funcid); + CTxDestination address; CTransaction vinTx,createTx; uint256 hashBlock,assetid,assetid2; int32_t i,starti,numvins,numvouts,preventCCvins,preventCCvouts; uint64_t amount,remaining_price,nValue,assetoshis,outputs,inputs,tmpprice,totalunits,ignore; std::vector origpubkey,tmporigpubkey,ignorepubkey; uint8_t funcid; char destaddr[64],origaddr[64],CCaddr[64]; numvins = tx.vin.size(); + numvouts = tx.vout.size(); outputs = inputs = 0; preventCCvins = preventCCvouts = -1; - if ( IsCCInput(tx.vin[0].scriptSig) != 0 ) + if ( (funcid= DecodeAssetOpRet(tx.vout[numvouts-1].scriptPubKey,assetid,assetid2,amount,origpubkey)) == 0 ) + return eval->Invalid("Invalid opreturn payload"); + fprintf(stderr,"AssetValidate (%c)\n",funcid); + if ( eval->GetTxUnconfirmed(assetid,createTx,hashBlock) == 0 ) + return eval->Invalid("cant find asset create txid"); + else if ( assetid2 != zero && eval->GetTxUnconfirmed(assetid2,createTx,hashBlock) == 0 ) + return eval->Invalid("cant find asset2 create txid"); + else if ( IsCCInput(tx.vin[0].scriptSig) != 0 ) return eval->Invalid("illegal asset vin0"); else if ( numvouts < 1 ) return eval->Invalid("no vouts"); @@ -148,7 +155,7 @@ bool AssetValidate(Eval* eval,const CTransaction &tx,int32_t numvouts,uint8_t fu else starti = 1; if ( assetid == zero ) return eval->Invalid("illegal assetid"); - else if ( AssetExactAmounts(inputs,starti,outputs,eval,tx,assetid) == false ) + else if ( AssetExactAmounts(cp,inputs,starti,outputs,eval,tx,assetid) == false ) return eval->Invalid("asset inputs != outputs"); } switch ( funcid ) @@ -179,11 +186,11 @@ bool AssetValidate(Eval* eval,const CTransaction &tx,int32_t numvouts,uint8_t fu // vout.n-1: opreturn [EVAL_ASSETS] ['b'] [assetid] [amount of asset required] [origpubkey] if ( remaining_price == 0 ) return eval->Invalid("illegal null amount for buyoffer"); - else if ( ConstrainVout(tx.vout[0],1,(char *)AssetsCCaddr,0) == 0 ) + else if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,0) == 0 ) return eval->Invalid("invalid vout for buyoffer"); preventCCvins = 1; preventCCvouts = 1; - fprintf(stderr,"buy offer validated to destaddr.(%s)\n",(char *)AssetsCCaddr); + fprintf(stderr,"buy offer validated to destaddr.(%s)\n",cp->unspendableCCaddr); break; case 'o': // cancelbuy @@ -192,7 +199,7 @@ bool AssetValidate(Eval* eval,const CTransaction &tx,int32_t numvouts,uint8_t fu //vout.0: vin.1 value to original pubkey buyTx.vout[0].nValue -> [origpubkey] //vout.1: normal output for change (if any) //vout.n-1: opreturn [EVAL_ASSETS] ['o'] - if ( (nValue= AssetValidateBuyvin(eval,tmpprice,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 ) + if ( (nValue= AssetValidateBuyvin(cp,eval,tmpprice,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 ) return(false); else if ( ConstrainVout(tx.vout[0],0,origaddr,nValue) == 0 ) return eval->Invalid("invalid refund for cancelbuy"); @@ -212,7 +219,7 @@ bool AssetValidate(Eval* eval,const CTransaction &tx,int32_t numvouts,uint8_t fu //vout.4: normal output for change (if any) //vout.n-1: opreturn [EVAL_ASSETS] ['B'] [assetid] [remaining asset required] [origpubkey] preventCCvouts = 4; - if ( (nValue= AssetValidateBuyvin(eval,totalunits,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 ) + if ( (nValue= AssetValidateBuyvin(cp,eval,totalunits,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 ) return(false); else if ( numvouts < 3 ) return eval->Invalid("not enough vouts for fillbuy"); @@ -230,7 +237,7 @@ bool AssetValidate(Eval* eval,const CTransaction &tx,int32_t numvouts,uint8_t fu { if ( remaining_price < 10000 ) return eval->Invalid("dust vout0 to AssetsCCaddr for fillbuy"); - else if ( ConstrainVout(tx.vout[0],1,(char *)AssetsCCaddr,0) == 0 ) + else if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,0) == 0 ) return eval->Invalid("mismatched vout0 AssetsCCaddr for fillbuy"); } } @@ -247,7 +254,7 @@ bool AssetValidate(Eval* eval,const CTransaction &tx,int32_t numvouts,uint8_t fu //'e'.vout.n-1: opreturn [EVAL_ASSETS] ['e'] [assetid] [assetid2] [amount of asset2 required] [origpubkey] if ( remaining_price == 0 ) return eval->Invalid("illegal null remaining_price for selloffer"); - else if ( ConstrainVout(tx.vout[0],1,(char *)AssetsCCaddr,0) == 0 ) + else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) return eval->Invalid("mismatched vout0 AssetsCCaddr for selloffer"); preventCCvouts = 1; break; @@ -258,7 +265,7 @@ bool AssetValidate(Eval* eval,const CTransaction &tx,int32_t numvouts,uint8_t fu //vout.0: vin.1 assetoshis to original pubkey CC sellTx/exchangeTx.vout[0].nValue -> [origpubkey] //vout.1: normal output for change (if any) //vout.n-1: opreturn [EVAL_ASSETS] ['x'] [assetid] - if ( (assetoshis= AssetValidateSellvin(eval,tmpprice,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 ) + if ( (assetoshis= AssetValidateSellvin(cp,eval,tmpprice,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 ) return(false); else if ( ConstrainVout(tx.vout[0],1,CCaddr,assetoshis) == 0 ) return eval->Invalid("invalid vout for cancel"); @@ -281,10 +288,10 @@ bool AssetValidate(Eval* eval,const CTransaction &tx,int32_t numvouts,uint8_t fu //'E'.vout.n-1: opreturn [EVAL_ASSETS] ['E'] [assetid vin0+1] [assetid vin2] [remaining asset2 required] [origpubkey] if ( funcid == 'E' ) { - if ( AssetExactAmounts(inputs,1,outputs,eval,tx,assetid2) == false ) + if ( AssetExactAmounts(cp,inputs,1,outputs,eval,tx,assetid2) == false ) eval->Invalid("asset2 inputs != outputs"); } - if ( (assetoshis= AssetValidateSellvin(eval,totalunits,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 ) + if ( (assetoshis= AssetValidateSellvin(cp,eval,totalunits,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 ) return(false); else if ( numvouts < 3 ) return eval->Invalid("not enough vouts for fill"); @@ -304,7 +311,7 @@ bool AssetValidate(Eval* eval,const CTransaction &tx,int32_t numvouts,uint8_t fu { if ( remaining_price < 10000 ) return eval->Invalid("dust vout0 to AssetsCCaddr for fill"); - else if ( ConstrainVout(tx.vout[0],1,(char *)AssetsCCaddr,0) == 0 ) + else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) return eval->Invalid("mismatched vout0 AssetsCCaddr for fill"); } } @@ -314,31 +321,4 @@ bool AssetValidate(Eval* eval,const CTransaction &tx,int32_t numvouts,uint8_t fu return(PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts)); } -bool ProcessAssets(Eval* eval, std::vector paramsNull,const CTransaction &ctx, unsigned int nIn) -{ - static uint256 zero,prevtxid; - CTransaction createTx; uint256 txid,assetid,assetid2,hashBlock; uint8_t funcid; int32_t i,n; uint64_t amount; std::vector origpubkey; - txid = ctx.GetHash(); - if ( txid == prevtxid ) - return(true); - fprintf(stderr,"ProcessAssets\n"); - if ( paramsNull.size() != 0 ) // Don't expect params - return eval->Invalid("Cannot have params"); - else if ( (n= ctx.vout.size()) == 0 ) - return eval->Invalid("no-vouts"); - else if ( (funcid= DecodeAssetOpRet(ctx.vout[n-1].scriptPubKey,assetid,assetid2,amount,origpubkey)) == 0 ) - return eval->Invalid("Invalid opreturn payload"); - else if ( eval->GetTxUnconfirmed(assetid,createTx,hashBlock) == 0 ) - return eval->Invalid("cant find asset create txid"); - else if ( assetid2 != zero && eval->GetTxUnconfirmed(assetid2,createTx,hashBlock) == 0 ) - return eval->Invalid("cant find asset2 create txid"); - else if ( AssetValidate(eval,ctx,n,funcid,assetid,assetid2,amount,origpubkey) != 0 ) - { - prevtxid = txid; - fprintf(stderr,"AssetValidate.(%c) passed\n",funcid); - return(true); - } - fprintf(stderr,"AssetValidate.(%c) failed\n",funcid); - return(false); -} diff --git a/src/cc/auction.cpp b/src/cc/auction.cpp new file mode 100644 index 000000000..a11f66e85 --- /dev/null +++ b/src/cc/auction.cpp @@ -0,0 +1,202 @@ +/****************************************************************************** + * Copyright © 2014-2018 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 "CCauction.h" +#include "../txmempool.h" + +/* +*/ + +// start of consensus code + +uint64_t IsAuctionvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v) +{ + char destaddr[64]; + if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 ) + { + if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 ) + return(tx.vout[v].nValue); + } + return(0); +} + +bool AuctionExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee) +{ + static uint256 zerohash; + CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; uint64_t inputs=0,outputs=0,assetoshis; + numvins = tx.vin.size(); + numvouts = tx.vout.size(); + for (i=0; iismyvin)(tx.vin[i].scriptSig) != 0 ) + { + //fprintf(stderr,"vini.%d check mempool\n",i); + if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 ) + return eval->Invalid("cant find vinTx"); + else + { + //fprintf(stderr,"vini.%d check hash and vout\n",i); + if ( hashBlock == zerohash ) + return eval->Invalid("cant Auction from mempool"); + if ( (assetoshis= IsAuctionvout(cp,vinTx,tx.vin[i].prevout.n)) != 0 ) + inputs += assetoshis; + } + } + } + for (i=0; iInvalid("mismatched inputs != outputs + COIN + txfee"); + } + else return(true); +} + +bool AuctionValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) +{ + int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; bool retval; + numvins = tx.vin.size(); + numvouts = tx.vout.size(); + preventCCvins = preventCCvouts = -1; + if ( numvouts < 1 ) + return eval->Invalid("no vouts"); + else + { + //fprintf(stderr,"check vins\n"); + for (i=0; iInvalid("illegal normal vini"); + } + } + //fprintf(stderr,"check amounts\n"); + if ( AuctionExactAmounts(cp,eval,tx,1,10000) == false ) + { + fprintf(stderr,"Auctionget invalid amount\n"); + return false; + } + else + { + preventCCvouts = 1; + if ( IsAuctionvout(cp,tx,0) != 0 ) + { + preventCCvouts++; + i = 1; + } else i = 0; + if ( tx.vout[i].nValue != COIN ) + return eval->Invalid("invalid Auction output"); + retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts); + if ( retval != 0 ) + fprintf(stderr,"Auctionget validated\n"); + else fprintf(stderr,"Auctionget invalid\n"); + return(retval); + } + } +} +// end of consensus code + +// helper functions for rpc calls in rpcwallet.cpp + +uint64_t AddAuctionInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) +{ + char coinaddr[64]; uint64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t n = 0; + std::vector > unspentOutputs; + GetCCaddress(cp,coinaddr,pk); + SetCCunspents(unspentOutputs,coinaddr); + for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) + { + txid = it->first.txhash; + if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) + { + if ( (nValue= IsAuctionvout(cp,vintx,(int32_t)it->first.index)) > 0 ) + { + if ( total != 0 && maxinputs != 0 ) + mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript())); + nValue = it->second.satoshis; + totalinputs += nValue; + n++; + if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) ) + break; + } + } + } + return(totalinputs); +} + +std::string AuctionBid(uint64_t txfee,uint256 itemhash,uint64_t amount) +{ + CMutableTransaction mtx; CPubKey mypk,Auctionpk; CScript opret; uint64_t inputs,CCchange=0,nValue=COIN; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_AUCTION); + if ( txfee == 0 ) + txfee = 10000; + Auctionpk = GetUnspendable(cp,0); + mypk = pubkey2pk(Mypubkey()); + if ( (inputs= AddAuctionInputs(cp,mtx,Auctionpk,nValue+txfee,60)) > 0 ) + { + if ( inputs > nValue ) + CCchange = (inputs - nValue - txfee); + if ( CCchange != 0 ) + mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,CCchange,Auctionpk)); + mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } else fprintf(stderr,"cant find Auction inputs\n"); + return(0); +} + +std::string AuctionDeliver(uint64_t txfee,uint256 itemhash,uint256 bidtxid) +{ + CMutableTransaction mtx; CPubKey mypk,Auctionpk; CScript opret; uint64_t inputs,CCchange=0,nValue=COIN; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_AUCTION); + if ( txfee == 0 ) + txfee = 10000; + Auctionpk = GetUnspendable(cp,0); + mypk = pubkey2pk(Mypubkey()); + if ( (inputs= AddAuctionInputs(cp,mtx,Auctionpk,nValue+txfee,60)) > 0 ) + { + if ( inputs > nValue ) + CCchange = (inputs - nValue - txfee); + if ( CCchange != 0 ) + mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,CCchange,Auctionpk)); + mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } else fprintf(stderr,"cant find Auction inputs\n"); + return(0); +} + +std::string AuctionPost(uint64_t txfee,uint256 itemhash,uint64_t minbid,char *title,char *description) +{ + CMutableTransaction mtx; CPubKey mypk,Auctionpk; uint64_t funds = 0; CScript opret; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_AUCTION); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + Auctionpk = GetUnspendable(cp,0); + if ( AddNormalinputs(mtx,mypk,txfee,64) > 0 ) + { + mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,funds,Auctionpk)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } + return(0); +} + + diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp new file mode 100644 index 000000000..f76fc02ab --- /dev/null +++ b/src/cc/dice.cpp @@ -0,0 +1,182 @@ +/****************************************************************************** + * Copyright © 2014-2018 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 "CCdice.h" +#include "../txmempool.h" + +/* +*/ + +// start of consensus code + +uint64_t IsDicevout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v) +{ + char destaddr[64]; + if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 ) + { + if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 ) + return(tx.vout[v].nValue); + } + return(0); +} + +bool DiceExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee) +{ + static uint256 zerohash; + CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; uint64_t inputs=0,outputs=0,assetoshis; + numvins = tx.vin.size(); + numvouts = tx.vout.size(); + for (i=0; iismyvin)(tx.vin[i].scriptSig) != 0 ) + { + //fprintf(stderr,"vini.%d check mempool\n",i); + if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 ) + return eval->Invalid("cant find vinTx"); + else + { + //fprintf(stderr,"vini.%d check hash and vout\n",i); + if ( hashBlock == zerohash ) + return eval->Invalid("cant dice from mempool"); + if ( (assetoshis= IsDicevout(cp,vinTx,tx.vin[i].prevout.n)) != 0 ) + inputs += assetoshis; + } + } + } + for (i=0; iInvalid("mismatched inputs != outputs + COIN + txfee"); + } + else return(true); +} + +bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) +{ + int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; bool retval; + numvins = tx.vin.size(); + numvouts = tx.vout.size(); + preventCCvins = preventCCvouts = -1; + if ( numvouts < 1 ) + return eval->Invalid("no vouts"); + else + { + //fprintf(stderr,"check vins\n"); + for (i=0; iInvalid("illegal normal vini"); + } + } + //fprintf(stderr,"check amounts\n"); + if ( DiceExactAmounts(cp,eval,tx,1,10000) == false ) + { + fprintf(stderr,"diceget invalid amount\n"); + return false; + } + else + { + preventCCvouts = 1; + if ( IsDicevout(cp,tx,0) != 0 ) + { + preventCCvouts++; + i = 1; + } else i = 0; + if ( tx.vout[i].nValue != COIN ) + return eval->Invalid("invalid dice output"); + retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts); + if ( retval != 0 ) + fprintf(stderr,"diceget validated\n"); + else fprintf(stderr,"diceget invalid\n"); + return(retval); + } + } +} +// end of consensus code + +// helper functions for rpc calls in rpcwallet.cpp + +uint64_t AddDiceInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) +{ + char coinaddr[64]; uint64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t n = 0; + std::vector > unspentOutputs; + GetCCaddress(cp,coinaddr,pk); + SetCCunspents(unspentOutputs,coinaddr); + for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) + { + txid = it->first.txhash; + if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) + { + if ( (nValue= IsDicevout(cp,vintx,(int32_t)it->first.index)) > 0 ) + { + if ( total != 0 && maxinputs != 0 ) + mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript())); + nValue = it->second.satoshis; + totalinputs += nValue; + n++; + if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) ) + break; + } + } + } + return(totalinputs); +} + +std::string DiceBet(uint64_t txfee,uint64_t amount,uint64_t odds) +{ + CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t inputs,CCchange=0,nValue=COIN; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_DICE); + if ( txfee == 0 ) + txfee = 10000; + dicepk = GetUnspendable(cp,0); + mypk = pubkey2pk(Mypubkey()); + if ( (inputs= AddDiceInputs(cp,mtx,dicepk,nValue+txfee,60)) > 0 ) + { + if ( inputs > nValue ) + CCchange = (inputs - nValue - txfee); + if ( CCchange != 0 ) + mtx.vout.push_back(MakeCC1vout(EVAL_DICE,CCchange,dicepk)); + mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } else fprintf(stderr,"cant find dice inputs\n"); + return(0); +} + +std::string DiceFund(uint64_t txfee,uint64_t funds) +{ + CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_DICE); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + dicepk = GetUnspendable(cp,0); + if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 ) + { + mtx.vout.push_back(MakeCC1vout(EVAL_DICE,funds,dicepk)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } + return(0); +} + + diff --git a/src/cc/eval.cpp b/src/cc/eval.cpp index 43b215748..f157b6197 100644 --- a/src/cc/eval.cpp +++ b/src/cc/eval.cpp @@ -21,6 +21,7 @@ #include "script/cc.h" #include "cc/eval.h" #include "cc/utils.h" +#include "cc/CCinclude.h" #include "main.h" #include "chain.h" #include "core_io.h" @@ -28,12 +29,11 @@ Eval* EVAL_TEST = 0; - +struct CCcontract_info CCinfos[0x100]; bool RunCCEval(const CC *cond, const CTransaction &tx, unsigned int nIn) { EvalRef eval; - bool out = eval->Dispatch(cond, tx, nIn); //fprintf(stderr,"out %d vs %d isValid\n",(int32_t)out,(int32_t)eval->state.IsValid()); assert(eval->state.IsValid() == out); @@ -56,10 +56,17 @@ bool RunCCEval(const CC *cond, const CTransaction &tx, unsigned int nIn) */ bool Eval::Dispatch(const CC *cond, const CTransaction &txTo, unsigned int nIn) { + struct CCcontract_info *cp; if (cond->codeLength == 0) return Invalid("empty-eval"); uint8_t ecode = cond->code[0]; + cp = &CCinfos[(int32_t)ecode]; + if ( cp->didinit == 0 ) + { + CCinit(cp,ecode); + cp->didinit = 1; + } std::vector vparams(cond->code+1, cond->code+cond->codeLength); switch ( ecode ) { @@ -71,16 +78,8 @@ bool Eval::Dispatch(const CC *cond, const CTransaction &txTo, unsigned int nIn) return ImportCoin(vparams, txTo, nIn); break; - case EVAL_ASSETS: - return ProcessAssets(this, vparams, txTo, nIn); - break; - - case EVAL_FAUCET: - return ProcessFaucet(this, vparams, txTo, nIn); - break; - - case EVAL_REWARDS: - return ProcessRewards(this, vparams, txTo, nIn); + default: + return(ProcessCC(cp,this, vparams, txTo, nIn)); break; } return Invalid("invalid-code, dont forget to add EVAL_NEWCC to Eval::Dispatch"); diff --git a/src/cc/eval.h b/src/cc/eval.h index d8ca4312d..5454cf449 100644 --- a/src/cc/eval.h +++ b/src/cc/eval.h @@ -41,7 +41,11 @@ EVAL(EVAL_IMPORTCOIN, 0xe2) \ EVAL(EVAL_ASSETS, 0xe3) \ EVAL(EVAL_FAUCET, 0xe4) \ - EVAL(EVAL_REWARDS, 0xe5) + EVAL(EVAL_REWARDS, 0xe5) \ + EVAL(EVAL_DICE, 0xe6) \ + EVAL(EVAL_PONZI, 0xe7) \ + EVAL(EVAL_AUCTION, 0xe8) \ + EVAL(EVAL_LOTTO, 0xe9) typedef uint8_t EvalCode; @@ -268,9 +272,8 @@ typedef std::pair TxProof; uint256 GetMerkleRoot(const std::vector& vLeaves); -bool ProcessAssets(Eval* eval, std::vector paramsNull, const CTransaction &tx, unsigned int nIn); -bool ProcessFaucet(Eval* eval, std::vector paramsNull, const CTransaction &tx, unsigned int nIn); -bool ProcessRewards(Eval* eval, std::vector paramsNull, const CTransaction &tx, unsigned int nIn); +struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode); +bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector paramsNull, const CTransaction &tx, unsigned int nIn); #endif /* CC_EVAL_H */ diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index 15686dc01..00b01d76f 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -26,18 +26,20 @@ To implement this, we can simply make any faucet vout fund the faucet. Then we can limit the number of confirmed utxo by combining faucet outputs and then only using utxo which are confirmed. This combined with a vout size limit will drastically limit the funds that can be withdrawn from the faucet. */ -uint64_t IsFaucetvout(const CTransaction& tx,int32_t v) +// start of consensus code + +uint64_t IsFaucetvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v) { char destaddr[64]; if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,FaucetCCaddr) == 0 ) + if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 ) return(tx.vout[v].nValue); } return(0); } -bool FaucetExactAmounts(Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee) +bool FaucetExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee) { static uint256 zerohash; CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; uint64_t inputs=0,outputs=0,assetoshis; @@ -46,7 +48,7 @@ bool FaucetExactAmounts(Eval* eval,const CTransaction &tx,int32_t minage,uint64_ for (i=0; iismyvin)(tx.vin[i].scriptSig) != 0 ) { //fprintf(stderr,"vini.%d check mempool\n",i); if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 ) @@ -56,7 +58,7 @@ bool FaucetExactAmounts(Eval* eval,const CTransaction &tx,int32_t minage,uint64_ //fprintf(stderr,"vini.%d check hash and vout\n",i); if ( hashBlock == zerohash ) return eval->Invalid("cant faucet from mempool"); - if ( (assetoshis= IsFaucetvout(vinTx,tx.vin[i].prevout.n)) != 0 ) + if ( (assetoshis= IsFaucetvout(cp,vinTx,tx.vin[i].prevout.n)) != 0 ) inputs += assetoshis; } } @@ -64,7 +66,7 @@ bool FaucetExactAmounts(Eval* eval,const CTransaction &tx,int32_t minage,uint64_ for (i=0; iInvalid("no vouts"); else { - fprintf(stderr,"check vins\n"); + //fprintf(stderr,"check vins\n"); for (i=0; iInvalid("illegal normal vini"); + } } - fprintf(stderr,"check amounts\n"); - if ( FaucetExactAmounts(eval,tx,1,10000) == false ) + //fprintf(stderr,"check amounts\n"); + if ( FaucetExactAmounts(cp,eval,tx,1,10000) == false ) + { + fprintf(stderr,"faucetget invalid amount\n"); return false; + } else { - fprintf(stderr,"check rest\n"); preventCCvouts = 1; - if ( IsFaucetvout(tx,0) != 0 ) + if ( IsFaucetvout(cp,tx,0) != 0 ) { preventCCvouts++; i = 1; } else i = 0; if ( tx.vout[i].nValue != COIN ) return eval->Invalid("invalid faucet output"); - fprintf(stderr,"check CC\n"); - return(PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts)); + retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts); + if ( retval != 0 ) + fprintf(stderr,"faucetget validated\n"); + else fprintf(stderr,"faucetget invalid\n"); + return(retval); } } } +// end of consensus code -bool ProcessFaucet(Eval* eval, std::vector paramsNull,const CTransaction &ctx, unsigned int nIn) -{ - static uint256 prevtxid; uint256 txid; - txid = ctx.GetHash(); - if ( txid == prevtxid ) - return(true); - fprintf(stderr,"start faucet validate\n"); - if ( paramsNull.size() != 0 ) // Don't expect params - return eval->Invalid("Cannot have params"); - else if ( ctx.vout.size() == 0 ) - return eval->Invalid("no-vouts"); - if ( FaucetValidate(eval,ctx) != 0 ) - { - prevtxid = txid; - fprintf(stderr,"faucet validated\n"); - return(true); - } - fprintf(stderr,"faucet validate failed\n"); - return(false); -} +// helper functions for rpc calls in rpcwallet.cpp -uint64_t AddFaucetInputs(CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) +uint64_t AddFaucetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { char coinaddr[64]; uint64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t n = 0; std::vector > unspentOutputs; - GetCCaddress(EVAL_FAUCET,coinaddr,pk); + GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { - if ( (nValue= IsFaucetvout(vintx,(int32_t)it->first.index)) > 0 ) + if ( (nValue= IsFaucetvout(cp,vintx,(int32_t)it->first.index)) > 0 ) { if ( total != 0 && maxinputs != 0 ) mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript())); @@ -158,37 +150,40 @@ uint64_t AddFaucetInputs(CMutableTransaction &mtx,CPubKey pk,uint64_t total,int3 return(totalinputs); } -std::string FaucetFund(uint64_t txfee,uint64_t funds) -{ - CMutableTransaction mtx; CPubKey mypk,faucetpk; CScript opret; - if ( txfee == 0 ) - txfee = 10000; - mypk = pubkey2pk(Mypubkey()); - faucetpk = GetUnspendable(EVAL_FAUCET,0); - if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 ) - { - mtx.vout.push_back(MakeCC1vout(EVAL_FAUCET,funds,faucetpk)); - return(FinalizeCCTx(EVAL_FAUCET,mtx,mypk,txfee,opret)); - } - return(0); -} - std::string FaucetGet(uint64_t txfee) { - CMutableTransaction mtx; CPubKey mypk,faucetpk; CScript opret; uint64_t inputs,CCchange=0,nValue=COIN; + CMutableTransaction mtx; CPubKey mypk,faucetpk; CScript opret; uint64_t inputs,CCchange=0,nValue=COIN; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_FAUCET); if ( txfee == 0 ) txfee = 10000; - faucetpk = GetUnspendable(EVAL_FAUCET,0); + faucetpk = GetUnspendable(cp,0); mypk = pubkey2pk(Mypubkey()); - if ( (inputs= AddFaucetInputs(mtx,faucetpk,nValue+txfee,60)) > 0 ) + if ( (inputs= AddFaucetInputs(cp,mtx,faucetpk,nValue+txfee,60)) > 0 ) { if ( inputs > nValue ) CCchange = (inputs - nValue - txfee); if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_FAUCET,CCchange,faucetpk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(EVAL_FAUCET,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"cant find faucet inputs\n"); return(0); } +std::string FaucetFund(uint64_t txfee,uint64_t funds) +{ + CMutableTransaction mtx; CPubKey mypk,faucetpk; CScript opret; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_FAUCET); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + faucetpk = GetUnspendable(cp,0); + if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 ) + { + mtx.vout.push_back(MakeCC1vout(EVAL_FAUCET,funds,faucetpk)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } + return(0); +} + + diff --git a/src/cc/lotto.cpp b/src/cc/lotto.cpp new file mode 100644 index 000000000..af6222b5d --- /dev/null +++ b/src/cc/lotto.cpp @@ -0,0 +1,182 @@ +/****************************************************************************** + * Copyright © 2014-2018 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 "CClotto.h" +#include "../txmempool.h" + +/* +*/ + +// start of consensus code + +uint64_t IsLottovout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v) +{ + char destaddr[64]; + if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 ) + { + if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 ) + return(tx.vout[v].nValue); + } + return(0); +} + +bool LottoExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee) +{ + static uint256 zerohash; + CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; uint64_t inputs=0,outputs=0,assetoshis; + numvins = tx.vin.size(); + numvouts = tx.vout.size(); + for (i=0; iismyvin)(tx.vin[i].scriptSig) != 0 ) + { + //fprintf(stderr,"vini.%d check mempool\n",i); + if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 ) + return eval->Invalid("cant find vinTx"); + else + { + //fprintf(stderr,"vini.%d check hash and vout\n",i); + if ( hashBlock == zerohash ) + return eval->Invalid("cant Lotto from mempool"); + if ( (assetoshis= IsLottovout(cp,vinTx,tx.vin[i].prevout.n)) != 0 ) + inputs += assetoshis; + } + } + } + for (i=0; iInvalid("mismatched inputs != outputs + COIN + txfee"); + } + else return(true); +} + +bool LottoValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) +{ + int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; bool retval; + numvins = tx.vin.size(); + numvouts = tx.vout.size(); + preventCCvins = preventCCvouts = -1; + if ( numvouts < 1 ) + return eval->Invalid("no vouts"); + else + { + //fprintf(stderr,"check vins\n"); + for (i=0; iInvalid("illegal normal vini"); + } + } + //fprintf(stderr,"check amounts\n"); + if ( LottoExactAmounts(cp,eval,tx,1,10000) == false ) + { + fprintf(stderr,"Lottoget invalid amount\n"); + return false; + } + else + { + preventCCvouts = 1; + if ( IsLottovout(cp,tx,0) != 0 ) + { + preventCCvouts++; + i = 1; + } else i = 0; + if ( tx.vout[i].nValue != COIN ) + return eval->Invalid("invalid Lotto output"); + retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts); + if ( retval != 0 ) + fprintf(stderr,"Lottoget validated\n"); + else fprintf(stderr,"Lottoget invalid\n"); + return(retval); + } + } +} +// end of consensus code + +// helper functions for rpc calls in rpcwallet.cpp + +uint64_t AddLottoInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) +{ + char coinaddr[64]; uint64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t n = 0; + std::vector > unspentOutputs; + GetCCaddress(cp,coinaddr,pk); + SetCCunspents(unspentOutputs,coinaddr); + for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) + { + txid = it->first.txhash; + if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) + { + if ( (nValue= IsLottovout(cp,vintx,(int32_t)it->first.index)) > 0 ) + { + if ( total != 0 && maxinputs != 0 ) + mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript())); + nValue = it->second.satoshis; + totalinputs += nValue; + n++; + if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) ) + break; + } + } + } + return(totalinputs); +} + +std::string LottoTicket(uint64_t txfee,uint64_t numtickets) +{ + CMutableTransaction mtx; CPubKey mypk,Lottopk; CScript opret; uint64_t inputs,CCchange=0,nValue=COIN; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_LOTTO); + if ( txfee == 0 ) + txfee = 10000; + Lottopk = GetUnspendable(cp,0); + mypk = pubkey2pk(Mypubkey()); + if ( (inputs= AddLottoInputs(cp,mtx,Lottopk,nValue+txfee,60)) > 0 ) + { + if ( inputs > nValue ) + CCchange = (inputs - nValue - txfee); + if ( CCchange != 0 ) + mtx.vout.push_back(MakeCC1vout(EVAL_LOTTO,CCchange,Lottopk)); + mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } else fprintf(stderr,"cant find Lotto inputs\n"); + return(0); +} + +std::string LottoWinner(uint64_t txfee) +{ + CMutableTransaction mtx; CPubKey mypk,Lottopk; uint64_t winnings = 0; CScript opret; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_LOTTO); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + Lottopk = GetUnspendable(cp,0); + if ( AddNormalinputs(mtx,mypk,txfee,64) > 0 ) + { + mtx.vout.push_back(MakeCC1vout(EVAL_LOTTO,winnings,Lottopk)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } + return(0); +} + + diff --git a/src/cc/ponzi.cpp b/src/cc/ponzi.cpp new file mode 100644 index 000000000..81021209e --- /dev/null +++ b/src/cc/ponzi.cpp @@ -0,0 +1,182 @@ +/****************************************************************************** + * Copyright © 2014-2018 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 "CCponzi.h" +#include "../txmempool.h" + +/* +*/ + +// start of consensus code + +uint64_t IsPonzivout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v) +{ + char destaddr[64]; + if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 ) + { + if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 ) + return(tx.vout[v].nValue); + } + return(0); +} + +bool PonziExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee) +{ + static uint256 zerohash; + CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; uint64_t inputs=0,outputs=0,assetoshis; + numvins = tx.vin.size(); + numvouts = tx.vout.size(); + for (i=0; iismyvin)(tx.vin[i].scriptSig) != 0 ) + { + //fprintf(stderr,"vini.%d check mempool\n",i); + if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 ) + return eval->Invalid("cant find vinTx"); + else + { + //fprintf(stderr,"vini.%d check hash and vout\n",i); + if ( hashBlock == zerohash ) + return eval->Invalid("cant ponzi from mempool"); + if ( (assetoshis= IsPonzivout(cp,vinTx,tx.vin[i].prevout.n)) != 0 ) + inputs += assetoshis; + } + } + } + for (i=0; iInvalid("mismatched inputs != outputs + COIN + txfee"); + } + else return(true); +} + +bool PonziValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) +{ + int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; bool retval; + numvins = tx.vin.size(); + numvouts = tx.vout.size(); + preventCCvins = preventCCvouts = -1; + if ( numvouts < 1 ) + return eval->Invalid("no vouts"); + else + { + //fprintf(stderr,"check vins\n"); + for (i=0; iInvalid("illegal normal vini"); + } + } + //fprintf(stderr,"check amounts\n"); + if ( PonziExactAmounts(cp,eval,tx,1,10000) == false ) + { + fprintf(stderr,"ponziget invalid amount\n"); + return false; + } + else + { + preventCCvouts = 1; + if ( IsPonzivout(cp,tx,0) != 0 ) + { + preventCCvouts++; + i = 1; + } else i = 0; + if ( tx.vout[i].nValue != COIN ) + return eval->Invalid("invalid ponzi output"); + retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts); + if ( retval != 0 ) + fprintf(stderr,"ponziget validated\n"); + else fprintf(stderr,"ponziget invalid\n"); + return(retval); + } + } +} +// end of consensus code + +// helper functions for rpc calls in rpcwallet.cpp + +uint64_t AddPonziInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) +{ + char coinaddr[64]; uint64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t n = 0; + std::vector > unspentOutputs; + GetCCaddress(cp,coinaddr,pk); + SetCCunspents(unspentOutputs,coinaddr); + for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) + { + txid = it->first.txhash; + if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) + { + if ( (nValue= IsPonzivout(cp,vintx,(int32_t)it->first.index)) > 0 ) + { + if ( total != 0 && maxinputs != 0 ) + mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript())); + nValue = it->second.satoshis; + totalinputs += nValue; + n++; + if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) ) + break; + } + } + } + return(totalinputs); +} + +std::string PonziBuy(uint64_t txfee,uint64_t amount) +{ + CMutableTransaction mtx; CPubKey mypk,ponzipk; CScript opret; uint64_t inputs,CCchange=0,nValue=COIN; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_PONZI); + if ( txfee == 0 ) + txfee = 10000; + ponzipk = GetUnspendable(cp,0); + mypk = pubkey2pk(Mypubkey()); + if ( (inputs= AddPonziInputs(cp,mtx,ponzipk,nValue+txfee,60)) > 0 ) + { + if ( inputs > nValue ) + CCchange = (inputs - nValue - txfee); + if ( CCchange != 0 ) + mtx.vout.push_back(MakeCC1vout(EVAL_PONZI,CCchange,ponzipk)); + mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } else fprintf(stderr,"cant find ponzi inputs\n"); + return(0); +} + +std::string PonziClaim(uint64_t txfee) +{ + CMutableTransaction mtx; CPubKey mypk,ponzipk; uint64_t funds = 0; CScript opret; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_PONZI); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + ponzipk = GetUnspendable(cp,0); + if ( AddNormalinputs(mtx,mypk,txfee,64) > 0 ) + { + mtx.vout.push_back(MakeCC1vout(EVAL_PONZI,funds,ponzipk)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } + return(0); +} + + diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 5253035ac..53ade6494 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -16,48 +16,49 @@ #include "CCinclude.h" /* + The rewards CC contract is initially for OOT, which needs this functionality. However, many of the attributes can be parameterized to allow different rewards programs to run. Multiple rewards plans could even run on the same blockchain, though the user would need to choose which one to lock funds into. + + At the high level, the user would lock funds for some amount of time and at the end of it, would get all the locked funds back with an additional reward. So there needs to be a lock funds and unlock funds ability. Additionally, the rewards need to come from somewhere, so similar to the faucet, there would be a way to fund the reward. + + Additional requirements are for the user to be able to lock funds via SPV. This requirement in turns forces the creation of a way for anybody to be able to unlock the funds as that operation requires a native daemon running and cant be done over SPV. The idea is to allow anybody to run a script that would unlock all funds that are matured. As far as the user is concerned, he locks his funds via SPV and after some time it comes back with an extra reward. + + In reality, the funds are locked into a CC address that is unspendable, except for some special conditions and it needs to come back to the address that funded the lock. In order to implement this, several things are clear. + + 1) each locked CC utxo needs to be linked to a specific rewards plan + 2) each locked CC utxo needs to know the only address that it can be unlocked into + 3) SPV requirement means the lock transaction needs to be able to be created without any CC signing + + The opreturn will be used to store the name of the rewards plan and all funding and locked funds with the same plan will use the same pool of rewards. plan names will be limited to 8 chars and encoded into a uint64_t. + + The initial funding transaction will have all the parameters for the rewards plan encoded in the vouts. Additional fundings will just increase the available CC utxo for the rewards. + + Locks wont have any CC vins, but will send to the RewardsCCaddress, with the plan stringbits in the opreturn. vout1 will have the unlock address and no other destination is valid. + + Unlock does a CC spend to the vout1 address */ -extern const char *RewardsCCaddr; -extern char RewardsCChexstr[67]; -bool IsRewardsInput(CScript const& scriptSig); - -uint64_t RewardsCalc(uint64_t claim,uint256 txid) +uint64_t RewardsCalc(uint64_t claim,uint256 txid) // min/max time, mindeposit and rate { uint64_t reward = 0; + // get start time, get current time + // if elapsed < mintime -> return 0 + // if elapsed > maxtime, elapsed = maxtime + // calc reward return(reward); } -/*CC *MakeRewardsCond(CPubKey pk) -{ - std::vector pks; uint8_t evalcode = EVAL_REWARDS; - pks.push_back(CCNewSecp256k1(pk)); - CC *rewardsCC = CCNewEval(E_MARSHAL(ss << evalcode)); - CC *Sig = CCNewThreshold(1, pks); - return CCNewThreshold(2, {rewardsCC, Sig}); -} - -CTxOut MakeRewardsVout(CAmount nValue,CPubKey pk) -{ - CTxOut vout; - CC *payoutCond = MakeCCcond1(EVAL_REWARDS,pk); - vout = CTxOut(nValue,CCPubKey(payoutCond)); - cc_free(payoutCond); - return(vout); -}*/ - -uint64_t IsRewardsvout(const CTransaction& tx,int32_t v) +uint64_t IsRewardsvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v) { char destaddr[64]; if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,RewardsCCaddr) == 0 ) + if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 ) return(tx.vout[v].nValue); } return(0); } -bool RewardsExactAmounts(Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee) +bool RewardsExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee) { static uint256 zerohash; CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; uint64_t inputs=0,outputs=0,assetoshis; @@ -65,7 +66,7 @@ bool RewardsExactAmounts(Eval* eval,const CTransaction &tx,int32_t minage,uint64 numvouts = tx.vout.size(); for (i=0; iismyvin)(tx.vin[i].scriptSig) != 0 ) { if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 ) return eval->Invalid("always should find vin, but didnt"); @@ -73,7 +74,7 @@ bool RewardsExactAmounts(Eval* eval,const CTransaction &tx,int32_t minage,uint64 { if ( hashBlock == zerohash ) return eval->Invalid("cant rewards from mempool"); - if ( (assetoshis= IsRewardsvout(vinTx,tx.vin[i].prevout.n)) != 0 ) + if ( (assetoshis= IsRewardsvout(cp,vinTx,tx.vin[i].prevout.n)) != 0 ) inputs += assetoshis; } } @@ -81,7 +82,7 @@ bool RewardsExactAmounts(Eval* eval,const CTransaction &tx,int32_t minage,uint64 for (i=0; iInvalid("no vouts"); else { + // follow rules for (i=0; iInvalid("illegal normal vini"); } - if ( RewardsExactAmounts(eval,tx,1,10000) == false ) + if ( RewardsExactAmounts(cp,eval,tx,1,10000) == false ) return false; else { preventCCvouts = 1; - if ( IsRewardsvout(tx,0) != 0 ) + if ( IsRewardsvout(cp,tx,0) != 0 ) { preventCCvouts++; i = 1; @@ -125,44 +127,22 @@ bool RewardsValidate(Eval* eval,const CTransaction &tx) return(true); } -bool ProcessRewards(Eval* eval, std::vector paramsNull,const CTransaction &ctx, unsigned int nIn) +uint64_t AddRewardsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { - static uint256 prevtxid; uint256 txid; - txid = ctx.GetHash(); - if ( txid == prevtxid ) - return(true); - fprintf(stderr,"ProcessRewards\n"); - if ( paramsNull.size() != 0 ) // Don't expect params - return eval->Invalid("Cannot have params"); - else if ( ctx.vout.size() == 0 ) - return eval->Invalid("no-vouts"); - if ( RewardsValidate(eval,ctx) != 0 ) - { - fprintf(stderr,"ProcessRewards valid\n"); - prevtxid = txid; - return(true); - } - fprintf(stderr,"ProcessRewards failed\n"); - return(false); -} - -uint64_t AddRewardsInputs(CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) -{ - char coinaddr[64]; uint64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t n = 0; + char coinaddr[64]; uint64_t nValue,totalinputs = 0; uint256 txid,hashBlock; CTransaction vintx; int32_t n = 0; std::vector > unspentOutputs; - GetCCaddress(EVAL_REWARDS,coinaddr,pk); + GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { - if ( (nValue= IsRewardsvout(vintx,(int32_t)it->first.index)) > 0 ) + if ( (nValue= IsRewardsvout(cp,vintx,(int32_t)it->first.index)) > 0 ) { if ( total != 0 && maxinputs != 0 ) mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript())); - nValue = it->second.satoshis; - totalinputs += nValue; + totalinputs += it->second.satoshis; n++; if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) ) break; @@ -172,63 +152,91 @@ uint64_t AddRewardsInputs(CMutableTransaction &mtx,CPubKey pk,uint64_t total,int return(totalinputs); } -// 0.834% every 60 days, min 100, capped at 0.834% - -std::string RewardsFund(uint64_t txfee,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) +uint64_t RewardsPlanFunds(uint64_t &refsbits,struct CCcontract_info *cp,CPubKey &pk,char *planstr) { - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; - if ( txfee == 0 ) - txfee = 10000; - mypk = pubkey2pk(Mypubkey()); - rewardspk = GetUnspendable(EVAL_REWARDS,0); - if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) + char coinaddr[64]; uint64_t sbits,nValue,totalinputs = 0; uint256 hashBlock; CTransaction vintx; + std::vector > unspentOutputs; + if ( planstr == 0 || planstr[0] == 0 || strlen(planstr) > 8 ) + return(0); + refsbits = stringbits(planstr); + GetCCaddress(cp,coinaddr,pk); + SetCCunspents(unspentOutputs,coinaddr); + for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { - mtx.vout.push_back(MakeCC1vout(EVAL_REWARDS,funds,rewardspk)); - mtx.vout.push_back(CTxOut(APR,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); - mtx.vout.push_back(CTxOut(minseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); - mtx.vout.push_back(CTxOut(maxseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); - mtx.vout.push_back(CTxOut(mindeposit,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); - return(FinalizeCCTx(EVAL_REWARDS,mtx,mypk,txfee,opret)); - } - return(0); -} - -std::string RewardsLock(uint64_t txfee,uint64_t amount) -{ - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; - if ( txfee == 0 ) - txfee = 10000; - rewardspk = GetUnspendable(EVAL_REWARDS,0); - mypk = pubkey2pk(Mypubkey()); - if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) - { - mtx.vout.push_back(MakeCC1vout(EVAL_REWARDS,amount,rewardspk)); - // specify destination pubkey, funding txid - //opret = ;// - return(FinalizeCCTx(EVAL_REWARDS,mtx,mypk,txfee,opret)); - } else fprintf(stderr,"cant find rewards inputs\n"); - return(0); -} - -std::string RewardsUnlock(uint64_t txfee) -{ - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t reward,claim,inputs,CCchange=0; - if ( txfee == 0 ) - txfee = 10000; - rewardspk = GetUnspendable(EVAL_REWARDS,0); - mypk = pubkey2pk(Mypubkey()); - if ( (claim= AddRewardsInputs(mtx,mypk,(1LL << 30),1)) > 0 && (reward= RewardsCalc(claim,mtx.vin[0].prevout.hash)) > txfee ) - { - if ( (inputs= AddRewardsInputs(mtx,mypk,reward+txfee,30)) > 0 ) + if ( GetTransaction(it->first.txhash,vintx,hashBlock,false) != 0 ) { - if ( inputs > (reward+txfee) ) - CCchange = (inputs - reward - txfee); + if ( (nValue= IsRewardsvout(cp,vintx,(int32_t)it->first.index)) > 0 ) + { + totalinputs += nValue; + } + } + } + return(totalinputs); +} + +std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid) +{ + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t reward,amount,inputs,CCchange=0; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_REWARDS); + if ( txfee == 0 ) + txfee = 10000; + rewardspk = GetUnspendable(cp,0); + mypk = pubkey2pk(Mypubkey()); + if ( (amount= AddRewardsInputs(cp,mtx,mypk,(1LL << 30),1)) > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash)) > txfee ) + { + if ( (inputs= AddRewardsInputs(cp,mtx,mypk,reward+amount+txfee,30)) > 0 ) + { + if ( inputs > (amount + reward + 2*txfee) ) + CCchange = (inputs - amount - reward - txfee); if ( CCchange != 0 ) - mtx.vout.push_back(MakeCC1vout(EVAL_REWARDS,CCchange,rewardspk)); - mtx.vout.push_back(CTxOut(claim+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(EVAL_REWARDS,mtx,mypk,txfee,opret)); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk)); + mtx.vout.push_back(CTxOut(amount+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + opret << OP_RETURN << E_MARSHAL(ss << cp->evalcode << 'U'); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); } } else fprintf(stderr,"cant find rewards inputs\n"); return(0); } +std::string RewardsFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) +{ + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_REWARDS); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + rewardspk = GetUnspendable(cp,0); + if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) + { + mtx.vout.push_back(MakeCC1vout(cp->evalcode,funds,rewardspk)); + mtx.vout.push_back(CTxOut(APR,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); + mtx.vout.push_back(CTxOut(minseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); + mtx.vout.push_back(CTxOut(maxseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); + mtx.vout.push_back(CTxOut(mindeposit,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); + opret << OP_RETURN << E_MARSHAL(ss << cp->evalcode << 'F' << sbits); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } + return(0); +} + +std::string RewardsLock(uint64_t txfee,char *planstr,uint64_t amount) +{ + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_REWARDS); + mypk = pubkey2pk(Mypubkey()); + rewardspk = GetUnspendable(cp,0); + if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,planstr)) >= amount ) + { + if ( txfee == 0 ) + txfee = 10000; + if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 ) + { + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,rewardspk)); + opret << OP_RETURN << E_MARSHAL(ss << cp->evalcode << 'L' << sbits); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } else fprintf(stderr,"cant find rewards inputs\n"); + } + return(0); +} + + diff --git a/src/miner.cpp b/src/miner.cpp index 4a2dda285..e377f69c4 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -494,7 +494,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn,int32_t gpucount) } } } - if ( pindexPrev != 0 && ASSETCHAINS_STAKED == 0 ) + if ( pindexPrev != 0 && ASSETCHAINS_STAKED == 0 && (ASSETCHAINS_SYMBOL[0] != 0 || IS_KOMODO_NOTARY == 0 || My_notaryid < 0) ) { CValidationState state; //fprintf(stderr,"check validity\n"); diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 4d766d5ae..afcb5cc59 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -344,6 +344,15 @@ static const CRPCCommand vRPCCommands[] = #ifdef ENABLE_WALLET { "rawtransactions", "fundrawtransaction", &fundrawtransaction, false }, #endif + /* auction */ + { "auction", "auctionaddress", &auctionaddress, true }, + + /* lotto */ + { "lotto", "lottoaddress", &lottoaddress, true }, + + /* ponzi */ + { "ponzi", "ponziaddress", &ponziaddress, true }, + /* rewards */ { "rewards", "rewardsfund", &rewardsfund, true }, { "rewards", "rewardslock", &rewardslock, true }, @@ -354,8 +363,13 @@ static const CRPCCommand vRPCCommands[] = { "faucet", "faucetfund", &faucetfund, true }, { "faucet", "faucetget", &faucetget, true }, { "faucet", "faucetaddress", &faucetaddress, true }, + + /* dice */ + { "dice", "dicefund", &dicefund, true }, + { "dice", "dicebet", &dicebet, true }, + { "dice", "diceaddress", &diceaddress, true }, -/* tokens */ + /* tokens */ { "tokens", "tokenorders", &tokenorders, true }, { "tokens", "tokenaddress", &tokenaddress, true }, { "tokens", "tokenbalance", &tokenbalance, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index eb9f69247..fe48bdd74 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -228,6 +228,12 @@ extern UniValue rewardsaddress(const UniValue& params, bool fHelp); extern UniValue rewardsfund(const UniValue& params, bool fHelp); extern UniValue rewardslock(const UniValue& params, bool fHelp); extern UniValue rewardsunlock(const UniValue& params, bool fHelp); +extern UniValue diceaddress(const UniValue& params, bool fHelp); +extern UniValue dicefund(const UniValue& params, bool fHelp); +extern UniValue dicebet(const UniValue& params, bool fHelp); +extern UniValue lottoaddress(const UniValue& params, bool fHelp); +extern UniValue ponziaddress(const UniValue& params, bool fHelp); +extern UniValue auctionaddress(const UniValue& params, bool fHelp); extern UniValue getnewaddress(const UniValue& params, bool fHelp); // in rpcwallet.cpp //extern UniValue getnewaddress64(const UniValue& params, bool fHelp); // in rpcwallet.cpp diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index c79226954..489976b78 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4842,59 +4842,149 @@ int32_t ensure_CCrequirements() #include "../cc/CCfaucet.h" #include "../cc/CCassets.h" -#define EVAL_REWARDS 0xe5 -std::string RewardsFund(uint64_t txfee,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit); -std::string RewardsLock(uint64_t txfee,uint64_t amount); -std::string RewardsUnlock(uint64_t txfee); +#include "../cc/CCrewards.h" +#include "../cc/CCdice.h" +#include "../cc/CCponzi.h" +#include "../cc/CCauction.h" +#include "../cc/CClotto.h" -UniValue rewardsaddress(const UniValue& params, bool fHelp) +UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector &pubkey) { - UniValue result(UniValue::VOBJ); std::vector pubkey; char destaddr[64]; - if ( fHelp || params.size() > 1 ) - throw runtime_error("rewardsaddress [pubkey]\n"); - if ( ensure_CCrequirements() < 0 ) - throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); + UniValue result(UniValue::VOBJ); ; char destaddr[64],str[64]; + // { uint8_t p[32]; Myprivkey(p); } result.push_back(Pair("result", "success")); - if ( GetCCaddress(EVAL_REWARDS,destaddr,pubkey2pk(pubkey)) != 0 ) - result.push_back(Pair("RewardsCCaddress",destaddr)); - if ( params.size() == 1 ) + sprintf(str,"%sCCaddress",name); + if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) + result.push_back(Pair(str,destaddr)); + if ( pubkey.size() == 33 ) { - pubkey = ParseHex(params[0].get_str().c_str()); - if ( GetCCaddress(EVAL_REWARDS,destaddr,pubkey2pk(pubkey)) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) result.push_back(Pair("CCaddress",destaddr)); } - if ( GetCCaddress(EVAL_REWARDS,destaddr,pubkey2pk(Mypubkey())) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(Mypubkey())) != 0 ) result.push_back(Pair("myCCaddress",destaddr)); return(result); } +UniValue lottoaddress(const UniValue& params, bool fHelp) +{ + struct CCcontract_info *cp,C; std::vector pubkey; + cp = CCinit(&C,EVAL_LOTTO); + if ( fHelp || params.size() > 1 ) + throw runtime_error("lottoaddress [pubkey]\n"); + if ( ensure_CCrequirements() < 0 ) + throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); + if ( params.size() == 1 ) + pubkey = ParseHex(params[0].get_str().c_str()); + return(CCaddress(cp,(char *)"Lotto",pubkey)); +} + +UniValue ponziaddress(const UniValue& params, bool fHelp) +{ + struct CCcontract_info *cp,C; std::vector pubkey; + cp = CCinit(&C,EVAL_PONZI); + if ( fHelp || params.size() > 1 ) + throw runtime_error("ponziaddress [pubkey]\n"); + if ( ensure_CCrequirements() < 0 ) + throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); + if ( params.size() == 1 ) + pubkey = ParseHex(params[0].get_str().c_str()); + return(CCaddress(cp,(char *)"Ponzi",pubkey)); +} + +UniValue auctionaddress(const UniValue& params, bool fHelp) +{ + struct CCcontract_info *cp,C; std::vector pubkey; + cp = CCinit(&C,EVAL_AUCTION); + if ( fHelp || params.size() > 1 ) + throw runtime_error("auctionaddress [pubkey]\n"); + if ( ensure_CCrequirements() < 0 ) + throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); + if ( params.size() == 1 ) + pubkey = ParseHex(params[0].get_str().c_str()); + return(CCaddress(cp,(char *)"Auction",pubkey)); +} + +UniValue diceaddress(const UniValue& params, bool fHelp) +{ + struct CCcontract_info *cp,C; std::vector pubkey; + cp = CCinit(&C,EVAL_DICE); + if ( fHelp || params.size() > 1 ) + throw runtime_error("diceaddress [pubkey]\n"); + if ( ensure_CCrequirements() < 0 ) + throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); + if ( params.size() == 1 ) + pubkey = ParseHex(params[0].get_str().c_str()); + return(CCaddress(cp,(char *)"Dice",pubkey)); +} + +UniValue faucetaddress(const UniValue& params, bool fHelp) +{ + struct CCcontract_info *cp,C; std::vector pubkey; + cp = CCinit(&C,EVAL_FAUCET); + if ( fHelp || params.size() > 1 ) + throw runtime_error("faucetaddress [pubkey]\n"); + if ( ensure_CCrequirements() < 0 ) + throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); + if ( params.size() == 1 ) + pubkey = ParseHex(params[0].get_str().c_str()); + return(CCaddress(cp,(char *)"Faucet",pubkey)); +} + +UniValue rewardsaddress(const UniValue& params, bool fHelp) +{ + struct CCcontract_info *cp,C; std::vector pubkey; + cp = CCinit(&C,EVAL_REWARDS); + if ( fHelp || params.size() > 1 ) + throw runtime_error("rewardsaddress [pubkey]\n"); + if ( ensure_CCrequirements() < 0 ) + throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); + if ( params.size() == 1 ) + pubkey = ParseHex(params[0].get_str().c_str()); + return(CCaddress(cp,(char *)"Rewards",pubkey)); +} + +UniValue tokenaddress(const UniValue& params, bool fHelp) +{ + struct CCcontract_info *cp,C; std::vector pubkey; + cp = CCinit(&C,EVAL_ASSETS); + if ( fHelp || params.size() > 1 ) + throw runtime_error("tokenaddress [pubkey]\n"); + if ( ensure_CCrequirements() < 0 ) + throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); + if ( params.size() == 1 ) + pubkey = ParseHex(params[0].get_str().c_str()); + return(CCaddress(cp,(char *)"Assets",pubkey)); +} + UniValue rewardsfund(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); uint64_t funds,APR,minseconds,maxseconds,mindeposit; std::string hex; - if ( fHelp || params.size() > 5 || params.size() < 1 ) - throw runtime_error("rewardsfund amount APR mindays maxdays mindeposit\n"); + UniValue result(UniValue::VOBJ); char *name; uint64_t funds,APR,minseconds,maxseconds,mindeposit; std::string hex; + if ( fHelp || params.size() > 6 || params.size() < 2 ) + throw runtime_error("rewardsfund name amount APR mindays maxdays mindeposit\n"); if ( ensure_CCrequirements() < 0 ) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); // default to OOT params APR = 5 * COIN; minseconds = maxseconds = 60 * 3600 * 24; mindeposit = 100 * COIN; - funds = atof(params[0].get_str().c_str()) * COIN; - if ( params.size() > 1 ) + name = (char *)params[1].get_str().c_str(); + funds = atof(params[1].get_str().c_str()) * COIN; + if ( params.size() > 2 ) { - APR = atof(params[1].get_str().c_str()) * COIN; - if ( params.size() > 2 ) + APR = atof(params[2].get_str().c_str()) * COIN; + if ( params.size() > 3 ) { - minseconds = atol(params[2].get_str().c_str()) * 3600 * 24; - if ( params.size() > 3 ) + minseconds = atol(params[3].get_str().c_str()) * 3600 * 24; + if ( params.size() > 4 ) { - maxseconds = atol(params[3].get_str().c_str()) * 3600 * 24; - if ( params.size() > 4 ) - mindeposit = atof(params[4].get_str().c_str()) * COIN; + maxseconds = atol(params[4].get_str().c_str()) * 3600 * 24; + if ( params.size() > 5 ) + mindeposit = atof(params[5].get_str().c_str()) * COIN; } } } - hex = RewardsFund(0,funds,APR,minseconds,maxseconds,mindeposit); + hex = RewardsFund(0,name,funds,APR,minseconds,maxseconds,mindeposit); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -4905,13 +4995,14 @@ UniValue rewardsfund(const UniValue& params, bool fHelp) UniValue rewardslock(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); uint64_t amount; std::string hex; - if ( fHelp || params.size() > 1 ) - throw runtime_error("rewardslock amount\n"); + UniValue result(UniValue::VOBJ); char *name; uint64_t amount; std::string hex; + if ( fHelp || params.size() > 2 ) + throw runtime_error("rewardslock name amount\n"); if ( ensure_CCrequirements() < 0 ) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); - amount = atof(params[0].get_str().c_str()) * COIN; - hex = RewardsLock(0,amount); + name = (char *)params[0].get_str().c_str(); + amount = atof(params[1].get_str().c_str()) * COIN; + hex = RewardsLock(0,name,amount); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -4922,12 +5013,16 @@ UniValue rewardslock(const UniValue& params, bool fHelp) UniValue rewardsunlock(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); std::string hex; - if ( fHelp || params.size() > 0 ) - throw runtime_error("rewardsunlock [txid]\n"); + UniValue result(UniValue::VOBJ); std::string hex; char *name; uint256 txid; + if ( fHelp || params.size() > 2 ) + throw runtime_error("rewardsunlock name [txid]\n"); if ( ensure_CCrequirements() < 0 ) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); - hex = RewardsUnlock(0); + name = (char *)params[0].get_str().c_str(); + if ( params.size() > 1 ) + txid = Parseuint256((char *)params[1].get_str().c_str()); + else memset(&txid,0,sizeof(txid)); + hex = RewardsUnlock(0,name,txid); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -4936,27 +5031,6 @@ UniValue rewardsunlock(const UniValue& params, bool fHelp) return(result); } -UniValue faucetaddress(const UniValue& params, bool fHelp) -{ - UniValue result(UniValue::VOBJ); std::vector pubkey; char destaddr[64]; - if ( fHelp || params.size() > 1 ) - throw runtime_error("faucetaddress [pubkey]\n"); - if ( ensure_CCrequirements() < 0 ) - throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); - result.push_back(Pair("result", "success")); - if ( GetCCaddress(EVAL_FAUCET,destaddr,pubkey2pk(pubkey)) != 0 ) - result.push_back(Pair("FaucetCCaddress",destaddr)); - if ( params.size() == 1 ) - { - pubkey = ParseHex(params[0].get_str().c_str()); - if ( GetCCaddress(EVAL_FAUCET,destaddr,pubkey2pk(pubkey)) != 0 ) - result.push_back(Pair("CCaddress",destaddr)); - } - if ( GetCCaddress(EVAL_FAUCET,destaddr,pubkey2pk(Mypubkey())) != 0 ) - result.push_back(Pair("myCCaddress",destaddr)); - return(result); -} - UniValue faucetfund(const UniValue& params, bool fHelp) { UniValue result(UniValue::VOBJ); uint64_t funds; std::string hex; @@ -4990,6 +5064,43 @@ UniValue faucetget(const UniValue& params, bool fHelp) return(result); } +UniValue dicefund(const UniValue& params, bool fHelp) +{ + UniValue result(UniValue::VOBJ); uint64_t funds; std::string hex; + if ( fHelp || params.size() > 1 ) + throw runtime_error("faucetfund amount\n"); + if ( ensure_CCrequirements() < 0 ) + throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); + funds = atof(params[0].get_str().c_str()) * COIN; + hex = DiceFund(0,funds); + if ( hex.size() > 0 ) + { + result.push_back(Pair("result", "success")); + result.push_back(Pair("hex", hex)); + } else result.push_back(Pair("error", "couldnt create faucet funding transaction")); + return(result); +} + +UniValue dicebet(const UniValue& params, bool fHelp) +{ + UniValue result(UniValue::VOBJ); std::string hex; uint64_t amount,odds; + if ( fHelp || params.size() > 2 ) + throw runtime_error("dicebet amount odds\n"); + if ( ensure_CCrequirements() < 0 ) + throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); + amount = atof(params[0].get_str().c_str()) * COIN; + if ( params.size() == 2 ) + odds = atof(params[1].get_str().c_str()) * COIN; + else odds = 1; + hex = DiceBet(0,amount,odds); + if ( hex.size() > 0 ) + { + result.push_back(Pair("result", "success")); + result.push_back(Pair("hex", hex)); + } else result.push_back(Pair("error", "couldnt create faucet get transaction")); + return(result); +} + UniValue tokenorders(const UniValue& params, bool fHelp) { uint256 tokenid; @@ -5005,7 +5116,8 @@ UniValue tokenorders(const UniValue& params, bool fHelp) UniValue tokenbalance(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); char destaddr[64]; uint256 tokenid; uint64_t balance; std::vector pubkey; + UniValue result(UniValue::VOBJ); char destaddr[64]; uint256 tokenid; uint64_t balance; std::vector pubkey; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( fHelp || params.size() > 2 ) throw runtime_error("tokenbalance tokenid [pubkey]\n"); if ( ensure_CCrequirements() < 0 ) @@ -5015,7 +5127,7 @@ UniValue tokenbalance(const UniValue& params, bool fHelp) pubkey = ParseHex(params[1].get_str().c_str()); else pubkey = Mypubkey(); result.push_back(Pair("result", "success")); - if ( GetCCaddress(EVAL_ASSETS,destaddr,pubkey2pk(pubkey)) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) result.push_back(Pair("CCaddress",destaddr)); balance = GetAssetBalance(pubkey2pk(pubkey),tokenid); result.push_back(Pair("tokenid", params[0].get_str())); @@ -5023,27 +5135,6 @@ UniValue tokenbalance(const UniValue& params, bool fHelp) return(result); } -UniValue tokenaddress(const UniValue& params, bool fHelp) -{ - UniValue result(UniValue::VOBJ); std::vector pubkey; char destaddr[64]; - if ( fHelp || params.size() > 1 ) - throw runtime_error("tokenaddress [pubkey]\n"); - if ( ensure_CCrequirements() < 0 ) - throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); - result.push_back(Pair("result", "success")); - if ( GetCCaddress(EVAL_ASSETS,destaddr,pubkey2pk(pubkey)) != 0 ) - result.push_back(Pair("AssetsCCaddress",destaddr)); - if ( params.size() == 1 ) - { - pubkey = ParseHex(params[0].get_str().c_str()); - if ( GetCCaddress(EVAL_ASSETS,destaddr,pubkey2pk(pubkey)) != 0 ) - result.push_back(Pair("CCaddress",destaddr)); - } - if ( GetCCaddress(EVAL_ASSETS,destaddr,pubkey2pk(Mypubkey())) != 0 ) - result.push_back(Pair("myCCaddress",destaddr)); - return(result); -} - UniValue tokencreate(const UniValue& params, bool fHelp) { UniValue result(UniValue::VOBJ); std::string name,description,hex; uint64_t supply;