Merge pull request #918 from blackjok3rtt/FSM

Dice bet/status error messages, and possible fix for fork issue.
This commit is contained in:
jl777
2018-10-27 21:14:34 -11:00
committed by GitHub
4 changed files with 598 additions and 586 deletions

View File

@@ -23,9 +23,9 @@
bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); 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 DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds,std::string &error);
std::string DiceBetFinish(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);
double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,std::string &error);
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 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); std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount);
UniValue DiceInfo(uint256 diceid); UniValue DiceInfo(uint256 diceid);

View File

@@ -440,9 +440,21 @@ bool DiceVerifyTimeout(CTransaction &betTx,int32_t timeoutblocks)
return(numblocks >= timeoutblocks); return(numblocks >= timeoutblocks);
} }
bool GetBlock(uint256 hash, CBlockIndex& blockIdx)
{
auto r = mapBlockIndex.find(hash);
if (r != mapBlockIndex.end()) {
blockIdx = *r->second;
return true;
}
fprintf(stderr, "CC Eval Error: Can't get block from index\n");
return false;
}
bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx)
{ {
uint256 txid,fundingtxid,vinfundingtxid,vinhentropy,vinproof,hashBlock,hash,proof,entropy; int64_t minbet,maxbet,maxodds,timeoutblocks,odds,winnings; uint64_t vinsbits,sbits,amount,inputs,outputs,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,iswin; uint8_t funcid; CScript fundingPubKey; CTransaction fundingTx,vinTx,vinofvinTx; char CCaddr[64]; uint256 txid,fundingtxid,vinfundingtxid,vinhentropy,vinproof,hashBlock,hash,proof,entropy; int64_t minbet,maxbet,maxodds,timeoutblocks,odds,winnings; uint64_t vinsbits,sbits,amount,inputs,outputs,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,iswin; uint8_t funcid; CScript fundingPubKey; CTransaction fundingTx,vinTx,vinofvinTx; char CCaddr[64];
CBlockIndex block;
numvins = tx.vin.size(); numvins = tx.vin.size();
numvouts = tx.vout.size(); numvouts = tx.vout.size();
preventCCvins = preventCCvouts = -1; preventCCvins = preventCCvouts = -1;
@@ -546,8 +558,10 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx)
return eval->Invalid("vin0 or vin1 normal vin for bet"); return eval->Invalid("vin0 or vin1 normal vin for bet");
else if ( tx.vin[1].prevout.hash != tx.vin[2].prevout.hash ) else if ( tx.vin[1].prevout.hash != tx.vin[2].prevout.hash )
return eval->Invalid("vin0 != vin1 prevout.hash for bet"); return eval->Invalid("vin0 != vin1 prevout.hash for bet");
else if ( eval->GetTxUnconfirmed(tx.vin[1].prevout.hash,vinTx,hashBlock) == 0 ) else if ( eval->GetTxUnconfirmed(tx.vin[1].prevout.hash,vinTx,hashBlock) == 0 ) {
return eval->Invalid("always should find vin.0, but didnt for wlt"); if (hashBlock.IsNull() || !GetBlock(hashBlock, block))
return eval->Invalid("always should find vin.0, but didnt for wlt");
}
else if ( vinTx.vout.size() < 3 || DecodeDiceOpRet(tx.vin[1].prevout.hash,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,vinhentropy,vinproof) != 'B' ) else if ( vinTx.vout.size() < 3 || DecodeDiceOpRet(tx.vin[1].prevout.hash,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,vinhentropy,vinproof) != 'B' )
return eval->Invalid("not betTx for vin0/1 for wlt"); return eval->Invalid("not betTx for vin0/1 for wlt");
else if ( sbits != vinsbits || fundingtxid != vinfundingtxid ) else if ( sbits != vinsbits || fundingtxid != vinfundingtxid )
@@ -931,36 +945,36 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6
return(""); return("");
} }
std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds) std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds, std::string &error)
{ {
CMutableTransaction mtx; CScript fundingPubKey; CPubKey mypk,dicepk; uint64_t sbits,entropyval; int64_t funding,minbet,maxbet,maxodds,timeoutblocks; uint256 entropytxid,entropy,hentropy; struct CCcontract_info *cp,C; CMutableTransaction mtx; CScript fundingPubKey; CPubKey mypk,dicepk; uint64_t sbits,entropyval; int64_t funding,minbet,maxbet,maxodds,timeoutblocks; uint256 entropytxid,entropy,hentropy; struct CCcontract_info *cp,C;
if ( bet < 0 ) if ( bet < 0 )
{ {
CCerror = "bet must be positive"; error = "bet must be positive";
fprintf(stderr,"%s\n", CCerror.c_str() );
return(""); return("");
} }
if ( odds < 1 || odds > 9999 ) if ( odds < 1 || odds > 9999 )
{ {
CCerror = "odds must be between 1 and 9999"; error = "odds must be between 1 and 9999";
fprintf(stderr,"%s\n", CCerror.c_str() );
return(""); return("");
} }
if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 )
return(""); return("");
if ( bet < minbet || bet > maxbet || odds > maxodds ) if ( bet < minbet || bet > maxbet || odds > maxodds )
{ {
CCerror = strprintf("Dice plan %s illegal bet %.8f: minbet %.8f maxbet %.8f or odds %d vs max.%d\n",planstr,(double)bet/COIN,(double)minbet/COIN,(double)maxbet/COIN,(int32_t)odds,(int32_t)maxodds); error = strprintf("Dice plan %s illegal bet %.8f: minbet %.8f maxbet %.8f or odds %d vs max.%d\n",planstr,(double)bet/COIN,(double)minbet/COIN,(double)maxbet/COIN,(int32_t)odds,(int32_t)maxodds);
fprintf(stderr,"%s\n", CCerror.c_str() );
return(""); return("");
} }
int32_t entropytxs; int32_t entropytxs;
if ( (funding= DicePlanFunds(entropyval,entropytxid,sbits,cp,dicepk,fundingtxid,entropytxs)) >= 2*bet*odds+txfee && entropyval != 0 ) if ( (funding= DicePlanFunds(entropyval,entropytxid,sbits,cp,dicepk,fundingtxid,entropytxs)) >= 2*bet*odds+txfee && entropyval != 0 )
{ {
if ( entropytxs < 2 ) {
error = "Your dealer is broke, find a new casino.";
return("");
}
if ( myIsutxo_spentinmempool(entropytxid,0) != 0 ) if ( myIsutxo_spentinmempool(entropytxid,0) != 0 )
{ {
CCerror = "entropy txid is spent"; error = "entropy txid is spent";
fprintf(stderr,"%s\n", CCerror.c_str() );
return(""); return("");
} }
mtx.vin.push_back(CTxIn(entropytxid,0,CScript())); mtx.vin.push_back(CTxIn(entropytxid,0,CScript()));
@@ -971,13 +985,12 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet
mtx.vout.push_back(MakeCC1vout(cp->evalcode,bet,dicepk)); mtx.vout.push_back(MakeCC1vout(cp->evalcode,bet,dicepk));
mtx.vout.push_back(CTxOut(txfee+odds,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); mtx.vout.push_back(CTxOut(txfee+odds,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG));
return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('B',sbits,fundingtxid,entropy,zeroid))); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('B',sbits,fundingtxid,entropy,zeroid)));
} else fprintf(stderr,"cant find enough normal inputs for %.8f, plan funding %.8f\n",(double)bet/COIN,(double)funding/COIN); } else error = "cant find enough normal inputs for %.8f, plan funding %.8f\n";
} }
if ( entropyval == 0 && funding != 0 ) if ( entropyval == 0 && funding != 0 )
CCerror = "cant find dice entropy inputs"; error = "cant find dice entropy inputs";
else else
CCerror = "cant find dice input"; error = "cant find dice input";
fprintf(stderr,"%s\n", CCerror.c_str() );
return(""); return("");
} }
@@ -1096,13 +1109,12 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256
return(""); return("");
} }
double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,std::string &error)
{ {
CScript fundingPubKey,scriptPubKey; CTransaction spenttx,betTx; uint256 hash,proof,txid,hashBlock,spenttxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int32_t i,result,vout,n=0; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char coinaddr[64]; std::string res; CScript fundingPubKey,scriptPubKey; CTransaction spenttx,betTx; uint256 hash,proof,txid,hashBlock,spenttxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int32_t i,result,vout,n=0; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char coinaddr[64]; std::string res;
if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 )
{ {
CCerror = "Diceinit error in status"; error = "Diceinit error in status";
fprintf(stderr,"%s\n", CCerror.c_str() );
return(0.); return(0.);
} }
fundingpk = DiceFundingPk(fundingPubKey); fundingpk = DiceFundingPk(fundingPubKey);
@@ -1153,8 +1165,7 @@ double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettx
return(0.); return(0.);
else return((double)spenttx.vout[2].nValue/COIN); else return((double)spenttx.vout[2].nValue/COIN);
} }
CCerror = "couldnt find bettx or spenttx %s\n",uint256_str(str,spenttxid); error = "couldnt find bettx or spenttx %s\n",uint256_str(str,spenttxid);
fprintf(stderr,"%s\n", CCerror.c_str());
return(-1.); return(-1.);
} }
else if ( scriptPubKey == fundingPubKey ) else if ( scriptPubKey == fundingPubKey )
@@ -1174,8 +1185,7 @@ double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettx
else return((double)spenttx.vout[2].nValue/COIN); else return((double)spenttx.vout[2].nValue/COIN);
} else return(0.); } else return(0.);
} }
CCerror = "didnt find dicefinish tx"; error = "didnt find dicefinish tx";
fprintf(stderr,"%s\n", CCerror.c_str());
} }
return(-1.); return(-1.);
} }

View File

@@ -6135,7 +6135,7 @@ UniValue diceaddfunds(const UniValue& params, bool fHelp)
UniValue dicebet(const UniValue& params, bool fHelp) UniValue dicebet(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); std::string hex; uint256 fundingtxid; int64_t amount,odds; char *name; UniValue result(UniValue::VOBJ); std::string hex,error; uint256 fundingtxid; int64_t amount,odds; char *name;
if ( fHelp || params.size() != 4 ) if ( fHelp || params.size() != 4 )
throw runtime_error("dicebet name fundingtxid amount odds\n"); throw runtime_error("dicebet name fundingtxid amount odds\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
@@ -6152,12 +6152,14 @@ UniValue dicebet(const UniValue& params, bool fHelp)
return(result); return(result);
} }
if (amount > 0 && odds > 0) { if (amount > 0 && odds > 0) {
hex = DiceBet(0,name,fundingtxid,amount,odds); hex = DiceBet(0,name,fundingtxid,amount,odds,error);
if ( hex.size() > 0 ) if ( hex.size() > 0 )
{ {
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex)); result.push_back(Pair("hex", hex));
} else ERR_RESULT("couldnt create dice bet transaction. make sure your address has funds"); } else if ( error[0] != 0 ) {
ERR_RESULT(error);
}
} else { } else {
ERR_RESULT("amount and odds must be positive"); ERR_RESULT("amount and odds must be positive");
} }
@@ -6194,7 +6196,7 @@ UniValue dicefinish(const UniValue& params, bool fHelp)
UniValue dicestatus(const UniValue& params, bool fHelp) UniValue dicestatus(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; std::string status; double winnings; UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; std::string status,error; double winnings;
if ( fHelp || (params.size() != 2 && params.size() != 3) ) if ( fHelp || (params.size() != 2 && params.size() != 3) )
throw runtime_error("dicestatus name fundingtxid bettxid\n"); throw runtime_error("dicestatus name fundingtxid bettxid\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
@@ -6210,10 +6212,10 @@ UniValue dicestatus(const UniValue& params, bool fHelp)
memset(&bettxid,0,sizeof(bettxid)); memset(&bettxid,0,sizeof(bettxid));
if ( params.size() == 3 ) if ( params.size() == 3 )
bettxid = Parseuint256((char *)params[2].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str());
winnings = DiceStatus(0,name,fundingtxid,bettxid); winnings = DiceStatus(0,name,fundingtxid,bettxid,error);
if (CCerror != "") { if ( error[0] != 0 ) {
ERR_RESULT(CCerror); ERR_RESULT(error);
return result; return(result);
} }
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
if ( winnings >= 0. ) if ( winnings >= 0. )