diff --git a/src/cc/CCdice.h b/src/cc/CCdice.h index 8ecd7addd..007cb7ec9 100644 --- a/src/cc/CCdice.h +++ b/src/cc/CCdice.h @@ -24,7 +24,8 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds); -std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout); +std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout); +std::string DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks); std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount); UniValue DiceInfo(uint256 diceid); diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 312d44a06..cf98251be 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -55,6 +55,8 @@ int32_t is_hexstr(char *str,int32_t n); bool myAddtomempool(CTransaction &tx); //uint64_t myGettxout(uint256 hash,int32_t n); bool myIsutxo_spentinmempool(uint256 txid,int32_t vout); +int32_t myIsutxo_spent(uint256 &spenttxid,uint256 txid,int32_t vout); +bool mySendrawtransaction(std:string res); // CCcustom CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv); diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 220916535..b158bd49a 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -92,9 +92,29 @@ struct dicefinish_info int32_t iswin; }; +bool mySendrawtransaction(std:string res) +{ + CTransaction tx; char str[65]; + if ( res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) + { + if ( DecodeHexTx(tx,res) != 0 ) + { + fprintf(stderr,"%s\n%s\n",res.c_str(),uint256_str(str,tx.GetHash())); + LOCK(cs_main); + if ( myAddtomempool(tx) != 0 ) + { + RelayTransaction(tx); + fprintf(stderr,"added to mempool and broadcast\n"); + return(true); + } else fprintf(stderr,"error adding to mempool\n"); + } else fprintf(stderr,"error decoding hex\n"); + } + return(false); +} + void *dicefinish(void *_ptr) { - char str[65],str2[65],name[32]; std::string res; int32_t i,duplicate=0; struct dicefinish_info *ptr; + char str[65],str2[65],name[32]; std::string res; int32_t i,result,duplicate=0; struct dicefinish_info *ptr; ptr = (struct dicefinish_info *)_ptr; sleep(3); // wait for bettxid to be in mempool for (i=0; ibettxid; } unstringbits(name,ptr->sbits); - fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bet.%s\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bettxid)); + //fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bet.%s\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bettxid)); if ( duplicate == 0 ) { - CTransaction tx,bettx; uint256 txid,hashBlock; char str[65]; int32_t result; res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); - if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) - { - if ( DecodeHexTx(tx,res) != 0 ) - { - fprintf(stderr,"iswin.%d %s\n%s\n",ptr->iswin,res.c_str(),uint256_str(str,tx.GetHash())); - LOCK(cs_main); - if ( myAddtomempool(tx) != 0 ) - { - RelayTransaction(tx); - fprintf(stderr,"added to mempool and broadcast\n"); - } else fprintf(stderr,"error adding to mempool\n"); - } else fprintf(stderr,"error decoding hex\n"); - } + if ( result != 0 ) + mySendrawtransaction(res); } free(ptr); return(0); @@ -621,7 +629,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi if ( funcid != 'F' && funcid != 'T' ) { n++; - fprintf(stderr,"(%c %.8f) ",funcid,(double)nValue/COIN); + fprintf(stderr,"%s.(%c %.8f) ",uint256_str(str,txid),funcid,(double)nValue/COIN); } totalinputs += nValue; if ( first == 0 && (funcid == 'E' || funcid == 'W' || funcid == 'L') ) @@ -885,7 +893,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet return(0); } -std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout) +std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout) { CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin=0; uint64_t entropyval,sbits; *resultp = 0; @@ -914,6 +922,7 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin bettorentropy = DiceGetEntropy(betTx,'B'); if ( winlosetimeout == 0 || (iswin= DiceIsWinner(hentropyproof,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { + winlosetimeout = iswin; if ( iswin == winlosetimeout ) { if ( myIsutxo_spentinmempool(bettxid,0) != 0 || myIsutxo_spentinmempool(bettxid,1) != 0 ) @@ -986,3 +995,47 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin return("0"); } +double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) +{ + CScript fundingPubKey; CTransaction spenttx; uint256 hashBlock,spenttxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int32_t result; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char resultstr[64]; std::string res; + if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) + { + fprintf(stderr,"Diceinit error\n"); + return("0"); + } + fundingpk = DiceFundingPk(fundingPubKey); + scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG; + if ( bettxid == zeroid ) // scan + { + + } + else + { + if ( (vout= myIsutxo_spent(spenttxid,bettxid,1)) >= 0 ) + { + if ( GetTransaction(spenttxid,spenttx,hashBlock,false) != 0 && spenttx.vout.size() > 2 ) + { + if ( spenttx.vout[2].scriptPubKey == fundingPubKey ) + return(0.); + else return((double)spenttx.vout[2].nValue/COIN); + } else return(0.); + } + else if ( scriptPubKey == fundingPubKey ) + res = DiceBetFinish(&result,txfee,planstr,fundingtxid,bettxid,1); + else res = DiceBetFinish(&result,txfee,planstr,fundingtxid,bettxid,0); + if ( result != 0 ) + mySendrawtransaction(res); + sleep(1); + if ( (vout= myIsutxo_spent(spenttxid,bettxid,1)) >= 0 ) + { + if ( GetTransaction(spenttxid,spenttx,hashBlock,false) != 0 && spenttx.vout.size() >= 2 ) + { + if ( spenttx.vout[2].scriptPubKey == fundingPubKey || ((uint8_t *)spenttx.vout[2].scriptPubKey.data())[0] == 0x6a ) + return(0.); + else return((double)spenttx.vout[2].nValue/COIN); + } else return(0.); + } + fprintf(stderr,"didnt find dicefinish tx\n"); + } + return(0.); +} diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index bedb84383..b76698917 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -348,8 +348,7 @@ UniValue getdifficulty(const UniValue& params, bool fHelp) bool myIsutxo_spentinmempool(uint256 txid,int32_t vout) { - char *uint256_str(char *str,uint256); - char str[65]; + //char *uint256_str(char *str,uint256); char str[65]; LOCK(mempool.cs); BOOST_FOREACH(const CTxMemPoolEntry &e,mempool.mapTx) { @@ -357,11 +356,11 @@ bool myIsutxo_spentinmempool(uint256 txid,int32_t vout) const uint256 &hash = tx.GetHash(); BOOST_FOREACH(const CTxIn &txin,tx.vin) { - fprintf(stderr,"%s/v%d ",uint256_str(str,txin.prevout.hash),txin.prevout.n); + //fprintf(stderr,"%s/v%d ",uint256_str(str,txin.prevout.hash),txin.prevout.n); if ( txin.prevout.n == vout && txin.prevout.hash == txid ) return(true); } - fprintf(stderr,"are vins for %s\n",uint256_str(str,hash)); + //fprintf(stderr,"are vins for %s\n",uint256_str(str,hash)); } return(false); } diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp index fa4118294..4c7488d02 100644 --- a/src/rpcrawtransaction.cpp +++ b/src/rpcrawtransaction.cpp @@ -119,6 +119,19 @@ UniValue TxJoinSplitToJSON(const CTransaction& tx) { uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 hash,int32_t n,int32_t checkheight,uint64_t checkvalue,int32_t tipheight); +int32_t myIsutxo_spent(uint256 &spenttxid,uint256 txid,int32_t vout) +{ + CSpentIndexValue spentInfo; CSpentIndexKey spentKey(txid,vout); + if ( GetSpentIndex(spentKey,spentInfo) ) + { + spenttxid = spentInfo.txid.GetHex(); + return((int32_t)spentInfo.inputIndex)); + // out.push_back(Pair("spentHeight", spentInfo.blockHeight)); + } + memset(&spenttxid,0,sizeof(spenttxid)); + return(-1); +} + void TxToJSONExpanded(const CTransaction& tx, const uint256 hashBlock, UniValue& entry, int nHeight = 0, int nConfirmations = 0, int nBlockTime = 0) { uint256 txid = tx.GetHash(); diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 3e92ab7b8..7b410babd 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -377,9 +377,8 @@ static const CRPCCommand vRPCCommands[] = { "dice", "dicefund", &dicefund, true }, { "dice", "diceaddfunds", &diceaddfunds, true }, { "dice", "dicebet", &dicebet, true }, - { "dice", "dicewinner", &dicewinner, true }, - { "dice", "diceloser", &diceloser, true }, - { "dice", "dicetimeout", &dicetimeout, true }, + { "dice", "dicefinish", &dicewinner, true }, + { "dice", "dicestatus", &diceloser, true }, { "dice", "diceaddress", &diceaddress, true }, /* tokens */ diff --git a/src/rpcserver.h b/src/rpcserver.h index 3e25fc4cc..4b5dcdc2c 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -240,9 +240,8 @@ extern UniValue dicelist(const UniValue& params, bool fHelp); extern UniValue diceinfo(const UniValue& params, bool fHelp); extern UniValue diceaddfunds(const UniValue& params, bool fHelp); extern UniValue dicebet(const UniValue& params, bool fHelp); -extern UniValue dicewinner(const UniValue& params, bool fHelp); -extern UniValue diceloser(const UniValue& params, bool fHelp); -extern UniValue dicetimeout(const UniValue& params, bool fHelp); +extern UniValue dicefinish(const UniValue& params, bool fHelp); +extern UniValue dicestatus(const UniValue& params, bool fHelp); extern UniValue lottoaddress(const UniValue& params, bool fHelp); extern UniValue FSMaddress(const UniValue& params, bool fHelp); extern UniValue FSMcreate(const UniValue& params, bool fHelp); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index b403c6ac5..58d790275 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5238,11 +5238,11 @@ UniValue dicebet(const UniValue& params, bool fHelp) return(result); } -UniValue dicewinner(const UniValue& params, bool fHelp) +UniValue dicefinish(const UniValue& params, bool fHelp) { UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t r; if ( fHelp || params.size() != 3 ) - throw runtime_error("dicewinner name fundingtxid bettxid\n"); + throw runtime_error("dicefinish name fundingtxid bettxid\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"); const CKeyStore& keystore = *pwalletMain; @@ -5250,20 +5250,20 @@ UniValue dicewinner(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinLoseTimeout(&r,0,name,fundingtxid,bettxid,1); + hex = DiceBetFinish(&r,0,name,fundingtxid,bettxid,1); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); result.push_back(Pair("hex", hex)); - } else result.push_back(Pair("error", "couldnt create dicewinner transaction")); + } else result.push_back(Pair("error", "couldnt create dicefinish transaction")); return(result); } -UniValue diceloser(const UniValue& params, bool fHelp) +UniValue dicestatus(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t r; + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string status; int32_t r; double winnings; if ( fHelp || params.size() != 3 ) - throw runtime_error("diceloser name fundingtxid bettxid\n"); + throw runtime_error("dicestatus name fundingtxid bettxid\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"); const CKeyStore& keystore = *pwalletMain; @@ -5271,33 +5271,13 @@ UniValue diceloser(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinLoseTimeout(&r,0,name,fundingtxid,bettxid,-1); - if ( hex.size() > 0 ) + winnings = DiceStatus(&r,0,name,fundingtxid,bettxid); + result.push_back(Pair("result", "success")); + if ( winnings != 0. ) { - result.push_back(Pair("result", "success")); - result.push_back(Pair("hex", hex)); - } else result.push_back(Pair("error", "couldnt create diceloser transaction")); - return(result); -} - -UniValue dicetimeout(const UniValue& params, bool fHelp) -{ - UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t r; - if ( fHelp || params.size() != 3 ) - throw runtime_error("dicetimeout name fundingtxid bettxid\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"); - const CKeyStore& keystore = *pwalletMain; - LOCK2(cs_main, pwalletMain->cs_wallet); - name = (char *)params[0].get_str().c_str(); - fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); - bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinLoseTimeout(&r,0,name,fundingtxid,bettxid,0); - if ( hex.size() > 0 ) - { - result.push_back(Pair("result", "success")); - result.push_back(Pair("hex", hex)); - } else result.push_back(Pair("error", "couldnt create dicetimeout transaction")); + result.push_back(Pair("status", "win")); + result.push_back(Pair("won", winnings)); + } else result.push_back(Pair("status", "loss")); return(result); }