Lots more error checking in rewards and dice

This commit is contained in:
Jonathan "Duke" Leto
2018-08-22 09:26:37 +02:00
parent d2b767f71d
commit a03146b340
4 changed files with 89 additions and 26 deletions

View File

@@ -6,7 +6,7 @@
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.authproxy import JSONRPCException from test_framework.authproxy import JSONRPCException
from test_framework.util import assert_equal, assert_greater_than, \ from test_framework.util import assert_equal, assert_greater_than, \
initialize_chain_clean, start_nodes, start_node, connect_nodes_bi, \ initialize_chain_clean, initialize_chain, start_nodes, start_node, connect_nodes_bi, \
stop_nodes, sync_blocks, sync_mempools, wait_bitcoinds, rpc_port stop_nodes, sync_blocks, sync_mempools, wait_bitcoinds, rpc_port
import time import time
@@ -327,10 +327,10 @@ class CryptoConditionsTest (BitcoinTestFramework):
print("Importing privkey") print("Importing privkey")
rpc.importprivkey(self.privkey) rpc.importprivkey(self.privkey)
self.run_faucet_tests()
self.run_rewards_tests() self.run_rewards_tests()
self.run_dice_tests() self.run_dice_tests()
self.run_token_tests() self.run_token_tests()
self.run_faucet_tests()
if __name__ == '__main__': if __name__ == '__main__':

View File

@@ -15,6 +15,8 @@
#include "CCdice.h" #include "CCdice.h"
extern std::string CCerror;
// timeout // timeout
/* /*
@@ -949,7 +951,8 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256
//char str[65]; fprintf(stderr,"DiceBetFinish.%s %s\n",planstr,uint256_str(str,bettxid)); //char str[65]; fprintf(stderr,"DiceBetFinish.%s %s\n",planstr,uint256_str(str,bettxid));
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 )
{ {
fprintf(stderr,"Diceinit error\n"); CCerror = "Diceinit error";
fprintf(stderr,"%s\n", CCerror.c_str() );
return(""); return("");
} }
fundingpk = DiceFundingPk(fundingPubKey); fundingpk = DiceFundingPk(fundingPubKey);
@@ -964,7 +967,8 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256
} }
if ( AddNormalinputs(mtx,mypk,txfee,1) == 0 ) if ( AddNormalinputs(mtx,mypk,txfee,1) == 0 )
{ {
fprintf(stderr,"no txfee inputs for win/lose\n"); CCerror = "no txfee inputs for win/lose";
fprintf(stderr,"%s\n", CCerror.c_str() );
return(""); return("");
} }
if ( GetTransaction(bettxid,betTx,hashBlock,false) != 0 && GetTransaction(betTx.vin[0].prevout.hash,entropyTx,hashBlock,false) != 0 ) if ( GetTransaction(bettxid,betTx,hashBlock,false) != 0 && GetTransaction(betTx.vin[0].prevout.hash,entropyTx,hashBlock,false) != 0 )
@@ -978,7 +982,8 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256
{ {
if ( myIsutxo_spentinmempool(bettxid,0) != 0 || myIsutxo_spentinmempool(bettxid,1) != 0 ) if ( myIsutxo_spentinmempool(bettxid,0) != 0 || myIsutxo_spentinmempool(bettxid,1) != 0 )
{ {
fprintf(stderr,"bettxid already spent\n"); CCerror = "bettxid already spent";
fprintf(stderr,"%s\n", CCerror.c_str() );
return(""); return("");
} }
//fprintf(stderr,"iswin.%d matches\n",iswin); //fprintf(stderr,"iswin.%d matches\n",iswin);
@@ -1056,7 +1061,8 @@ double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettx
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 )
{ {
fprintf(stderr,"Diceinit error\n"); CCerror = "Diceinit error";
fprintf(stderr,"%s\n", CCerror.c_str() );
return(0.); return(0.);
} }
fundingpk = DiceFundingPk(fundingPubKey); fundingpk = DiceFundingPk(fundingPubKey);

View File

@@ -539,7 +539,8 @@ std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,i
sbits = stringbits(planstr); sbits = stringbits(planstr);
if ( RewardsPlanExists(cp,sbits,rewardspk,a,b,c,d) == 0 ) if ( RewardsPlanExists(cp,sbits,rewardspk,a,b,c,d) == 0 )
{ {
fprintf(stderr,"Rewards plan %s doesnt exist\n",planstr); CCerror = strprintf("Rewards plan %s doesnt exist\n",planstr);
fprintf(stderr,"%s\n",CCerror.c_str());
return(""); return("");
} }
sbits = stringbits(planstr); sbits = stringbits(planstr);
@@ -547,8 +548,12 @@ std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,i
{ {
mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,rewardspk)); mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,rewardspk));
return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeRewardsOpRet('A',sbits,fundingtxid))); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeRewardsOpRet('A',sbits,fundingtxid)));
} else fprintf(stderr,"cant find enough inputs\n"); } else {
fprintf(stderr,"cant find fundingtxid\n"); CCerror = "cant find enough inputs";
fprintf(stderr,"%s\n", CCerror.c_str());
}
CCerror = "cant find fundingtxid";
fprintf(stderr,"%s\n", CCerror.c_str());
return(""); return("");
} }
@@ -557,7 +562,8 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t
CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t lockedfunds,sbits,funding,APR,minseconds,maxseconds,mindeposit; struct CCcontract_info *cp,C; CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t lockedfunds,sbits,funding,APR,minseconds,maxseconds,mindeposit; struct CCcontract_info *cp,C;
if ( deposit < txfee ) if ( deposit < txfee )
{ {
fprintf(stderr,"deposit amount less than txfee\n"); CCerror = "deposit amount less than txfee";
fprintf(stderr,"%s\n",CCerror.c_str());
return(""); return("");
} }
cp = CCinit(&C,EVAL_REWARDS); cp = CCinit(&C,EVAL_REWARDS);
@@ -568,12 +574,14 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t
sbits = stringbits(planstr); sbits = stringbits(planstr);
if ( RewardsPlanExists(cp,sbits,rewardspk,APR,minseconds,maxseconds,mindeposit) == 0 ) if ( RewardsPlanExists(cp,sbits,rewardspk,APR,minseconds,maxseconds,mindeposit) == 0 )
{ {
fprintf(stderr,"Rewards plan %s doesnt exist\n",planstr); CCerror = strprintf("Rewards plan %s doesnt exist\n",planstr);
fprintf(stderr,"%s\n",CCerror.c_str());
return(""); return("");
} }
if ( deposit < mindeposit ) if ( deposit < mindeposit )
{ {
fprintf(stderr,"Rewards plan %s deposit %.8f < mindeposit %.8f\n",planstr,(double)deposit/COIN,(double)mindeposit/COIN); CCerror = strprintf("Rewards plan %s deposit %.8f < mindeposit %.8f\n",planstr,(double)deposit/COIN,(double)mindeposit/COIN);
fprintf(stderr,"%s\n",CCerror.c_str());
return(""); return("");
} }
if ( (funding= RewardsPlanFunds(lockedfunds,sbits,cp,rewardspk,fundingtxid)) >= deposit ) // arbitrary cmpval if ( (funding= RewardsPlanFunds(lockedfunds,sbits,cp,rewardspk,fundingtxid)) >= deposit ) // arbitrary cmpval
@@ -583,7 +591,10 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t
mtx.vout.push_back(MakeCC1vout(cp->evalcode,deposit,rewardspk)); mtx.vout.push_back(MakeCC1vout(cp->evalcode,deposit,rewardspk));
mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG));
return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeRewardsOpRet('L',sbits,fundingtxid))); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeRewardsOpRet('L',sbits,fundingtxid)));
} else fprintf(stderr,"cant find enough inputs %.8f not enough for %.8f, make sure you imported privkey for the -pubkey address\n",(double)funding/COIN,(double)deposit/COIN); } else {
CCerror = strprintf("cant find enough inputs %.8f not enough for %.8f, make sure you imported privkey for the -pubkey address\n",(double)funding/COIN,(double)deposit/COIN);
fprintf(stderr,"%s\n",CCerror.c_str());
}
} }
fprintf(stderr,"cant find rewards inputs funding %.8f locked %.8f vs deposit %.8f\n",(double)funding/COIN,(double)lockedfunds/COIN,(double)deposit/COIN); fprintf(stderr,"cant find rewards inputs funding %.8f locked %.8f vs deposit %.8f\n",(double)funding/COIN,(double)lockedfunds/COIN,(double)deposit/COIN);
return(""); return("");

View File

@@ -4968,7 +4968,7 @@ UniValue tokenaddress(const UniValue& params, bool fHelp)
UniValue rewardscreatefunding(const UniValue& params, bool fHelp) UniValue rewardscreatefunding(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); char *name; uint64_t funds,APR,minseconds,maxseconds,mindeposit; std::string hex; UniValue result(UniValue::VOBJ); char *name; int64_t funds,APR,minseconds,maxseconds,mindeposit; std::string hex;
if ( fHelp || params.size() > 6 || params.size() < 2 ) if ( fHelp || params.size() > 6 || params.size() < 2 )
throw runtime_error("rewardscreatefunding name amount APR mindays maxdays mindeposit\n"); throw runtime_error("rewardscreatefunding name amount APR mindays maxdays mindeposit\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
@@ -4981,17 +4981,43 @@ UniValue rewardscreatefunding(const UniValue& params, bool fHelp)
mindeposit = 100 * COIN; mindeposit = 100 * COIN;
name = (char *)params[0].get_str().c_str(); name = (char *)params[0].get_str().c_str();
funds = atof(params[1].get_str().c_str()) * COIN; funds = atof(params[1].get_str().c_str()) * COIN;
if ( funds <= 0 ) {
ERR_RESULT("funds must be positive");
return result;
}
if ( params.size() > 2 ) if ( params.size() > 2 )
{ {
APR = atof(params[2].get_str().c_str()) * COIN; APR = atof(params[2].get_str().c_str()) * COIN;
if ( APR > REWARDSCC_MAXAPR )
{
ERR_RESULT("25% APR is maximum");
return result;
}
if ( params.size() > 3 ) if ( params.size() > 3 )
{ {
minseconds = atol(params[3].get_str().c_str()) * 3600 * 24; minseconds = atol(params[3].get_str().c_str()) * 3600 * 24;
if ( minseconds < 0 ) {
ERR_RESULT("mindays must be non-negative");
return result;
}
if ( params.size() > 4 ) if ( params.size() > 4 )
{ {
maxseconds = atol(params[4].get_str().c_str()) * 3600 * 24; maxseconds = atol(params[4].get_str().c_str()) * 3600 * 24;
if ( maxseconds <= 0 ) {
ERR_RESULT("maxdays must be positive");
return result;
}
if ( maxseconds < minseconds ) {
ERR_RESULT("maxdays must be greater than mindays");
return result;
}
if ( params.size() > 5 ) if ( params.size() > 5 )
mindeposit = atof(params[5].get_str().c_str()) * COIN; mindeposit = atof(params[5].get_str().c_str()) * COIN;
if ( mindeposit <= 0 ) {
ERR_RESULT("mindeposit must be positive");
return result;
}
} }
} }
} }
@@ -5017,7 +5043,9 @@ UniValue rewardslock(const UniValue& params, bool fHelp)
fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); fundingtxid = Parseuint256((char *)params[1].get_str().c_str());
amount = atof(params[2].get_str().c_str()) * COIN; amount = atof(params[2].get_str().c_str()) * COIN;
hex = RewardsLock(0,name,fundingtxid,amount); hex = RewardsLock(0,name,fundingtxid,amount);
if ( amount > 0 ) { if ( CCerror != "" ){
ERR_RESULT(CCerror);
} else if ( amount > 0 ) {
if ( hex.size() > 0 ) if ( hex.size() > 0 )
{ {
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
@@ -5040,7 +5068,9 @@ UniValue rewardsaddfunding(const UniValue& params, bool fHelp)
fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); fundingtxid = Parseuint256((char *)params[1].get_str().c_str());
amount = atof(params[2].get_str().c_str()) * COIN; amount = atof(params[2].get_str().c_str()) * COIN;
hex = RewardsAddfunding(0,name,fundingtxid,amount); hex = RewardsAddfunding(0,name,fundingtxid,amount);
if (amount > 0) { if (CCerror != "") {
ERR_RESULT(CCerror);
} else if (amount > 0) {
if ( hex.size() > 0 ) if ( hex.size() > 0 )
{ {
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
@@ -5050,8 +5080,7 @@ UniValue rewardsaddfunding(const UniValue& params, bool fHelp)
result.push_back(Pair("error", "couldnt create rewards addfunding transaction")); result.push_back(Pair("error", "couldnt create rewards addfunding transaction"));
} }
} else { } else {
result.push_back(Pair("result", "error")); ERR_RESULT("funding amount must be positive");
result.push_back(Pair("error", "funding amount must be positive"));
} }
return(result); return(result);
} }
@@ -5260,7 +5289,7 @@ UniValue dicebet(const UniValue& params, bool fHelp)
{ {
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 faucet get transaction"); } else ERR_RESULT("couldnt create dice bet transaction");
} else { } else {
ERR_RESULT("amount and odds must be positive"); ERR_RESULT("amount and odds must be positive");
} }
@@ -5280,7 +5309,10 @@ UniValue dicefinish(const UniValue& params, bool fHelp)
fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); fundingtxid = Parseuint256((char *)params[1].get_str().c_str());
bettxid = Parseuint256((char *)params[2].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str());
hex = DiceBetFinish(&r,0,name,fundingtxid,bettxid,1); hex = DiceBetFinish(&r,0,name,fundingtxid,bettxid,1);
if ( hex.size() > 0 ) if ( CCerror != "" )
{
ERR_RESULT(CCerror);
} else 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));
@@ -5303,6 +5335,10 @@ UniValue dicestatus(const UniValue& params, bool fHelp)
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);
if (CCerror != "") {
ERR_RESULT(CCerror);
return result;
}
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
if ( winnings >= 0. ) if ( winnings >= 0. )
{ {
@@ -5528,7 +5564,7 @@ UniValue tokencancelbid(const UniValue& params, bool fHelp)
UniValue tokenfillbid(const UniValue& params, bool fHelp) UniValue tokenfillbid(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); uint64_t fillamount; std::string hex; uint256 tokenid,bidtxid; UniValue result(UniValue::VOBJ); int64_t fillamount; std::string hex; uint256 tokenid,bidtxid;
if ( fHelp || params.size() != 3 ) if ( fHelp || params.size() != 3 )
throw runtime_error("tokenfillbid tokenid bidtxid fillamount\n"); throw runtime_error("tokenfillbid tokenid bidtxid fillamount\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
@@ -5538,9 +5574,14 @@ UniValue tokenfillbid(const UniValue& params, bool fHelp)
tokenid = Parseuint256((char *)params[0].get_str().c_str()); tokenid = Parseuint256((char *)params[0].get_str().c_str());
bidtxid = Parseuint256((char *)params[1].get_str().c_str()); bidtxid = Parseuint256((char *)params[1].get_str().c_str());
fillamount = atol(params[2].get_str().c_str()); fillamount = atol(params[2].get_str().c_str());
if ( tokenid == zeroid || bidtxid == zeroid || fillamount <= 0 ) if ( fillamount <= 0 )
{ {
result.push_back(Pair("error", "invalid parameter")); ERR_RESULT("fillamount must be positive");
return(result);
}
if ( tokenid == zeroid || bidtxid == zeroid )
{
ERR_RESULT("must provide tokenid and bidtxid");
return(result); return(result);
} }
hex = FillBuyOffer(0,tokenid,bidtxid,fillamount); hex = FillBuyOffer(0,tokenid,bidtxid,fillamount);
@@ -5567,7 +5608,7 @@ UniValue tokenask(const UniValue& params, bool fHelp)
askamount = (price * numtokens) * COIN + 0.0000000049999; askamount = (price * numtokens) * COIN + 0.0000000049999;
if ( tokenid == zeroid || numtokens <= 0 || price <= 0 || askamount <= 0 ) if ( tokenid == zeroid || numtokens <= 0 || price <= 0 || askamount <= 0 )
{ {
result.push_back(Pair("error", "invalid parameter")); ERR_RESULT("invalid parameter");
return(result); return(result);
} }
hex = CreateSell(0,numtokens,tokenid,askamount); hex = CreateSell(0,numtokens,tokenid,askamount);
@@ -5638,7 +5679,7 @@ UniValue tokencancelask(const UniValue& params, bool fHelp)
UniValue tokenfillask(const UniValue& params, bool fHelp) UniValue tokenfillask(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); uint64_t fillunits; std::string hex; uint256 tokenid,asktxid; UniValue result(UniValue::VOBJ); int64_t fillunits; std::string hex; uint256 tokenid,asktxid;
if ( fHelp || params.size() != 3 ) if ( fHelp || params.size() != 3 )
throw runtime_error("tokenfillask tokenid asktxid fillunits\n"); throw runtime_error("tokenfillask tokenid asktxid fillunits\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
@@ -5648,7 +5689,12 @@ UniValue tokenfillask(const UniValue& params, bool fHelp)
tokenid = Parseuint256((char *)params[0].get_str().c_str()); tokenid = Parseuint256((char *)params[0].get_str().c_str());
asktxid = Parseuint256((char *)params[1].get_str().c_str()); asktxid = Parseuint256((char *)params[1].get_str().c_str());
fillunits = atol(params[2].get_str().c_str()); fillunits = atol(params[2].get_str().c_str());
if ( tokenid == zeroid || asktxid == zeroid || fillunits <= 0 ) if ( fillunits <= 0 )
{
ERR_RESULT("fillunits must be positive");
return(result);
}
if ( tokenid == zeroid || asktxid == zeroid )
{ {
result.push_back(Pair("error", "invalid parameter")); result.push_back(Pair("error", "invalid parameter"));
return(result); return(result);