More token tests and more error checking and better errors from internals

This commit is contained in:
Jonathan "Duke" Leto
2018-08-16 17:17:54 +02:00
parent 8e0ff2b732
commit 8a3e1884d6
3 changed files with 86 additions and 66 deletions

View File

@@ -12,6 +12,12 @@ from test_framework.util import assert_equal, assert_greater_than, \
import time import time
from decimal import Decimal from decimal import Decimal
def assert_success(result):
assert_equal(result['result'], 'success')
def assert_error(result):
assert_equal(result['result'], 'error')
class CryptoConditionsTest (BitcoinTestFramework): class CryptoConditionsTest (BitcoinTestFramework):
def setup_chain(self): def setup_chain(self):
@@ -149,7 +155,6 @@ class CryptoConditionsTest (BitcoinTestFramework):
assert_equal(result['result'], 'success') assert_equal(result['result'], 'success')
for x in ['AssetsCCaddress', 'myCCaddress', 'Assetsmarker', 'myaddress', 'CCaddress']: for x in ['AssetsCCaddress', 'myCCaddress', 'Assetsmarker', 'myaddress', 'CCaddress']:
assert_equal(result[x][0], 'R') assert_equal(result[x][0], 'R')
# there are no tokens created yet # there are no tokens created yet
result = rpc.tokenlist() result = rpc.tokenlist()
assert_equal(result, []) assert_equal(result, [])
@@ -174,8 +179,47 @@ class CryptoConditionsTest (BitcoinTestFramework):
# this is not a valid assetid # this is not a valid assetid
result = rpc.tokeninfo(self.pubkey) result = rpc.tokeninfo(self.pubkey)
assert_equal(result['result'], 'error') assert_error(result)
# invalid numtokens
result = rpc.tokenask("-1", tokenid, "1")
assert_error(result)
# invalid numtokens
result = rpc.tokenask("0", tokenid, "1")
assert_error(result)
# invalid price
result = rpc.tokenask("1", tokenid, "-1")
assert_error(result)
# invalid price
result = rpc.tokenask("1", tokenid, "0")
assert_error(result)
# invalid tokenid
result = rpc.tokenask("100", "deadbeef", "1")
assert_error(result)
# valid
result = rpc.tokenask("100", tokenid, "7.77")
assert_success(result)
tokenaskhex = result['hex']
assert tokenaskhex, "got tokenask hexk"
tokenaskid = self.send_and_mine(result['hex'])
# invalid fillunits
result = rpc.tokenfillask(tokenid, tokenaskid, "0")
assert_error(result)
# invalid fillunits
result = rpc.tokenfillask(tokenid, tokenaskid, "-777")
assert_error(result)
# should this pass or fail?
result = rpc.tokenfillask(tokenid, tokenaskid, "10")
#assert_success(result)
def run_rewards_tests(self): def run_rewards_tests(self):
rpc = self.nodes[0] rpc = self.nodes[0]

View File

@@ -14,6 +14,7 @@
******************************************************************************/ ******************************************************************************/
#include "CCassets.h" #include "CCassets.h"
extern std::string CCerror;
int64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint256 assetid,int64_t total,int32_t maxinputs) int64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint256 assetid,int64_t total,int32_t maxinputs)
{ {
@@ -427,12 +428,14 @@ std::string FillSell(int64_t txfee,uint256 assetid,uint256 assetid2,uint256 askt
CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector<uint8_t> origpubkey; double dprice; uint64_t mask; int32_t askvout=0; int64_t received_assetoshis,total_nValue,orig_assetoshis,paid_nValue,remaining_nValue,inputs,CCchange=0; struct CCcontract_info *cp,C; CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector<uint8_t> origpubkey; double dprice; uint64_t mask; int32_t askvout=0; int64_t received_assetoshis,total_nValue,orig_assetoshis,paid_nValue,remaining_nValue,inputs,CCchange=0; struct CCcontract_info *cp,C;
if ( fillunits < 0 ) if ( fillunits < 0 )
{ {
fprintf(stderr,"negative fillunits %lld\n",(long long)fillunits); CCerror = strprintf("negative fillunits %lld\n",(long long)fillunits);
fprintf(stderr,"%s\n",CCerror.c_str());
return(""); return("");
} }
if ( assetid2 != zeroid ) if ( assetid2 != zeroid )
{ {
fprintf(stderr,"asset swaps disabled\n"); CCerror = "asset swaps disabled";
fprintf(stderr,"%s\n",CCerror.c_str());
return(""); return("");
} }
@@ -474,7 +477,10 @@ std::string FillSell(int64_t txfee,uint256 assetid,uint256 assetid2,uint256 askt
if ( CCchange != 0 ) if ( CCchange != 0 )
mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk));
return(FinalizeCCTx(mask,cp,mtx,mypk,txfee,EncodeAssetOpRet(assetid2!=zeroid?'E':'S',assetid,assetid2,remaining_nValue,origpubkey))); return(FinalizeCCTx(mask,cp,mtx,mypk,txfee,EncodeAssetOpRet(assetid2!=zeroid?'E':'S',assetid,assetid2,remaining_nValue,origpubkey)));
} else fprintf(stderr,"filltx not enough utxos\n"); } else {
CCerror = strprintf("filltx not enough utxos");
fprintf(stderr,"%s\n", CCerror.c_str());
}
} }
} }
return(""); return("");

View File

@@ -38,8 +38,7 @@
#include <numeric> #include <numeric>
#define ERR_RESULT(x) result.push_back(Pair("result", "error")); \ #define ERR_RESULT(x) result.push_back(Pair("result", "error")) , result.push_back(Pair("error", x));
result.push_back(Pair("error", x));
using namespace std; using namespace std;
@@ -52,7 +51,6 @@ uint32_t komodo_segid32(char *coinaddr);
int64_t nWalletUnlockTime; int64_t nWalletUnlockTime;
static CCriticalSection cs_nWalletUnlockTime; static CCriticalSection cs_nWalletUnlockTime;
//TODO: find a better place for this
std::string CCerror; std::string CCerror;
// Private method: // Private method:
@@ -5002,7 +5000,7 @@ UniValue rewardscreatefunding(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 result.push_back(Pair("error", "couldnt create rewards funding transaction")); } else ERR_RESULT("couldnt create rewards funding transaction");
return(result); return(result);
} }
@@ -5024,14 +5022,8 @@ UniValue rewardslock(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 { } else ERR_RESULT( "couldnt create rewards lock transaction");
result.push_back(Pair("result", "error")); } else ERR_RESULT("amount must be positive");
result.push_back(Pair("error", "couldnt create rewards lock transaction"));
}
} else {
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "amount must be positive"));
}
return(result); return(result);
} }
@@ -5080,16 +5072,11 @@ UniValue rewardsunlock(const UniValue& params, bool fHelp)
else memset(&txid,0,sizeof(txid)); else memset(&txid,0,sizeof(txid));
hex = RewardsUnlock(0,name,fundingtxid,txid); hex = RewardsUnlock(0,name,fundingtxid,txid);
if (CCerror != "") { if (CCerror != "") {
result.push_back(Pair("result", "error")); ERR_RESULT(CCerror);
result.push_back(Pair("error", CCerror)); } else if ( hex.size() > 0 ) {
} 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));
} else { } else ERR_RESULT("couldnt create rewards unlock transaction");
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "couldnt create rewards unlock transaction"));
}
return(result); return(result);
} }
@@ -5181,14 +5168,8 @@ UniValue faucetfund(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 { } else ERR_RESULT("couldnt create faucet funding transaction");
result.push_back(Pair("result", "error")); } else ERR_RESULT( "funding amount must be positive");
result.push_back(Pair("error", "couldnt create faucet funding transaction"));
}
} else {
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "funding amount must be positive"));
}
return(result); return(result);
} }
@@ -5205,10 +5186,7 @@ UniValue faucetget(const UniValue& params, bool fHelp)
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 { } else ERR_RESULT("couldnt create faucet get transaction");
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "couldnt create faucet get transaction"));
}
return(result); return(result);
} }
@@ -5229,14 +5207,12 @@ UniValue dicefund(const UniValue& params, bool fHelp)
timeoutblocks = atol(params[5].get_str().c_str()); timeoutblocks = atol(params[5].get_str().c_str());
hex = DiceCreateFunding(0,name,funds,minbet,maxbet,maxodds,timeoutblocks); hex = DiceCreateFunding(0,name,funds,minbet,maxbet,maxodds,timeoutblocks);
if (CCerror != "") { if (CCerror != "") {
result.push_back(Pair("result", "error")); ERR_RESULT(CCerror);
result.push_back(Pair("error", CCerror));
} else if ( hex.size() > 0 ) { } 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));
} else { } else {
result.push_back(Pair("error", "couldnt create dice funding transaction")); ERR_RESULT( "couldnt create dice funding transaction");
result.push_back(Pair("result", "error"));
} }
return(result); return(result);
} }
@@ -5256,19 +5232,12 @@ UniValue diceaddfunds(const UniValue& params, bool fHelp)
if ( amount > 0 ) { if ( amount > 0 ) {
hex = DiceAddfunding(0,name,fundingtxid,amount); hex = DiceAddfunding(0,name,fundingtxid,amount);
if (CCerror != "") { if (CCerror != "") {
result.push_back(Pair("result", "error")); ERR_RESULT(CCerror);
result.push_back(Pair("error", CCerror));
} else if ( hex.size() > 0 ) { } 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));
} else { } else ERR_RESULT("couldnt create dice addfunding transaction");
result.push_back(Pair("result", "error")); } else ERR_RESULT("amount must be positive");
result.push_back(Pair("error", "couldnt create dice addfunding transaction"));
}
} else {
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "amount must be positive"));
}
return(result); return(result);
} }
@@ -5291,9 +5260,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 { } else ERR_RESULT("couldnt create faucet get transaction");
ERR_RESULT("couldnt create faucet get transaction");
}
} else { } else {
ERR_RESULT("amount and odds must be positive"); ERR_RESULT("amount and odds must be positive");
} }
@@ -5317,7 +5284,7 @@ UniValue dicefinish(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 result.push_back(Pair("error", "couldnt create dicefinish transaction")); } else ERR_RESULT( "couldnt create dicefinish transaction");
return(result); return(result);
} }
@@ -5457,7 +5424,7 @@ UniValue tokencreate(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 result.push_back(Pair("error", "couldnt create transaction")); } else ERR_RESULT("couldnt create transaction");
return(result); return(result);
} }
@@ -5479,7 +5446,7 @@ UniValue tokentransfer(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 result.push_back(Pair("error", "couldnt transfer assets")); } else ERR_RESULT("couldnt transfer assets");
} else { } else {
ERR_RESULT("amount must be positive"); ERR_RESULT("amount must be positive");
} }
@@ -5528,7 +5495,7 @@ UniValue tokencancelbid(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 result.push_back(Pair("error", "couldnt cancel bid")); } else ERR_RESULT("couldnt cancel bid");
return(result); return(result);
} }
@@ -5549,7 +5516,7 @@ UniValue tokenfillbid(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 result.push_back(Pair("error", "couldnt fill bid")); } else ERR_RESULT("couldnt fill bid");
return(result); return(result);
} }
@@ -5572,7 +5539,7 @@ UniValue tokenask(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 result.push_back(Pair("error", "couldnt create ask")); } else ERR_RESULT("couldnt create ask");
} else { } else {
ERR_RESULT("price and numtokens must be positive"); ERR_RESULT("price and numtokens must be positive");
} }
@@ -5600,7 +5567,7 @@ UniValue tokenswapask(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 result.push_back(Pair("error", "couldnt create swap")); } else ERR_RESULT("couldnt create swap");
} else { } else {
ERR_RESULT("price and numtokens must be positive"); ERR_RESULT("price and numtokens must be positive");
} }
@@ -5623,7 +5590,7 @@ UniValue tokencancelask(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 result.push_back(Pair("error", "couldnt cancel bid")); } else ERR_RESULT("couldnt cancel bid");
return(result); return(result);
} }
@@ -5642,11 +5609,14 @@ UniValue tokenfillask(const UniValue& params, bool fHelp)
fillunits = atol(params[2].get_str().c_str()); fillunits = atol(params[2].get_str().c_str());
hex = FillSell(0,tokenid,zeroid,asktxid,fillunits); hex = FillSell(0,tokenid,zeroid,asktxid,fillunits);
if (fillunits > 0) { if (fillunits > 0) {
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));
} else ERR_RESULT("couldnt fill bid"); } else {
ERR_RESULT("couldnt fill bid");
}
} else { } else {
ERR_RESULT("fillunits must be positive"); ERR_RESULT("fillunits must be positive");
} }