From 287efad431a3a62765f65000bdb847673a8db793 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:05:30 -1100 Subject: [PATCH 01/42] CCcontract_info --- src/cc/CCassets.h | 4 +- src/cc/CCcustom.cpp | 111 +++++++++++---------------------------- src/cc/CCcustom.inc | 1 - src/cc/CCfaucet.h | 4 +- src/cc/CCinclude.h | 10 ++++ src/cc/CCrewards.h | 30 +++++++++++ src/cc/CCutils.cpp | 28 ++++++++++ src/cc/assets.cpp | 42 ++++----------- src/cc/eval.cpp | 23 ++++---- src/cc/eval.h | 5 +- src/cc/faucet.cpp | 8 +-- src/cc/rewards.cpp | 34 +++--------- src/wallet/rpcwallet.cpp | 5 +- 13 files changed, 135 insertions(+), 170 deletions(-) create mode 100644 src/cc/CCrewards.h diff --git a/src/cc/CCassets.h b/src/cc/CCassets.h index 0b4dd5578..343108aa8 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); diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index a689c117f..1f9d634e0 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -14,22 +14,20 @@ ******************************************************************************/ #include "CCinclude.h" +#include "CCassets.h" +#include "CCfaucet.h" +#include "CCrewards.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. */ -//CC *MakeAssetCond(CPubKey pk); -//CC *MakeFaucetCond(CPubKey pk); -//CC *MakeRewardsCond(CPubKey pk); - //BTCD Address: RAssetsAtGnvwgK9gVHBbAU4sVTah1hAm5 //BTCD Privkey: UvtvQVgVScXEYm4J3r4nE4nbFuGXSVM5pKec8VWXwgG9dmpWBuDh //BTCD Address: RSavingsEYcivt2DFsxsKeCjqArV6oVtVZ @@ -67,82 +65,33 @@ uint8_t RewardsCCpriv[32] = { 0x9f, 0x0c, 0x57, 0xdc, 0x6f, 0x78, 0xae, 0xb0, 0x #undef FUNCNAME #undef EVALCODE -/*bool IsAssetsInput(CScript const& scriptSig) +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->CCaddress,AssetsCCaddr); + strcpy(cp->CChexstr,AssetsCChexstr); + memcpy(cp->CCpriv,AssetsCCpriv,32); + cp->validate = AssetsValidate; + cp->ismyvin = IsAssetsInput; + break; + case EVAL_FAUCET: + strcpy(cp->CCaddress,FaucetCCaddr); + strcpy(cp->CChexstr,FaucetCChexstr); + memcpy(cp->CCpriv,FaucetCCpriv,32); + cp->validate = FaucetValidate; + cp->ismyvin = IsFaucetInput; + break; + case EVAL_REWARDS: + strcpy(cp->CCaddress,RewardsCCaddr); + strcpy(cp->CChexstr,RewardsCChexstr); + memcpy(cp->CCpriv,RewardsCCpriv,32); + cp->validate = RewardsValidate; + cp->ismyvin = IsRewardsInput; + 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/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..e4b4c1b52 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -28,6 +28,16 @@ #include #include +struct CCcontract_info +{ + char CCaddress[64],CChexstr[72]; + uint8_t CCpriv[32]; + bool (*validate)(Eval* eval,struct CCcontract_info *cp,const CTransaction &tx); + bool (*ismyvin)(CScript const& scriptSig); + uint8_t evalcode; +}; +struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode); + #ifdef ENABLE_WALLET extern CWallet* pwalletMain; #endif diff --git a/src/cc/CCrewards.h b/src/cc/CCrewards.h new file mode 100644 index 000000000..fe900c02b --- /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,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); + +#endif diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 010727132..ceccee93e 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -207,3 +207,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..60268b20b 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -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; const CTransaction vinTx; uint256 hashBlock,assetid,assetid2; int32_t i,starti,numvins,numvouts,preventCCvins,preventCCvouts; uint64_t 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(ctx.vout[n-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"); @@ -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/eval.cpp b/src/cc/eval.cpp index 43b215748..2c886668d 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/CCutils.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..59404dba8 100644 --- a/src/cc/eval.h +++ b/src/cc/eval.h @@ -268,9 +268,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); +void 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..bbce16366 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -37,7 +37,7 @@ uint64_t IsFaucetvout(const CTransaction& tx,int32_t v) 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 +46,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 ) @@ -111,7 +111,7 @@ bool FaucetValidate(Eval* eval,const CTransaction &tx) } } -bool ProcessFaucet(Eval* eval, std::vector paramsNull,const CTransaction &ctx, unsigned int nIn) +/*bool ProcessFaucet(Eval* eval, std::vector paramsNull,const CTransaction &ctx, unsigned int nIn) { static uint256 prevtxid; uint256 txid; txid = ctx.GetHash(); @@ -130,7 +130,7 @@ bool ProcessFaucet(Eval* eval, std::vector paramsNull,const CTransactio } fprintf(stderr,"faucet validate failed\n"); return(false); -} +}*/ uint64_t AddFaucetInputs(CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 5253035ac..b08cd6bd7 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -18,34 +18,12 @@ /* */ -extern const char *RewardsCCaddr; -extern char RewardsCChexstr[67]; -bool IsRewardsInput(CScript const& scriptSig); - uint64_t RewardsCalc(uint64_t claim,uint256 txid) { uint64_t reward = 0; 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) { char destaddr[64]; @@ -57,7 +35,7 @@ uint64_t IsRewardsvout(const CTransaction& tx,int32_t v) 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 +43,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"); @@ -92,7 +70,7 @@ bool RewardsExactAmounts(Eval* eval,const CTransaction &tx,int32_t minage,uint64 else return(true); } -bool RewardsValidate(Eval* eval,const CTransaction &tx) +bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; numvins = tx.vin.size(); @@ -107,7 +85,7 @@ bool RewardsValidate(Eval* eval,const CTransaction &tx) if ( IsCCInput(tx.vin[0].scriptSig) == 0 ) return eval->Invalid("illegal normal vini"); } - if ( RewardsExactAmounts(eval,tx,1,10000) == false ) + if ( RewardsExactAmounts(cp,eval,tx,1,10000) == false ) return false; else { @@ -125,7 +103,7 @@ bool RewardsValidate(Eval* eval,const CTransaction &tx) return(true); } -bool ProcessRewards(Eval* eval, std::vector paramsNull,const CTransaction &ctx, unsigned int nIn) +/*bool ProcessRewards(Eval* eval, std::vector paramsNull,const CTransaction &ctx, unsigned int nIn) { static uint256 prevtxid; uint256 txid; txid = ctx.GetHash(); @@ -144,7 +122,7 @@ bool ProcessRewards(Eval* eval, std::vector paramsNull,const CTransacti } fprintf(stderr,"ProcessRewards failed\n"); return(false); -} +}*/ uint64_t AddRewardsInputs(CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index c79226954..ac202298d 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4842,10 +4842,7 @@ 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" UniValue rewardsaddress(const UniValue& params, bool fHelp) { From 6d450023961ff8042567f9ea8ddf38580a256ff1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:06:48 -1100 Subject: [PATCH 02/42] CCinclude.h --- src/cc/eval.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/eval.cpp b/src/cc/eval.cpp index 2c886668d..f157b6197 100644 --- a/src/cc/eval.cpp +++ b/src/cc/eval.cpp @@ -21,7 +21,7 @@ #include "script/cc.h" #include "cc/eval.h" #include "cc/utils.h" -#include "cc/CCutils.h" +#include "cc/CCinclude.h" #include "main.h" #include "chain.h" #include "core_io.h" From b646136072eefbe13fc7459877b43d655649c270 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:07:49 -1100 Subject: [PATCH 03/42] struct CCcontract_info * --- src/cc/eval.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/eval.h b/src/cc/eval.h index 59404dba8..e35bd053a 100644 --- a/src/cc/eval.h +++ b/src/cc/eval.h @@ -268,7 +268,7 @@ typedef std::pair TxProof; uint256 GetMerkleRoot(const std::vector& vLeaves); -void CCinit(struct CCcontract_info *cp,uint8_t evalcode); +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); From 633335654f03b1e0794f86e1d844ccb74a38a789 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:08:19 -1100 Subject: [PATCH 04/42] Prevtxid --- src/cc/CCinclude.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index e4b4c1b52..7f4feb426 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -30,11 +30,12 @@ struct CCcontract_info { + uint256 prevtxid; char CCaddress[64],CChexstr[72]; uint8_t CCpriv[32]; bool (*validate)(Eval* eval,struct CCcontract_info *cp,const CTransaction &tx); bool (*ismyvin)(CScript const& scriptSig); - uint8_t evalcode; + uint8_t evalcode,didinit; }; struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode); From d4d7e23790f7c843886a77b2ad651520f1299447 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:10:40 -1100 Subject: [PATCH 05/42] Cp arg --- src/cc/CCassets.h | 2 +- src/cc/CCassetsCore.cpp | 4 ++-- src/cc/assets.cpp | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cc/CCassets.h b/src/cc/CCassets.h index 343108aa8..2dd35577e 100644 --- a/src/cc/CCassets.h +++ b/src/cc/CCassets.h @@ -39,7 +39,7 @@ bool ValidateAssetRemainder(int32_t sellflag,uint64_t remaining_price,uint64_t r 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); +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..1891bbbdf 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -297,7 +297,7 @@ uint64_t AssetValidateSellvin(Eval* eval,uint64_t &tmpprice,std::vector 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/assets.cpp b/src/cc/assets.cpp index 60268b20b..13cfd285b 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -155,7 +155,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx 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 ) @@ -288,7 +288,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx //'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 ) From eb304fa0e9fa50ee6c4d41dba21db85b11e2da24 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:12:31 -1100 Subject: [PATCH 06/42] unspendableCCaddr --- src/cc/CCcustom.cpp | 6 +++--- src/cc/CCinclude.h | 2 +- src/cc/assets.cpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index 1f9d634e0..69353213f 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -71,21 +71,21 @@ struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode) switch ( evalcode ) { case EVAL_ASSETS: - strcpy(cp->CCaddress,AssetsCCaddr); + 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->CCaddress,FaucetCCaddr); + 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->CCaddress,RewardsCCaddr); + strcpy(cp->unspendableCCaddr,RewardsCCaddr); strcpy(cp->CChexstr,RewardsCChexstr); memcpy(cp->CCpriv,RewardsCCpriv,32); cp->validate = RewardsValidate; diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 7f4feb426..2eb6785fd 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -31,7 +31,7 @@ struct CCcontract_info { uint256 prevtxid; - char CCaddress[64],CChexstr[72]; + char unspendableCCaddr[64],CChexstr[72]; uint8_t CCpriv[32]; bool (*validate)(Eval* eval,struct CCcontract_info *cp,const CTransaction &tx); bool (*ismyvin)(CScript const& scriptSig); diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 13cfd285b..0c3d18dea 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -254,7 +254,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx //'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; @@ -311,7 +311,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx { 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"); } } From 14294273b6734c7b5c97972d813347bd8635ebed Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:14:14 -1100 Subject: [PATCH 07/42] struct CCcontract_info *cp, --- src/cc/CCassets.h | 4 ++-- src/cc/CCassetsCore.cpp | 12 ++++++------ src/cc/assets.cpp | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/cc/CCassets.h b/src/cc/CCassets.h index 2dd35577e..0fbe4891c 100644 --- a/src/cc/CCassets.h +++ b/src/cc/CCassets.h @@ -37,8 +37,8 @@ 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); +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 diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 1891bbbdf..d429c4ef3 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -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,7 +251,7 @@ 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); return eval->Invalid("invalid vin AssetsCCaddr"); @@ -264,11 +264,11 @@ uint64_t AssetValidateCCvin(Eval* eval,char *CCaddr,char *origaddr,const CTransa 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,11 +286,11 @@ 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"); diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 0c3d18dea..3b3b6f428 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -265,7 +265,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx //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"); @@ -291,7 +291,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx 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"); From ae02bd6ba228251d8f14fa6c21f75e584ec6bba8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:15:17 -1100 Subject: [PATCH 08/42] Fix --- src/cc/CCassetsCore.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index d429c4ef3..e837b0fb2 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -253,7 +253,7 @@ uint64_t AssetValidateCCvin(struct CCcontract_info *cp,Eval* eval,char *CCaddr,c } 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 ) From ced7a51333b90744c6ccdd35c9543b207368cacb Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:17:21 -1100 Subject: [PATCH 09/42] Eval* eval, --- src/cc/CCinclude.h | 2 +- src/cc/faucet.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 2eb6785fd..539a9b123 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -33,7 +33,7 @@ struct CCcontract_info uint256 prevtxid; char unspendableCCaddr[64],CChexstr[72]; uint8_t CCpriv[32]; - bool (*validate)(Eval* eval,struct CCcontract_info *cp,const CTransaction &tx); + bool (*validate)(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); bool (*ismyvin)(CScript const& scriptSig); uint8_t evalcode,didinit; }; diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index bbce16366..cc76fa8e6 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -75,7 +75,7 @@ bool FaucetExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction else return(true); } -bool FaucetValidate(Eval* eval,const CTransaction &tx) +bool FaucetValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; numvins = tx.vin.size(); @@ -92,7 +92,7 @@ bool FaucetValidate(Eval* eval,const CTransaction &tx) return eval->Invalid("illegal normal vini"); } fprintf(stderr,"check amounts\n"); - if ( FaucetExactAmounts(eval,tx,1,10000) == false ) + if ( FaucetExactAmounts(cp,eval,tx,1,10000) == false ) return false; else { From 98252f712b0090d247052a8729d6e7bfeb5b5bae Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:20:45 -1100 Subject: [PATCH 10/42] Fix --- src/cc/assets.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 3b3b6f428..d720894f5 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -132,12 +132,12 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { static uint256 zero; - CTxDestination address; const CTransaction vinTx; uint256 hashBlock,assetid,assetid2; int32_t i,starti,numvins,numvouts,preventCCvins,preventCCvouts; uint64_t remaining_price,nValue,assetoshis,outputs,inputs,tmpprice,totalunits,ignore; std::vector origpubkey,tmporigpubkey,ignorepubkey; uint8_t funcid; char destaddr[64],origaddr[64],CCaddr[64]; + CTxDestination address; const 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 ( (funcid= DecodeAssetOpRet(ctx.vout[n-1].scriptPubKey,assetid,assetid2,amount,origpubkey)) == 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 ) @@ -186,11 +186,11 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx // 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 @@ -219,7 +219,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx //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"); From 11ac375bfa8b3f853c48dab3c663b69f6881d715 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:22:01 -1100 Subject: [PATCH 11/42] Test --- src/cc/assets.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index d720894f5..b1a34d524 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -132,7 +132,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { static uint256 zero; - CTxDestination address; const 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]; + 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; @@ -199,7 +199,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx //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"); @@ -237,7 +237,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx { 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"); } } From 32406f1be77e99574767df11b521bfc73ce249b7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:23:47 -1100 Subject: [PATCH 12/42] Fix --- src/cc/faucet.cpp | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index cc76fa8e6..98295c300 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -26,6 +26,8 @@ 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. */ +// start of consensus code + uint64_t IsFaucetvout(const CTransaction& tx,int32_t v) { char destaddr[64]; @@ -110,27 +112,9 @@ bool FaucetValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx } } } +// 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) { From b9f95c74f6b2e07861ff29777f92ff7a38c3ab2e Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:24:49 -1100 Subject: [PATCH 13/42] Add cp --- src/cc/faucet.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index 98295c300..8d0075f34 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -28,12 +28,12 @@ // start of consensus code -uint64_t IsFaucetvout(const CTransaction& tx,int32_t v) +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); @@ -58,7 +58,7 @@ bool FaucetExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction //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; } } @@ -66,7 +66,7 @@ bool FaucetExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction for (i=0; i Date: Tue, 24 Jul 2018 00:26:51 -1100 Subject: [PATCH 14/42] C --- src/cc/faucet.cpp | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index 8d0075f34..24cd6f612 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -116,7 +116,7 @@ bool FaucetValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx // 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; @@ -127,7 +127,7 @@ uint64_t AddFaucetInputs(CMutableTransaction &mtx,CPubKey pk,uint64_t total,int3 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())); @@ -142,6 +142,26 @@ uint64_t AddFaucetInputs(CMutableTransaction &mtx,CPubKey pk,uint64_t total,int3 return(totalinputs); } +std::string FaucetGet(uint64_t txfee) +{ + 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); + mypk = pubkey2pk(Mypubkey()); + 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)); + } 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; @@ -157,22 +177,4 @@ std::string FaucetFund(uint64_t txfee,uint64_t funds) return(0); } -std::string FaucetGet(uint64_t txfee) -{ - CMutableTransaction mtx; CPubKey mypk,faucetpk; CScript opret; uint64_t inputs,CCchange=0,nValue=COIN; - if ( txfee == 0 ) - txfee = 10000; - faucetpk = GetUnspendable(EVAL_FAUCET,0); - mypk = pubkey2pk(Mypubkey()); - if ( (inputs= AddFaucetInputs(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)); - } else fprintf(stderr,"cant find faucet inputs\n"); - return(0); -} From e04b5c084b1566bffee8f58ff37eb689f4790ac4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:39:58 -1100 Subject: [PATCH 15/42] Fixes --- src/cc/CCassetsCore.cpp | 4 +- src/cc/CCassetstx.cpp | 70 +++++++++++++++------------ src/cc/CCinclude.h | 4 +- src/cc/CCtx.cpp | 12 ++--- src/cc/CCutils.cpp | 6 +-- src/cc/faucet.cpp | 13 ++--- src/cc/rewards.cpp | 102 ++++++++++++++++++++------------------- src/wallet/rpcwallet.cpp | 32 ++++++------ 8 files changed, 131 insertions(+), 112 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index e837b0fb2..88ebf0feb 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); } diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 29fb299c0..d961d1713 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,13 +44,15 @@ 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]; + 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 *)AssetsCCaddr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { @@ -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()); @@ -106,14 +109,15 @@ std::string CreateAsset(uint64_t txfee,uint64_t assetsupply,std::string name,std { 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))); + 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/CCinclude.h b/src/cc/CCinclude.h index 539a9b123..1b262f301 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -47,7 +47,7 @@ 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); 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; ievalcode,pk)) != 0 ) { Getscriptaddress(destaddr,CCPubKey(payoutCond)); cc_free(payoutCond); diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index 24cd6f612..0882e0a6b 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -120,7 +120,7 @@ uint64_t AddFaucetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPu { 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++) { @@ -148,7 +148,7 @@ std::string FaucetGet(uint64_t txfee) 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(cp,mtx,faucetpk,nValue+txfee,60)) > 0 ) { @@ -157,22 +157,23 @@ std::string FaucetGet(uint64_t 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; + 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(EVAL_FAUCET,0); + faucetpk = GetUnspendable(cp,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(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); } return(0); } diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index b08cd6bd7..6dfae6839 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -24,12 +24,12 @@ uint64_t RewardsCalc(uint64_t claim,uint256 txid) return(reward); } -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); @@ -51,7 +51,7 @@ bool RewardsExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransactio { 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; } } @@ -59,7 +59,7 @@ bool RewardsExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransactio for (i=0; i origpubkey; 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())); @@ -150,50 +150,13 @@ 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) -{ - 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 ) - { - 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; + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t reward,claim,inputs,CCchange=0; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; - rewardspk = GetUnspendable(EVAL_REWARDS,0); + rewardspk = GetUnspendable(cp,0); mypk = pubkey2pk(Mypubkey()); if ( (claim= AddRewardsInputs(mtx,mypk,(1LL << 30),1)) > 0 && (reward= RewardsCalc(claim,mtx.vin[0].prevout.hash)) > txfee ) { @@ -204,9 +167,50 @@ std::string RewardsUnlock(uint64_t 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)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); } } else fprintf(stderr,"cant find rewards inputs\n"); return(0); } +// 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) +{ + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; 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(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(cp,mtx,mypk,txfee,opret)); + } + return(0); +} + +std::string RewardsLock(uint64_t txfee,uint64_t amount) +{ + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_REWARDS); + if ( txfee == 0 ) + txfee = 10000; + rewardspk = GetUnspendable(cp,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(cp,mtx,mypk,txfee,opret)); + } else fprintf(stderr,"cant find rewards inputs\n"); + return(0); +} + + diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index ac202298d..5d92a585f 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4846,21 +4846,22 @@ int32_t ensure_CCrequirements() UniValue rewardsaddress(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); std::vector pubkey; char destaddr[64]; + UniValue result(UniValue::VOBJ); std::vector pubkey; char destaddr[64]; struct CCcontract_info *cp,C; + 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"); result.push_back(Pair("result", "success")); - if ( GetCCaddress(EVAL_REWARDS,destaddr,pubkey2pk(pubkey)) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) result.push_back(Pair("RewardsCCaddress",destaddr)); if ( params.size() == 1 ) { 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); } @@ -4935,21 +4936,22 @@ UniValue rewardsunlock(const UniValue& params, bool fHelp) UniValue faucetaddress(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); std::vector pubkey; char destaddr[64]; + UniValue result(UniValue::VOBJ); std::vector pubkey; char destaddr[64]; struct CCcontract_info *cp,C; + 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"); result.push_back(Pair("result", "success")); - if ( GetCCaddress(EVAL_FAUCET,destaddr,pubkey2pk(pubkey)) != 0 ) + if ( GetCCaddress(cp,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 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) result.push_back(Pair("CCaddress",destaddr)); } - if ( GetCCaddress(EVAL_FAUCET,destaddr,pubkey2pk(Mypubkey())) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(Mypubkey())) != 0 ) result.push_back(Pair("myCCaddress",destaddr)); return(result); } @@ -5002,7 +5004,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 ) @@ -5012,7 +5015,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())); @@ -5022,21 +5025,22 @@ UniValue tokenbalance(const UniValue& params, bool fHelp) UniValue tokenaddress(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); std::vector pubkey; char destaddr[64]; + UniValue result(UniValue::VOBJ); std::vector pubkey; char destaddr[64]; struct CCcontract_info *cp,C; + 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"); 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("AssetsCCaddress",destaddr)); if ( params.size() == 1 ) { pubkey = ParseHex(params[0].get_str().c_str()); - if ( GetCCaddress(EVAL_ASSETS,destaddr,pubkey2pk(pubkey)) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) result.push_back(Pair("CCaddress",destaddr)); } - if ( GetCCaddress(EVAL_ASSETS,destaddr,pubkey2pk(Mypubkey())) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(Mypubkey())) != 0 ) result.push_back(Pair("myCCaddress",destaddr)); return(result); } From 3749ec7019829cab486553e0c9518d8e11dc82f1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:41:23 -1100 Subject: [PATCH 16/42] Fx --- src/cc/CCassetsCore.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 88ebf0feb..466db2d1b 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -258,7 +258,7 @@ uint64_t AssetValidateCCvin(struct CCcontract_info *cp,Eval* eval,char *CCaddr,c } 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); From d0c3462029c20b434404688ca1f7a77dc515cee9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:43:48 -1100 Subject: [PATCH 17/42] Test --- src/cc/CCinclude.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 1b262f301..b10b64478 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -66,7 +66,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); From f9ebfa97bb39aad8f3560f48580c606647b11f9f Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:45:05 -1100 Subject: [PATCH 18/42] Fix --- src/cc/rewards.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 6dfae6839..961883c5f 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -158,9 +158,9 @@ std::string RewardsUnlock(uint64_t txfee) txfee = 10000; rewardspk = GetUnspendable(cp,0); mypk = pubkey2pk(Mypubkey()); - if ( (claim= AddRewardsInputs(mtx,mypk,(1LL << 30),1)) > 0 && (reward= RewardsCalc(claim,mtx.vin[0].prevout.hash)) > txfee ) + if ( (claim= AddRewardsInputs(cp,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 ( (inputs= AddRewardsInputs(cp,mtx,mypk,reward+txfee,30)) > 0 ) { if ( inputs > (reward+txfee) ) CCchange = (inputs - reward - txfee); From a35184925bba241f193b700339897edfa6c2b9b4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:50:32 -1100 Subject: [PATCH 19/42] Test --- src/cc/CCassetstx.cpp | 4 ++-- src/cc/CCcustom.cpp | 8 ++++++-- src/cc/rewards.cpp | 21 --------------------- 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index d961d1713..c52864218 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -53,7 +53,7 @@ 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]; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_ASSETS); - SetCCunspents(unspentOutputs,(char *)AssetsCCaddr); + SetCCunspents(unspentOutputs,(char *)cp->unspendableCCaddr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; @@ -108,7 +108,7 @@ 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)); + 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); diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index 69353213f..8ebbe821d 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -21,11 +21,15 @@ /* 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 - 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 */ //BTCD Address: RAssetsAtGnvwgK9gVHBbAU4sVTah1hAm5 diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 961883c5f..fe1583303 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -103,27 +103,6 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t return(true); } -/*bool ProcessRewards(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,"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(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; From da8b0b333653ce2539fd61c3d1fc6f27a9944fe2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 00:56:29 -1100 Subject: [PATCH 20/42] Test --- src/cc/CCcustom.cpp | 2 ++ src/cc/faucet.cpp | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index 8ebbe821d..d15e43a02 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -30,6 +30,8 @@ 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 */ //BTCD Address: RAssetsAtGnvwgK9gVHBbAU4sVTah1hAm5 diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index 0882e0a6b..00b01d76f 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -79,7 +79,7 @@ bool FaucetExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction bool FaucetValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { - int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; + int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; bool retval; numvins = tx.vin.size(); numvouts = tx.vout.size(); preventCCvins = preventCCvouts = -1; @@ -87,18 +87,23 @@ bool FaucetValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("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"); + //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(cp,tx,0) != 0 ) { @@ -107,8 +112,11 @@ bool FaucetValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx } 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); } } } From cfea7a4690190628a29d0bed00e363b86bbe5232 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 07:13:05 -1100 Subject: [PATCH 21/42] Dice skeleton --- src/cc/CCcustom.cpp | 17 ++++ src/cc/CCdice.h | 29 +++++++ src/cc/CCinclude.h | 2 + src/cc/CCrewards.h | 4 +- src/cc/CCutils.cpp | 24 ++++++ src/cc/assets.cpp | 2 +- src/cc/dice.cpp | 182 +++++++++++++++++++++++++++++++++++++++ src/cc/rewards.cpp | 101 ++++++++++++++++------ src/rpcserver.cpp | 7 +- src/rpcserver.h | 3 + src/wallet/rpcwallet.cpp | 149 ++++++++++++++++++++------------ 11 files changed, 435 insertions(+), 85 deletions(-) create mode 100644 src/cc/CCdice.h create mode 100644 src/cc/dice.cpp diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index d15e43a02..f4642c82e 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -71,6 +71,16 @@ uint8_t RewardsCCpriv[32] = { 0x9f, 0x0c, 0x57, 0xdc, 0x6f, 0x78, 0xae, 0xb0, 0x #undef FUNCNAME #undef EVALCODE +// Dice +#define FUNCNAME IsDiceInput +#define EVALCODE EVAL_DICE +const char *DiceCCaddr = "RJCqA4jQTFEZ841dZgxko8aYgUU3FRNGNm" ;//"RYRJGMAYEfLCZ6ZddbpxPiUZ1sens8vPYK"; +char DiceCChexstr[67] = { "026f00fdc2f1ed0006d66e2ca1787633590581c2fc90e7cb7b01a6c1131b40e94d" }; +uint8_t DiceCCpriv[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 + struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode) { cp->evalcode = evalcode; @@ -97,6 +107,13 @@ struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode) 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; } return(cp); } diff --git a/src/cc/CCdice.h b/src/cc/CCdice.h new file mode 100644 index 000000000..582a9fe64 --- /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_REWARDS_H +#define CC_REWARDS_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/CCinclude.h b/src/cc/CCinclude.h index b10b64478..f72fce237 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -54,6 +54,8 @@ CTxOut MakeCC1vout(uint8_t evalcode,CAmount nValue,CPubKey pk); CC *MakeCCcond1(uint8_t evalcode,CPubKey pk); CC* GetCryptoCondition(CScript const& scriptSig); bool IsCCInput(CScript const& scriptSig); +int32_t unstringbits(char *buf,uint64_t bits); +uint64_t stringbits(char *str); uint256 revuint256(uint256 txid); char *uint256_str(char *dest,uint256 txid); uint256 Parseuint256(char *hexstr); diff --git a/src/cc/CCrewards.h b/src/cc/CCrewards.h index fe900c02b..35fe87960 100644 --- a/src/cc/CCrewards.h +++ b/src/cc/CCrewards.h @@ -23,8 +23,8 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); -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 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); #endif diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 80a2e5af6..f8617e536 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -55,6 +55,30 @@ bool IsCCInput(CScript const& scriptSig) return true; } +int32_t unstringbits(char *buf,uint64_t bits) +{ + int32_t i; + for (i=0; i<8; i++,bits>>=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; diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index b1a34d524..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] "" "" 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/rewards.cpp b/src/cc/rewards.cpp index fe1583303..7ec3b83e6 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -16,11 +16,34 @@ #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 */ -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); } @@ -80,6 +103,7 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t return eval->Invalid("no vouts"); else { + // follow rules for (i=0; i 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(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); @@ -118,8 +142,7 @@ uint64_t AddRewardsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CP { 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; @@ -129,34 +152,55 @@ uint64_t AddRewardsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CP return(totalinputs); } +uint64_t RewardsPlanFunds(uint64_t &refsbits,struct CCcontract_info *cp,CPubKey &pk,char *planstr) +{ + char coinaddr[64]; uint64_t sbits,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++) + { + if ( GetTransaction(it->first.txhash,vintx,hashBlock,false) != 0 ) + { + xxx if ( (nValue= IsRewardsvout(cp,vintx,(int32_t)it->first.index)) > 0 ) + { + totalinputs += nValue; + } + } + } + return(totalinputs); +} + std::string RewardsUnlock(uint64_t txfee) { - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t reward,claim,inputs,CCchange=0; struct CCcontract_info *cp,C; + 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 ( (claim= AddRewardsInputs(cp,mtx,mypk,(1LL << 30),1)) > 0 && (reward= RewardsCalc(claim,mtx.vin[0].prevout.hash)) > txfee ) + if ( (amount= AddRewardsInputs(cp,mtx,mypk,(1LL << 30),1)) > 0 && (reward= RewardsCalc(claim,mtx.vin[0].prevout.hash)) > txfee ) { - if ( (inputs= AddRewardsInputs(cp,mtx,mypk,reward+txfee,30)) > 0 ) + if ( (inputs= AddRewardsInputs(cp,mtx,mypk,reward+amount+txfee,30)) > 0 ) { - if ( inputs > (reward+txfee) ) - CCchange = (inputs - reward - txfee); + 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)); + 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' << sbits); return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); } } else fprintf(stderr,"cant find rewards inputs\n"); return(0); } -// 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) +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; struct CCcontract_info *cp,C; + 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; @@ -164,31 +208,34 @@ std::string RewardsFund(uint64_t txfee,uint64_t funds,uint64_t APR,uint64_t mins rewardspk = GetUnspendable(cp,0); if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) { - mtx.vout.push_back(MakeCC1vout(EVAL_REWARDS,funds,rewardspk)); + 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,uint64_t amount) +std::string RewardsLock(uint64_t txfee,char *planstr,uint64_t amount) { - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); - if ( txfee == 0 ) - txfee = 10000; - rewardspk = GetUnspendable(cp,0); mypk = pubkey2pk(Mypubkey()); - if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + rewardspk = GetUnspendable(cp,0); + if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,planstr)) >= amount ) { - mtx.vout.push_back(MakeCC1vout(EVAL_REWARDS,amount,rewardspk)); - // specify destination pubkey, funding txid - //opret = ;// - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); - } else fprintf(stderr,"cant find rewards inputs\n"); + 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/rpcserver.cpp b/src/rpcserver.cpp index 4d766d5ae..40ebd162c 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -354,8 +354,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..ff06966ba 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -228,6 +228,9 @@ 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 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 5d92a585f..e10a7b09b 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4843,21 +4843,17 @@ int32_t ensure_CCrequirements() #include "../cc/CCfaucet.h" #include "../cc/CCassets.h" #include "../cc/CCrewards.h" +#include "../cc/CCdice.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]; struct CCcontract_info *cp,C; - 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"); + UniValue result(UniValue::VOBJ); ; char destaddr[64],str[64]; result.push_back(Pair("result", "success")); + sprintf(str,"%sCCaddress",name); if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) - result.push_back(Pair("RewardsCCaddress",destaddr)); - if ( params.size() == 1 ) + result.push_back(Pair(str,destaddr)); + if ( pubkey.size() == 33 ) { - pubkey = ParseHex(params[0].get_str().c_str()); if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) result.push_back(Pair("CCaddress",destaddr)); } @@ -4866,6 +4862,58 @@ UniValue rewardsaddress(const UniValue& params, bool fHelp) return(result); } +UniValue diceaddress(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("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,"Dice",pubkey)); +} + +UniValue faucetaddress(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("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,"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,"Rewards",pubkey)); +} + +UniValue tokenaddress(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("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,"Assets",pubkey)); +} + UniValue rewardsfund(const UniValue& params, bool fHelp) { UniValue result(UniValue::VOBJ); uint64_t funds,APR,minseconds,maxseconds,mindeposit; std::string hex; @@ -4934,28 +4982,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]; struct CCcontract_info *cp,C; - 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"); - result.push_back(Pair("result", "success")); - if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) - result.push_back(Pair("FaucetCCaddress",destaddr)); - if ( params.size() == 1 ) - { - pubkey = ParseHex(params[0].get_str().c_str()); - if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) - result.push_back(Pair("CCaddress",destaddr)); - } - if ( GetCCaddress(cp,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; @@ -4989,6 +5015,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; @@ -5023,28 +5086,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]; struct CCcontract_info *cp,C; - 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"); - result.push_back(Pair("result", "success")); - if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) - result.push_back(Pair("AssetsCCaddress",destaddr)); - if ( params.size() == 1 ) - { - pubkey = ParseHex(params[0].get_str().c_str()); - if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) - result.push_back(Pair("CCaddress",destaddr)); - } - if ( GetCCaddress(cp,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; From 2fcf7a420c172ff0f538b03b8c73c083562919bd Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 07:15:29 -1100 Subject: [PATCH 22/42] Fix --- src/cc/CCcustom.cpp | 1 + src/cc/CCdice.h | 4 ++-- src/cc/eval.h | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index f4642c82e..c0254d0b1 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -17,6 +17,7 @@ #include "CCassets.h" #include "CCfaucet.h" #include "CCrewards.h" +#include "CCdice.h" /* CCcustom has most of the functions that need to be extended to create a new CC contract. diff --git a/src/cc/CCdice.h b/src/cc/CCdice.h index 582a9fe64..77f7d5dd9 100644 --- a/src/cc/CCdice.h +++ b/src/cc/CCdice.h @@ -14,8 +14,8 @@ ******************************************************************************/ -#ifndef CC_REWARDS_H -#define CC_REWARDS_H +#ifndef CC_DICE_H +#define CC_DICE_H #include "CCinclude.h" diff --git a/src/cc/eval.h b/src/cc/eval.h index e35bd053a..0ce5b6bf7 100644 --- a/src/cc/eval.h +++ b/src/cc/eval.h @@ -41,7 +41,8 @@ EVAL(EVAL_IMPORTCOIN, 0xe2) \ EVAL(EVAL_ASSETS, 0xe3) \ EVAL(EVAL_FAUCET, 0xe4) \ - EVAL(EVAL_REWARDS, 0xe5) + EVAL(EVAL_REWARDS, 0xe5) \ + EVAL(EVAL_DICE, 0xe6) typedef uint8_t EvalCode; From c9bab2a8d2e8c4956faa58df052c8518ed23eee3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 07:18:26 -1100 Subject: [PATCH 23/42] Fix --- src/cc/CCrewards.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCrewards.h b/src/cc/CCrewards.h index 35fe87960..8deff3d81 100644 --- a/src/cc/CCrewards.h +++ b/src/cc/CCrewards.h @@ -24,7 +24,7 @@ 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 RewardsLock(uint64_t txfee,char *planstr,uint64_t amount); std::string RewardsUnlock(uint64_t txfee); #endif From 06b1503b44d4dd3d2d46ba0211d13c048dc9faca Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 07:21:33 -1100 Subject: [PATCH 24/42] Test --- src/cc/CCcustom.cpp | 8 ++++---- src/cc/CCdice.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index c0254d0b1..fb6753e28 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -43,7 +43,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 }; @@ -54,7 +54,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 }; @@ -65,7 +65,7 @@ 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" @@ -75,7 +75,7 @@ uint8_t RewardsCCpriv[32] = { 0x9f, 0x0c, 0x57, 0xdc, 0x6f, 0x78, 0xae, 0xb0, 0x // Dice #define FUNCNAME IsDiceInput #define EVALCODE EVAL_DICE -const char *DiceCCaddr = "RJCqA4jQTFEZ841dZgxko8aYgUU3FRNGNm" ;//"RYRJGMAYEfLCZ6ZddbpxPiUZ1sens8vPYK"; +const char *DiceCCaddr = "RJCqA4jQTFEZ841dZgxko8aYgUU3FRNGNm"; //"RYRJGMAYEfLCZ6ZddbpxPiUZ1sens8vPYK"; char DiceCChexstr[67] = { "026f00fdc2f1ed0006d66e2ca1787633590581c2fc90e7cb7b01a6c1131b40e94d" }; uint8_t DiceCCpriv[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" diff --git a/src/cc/CCdice.h b/src/cc/CCdice.h index 77f7d5dd9..7c89d18d5 100644 --- a/src/cc/CCdice.h +++ b/src/cc/CCdice.h @@ -24,6 +24,6 @@ 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) +std::string DiceBet(uint64_t txfee,uint64_t amount,uint64_t odds); #endif From 05c8a626893638151b7727a3f2af4ce01fdb0add Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 07:30:20 -1100 Subject: [PATCH 25/42] tst --- src/cc/rewards.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 7ec3b83e6..33ab9a900 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -165,7 +165,7 @@ uint64_t RewardsPlanFunds(uint64_t &refsbits,struct CCcontract_info *cp,CPubKey { if ( GetTransaction(it->first.txhash,vintx,hashBlock,false) != 0 ) { - xxx if ( (nValue= IsRewardsvout(cp,vintx,(int32_t)it->first.index)) > 0 ) + if ( (nValue= IsRewardsvout(cp,vintx,(int32_t)it->first.index)) > 0 ) { totalinputs += nValue; } @@ -182,7 +182,7 @@ std::string RewardsUnlock(uint64_t txfee) txfee = 10000; rewardspk = GetUnspendable(cp,0); mypk = pubkey2pk(Mypubkey()); - if ( (amount= AddRewardsInputs(cp,mtx,mypk,(1LL << 30),1)) > 0 && (reward= RewardsCalc(claim,mtx.vin[0].prevout.hash)) > txfee ) + 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 ) { @@ -191,7 +191,7 @@ std::string RewardsUnlock(uint64_t txfee) if ( CCchange != 0 ) 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' << sbits); + opret << OP_RETURN << E_MARSHAL(ss << cp->evalcode << 'U'); return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); } } else fprintf(stderr,"cant find rewards inputs\n"); From 1f658f5912f966e59c174b8598b1d3c4684f8ccf Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 07:32:48 -1100 Subject: [PATCH 26/42] Test --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 33ab9a900..9f567ae79 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -154,7 +154,7 @@ uint64_t AddRewardsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CP uint64_t RewardsPlanFunds(uint64_t &refsbits,struct CCcontract_info *cp,CPubKey &pk,char *planstr) { - char coinaddr[64]; uint64_t sbits,totalinputs = 0; uint256 hashBlock; CTransaction vintx; + 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); From f0f5f6c09b6d7a60aa457c42e03b6531bae5343e Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 07:44:11 -1100 Subject: [PATCH 27/42] Fix --- src/cc/CCrewards.h | 2 +- src/cc/rewards.cpp | 2 +- src/wallet/rpcwallet.cpp | 58 ++++++++++++++++++++++------------------ 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/cc/CCrewards.h b/src/cc/CCrewards.h index 8deff3d81..b9addfb95 100644 --- a/src/cc/CCrewards.h +++ b/src/cc/CCrewards.h @@ -25,6 +25,6 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t 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); +std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid); #endif diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 9f567ae79..53ade6494 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -174,7 +174,7 @@ uint64_t RewardsPlanFunds(uint64_t &refsbits,struct CCcontract_info *cp,CPubKey return(totalinputs); } -std::string RewardsUnlock(uint64_t txfee) +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); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index e10a7b09b..2c392e3c4 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4872,7 +4872,7 @@ UniValue diceaddress(const UniValue& params, bool fHelp) 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,"Dice",pubkey)); + return(CCaddress(cp,(char *)"Dice",pubkey)); } UniValue faucetaddress(const UniValue& params, bool fHelp) @@ -4885,7 +4885,7 @@ UniValue faucetaddress(const UniValue& params, bool fHelp) 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,"Faucet",pubkey)); + return(CCaddress(cp,(char *)"Faucet",pubkey)); } UniValue rewardsaddress(const UniValue& params, bool fHelp) @@ -4898,7 +4898,7 @@ UniValue rewardsaddress(const UniValue& params, bool fHelp) 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,"Rewards",pubkey)); + return(CCaddress(cp,(char *)"Rewards",pubkey)); } UniValue tokenaddress(const UniValue& params, bool fHelp) @@ -4911,36 +4911,37 @@ UniValue tokenaddress(const UniValue& params, bool fHelp) 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,"Assets",pubkey)); + 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")); @@ -4951,13 +4952,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")); @@ -4968,12 +4970,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")); From 0542b1cd5506078a1558eb895f36df606e17622b Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 07:45:35 -1100 Subject: [PATCH 28/42] Add dice.cpp --- src/Makefile.am | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Makefile.am b/src/Makefile.am index a0c474146..3b7d17e85 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -264,6 +264,7 @@ libbitcoin_server_a_SOURCES = \ cc/assets.cpp \ cc/faucet.cpp \ cc/rewards.cpp \ + cc/dice.cpp \ cc/betprotocol.cpp \ chain.cpp \ checkpoints.cpp \ From fac62537c48bd11e5d4398e7f3a38ffec8f10cb1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 07:48:55 -1100 Subject: [PATCH 29/42] test --- src/cc/CCcustom.cpp | 4 ++-- src/cc/CCutils.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index fb6753e28..982bde154 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -75,8 +75,8 @@ uint8_t RewardsCCpriv[32] = { 0x9f, 0x0c, 0x57, 0xdc, 0x6f, 0x78, 0xae, 0xb0, 0x // Dice #define FUNCNAME IsDiceInput #define EVALCODE EVAL_DICE -const char *DiceCCaddr = "RJCqA4jQTFEZ841dZgxko8aYgUU3FRNGNm"; //"RYRJGMAYEfLCZ6ZddbpxPiUZ1sens8vPYK"; -char DiceCChexstr[67] = { "026f00fdc2f1ed0006d66e2ca1787633590581c2fc90e7cb7b01a6c1131b40e94d" }; +const char *DiceCCaddr = "RJCqA4jQTFEZ841dZgxko8aYgUU3FRNGNm"; //"RLEe8f7Eg3TDuXii9BmNiiiaVGraHUt25c"; +char DiceCChexstr[67] = { "039d966927cfdadab3ee6c56da63c21f17ea753dde4b3dfd41487103e24b27e94e" }; uint8_t DiceCCpriv[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 diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index f8617e536..e61f91f53 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -216,7 +216,7 @@ bool Myprivkey(uint8_t myprivkey[]) if ( pwalletMain->GetKey(keyID,vchSecret) != 0 ) { memcpy(myprivkey,vchSecret.begin(),32); - if ( 0 ) + if ( 1 ) { for (i=0; i<32; i++) fprintf(stderr,"0x%02x, ",myprivkey[i]); From b2b7d05b5e18aeea6c7f4593a47722956e081233 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 07:52:41 -1100 Subject: [PATCH 30/42] test --- src/cc/CCutils.cpp | 2 +- src/wallet/rpcwallet.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index e61f91f53..f8617e536 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -216,7 +216,7 @@ bool Myprivkey(uint8_t myprivkey[]) if ( pwalletMain->GetKey(keyID,vchSecret) != 0 ) { memcpy(myprivkey,vchSecret.begin(),32); - if ( 1 ) + if ( 0 ) { for (i=0; i<32; i++) fprintf(stderr,"0x%02x, ",myprivkey[i]); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 2c392e3c4..7f6816cee 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4865,7 +4865,7 @@ UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector pubkey; - cp = CCinit(&C,EVAL_REWARDS); + cp = CCinit(&C,EVAL_DICE); if ( fHelp || params.size() > 1 ) throw runtime_error("diceaddress [pubkey]\n"); if ( ensure_CCrequirements() < 0 ) @@ -4878,7 +4878,7 @@ UniValue diceaddress(const UniValue& params, bool fHelp) UniValue faucetaddress(const UniValue& params, bool fHelp) { struct CCcontract_info *cp,C; std::vector pubkey; - cp = CCinit(&C,EVAL_REWARDS); + cp = CCinit(&C,EVAL_FAUCET); if ( fHelp || params.size() > 1 ) throw runtime_error("faucetaddress [pubkey]\n"); if ( ensure_CCrequirements() < 0 ) @@ -4904,7 +4904,7 @@ UniValue rewardsaddress(const UniValue& params, bool fHelp) UniValue tokenaddress(const UniValue& params, bool fHelp) { struct CCcontract_info *cp,C; std::vector pubkey; - cp = CCinit(&C,EVAL_REWARDS); + cp = CCinit(&C,EVAL_ASSETS); if ( fHelp || params.size() > 1 ) throw runtime_error("tokenaddress [pubkey]\n"); if ( ensure_CCrequirements() < 0 ) From cd56547d355821e24aff7c782a358f3b80769fa7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 07:54:27 -1100 Subject: [PATCH 31/42] Test --- src/cc/CCcustom.cpp | 2 +- src/cc/CCutils.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index 982bde154..243177593 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -75,7 +75,7 @@ uint8_t RewardsCCpriv[32] = { 0x9f, 0x0c, 0x57, 0xdc, 0x6f, 0x78, 0xae, 0xb0, 0x // Dice #define FUNCNAME IsDiceInput #define EVALCODE EVAL_DICE -const char *DiceCCaddr = "RJCqA4jQTFEZ841dZgxko8aYgUU3FRNGNm"; //"RLEe8f7Eg3TDuXii9BmNiiiaVGraHUt25c"; +const char *DiceCCaddr = "REabWB7KjFN5C3LFMZ5odExHPenYzHLtVw"; //"RLEe8f7Eg3TDuXii9BmNiiiaVGraHUt25c"; char DiceCChexstr[67] = { "039d966927cfdadab3ee6c56da63c21f17ea753dde4b3dfd41487103e24b27e94e" }; uint8_t DiceCCpriv[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" diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index f8617e536..e61f91f53 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -216,7 +216,7 @@ bool Myprivkey(uint8_t myprivkey[]) if ( pwalletMain->GetKey(keyID,vchSecret) != 0 ) { memcpy(myprivkey,vchSecret.begin(),32); - if ( 0 ) + if ( 1 ) { for (i=0; i<32; i++) fprintf(stderr,"0x%02x, ",myprivkey[i]); From 6f1e714b5a6a29a162cbbddcb9ca4007967376d9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 08:01:39 -1100 Subject: [PATCH 32/42] Test --- src/cc/CCcustom.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index 243177593..cb8a1de87 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -77,7 +77,7 @@ uint8_t RewardsCCpriv[32] = { 0x9f, 0x0c, 0x57, 0xdc, 0x6f, 0x78, 0xae, 0xb0, 0x #define EVALCODE EVAL_DICE const char *DiceCCaddr = "REabWB7KjFN5C3LFMZ5odExHPenYzHLtVw"; //"RLEe8f7Eg3TDuXii9BmNiiiaVGraHUt25c"; char DiceCChexstr[67] = { "039d966927cfdadab3ee6c56da63c21f17ea753dde4b3dfd41487103e24b27e94e" }; -uint8_t DiceCCpriv[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 }; +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 From 6982938529e9620bc27a3ced4bf0c5b34f94165f Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 08:43:50 -1100 Subject: [PATCH 33/42] Skeletons for lotto, Ponzi and auction --- src/cc/CCauction.h | 30 +++++++ src/cc/CCcustom.cpp | 33 ++++++++ src/cc/CClotto.h | 29 +++++++ src/cc/CCponzi.h | 29 +++++++ src/cc/auction.cpp | 202 ++++++++++++++++++++++++++++++++++++++++++++ src/cc/eval.h | 5 +- src/cc/lotto.cpp | 182 +++++++++++++++++++++++++++++++++++++++ src/cc/ponzi.cpp | 182 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 691 insertions(+), 1 deletion(-) create mode 100644 src/cc/CCauction.h create mode 100644 src/cc/CClotto.h create mode 100644 src/cc/CCponzi.h create mode 100644 src/cc/auction.cpp create mode 100644 src/cc/lotto.cpp create mode 100644 src/cc/ponzi.cpp 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 cb8a1de87..7080e3ed5 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -18,6 +18,9 @@ #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. @@ -82,6 +85,36 @@ uint8_t DiceCCpriv[32] = { 0x0e, 0xe8, 0xf5, 0xb4, 0x3d, 0x25, 0xcc, 0x35, 0xd1, #undef FUNCNAME #undef EVALCODE +// Lotto +#define FUNCNAME IsLottoInput +#define EVALCODE EVAL_LOTTO +const char *LottoCCaddr = "REabWB7KjFN5C3LFMZ5odExHPenYzHLtVw"; //"RLW6hhRqBZZMBndnyPv29Yg3krh6iBYCyg"; +char LottoCChexstr[67] = { "03f72d2c4db440df1e706502b09ca5fec73ffe954ea1883e4049e98da68690d98f" }; +uint8_t LottoCCpriv[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 + +// Ponzi +#define FUNCNAME IsPonziInput +#define EVALCODE EVAL_PONZI +const char *PonziCCaddr = "REabWB7KjFN5C3LFMZ5odExHPenYzHLtVw"; //"RWSHRbxnJYLvDjpcQ2i8MekgP6h2ctTKaj"; +char PonziCChexstr[67] = { "039b52d294b413b07f3643c1a28c5467901a76562d8b39a785910ae0a0f3043810" }; +uint8_t PonziCCpriv[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 + +// Auction +#define FUNCNAME IsAuctionInput +#define EVALCODE EVAL_AUCTION +const char *AuctionCCaddr = "REabWB7KjFN5C3LFMZ5odExHPenYzHLtVw"; //"RFtVDNmdTZBTNZdmFRbfBgJ6LitgTghikL"; +char AuctionCChexstr[67] = { "037eefe050c14cb60ae65d5b2f69eaa1c9006826d729bc0957bdc3024e3ca1dbe6" }; +uint8_t AuctionCCpriv[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 + struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode) { cp->evalcode = evalcode; 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/auction.cpp b/src/cc/auction.cpp new file mode 100644 index 000000000..9676af7cc --- /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; 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,funds+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/eval.h b/src/cc/eval.h index 0ce5b6bf7..5454cf449 100644 --- a/src/cc/eval.h +++ b/src/cc/eval.h @@ -42,7 +42,10 @@ EVAL(EVAL_ASSETS, 0xe3) \ EVAL(EVAL_FAUCET, 0xe4) \ EVAL(EVAL_REWARDS, 0xe5) \ - EVAL(EVAL_DICE, 0xe6) + EVAL(EVAL_DICE, 0xe6) \ + EVAL(EVAL_PONZI, 0xe7) \ + EVAL(EVAL_AUCTION, 0xe8) \ + EVAL(EVAL_LOTTO, 0xe9) typedef uint8_t EvalCode; diff --git a/src/cc/lotto.cpp b/src/cc/lotto.cpp new file mode 100644 index 000000000..a699ffa5c --- /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; 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,funds+txfee,64) > 0 ) + { + mtx.vout.push_back(MakeCC1vout(EVAL_LOTTO,funds,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..26cf98515 --- /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; 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,funds+txfee,64) > 0 ) + { + mtx.vout.push_back(MakeCC1vout(EVAL_PONZI,funds,ponzipk)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } + return(0); +} + + From eca34fd97846f3e8a03c8fd7375dd8922c47dd5e Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 08:50:42 -1100 Subject: [PATCH 34/42] Test --- src/wallet/rpcwallet.cpp | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 7f6816cee..3c2e45b55 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4844,10 +4844,14 @@ int32_t ensure_CCrequirements() #include "../cc/CCassets.h" #include "../cc/CCrewards.h" #include "../cc/CCdice.h" +#include "../cc/CCponzi.h" +#include "../cc/CCauction.h" +#include "../cc/CClotto.h" UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector &pubkey) { UniValue result(UniValue::VOBJ); ; char destaddr[64],str[64]; + { uint8_t p[32]; Myprivkey(p); } result.push_back(Pair("result", "success")); sprintf(str,"%sCCaddress",name); if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) @@ -4862,6 +4866,45 @@ UniValue CCaddress(struct CCcontract_info *cp,char *name,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_DICE); + 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; From 8ee5c997703c23aa8dcc1e137e9c5dd93500ad02 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 08:54:29 -1100 Subject: [PATCH 35/42] Address functions --- src/cc/CCcustom.cpp | 2 +- src/rpcserver.cpp | 9 +++++++++ src/rpcserver.h | 3 +++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index 7080e3ed5..c03907c4a 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -110,7 +110,7 @@ uint8_t PonziCCpriv[32] = { 0x0e, 0xe8, 0xf5, 0xb4, 0x3d, 0x25, 0xcc, 0x35, 0xd1 #define EVALCODE EVAL_AUCTION const char *AuctionCCaddr = "REabWB7KjFN5C3LFMZ5odExHPenYzHLtVw"; //"RFtVDNmdTZBTNZdmFRbfBgJ6LitgTghikL"; char AuctionCChexstr[67] = { "037eefe050c14cb60ae65d5b2f69eaa1c9006826d729bc0957bdc3024e3ca1dbe6" }; -uint8_t AuctionCCpriv[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 }; +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 diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 40ebd162c..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 }, diff --git a/src/rpcserver.h b/src/rpcserver.h index ff06966ba..fe48bdd74 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -231,6 +231,9 @@ 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 From b935ab65f4b2f0155cee371483b3b90bbe7ab903 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 09:02:15 -1100 Subject: [PATCH 36/42] Fix --- src/cc/CCcustom.cpp | 27 ++++++++++++++++++++++++--- src/wallet/rpcwallet.cpp | 2 +- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index c03907c4a..aab1aa9af 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -88,7 +88,7 @@ uint8_t DiceCCpriv[32] = { 0x0e, 0xe8, 0xf5, 0xb4, 0x3d, 0x25, 0xcc, 0x35, 0xd1, // Lotto #define FUNCNAME IsLottoInput #define EVALCODE EVAL_LOTTO -const char *LottoCCaddr = "REabWB7KjFN5C3LFMZ5odExHPenYzHLtVw"; //"RLW6hhRqBZZMBndnyPv29Yg3krh6iBYCyg"; +const char *LottoCCaddr = "RNXZxgyWSAE6XS3qGnTaf5dVNCxnYzhPrg"; //"RLW6hhRqBZZMBndnyPv29Yg3krh6iBYCyg"; char LottoCChexstr[67] = { "03f72d2c4db440df1e706502b09ca5fec73ffe954ea1883e4049e98da68690d98f" }; uint8_t LottoCCpriv[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" @@ -98,9 +98,9 @@ uint8_t LottoCCpriv[32] = { 0x0e, 0xe8, 0xf5, 0xb4, 0x3d, 0x25, 0xcc, 0x35, 0xd1 // Ponzi #define FUNCNAME IsPonziInput #define EVALCODE EVAL_PONZI -const char *PonziCCaddr = "REabWB7KjFN5C3LFMZ5odExHPenYzHLtVw"; //"RWSHRbxnJYLvDjpcQ2i8MekgP6h2ctTKaj"; +const char *PonziCCaddr = "RUKTbLBeKgHkm3Ss4hKZP3ikuLW1xx7B2x"; //"RWSHRbxnJYLvDjpcQ2i8MekgP6h2ctTKaj"; char PonziCChexstr[67] = { "039b52d294b413b07f3643c1a28c5467901a76562d8b39a785910ae0a0f3043810" }; -uint8_t PonziCCpriv[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 }; +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 @@ -148,6 +148,27 @@ struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode) 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/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 3c2e45b55..f5db1ed35 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4895,7 +4895,7 @@ UniValue ponziaddress(const UniValue& params, bool fHelp) UniValue auctionaddress(const UniValue& params, bool fHelp) { struct CCcontract_info *cp,C; std::vector pubkey; - cp = CCinit(&C,EVAL_DICE); + cp = CCinit(&C,EVAL_AUCTION); if ( fHelp || params.size() > 1 ) throw runtime_error("auctionaddress [pubkey]\n"); if ( ensure_CCrequirements() < 0 ) From 4391153705fe8d2dd9c4cf411ef130e144e6b75a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 09:05:31 -1100 Subject: [PATCH 37/42] Makefile --- src/Makefile.am | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Makefile.am b/src/Makefile.am index 3b7d17e85..a44839781 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -265,6 +265,9 @@ libbitcoin_server_a_SOURCES = \ 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 \ From 90a233e9ace692837e907810ad0f983522666e9a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 09:07:49 -1100 Subject: [PATCH 38/42] Test --- src/cc/lotto.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/lotto.cpp b/src/cc/lotto.cpp index a699ffa5c..079cf5793 100644 --- a/src/cc/lotto.cpp +++ b/src/cc/lotto.cpp @@ -171,7 +171,7 @@ std::string LottoWinner(uint64_t txfee) txfee = 10000; mypk = pubkey2pk(Mypubkey()); Lottopk = GetUnspendable(cp,0); - if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 ) + if ( AddNormalinputs(mtx,mypk,txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(EVAL_LOTTO,funds,Lottopk)); return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); From 372625beb6077f0fec354495522d071331bede31 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 09:09:32 -1100 Subject: [PATCH 39/42] Test --- src/cc/lotto.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/lotto.cpp b/src/cc/lotto.cpp index 079cf5793..af6222b5d 100644 --- a/src/cc/lotto.cpp +++ b/src/cc/lotto.cpp @@ -165,7 +165,7 @@ std::string LottoTicket(uint64_t txfee,uint64_t numtickets) std::string LottoWinner(uint64_t txfee) { - CMutableTransaction mtx; CPubKey mypk,Lottopk; CScript opret; struct CCcontract_info *cp,C; + 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; @@ -173,7 +173,7 @@ std::string LottoWinner(uint64_t txfee) Lottopk = GetUnspendable(cp,0); if ( AddNormalinputs(mtx,mypk,txfee,64) > 0 ) { - mtx.vout.push_back(MakeCC1vout(EVAL_LOTTO,funds,Lottopk)); + mtx.vout.push_back(MakeCC1vout(EVAL_LOTTO,winnings,Lottopk)); return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); } return(0); From b08b67f2115ef3a1aaca0063b4633786ed559f75 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 09:11:32 -1100 Subject: [PATCH 40/42] Test --- src/cc/auction.cpp | 6 +++--- src/cc/ponzi.cpp | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cc/auction.cpp b/src/cc/auction.cpp index 9676af7cc..a11f66e85 100644 --- a/src/cc/auction.cpp +++ b/src/cc/auction.cpp @@ -185,15 +185,15 @@ std::string AuctionDeliver(uint64_t txfee,uint256 itemhash,uint256 bidtxid) std::string AuctionPost(uint64_t txfee,uint256 itemhash,uint64_t minbid,char *title,char *description) { - CMutableTransaction mtx; CPubKey mypk,Auctionpk; CScript opret; struct CCcontract_info *cp,C; + 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,funds+txfee,64) > 0 ) + if ( AddNormalinputs(mtx,mypk,txfee,64) > 0 ) { - //mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,funds,Auctionpk)); + mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,funds,Auctionpk)); return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); } return(0); diff --git a/src/cc/ponzi.cpp b/src/cc/ponzi.cpp index 26cf98515..81021209e 100644 --- a/src/cc/ponzi.cpp +++ b/src/cc/ponzi.cpp @@ -165,13 +165,13 @@ std::string PonziBuy(uint64_t txfee,uint64_t amount) std::string PonziClaim(uint64_t txfee) { - CMutableTransaction mtx; CPubKey mypk,ponzipk; CScript opret; struct CCcontract_info *cp,C; + 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,funds+txfee,64) > 0 ) + if ( AddNormalinputs(mtx,mypk,txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(EVAL_PONZI,funds,ponzipk)); return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); From d3e128d961ef4e6ec5985f8f0ffcf2978887db7d Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 09:15:43 -1100 Subject: [PATCH 41/42] Test --- src/cc/CCcustom.cpp | 4 ++-- src/cc/CCutils.cpp | 2 +- src/wallet/rpcwallet.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index aab1aa9af..db891c583 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -90,7 +90,7 @@ uint8_t DiceCCpriv[32] = { 0x0e, 0xe8, 0xf5, 0xb4, 0x3d, 0x25, 0xcc, 0x35, 0xd1, #define EVALCODE EVAL_LOTTO const char *LottoCCaddr = "RNXZxgyWSAE6XS3qGnTaf5dVNCxnYzhPrg"; //"RLW6hhRqBZZMBndnyPv29Yg3krh6iBYCyg"; char LottoCChexstr[67] = { "03f72d2c4db440df1e706502b09ca5fec73ffe954ea1883e4049e98da68690d98f" }; -uint8_t LottoCCpriv[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 }; +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 @@ -108,7 +108,7 @@ uint8_t PonziCCpriv[32] = { 0x11, 0xe1, 0xea, 0x3e, 0xdb, 0x36, 0xf0, 0xa8, 0xc6 // Auction #define FUNCNAME IsAuctionInput #define EVALCODE EVAL_AUCTION -const char *AuctionCCaddr = "REabWB7KjFN5C3LFMZ5odExHPenYzHLtVw"; //"RFtVDNmdTZBTNZdmFRbfBgJ6LitgTghikL"; +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" diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index e61f91f53..f8617e536 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -216,7 +216,7 @@ bool Myprivkey(uint8_t myprivkey[]) if ( pwalletMain->GetKey(keyID,vchSecret) != 0 ) { memcpy(myprivkey,vchSecret.begin(),32); - if ( 1 ) + if ( 0 ) { for (i=0; i<32; i++) fprintf(stderr,"0x%02x, ",myprivkey[i]); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index f5db1ed35..489976b78 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4851,7 +4851,7 @@ int32_t ensure_CCrequirements() UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector &pubkey) { UniValue result(UniValue::VOBJ); ; char destaddr[64],str[64]; - { uint8_t p[32]; Myprivkey(p); } + // { uint8_t p[32]; Myprivkey(p); } result.push_back(Pair("result", "success")); sprintf(str,"%sCCaddress",name); if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) From 0c6665ad63fe1e3807728d726fbdc4c433650d09 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Jul 2018 22:30:34 -1100 Subject: [PATCH 42/42] fix notary mining --- src/miner.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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");