From 124819cef50c517547aa02ee0abde1397d840d51 Mon Sep 17 00:00:00 2001 From: "Jonathan \"Duke\" Leto" Date: Mon, 27 Aug 2018 00:48:20 +0200 Subject: [PATCH 1/7] Lots of error checking improvements and more passing tests --- qa/rpc-tests/cryptoconditions.py | 53 +++++++++++++++++++--------- src/wallet/rpcwallet.cpp | 59 +++++++++++++++++++++++++++----- 2 files changed, 88 insertions(+), 24 deletions(-) diff --git a/qa/rpc-tests/cryptoconditions.py b/qa/rpc-tests/cryptoconditions.py index f133ae28a..dd6adba76 100755 --- a/qa/rpc-tests/cryptoconditions.py +++ b/qa/rpc-tests/cryptoconditions.py @@ -78,6 +78,12 @@ class CryptoConditionsTest (BitcoinTestFramework): for x in ['myCCaddress', 'FaucetCCaddress', 'Faucetmarker', 'myaddress']: assert_equal(faucet[x][0], 'R') + result = rpc.faucetaddress(self.pubkey) + assert_success(result) + # test that additional CCaddress key is returned + for x in ['myCCaddress', 'FaucetCCaddress', 'Faucetmarker', 'myaddress', 'CCaddress']: + assert_equal(result[x][0], 'R') + # no funds in the faucet yet result = rpc.faucetget() assert_error(result) @@ -134,12 +140,20 @@ class CryptoConditionsTest (BitcoinTestFramework): for x in ['myCCaddress', 'DiceCCaddress', 'Dicemarker', 'myaddress']: assert_equal(dice[x][0], 'R') + dice = rpc.diceaddress(self.pubkey) + assert_equal(dice['result'], 'success') + for x in ['myCCaddress', 'DiceCCaddress', 'Dicemarker', 'myaddress', 'CCaddress']: + assert_equal(dice[x][0], 'R') + # no dice created yet result = rpc.dicelist() assert_equal(result, []) - #result = rpc.dicefund("LUCKY",10000,1,10000,10,5) - #assert_equal(result, []) + result = rpc.diceinfo("invalid") + assert_error(result) + + result = rpc.dicefund("THISISTOOLONG", "10000", "10", "10000", "10", "5") + assert_error(result) def run_token_tests(self): rpc = self.nodes[0] @@ -156,8 +170,15 @@ class CryptoConditionsTest (BitcoinTestFramework): result = rpc.tokenlist() assert_equal(result, []) - result = rpc.tokencreate("DUKE", "1987.420", "duke") + result = rpc.tokencreate("NUKE", "-1987420", "no bueno supply") + assert_error(result) + + result = rpc.tokencreate("NUKE123456789012345678901234567890", "1987420", "name too long") + assert_error(result) + + result = rpc.tokencreate("DUKE", "1987.420", "Duke's custom token") assert_success(result) + tokenid = self.send_and_mine(result['hex']) result = rpc.tokenlist() @@ -197,7 +218,7 @@ class CryptoConditionsTest (BitcoinTestFramework): assert_equal(result['owner'], self.pubkey) assert_equal(result['name'], "DUKE") assert_equal(result['supply'], 198742000000) - assert_equal(result['description'], "duke") + assert_equal(result['description'], "Duke's custom token") # invalid numtokens ask result = rpc.tokenask("-1", tokenid, "1") @@ -253,25 +274,25 @@ class CryptoConditionsTest (BitcoinTestFramework): result = rpc.tokenorders() assert_equal(result, []) - # invalid numtokens bid (have to add status to CC code!) + # invalid numtokens bid result = rpc.tokenbid("-1", tokenid, "1") - assert_equal(result['error'], 'invalid parameter') + assert_error(result); - # invalid numtokens bid (have to add status to CC code!) + # invalid numtokens bid result = rpc.tokenbid("0", tokenid, "1") - assert_equal(result['error'], 'invalid parameter') + assert_error(result); - # invalid price bid (have to add status to CC code!) + # invalid price bid result = rpc.tokenbid("1", tokenid, "-1") - assert_equal(result['error'], 'invalid parameter') + assert_error(result); - # invalid price bid (have to add status to CC code!) + # invalid price bid result = rpc.tokenbid("1", tokenid, "0") - assert_equal(result['error'], 'invalid parameter') + assert_error(result); - # invalid tokenid bid (have to add status to CC code!) + # invalid tokenid bid result = rpc.tokenbid("100", "deadbeef", "1") - assert_equal(result['error'], 'invalid parameter') + assert_error(result); # valid bid tokenbid = rpc.tokenbid("100", tokenid, "10") @@ -310,11 +331,11 @@ class CryptoConditionsTest (BitcoinTestFramework): # invalid token transfer amount (have to add status to CC code!) randompubkey = "021a559101e355c907d9c553671044d619769a6e71d624f68bfec7d0afa6bd6a96" result = rpc.tokentransfer(tokenid,randompubkey,"0") - assert_equal(result['error'], 'invalid parameter') + assert_error(result); # invalid token transfer amount (have to add status to CC code!) result = rpc.tokentransfer(tokenid,randompubkey,"-1") - assert_equal(result['error'], 'invalid parameter') + assert_error(result); # valid token transfer sendtokens = rpc.tokentransfer(tokenid,randompubkey,"1") diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 1012a0205..6a0555f54 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5234,6 +5234,12 @@ UniValue dicefund(const UniValue& params, bool fHelp) maxbet = atof(params[3].get_str().c_str()) * COIN; maxodds = atol(params[4].get_str().c_str()); timeoutblocks = atol(params[5].get_str().c_str()); + + if (!VALID_PLAN_NAME(name)) { + ERR_RESULT(strprintf("Plan name can be at most %d ASCII characters",PLAN_NAME_MAX)); + return(result); + } + hex = DiceCreateFunding(0,name,funds,minbet,maxbet,maxodds,timeoutblocks); if (CCerror != "") { ERR_RESULT(CCerror); @@ -5258,6 +5264,10 @@ UniValue diceaddfunds(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); amount = atof(params[2].get_str().c_str()) * COIN; + if (!VALID_PLAN_NAME(name)) { + ERR_RESULT(strprintf("Plan name can be at most %d ASCII characters",PLAN_NAME_MAX)); + return(result); + } if ( amount > 0 ) { hex = DiceAddfunding(0,name,fundingtxid,amount); if (CCerror != "") { @@ -5283,6 +5293,11 @@ UniValue dicebet(const UniValue& params, bool fHelp) fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); amount = atof(params[2].get_str().c_str()) * COIN; odds = atol(params[3].get_str().c_str()); + + if (!VALID_PLAN_NAME(name)) { + ERR_RESULT(strprintf("Plan name can be at most %d ASCII characters",PLAN_NAME_MAX)); + return(result); + } if (amount > 0 && odds > 0) { hex = DiceBet(0,name,fundingtxid,amount,odds); if ( hex.size() > 0 ) @@ -5306,6 +5321,10 @@ UniValue dicefinish(const UniValue& params, bool fHelp) const CKeyStore& keystore = *pwalletMain; LOCK2(cs_main, pwalletMain->cs_wallet); name = (char *)params[0].get_str().c_str(); + if (!VALID_PLAN_NAME(name)) { + ERR_RESULT(strprintf("Plan name can be at most %d ASCII characters",PLAN_NAME_MAX)); + return(result); + } fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); hex = DiceBetFinish(&r,0,name,fundingtxid,bettxid,1); @@ -5330,6 +5349,10 @@ UniValue dicestatus(const UniValue& params, bool fHelp) const CKeyStore& keystore = *pwalletMain; LOCK2(cs_main, pwalletMain->cs_wallet); name = (char *)params[0].get_str().c_str(); + if (!VALID_PLAN_NAME(name)) { + ERR_RESULT(strprintf("Plan name can be at most %d ASCII characters",PLAN_NAME_MAX)); + return(result); + } fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); memset(&bettxid,0,sizeof(bettxid)); if ( params.size() == 3 ) @@ -5453,9 +5476,14 @@ UniValue tokencreate(const UniValue& params, bool fHelp) LOCK2(cs_main, pwalletMain->cs_wallet); name = params[0].get_str(); supply = atof(params[1].get_str().c_str()) * COIN; - if ( name.size() == 0 || supply <= 0 ) + if ( name.size() == 0 || name.size() > 32) { - result.push_back(Pair("error", "invalid parameter")); + ERR_RESULT("Token name must not be empty and up to 32 characters"); + return(result); + } + if ( supply <= 0 ) + { + ERR_RESULT("Token supply must be positive"); return(result); } if ( params.size() == 3 ) @@ -5463,7 +5491,7 @@ UniValue tokencreate(const UniValue& params, bool fHelp) description = params[2].get_str(); if ( description.size() > 4096 ) { - result.push_back(Pair("error", "token description longer than 4096")); + ERR_RESULT("Token description must be <= 4096 characters"); return(result); } } @@ -5488,9 +5516,14 @@ UniValue tokentransfer(const UniValue& params, bool fHelp) tokenid = Parseuint256((char *)params[0].get_str().c_str()); std::vector pubkey(ParseHex(params[1].get_str().c_str())); amount = atol(params[2].get_str().c_str()); - if ( tokenid == zeroid || amount <= 0 ) + if ( tokenid == zeroid ) { - result.push_back(Pair("error", "invalid parameter")); + ERR_RESULT("invalid tokenid"); + return(result); + } + if ( amount <= 0 ) + { + ERR_RESULT("amount must be positive"); return(result); } hex = AssetTransfer(0,tokenid,pubkey,amount); @@ -5519,9 +5552,19 @@ UniValue tokenbid(const UniValue& params, bool fHelp) tokenid = Parseuint256((char *)params[1].get_str().c_str()); price = atof(params[2].get_str().c_str()); bidamount = (price * numtokens) * COIN + 0.0000000049999; - if ( tokenid == zeroid || tokenid == zeroid || price <= 0 || bidamount <= 0 ) + if ( price <= 0 ) { - result.push_back(Pair("error", "invalid parameter")); + ERR_RESULT("price must be positive"); + return(result); + } + if ( tokenid == zeroid ) + { + ERR_RESULT("invalid tokenid"); + return(result); + } + if ( bidamount <= 0 ) + { + ERR_RESULT("bid amount must be positive"); return(result); } hex = CreateBuyOffer(0,bidamount,tokenid,numtokens); @@ -5530,7 +5573,7 @@ UniValue tokenbid(const UniValue& params, bool fHelp) { result.push_back(Pair("result", "success")); result.push_back(Pair("hex", hex)); - } else result.push_back(Pair("error", "couldnt create bid")); + } else ERR_RESULT("couldnt create bid"); } else { ERR_RESULT("price and numtokens must be positive"); } From 820043536303738d2b21e7e601997d06cfc19181 Mon Sep 17 00:00:00 2001 From: "Jonathan \"Duke\" Leto" Date: Sun, 26 Aug 2018 22:30:28 +0200 Subject: [PATCH 2/7] Validate plan name in dicebet --- src/wallet/rpcwallet.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 6a0555f54..b99fffa59 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -55,6 +55,9 @@ std::string CCerror; // Private method: UniValue z_getoperationstatus_IMPL(const UniValue&, bool); +#define PLAN_NAME_MAX 8 +#define VALID_PLAN_NAME(x) (strlen(x) <= PLAN_NAME_MAX) + std::string HelpRequiringPassphrase() { return pwalletMain && pwalletMain->IsCrypted() From 6e3dc787c55249fe4e36d37b3bb5de0761849b69 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 27 Aug 2018 09:35:17 -1100 Subject: [PATCH 3/7] Fix early activation --- src/cc/CCutils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index a57197537..8e8b103c8 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -259,7 +259,7 @@ bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector param height = KOMODO_CONNECTING; if ( KOMODO_CONNECTING < 0 ) // always comes back with > 0 for final confirmation return(true); - if ( ASSETCHAINS_CC == 0 || height < KOMODO_CCACTIVATE ) + if ( ASSETCHAINS_CC == 0 || (height & ~(1<<30)) < KOMODO_CCACTIVATE ) return eval->Invalid("CC are disabled or not active yet"); if ( (KOMODO_CONNECTING & (1<<30)) != 0 ) { From 6bd0c10acbc0ed1bd1017e7ed0fa772b9ea5d4e0 Mon Sep 17 00:00:00 2001 From: Anton Lysakov Date: Tue, 28 Aug 2018 17:32:59 +0700 Subject: [PATCH 4/7] More Dice CC tests --- qa/rpc-tests/cryptoconditions.py | 151 +++++++++++++++++++++++++++++-- 1 file changed, 142 insertions(+), 9 deletions(-) diff --git a/qa/rpc-tests/cryptoconditions.py b/qa/rpc-tests/cryptoconditions.py index dd6adba76..c8a78c191 100755 --- a/qa/rpc-tests/cryptoconditions.py +++ b/qa/rpc-tests/cryptoconditions.py @@ -142,19 +142,149 @@ class CryptoConditionsTest (BitcoinTestFramework): dice = rpc.diceaddress(self.pubkey) assert_equal(dice['result'], 'success') - for x in ['myCCaddress', 'DiceCCaddress', 'Dicemarker', 'myaddress', 'CCaddress']: + for x in ['myCCaddress', 'DiceCCaddress', 'Dicemarker', 'myaddress']: assert_equal(dice[x][0], 'R') # no dice created yet result = rpc.dicelist() assert_equal(result, []) + # creating dice plan with too long name + result = rpc.dicefund("THISISTOOLONG", "10000", "10", "10000", "10", "5") + assert_error(result) + + # creating dice plan with < 100 funding + result = rpc.dicefund("LUCKY","10","1","10000","10","5") + assert_error(result) + + # creating dice plan with 0 blocks timeout + result = rpc.dicefund("LUCKY","10","1","10000","10","0") + assert_error(result) + + # creating dice plan + dicefundtx = rpc.dicefund("LUCKY","1000","1","800","10","5") + diceid = self.send_and_mine(dicefundtx['hex']) + + # checking if it in plans list now + result = rpc.dicelist() + assert_equal(result[0], diceid) + + # set dice name for futher usage + dicename = "LUCKY" + + # adding zero funds to plan + result = rpc.diceaddfunds(dicename,diceid,"0") + assert_error(result) + + # adding negative funds to plan + result = rpc.diceaddfunds(dicename,diceid,"-1") + assert_error(result) + + # adding funds to plan + addfundstx = rpc.diceaddfunds(dicename,diceid,"1100") + result = self.send_and_mine(addfundstx['hex']) + + # checking if funds added to plan + result = rpc.diceinfo(diceid) + assert_equal(result["funding"], "2100.00000000") + + # not valid dice info checking result = rpc.diceinfo("invalid") assert_error(result) - result = rpc.dicefund("THISISTOOLONG", "10000", "10", "10000", "10", "5") + # placing 0 amount bet + result = rpc.dicebet(dicename,diceid,"0","1") assert_error(result) + # placing negative amount bet + result = rpc.dicebet(dicename,diceid,"-1","1") + assert_error(result) + + # placing bet more than maxbet + result = rpc.dicebet(dicename,diceid,"900","1") + assert_error(result) + + # placing bet with amount more than funding + result = rpc.dicebet(dicename,diceid,"3000","1") + assert_error(result) + + # placing bet with potential won more than funding + result = rpc.dicebet(dicename,diceid,"750","9") + assert_error(result) + + # placing 0 odds bet + result = rpc.dicebet(dicename,diceid,"1","0") + assert_error(result) + + # placing negative odds bet + result = rpc.dicebet(dicename,diceid,"1","-1") + assert_error(result) + + # placing bet with odds more than allowed + result = rpc.dicebet(dicename,diceid,"1","11") + assert_error(result) + + # placing bet with possible payout more than funding + result = rpc.dicebet(dicename,diceid,"500","4") + assert_error(result) + + # placing bet with not correct dice name + result = rpc.dicebet("nope",diceid,"100","1") + assert_error(result) + + # placing bet with not correct dice id + result = rpc.dicebet(dicename,self.pubkey,"100","1") + assert_error(result) + + # valid bet placing + placebet = rpc.dicebet(dicename,diceid,"100","1") + betid = self.send_and_mine(placebet["hex"]) + assert result, "bet placed" + + # check bet status + result = rpc.dicestatus(dicename,diceid,betid) + assert_success(result) + + # have to make some entropy for the next test + entropytx = 0 + fundingsum = 1 + while entropytx < 10: + fundingsuminput = str(fundingsum) + fundinghex = rpc.diceaddfunds(dicename,diceid,fundingsuminput) + result = self.send_and_mine(fundinghex['hex']) + entropytx = entropytx + 1 + fundingsum = fundingsum + 1 + + rpc.generate(2) + + # note initial dice funding state at this point. + # TODO: track player balance somehow (hard to do because of mining and fees) + diceinfo = rpc.diceinfo(diceid) + funding = float(diceinfo['funding']) + + # placing same amount bets with amount 1 and odds 1:2, checking if balance changed correct + losscounter = 0 + wincounter = 0 + betcounter = 0 + + while (betcounter < 10): + placebet = rpc.dicebet(dicename,diceid,"1","1") + betid = self.send_and_mine(placebet["hex"]) + finish = rpc.dicefinish(dicename,diceid,betid) + self.send_and_mine(finish["hex"]) + betresult = rpc.dicestatus(dicename,diceid,betid) + betcounter = betcounter + 1 + if betresult["status"] == "loss": + losscounter = losscounter + 1 + elif betresult["status"] == "win": + wincounter = wincounter + 1 + + # funding balance should increase if player loss, decrease if player won + fundbalanceguess = funding + losscounter - wincounter + fundinfoactual = rpc.diceinfo(diceid) + assert_equal(round(fundbalanceguess),round(float(fundinfoactual['funding']))) + + def run_token_tests(self): rpc = self.nodes[0] result = rpc.tokenaddress() @@ -170,12 +300,15 @@ class CryptoConditionsTest (BitcoinTestFramework): result = rpc.tokenlist() assert_equal(result, []) + # trying to create token with negaive supply result = rpc.tokencreate("NUKE", "-1987420", "no bueno supply") assert_error(result) + # creating token with name more than 16 chars result = rpc.tokencreate("NUKE123456789012345678901234567890", "1987420", "name too long") assert_error(result) + # creating valid token result = rpc.tokencreate("DUKE", "1987.420", "Duke's custom token") assert_success(result) @@ -276,23 +409,23 @@ class CryptoConditionsTest (BitcoinTestFramework): # invalid numtokens bid result = rpc.tokenbid("-1", tokenid, "1") - assert_error(result); + assert_error(result) # invalid numtokens bid result = rpc.tokenbid("0", tokenid, "1") - assert_error(result); + assert_error(result) # invalid price bid result = rpc.tokenbid("1", tokenid, "-1") - assert_error(result); + assert_error(result) # invalid price bid result = rpc.tokenbid("1", tokenid, "0") - assert_error(result); + assert_error(result) # invalid tokenid bid result = rpc.tokenbid("100", "deadbeef", "1") - assert_error(result); + assert_error(result) # valid bid tokenbid = rpc.tokenbid("100", tokenid, "10") @@ -331,11 +464,11 @@ class CryptoConditionsTest (BitcoinTestFramework): # invalid token transfer amount (have to add status to CC code!) randompubkey = "021a559101e355c907d9c553671044d619769a6e71d624f68bfec7d0afa6bd6a96" result = rpc.tokentransfer(tokenid,randompubkey,"0") - assert_error(result); + assert_error(result) # invalid token transfer amount (have to add status to CC code!) result = rpc.tokentransfer(tokenid,randompubkey,"-1") - assert_error(result); + assert_error(result) # valid token transfer sendtokens = rpc.tokentransfer(tokenid,randompubkey,"1") From 881869c081aa7be8d8413d26247e4ab61621e51f Mon Sep 17 00:00:00 2001 From: Anton Lysakov Date: Tue, 28 Aug 2018 17:37:31 +0700 Subject: [PATCH 5/7] fixed local merge disorder --- qa/rpc-tests/cryptoconditions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qa/rpc-tests/cryptoconditions.py b/qa/rpc-tests/cryptoconditions.py index c8a78c191..ceb6237fd 100755 --- a/qa/rpc-tests/cryptoconditions.py +++ b/qa/rpc-tests/cryptoconditions.py @@ -142,7 +142,7 @@ class CryptoConditionsTest (BitcoinTestFramework): dice = rpc.diceaddress(self.pubkey) assert_equal(dice['result'], 'success') - for x in ['myCCaddress', 'DiceCCaddress', 'Dicemarker', 'myaddress']: + for x in ['myCCaddress', 'DiceCCaddress', 'Dicemarker', 'myaddress', 'CCaddress']: assert_equal(dice[x][0], 'R') # no dice created yet From aa54d5452917348c8c57c3bf9bac66bb019d5d2b Mon Sep 17 00:00:00 2001 From: Anton Lysakov Date: Tue, 28 Aug 2018 17:48:47 +0700 Subject: [PATCH 6/7] Described tokename dicename length limits --- qa/rpc-tests/cryptoconditions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qa/rpc-tests/cryptoconditions.py b/qa/rpc-tests/cryptoconditions.py index ceb6237fd..cc88e8fac 100755 --- a/qa/rpc-tests/cryptoconditions.py +++ b/qa/rpc-tests/cryptoconditions.py @@ -149,7 +149,7 @@ class CryptoConditionsTest (BitcoinTestFramework): result = rpc.dicelist() assert_equal(result, []) - # creating dice plan with too long name + # creating dice plan with too long name (>8 chars) result = rpc.dicefund("THISISTOOLONG", "10000", "10", "10000", "10", "5") assert_error(result) @@ -304,7 +304,7 @@ class CryptoConditionsTest (BitcoinTestFramework): result = rpc.tokencreate("NUKE", "-1987420", "no bueno supply") assert_error(result) - # creating token with name more than 16 chars + # creating token with name more than 32 chars result = rpc.tokencreate("NUKE123456789012345678901234567890", "1987420", "name too long") assert_error(result) From de6be280c41094d997d1871c80e3bcb0f1ab0813 Mon Sep 17 00:00:00 2001 From: Anton Lysakov Date: Tue, 28 Aug 2018 17:58:14 +0700 Subject: [PATCH 7/7] Deleted test duplicate --- qa/rpc-tests/cryptoconditions.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/qa/rpc-tests/cryptoconditions.py b/qa/rpc-tests/cryptoconditions.py index cc88e8fac..fbf149780 100755 --- a/qa/rpc-tests/cryptoconditions.py +++ b/qa/rpc-tests/cryptoconditions.py @@ -224,10 +224,6 @@ class CryptoConditionsTest (BitcoinTestFramework): result = rpc.dicebet(dicename,diceid,"1","11") assert_error(result) - # placing bet with possible payout more than funding - result = rpc.dicebet(dicename,diceid,"500","4") - assert_error(result) - # placing bet with not correct dice name result = rpc.dicebet("nope",diceid,"100","1") assert_error(result)