From f1e1ea613397f369cc95cab636e1675aee66d992 Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Mon, 23 Jul 2018 04:33:47 +0000 Subject: [PATCH 001/204] WIP add 'top N only' feature to gensnapshot rpc --- src/main.cpp | 4 ++-- src/rpcmisc.cpp | 43 +++++++++++++++++++++++++++++++++++++++---- src/txdb.cpp | 18 +++++++++++++++--- src/txdb.h | 2 +- 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index aa4435843..5ac78e38b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -588,7 +588,7 @@ CBlockTreeDB *pblocktree = NULL; #define KOMODO_ZCASH #include "komodo.h" -UniValue komodo_snapshot() +UniValue komodo_snapshot(int top) { LOCK(cs_main); int64_t total = -1; @@ -596,7 +596,7 @@ UniValue komodo_snapshot() if (fAddressIndex) { if ( pblocktree != 0 ) { - result = pblocktree->Snapshot(); + result = pblocktree->Snapshot(top); } else { fprintf(stderr,"null pblocktree start with -addressindex=true\n"); } diff --git a/src/rpcmisc.cpp b/src/rpcmisc.cpp index 8eeff7877..657227173 100644 --- a/src/rpcmisc.cpp +++ b/src/rpcmisc.cpp @@ -1015,18 +1015,53 @@ UniValue getaddressbalance(const UniValue& params, bool fHelp) } -UniValue komodo_snapshot(); +UniValue komodo_snapshot(int top); UniValue getsnapshot(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); int64_t total; - if ( fHelp || params.size() > 0 ) + UniValue result(UniValue::VOBJ); int64_t total; int top = 0; + + if (params.size() > 0 && !params[0].isNull()) { + top = params[0].get_str(); + fprintf(stderr, "top=%s\n",top); + if (top <= 0) + throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, top must be a positive integer"); + } + + if ( fHelp || params.size() > 1) { throw runtime_error( "getsnapshot\n" + "\nReturns a snapshot of (address,amount) pairs at current height (requires addressindex to be enabled).\n" + "\nArguments:\n" + " \"top\" (number, optional) Only return this many addresses, i.e. top N richlist\n" + "\nResult:\n" + "{\n" + " \"addresses\": [\n" + " {\n" + " \"addr\": \"RMEBhzvATA8mrfVK82E5TgPzzjtaggRGN3\",\n" + " \"amount\": \"100.0\"\n" + " },\n" + " {\n" + " \"addr\": \"RqEBhzvATAJmrfVL82E57gPzzjtaggR777\",\n" + " \"amount\": \"23.45\"\n" + " }\n" + " ],\n" + " \"total\": 123.45 (numeric) Total amount in snapshot\n" + " \"average\": 61.7, (numeric) Average amount in each address \n" + " \"utxos\": 14, (number) Total number of UTXOs in snapshot\n" + " \"total_addresses\": 2, (number) Total number of addresses in snapshot,\n" + " \"start_height\": 91, (number) Block height snapshot began\n" + " \"ending_height\": 91 (number) Block height snapsho finished,\n" + " \"start_time\": 1531982752, (number) Unix epoch time snapshot started\n" + " \"end_time\": 1531982752 (number) Unix epoch time snapshot finished\n" + "}\n" + "\nExamples:\n" + + HelpExampleCli("getsnapshot","") + + HelpExampleRpc("getsnapshot", "1000") ); } - result = komodo_snapshot(); + result = komodo_snapshot(top); if ( result.size() > 0 ) { result.push_back(Pair("end_time", time(NULL))); } else { diff --git a/src/txdb.cpp b/src/txdb.cpp index 6d0be5b61..7ca6175b0 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -399,7 +399,7 @@ bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, int type, bool getAddressFromIndex(const int &type, const uint160 &hash, std::string &address); -extern UniValue CBlockTreeDB::Snapshot() +extern UniValue CBlockTreeDB::Snapshot(int top) { char chType; int64_t total = 0; int64_t totalAddresses = 0; std::string address; int64_t utxos = 0; int64_t ignoredAddresses; @@ -472,7 +472,7 @@ extern UniValue CBlockTreeDB::Snapshot() addressAmounts[address] += nValue; } //fprintf(stderr,"{\"%s\", %.8f},\n",address.c_str(),(double)nValue/COIN); - total += nValue; + // total += nValue; utxos++; } catch (const std::exception& e) { fprintf(stderr, "DONE %s: LevelDB addressindex exception! - %s\n", __func__, e.what()); @@ -495,21 +495,33 @@ extern UniValue CBlockTreeDB::Snapshot() UniValue obj(UniValue::VOBJ); UniValue addressesSorted(UniValue::VARR); + int topN = 0; for (std::vector>::iterator it = vaddr.begin(); it!=vaddr.end(); ++it) { UniValue obj(UniValue::VOBJ); obj.push_back( make_pair("addr", it->second.c_str() ) ); char amount[32]; sprintf(amount, "%.8f", (double) it->first / COIN); obj.push_back( make_pair("amount", amount) ); + total += it->first; addressesSorted.push_back(obj); + topN++; + // If requested, only show top N addresses in output JSON + if (top == topN) + break; } + if (top) + totalAddresses = top; + if (totalAddresses > 0) { + // Array of all addreses with balances result.push_back(make_pair("addresses", addressesSorted)); + // Total amount in this snapshot, which is less than circulating supply if top parameter is used result.push_back(make_pair("total", (double) total / COIN )); + // Average amount in each address of this snapshot result.push_back(make_pair("average",(double) (total/COIN) / totalAddresses )); } - // Total number of utxos in this snaphot + // Total number of utxos processed in this snaphot result.push_back(make_pair("utxos", utxos)); // Total number of addresses in this snaphot result.push_back(make_pair("total_addresses", totalAddresses)); diff --git a/src/txdb.h b/src/txdb.h index 6c6b41917..4a5a367e7 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -95,7 +95,7 @@ public: bool ReadFlag(const std::string &name, bool &fValue); bool LoadBlockIndexGuts(); bool blockOnchainActive(const uint256 &hash); - UniValue Snapshot(); + UniValue Snapshot(int top); }; #endif // BITCOIN_TXDB_H From 99ba67a0d19a27366cff62ce5a26f972ef618d91 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 00:00:29 -1100 Subject: [PATCH 002/204] insync flag --- src/komodo_globals.h | 2 +- src/main.cpp | 8 ++++++-- src/miner.cpp | 16 +++++++--------- src/rpcnet.cpp | 1 + 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/komodo_globals.h b/src/komodo_globals.h index 8dfc7108c..c4ba26eb6 100644 --- a/src/komodo_globals.h +++ b/src/komodo_globals.h @@ -46,7 +46,7 @@ struct komodo_state KOMODO_STATES[34]; int COINBASE_MATURITY = _COINBASE_MATURITY;//100; int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAINS_SEED,KOMODO_ON_DEMAND,KOMODO_EXTERNAL_NOTARIES,KOMODO_PASSPORT_INITDONE,KOMODO_PAX,KOMODO_EXCHANGEWALLET,KOMODO_REWIND; -int32_t KOMODO_LASTMINED,prevKOMODO_LASTMINED,JUMBLR_PAUSE = 1; +int32_t KOMODO_INSYNC,KOMODO_LASTMINED,prevKOMODO_LASTMINED,JUMBLR_PAUSE = 1; std::string NOTARY_PUBKEY,ASSETCHAINS_NOTARIES,ASSETCHAINS_OVERRIDE_PUBKEY,DONATION_PUBKEY; uint8_t NOTARY_PUBKEY33[33],ASSETCHAINS_OVERRIDE_PUBKEY33[33],ASSETCHAINS_PUBLIC,ASSETCHAINS_PRIVATE; diff --git a/src/main.cpp b/src/main.cpp index 80d90ba96..c6d6dee17 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -56,7 +56,7 @@ using namespace std; CCriticalSection cs_main; extern uint8_t NOTARY_PUBKEY33[33]; -extern int32_t KOMODO_LOADINGBLOCKS,KOMODO_LONGESTCHAIN; +extern int32_t KOMODO_LOADINGBLOCKS,KOMODO_LONGESTCHAIN,KOMODO_INSYNC; int32_t KOMODO_NEWBLOCKS; int32_t komodo_block2pubkey33(uint8_t *pubkey33,CBlock *block); void komodo_broadcast(CBlock *pblock,int32_t limit); @@ -3471,7 +3471,11 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock * int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1; LogPrint("bench", " - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001); LogPrint("bench", "- Connect block: %.2fms [%.2fs]\n", (nTime6 - nTime1) * 0.001, nTimeTotal * 0.000001); - if ( ASSETCHAINS_SYMBOL[0] == 0 ) + if ( KOMODO_LONGESTCHAIN != 0 && pindexNew->nHeight >= KOMODO_LONGESTCHAIN+1 ) + KOMODO_INSYNC = 1; + else KOMODO_INSYNC = 0; + fprintf(stderr,"connect.%d insync.%d\n",(int32_t)pindexNew->nHeight,KOMODO_INSYNC); + if ( ASSETCHAINS_SYMBOL[0] == 0 && KOMODO_INSYNC != 0 ) komodo_broadcast(pblock,8); return true; } diff --git a/src/miner.cpp b/src/miner.cpp index e377f69c4..93981c552 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -699,7 +699,7 @@ int32_t komodo_baseid(char *origbase); int32_t komodo_eligiblenotary(uint8_t pubkeys[66][33],int32_t *mids,uint32_t *blocktimes,int32_t *nonzpkeysp,int32_t height); arith_uint256 komodo_PoWtarget(int32_t *percPoSp,arith_uint256 target,int32_t height,int32_t goalperc); int32_t FOUND_BLOCK,KOMODO_MAYBEMINED; -extern int32_t KOMODO_LASTMINED; +extern int32_t KOMODO_LASTMINED,KOMODO_INSYNC; int32_t roundrobin_delay; arith_uint256 HASHTarget,HASHTarget_POW; @@ -725,7 +725,7 @@ void static BitcoinMiner() unsigned int n = chainparams.EquihashN(); unsigned int k = chainparams.EquihashK(); uint8_t *script; uint64_t total,checktoshis; int32_t i,j,gpucount=KOMODO_MAXGPUCOUNT,notaryid = -1; - while ( (ASSETCHAIN_INIT == 0 || KOMODO_INITDONE == 0) ) //chainActive.LastTip()->nHeight != 235300 && + while ( (ASSETCHAIN_INIT == 0 || KOMODO_INITDONE == 0) ) { sleep(1); if ( komodo_baseid(ASSETCHAINS_SYMBOL) < 0 ) @@ -911,13 +911,11 @@ void static BitcoinMiner() } while (true) { - /*if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && pblock->vtx[0].vout.size() == 1 && Mining_height > ASSETCHAINS_MINHEIGHT ) // skips when it shouldnt - { - fprintf(stderr,"skip generating %s on-demand block, no tx avail\n",ASSETCHAINS_SYMBOL); - sleep(10); - break; - }*/ - //fprintf(stderr,"top of while\n"); + if ( KOMODO_INSYNC == 0 ) + { + fprintf(stderr,"Mining when blockchain might not be in sync\n"); + sleep(3); + } // Hash state KOMODO_CHOSEN_ONE = 0; crypto_generichash_blake2b_state state; diff --git a/src/rpcnet.cpp b/src/rpcnet.cpp index ec4c7be2a..33a1c317d 100644 --- a/src/rpcnet.cpp +++ b/src/rpcnet.cpp @@ -197,6 +197,7 @@ int32_t komodo_longestchain() if ( num > (n >> 1) ) { KOMODO_LONGESTCHAIN = height; + fprintf(stderr,"set KOMODO_LONGESTCHAIN <- %d\n",height); return(height); } KOMODO_LONGESTCHAIN = 0; From c3d91e3bae0b611ca3607704ce00212eca6c53ef Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 00:36:23 -1100 Subject: [PATCH 003/204] Autodetect insync status --- src/main.cpp | 2 +- src/rpcnet.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c6d6dee17..1e3ee8019 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3471,7 +3471,7 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock * int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1; LogPrint("bench", " - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001); LogPrint("bench", "- Connect block: %.2fms [%.2fs]\n", (nTime6 - nTime1) * 0.001, nTimeTotal * 0.000001); - if ( KOMODO_LONGESTCHAIN != 0 && pindexNew->nHeight >= KOMODO_LONGESTCHAIN+1 ) + if ( KOMODO_LONGESTCHAIN != 0 && pindexNew->nHeight >= KOMODO_LONGESTCHAIN ) KOMODO_INSYNC = 1; else KOMODO_INSYNC = 0; fprintf(stderr,"connect.%d insync.%d\n",(int32_t)pindexNew->nHeight,KOMODO_INSYNC); diff --git a/src/rpcnet.cpp b/src/rpcnet.cpp index 33a1c317d..27650aa17 100644 --- a/src/rpcnet.cpp +++ b/src/rpcnet.cpp @@ -196,8 +196,9 @@ int32_t komodo_longestchain() } if ( num > (n >> 1) ) { + if ( height != KOMODO_LONGESTCHAIN ) + fprintf(stderr,"set %s KOMODO_LONGESTCHAIN <- %d\n",ASSETCHAINS_SYMBOL,height); KOMODO_LONGESTCHAIN = height; - fprintf(stderr,"set KOMODO_LONGESTCHAIN <- %d\n",height); return(height); } KOMODO_LONGESTCHAIN = 0; From 20e2d0b747ff50394cab8e613eb9111f2e13731d Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 00:38:35 -1100 Subject: [PATCH 004/204] Test --- src/rpcnet.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rpcnet.cpp b/src/rpcnet.cpp index 27650aa17..233aeb2c8 100644 --- a/src/rpcnet.cpp +++ b/src/rpcnet.cpp @@ -196,6 +196,7 @@ int32_t komodo_longestchain() } if ( num > (n >> 1) ) { + extern char ASSETCHAINS_SYMBOL[]; if ( height != KOMODO_LONGESTCHAIN ) fprintf(stderr,"set %s KOMODO_LONGESTCHAIN <- %d\n",ASSETCHAINS_SYMBOL,height); KOMODO_LONGESTCHAIN = height; From f881f4dc684807dced341bf09a82a1af6086e70c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 01:04:27 -1100 Subject: [PATCH 005/204] Error message --- src/cc/CCassetstx.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index c52864218..de96728a5 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -249,10 +249,10 @@ std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); fprintf(stderr,"remaining %llu -> origpubkey\n",(long long)remaining_required); return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('B',assetid,zeroid,remaining_required,origpubkey))); - } else fprintf(stderr,"filltx wasnt for assetid\n"); + } else return("dont have any assets to fill bid\n"); } } - return(0); + return("no normal coins left"); } std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,uint64_t fillamount) From 6dcd15b5c7388d8d771f3804d1a114d2f152993b Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 03:16:49 -1100 Subject: [PATCH 006/204] Fix dustvout --- src/cc/CCassetstx.cpp | 1 - src/cc/assets.cpp | 8 ++------ src/cc/rewards.cpp | 47 +++++++++++++++++++++++++++++++++++++------ src/main.cpp | 2 +- src/rpcnet.cpp | 2 +- 5 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index de96728a5..c9405f405 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -21,7 +21,6 @@ uint64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPub std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); - //std::sort(unspentOutputs.begin(), unspentOutputs.end(), heightSort); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 8462c0b16..9eaf3d9b6 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -235,9 +235,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("mismatched remainder for fillbuy"); else if ( remaining_price != 0 ) { - if ( remaining_price < 10000 ) - return eval->Invalid("dust vout0 to AssetsCCaddr for fillbuy"); - else if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,0) == 0 ) + if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,0) == 0 ) return eval->Invalid("mismatched vout0 AssetsCCaddr for fillbuy"); } } @@ -309,9 +307,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("CC vout2 for fillask"); else if ( remaining_price != 0 ) { - if ( remaining_price < 10000 ) - return eval->Invalid("dust vout0 to AssetsCCaddr for fill"); - else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) + if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) return eval->Invalid("mismatched vout0 AssetsCCaddr for fill"); } } diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 53ade6494..8059564de 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -47,6 +47,43 @@ uint64_t RewardsCalc(uint64_t claim,uint256 txid) // min/max time, mindeposit an return(reward); } +CScript EncodeRewardsOpRet(uint8_t funcid,uint64_t sbits) +{ + CScript opret; uint8_t evalcode = EVAL_REWARDS; + switch ( funcid ) + { + case 't': case 'x': case 'o': + opret << OP_RETURN << E_MARSHAL(ss << evalcode << funcid << sbits); + break; + } + return(opret); +} + +uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint64_t &sbits) +{ + std::vector vopret; uint8_t funcid=0,*script,e,f; + GetOpReturnData(scriptPubKey, vopret); + script = (uint8_t *)vopret.data(); + price = 0; + if ( script[0] == EVAL_REWARDS ) + { + funcid = script[1]; + //fprintf(stderr,"decode.[%c]\n",funcid); + switch ( funcid ) + { + case 't': case 'x': case 'o': + if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits) != 0 ) + return(funcid); + break; + default: + fprintf(stderr,"DecodeRewardsOpRet: illegal funcid.%02x\n",funcid); + funcid = 0; + break; + } + } + return(funcid); +} + uint64_t IsRewardsvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v) { char destaddr[64]; @@ -167,6 +204,7 @@ uint64_t RewardsPlanFunds(uint64_t &refsbits,struct CCcontract_info *cp,CPubKey { if ( (nValue= IsRewardsvout(cp,vintx,(int32_t)it->first.index)) > 0 ) { + totalinputs += nValue; } } @@ -191,8 +229,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk)); mtx.vout.push_back(CTxOut(amount+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - opret << OP_RETURN << E_MARSHAL(ss << cp->evalcode << 'U'); - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsCreateOpRet('U',sbits)); } } else fprintf(stderr,"cant find rewards inputs\n"); return(0); @@ -213,8 +250,7 @@ std::string RewardsFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR mtx.vout.push_back(CTxOut(minseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); mtx.vout.push_back(CTxOut(maxseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); mtx.vout.push_back(CTxOut(mindeposit,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); - opret << OP_RETURN << E_MARSHAL(ss << cp->evalcode << 'F' << sbits); - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsCreateOpRet('F',sbits))); } return(0); } @@ -232,8 +268,7 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint64_t amount) if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,rewardspk)); - opret << OP_RETURN << E_MARSHAL(ss << cp->evalcode << 'L' << sbits); - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsCreateOpRet('L',sbits))); } else fprintf(stderr,"cant find rewards inputs\n"); } return(0); diff --git a/src/main.cpp b/src/main.cpp index 1e3ee8019..b5eb93413 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3474,7 +3474,7 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock * if ( KOMODO_LONGESTCHAIN != 0 && pindexNew->nHeight >= KOMODO_LONGESTCHAIN ) KOMODO_INSYNC = 1; else KOMODO_INSYNC = 0; - fprintf(stderr,"connect.%d insync.%d\n",(int32_t)pindexNew->nHeight,KOMODO_INSYNC); + //fprintf(stderr,"connect.%d insync.%d\n",(int32_t)pindexNew->nHeight,KOMODO_INSYNC); if ( ASSETCHAINS_SYMBOL[0] == 0 && KOMODO_INSYNC != 0 ) komodo_broadcast(pblock,8); return true; diff --git a/src/rpcnet.cpp b/src/rpcnet.cpp index 233aeb2c8..db6ffbc14 100644 --- a/src/rpcnet.cpp +++ b/src/rpcnet.cpp @@ -197,7 +197,7 @@ int32_t komodo_longestchain() if ( num > (n >> 1) ) { extern char ASSETCHAINS_SYMBOL[]; - if ( height != KOMODO_LONGESTCHAIN ) + if ( 0 && height != KOMODO_LONGESTCHAIN ) fprintf(stderr,"set %s KOMODO_LONGESTCHAIN <- %d\n",ASSETCHAINS_SYMBOL,height); KOMODO_LONGESTCHAIN = height; return(height); From 85c54375ceae15c56ca01fb6a7e87926d8199631 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 03:23:26 -1100 Subject: [PATCH 007/204] Sbits --- src/cc/rewards.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 8059564de..bfb4854d6 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -64,7 +64,7 @@ uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint64_t &sbits) std::vector vopret; uint8_t funcid=0,*script,e,f; GetOpReturnData(scriptPubKey, vopret); script = (uint8_t *)vopret.data(); - price = 0; + sbits = 0; if ( script[0] == EVAL_REWARDS ) { funcid = script[1]; @@ -214,12 +214,13 @@ uint64_t RewardsPlanFunds(uint64_t &refsbits,struct CCcontract_info *cp,CPubKey std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid) { - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t reward,amount,inputs,CCchange=0; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,reward,amount,inputs,CCchange=0; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; rewardspk = GetUnspendable(cp,0); mypk = pubkey2pk(Mypubkey()); + funding = RewardsPlanFunds(sbits,cp,rewardspk,planstr); if ( (amount= AddRewardsInputs(cp,mtx,mypk,(1LL << 30),1)) > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash)) > txfee ) { if ( (inputs= AddRewardsInputs(cp,mtx,mypk,reward+amount+txfee,30)) > 0 ) @@ -237,12 +238,13 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid) std::string RewardsFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) { - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); + funding = RewardsPlanFunds(sbits,cp,rewardspk,planstr); if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,funds,rewardspk)); @@ -259,12 +261,12 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint64_t amount) { CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); + if ( txfee == 0 ) + txfee = 10000; mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,planstr)) >= amount ) { - if ( txfee == 0 ) - txfee = 10000; if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,rewardspk)); From 4f0f369ac2f41e10a9f2a32aa9a103c6db1bc41f Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 03:24:32 -1100 Subject: [PATCH 008/204] Fix --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index bfb4854d6..4f957dca3 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -214,7 +214,7 @@ uint64_t RewardsPlanFunds(uint64_t &refsbits,struct CCcontract_info *cp,CPubKey std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid) { - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,reward,amount,inputs,CCchange=0; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,sbits,reward,amount,inputs,CCchange=0; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; From e68b1b002670f4463b867f1d9354fb2a294e6dd6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 03:25:16 -1100 Subject: [PATCH 009/204] EncodeRewardsOpRet --- src/cc/rewards.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 4f957dca3..37465f4a3 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -230,7 +230,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk)); mtx.vout.push_back(CTxOut(amount+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsCreateOpRet('U',sbits)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits)); } } else fprintf(stderr,"cant find rewards inputs\n"); return(0); @@ -252,7 +252,7 @@ std::string RewardsFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR mtx.vout.push_back(CTxOut(minseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); mtx.vout.push_back(CTxOut(maxseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); mtx.vout.push_back(CTxOut(mindeposit,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsCreateOpRet('F',sbits))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('F',sbits))); } return(0); } @@ -270,7 +270,7 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint64_t amount) if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,rewardspk)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsCreateOpRet('L',sbits))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('L',sbits))); } else fprintf(stderr,"cant find rewards inputs\n"); } return(0); From 4cb33e3741e4fbfa0650888f77953395cae356a3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 03:25:56 -1100 Subject: [PATCH 010/204] ) --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 37465f4a3..b595a0423 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -230,7 +230,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk)); mtx.vout.push_back(CTxOut(amount+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits))); } } else fprintf(stderr,"cant find rewards inputs\n"); return(0); From 0039f78ac682dbc31095a7a07b6d38821a9f1295 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 03:53:52 -1100 Subject: [PATCH 011/204] Fix 'B' remaining_amount --- src/cc/assets.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 9eaf3d9b6..69f82017d 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -132,12 +132,12 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { static uint256 zero; - CTxDestination address; CTransaction vinTx,createTx; uint256 hashBlock,assetid,assetid2; int32_t i,starti,numvins,numvouts,preventCCvins,preventCCvouts; uint64_t amount,remaining_price,nValue,assetoshis,outputs,inputs,tmpprice,totalunits,ignore; std::vector origpubkey,tmporigpubkey,ignorepubkey; uint8_t funcid; char destaddr[64],origaddr[64],CCaddr[64]; + CTxDestination address; CTransaction vinTx,createTx; uint256 hashBlock,assetid,assetid2; int32_t i,starti,numvins,numvouts,preventCCvins,preventCCvouts; uint64_t remaining_price,nValue,assetoshis,outputs,inputs,tmpprice,totalunits,ignore; std::vector origpubkey,tmporigpubkey,ignorepubkey; uint8_t funcid; char destaddr[64],origaddr[64],CCaddr[64]; numvins = tx.vin.size(); numvouts = tx.vout.size(); outputs = inputs = 0; preventCCvins = preventCCvouts = -1; - if ( (funcid= DecodeAssetOpRet(tx.vout[numvouts-1].scriptPubKey,assetid,assetid2,amount,origpubkey)) == 0 ) + if ( (funcid= DecodeAssetOpRet(tx.vout[numvouts-1].scriptPubKey,assetid,assetid2,remaining_price,origpubkey)) == 0 ) return eval->Invalid("Invalid opreturn payload"); fprintf(stderr,"AssetValidate (%c)\n",funcid); if ( eval->GetTxUnconfirmed(assetid,createTx,hashBlock) == 0 ) From 7304b4652f55d41b4cf37f9653cd4cf7ea419fbe Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 04:01:43 -1100 Subject: [PATCH 012/204] Token orders fix --- src/cc/CCassetstx.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index c9405f405..41003a585 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -60,6 +60,8 @@ UniValue AssetOrders(uint256 refassetid) { if ( (funcid= DecodeAssetOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,assetid,assetid2,price,origpubkey)) != 0 ) { + if ( assetid != refassetid ) + continue; UniValue item(UniValue::VOBJ); funcidstr[0] = funcid; funcidstr[1] = 0; From 2e84ef913613f041135f634573cff3b5449c0dd9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 04:14:23 -1100 Subject: [PATCH 013/204] Fix compiler issue --- src/cc/CCcustom.inc | 2 +- src/cc/rewards.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/CCcustom.inc b/src/cc/CCcustom.inc index a5a88f1eb..c33ca4f5b 100644 --- a/src/cc/CCcustom.inc +++ b/src/cc/CCcustom.inc @@ -5,7 +5,7 @@ bool FUNCNAME(CScript const& scriptSig) if (!(cond = GetCryptoCondition(scriptSig))) return false; // Recurse the CC tree to find asset condition - auto findEval = [&] (CC *cond, struct CCVisitor _) { + auto findEval = [] (CC *cond, struct CCVisitor _) { bool r = cc_typeId(cond) == CC_Eval && cond->codeLength == 1 && cond->code[0] == EVALCODE; // false for a match, true for continue return r ? 0 : 1; diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index b595a0423..e21fac274 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -245,7 +245,7 @@ std::string RewardsFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); funding = RewardsPlanFunds(sbits,cp,rewardspk,planstr); - if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) + if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,funds,rewardspk)); mtx.vout.push_back(CTxOut(APR,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); From 88acd162ae2d23f8ad05946f39f3c21b8e3fccad Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 05:09:28 -1100 Subject: [PATCH 014/204] +print --- src/cc/CCassetstx.cpp | 9 ++++++ src/cc/rewards.cpp | 70 +++++++++++++++++++++++-------------------- 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 41003a585..d355fccae 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -61,7 +61,16 @@ UniValue AssetOrders(uint256 refassetid) if ( (funcid= DecodeAssetOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,assetid,assetid2,price,origpubkey)) != 0 ) { if ( assetid != refassetid ) + { + int32_t z; + for (z=31; z>=0; z--) fprintf(stderr,"%02x",((uint8_t *)&txid)[z]); + fprintf(stderr," txid\n"); + for (z=31; z>=0; z--) fprintf(stderr,"%02x",((uint8_t *)&assetid)[z]); + fprintf(stderr," assetid\n"); + for (z=31; z>=0; z--) fprintf(stderr,"%02x",((uint8_t *)&refassetid)[z]); + fprintf(stderr," refassetid\n"); continue; + } UniValue item(UniValue::VOBJ); funcidstr[0] = funcid; funcidstr[1] = 0; diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index e21fac274..ecad222da 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -47,41 +47,44 @@ uint64_t RewardsCalc(uint64_t claim,uint256 txid) // min/max time, mindeposit an return(reward); } -CScript EncodeRewardsOpRet(uint8_t funcid,uint64_t sbits) +CScript EncodeRewardsFundingOpRet(uint8_t funcid,uint64_t sbits,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) { CScript opret; uint8_t evalcode = EVAL_REWARDS; - switch ( funcid ) - { - case 't': case 'x': case 'o': - opret << OP_RETURN << E_MARSHAL(ss << evalcode << funcid << sbits); - break; - } + opret << OP_RETURN << E_MARSHAL(ss << evalcode << 'F' << sbits << APR << minseconds << maxseconds << mindeposit); return(opret); } -uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint64_t &sbits) +uint8_t DecodeRewardsFundingOpRet(const CScript &scriptPubKey,uint64_t &sbits,uint64_t &APR,uint64_t &minseconds,uint64_t &maxseconds,uint64_t &mindeposit) { - std::vector vopret; uint8_t funcid=0,*script,e,f; + std::vector vopret; uint8_t *script,e,f; GetOpReturnData(scriptPubKey, vopret); script = (uint8_t *)vopret.data(); - sbits = 0; - if ( script[0] == EVAL_REWARDS ) + if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> APR; ss >> minseconds; ss >> maxseconds; ss >> mindeposit) != 0 ) { - funcid = script[1]; - //fprintf(stderr,"decode.[%c]\n",funcid); - switch ( funcid ) - { - case 't': case 'x': case 'o': - if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits) != 0 ) - return(funcid); - break; - default: - fprintf(stderr,"DecodeRewardsOpRet: illegal funcid.%02x\n",funcid); - funcid = 0; - break; - } + if ( e == EVAL_REWARDS && f == 'F' ) + return(f); } - return(funcid); + return(0); +} + +CScript EncodeRewardsOpRet(uint8_t funcid,uint64_t sbits) +{ + CScript opret; uint8_t evalcode = EVAL_REWARDS; + opret << OP_RETURN << E_MARSHAL(ss << evalcode << funcid << sbits); + return(opret); +} + +uint8_t DecodeRewardsOpRet(const CScript &scriptPubKey,uint64_t &sbits) +{ + std::vector vopret; uint8_t *script,e,f; + GetOpReturnData(scriptPubKey, vopret); + script = (uint8_t *)vopret.data(); + if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits) != 0 ) + { + if ( e == EVAL_REWARDS && (f == 'L' || f == 'U') ) + return(f); + } + return(0); } uint64_t IsRewardsvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v) @@ -232,7 +235,9 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid) mtx.vout.push_back(CTxOut(amount+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits))); } - } else fprintf(stderr,"cant find rewards inputs\n"); + fprintf(stderr,"cant find enough rewards inputs\n"); + } + fprintf(stderr,"cant find rewards inputs\n"); return(0); } @@ -245,15 +250,13 @@ std::string RewardsFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); funding = RewardsPlanFunds(sbits,cp,rewardspk,planstr); - if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 ) + if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,funds,rewardspk)); - mtx.vout.push_back(CTxOut(APR,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); - mtx.vout.push_back(CTxOut(minseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); - mtx.vout.push_back(CTxOut(maxseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); - mtx.vout.push_back(CTxOut(mindeposit,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('F',sbits))); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsFundingOpRet('F',sbits,APR,minseconds,maxseconds,mindeposit))); } + fprintf(stderr,"cant find enough inputs\n"); return(0); } @@ -271,8 +274,9 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint64_t amount) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,rewardspk)); return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('L',sbits))); - } else fprintf(stderr,"cant find rewards inputs\n"); + } else fprintf(stderr,"cant find enough inputs\n"); } + fprintf(stderr,"cant find rewards inputs\n"); return(0); } From a7c2a2fa82861996b74e07bec70a44fa906f54da Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 05:11:09 -1100 Subject: [PATCH 015/204] Fix missing order book --- src/cc/CCassetstx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index d355fccae..051459f14 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -60,7 +60,7 @@ UniValue AssetOrders(uint256 refassetid) { if ( (funcid= DecodeAssetOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,assetid,assetid2,price,origpubkey)) != 0 ) { - if ( assetid != refassetid ) + if ( refassetid != zero && assetid != refassetid ) { int32_t z; for (z=31; z>=0; z--) fprintf(stderr,"%02x",((uint8_t *)&txid)[z]); From e4276c3aa1708d03fa8e9be774f63d921b4bc474 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 05:11:56 -1100 Subject: [PATCH 016/204] Fix --- src/cc/CCassetstx.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 051459f14..dfe76655c 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -50,6 +50,7 @@ uint64_t GetAssetBalance(CPubKey pk,uint256 tokenid) UniValue AssetOrders(uint256 refassetid) { + static uint256 zero; uint64_t price; uint256 txid,hashBlock,assetid,assetid2; std::vector origpubkey; CTransaction vintx; UniValue result(UniValue::VARR); std::vector > unspentOutputs; uint8_t funcid; char funcidstr[16],origaddr[64],assetidstr[65]; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_ASSETS); SetCCunspents(unspentOutputs,(char *)cp->unspendableCCaddr); From f2b657df15cc1e3035a97bcae6ae19008d5d0a7d Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 05:13:17 -1100 Subject: [PATCH 017/204] -print --- src/cc/CCassetstx.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index dfe76655c..204eba87a 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -63,13 +63,13 @@ UniValue AssetOrders(uint256 refassetid) { if ( refassetid != zero && assetid != refassetid ) { - int32_t z; - for (z=31; z>=0; z--) fprintf(stderr,"%02x",((uint8_t *)&txid)[z]); - fprintf(stderr," txid\n"); - for (z=31; z>=0; z--) fprintf(stderr,"%02x",((uint8_t *)&assetid)[z]); - fprintf(stderr," assetid\n"); - for (z=31; z>=0; z--) fprintf(stderr,"%02x",((uint8_t *)&refassetid)[z]); - fprintf(stderr," refassetid\n"); + //int32_t z; + //for (z=31; z>=0; z--) fprintf(stderr,"%02x",((uint8_t *)&txid)[z]); + //fprintf(stderr," txid\n"); + //for (z=31; z>=0; z--) fprintf(stderr,"%02x",((uint8_t *)&assetid)[z]); + //fprintf(stderr," assetid\n"); + //for (z=31; z>=0; z--) fprintf(stderr,"%02x",((uint8_t *)&refassetid)[z]); + //fprintf(stderr," refassetid\n"); continue; } UniValue item(UniValue::VOBJ); From 43139d3c6d95dcf64a24dd6775ed2380f62979f2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 05:16:47 -1100 Subject: [PATCH 018/204] Filter null outputs --- src/cc/CCassetstx.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 204eba87a..db97ddded 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -72,6 +72,8 @@ UniValue AssetOrders(uint256 refassetid) //fprintf(stderr," refassetid\n"); continue; } + if ( vintx.vout[it->first.index].nValue == 0 ) + continue; UniValue item(UniValue::VOBJ); funcidstr[0] = funcid; funcidstr[1] = 0; From c4e7f6166b5269529bbc880f8c70db0ec5149661 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 05:49:42 -1100 Subject: [PATCH 019/204] Rewards cc --- src/cc/CCrewards.h | 7 ++++--- src/cc/rewards.cpp | 33 +++++++++++++++-------------- src/rpcserver.cpp | 3 ++- src/rpcserver.h | 3 ++- src/wallet/rpcwallet.cpp | 45 +++++++++++++++++++++++++++++----------- 5 files changed, 59 insertions(+), 32 deletions(-) diff --git a/src/cc/CCrewards.h b/src/cc/CCrewards.h index b9addfb95..619bf4ddb 100644 --- a/src/cc/CCrewards.h +++ b/src/cc/CCrewards.h @@ -23,8 +23,9 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); -std::string RewardsFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit); -std::string RewardsLock(uint64_t txfee,char *planstr,uint64_t amount); -std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid); +std::string RewardsCreateFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit); +std::string RewardsAddfunds(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount); +std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount); +std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid); #endif diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index ecad222da..b67829ed9 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -67,19 +67,19 @@ uint8_t DecodeRewardsFundingOpRet(const CScript &scriptPubKey,uint64_t &sbits,ui return(0); } -CScript EncodeRewardsOpRet(uint8_t funcid,uint64_t sbits) +CScript EncodeRewardsOpRet(uint8_t funcid,uint64_t sbits,uint256 fundingtxid) { CScript opret; uint8_t evalcode = EVAL_REWARDS; - opret << OP_RETURN << E_MARSHAL(ss << evalcode << funcid << sbits); + opret << OP_RETURN << E_MARSHAL(ss << evalcode << funcid << sbits << fundingtxid); return(opret); } -uint8_t DecodeRewardsOpRet(const CScript &scriptPubKey,uint64_t &sbits) +uint8_t DecodeRewardsOpRet(const CScript &scriptPubKey,uint64_t &sbits,uint256 &fundingtxid) { std::vector vopret; uint8_t *script,e,f; GetOpReturnData(scriptPubKey, vopret); script = (uint8_t *)vopret.data(); - if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits) != 0 ) + if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> fundingtxid) != 0 ) { if ( e == EVAL_REWARDS && (f == 'L' || f == 'U') ) return(f); @@ -215,25 +215,28 @@ uint64_t RewardsPlanFunds(uint64_t &refsbits,struct CCcontract_info *cp,CPubKey return(totalinputs); } -std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid) +std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid) { - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,sbits,reward,amount,inputs,CCchange=0; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,sbits,reward,amount=0,inputs,CCchange=0; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; rewardspk = GetUnspendable(cp,0); mypk = pubkey2pk(Mypubkey()); - funding = RewardsPlanFunds(sbits,cp,rewardspk,planstr); - if ( (amount= AddRewardsInputs(cp,mtx,mypk,(1LL << 30),1)) > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash)) > txfee ) + funding = RewardsPlanFunds(sbits,cp,rewardspk,planstr,fundingtxid); + if ( locktxid == zeroid ) + amount= AddRewardsInputs(cp,mtx,mypk,(1LL << 30),1); + //else amount = value...; + if ( amount > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash)) > txfee ) { if ( (inputs= AddRewardsInputs(cp,mtx,mypk,reward+amount+txfee,30)) > 0 ) { - if ( inputs > (amount + reward + 2*txfee) ) + if ( inputs >= (amount + reward + 2*txfee) ) CCchange = (inputs - amount - reward - txfee); if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk)); mtx.vout.push_back(CTxOut(amount+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid))); } fprintf(stderr,"cant find enough rewards inputs\n"); } @@ -241,7 +244,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid) return(0); } -std::string RewardsFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) +std::string RewardsCreateFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) { CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); @@ -249,7 +252,7 @@ std::string RewardsFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR txfee = 10000; mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); - funding = RewardsPlanFunds(sbits,cp,rewardspk,planstr); + funding = RewardsPlanFunds(sbits,cp,rewardspk,planstr,zeroid); if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,funds,rewardspk)); @@ -260,7 +263,7 @@ std::string RewardsFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR return(0); } -std::string RewardsLock(uint64_t txfee,char *planstr,uint64_t amount) +std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount) { CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); @@ -268,12 +271,12 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint64_t amount) txfee = 10000; mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); - if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,planstr)) >= amount ) + if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,planstr,fundingtxid)) >= amount ) { if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,rewardspk)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('L',sbits))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('L',sbits,fundingtxid))); } else fprintf(stderr,"cant find enough inputs\n"); } fprintf(stderr,"cant find rewards inputs\n"); diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index afcb5cc59..c31204d29 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -354,7 +354,8 @@ static const CRPCCommand vRPCCommands[] = { "ponzi", "ponziaddress", &ponziaddress, true }, /* rewards */ - { "rewards", "rewardsfund", &rewardsfund, true }, + { "rewards", "rewardscreatefunding", &rewardscreatefunding, true }, + { "rewards", "rewardsaddfunding", &rewardsaddfunding, true }, { "rewards", "rewardslock", &rewardslock, true }, { "rewards", "rewardsunlock", &rewardsunlock, true }, { "rewards", "rewardsaddress", &rewardsaddress, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index fe48bdd74..2796c9476 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -225,7 +225,8 @@ extern UniValue faucetfund(const UniValue& params, bool fHelp); extern UniValue faucetget(const UniValue& params, bool fHelp); extern UniValue faucetaddress(const UniValue& params, bool fHelp); extern UniValue rewardsaddress(const UniValue& params, bool fHelp); -extern UniValue rewardsfund(const UniValue& params, bool fHelp); +extern UniValue rewardscreatefund(const UniValue& params, bool fHelp); +extern UniValue rewardsaddfunding(const UniValue& params, bool fHelp); extern UniValue rewardslock(const UniValue& params, bool fHelp); extern UniValue rewardsunlock(const UniValue& params, bool fHelp); extern UniValue diceaddress(const UniValue& params, bool fHelp); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 489976b78..6354afb3d 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4957,7 +4957,7 @@ UniValue tokenaddress(const UniValue& params, bool fHelp) return(CCaddress(cp,(char *)"Assets",pubkey)); } -UniValue rewardsfund(const UniValue& params, bool fHelp) +UniValue rewardscreatefund(const UniValue& params, bool fHelp) { UniValue result(UniValue::VOBJ); char *name; uint64_t funds,APR,minseconds,maxseconds,mindeposit; std::string hex; if ( fHelp || params.size() > 6 || params.size() < 2 ) @@ -4995,14 +4995,34 @@ UniValue rewardsfund(const UniValue& params, bool fHelp) UniValue rewardslock(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); char *name; uint64_t amount; std::string hex; - if ( fHelp || params.size() > 2 ) - throw runtime_error("rewardslock name amount\n"); + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid; uint64_t amount; std::string hex; + if ( fHelp || params.size() != 3 ) + throw runtime_error("rewardslock name fundingtxid amount\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"); name = (char *)params[0].get_str().c_str(); - amount = atof(params[1].get_str().c_str()) * COIN; - hex = RewardsLock(0,name,amount); + fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); + amount = atof(params[2].get_str().c_str()) * COIN; + hex = RewardsLock(0,name,fundingtxid,amount); + if ( hex.size() > 0 ) + { + result.push_back(Pair("result", "success")); + result.push_back(Pair("hex", hex)); + } else result.push_back(Pair("error", "couldnt create rewards lock transaction")); + return(result); +} + +UniValue rewardsaddfunding(const UniValue& params, bool fHelp) +{ + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid; uint64_t amount; std::string hex; + if ( fHelp || params.size() != 3 ) + throw runtime_error("rewardsaddfunding name fundingtxid amount\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"); + 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; + hex = RewardsAddFunds(0,name,fundingtxid,amount); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -5013,16 +5033,17 @@ UniValue rewardslock(const UniValue& params, bool fHelp) UniValue rewardsunlock(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); std::string hex; char *name; uint256 txid; - if ( fHelp || params.size() > 2 ) - throw runtime_error("rewardsunlock name [txid]\n"); + UniValue result(UniValue::VOBJ); std::string hex; char *name; uint256 fundingtxid,txid; + if ( fHelp || params.size() > 3 ) + throw runtime_error("rewardsunlock name fundingtxid [txid]\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"); name = (char *)params[0].get_str().c_str(); - if ( params.size() > 1 ) - txid = Parseuint256((char *)params[1].get_str().c_str()); + fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); + if ( params.size() > 2 ) + txid = Parseuint256((char *)params[2].get_str().c_str()); else memset(&txid,0,sizeof(txid)); - hex = RewardsUnlock(0,name,txid); + hex = RewardsUnlock(0,name,fundingtxid,txid); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); From 2160b65bde08ecff2351946d86953a2b7a535de1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 05:51:22 -1100 Subject: [PATCH 020/204] Fix --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index b67829ed9..054fdfde8 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -192,7 +192,7 @@ uint64_t AddRewardsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CP return(totalinputs); } -uint64_t RewardsPlanFunds(uint64_t &refsbits,struct CCcontract_info *cp,CPubKey &pk,char *planstr) +uint64_t RewardsPlanFunds(uint64_t &refsbits,struct CCcontract_info *cp,CPubKey &pk,char *planstr,uint256 fundingtxid) { char coinaddr[64]; uint64_t sbits,nValue,totalinputs = 0; uint256 hashBlock; CTransaction vintx; std::vector > unspentOutputs; From fc3c4d7a63b4ae077dafe5714d73f268b1a60998 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 05:53:24 -1100 Subject: [PATCH 021/204] Linker --- src/rpcserver.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpcserver.h b/src/rpcserver.h index 2796c9476..17b6bf812 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -225,7 +225,7 @@ extern UniValue faucetfund(const UniValue& params, bool fHelp); extern UniValue faucetget(const UniValue& params, bool fHelp); extern UniValue faucetaddress(const UniValue& params, bool fHelp); extern UniValue rewardsaddress(const UniValue& params, bool fHelp); -extern UniValue rewardscreatefund(const UniValue& params, bool fHelp); +extern UniValue rewardscreatefunding(const UniValue& params, bool fHelp); extern UniValue rewardsaddfunding(const UniValue& params, bool fHelp); extern UniValue rewardslock(const UniValue& params, bool fHelp); extern UniValue rewardsunlock(const UniValue& params, bool fHelp); From e95b95821eae175b7fd90c0a81fc37d91e57985d Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 05:59:14 -1100 Subject: [PATCH 022/204] Fix --- src/cc/CCrewards.h | 4 ++-- src/cc/rewards.cpp | 22 +++++++++++++++++++++- src/wallet/rpcwallet.cpp | 4 ++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/cc/CCrewards.h b/src/cc/CCrewards.h index 619bf4ddb..fe9c33b07 100644 --- a/src/cc/CCrewards.h +++ b/src/cc/CCrewards.h @@ -23,8 +23,8 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); -std::string RewardsCreateFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit); -std::string RewardsAddfunds(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount); +std::string RewardsCreateFunding(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit); +std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount); std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount); std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid); diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 054fdfde8..01228b5f3 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -244,7 +244,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 return(0); } -std::string RewardsCreateFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) +std::string RewardsCreateFunding(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) { CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); @@ -263,6 +263,26 @@ std::string RewardsCreateFund(uint64_t txfee,char *planstr,uint64_t funds,uint64 return(0); } +std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount) +{ + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_REWARDS); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + rewardspk = GetUnspendable(cp,0); + if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,planstr,fundingtxid)) >= amount ) + { + if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 ) + { + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,rewardspk)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('L',sbits,fundingtxid))); + } else fprintf(stderr,"cant find enough inputs\n"); + } + fprintf(stderr,"cant find rewards inputs\n"); + return(0); +} + std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount) { CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding; struct CCcontract_info *cp,C; diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 6354afb3d..d4fc9605b 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4957,7 +4957,7 @@ UniValue tokenaddress(const UniValue& params, bool fHelp) return(CCaddress(cp,(char *)"Assets",pubkey)); } -UniValue rewardscreatefund(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; if ( fHelp || params.size() > 6 || params.size() < 2 ) @@ -5022,7 +5022,7 @@ UniValue rewardsaddfunding(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; - hex = RewardsAddFunds(0,name,fundingtxid,amount); + hex = RewardsAddfunding(0,name,fundingtxid,amount); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); From 4f394f447769f5ac5f0e80e7d9328f3f9bc6c89c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 06:00:30 -1100 Subject: [PATCH 023/204] Fix --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index d4fc9605b..6be760fae 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4984,7 +4984,7 @@ UniValue rewardscreatefunding(const UniValue& params, bool fHelp) } } } - hex = RewardsFund(0,name,funds,APR,minseconds,maxseconds,mindeposit); + hex = RewardsCreateFunding(0,name,funds,APR,minseconds,maxseconds,mindeposit); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); From a02c45db891b38b243f4bcd28f94d7e61c97055d Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 06:14:36 -1100 Subject: [PATCH 024/204] KOMODO_LONGESTCHAIN --- src/miner.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index 93981c552..5590a26ad 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -106,7 +106,7 @@ void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, #include "komodo_defs.h" -extern int32_t ASSETCHAINS_SEED,IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAIN_INIT,KOMODO_INITDONE,KOMODO_ON_DEMAND,KOMODO_INITDONE,KOMODO_PASSPORT_INITDONE; +extern int32_t KOMODO_LONGESTCHAIN,ASSETCHAINS_SEED,IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAIN_INIT,KOMODO_INITDONE,KOMODO_ON_DEMAND,KOMODO_INITDONE,KOMODO_PASSPORT_INITDONE; extern uint64_t ASSETCHAINS_REWARD,ASSETCHAINS_COMMISSION,ASSETCHAINS_STAKED; extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; extern std::string NOTARY_PUBKEY,ASSETCHAINS_OVERRIDE_PUBKEY; @@ -913,7 +913,9 @@ void static BitcoinMiner() { if ( KOMODO_INSYNC == 0 ) { - fprintf(stderr,"Mining when blockchain might not be in sync\n"); + fprintf(stderr,"Mining when blockchain might not be in sync longest.%d vs %d\n",KOMODO_LONGESTCHAIN,(int32_t)pindexNew->nHeight); + if ( KOMODO_LONGESTCHAIN != 0 && pindexNew->nHeight >= KOMODO_LONGESTCHAIN ) + KOMODO_INSYNC = 1; sleep(3); } // Hash state From e9d56b2cda7cdd31306f36aaa36ed29983e83dab Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 06:15:09 -1100 Subject: [PATCH 025/204] Mining_height --- src/miner.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index 5590a26ad..1750e27ca 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -913,8 +913,8 @@ void static BitcoinMiner() { if ( KOMODO_INSYNC == 0 ) { - fprintf(stderr,"Mining when blockchain might not be in sync longest.%d vs %d\n",KOMODO_LONGESTCHAIN,(int32_t)pindexNew->nHeight); - if ( KOMODO_LONGESTCHAIN != 0 && pindexNew->nHeight >= KOMODO_LONGESTCHAIN ) + fprintf(stderr,"Mining when blockchain might not be in sync longest.%d vs %d\n",KOMODO_LONGESTCHAIN,Mining_height); + if ( KOMODO_LONGESTCHAIN != 0 && Mining_height >= KOMODO_LONGESTCHAIN ) KOMODO_INSYNC = 1; sleep(3); } From eac2c15e5da30704a4214aa6ed2f1a12d3ec92fb Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 07:51:07 -1100 Subject: [PATCH 026/204] Fix dropped assetoshis --- src/cc/CCassetstx.cpp | 4 ++-- src/cc/rewards.cpp | 33 +++++++++++++++++++++++---------- src/wallet/rpcwallet.cpp | 2 +- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index db97ddded..aef215f96 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -252,9 +252,9 @@ std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t mtx.vin.push_back(CTxIn(bidtxid,bidvout,CScript())); if ( (inputs= AddAssetInputs(cp,mtx,mypk,assetid,fillamount,60)) > 0 ) { + SetAssetFillamounts(0,paid_amount,remaining_required,bidamount,fillamount,origprice); if ( inputs > fillamount ) CCchange = (inputs - fillamount); - SetAssetFillamounts(0,paid_amount,remaining_required,bidamount,fillamount,origprice); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,bidamount - paid_amount,GetUnspendable(cp,0))); mtx.vout.push_back(CTxOut(paid_amount,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,fillamount,pubkey2pk(origpubkey))); @@ -287,9 +287,9 @@ std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 ask else inputs = AddNormalinputs(mtx,mypk,fillamount,60); if ( inputs > 0 ) { + SetAssetFillamounts(1,paid_amount,remaining_required,askamount,fillamount,totalunits); if ( assetid2 == zeroid && inputs > fillamount ) CCchange = (inputs - fillamount); - SetAssetFillamounts(1,paid_amount,remaining_required,askamount,fillamount,totalunits); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount - paid_amount,GetUnspendable(cp,0))); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,paid_amount,mypk)); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,fillamount,pubkey2pk(origpubkey))); diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 01228b5f3..65ad8b6f0 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -81,7 +81,7 @@ uint8_t DecodeRewardsOpRet(const CScript &scriptPubKey,uint64_t &sbits,uint256 & script = (uint8_t *)vopret.data(); if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> fundingtxid) != 0 ) { - if ( e == EVAL_REWARDS && (f == 'L' || f == 'U') ) + if ( e == EVAL_REWARDS && (f == 'L' || f == 'U' || f == 'A') ) return(f); } return(0); @@ -215,6 +215,12 @@ uint64_t RewardsPlanFunds(uint64_t &refsbits,struct CCcontract_info *cp,CPubKey return(totalinputs); } +bool RewardsPlanExists(uint64_t sbits) +{ + // get list of all possible plans + // compare sbits +} + std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid) { CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,sbits,reward,amount=0,inputs,CCchange=0; struct CCcontract_info *cp,C; @@ -252,6 +258,11 @@ std::string RewardsCreateFunding(uint64_t txfee,char *planstr,uint64_t funds,uin txfee = 10000; mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); + if ( RewardsPlanExists(sbits) != 0 ) + { + fprintf(stderr,"Rewards plan %s already exists\n",planstr); + return(0); + } funding = RewardsPlanFunds(sbits,cp,rewardspk,planstr,zeroid); if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) { @@ -265,21 +276,23 @@ std::string RewardsCreateFunding(uint64_t txfee,char *planstr,uint64_t funds,uin std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount) { - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); - if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,planstr,fundingtxid)) >= amount ) + if ( RewardsPlanExists(sbits) == 0 ) { - if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 ) - { - mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,rewardspk)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('L',sbits,fundingtxid))); - } else fprintf(stderr,"cant find enough inputs\n"); + fprintf(stderr,"Rewards plan %s doesnt exist\n",planstr); + return(0); } - fprintf(stderr,"cant find rewards inputs\n"); + if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 ) + { + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,rewardspk)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('A',sbits,fundingtxid))); + } else fprintf(stderr,"cant find enough inputs\n"); + fprintf(stderr,"cant find fundingtxid\n"); return(0); } @@ -291,7 +304,7 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_ txfee = 10000; mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); - if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,planstr,fundingtxid)) >= amount ) + if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,planstr,fundingtxid)) >= amount ) // arbitrary cmp { if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 ) { diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 6be760fae..16df9128f 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4961,7 +4961,7 @@ UniValue rewardscreatefunding(const UniValue& params, bool fHelp) { UniValue result(UniValue::VOBJ); char *name; uint64_t funds,APR,minseconds,maxseconds,mindeposit; std::string hex; if ( fHelp || params.size() > 6 || params.size() < 2 ) - throw runtime_error("rewardsfund name amount APR mindays maxdays mindeposit\n"); + throw runtime_error("rewardscreatefunding name amount APR mindays maxdays mindeposit\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"); // default to OOT params From 409643044a5c8e22105aee6705ba20aba164826e Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 08:12:43 -1100 Subject: [PATCH 027/204] Error null CCvin --- src/cc/CCassetsCore.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 466db2d1b..a46e130f9 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -261,6 +261,8 @@ uint64_t AssetValidateCCvin(struct CCcontract_info *cp,Eval* eval,char *CCaddr,c else if ( GetAssetorigaddrs(cp,CCaddr,origaddr,vinTx) == 0 ) return eval->Invalid("couldnt get origaddr for buyvin"); fprintf(stderr,"Got %.8f to origaddr.(%s)\n",(double)vinTx.vout[tx.vin[vini].prevout.n].nValue/COIN,origaddr); + if ( vinTx.vout[0].nValue == 0 ) + return eval->Invalid("null value CCvin"); return(vinTx.vout[0].nValue); } From a8c3f8fa0358ab0453d7795bbf55f3bee1c1f8d1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 08:25:25 -1100 Subject: [PATCH 028/204] Test --- src/cc/assets.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 69f82017d..41efe7a58 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -254,6 +254,9 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("illegal null remaining_price for selloffer"); else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) return eval->Invalid("mismatched vout0 AssetsCCaddr for selloffer"); + fprintf(stderr,"remaining.%d for sell\n",remaining_price); + if ( funcid == 's' ) + preventCCvins = 1; preventCCvouts = 1; break; From 6ea3b878be30f987dc1143265f281f2ded6fd224 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 08:26:21 -1100 Subject: [PATCH 029/204] Test --- src/cc/assets.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 41efe7a58..f26a56a5b 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -254,7 +254,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("illegal null remaining_price for selloffer"); else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) return eval->Invalid("mismatched vout0 AssetsCCaddr for selloffer"); - fprintf(stderr,"remaining.%d for sell\n",remaining_price); + fprintf(stderr,"remaining.%d for sell\n",(int32_t)remaining_price); if ( funcid == 's' ) preventCCvins = 1; preventCCvouts = 1; From f7a6568660e7c20466bb4884a387bff0b1f72730 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 08:29:20 -1100 Subject: [PATCH 030/204] Test --- src/cc/assets.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index f26a56a5b..7b00af242 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -255,8 +255,6 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) return eval->Invalid("mismatched vout0 AssetsCCaddr for selloffer"); fprintf(stderr,"remaining.%d for sell\n",(int32_t)remaining_price); - if ( funcid == 's' ) - preventCCvins = 1; preventCCvouts = 1; break; From 9f5388d7506766ad870f614532fa11c0e4c4ee5b Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 08:31:12 -1100 Subject: [PATCH 031/204] Fix sell --- src/cc/assets.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 7b00af242..73af4ca9c 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -254,8 +254,8 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("illegal null remaining_price for selloffer"); else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) return eval->Invalid("mismatched vout0 AssetsCCaddr for selloffer"); - fprintf(stderr,"remaining.%d for sell\n",(int32_t)remaining_price); - preventCCvouts = 1; + //fprintf(stderr,"remaining.%d for sell\n",(int32_t)remaining_price); + preventCCvouts = 2; break; case 'x': // cancel From 05e35a3364fd0c0e4af2ed625b7348c85a5e0418 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 08:52:44 -1100 Subject: [PATCH 032/204] Fix order book prints --- src/cc/CCassetstx.cpp | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index aef215f96..a63f5f971 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -51,7 +51,7 @@ uint64_t GetAssetBalance(CPubKey pk,uint256 tokenid) UniValue AssetOrders(uint256 refassetid) { static uint256 zero; - uint64_t price; uint256 txid,hashBlock,assetid,assetid2; std::vector origpubkey; CTransaction vintx; UniValue result(UniValue::VARR); std::vector > unspentOutputs; uint8_t funcid; char funcidstr[16],origaddr[64],assetidstr[65]; struct CCcontract_info *cp,C; + uint64_t price; uint256 txid,hashBlock,assetid,assetid2; std::vector origpubkey; CTransaction vintx; UniValue result(UniValue::VARR); std::vector > unspentOutputs; uint8_t funcid; char numstr[32],funcidstr[16],origaddr[64],assetidstr[65]; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_ASSETS); SetCCunspents(unspentOutputs,(char *)cp->unspendableCCaddr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) @@ -80,10 +80,18 @@ UniValue AssetOrders(uint256 refassetid) item.push_back(Pair("funcid", funcidstr)); item.push_back(Pair("txid", uint256_str(assetidstr,txid))); item.push_back(Pair("vout", (int64_t)it->first.index)); - item.push_back(Pair("amount", (double)vintx.vout[it->first.index].nValue/COIN)); + sprintf(numstr,"%.8f",(double)vintx.vout[it->first.index].nValue/COIN); + item.push_back(Pair("amount",numstr)); if ( funcid == 'b' || funcid == 'B' ) - item.push_back(Pair("bidamount",(double)vintx.vout[0].nValue/COIN)); - else item.push_back(Pair("askamount",(double)vintx.vout[0].nValue)); + { + sprintf(numstr,"%.8f",(double)vintx.vout[0].nValue/COIN); + item.push_back(Pair("bidamount",numstr)); + } + else + { + sprintf(numstr,"%.8f",(double)vintx.vout[0].nValue); + item.push_back(Pair("askamount",numstr)); + } if ( origpubkey.size() == 33 ) { GetCCaddress(cp,origaddr,pubkey2pk(origpubkey)); @@ -96,7 +104,16 @@ UniValue AssetOrders(uint256 refassetid) if ( price > 0 ) { item.push_back(Pair("totalrequired", (int64_t)price)); - item.push_back(Pair("price", (double)vintx.vout[0].nValue / (price * COIN))); + if ( funcid == 's' || funcid == 'S' || funcid == 'e' || funcid == 'e' ) + { + sprintf(numstr,"%.8f",(double)price / vintx.vout[0].nValue)); + item.push_back(Pair("price", (double)price / vintx.vout[0].nValue)); + } + else + { + sprintf(numstr,"%.8f",(double)vintx.vout[0].nValue / (price * COIN)); + item.push_back(Pair("price",numstr)); + } } result.push_back(item); //fprintf(stderr,"func.(%c) %s/v%d %.8f\n",funcid,uint256_str(assetidstr,txid),(int32_t)it->first.index,(double)vintx.vout[it->first.index].nValue/COIN); From 4c0f3c874566c269ffc9b564e8d3edeb21319024 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 08:53:18 -1100 Subject: [PATCH 033/204] ) --- src/cc/CCassetstx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index a63f5f971..3931be0e4 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -106,7 +106,7 @@ UniValue AssetOrders(uint256 refassetid) item.push_back(Pair("totalrequired", (int64_t)price)); if ( funcid == 's' || funcid == 'S' || funcid == 'e' || funcid == 'e' ) { - sprintf(numstr,"%.8f",(double)price / vintx.vout[0].nValue)); + sprintf(numstr,"%.8f",(double)price / vintx.vout[0].nValue); item.push_back(Pair("price", (double)price / vintx.vout[0].nValue)); } else From 289636be6d9fc969c83ca015dbb2f593f3cbe09b Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 08:57:24 -1100 Subject: [PATCH 034/204] Test --- src/cc/CCassetstx.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 3931be0e4..0328d5404 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -103,14 +103,16 @@ UniValue AssetOrders(uint256 refassetid) item.push_back(Pair("otherid",uint256_str(assetidstr,assetid2))); if ( price > 0 ) { - item.push_back(Pair("totalrequired", (int64_t)price)); if ( funcid == 's' || funcid == 'S' || funcid == 'e' || funcid == 'e' ) { + sprintf(numstr,"%.8f",(double)price / COIN); + item.push_back(Pair("totalrequired", numstr)); sprintf(numstr,"%.8f",(double)price / vintx.vout[0].nValue); - item.push_back(Pair("price", (double)price / vintx.vout[0].nValue)); + item.push_back(Pair("price", numstr)); } else { + item.push_back(Pair("totalrequired", (int64_t)price)); sprintf(numstr,"%.8f",(double)vintx.vout[0].nValue / (price * COIN)); item.push_back(Pair("price",numstr)); } From ef6cece390f709ee89df30a5337942d2281c2eaa Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 09:00:54 -1100 Subject: [PATCH 035/204] Test --- src/cc/CCassetstx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 0328d5404..fd57ba007 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -107,7 +107,7 @@ UniValue AssetOrders(uint256 refassetid) { sprintf(numstr,"%.8f",(double)price / COIN); item.push_back(Pair("totalrequired", numstr)); - sprintf(numstr,"%.8f",(double)price / vintx.vout[0].nValue); + sprintf(numstr,"%.8f",(double)price / (COIN * vintx.vout[0].nValue)); item.push_back(Pair("price", numstr)); } else From 76a601923df971d15c0ef265820aa47a17bcea97 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 09:04:01 -1100 Subject: [PATCH 036/204] Fix order display --- src/cc/CCassetstx.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index fd57ba007..f09778376 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -80,16 +80,18 @@ UniValue AssetOrders(uint256 refassetid) item.push_back(Pair("funcid", funcidstr)); item.push_back(Pair("txid", uint256_str(assetidstr,txid))); item.push_back(Pair("vout", (int64_t)it->first.index)); - sprintf(numstr,"%.8f",(double)vintx.vout[it->first.index].nValue/COIN); - item.push_back(Pair("amount",numstr)); if ( funcid == 'b' || funcid == 'B' ) { + sprintf(numstr,"%.8f",(double)vintx.vout[it->first.index].nValue/COIN); + item.push_back(Pair("amount",numstr)); sprintf(numstr,"%.8f",(double)vintx.vout[0].nValue/COIN); item.push_back(Pair("bidamount",numstr)); } else { - sprintf(numstr,"%.8f",(double)vintx.vout[0].nValue); + sprintf(numstr,"%llu",(long long)vintx.vout[it->first.index].nValue); + item.push_back(Pair("amount",numstr)); + sprintf(numstr,"%llu",(long long)vintx.vout[0].nValue); item.push_back(Pair("askamount",numstr)); } if ( origpubkey.size() == 33 ) From 1fcdefbbb16af16657fe445e10d02c380fb15782 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 09:25:31 -1100 Subject: [PATCH 037/204] Fix oops --- src/cc/CCassetstx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index f09778376..f70080ec2 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -303,7 +303,7 @@ std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 ask askamount = vintx.vout[askvout].nValue; SetAssetOrigpubkey(origpubkey,totalunits,vintx); mtx.vin.push_back(CTxIn(asktxid,askvout,CScript())); - if ( assetid2 == zeroid ) + if ( assetid2 != zeroid ) inputs = AddAssetInputs(cp,mtx,mypk,assetid2,fillamount,60); else inputs = AddNormalinputs(mtx,mypk,fillamount,60); if ( inputs > 0 ) From c6c490af1d8e466fdc26ba7405149b7dfc0533a1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 09:33:23 -1100 Subject: [PATCH 038/204] Fix duplicate vin --- src/cc/CCassetstx.cpp | 12 +++++++++--- src/cc/CCtx.cpp | 11 +++++++++-- src/cc/CCutils.cpp | 1 - src/cc/auction.cpp | 1 + src/cc/dice.cpp | 1 + src/cc/faucet.cpp | 1 + src/cc/lotto.cpp | 1 + src/cc/ponzi.cpp | 1 + src/cc/rewards.cpp | 1 + 9 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index f70080ec2..b4185bfae 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -17,19 +17,25 @@ uint64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint256 assetid,uint64_t total,int32_t maxinputs) { - char coinaddr[64]; uint64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t n = 0; + char coinaddr[64]; uint64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t j,vout,n = 0; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; + vout = (int32_t)it->first.index; + for (j=0; jfirst.index,assetid)) > 0 ) + if ( (nValue= IsAssetvout(price,origpubkey,vintx,vout,assetid)) > 0 ) { if ( total != 0 && maxinputs != 0 ) - mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript())); + mtx.vin.push_back(CTxIn(txid,vout,CScript())); nValue = it->second.satoshis; totalinputs += nValue; n++; diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index b37e6f061..3f871de74 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -165,7 +165,7 @@ void SetCCunspents(std::vector vecOutputs; + int32_t vout,j,n = 0; uint64_t nValue,totalinputs = 0; uint256 txid; std::vector vecOutputs; #ifdef ENABLE_WALLET const CKeyStore& keystore = *pwalletMain; assert(pwalletMain != NULL); @@ -175,7 +175,14 @@ uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,in { if ( out.fSpendable != 0 ) { - mtx.vin.push_back(CTxIn(out.tx->GetHash(),out.i,CScript())); + txid = out.tx->GetHash(); + vout = out.i; + for (j=0; jvout[out.i].nValue; totalinputs += nValue; n++; diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index f8617e536..d9672c61d 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -257,5 +257,4 @@ bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector param } -uint64_t AddFaucetInputs(CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs); diff --git a/src/cc/auction.cpp b/src/cc/auction.cpp index a11f66e85..1be6a50aa 100644 --- a/src/cc/auction.cpp +++ b/src/cc/auction.cpp @@ -126,6 +126,7 @@ uint64_t AddAuctionInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CP for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; + // prevent dup if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { if ( (nValue= IsAuctionvout(cp,vintx,(int32_t)it->first.index)) > 0 ) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index f76fc02ab..a551fed1c 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -126,6 +126,7 @@ uint64_t AddDiceInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubK for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; + // prevent dup if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { if ( (nValue= IsDicevout(cp,vintx,(int32_t)it->first.index)) > 0 ) diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index 00b01d76f..b9071e621 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -133,6 +133,7 @@ uint64_t AddFaucetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPu for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; + // prevent dup if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { if ( (nValue= IsFaucetvout(cp,vintx,(int32_t)it->first.index)) > 0 ) diff --git a/src/cc/lotto.cpp b/src/cc/lotto.cpp index af6222b5d..ee8db7c39 100644 --- a/src/cc/lotto.cpp +++ b/src/cc/lotto.cpp @@ -126,6 +126,7 @@ uint64_t AddLottoInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPub for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; + // prevent dup if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { if ( (nValue= IsLottovout(cp,vintx,(int32_t)it->first.index)) > 0 ) diff --git a/src/cc/ponzi.cpp b/src/cc/ponzi.cpp index 81021209e..19e00bc2b 100644 --- a/src/cc/ponzi.cpp +++ b/src/cc/ponzi.cpp @@ -126,6 +126,7 @@ uint64_t AddPonziInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPub for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; + // prevent dup if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { if ( (nValue= IsPonzivout(cp,vintx,(int32_t)it->first.index)) > 0 ) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 65ad8b6f0..17c838c51 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -176,6 +176,7 @@ uint64_t AddRewardsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CP for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; + // prevent dup if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { if ( (nValue= IsRewardsvout(cp,vintx,(int32_t)it->first.index)) > 0 ) From 8d44e2c321ecdbeb3cb6f76b2ccbe941bfe04de8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 09:36:23 -1100 Subject: [PATCH 039/204] Remove dust check --- src/cc/CCassetsCore.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index a46e130f9..f6574ba8c 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -162,7 +162,7 @@ uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint256 &assetid,uint256 &a case 'E': case 'e': if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> assetid; ss >> assetid2; ss >> price; ss >> origpubkey) != 0 ) { - fprintf(stderr,"got price %llu\n",(long long)price); + //fprintf(stderr,"got price %llu\n",(long long)price); assetid = revuint256(assetid); assetid2 = revuint256(assetid2); return(funcid); @@ -256,8 +256,8 @@ uint64_t AssetValidateCCvin(struct CCcontract_info *cp,Eval* eval,char *CCaddr,c fprintf(stderr,"%s vs %s\n",destaddr,(char *)cp->unspendableCCaddr); return eval->Invalid("invalid vin AssetsCCaddr"); } - else if ( vinTx.vout[0].nValue < 10000 ) - return eval->Invalid("invalid dust for buyvin"); + //else if ( vinTx.vout[0].nValue < 10000 ) + // return eval->Invalid("invalid dust for buyvin"); else if ( GetAssetorigaddrs(cp,CCaddr,origaddr,vinTx) == 0 ) return eval->Invalid("couldnt get origaddr for buyvin"); fprintf(stderr,"Got %.8f to origaddr.(%s)\n",(double)vinTx.vout[tx.vin[vini].prevout.n].nValue/COIN,origaddr); From aaff16ceb97e39f7ba4ef33b32b1a115a1299081 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 09:39:34 -1100 Subject: [PATCH 040/204] Fix silly bug --- src/cc/CCassetsCore.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index f6574ba8c..74aa3f911 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -294,7 +294,7 @@ uint64_t AssetValidateSellvin(struct CCcontract_info *cp,Eval* eval,uint64_t &tm fprintf(stderr,"AssetValidateSellvin\n"); if ( (nValue= AssetValidateCCvin(cp,eval,CCaddr,origaddr,tx,1,vinTx)) == 0 ) return(0); - if ( (assetoshis= IsAssetvout(tmpprice,tmporigpubkey,vinTx,0,assetid)) != 0 ) + if ( (assetoshis= IsAssetvout(tmpprice,tmporigpubkey,vinTx,0,assetid)) == 0 ) return eval->Invalid("invalid missing CC vout0 for sellvin"); else return(assetoshis); } From c2fb22315d36d7522bee040b040d4ddbf3614705 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 09:48:01 -1100 Subject: [PATCH 041/204] Test --- src/cc/assets.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 73af4ca9c..1aea32e00 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -298,7 +298,9 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("mismatched origpubkeys for fill"); else { - if ( ValidateAssetRemainder(1,remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) + //ValidateAssetRemainder: orig_nValue == 10 || received_nValue == 0 || paidunits == 10 || totalunits == 100000000000 + //bool ValidateAssetRemainder(int32_t sellflag,uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidunits,uint64_t totalunits) + if ( ValidateAssetRemainder(1,remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[2].nValue,tx.vout[1].nValue,totalunits) == false ) return eval->Invalid("mismatched remainder for fill"); else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 ) return eval->Invalid("normal vout1 for fillask"); From 1c7b71487c780bc676a2106503bb8e90db5c8d14 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 09:55:48 -1100 Subject: [PATCH 042/204] Test --- src/cc/CCassetsCore.cpp | 1 + src/cc/CCassetstx.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 74aa3f911..16be82ce5 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -93,6 +93,7 @@ bool SetAssetFillamounts(int32_t sellflag,uint64_t &received_nValue,uint64_t &re if ( unitprice > 0 && (received_nValue= (paidunits * unitprice)/COIN) > 0 && received_nValue < orig_nValue ) { remaining_nValue = (orig_nValue - received_nValue); + printf("total.%llu 0 paid.%llu, remaining %llu <- %llu (%llu - %llu)\n",(long long)totalunits,(long long)paidunits,(long long)remaining_nValue,(long long)(orig_nValue - received_nValue),(long long)orig_nValue,(long long)received_nValue) return(ValidateAssetRemainder(sellflag,remaining_price,remaining_nValue,orig_nValue,received_nValue,paidunits,totalunits)); } else return(false); } diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index b4185bfae..62ce31570 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -315,7 +315,7 @@ std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 ask if ( inputs > 0 ) { SetAssetFillamounts(1,paid_amount,remaining_required,askamount,fillamount,totalunits); - if ( assetid2 == zeroid && inputs > fillamount ) + if ( assetid2 != zeroid && inputs > fillamount ) CCchange = (inputs - fillamount); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount - paid_amount,GetUnspendable(cp,0))); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,paid_amount,mypk)); From 7b676bd92a27e2d4fc31de92a0a0877d33709502 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 09:56:47 -1100 Subject: [PATCH 043/204] Fix --- src/cc/CCassetsCore.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 16be82ce5..14226a04f 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -93,7 +93,7 @@ bool SetAssetFillamounts(int32_t sellflag,uint64_t &received_nValue,uint64_t &re if ( unitprice > 0 && (received_nValue= (paidunits * unitprice)/COIN) > 0 && received_nValue < orig_nValue ) { remaining_nValue = (orig_nValue - received_nValue); - printf("total.%llu 0 paid.%llu, remaining %llu <- %llu (%llu - %llu)\n",(long long)totalunits,(long long)paidunits,(long long)remaining_nValue,(long long)(orig_nValue - received_nValue),(long long)orig_nValue,(long long)received_nValue) + printf("total.%llu 0 paid.%llu, remaining %llu <- %llu (%llu - %llu)\n",(long long)totalunits,(long long)paidunits,(long long)remaining_nValue,(long long)(orig_nValue - received_nValue),(long long)orig_nValue,(long long)received_nValue); return(ValidateAssetRemainder(sellflag,remaining_price,remaining_nValue,orig_nValue,received_nValue,paidunits,totalunits)); } else return(false); } From b5269aa620908131ab254d85d85088276e938976 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 10:02:49 -1100 Subject: [PATCH 044/204] Test --- src/cc/CCassetstx.cpp | 3 +++ src/wallet/rpcwallet.cpp | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 62ce31570..2683a13c0 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -314,6 +314,9 @@ std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 ask else inputs = AddNormalinputs(mtx,mypk,fillamount,60); if ( inputs > 0 ) { + if ( inputs < fillamount ) + fillamount = inputs; + fprintf(stderr,"inputs %llu, fillamount.%llu\n",(long long)inputs,(long long)fillamount); SetAssetFillamounts(1,paid_amount,remaining_required,askamount,fillamount,totalunits); if ( assetid2 != zeroid && inputs > fillamount ) CCchange = (inputs - fillamount); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 16df9128f..dcce4324e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5323,7 +5323,7 @@ UniValue tokenfillask(const UniValue& params, bool fHelp) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); tokenid = Parseuint256((char *)params[0].get_str().c_str()); asktxid = Parseuint256((char *)params[1].get_str().c_str()); - fillamount = atol(params[2].get_str().c_str()); + fillamount = atof(params[2].get_str().c_str()) * COIN; hex = FillSell(0,tokenid,zeroid,asktxid,fillamount); if ( hex.size() > 0 ) { From 6e466feb3532441ba4ea5fb6410b98e740ec1ff3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 10:05:49 -1100 Subject: [PATCH 045/204] Test --- src/cc/CCassetstx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 2683a13c0..9d3ca065c 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -326,7 +326,7 @@ std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 ask if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); fprintf(stderr,"remaining %llu -> origpubkey\n",(long long)remaining_required); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet(assetid2==zeroid?'E':'S',assetid,assetid2,remaining_required,origpubkey))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet(assetid2!=zeroid?'E':'S',assetid,assetid2,remaining_required,origpubkey))); } else fprintf(stderr,"filltx not enough utxos\n"); } } From fd165fce3817922f9ddac01139d3b87fb359f6b5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 10:11:49 -1100 Subject: [PATCH 046/204] Test --- src/cc/CCassetsCore.cpp | 1 + src/cc/CCassetstx.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 14226a04f..afc50a703 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -75,6 +75,7 @@ bool ValidateAssetRemainder(int32_t sellflag,uint64_t remaining_price,uint64_t r bool SetAssetFillamounts(int32_t sellflag,uint64_t &received_nValue,uint64_t &remaining_price,uint64_t orig_nValue,uint64_t &paidunits,uint64_t totalunits) { uint64_t remaining_nValue,unitprice; + if ( totalunits == 0 ) { received_nValue = remaining_price = paidunits = 0; diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 9d3ca065c..1ef415b63 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -318,6 +318,7 @@ std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 ask fillamount = inputs; fprintf(stderr,"inputs %llu, fillamount.%llu\n",(long long)inputs,(long long)fillamount); SetAssetFillamounts(1,paid_amount,remaining_required,askamount,fillamount,totalunits); + fprintf(stderr,"paidamount %llu remain %llu, fill %llu\n",(long long)paid_amount,(long long_remaining_required,(long long)fillamount); if ( assetid2 != zeroid && inputs > fillamount ) CCchange = (inputs - fillamount); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount - paid_amount,GetUnspendable(cp,0))); From 08d1ae2554292b30c213913b6365f0bfdb192228 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 10:12:46 -1100 Subject: [PATCH 047/204] Test --- src/cc/CCassetstx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 1ef415b63..4188553e1 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -318,7 +318,7 @@ std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 ask fillamount = inputs; fprintf(stderr,"inputs %llu, fillamount.%llu\n",(long long)inputs,(long long)fillamount); SetAssetFillamounts(1,paid_amount,remaining_required,askamount,fillamount,totalunits); - fprintf(stderr,"paidamount %llu remain %llu, fill %llu\n",(long long)paid_amount,(long long_remaining_required,(long long)fillamount); + fprintf(stderr,"paidamount %llu remain %llu, fill %llu\n",(long long)paid_amount,(long long)remaining_required,(long long)fillamount); if ( assetid2 != zeroid && inputs > fillamount ) CCchange = (inputs - fillamount); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount - paid_amount,GetUnspendable(cp,0))); From ae7a0a5728def0fef65c1489fd353732ea2989cb Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 10:18:33 -1100 Subject: [PATCH 048/204] Test --- src/cc/CCassetsCore.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index afc50a703..892341645 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -74,7 +74,7 @@ bool ValidateAssetRemainder(int32_t sellflag,uint64_t remaining_price,uint64_t r bool SetAssetFillamounts(int32_t sellflag,uint64_t &received_nValue,uint64_t &remaining_price,uint64_t orig_nValue,uint64_t &paidunits,uint64_t totalunits) { - uint64_t remaining_nValue,unitprice; + uint64_t remaining_nValue,unitprice; double dprice; if ( totalunits == 0 ) { @@ -90,8 +90,17 @@ bool SetAssetFillamounts(int32_t sellflag,uint64_t &received_nValue,uint64_t &re return(true); } remaining_price = (totalunits - paidunits); - unitprice = (orig_nValue * COIN) / totalunits; // unit price has 10 decimals precision, eg. unitprice of 100 million is 1 COIN per unit - if ( unitprice > 0 && (received_nValue= (paidunits * unitprice)/COIN) > 0 && received_nValue < orig_nValue ) + if ( sellflag == 0 ) + { + unitprice = (orig_nValue * COIN) / totalunits; + received_nValue = (paidunits * unitprice) / COIN; + } + else + { + dprice = (double)orig_nValue / totalunits; + received_nValue = (paidunits * dprice); + } + if ( unitprice > 0 && received_nValue > 0 && received_nValue <= orig_nValue ) { remaining_nValue = (orig_nValue - received_nValue); printf("total.%llu 0 paid.%llu, remaining %llu <- %llu (%llu - %llu)\n",(long long)totalunits,(long long)paidunits,(long long)remaining_nValue,(long long)(orig_nValue - received_nValue),(long long)orig_nValue,(long long)received_nValue); From 312424d66dc3659542135775a161d9a4de88a04d Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 10:20:47 -1100 Subject: [PATCH 049/204] Test --- src/cc/CCassetsCore.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 892341645..3258f60ae 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -99,6 +99,7 @@ bool SetAssetFillamounts(int32_t sellflag,uint64_t &received_nValue,uint64_t &re { dprice = (double)orig_nValue / totalunits; received_nValue = (paidunits * dprice); + fprintf(stderr,"dprice %.8f orig %llu/total %llu, recv %llu\n",dprice,(long long)orig_nValue,(long long)totalunits,(long long)received_nValue); } if ( unitprice > 0 && received_nValue > 0 && received_nValue <= orig_nValue ) { From f8bcad38ed84eacbbfe44514476869cf1ec31981 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 10:22:45 -1100 Subject: [PATCH 050/204] Test --- src/cc/CCassetsCore.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 3258f60ae..d87dcf5d4 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -97,7 +97,7 @@ bool SetAssetFillamounts(int32_t sellflag,uint64_t &received_nValue,uint64_t &re } else { - dprice = (double)orig_nValue / totalunits; + dprice = (double)(orig_nValue * COIN) / totalunits; received_nValue = (paidunits * dprice); fprintf(stderr,"dprice %.8f orig %llu/total %llu, recv %llu\n",dprice,(long long)orig_nValue,(long long)totalunits,(long long)received_nValue); } From 66d08a364091ddebeb29383c97d846913863122a Mon Sep 17 00:00:00 2001 From: "Jonathan \"Duke\" Leto" Date: Wed, 25 Jul 2018 21:33:01 -0700 Subject: [PATCH 051/204] Fix compiler error: call of overloaded 'Pair(const char [9], time_t)' is ambiguous --- src/rpcmisc.cpp | 2 +- src/txdb.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rpcmisc.cpp b/src/rpcmisc.cpp index 657227173..45b70444b 100644 --- a/src/rpcmisc.cpp +++ b/src/rpcmisc.cpp @@ -1063,7 +1063,7 @@ UniValue getsnapshot(const UniValue& params, bool fHelp) } result = komodo_snapshot(top); if ( result.size() > 0 ) { - result.push_back(Pair("end_time", time(NULL))); + result.push_back(Pair("end_time", (int) time(NULL))); } else { result.push_back(Pair("error", "no addressindex")); } diff --git a/src/txdb.cpp b/src/txdb.cpp index 7ca6175b0..5046381d9 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -407,7 +407,7 @@ extern UniValue CBlockTreeDB::Snapshot(int top) std::map addressAmounts; std::vector > vaddr; UniValue result(UniValue::VOBJ); - result.push_back(Pair("start_time", time(NULL))); + result.push_back(Pair("start_time", (int) time(NULL))); std::map ignoredMap = { {"RReUxSs5hGE39ELU23DfydX8riUuzdrHAE", 1}, From e9ca010084c58d404b9ba4159da5cb58d02955f6 Mon Sep 17 00:00:00 2001 From: "Jonathan \"Duke\" Leto" Date: Wed, 25 Jul 2018 22:36:51 -0700 Subject: [PATCH 052/204] Correctly parse optional top parameter to getsnapshot --- src/rpcmisc.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/rpcmisc.cpp b/src/rpcmisc.cpp index 45b70444b..b15b3083d 100644 --- a/src/rpcmisc.cpp +++ b/src/rpcmisc.cpp @@ -1019,13 +1019,12 @@ UniValue komodo_snapshot(int top); UniValue getsnapshot(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); int64_t total; int top = 0; + UniValue result(UniValue::VOBJ); int64_t total; int32_t top = 0; if (params.size() > 0 && !params[0].isNull()) { - top = params[0].get_str(); - fprintf(stderr, "top=%s\n",top); - if (top <= 0) - throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, top must be a positive integer"); + top = atoi(params[0].get_str().c_str()); + if (top <= 0) + throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, top must be a positive integer"); } if ( fHelp || params.size() > 1) From 19da5c64463be36be9fdac8b4cdc62b7659722c2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 20:18:29 -1100 Subject: [PATCH 053/204] Fix token orders crash --- src/cc/CCassetsCore.cpp | 18 +++++++++++++++--- src/cc/CCassetstx.cpp | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index d87dcf5d4..a49da9beb 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -97,6 +97,18 @@ bool SetAssetFillamounts(int32_t sellflag,uint64_t &received_nValue,uint64_t &re } else { + // ./komodo-cli -ac_name=ATEST tokenfillask 9217014eae0a83a0b64632f379c1b474859794f9eaf1cf1eecf5804ed6124a5e ce7811a63e2d06ec6bde8a553b05b7dca95b17e8a676e431a887135ed62549b7 10 + /*{ + "funcid": "s", + "txid": "ce7811a63e2d06ec6bde8a553b05b7dca95b17e8a676e431a887135ed62549b7", + "vout": 0, + "amount": "10", + "askamount": "10", + "origaddress": "RRPpWbVdxcxmhx4xnWnVZFDfGc9p1177ti", + "tokenid": "9217014eae0a83a0b64632f379c1b474859794f9eaf1cf1eecf5804ed6124a5e", + "totalrequired": "1000.00000000", + "price": "100.00000000" + },*/ dprice = (double)(orig_nValue * COIN) / totalunits; received_nValue = (paidunits * dprice); fprintf(stderr,"dprice %.8f orig %llu/total %llu, recv %llu\n",dprice,(long long)orig_nValue,(long long)totalunits,(long long)received_nValue); @@ -148,7 +160,7 @@ uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint256 &assetid,uint256 &a memset(&assetid,0,sizeof(assetid)); memset(&assetid2,0,sizeof(assetid2)); price = 0; - if ( script[0] == EVAL_ASSETS ) + if ( script != 0 && script[0] == EVAL_ASSETS ) { funcid = script[1]; //fprintf(stderr,"decode.[%c]\n",funcid); @@ -192,7 +204,7 @@ uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint256 &assetid,uint256 &a bool SetAssetOrigpubkey(std::vector &origpubkey,uint64_t &price,const CTransaction &tx) { uint256 assetid,assetid2; - if ( DecodeAssetOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,assetid,assetid2,price,origpubkey) != 0 ) + if ( tx.vout.size() > 0 && DecodeAssetOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,assetid,assetid2,price,origpubkey) != 0 ) return(true); else return(false); } @@ -289,7 +301,7 @@ uint64_t AssetValidateBuyvin(struct CCcontract_info *cp,Eval* eval,uint64_t &tmp else { //fprintf(stderr,"have %.8f checking assetid origaddr.(%s)\n",(double)nValue/COIN,origaddr); - if ( (funcid= DecodeAssetOpRet(vinTx.vout[vinTx.vout.size()-1].scriptPubKey,assetid,assetid2,tmpprice,tmporigpubkey)) != 'b' && funcid != 'B' ) + if ( vinTx.vout.size() > 0 && (funcid= DecodeAssetOpRet(vinTx.vout[vinTx.vout.size()-1].scriptPubKey,assetid,assetid2,tmpprice,tmporigpubkey)) != 'b' && funcid != 'B' ) return eval->Invalid("invalid opreturn for buyvin"); else if ( refassetid != assetid ) return eval->Invalid("invalid assetid for buyvin"); diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 4188553e1..0c0b0923d 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -65,7 +65,7 @@ UniValue AssetOrders(uint256 refassetid) txid = it->first.txhash; if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { - if ( (funcid= DecodeAssetOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,assetid,assetid2,price,origpubkey)) != 0 ) + if ( vintx.vout.size() > 0 && (funcid= DecodeAssetOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,assetid,assetid2,price,origpubkey)) != 0 ) { if ( refassetid != zero && assetid != refassetid ) { From dda4711a587b6665f6fceba00f683c2768325d26 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 23:08:08 -1100 Subject: [PATCH 054/204] Split amounts/validation: bid, ask, swap --- src/cc/CCassets.h | 8 +- src/cc/CCassetsCore.cpp | 160 ++++++++++++++++++++++++++++++++++++---- src/cc/CCassetstx.cpp | 39 +++++----- src/cc/assets.cpp | 73 ++++++++++++++---- 4 files changed, 233 insertions(+), 47 deletions(-) diff --git a/src/cc/CCassets.h b/src/cc/CCassets.h index 0fbe4891c..3a3df3680 100644 --- a/src/cc/CCassets.h +++ b/src/cc/CCassets.h @@ -35,8 +35,12 @@ CScript EncodeAssetOpRet(uint8_t funcid,uint256 assetid,uint256 assetid2,uint64_ uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint256 &assetid,uint256 &assetid2,uint64_t &price,std::vector &origpubkey); bool SetAssetOrigpubkey(std::vector &origpubkey,uint64_t &price,const CTransaction &tx); uint64_t IsAssetvout(uint64_t &price,std::vector &origpubkey,const CTransaction& tx,int32_t v,uint256 refassetid); -bool ValidateAssetRemainder(int32_t sellflag,uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidprice,uint64_t totalprice); -bool SetAssetFillamounts(int32_t sellflag,uint64_t &paid,uint64_t &remaining_price,uint64_t orig_nValue,uint64_t &received,uint64_t totalprice); +bool ValidateBidRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidprice,uint64_t totalprice); +bool ValidateAskRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidprice,uint64_t totalprice); +bool ValidateSwapRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidprice,uint64_t totalprice); +bool SetBidFillamounts(uint64_t &paid,uint64_t &remaining_price,uint64_t orig_nValue,uint64_t &received,uint64_t totalprice); +bool SetAskFillamounts(uint64_t &paid,uint64_t &remaining_price,uint64_t orig_nValue,uint64_t &received,uint64_t totalprice); +bool SetSwapFillamounts(uint64_t &paid,uint64_t &remaining_price,uint64_t orig_nValue,uint64_t &received,uint64_t totalprice); uint64_t AssetValidateBuyvin(struct CCcontract_info *cp,Eval* eval,uint64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 refassetid); uint64_t AssetValidateSellvin(struct CCcontract_info *cp,Eval* eval,uint64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 assetid); bool AssetExactAmounts(struct CCcontract_info *cp,uint64_t &inputs,int32_t starti,uint64_t &outputs,Eval* eval,const CTransaction &tx,uint256 assetid); diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index a49da9beb..660479f0c 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -38,7 +38,34 @@ We assume that the effective unit cost in the orderbook is valid and that that amount was paid and also that any remainder will be close enough in effective unit cost to not matter. At the edge cases, this will probably be not true and maybe some orders wont be practically fillable when reduced to fractional state. However, the original pubkey that created the offer can always reclaim it. */ -bool ValidateAssetRemainder(int32_t sellflag,uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidunits,uint64_t totalunits) +bool SetBidFillamounts(uint64_t &received_nValue,uint64_t &remaining_price,uint64_t orig_nValue,uint64_t &paidunits,uint64_t totalunits) +{ + uint64_t remaining_nValue,unitprice; double dprice; + if ( totalunits == 0 ) + { + received_nValue = remaining_price = paidunits = 0; + return(false); + } + if ( paidunits >= totalunits ) + { + paidunits = totalunits; + received_nValue = orig_nValue; + remaining_price = 0; + fprintf(stderr,"totally filled!\n"); + return(true); + } + remaining_price = (totalunits - paidunits); + unitprice = (orig_nValue * COIN) / totalunits; + received_nValue = (paidunits * unitprice) / COIN; + if ( unitprice > 0 && received_nValue > 0 && received_nValue <= orig_nValue ) + { + remaining_nValue = (orig_nValue - received_nValue); + printf("total.%llu 0 paid.%llu, remaining %llu <- %llu (%llu - %llu)\n",(long long)totalunits,(long long)paidunits,(long long)remaining_nValue,(long long)(orig_nValue - received_nValue),(long long)orig_nValue,(long long)received_nValue); + return(ValidateAssetRemainder(sellflag,remaining_price,remaining_nValue,orig_nValue,received_nValue,paidunits,totalunits)); + } else return(false); +} + +bool ValidateBidRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidunits,uint64_t totalunits) { uint64_t unitprice,recvunitprice,newunitprice=0; if ( orig_nValue == 0 || received_nValue == 0 || paidunits == 0 || totalunits == 0 ) @@ -72,10 +99,83 @@ bool ValidateAssetRemainder(int32_t sellflag,uint64_t remaining_price,uint64_t r return(true); } -bool SetAssetFillamounts(int32_t sellflag,uint64_t &received_nValue,uint64_t &remaining_price,uint64_t orig_nValue,uint64_t &paidunits,uint64_t totalunits) +bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue,uint64_t orig_assetoshis,uint64_t &paid_nValue,uint64_t total_nValue) +{ + uint64_t remaining_nValue,unitprice; double dprice; + if ( totalunits == 0 ) + { + received_nValue = remaining_price = paidunits = 0; + return(false); + } + if ( paidunits >= totalunits ) + { + paidunits = totalunits; + received_nValue = orig_nValue; + remaining_price = 0; + fprintf(stderr,"totally filled!\n"); + return(true); + } + remaining_price = (totalunits - paidunits); + // ./komodo-cli -ac_name=ATEST tokenfillask 9217014eae0a83a0b64632f379c1b474859794f9eaf1cf1eecf5804ed6124a5e ce7811a63e2d06ec6bde8a553b05b7dca95b17e8a676e431a887135ed62549b7 10 + /*{ + "funcid": "s", + "txid": "ce7811a63e2d06ec6bde8a553b05b7dca95b17e8a676e431a887135ed62549b7", + "vout": 0, + "amount": "10", + "askamount": "10", + "origaddress": "RRPpWbVdxcxmhx4xnWnVZFDfGc9p1177ti", + "tokenid": "9217014eae0a83a0b64632f379c1b474859794f9eaf1cf1eecf5804ed6124a5e", + "totalrequired": "1000.00000000", + "price": "100.00000000" + },*/ + dprice = (double)(orig_nValue * COIN) / totalunits; + received_nValue = (paidunits * dprice); + fprintf(stderr,"dprice %.8f orig %llu/total %llu, recv %llu\n",dprice,(long long)orig_nValue,(long long)totalunits,(long long)received_nValue); + if ( unitprice > 0 && received_nValue > 0 && received_nValue <= orig_nValue ) + { + remaining_nValue = (orig_nValue - received_nValue); + printf("total.%llu 0 paid.%llu, remaining %llu <- %llu (%llu - %llu)\n",(long long)totalunits,(long long)paidunits,(long long)remaining_nValue,(long long)(orig_nValue - received_nValue),(long long)orig_nValue,(long long)received_nValue); + return(ValidateAssetRemainder(sellflag,remaining_price,remaining_nValue,orig_nValue,received_nValue,paidunits,totalunits)); + } else return(false); +} + +bool ValidateAskRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidunits,uint64_t totalunits) +{ + uint64_t unitprice,recvunitprice,newunitprice=0; + if ( orig_nValue == 0 || received_nValue == 0 || paidunits == 0 || totalunits == 0 ) + { + fprintf(stderr,"ValidateAssetRemainder: orig_nValue == %llu || received_nValue == %llu || paidunits == %llu || totalunits == %llu\n",(long long)orig_nValue,(long long)received_nValue,(long long)paidunits,(long long)totalunits); + return(false); + } + else if ( totalunits != (remaining_price + paidunits) ) + { + fprintf(stderr,"ValidateAssetRemainder: totalunits %llu != %llu (remaining_price %llu + %llu paidunits)\n",(long long)totalunits,(long long)(remaining_price + paidunits),(long long)remaining_price,(long long)paidunits); + return(false); + } + else if ( orig_nValue != (remaining_nValue + received_nValue) ) + { + fprintf(stderr,"ValidateAssetRemainder: orig_nValue %llu != %llu (remaining_nValue %llu + %llu received_nValue)\n",(long long)orig_nValue,(long long)(remaining_nValue - received_nValue),(long long)remaining_nValue,(long long)received_nValue); + return(false); + } + else + { + unitprice = (orig_nValue * COIN) / totalunits; + recvunitprice = (received_nValue * COIN) / paidunits; + if ( remaining_price != 0 ) + newunitprice = (remaining_nValue * COIN) / remaining_price; + if ( recvunitprice < unitprice ) + { + fprintf(stderr,"error recvunitprice %.16f < %.16f unitprice, new unitprice %.16f\n",(double)recvunitprice/(COIN*COIN),(double)unitprice/(COIN*COIN),(double)newunitprice/(COIN*COIN)); + return(false); + } + fprintf(stderr,"recvunitprice %.16f >= %.16f unitprice, new unitprice %.16f\n",(double)recvunitprice/(COIN*COIN),(double)unitprice/(COIN*COIN),(double)newunitprice/(COIN*COIN)); + } + return(true); +} + +bool SetSwapFillamounts(int32_t sellflag,uint64_t &received_nValue,uint64_t &remaining_price,uint64_t orig_nValue,uint64_t &paidunits,uint64_t totalunits) { uint64_t remaining_nValue,unitprice; double dprice; - if ( totalunits == 0 ) { received_nValue = remaining_price = paidunits = 0; @@ -99,16 +199,16 @@ bool SetAssetFillamounts(int32_t sellflag,uint64_t &received_nValue,uint64_t &re { // ./komodo-cli -ac_name=ATEST tokenfillask 9217014eae0a83a0b64632f379c1b474859794f9eaf1cf1eecf5804ed6124a5e ce7811a63e2d06ec6bde8a553b05b7dca95b17e8a676e431a887135ed62549b7 10 /*{ - "funcid": "s", - "txid": "ce7811a63e2d06ec6bde8a553b05b7dca95b17e8a676e431a887135ed62549b7", - "vout": 0, - "amount": "10", - "askamount": "10", - "origaddress": "RRPpWbVdxcxmhx4xnWnVZFDfGc9p1177ti", - "tokenid": "9217014eae0a83a0b64632f379c1b474859794f9eaf1cf1eecf5804ed6124a5e", - "totalrequired": "1000.00000000", - "price": "100.00000000" - },*/ + "funcid": "s", + "txid": "ce7811a63e2d06ec6bde8a553b05b7dca95b17e8a676e431a887135ed62549b7", + "vout": 0, + "amount": "10", + "askamount": "10", + "origaddress": "RRPpWbVdxcxmhx4xnWnVZFDfGc9p1177ti", + "tokenid": "9217014eae0a83a0b64632f379c1b474859794f9eaf1cf1eecf5804ed6124a5e", + "totalrequired": "1000.00000000", + "price": "100.00000000" + },*/ dprice = (double)(orig_nValue * COIN) / totalunits; received_nValue = (paidunits * dprice); fprintf(stderr,"dprice %.8f orig %llu/total %llu, recv %llu\n",dprice,(long long)orig_nValue,(long long)totalunits,(long long)received_nValue); @@ -121,6 +221,40 @@ bool SetAssetFillamounts(int32_t sellflag,uint64_t &received_nValue,uint64_t &re } else return(false); } +bool ValidateSwapRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidunits,uint64_t totalunits) +{ + uint64_t unitprice,recvunitprice,newunitprice=0; + if ( orig_nValue == 0 || received_nValue == 0 || paidunits == 0 || totalunits == 0 ) + { + fprintf(stderr,"ValidateAssetRemainder: orig_nValue == %llu || received_nValue == %llu || paidunits == %llu || totalunits == %llu\n",(long long)orig_nValue,(long long)received_nValue,(long long)paidunits,(long long)totalunits); + return(false); + } + else if ( totalunits != (remaining_price + paidunits) ) + { + fprintf(stderr,"ValidateAssetRemainder: totalunits %llu != %llu (remaining_price %llu + %llu paidunits)\n",(long long)totalunits,(long long)(remaining_price + paidunits),(long long)remaining_price,(long long)paidunits); + return(false); + } + else if ( orig_nValue != (remaining_nValue + received_nValue) ) + { + fprintf(stderr,"ValidateAssetRemainder: orig_nValue %llu != %llu (remaining_nValue %llu + %llu received_nValue)\n",(long long)orig_nValue,(long long)(remaining_nValue - received_nValue),(long long)remaining_nValue,(long long)received_nValue); + return(false); + } + else + { + unitprice = (orig_nValue * COIN) / totalunits; + recvunitprice = (received_nValue * COIN) / paidunits; + if ( remaining_price != 0 ) + newunitprice = (remaining_nValue * COIN) / remaining_price; + if ( recvunitprice < unitprice ) + { + fprintf(stderr,"error recvunitprice %.16f < %.16f unitprice, new unitprice %.16f\n",(double)recvunitprice/(COIN*COIN),(double)unitprice/(COIN*COIN),(double)newunitprice/(COIN*COIN)); + return(false); + } + fprintf(stderr,"recvunitprice %.16f >= %.16f unitprice, new unitprice %.16f\n",(double)recvunitprice/(COIN*COIN),(double)unitprice/(COIN*COIN),(double)newunitprice/(COIN*COIN)); + } + return(true); +} + CScript EncodeAssetCreateOpRet(uint8_t funcid,std::vector origpubkey,std::string name,std::string description) { CScript opret; uint8_t evalcode = EVAL_ASSETS; diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 0c0b0923d..567dce3f0 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -279,7 +279,9 @@ std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t mtx.vin.push_back(CTxIn(bidtxid,bidvout,CScript())); if ( (inputs= AddAssetInputs(cp,mtx,mypk,assetid,fillamount,60)) > 0 ) { - SetAssetFillamounts(0,paid_amount,remaining_required,bidamount,fillamount,origprice); + if ( inputs < fillamount ) + fillamount = inputs; + SetBidFillamounts(paid_amount,remaining_required,bidamount,fillamount,origprice); if ( inputs > fillamount ) CCchange = (inputs - fillamount); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,bidamount - paid_amount,GetUnspendable(cp,0))); @@ -295,9 +297,9 @@ std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t return("no normal coins left"); } -std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,uint64_t fillamount) +std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,uint64_t paid_nValue) { - CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t askvout=0; uint64_t totalunits,askamount,paid_amount,remaining_required,inputs,CCchange=0; struct CCcontract_info *cp,C; + CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t askvout=0; uint64_t received_assetoshis,total_nValue,orig_assetoshis,paid_nValue,remaining_nValue,inputs,CCchange=0; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; @@ -306,28 +308,27 @@ std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 ask { if ( GetTransaction(asktxid,vintx,hashBlock,false) != 0 ) { - askamount = vintx.vout[askvout].nValue; - SetAssetOrigpubkey(origpubkey,totalunits,vintx); + orig_assetoshis = vintx.vout[askvout].nValue; + SetAssetOrigpubkey(origpubkey,total_nValue,vintx); mtx.vin.push_back(CTxIn(asktxid,askvout,CScript())); if ( assetid2 != zeroid ) - inputs = AddAssetInputs(cp,mtx,mypk,assetid2,fillamount,60); - else inputs = AddNormalinputs(mtx,mypk,fillamount,60); + inputs = AddAssetInputs(cp,mtx,mypk,assetid2,paid_nValue,60); + else inputs = AddNormalinputs(mtx,mypk,paid_nValue,60); if ( inputs > 0 ) { - if ( inputs < fillamount ) - fillamount = inputs; - fprintf(stderr,"inputs %llu, fillamount.%llu\n",(long long)inputs,(long long)fillamount); - SetAssetFillamounts(1,paid_amount,remaining_required,askamount,fillamount,totalunits); - fprintf(stderr,"paidamount %llu remain %llu, fill %llu\n",(long long)paid_amount,(long long)remaining_required,(long long)fillamount); - if ( assetid2 != zeroid && inputs > fillamount ) - CCchange = (inputs - fillamount); - mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount - paid_amount,GetUnspendable(cp,0))); - mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,paid_amount,mypk)); - mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,fillamount,pubkey2pk(origpubkey))); + if ( inputs < paid_nValue ) + paid_nValue = inputs; + if ( assetid2 != zeroid ) + SetSwapFillamounts(received_assetoshis,remaining_nValue,orig_assetoshis,paid_nValue,total_nValue); + else SetAskFillamounts(received_assetoshis,remaining_nValue,orig_assetoshis,paid_nValue,total_nValue); + if ( assetid2 != zeroid && inputs > paid_nValue ) + CCchange = (inputs - paid_nValue); + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,orig_assetoshis - received_assetoshis,GetUnspendable(cp,0))); + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,received_assetoshis,mypk)); + mtx.vout.push_back(CTxOut(paid_nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); - fprintf(stderr,"remaining %llu -> origpubkey\n",(long long)remaining_required); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet(assetid2!=zeroid?'E':'S',assetid,assetid2,remaining_required,origpubkey))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet(assetid2!=zeroid?'E':'S',assetid,assetid2,remaining_nValue,origpubkey))); } else fprintf(stderr,"filltx not enough utxos\n"); } } diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 1aea32e00..ee153b6f9 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -227,11 +227,17 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("mismatched origpubkeys for fillbuy"); else { - if ( ConstrainVout(tx.vout[1],0,0,0) == 0 ) - return eval->Invalid("vout1 is CC for fillbuy"); - else if ( ConstrainVout(tx.vout[2],1,CCaddr,0) == 0 ) + inputs = 0; + for (i=2; iInvalid("vout2 is normal for fillbuy"); - else if ( ValidateAssetRemainder(0,remaining_price,tx.vout[0].nValue,nValue,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) + else if ( ConstrainVout(tx.vout[1],0,0,0) == 0 ) + return eval->Invalid("vout1 is CC for fillbuy"); + else if ( ValidateBidRemainder(remaining_price,tx.vout[0].nValue,nValue,tx.vout[1].nValue,inputs,totalunits) == false ) return eval->Invalid("mismatched remainder for fillbuy"); else if ( remaining_price != 0 ) { @@ -273,6 +279,45 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx break; case 'S': // fillsell + //vin.0: normal input + //vin.1: unspendable.(vout.0 assetoshis from selloffer) sellTx.vout[0] + //'S'.vin.2+: normal output that satisfies selloffer (*tx.vin[2])->nValue + //vout.0: remaining assetoshis -> unspendable + //vout.1: vin.1 assetoshis to signer of vin.2 sellTx.vout[0].nValue -> any + //'S'.vout.2: vin.2 value to original pubkey [origpubkey] + //vout.3: normal output for change (if any) + //'S'.vout.n-1: opreturn [EVAL_ASSETS] ['S'] [assetid] [amount of coin still required] [origpubkey] + if ( (assetoshis= AssetValidateSellvin(cp,eval,totalunits,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 ) + return(false); + else if ( numvouts < 3 ) + return eval->Invalid("not enough vouts for fill"); + else if ( tmporigpubkey != origpubkey ) + return eval->Invalid("mismatched origpubkeys for fill"); + else + { + inputs = 0; + for (i=2; i orig, vout1 %llu, total %llu\n",(long long)tx.vout[0].nValue,(long long)assetoshis,(long long)tx.vout[2].nValue,(long long)tx.vout[1].nValue,(long long)totalunits); + if ( ValidateAskRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[2].nValue,tx.vout[1].nValue,totalunits) == false ) + return eval->Invalid("mismatched remainder for fill"); + else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 ) + return eval->Invalid("normal vout1 for fillask"); + else if ( ConstrainVout(tx.vout[2],0,origaddr,0) == 0 ) + return eval->Invalid("CC vout2 for fillask"); + else if ( remaining_price != 0 ) + { + if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) + return eval->Invalid("mismatched vout0 AssetsCCaddr for fill"); + } + } + fprintf(stderr,"fill validated\n"); + break; case 'E': // fillexchange //vin.0: normal input //vin.1: unspendable.(vout.0 assetoshis from selloffer) sellTx.vout[0] @@ -285,11 +330,8 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx //vout.3: normal output for change (if any) //'S'.vout.n-1: opreturn [EVAL_ASSETS] ['S'] [assetid] [amount of coin still required] [origpubkey] //'E'.vout.n-1: opreturn [EVAL_ASSETS] ['E'] [assetid vin0+1] [assetid vin2] [remaining asset2 required] [origpubkey] - if ( funcid == 'E' ) - { - if ( AssetExactAmounts(cp,inputs,1,outputs,eval,tx,assetid2) == false ) - eval->Invalid("asset2 inputs != outputs"); - } + if ( AssetExactAmounts(cp,inputs,1,outputs,eval,tx,assetid2) == false ) + eval->Invalid("asset2 inputs != outputs"); if ( (assetoshis= AssetValidateSellvin(cp,eval,totalunits,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 ) return(false); else if ( numvouts < 3 ) @@ -298,16 +340,21 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("mismatched origpubkeys for fill"); else { + inputs = 0; + for (i=2; i orig, vout1 %llu, total %llu\n",(long long)tx.vout[0].nValue,(long long)assetoshis,(long long)tx.vout[2].nValue,(long long)tx.vout[1].nValue,(long long)totalunits); + if ( ValidateSwapRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[2].nValue,tx.vout[1].nValue,totalunits) == false ) return eval->Invalid("mismatched remainder for fill"); else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 ) return eval->Invalid("normal vout1 for fillask"); - else if ( funcid == 'E' && ConstrainVout(tx.vout[2],1,CCaddr,0) == 0 ) + else if ( ConstrainVout(tx.vout[2],1,CCaddr,0) == 0 ) return eval->Invalid("normal vout2 for fillask"); - else if ( funcid == 'S' && ConstrainVout(tx.vout[2],0,origaddr,0) == 0 ) - return eval->Invalid("CC vout2 for fillask"); else if ( remaining_price != 0 ) { if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) From 6cc6dc2a9a134fa063f7a8441d7aecbdb0679b68 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 23:26:01 -1100 Subject: [PATCH 055/204] Fixes --- src/cc/CCassetsCore.cpp | 126 ++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 75 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 660479f0c..fd15faf94 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -38,34 +38,7 @@ We assume that the effective unit cost in the orderbook is valid and that that amount was paid and also that any remainder will be close enough in effective unit cost to not matter. At the edge cases, this will probably be not true and maybe some orders wont be practically fillable when reduced to fractional state. However, the original pubkey that created the offer can always reclaim it. */ -bool SetBidFillamounts(uint64_t &received_nValue,uint64_t &remaining_price,uint64_t orig_nValue,uint64_t &paidunits,uint64_t totalunits) -{ - uint64_t remaining_nValue,unitprice; double dprice; - if ( totalunits == 0 ) - { - received_nValue = remaining_price = paidunits = 0; - return(false); - } - if ( paidunits >= totalunits ) - { - paidunits = totalunits; - received_nValue = orig_nValue; - remaining_price = 0; - fprintf(stderr,"totally filled!\n"); - return(true); - } - remaining_price = (totalunits - paidunits); - unitprice = (orig_nValue * COIN) / totalunits; - received_nValue = (paidunits * unitprice) / COIN; - if ( unitprice > 0 && received_nValue > 0 && received_nValue <= orig_nValue ) - { - remaining_nValue = (orig_nValue - received_nValue); - printf("total.%llu 0 paid.%llu, remaining %llu <- %llu (%llu - %llu)\n",(long long)totalunits,(long long)paidunits,(long long)remaining_nValue,(long long)(orig_nValue - received_nValue),(long long)orig_nValue,(long long)received_nValue); - return(ValidateAssetRemainder(sellflag,remaining_price,remaining_nValue,orig_nValue,received_nValue,paidunits,totalunits)); - } else return(false); -} - -bool ValidateBidRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidunits,uint64_t totalunits) +bool ValidateBidRemainder(uint64_t remaining_units,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidunits,uint64_t totalunits) { uint64_t unitprice,recvunitprice,newunitprice=0; if ( orig_nValue == 0 || received_nValue == 0 || paidunits == 0 || totalunits == 0 ) @@ -73,7 +46,7 @@ bool ValidateBidRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uin fprintf(stderr,"ValidateAssetRemainder: orig_nValue == %llu || received_nValue == %llu || paidunits == %llu || totalunits == %llu\n",(long long)orig_nValue,(long long)received_nValue,(long long)paidunits,(long long)totalunits); return(false); } - else if ( totalunits != (remaining_price + paidunits) ) + else if ( totalunits != (remaining_units + paidunits) ) { fprintf(stderr,"ValidateAssetRemainder: totalunits %llu != %llu (remaining_price %llu + %llu paidunits)\n",(long long)totalunits,(long long)(remaining_price + paidunits),(long long)remaining_price,(long long)paidunits); return(false); @@ -87,8 +60,8 @@ bool ValidateBidRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uin { unitprice = (orig_nValue * COIN) / totalunits; recvunitprice = (received_nValue * COIN) / paidunits; - if ( remaining_price != 0 ) - newunitprice = (remaining_nValue * COIN) / remaining_price; + if ( remaining_units != 0 ) + newunitprice = (remaining_nValue * COIN) / remaining_units; if ( recvunitprice < unitprice ) { fprintf(stderr,"error recvunitprice %.16f < %.16f unitprice, new unitprice %.16f\n",(double)recvunitprice/(COIN*COIN),(double)unitprice/(COIN*COIN),(double)newunitprice/(COIN*COIN)); @@ -99,7 +72,7 @@ bool ValidateBidRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uin return(true); } -bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue,uint64_t orig_assetoshis,uint64_t &paid_nValue,uint64_t total_nValue) +bool SetBidFillamounts(uint64_t &received_nValue,uint64_t &remaining_units,uint64_t orig_nValue,uint64_t &paidunits,uint64_t totalunits) { uint64_t remaining_nValue,unitprice; double dprice; if ( totalunits == 0 ) @@ -115,7 +88,34 @@ bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue, fprintf(stderr,"totally filled!\n"); return(true); } - remaining_price = (totalunits - paidunits); + remaining_units = (totalunits - paidunits); + unitprice = (orig_nValue * COIN) / totalunits; + received_nValue = (paidunits * unitprice) / COIN; + if ( unitprice > 0 && received_nValue > 0 && received_nValue <= orig_nValue ) + { + remaining_nValue = (orig_nValue - received_nValue); + printf("total.%llu 0 paid.%llu, remaining %llu <- %llu (%llu - %llu)\n",(long long)totalunits,(long long)paidunits,(long long)remaining_nValue,(long long)(orig_nValue - received_nValue),(long long)orig_nValue,(long long)received_nValue); + return(ValidateBidRemainder(remaining_units,remaining_nValue,orig_nValue,received_nValue,paidunits,totalunits)); + } else return(false); +} + +bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue,uint64_t orig_assetoshis,uint64_t &paid_nValue,uint64_t total_nValue) +{ + uint64_t remaining_assetoshis,unitprice; + if ( totalunits == 0 ) + { + received_assetoshis = remaining_nValue = paid_nValue = 0; + return(false); + } + if ( paid_nValue >= total_nValue ) + { + paid_nValue = total_nValue; + received_assetoshis = orig_assetoshis; + remaining_nValue = 0; + fprintf(stderr,"totally filled!\n"); + return(true); + } + remaining_nValue = (total_nValue - paid_nValue); // ./komodo-cli -ac_name=ATEST tokenfillask 9217014eae0a83a0b64632f379c1b474859794f9eaf1cf1eecf5804ed6124a5e ce7811a63e2d06ec6bde8a553b05b7dca95b17e8a676e431a887135ed62549b7 10 /*{ "funcid": "s", @@ -128,14 +128,12 @@ bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue, "totalrequired": "1000.00000000", "price": "100.00000000" },*/ - dprice = (double)(orig_nValue * COIN) / totalunits; - received_nValue = (paidunits * dprice); - fprintf(stderr,"dprice %.8f orig %llu/total %llu, recv %llu\n",dprice,(long long)orig_nValue,(long long)totalunits,(long long)received_nValue); - if ( unitprice > 0 && received_nValue > 0 && received_nValue <= orig_nValue ) + unitprice = (double)(total_nValue * COIN) / origassetoshis; + received_assetoshis = (paid_nValue * COIN) / unitprice; + if ( unitprice > 0 && received_assetoshis > 0 && received_assetoshis <= origassetoshis ) { - remaining_nValue = (orig_nValue - received_nValue); - printf("total.%llu 0 paid.%llu, remaining %llu <- %llu (%llu - %llu)\n",(long long)totalunits,(long long)paidunits,(long long)remaining_nValue,(long long)(orig_nValue - received_nValue),(long long)orig_nValue,(long long)received_nValue); - return(ValidateAssetRemainder(sellflag,remaining_price,remaining_nValue,orig_nValue,received_nValue,paidunits,totalunits)); + remaining_assetoshis = (origassetoshis - received_assetoshis); + return(ValidateAskRemainder(remaining_nValue,remaining_assetoshis,origassetoshis,received_assetoshis,paid_nValue,total_nValue)); } else return(false); } @@ -173,51 +171,29 @@ bool ValidateAskRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uin return(true); } -bool SetSwapFillamounts(int32_t sellflag,uint64_t &received_nValue,uint64_t &remaining_price,uint64_t orig_nValue,uint64_t &paidunits,uint64_t totalunits) +bool SetSwapFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue,uint64_t orig_assetoshis,uint64_t &paid_nValue,uint64_t total_nValue) { - uint64_t remaining_nValue,unitprice; double dprice; + uint64_t remaining_assetoshis,unitprice; if ( totalunits == 0 ) { - received_nValue = remaining_price = paidunits = 0; + received_assetoshis = remaining_nValue = paid_nValue = 0; return(false); } - if ( paidunits >= totalunits ) + if ( paid_nValue >= total_nValue ) { - paidunits = totalunits; - received_nValue = orig_nValue; - remaining_price = 0; + paid_nValue = total_nValue; + received_assetoshis = orig_assetoshis; + remaining_nValue = 0; fprintf(stderr,"totally filled!\n"); return(true); } - remaining_price = (totalunits - paidunits); - if ( sellflag == 0 ) + remaining_nValue = (total_nValue - paid_nValue); + unitprice = (double)(total_nValue * COIN) / origassetoshis; + received_assetoshis = (paid_nValue * COIN) / unitprice; + if ( unitprice > 0 && received_assetoshis > 0 && received_assetoshis <= origassetoshis ) { - unitprice = (orig_nValue * COIN) / totalunits; - received_nValue = (paidunits * unitprice) / COIN; - } - else - { - // ./komodo-cli -ac_name=ATEST tokenfillask 9217014eae0a83a0b64632f379c1b474859794f9eaf1cf1eecf5804ed6124a5e ce7811a63e2d06ec6bde8a553b05b7dca95b17e8a676e431a887135ed62549b7 10 - /*{ - "funcid": "s", - "txid": "ce7811a63e2d06ec6bde8a553b05b7dca95b17e8a676e431a887135ed62549b7", - "vout": 0, - "amount": "10", - "askamount": "10", - "origaddress": "RRPpWbVdxcxmhx4xnWnVZFDfGc9p1177ti", - "tokenid": "9217014eae0a83a0b64632f379c1b474859794f9eaf1cf1eecf5804ed6124a5e", - "totalrequired": "1000.00000000", - "price": "100.00000000" - },*/ - dprice = (double)(orig_nValue * COIN) / totalunits; - received_nValue = (paidunits * dprice); - fprintf(stderr,"dprice %.8f orig %llu/total %llu, recv %llu\n",dprice,(long long)orig_nValue,(long long)totalunits,(long long)received_nValue); - } - if ( unitprice > 0 && received_nValue > 0 && received_nValue <= orig_nValue ) - { - remaining_nValue = (orig_nValue - received_nValue); - printf("total.%llu 0 paid.%llu, remaining %llu <- %llu (%llu - %llu)\n",(long long)totalunits,(long long)paidunits,(long long)remaining_nValue,(long long)(orig_nValue - received_nValue),(long long)orig_nValue,(long long)received_nValue); - return(ValidateAssetRemainder(sellflag,remaining_price,remaining_nValue,orig_nValue,received_nValue,paidunits,totalunits)); + remaining_assetoshis = (origassetoshis - received_assetoshis); + return(ValidateSwapRemainder(remaining_nValue,remaining_assetoshis,origassetoshis,received_assetoshis,paid_nValue,total_nValue)); } else return(false); } From c76fe5e087b9f43efc14f84fdaaa4440ad444a76 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 23:29:31 -1100 Subject: [PATCH 056/204] Test --- src/cc/CCassetsCore.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index fd15faf94..44a929a07 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -48,7 +48,7 @@ bool ValidateBidRemainder(uint64_t remaining_units,uint64_t remaining_nValue,uin } else if ( totalunits != (remaining_units + paidunits) ) { - fprintf(stderr,"ValidateAssetRemainder: totalunits %llu != %llu (remaining_price %llu + %llu paidunits)\n",(long long)totalunits,(long long)(remaining_price + paidunits),(long long)remaining_price,(long long)paidunits); + fprintf(stderr,"ValidateAssetRemainder: totalunits %llu != %llu (remaining_units %llu + %llu paidunits)\n",(long long)totalunits,(long long)(remaining_units + paidunits),(long long)remaining_units,(long long)paidunits); return(false); } else if ( orig_nValue != (remaining_nValue + received_nValue) ) @@ -77,14 +77,14 @@ bool SetBidFillamounts(uint64_t &received_nValue,uint64_t &remaining_units,uint6 uint64_t remaining_nValue,unitprice; double dprice; if ( totalunits == 0 ) { - received_nValue = remaining_price = paidunits = 0; + received_nValue = remaining_units = paidunits = 0; return(false); } if ( paidunits >= totalunits ) { paidunits = totalunits; received_nValue = orig_nValue; - remaining_price = 0; + remaining_units = 0; fprintf(stderr,"totally filled!\n"); return(true); } @@ -102,7 +102,7 @@ bool SetBidFillamounts(uint64_t &received_nValue,uint64_t &remaining_units,uint6 bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue,uint64_t orig_assetoshis,uint64_t &paid_nValue,uint64_t total_nValue) { uint64_t remaining_assetoshis,unitprice; - if ( totalunits == 0 ) + if ( total_nValue == 0 ) { received_assetoshis = remaining_nValue = paid_nValue = 0; return(false); @@ -128,12 +128,12 @@ bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue, "totalrequired": "1000.00000000", "price": "100.00000000" },*/ - unitprice = (double)(total_nValue * COIN) / origassetoshis; + unitprice = (double)(total_nValue * COIN) / orig_assetoshis; received_assetoshis = (paid_nValue * COIN) / unitprice; - if ( unitprice > 0 && received_assetoshis > 0 && received_assetoshis <= origassetoshis ) + if ( unitprice > 0 && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) { - remaining_assetoshis = (origassetoshis - received_assetoshis); - return(ValidateAskRemainder(remaining_nValue,remaining_assetoshis,origassetoshis,received_assetoshis,paid_nValue,total_nValue)); + remaining_assetoshis = (orig_assetoshis - received_assetoshis); + return(ValidateAskRemainder(remaining_nValue,remaining_assetoshis,orig_assetoshis,received_assetoshis,paid_nValue,total_nValue)); } else return(false); } @@ -190,10 +190,10 @@ bool SetSwapFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue remaining_nValue = (total_nValue - paid_nValue); unitprice = (double)(total_nValue * COIN) / origassetoshis; received_assetoshis = (paid_nValue * COIN) / unitprice; - if ( unitprice > 0 && received_assetoshis > 0 && received_assetoshis <= origassetoshis ) + if ( unitprice > 0 && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) { - remaining_assetoshis = (origassetoshis - received_assetoshis); - return(ValidateSwapRemainder(remaining_nValue,remaining_assetoshis,origassetoshis,received_assetoshis,paid_nValue,total_nValue)); + remaining_assetoshis = (orig_assetoshis - received_assetoshis); + return(ValidateSwapRemainder(remaining_nValue,remaining_assetoshis,orig_assetoshis,received_assetoshis,paid_nValue,total_nValue)); } else return(false); } From 37d792748723180affa5d8631d8c843480de1ad8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 23:30:17 -1100 Subject: [PATCH 057/204] Test --- src/cc/CCassetsCore.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 44a929a07..c3748f72c 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -174,7 +174,7 @@ bool ValidateAskRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uin bool SetSwapFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue,uint64_t orig_assetoshis,uint64_t &paid_nValue,uint64_t total_nValue) { uint64_t remaining_assetoshis,unitprice; - if ( totalunits == 0 ) + if ( total_nValue == 0 ) { received_assetoshis = remaining_nValue = paid_nValue = 0; return(false); @@ -188,7 +188,7 @@ bool SetSwapFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue return(true); } remaining_nValue = (total_nValue - paid_nValue); - unitprice = (double)(total_nValue * COIN) / origassetoshis; + unitprice = (double)(total_nValue * COIN) / orig_assetoshis; received_assetoshis = (paid_nValue * COIN) / unitprice; if ( unitprice > 0 && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) { From 1ef7caa451045d9ee14f0070c06f98f8fc214090 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 23:33:01 -1100 Subject: [PATCH 058/204] Test --- src/cc/assets.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index ee153b6f9..af06d455f 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -230,7 +230,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx inputs = 0; for (i=2; i Date: Wed, 25 Jul 2018 23:34:10 -1100 Subject: [PATCH 059/204] Test --- src/cc/CCassetstx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 567dce3f0..d59d32d5c 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -299,7 +299,7 @@ std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,uint64_t paid_nValue) { - CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t askvout=0; uint64_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 origpubkey; int32_t askvout=0; uint64_t received_assetoshis,total_nValue,orig_assetoshis,remaining_nValue,inputs,CCchange=0; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; From d4dc91931c3dfe60cc8c7d2746382315d6aaabba Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 23:40:41 -1100 Subject: [PATCH 060/204] +print --- src/cc/CCassetsCore.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index c3748f72c..4824ba733 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -190,6 +190,8 @@ bool SetSwapFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue remaining_nValue = (total_nValue - paid_nValue); unitprice = (double)(total_nValue * COIN) / orig_assetoshis; received_assetoshis = (paid_nValue * COIN) / unitprice; + fprintf(stderr,"remaining_nValue %llu (%llu - %llu)\n",(long long)remaining_nValue,(long long)(total_nValue - paid_nValue),(long long)total_nValue,(long long)paid_nValue); + fprintf(stderr,"unitprice %llu received_assetoshis %llu orig %llu\n",(long long)unitprice,(long long)received_assetoshis,(long long)orig_assetoshis); if ( unitprice > 0 && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) { remaining_assetoshis = (orig_assetoshis - received_assetoshis); From ca4da23d562198ec503fca21b38806c606d53045 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 23:41:47 -1100 Subject: [PATCH 061/204] Test --- src/cc/CCassetsCore.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 4824ba733..44fd7f261 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -190,7 +190,7 @@ bool SetSwapFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue remaining_nValue = (total_nValue - paid_nValue); unitprice = (double)(total_nValue * COIN) / orig_assetoshis; received_assetoshis = (paid_nValue * COIN) / unitprice; - fprintf(stderr,"remaining_nValue %llu (%llu - %llu)\n",(long long)remaining_nValue,(long long)(total_nValue - paid_nValue),(long long)total_nValue,(long long)paid_nValue); + fprintf(stderr,"remaining_nValue %llu (%llu - %llu)\n",(long long)remaining_nValue,(long long)total_nValue,(long long)paid_nValue); fprintf(stderr,"unitprice %llu received_assetoshis %llu orig %llu\n",(long long)unitprice,(long long)received_assetoshis,(long long)orig_assetoshis); if ( unitprice > 0 && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) { From 1e25835880e53a8c27cdb9afc1500efea0b0aa8b Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 23:44:56 -1100 Subject: [PATCH 062/204] Test --- src/cc/CCassetsCore.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 44fd7f261..e46325d24 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -130,6 +130,8 @@ bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue, },*/ unitprice = (double)(total_nValue * COIN) / orig_assetoshis; received_assetoshis = (paid_nValue * COIN) / unitprice; + fprintf(stderr,"remaining_nValue %llu (%llu - %llu)\n",(long long)remaining_nValue,(long long)total_nValue,(long long)paid_nValue); + fprintf(stderr,"unitprice %llu received_assetoshis %llu orig %llu\n",(long long)unitprice,(long long)received_assetoshis,(long long)orig_assetoshis); if ( unitprice > 0 && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) { remaining_assetoshis = (orig_assetoshis - received_assetoshis); From 6fa0da91a0fe7f16fb8ef88d42bf03d085044cc1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Jul 2018 23:59:13 -1100 Subject: [PATCH 063/204] Test --- src/cc/CCassetsCore.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index e46325d24..d8ab4f44d 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -128,10 +128,10 @@ bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue, "totalrequired": "1000.00000000", "price": "100.00000000" },*/ - unitprice = (double)(total_nValue * COIN) / orig_assetoshis; - received_assetoshis = (paid_nValue * COIN) / unitprice; - fprintf(stderr,"remaining_nValue %llu (%llu - %llu)\n",(long long)remaining_nValue,(long long)total_nValue,(long long)paid_nValue); - fprintf(stderr,"unitprice %llu received_assetoshis %llu orig %llu\n",(long long)unitprice,(long long)received_assetoshis,(long long)orig_assetoshis); + unitprice = (total_nValue / orig_assetoshis); + received_assetoshis = (paid_nValue / unitprice); + fprintf(stderr,"remaining_nValue %.8f (%.8f - %.8f)\n",(double)remaining_nValue/COIN,(double)total_nValue/COIN,(double)paid_nValue/COIN); + fprintf(stderr,"unitprice %.8f received_assetoshis %llu orig %llu\n",(double)unitprice/COIN,(long long)received_assetoshis,(long long)orig_assetoshis); if ( unitprice > 0 && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) { remaining_assetoshis = (orig_assetoshis - received_assetoshis); From ba8a98f2e7a03bdffcdf001f0859fb6e6a9449ae Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 00:17:47 -1100 Subject: [PATCH 064/204] Test --- src/cc/CCassetsCore.cpp | 8 ++++---- src/cc/CCassetstx.cpp | 6 ++++-- src/wallet/rpcwallet.cpp | 16 ++++++++-------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index d8ab4f44d..73836e144 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -101,7 +101,7 @@ bool SetBidFillamounts(uint64_t &received_nValue,uint64_t &remaining_units,uint6 bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue,uint64_t orig_assetoshis,uint64_t &paid_nValue,uint64_t total_nValue) { - uint64_t remaining_assetoshis,unitprice; + uint64_t remaining_assetoshis; double dunitprice; if ( total_nValue == 0 ) { received_assetoshis = remaining_nValue = paid_nValue = 0; @@ -128,10 +128,10 @@ bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue, "totalrequired": "1000.00000000", "price": "100.00000000" },*/ - unitprice = (total_nValue / orig_assetoshis); - received_assetoshis = (paid_nValue / unitprice); + dunitprice = ((double)total_nValue / orig_assetoshis); + received_assetoshis = (paid_nValue / dunitprice); fprintf(stderr,"remaining_nValue %.8f (%.8f - %.8f)\n",(double)remaining_nValue/COIN,(double)total_nValue/COIN,(double)paid_nValue/COIN); - fprintf(stderr,"unitprice %.8f received_assetoshis %llu orig %llu\n",(double)unitprice/COIN,(long long)received_assetoshis,(long long)orig_assetoshis); + fprintf(stderr,"unitprice %.8f received_assetoshis %llu orig %llu\n",unitprice,(long long)received_assetoshis,(long long)orig_assetoshis); if ( unitprice > 0 && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) { remaining_assetoshis = (orig_assetoshis - received_assetoshis); diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index d59d32d5c..13e5fa2ca 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -297,9 +297,9 @@ std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t return("no normal coins left"); } -std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,uint64_t paid_nValue) +std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,uint64_t fillunits) { - CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t askvout=0; uint64_t received_assetoshis,total_nValue,orig_assetoshis,remaining_nValue,inputs,CCchange=0; struct CCcontract_info *cp,C; + CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; double dprice; int32_t askvout=0; uint64_t received_assetoshis,total_nValue,orig_assetoshis,paid_nValue,remaining_nValue,inputs,CCchange=0; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; @@ -310,6 +310,8 @@ std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 ask { orig_assetoshis = vintx.vout[askvout].nValue; SetAssetOrigpubkey(origpubkey,total_nValue,vintx); + dprice = (double)total_nValue / (COIN * orig_assetoshis); + paid_nValue = dprice * fillunits; mtx.vin.push_back(CTxIn(asktxid,askvout,CScript())); if ( assetid2 != zeroid ) inputs = AddAssetInputs(cp,mtx,mypk,assetid2,paid_nValue,60); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index dcce4324e..d1fef5092 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5316,15 +5316,15 @@ UniValue tokencancelask(const UniValue& params, bool fHelp) UniValue tokenfillask(const UniValue& params, bool fHelp) { static uint256 zeroid; - UniValue result(UniValue::VOBJ); uint64_t fillamount; std::string hex; uint256 tokenid,asktxid; + UniValue result(UniValue::VOBJ); uint64_t fillunits; std::string hex; uint256 tokenid,asktxid; if ( fHelp || params.size() != 3 ) - throw runtime_error("tokenfillask tokenid asktxid fillamount\n"); + throw runtime_error("tokenfillask tokenid asktxid fillunits\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"); tokenid = Parseuint256((char *)params[0].get_str().c_str()); asktxid = Parseuint256((char *)params[1].get_str().c_str()); - fillamount = atof(params[2].get_str().c_str()) * COIN; - hex = FillSell(0,tokenid,zeroid,asktxid,fillamount); + fillunits = atol(params[2].get_str().c_str()); + hex = FillSell(0,tokenid,zeroid,asktxid,fillunits); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -5336,16 +5336,16 @@ UniValue tokenfillask(const UniValue& params, bool fHelp) UniValue tokenfillswap(const UniValue& params, bool fHelp) { static uint256 zeroid; - UniValue result(UniValue::VOBJ); uint64_t fillamount; std::string hex; uint256 tokenid,otherid,asktxid; + UniValue result(UniValue::VOBJ); uint64_t fillunits; std::string hex; uint256 tokenid,otherid,asktxid; if ( fHelp || params.size() != 4 ) - throw runtime_error("tokenfillswap tokenid otherid asktxid fillamount\n"); + throw runtime_error("tokenfillswap tokenid otherid asktxid fillunits\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"); tokenid = Parseuint256((char *)params[0].get_str().c_str()); otherid = Parseuint256((char *)params[1].get_str().c_str()); asktxid = Parseuint256((char *)params[2].get_str().c_str()); - fillamount = atol(params[3].get_str().c_str()); - hex = FillSell(0,tokenid,otherid,asktxid,fillamount); + fillunits = atol(params[3].get_str().c_str()); + hex = FillSell(0,tokenid,otherid,asktxid,fillunits); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); From 80ec17cb256f0993aaead5cf8fc8374d5ea42c72 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 00:20:27 -1100 Subject: [PATCH 065/204] Test --- src/cc/CCassetsCore.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 73836e144..30c069960 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -131,7 +131,7 @@ bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue, dunitprice = ((double)total_nValue / orig_assetoshis); received_assetoshis = (paid_nValue / dunitprice); fprintf(stderr,"remaining_nValue %.8f (%.8f - %.8f)\n",(double)remaining_nValue/COIN,(double)total_nValue/COIN,(double)paid_nValue/COIN); - fprintf(stderr,"unitprice %.8f received_assetoshis %llu orig %llu\n",unitprice,(long long)received_assetoshis,(long long)orig_assetoshis); + fprintf(stderr,"unitprice %.8f received_assetoshis %llu orig %llu\n",dunitprice,(long long)received_assetoshis,(long long)orig_assetoshis); if ( unitprice > 0 && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) { remaining_assetoshis = (orig_assetoshis - received_assetoshis); From 3e1ed867aa93573f8abe70926d201cbf62bccba5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 00:21:10 -1100 Subject: [PATCH 066/204] Test --- src/cc/CCassetsCore.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 30c069960..d29a06542 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -132,7 +132,7 @@ bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue, received_assetoshis = (paid_nValue / dunitprice); fprintf(stderr,"remaining_nValue %.8f (%.8f - %.8f)\n",(double)remaining_nValue/COIN,(double)total_nValue/COIN,(double)paid_nValue/COIN); fprintf(stderr,"unitprice %.8f received_assetoshis %llu orig %llu\n",dunitprice,(long long)received_assetoshis,(long long)orig_assetoshis); - if ( unitprice > 0 && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) + if ( fabs(unitprice) > SMALLVAL && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) { remaining_assetoshis = (orig_assetoshis - received_assetoshis); return(ValidateAskRemainder(remaining_nValue,remaining_assetoshis,orig_assetoshis,received_assetoshis,paid_nValue,total_nValue)); From c2c2a808cb2e05895b4d4323d73554db8ba6cb74 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 00:22:08 -1100 Subject: [PATCH 067/204] SMALLVAL --- src/cc/CCinclude.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index f72fce237..8dfc9bf6a 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -28,6 +28,8 @@ #include #include +#define SMALLVAL 0.000000000000001 + struct CCcontract_info { uint256 prevtxid; From 5901a2d530fd6392cb04f1f4e57f86a2558d1a7e Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 00:22:50 -1100 Subject: [PATCH 068/204] Test --- src/cc/CCassetsCore.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index d29a06542..56ce6ee1d 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -132,7 +132,7 @@ bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue, received_assetoshis = (paid_nValue / dunitprice); fprintf(stderr,"remaining_nValue %.8f (%.8f - %.8f)\n",(double)remaining_nValue/COIN,(double)total_nValue/COIN,(double)paid_nValue/COIN); fprintf(stderr,"unitprice %.8f received_assetoshis %llu orig %llu\n",dunitprice,(long long)received_assetoshis,(long long)orig_assetoshis); - if ( fabs(unitprice) > SMALLVAL && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) + if ( fabs(dunitprice) > SMALLVAL && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) { remaining_assetoshis = (orig_assetoshis - received_assetoshis); return(ValidateAskRemainder(remaining_nValue,remaining_assetoshis,orig_assetoshis,received_assetoshis,paid_nValue,total_nValue)); From c272ff8636fe843b5202ceb30cc957e4d4e80062 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 00:26:11 -1100 Subject: [PATCH 069/204] Test --- src/cc/CCassetstx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 13e5fa2ca..ae0a3d593 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -310,7 +310,7 @@ std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 ask { orig_assetoshis = vintx.vout[askvout].nValue; SetAssetOrigpubkey(origpubkey,total_nValue,vintx); - dprice = (double)total_nValue / (COIN * orig_assetoshis); + dprice = (double)total_nValue / orig_assetoshis; paid_nValue = dprice * fillunits; mtx.vin.push_back(CTxIn(asktxid,askvout,CScript())); if ( assetid2 != zeroid ) From 855ff4a1748bf2a372188c09f00d0db3546912b4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 00:36:53 -1100 Subject: [PATCH 070/204] Test --- src/cc/CCassetsCore.cpp | 26 +++++++++++++------------- src/cc/assets.cpp | 8 +++----- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 56ce6ee1d..707faae8f 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -139,36 +139,36 @@ bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue, } else return(false); } -bool ValidateAskRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidunits,uint64_t totalunits) +bool ValidateAskRemainder(uint64_t remaining_nValue,uint64_t remaining_assetoshis,uint64_t orig_assetoshis,uint64_t received_assetoshis,uint64_t paid_nValue,uint64_t total_nValue) { uint64_t unitprice,recvunitprice,newunitprice=0; - if ( orig_nValue == 0 || received_nValue == 0 || paidunits == 0 || totalunits == 0 ) + if ( orig_assetoshis == 0 || received_assetoshis == 0 || paid_nValue == 0 || total_nValue == 0 ) { - fprintf(stderr,"ValidateAssetRemainder: orig_nValue == %llu || received_nValue == %llu || paidunits == %llu || totalunits == %llu\n",(long long)orig_nValue,(long long)received_nValue,(long long)paidunits,(long long)totalunits); + fprintf(stderr,"ValidateAssetRemainder: orig_assetoshis == %llu || received_assetoshis == %llu || paid_nValue == %llu || total_nValue == %llu\n",(long long)orig_assetoshis,(long long)received_assetoshis,(long long)paid_nValue,(long long)total_nValue); return(false); } - else if ( totalunits != (remaining_price + paidunits) ) + else if ( total_nValue != (remaining_nValue + paid_nValue) ) { - fprintf(stderr,"ValidateAssetRemainder: totalunits %llu != %llu (remaining_price %llu + %llu paidunits)\n",(long long)totalunits,(long long)(remaining_price + paidunits),(long long)remaining_price,(long long)paidunits); + fprintf(stderr,"ValidateAssetRemainder: total_nValue %llu != %llu (remaining_nValue %llu + %llu paid_nValue)\n",(long long)total_nValue,(long long)(remaining_nValue + paid_nValue),(long long)remaining_nValue,(long long)paid_nValue); return(false); } - else if ( orig_nValue != (remaining_nValue + received_nValue) ) + else if ( orig_assetoshis != (remaining_assetoshis + received_assetoshis) ) { - fprintf(stderr,"ValidateAssetRemainder: orig_nValue %llu != %llu (remaining_nValue %llu + %llu received_nValue)\n",(long long)orig_nValue,(long long)(remaining_nValue - received_nValue),(long long)remaining_nValue,(long long)received_nValue); + fprintf(stderr,"ValidateAssetRemainder: orig_assetoshis %llu != %llu (remaining_nValue %llu + %llu received_nValue)\n",(long long)orig_assetoshis,(long long)(remaining_assetoshis - received_assetoshis),(long long)remaining_assetoshis,(long long)received_assetoshis); return(false); } else { - unitprice = (orig_nValue * COIN) / totalunits; - recvunitprice = (received_nValue * COIN) / paidunits; - if ( remaining_price != 0 ) - newunitprice = (remaining_nValue * COIN) / remaining_price; + unitprice = (orig_nValue / orig_assetoshis); + recvunitprice = (received_nValue / received_assetoshis); + if ( remaining_nValue != 0 ) + newunitprice = (remaining_nValue / remaining_assetoshis); if ( recvunitprice < unitprice ) { - fprintf(stderr,"error recvunitprice %.16f < %.16f unitprice, new unitprice %.16f\n",(double)recvunitprice/(COIN*COIN),(double)unitprice/(COIN*COIN),(double)newunitprice/(COIN*COIN)); + fprintf(stderr,"error recvunitprice %.8f < %.8f unitprice, new unitprice %.8f\n",(double)recvunitprice/COIN,(double)unitprice/COIN,(double)newunitprice/COIN); return(false); } - fprintf(stderr,"recvunitprice %.16f >= %.16f unitprice, new unitprice %.16f\n",(double)recvunitprice/(COIN*COIN),(double)unitprice/(COIN*COIN),(double)newunitprice/(COIN*COIN)); + fprintf(stderr,"error recvunitprice %.8f >= %.8f unitprice, new unitprice %.8f\n",(double)recvunitprice/COIN,(double)unitprice/COIN,(double)newunitprice/COIN); } return(true); } diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index af06d455f..1c37827b8 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -228,7 +228,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx else { inputs = 0; - for (i=2; i orig, vout1 %llu, total %llu\n",(long long)tx.vout[0].nValue,(long long)assetoshis,(long long)tx.vout[2].nValue,(long long)tx.vout[1].nValue,(long long)totalunits); if ( ValidateSwapRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[2].nValue,tx.vout[1].nValue,totalunits) == false ) return eval->Invalid("mismatched remainder for fill"); From 7ca7aaf71999aa6f8e87d082bc6e8df9748b71d3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 00:39:54 -1100 Subject: [PATCH 071/204] Test --- src/cc/CCassetsCore.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 707faae8f..bd4d58ab9 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -159,8 +159,8 @@ bool ValidateAskRemainder(uint64_t remaining_nValue,uint64_t remaining_assetoshi } else { - unitprice = (orig_nValue / orig_assetoshis); - recvunitprice = (received_nValue / received_assetoshis); + unitprice = (total_nValue / orig_assetoshis); + recvunitprice = (paid_nValue / received_assetoshis); if ( remaining_nValue != 0 ) newunitprice = (remaining_nValue / remaining_assetoshis); if ( recvunitprice < unitprice ) From 84ffe75ecfba363cbc3de1db9eeccadcdbe9dcef Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 00:44:49 -1100 Subject: [PATCH 072/204] Test --- src/cc/CCutils.cpp | 2 +- src/cc/assets.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index d9672c61d..249b22690 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -154,7 +154,7 @@ bool ConstrainVout(CTxOut vout,int32_t CCflag,char *cmpaddr,uint64_t nValue) } else if ( cmpaddr != 0 && (Getscriptaddress(destaddr,vout.scriptPubKey) == 0 || strcmp(destaddr,cmpaddr) != 0) ) { - fprintf(stderr,"constrain vout error addr %s vs %s\n",cmpaddr!=0?cmpaddr:"",destaddr!=0?destaddr:""); + //fprintf(stderr,"constrain vout error addr %s vs %s\n",cmpaddr!=0?cmpaddr:"",destaddr!=0?destaddr:""); return(false); } else if ( nValue != 0 && nValue != vout.nValue ) //(nValue == 0 && vout.nValue < 10000) || ( diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 1c37827b8..e8e1bfb90 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -230,7 +230,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx inputs = 0; for (i=2; i orig, vout1 %llu, total %llu\n",(long long)tx.vout[0].nValue,(long long)assetoshis,(long long)tx.vout[2].nValue,(long long)tx.vout[1].nValue,(long long)totalunits); From 6b12f35df851cd36bb2596634cdd3cd929898590 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 00:58:14 -1100 Subject: [PATCH 073/204] Test --- src/cc/CCassetsCore.cpp | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index bd4d58ab9..54323b916 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -116,22 +116,10 @@ bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue, return(true); } remaining_nValue = (total_nValue - paid_nValue); - // ./komodo-cli -ac_name=ATEST tokenfillask 9217014eae0a83a0b64632f379c1b474859794f9eaf1cf1eecf5804ed6124a5e ce7811a63e2d06ec6bde8a553b05b7dca95b17e8a676e431a887135ed62549b7 10 - /*{ - "funcid": "s", - "txid": "ce7811a63e2d06ec6bde8a553b05b7dca95b17e8a676e431a887135ed62549b7", - "vout": 0, - "amount": "10", - "askamount": "10", - "origaddress": "RRPpWbVdxcxmhx4xnWnVZFDfGc9p1177ti", - "tokenid": "9217014eae0a83a0b64632f379c1b474859794f9eaf1cf1eecf5804ed6124a5e", - "totalrequired": "1000.00000000", - "price": "100.00000000" - },*/ dunitprice = ((double)total_nValue / orig_assetoshis); received_assetoshis = (paid_nValue / dunitprice); fprintf(stderr,"remaining_nValue %.8f (%.8f - %.8f)\n",(double)remaining_nValue/COIN,(double)total_nValue/COIN,(double)paid_nValue/COIN); - fprintf(stderr,"unitprice %.8f received_assetoshis %llu orig %llu\n",dunitprice,(long long)received_assetoshis,(long long)orig_assetoshis); + fprintf(stderr,"unitprice %.8f received_assetoshis %llu orig %llu\n",dunitprice/COIN,(long long)received_assetoshis,(long long)orig_assetoshis); if ( fabs(dunitprice) > SMALLVAL && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) { remaining_assetoshis = (orig_assetoshis - received_assetoshis); @@ -142,6 +130,7 @@ bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue, bool ValidateAskRemainder(uint64_t remaining_nValue,uint64_t remaining_assetoshis,uint64_t orig_assetoshis,uint64_t received_assetoshis,uint64_t paid_nValue,uint64_t total_nValue) { uint64_t unitprice,recvunitprice,newunitprice=0; + fprintf(stderr,"%llu %llu %llu %llu %llu %llu\n",(long long)remaining_nValue,(long long)remaining_assetoshis,(long long)orig_assetoshis,(long long)received_assetoshis,(long long)paid_nValue,(long long)total_nValue); if ( orig_assetoshis == 0 || received_assetoshis == 0 || paid_nValue == 0 || total_nValue == 0 ) { fprintf(stderr,"ValidateAssetRemainder: orig_assetoshis == %llu || received_assetoshis == %llu || paid_nValue == %llu || total_nValue == %llu\n",(long long)orig_assetoshis,(long long)received_assetoshis,(long long)paid_nValue,(long long)total_nValue); From 6a2641f71cf90adac4b5bc4794c0f3dd9d1dc5cb Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 01:02:23 -1100 Subject: [PATCH 074/204] Test --- src/cc/CCassetsCore.cpp | 2 +- src/cc/assets.cpp | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 54323b916..87b04a17c 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -157,7 +157,7 @@ bool ValidateAskRemainder(uint64_t remaining_nValue,uint64_t remaining_assetoshi fprintf(stderr,"error recvunitprice %.8f < %.8f unitprice, new unitprice %.8f\n",(double)recvunitprice/COIN,(double)unitprice/COIN,(double)newunitprice/COIN); return(false); } - fprintf(stderr,"error recvunitprice %.8f >= %.8f unitprice, new unitprice %.8f\n",(double)recvunitprice/COIN,(double)unitprice/COIN,(double)newunitprice/COIN); + fprintf(stderr,"got recvunitprice %.8f >= %.8f unitprice, new unitprice %.8f\n",(double)recvunitprice/COIN,(double)unitprice/COIN,(double)newunitprice/COIN); } return(true); } diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index e8e1bfb90..9135a5cf7 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -303,8 +303,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx } //ValidateAssetRemainder: orig_nValue == 10 || received_nValue == 0 || paidunits == 10 || totalunits == 100000000000 //bool ValidateAssetRemainder(int32_t sellflag,uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidunits,uint64_t totalunits) - fprintf(stderr,"assets vout0 %llu, vin1 %llu, vout2 %llu -> orig, vout1 %llu, total %llu\n",(long long)tx.vout[0].nValue,(long long)assetoshis,(long long)tx.vout[2].nValue,(long long)tx.vout[1].nValue,(long long)totalunits); - if ( ValidateAskRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[2].nValue,tx.vout[1].nValue,totalunits) == false ) + if ( ValidateAskRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) return eval->Invalid("mismatched remainder for fill"); else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 ) return eval->Invalid("normal vout1 for fillask"); @@ -347,7 +346,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx inputs += assetoshis; } fprintf(stderr,"assets vout0 %llu, vin1 %llu, vout2 %llu -> orig, vout1 %llu, total %llu\n",(long long)tx.vout[0].nValue,(long long)assetoshis,(long long)tx.vout[2].nValue,(long long)tx.vout[1].nValue,(long long)totalunits); - if ( ValidateSwapRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[2].nValue,tx.vout[1].nValue,totalunits) == false ) + if ( ValidateSwapRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) return eval->Invalid("mismatched remainder for fill"); else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 ) return eval->Invalid("normal vout1 for fillask"); From f2f62e9ed982aaab63d6678f50e5963960b97393 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 01:17:50 -1100 Subject: [PATCH 075/204] Properly handle multiple vins funding fills --- src/cc/assets.cpp | 57 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 9135a5cf7..9f51bd46f 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -228,10 +228,21 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx else { inputs = 0; - for (i=2; iismyvin)(tx.vin[i].scriptSig) != 0 ) + { + if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 ) + { + fprintf(stderr,"i.%d starti.%d numvins.%d\n",i,starti,numvins); + return eval->Invalid("always should find vin, but didnt"); + } + else if ( (assetoshis= IsAssetvout(tmpprice,tmporigpubkey,vinTx,tx.vin[i].prevout.n,assetid)) != 0 && ConstrainVout(vinTx.vout[tx.vin[i].prevout.n],1,CCaddr,0) != 0 ) + { + fprintf(stderr,"vin%d %llu, ",i,(long long)assetoshis); + inputs += assetoshis; + } + } } if ( inputs == 0 ) return eval->Invalid("vout2 is normal for fillbuy"); @@ -296,14 +307,23 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx else { inputs = 0; - for (i=2; iismyvin)(tx.vin[i].scriptSig) == 0 ) + { + if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 ) + { + fprintf(stderr,"i.%d starti.%d numvins.%d\n",i,starti,numvins); + return eval->Invalid("always should find vin, but didnt"); + } + else if ( ConstrainVout(vinTx.vout[tx.vin[i].prevout.n],0,origaddr,0) != 0 ) + { + fprintf(stderr,"vin%d %llu, ",i,(long long)assetoshis); + inputs += vinTx.vout[tx.vin[i].prevout.n].nValue; + } + } } - //ValidateAssetRemainder: orig_nValue == 10 || received_nValue == 0 || paidunits == 10 || totalunits == 100000000000 - //bool ValidateAssetRemainder(int32_t sellflag,uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidunits,uint64_t totalunits) - if ( ValidateAskRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) + if ( ValidateAskRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,inputs,totalunits) == false ) return eval->Invalid("mismatched remainder for fill"); else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 ) return eval->Invalid("normal vout1 for fillask"); @@ -340,13 +360,24 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx else { inputs = 0; - for (i=2; iismyvin)(tx.vin[i].scriptSig) != 0 ) + { + if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 ) + { + fprintf(stderr,"i.%d starti.%d numvins.%d\n",i,starti,numvins); + return eval->Invalid("always should find vin, but didnt"); + } + else if ( (assetoshis= IsAssetvout(tmpprice,tmporigpubkey,vinTx,tx.vin[i].prevout.n,assetid)) != 0 && ConstrainVout(vinTx.vout[tx.vin[i].prevout.n],1,CCaddr,0) != 0 ) + { + fprintf(stderr,"vin%d %llu, ",i,(long long)assetoshis); + inputs += assetoshis; + } + } } fprintf(stderr,"assets vout0 %llu, vin1 %llu, vout2 %llu -> orig, vout1 %llu, total %llu\n",(long long)tx.vout[0].nValue,(long long)assetoshis,(long long)tx.vout[2].nValue,(long long)tx.vout[1].nValue,(long long)totalunits); - if ( ValidateSwapRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) + if ( ValidateSwapRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,inputs,totalunits) == false ) return eval->Invalid("mismatched remainder for fill"); else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 ) return eval->Invalid("normal vout1 for fillask"); From 87a6afd303114e802d8c7221cb4c9398d4f2cf6d Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 01:21:24 -1100 Subject: [PATCH 076/204] Test --- src/cc/assets.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 9f51bd46f..8bd19c58e 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -237,7 +237,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx fprintf(stderr,"i.%d starti.%d numvins.%d\n",i,starti,numvins); return eval->Invalid("always should find vin, but didnt"); } - else if ( (assetoshis= IsAssetvout(tmpprice,tmporigpubkey,vinTx,tx.vin[i].prevout.n,assetid)) != 0 && ConstrainVout(vinTx.vout[tx.vin[i].prevout.n],1,CCaddr,0) != 0 ) + else if ( (assetoshis= IsAssetvout(tmpprice,tmporigpubkey,vinTx,tx.vin[i].prevout.n,assetid)) != 0 ) { fprintf(stderr,"vin%d %llu, ",i,(long long)assetoshis); inputs += assetoshis; @@ -316,11 +316,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx fprintf(stderr,"i.%d starti.%d numvins.%d\n",i,starti,numvins); return eval->Invalid("always should find vin, but didnt"); } - else if ( ConstrainVout(vinTx.vout[tx.vin[i].prevout.n],0,origaddr,0) != 0 ) - { - fprintf(stderr,"vin%d %llu, ",i,(long long)assetoshis); - inputs += vinTx.vout[tx.vin[i].prevout.n].nValue; - } + inputs += vinTx.vout[tx.vin[i].prevout.n].nValue; } } if ( ValidateAskRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,inputs,totalunits) == false ) @@ -369,7 +365,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx fprintf(stderr,"i.%d starti.%d numvins.%d\n",i,starti,numvins); return eval->Invalid("always should find vin, but didnt"); } - else if ( (assetoshis= IsAssetvout(tmpprice,tmporigpubkey,vinTx,tx.vin[i].prevout.n,assetid)) != 0 && ConstrainVout(vinTx.vout[tx.vin[i].prevout.n],1,CCaddr,0) != 0 ) + else if ( (assetoshis= IsAssetvout(tmpprice,tmporigpubkey,vinTx,tx.vin[i].prevout.n,assetid)) != 0 ) { fprintf(stderr,"vin%d %llu, ",i,(long long)assetoshis); inputs += assetoshis; From 546f76cad315b3ac6f94a23dcedcce5720e56e48 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 01:31:14 -1100 Subject: [PATCH 077/204] Test --- src/cc/assets.cpp | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 8bd19c58e..d760c4825 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -227,7 +227,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("mismatched origpubkeys for fillbuy"); else { - inputs = 0; + /*inputs = 0; for (i=2; iismyvin)(tx.vin[i].scriptSig) != 0 ) @@ -237,18 +237,16 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx fprintf(stderr,"i.%d starti.%d numvins.%d\n",i,starti,numvins); return eval->Invalid("always should find vin, but didnt"); } - else if ( (assetoshis= IsAssetvout(tmpprice,tmporigpubkey,vinTx,tx.vin[i].prevout.n,assetid)) != 0 ) + else if ( (assetoshis= IsAssetvout(tmpprice,tmporigpubkey,vinTx,tx.vin[i].prevout.n,assetid)) != 0 && ConstrainVout(vinTx.vout[tx.vin[i].prevout.n],1,CCaddr,0) != 0 ) { fprintf(stderr,"vin%d %llu, ",i,(long long)assetoshis); inputs += assetoshis; } } - } - if ( inputs == 0 ) - return eval->Invalid("vout2 is normal for fillbuy"); - else if ( ConstrainVout(tx.vout[1],0,0,0) == 0 ) + }*/ + if ( ConstrainVout(tx.vout[1],0,0,0) == 0 ) return eval->Invalid("vout1 is CC for fillbuy"); - else if ( ValidateBidRemainder(remaining_price,tx.vout[0].nValue,nValue,tx.vout[1].nValue,inputs,totalunits) == false ) + else if ( ValidateBidRemainder(remaining_price,tx.vout[0].nValue,nValue,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) return eval->Invalid("mismatched remainder for fillbuy"); else if ( remaining_price != 0 ) { @@ -306,7 +304,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("mismatched origpubkeys for fill"); else { - inputs = 0; + /*inputs = 0; for (i=2; iismyvin)(tx.vin[i].scriptSig) == 0 ) @@ -318,8 +316,8 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx } inputs += vinTx.vout[tx.vin[i].prevout.n].nValue; } - } - if ( ValidateAskRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,inputs,totalunits) == false ) + }*/ + if ( ValidateAskRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) return eval->Invalid("mismatched remainder for fill"); else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 ) return eval->Invalid("normal vout1 for fillask"); @@ -356,24 +354,8 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx else { inputs = 0; - for (i=2; iismyvin)(tx.vin[i].scriptSig) != 0 ) - { - if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 ) - { - fprintf(stderr,"i.%d starti.%d numvins.%d\n",i,starti,numvins); - return eval->Invalid("always should find vin, but didnt"); - } - else if ( (assetoshis= IsAssetvout(tmpprice,tmporigpubkey,vinTx,tx.vin[i].prevout.n,assetid)) != 0 ) - { - fprintf(stderr,"vin%d %llu, ",i,(long long)assetoshis); - inputs += assetoshis; - } - } - } fprintf(stderr,"assets vout0 %llu, vin1 %llu, vout2 %llu -> orig, vout1 %llu, total %llu\n",(long long)tx.vout[0].nValue,(long long)assetoshis,(long long)tx.vout[2].nValue,(long long)tx.vout[1].nValue,(long long)totalunits); - if ( ValidateSwapRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,inputs,totalunits) == false ) + if ( ValidateSwapRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) return eval->Invalid("mismatched remainder for fill"); else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 ) return eval->Invalid("normal vout1 for fillask"); From 20eb6d30136b471457108746a9f4906197becab0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 01:33:23 -1100 Subject: [PATCH 078/204] Test --- 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 249b22690..d9672c61d 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -154,7 +154,7 @@ bool ConstrainVout(CTxOut vout,int32_t CCflag,char *cmpaddr,uint64_t nValue) } else if ( cmpaddr != 0 && (Getscriptaddress(destaddr,vout.scriptPubKey) == 0 || strcmp(destaddr,cmpaddr) != 0) ) { - //fprintf(stderr,"constrain vout error addr %s vs %s\n",cmpaddr!=0?cmpaddr:"",destaddr!=0?destaddr:""); + fprintf(stderr,"constrain vout error addr %s vs %s\n",cmpaddr!=0?cmpaddr:"",destaddr!=0?destaddr:""); return(false); } else if ( nValue != 0 && nValue != vout.nValue ) //(nValue == 0 && vout.nValue < 10000) || ( From a7b6ef9acc068a8f692d9eb9a90905f92d035969 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 01:36:42 -1100 Subject: [PATCH 079/204] Test --- src/cc/assets.cpp | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index d760c4825..54e95c95b 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -227,23 +227,6 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("mismatched origpubkeys for fillbuy"); else { - /*inputs = 0; - for (i=2; iismyvin)(tx.vin[i].scriptSig) != 0 ) - { - if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 ) - { - fprintf(stderr,"i.%d starti.%d numvins.%d\n",i,starti,numvins); - return eval->Invalid("always should find vin, but didnt"); - } - else if ( (assetoshis= IsAssetvout(tmpprice,tmporigpubkey,vinTx,tx.vin[i].prevout.n,assetid)) != 0 && ConstrainVout(vinTx.vout[tx.vin[i].prevout.n],1,CCaddr,0) != 0 ) - { - fprintf(stderr,"vin%d %llu, ",i,(long long)assetoshis); - inputs += assetoshis; - } - } - }*/ if ( ConstrainVout(tx.vout[1],0,0,0) == 0 ) return eval->Invalid("vout1 is CC for fillbuy"); else if ( ValidateBidRemainder(remaining_price,tx.vout[0].nValue,nValue,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) @@ -304,25 +287,10 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("mismatched origpubkeys for fill"); else { - /*inputs = 0; - for (i=2; iismyvin)(tx.vin[i].scriptSig) == 0 ) - { - if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 ) - { - fprintf(stderr,"i.%d starti.%d numvins.%d\n",i,starti,numvins); - return eval->Invalid("always should find vin, but didnt"); - } - inputs += vinTx.vout[tx.vin[i].prevout.n].nValue; - } - }*/ if ( ValidateAskRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) return eval->Invalid("mismatched remainder for fill"); else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 ) return eval->Invalid("normal vout1 for fillask"); - else if ( ConstrainVout(tx.vout[2],0,origaddr,0) == 0 ) - return eval->Invalid("CC vout2 for fillask"); else if ( remaining_price != 0 ) { if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) @@ -353,7 +321,6 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("mismatched origpubkeys for fill"); else { - inputs = 0; fprintf(stderr,"assets vout0 %llu, vin1 %llu, vout2 %llu -> orig, vout1 %llu, total %llu\n",(long long)tx.vout[0].nValue,(long long)assetoshis,(long long)tx.vout[2].nValue,(long long)tx.vout[1].nValue,(long long)totalunits); if ( ValidateSwapRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) return eval->Invalid("mismatched remainder for fill"); From ddf40bb69e6b95c913eaca81a54a979dcabfcc3a Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 02:06:43 -1100 Subject: [PATCH 080/204] Fix ask fill dest --- src/cc/CCassetstx.cpp | 2 +- src/cc/assets.cpp | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index ae0a3d593..49e668526 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -327,7 +327,7 @@ std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 ask CCchange = (inputs - paid_nValue); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,orig_assetoshis - received_assetoshis,GetUnspendable(cp,0))); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,received_assetoshis,mypk)); - mtx.vout.push_back(CTxOut(paid_nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + mtx.vout.push_back(CTxOut(paid_nValue,CScript() << origpubkey << OP_CHECKSIG)); if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet(assetid2!=zeroid?'E':'S',assetid,assetid2,remaining_nValue,origpubkey))); diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 54e95c95b..c13264c1e 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -229,6 +229,8 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx { if ( ConstrainVout(tx.vout[1],0,0,0) == 0 ) return eval->Invalid("vout1 is CC for fillbuy"); + else if ( ConstrainVout(tx.vout[2],1,CCaddr,0) == 0 ) + return eval->Invalid("vout1 is normal for fillbuy"); else if ( ValidateBidRemainder(remaining_price,tx.vout[0].nValue,nValue,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) return eval->Invalid("mismatched remainder for fillbuy"); else if ( remaining_price != 0 ) @@ -291,6 +293,8 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("mismatched remainder for fill"); else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 ) return eval->Invalid("normal vout1 for fillask"); + else if ( ConstrainVout(tx.vout[2],0,origaddr,0) == 0 ) + return eval->Invalid("normal vout1 for fillask"); else if ( remaining_price != 0 ) { if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) From 2e202399067d551e35bead7228c2ffdfcd9a87e7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 02:15:55 -1100 Subject: [PATCH 081/204] Test --- src/cc/CCassetsCore.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 87b04a17c..d92db3c01 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -164,7 +164,7 @@ bool ValidateAskRemainder(uint64_t remaining_nValue,uint64_t remaining_assetoshi bool SetSwapFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue,uint64_t orig_assetoshis,uint64_t &paid_nValue,uint64_t total_nValue) { - uint64_t remaining_assetoshis,unitprice; + uint64_t remaining_assetoshis; double dunitprice; if ( total_nValue == 0 ) { received_assetoshis = remaining_nValue = paid_nValue = 0; @@ -179,14 +179,14 @@ bool SetSwapFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue return(true); } remaining_nValue = (total_nValue - paid_nValue); - unitprice = (double)(total_nValue * COIN) / orig_assetoshis; - received_assetoshis = (paid_nValue * COIN) / unitprice; - fprintf(stderr,"remaining_nValue %llu (%llu - %llu)\n",(long long)remaining_nValue,(long long)total_nValue,(long long)paid_nValue); - fprintf(stderr,"unitprice %llu received_assetoshis %llu orig %llu\n",(long long)unitprice,(long long)received_assetoshis,(long long)orig_assetoshis); - if ( unitprice > 0 && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) + dunitprice = ((double)total_nValue / orig_assetoshis); + received_assetoshis = (paid_nValue / dunitprice); + fprintf(stderr,"remaining_nValue %.8f (%.8f - %.8f)\n",(double)remaining_nValue/COIN,(double)total_nValue/COIN,(double)paid_nValue/COIN); + fprintf(stderr,"unitprice %.8f received_assetoshis %llu orig %llu\n",dunitprice/COIN,(long long)received_assetoshis,(long long)orig_assetoshis); + if ( fabs(dunitprice) > SMALLVAL && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) { remaining_assetoshis = (orig_assetoshis - received_assetoshis); - return(ValidateSwapRemainder(remaining_nValue,remaining_assetoshis,orig_assetoshis,received_assetoshis,paid_nValue,total_nValue)); + return(ValidateAskRemainder(remaining_nValue,remaining_assetoshis,orig_assetoshis,received_assetoshis,paid_nValue,total_nValue)); } else return(false); } From fac877649787742fa89319b18d9e001828d92af2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 03:31:05 -1100 Subject: [PATCH 082/204] Rewards functions --- src/cc/CCinclude.h | 1 + src/cc/CCtx.cpp | 19 +++++++++++++ src/cc/rewards.cpp | 70 +++++++++++++++++++++++++++++++--------------- 3 files changed, 67 insertions(+), 23 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 8dfc9bf6a..da1cf252e 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -72,6 +72,7 @@ bool Myprivkey(uint8_t myprivkey[]); // CCtx std::string FinalizeCCTx(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret); void SetCCunspents(std::vector > &unspentOutputs,char *coinaddr); +void SetCCtxids(std::vector > &addressIndex,char *coinaddr); uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,int32_t maxinputs); #endif diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index 3f871de74..7b8aaa866 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -163,6 +163,25 @@ void SetCCunspents(std::vector > &addressIndex,char *coinaddr) +{ + int32_t type=0,i,n; char *ptr; std::string addrstr; uint160 hashBytes; std::vector > addresses; + n = (int32_t)strlen(coinaddr); + addrstr.resize(n+1); + ptr = (char *)addrstr.data(); + for (i=0; i<=n; i++) + ptr[i] = coinaddr[i]; + CBitcoinAddress address(addrstr); + if ( address.GetIndexKey(hashBytes, type) == 0 ) + return; + addresses.push_back(std::make_pair(hashBytes,type)); + for (std::vector >::iterator it = addresses.begin(); it != addresses.end(); it++) + { + if ( GetAddressIndex((*it).first, (*it).second, addressIndex) == 0 ) + return; + } +} + uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,int32_t maxinputs) { int32_t vout,j,n = 0; uint64_t nValue,totalinputs = 0; uint256 txid; std::vector vecOutputs; diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 17c838c51..42e7c05bf 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -169,20 +169,25 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t uint64_t AddRewardsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { - char coinaddr[64]; uint64_t nValue,totalinputs = 0; uint256 txid,hashBlock; CTransaction vintx; int32_t n = 0; + char coinaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,nValue,totalinputs = 0; uint256 txid,hashBlock,fundingtxid; CTransaction vintx; int32_t j,vout,n = 0; uint8_t funcid; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; - // prevent dup - if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) + vout = (int32_t)it->first.index; + for (j=0; jfirst.index)) > 0 ) + if ( (funcid= DecodeRewardsFundingOpRet(tx.vout[vintx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || DecodeRewardsOpRet(tx.vout[vintx.vout.size()-1].scriptPubKey,sbits,fundingtxid) != 0 ) { if ( total != 0 && maxinputs != 0 ) - mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript())); + mtx.vin.push_back(CTxIn(txid,vout,CScript())); totalinputs += it->second.satoshis; n++; if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) ) @@ -193,33 +198,51 @@ uint64_t AddRewardsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CP return(totalinputs); } -uint64_t RewardsPlanFunds(uint64_t &refsbits,struct CCcontract_info *cp,CPubKey &pk,char *planstr,uint256 fundingtxid) +uint64_t RewardsPlanFunds(uint64_t refsbits,struct CCcontract_info *cp,CPubKey pk,uint256 reffundingtxid) { - char coinaddr[64]; uint64_t sbits,nValue,totalinputs = 0; uint256 hashBlock; CTransaction vintx; + char coinaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,nValue,totalinputs = 0; uint256 txid,hashBlock; CTransaction tx; int32_t vout; uint8_t funcid; std::vector > unspentOutputs; - if ( planstr == 0 || planstr[0] == 0 || strlen(planstr) > 8 ) - return(0); - refsbits = stringbits(planstr); GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { - if ( GetTransaction(it->first.txhash,vintx,hashBlock,false) != 0 ) + txid = it->first.txhash; + vout = (int32_t)it->first.index; + if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (nValue= IsRewardsvout(cp,vintx,(int32_t)it->first.index)) > 0 ) + if ( (funcid= DecodeRewardsFundingOpRet(tx.vout[vintx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || DecodeRewardsOpRet(tx.vout[vintx.vout.size()-1].scriptPubKey,sbits,fundingtxid) != 0 ) { - - totalinputs += nValue; + if ( (funcid == 'F' && reffundingtxid == txid) || reffundingtxid == fundingtxid ) + { + if ( refsbits == sbits && (nValue= IsRewardsvout(cp,tx,vout)) > 0 ) + totalinputs += nValue; + } } } } return(totalinputs); } -bool RewardsPlanExists(uint64_t sbits) +bool RewardsPlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey rewardspk) { - // get list of all possible plans - // compare sbits + char coinaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit; uint256 txid,hashBlock; CTransaction tx; + std::vector > txids; + GetCCaddress(cp,coinaddr,rewardspk); + SetCCtxids(txids,coinaddr); + for (std::vector >::const_iterator it=txids.begin(); it!=txids.end(); it++) + { + //int height = it->first.blockHeight; + txid = it->first.txhash; + if ( GetTransaction(txid,tx,hashBlock,false) != 0 && vintx.vout.size() > 0 && ConstrainVout(tx.vout[0],1,CCaddr,0) != 0 ) + { + if ( DecodeRewardsFundingOpRet(tx.vout[vintx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit) == 'F' ) + { + if ( sbits == refsbits ) + return(true); + } + } + } + return(false); } std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid) @@ -230,7 +253,8 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 txfee = 10000; rewardspk = GetUnspendable(cp,0); mypk = pubkey2pk(Mypubkey()); - funding = RewardsPlanFunds(sbits,cp,rewardspk,planstr,fundingtxid); + sbits = stringbits(planstr); + funding = RewardsPlanFunds(sbits,cp,rewardspk,fundingtxid); // set plan params if ( locktxid == zeroid ) amount= AddRewardsInputs(cp,mtx,mypk,(1LL << 30),1); //else amount = value...; @@ -253,18 +277,17 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 std::string RewardsCreateFunding(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) { - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); - if ( RewardsPlanExists(sbits) != 0 ) + if ( RewardsPlanExists(cp,sbits,rewardspk) != 0 ) { fprintf(stderr,"Rewards plan %s already exists\n",planstr); return(0); } - funding = RewardsPlanFunds(sbits,cp,rewardspk,planstr,zeroid); if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,funds,rewardspk)); @@ -283,7 +306,7 @@ std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,u txfee = 10000; mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); - if ( RewardsPlanExists(sbits) == 0 ) + if ( RewardsPlanExists(cp,sbits,rewardspk) == 0 ) { fprintf(stderr,"Rewards plan %s doesnt exist\n",planstr); return(0); @@ -305,7 +328,8 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_ txfee = 10000; mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); - if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,planstr,fundingtxid)) >= amount ) // arbitrary cmp + sbits = stringbits(planstr); + if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,fundingtxid)) >= amount ) // arbitrary cmpval { if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 ) { From 4610bb91884e66a93a88639fff6b8129d5b2788c Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 03:33:10 -1100 Subject: [PATCH 083/204] Fix --- src/cc/rewards.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 42e7c05bf..ef1d0e4cc 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -169,7 +169,7 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t uint64_t AddRewardsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { - char coinaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,nValue,totalinputs = 0; uint256 txid,hashBlock,fundingtxid; CTransaction vintx; int32_t j,vout,n = 0; uint8_t funcid; + char coinaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,nValue,totalinputs = 0; uint256 txid,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); @@ -200,7 +200,7 @@ uint64_t AddRewardsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CP uint64_t RewardsPlanFunds(uint64_t refsbits,struct CCcontract_info *cp,CPubKey pk,uint256 reffundingtxid) { - char coinaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,nValue,totalinputs = 0; uint256 txid,hashBlock; CTransaction tx; int32_t vout; uint8_t funcid; + char coinaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,nValue,totalinputs = 0; uint256 txid,hashBlock,fundingtxid; CTransaction tx; int32_t vout; uint8_t funcid; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); @@ -225,10 +225,10 @@ uint64_t RewardsPlanFunds(uint64_t refsbits,struct CCcontract_info *cp,CPubKey p bool RewardsPlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey rewardspk) { - char coinaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit; uint256 txid,hashBlock; CTransaction tx; + char CCaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit; uint256 txid,hashBlock; CTransaction tx; std::vector > txids; - GetCCaddress(cp,coinaddr,rewardspk); - SetCCtxids(txids,coinaddr); + GetCCaddress(cp,CCaddr,rewardspk); + SetCCtxids(txids,CCaddr); for (std::vector >::const_iterator it=txids.begin(); it!=txids.end(); it++) { //int height = it->first.blockHeight; From d18420d255906a8623fc7cc15add15d38d178da8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 03:34:27 -1100 Subject: [PATCH 084/204] Test --- src/cc/rewards.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index ef1d0e4cc..7e1a15402 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -184,7 +184,7 @@ uint64_t AddRewardsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CP continue; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (funcid= DecodeRewardsFundingOpRet(tx.vout[vintx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || DecodeRewardsOpRet(tx.vout[vintx.vout.size()-1].scriptPubKey,sbits,fundingtxid) != 0 ) + if ( (funcid= DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || DecodeRewardsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid) != 0 ) { if ( total != 0 && maxinputs != 0 ) mtx.vin.push_back(CTxIn(txid,vout,CScript())); @@ -210,7 +210,7 @@ uint64_t RewardsPlanFunds(uint64_t refsbits,struct CCcontract_info *cp,CPubKey p vout = (int32_t)it->first.index; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (funcid= DecodeRewardsFundingOpRet(tx.vout[vintx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || DecodeRewardsOpRet(tx.vout[vintx.vout.size()-1].scriptPubKey,sbits,fundingtxid) != 0 ) + if ( (funcid= DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || DecodeRewardsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid) != 0 ) { if ( (funcid == 'F' && reffundingtxid == txid) || reffundingtxid == fundingtxid ) { @@ -233,9 +233,9 @@ bool RewardsPlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey rewa { //int height = it->first.blockHeight; txid = it->first.txhash; - if ( GetTransaction(txid,tx,hashBlock,false) != 0 && vintx.vout.size() > 0 && ConstrainVout(tx.vout[0],1,CCaddr,0) != 0 ) + if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && ConstrainVout(tx.vout[0],1,CCaddr,0) != 0 ) { - if ( DecodeRewardsFundingOpRet(tx.vout[vintx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit) == 'F' ) + if ( DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit) == 'F' ) { if ( sbits == refsbits ) return(true); From db6bfcc4f620f27cbcec9008f142e7c60aea4d33 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 03:49:26 -1100 Subject: [PATCH 085/204] Params to rewardscalc --- src/cc/rewards.cpp | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 7e1a15402..a14fe3c62 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -37,7 +37,7 @@ Unlock does a CC spend to the vout1 address */ -uint64_t RewardsCalc(uint64_t claim,uint256 txid) // min/max time, mindeposit and rate +uint64_t RewardsCalc(uint64_t claim,uint256 txid,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) { uint64_t reward = 0; // get start time, get current time @@ -223,9 +223,9 @@ uint64_t RewardsPlanFunds(uint64_t refsbits,struct CCcontract_info *cp,CPubKey p return(totalinputs); } -bool RewardsPlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey rewardspk) +bool RewardsPlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey rewardspk,uint64_t &APR,uint64_t &minseconds,uint64_t &maxseconds,uint64_t &mindeposit) { - char CCaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit; uint256 txid,hashBlock; CTransaction tx; + char CCaddr[64]; uint64_t sbits; uint256 txid,hashBlock; CTransaction tx; std::vector > txids; GetCCaddress(cp,CCaddr,rewardspk); SetCCtxids(txids,CCaddr); @@ -247,18 +247,26 @@ bool RewardsPlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey rewa std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid) { - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,sbits,reward,amount=0,inputs,CCchange=0; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,sbits,reward,amount=0,inputs,CCchange=0,APR,minseconds,maxseconds,mindeposit; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; rewardspk = GetUnspendable(cp,0); mypk = pubkey2pk(Mypubkey()); sbits = stringbits(planstr); - funding = RewardsPlanFunds(sbits,cp,rewardspk,fundingtxid); // set plan params + if ( RewardsPlanExists(cp,sbits,rewardspk,APR,minseconds,maxseconds,mindeposit) == 0 ) + { + fprintf(stderr,"Rewards plan %s doesnt exist\n",planstr); + return(0); + } if ( locktxid == zeroid ) - amount= AddRewardsInputs(cp,mtx,mypk,(1LL << 30),1); - //else amount = value...; - if ( amount > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash)) > txfee ) + amount = AddRewardsInputs(cp,mtx,rewardspk,(1LL << 30),1); + else + { + fprintf(stderr,"check if locktxid is unspent\n"); + return(0); + } + if ( amount > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash,APR,minseconds,maxseconds,mindeposit)) > txfee ) { if ( (inputs= AddRewardsInputs(cp,mtx,mypk,reward+amount+txfee,30)) > 0 ) { @@ -277,13 +285,13 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 std::string RewardsCreateFunding(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) { - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,a,b,c,d; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); - if ( RewardsPlanExists(cp,sbits,rewardspk) != 0 ) + if ( RewardsPlanExists(cp,sbits,rewardspk,a,b,c,d) != 0 ) { fprintf(stderr,"Rewards plan %s already exists\n",planstr); return(0); @@ -300,13 +308,13 @@ std::string RewardsCreateFunding(uint64_t txfee,char *planstr,uint64_t funds,uin std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount) { - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,a,b,c,d; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); - if ( RewardsPlanExists(cp,sbits,rewardspk) == 0 ) + if ( RewardsPlanExists(cp,sbits,rewardspk,a,b,c,d) == 0 ) { fprintf(stderr,"Rewards plan %s doesnt exist\n",planstr); return(0); From 1d801ae6897c312d6688f7bec916e8c7e0d2dba0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 03:52:22 -1100 Subject: [PATCH 086/204] Create funding, addfunding, lock --- src/cc/rewards.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index a14fe3c62..fc8a61fd6 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -328,7 +328,7 @@ std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,u return(0); } -std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount) +std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t deposit) { CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); @@ -337,11 +337,21 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_ mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); sbits = stringbits(planstr); - if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,fundingtxid)) >= amount ) // arbitrary cmpval + if ( RewardsPlanExists(cp,sbits,rewardspk,APR,minseconds,maxseconds,mindeposit) == 0 ) { - if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 ) + fprintf(stderr,"Rewards plan %s doesnt exist\n",planstr); + return(0); + } + if ( deposit < mindeposit ) + { + fprintf(stderr,"Rewards plan %s deposit %.8f < mindeposit %.8f\n",planstr,(double)deposit/COIN,(double)mindeposit/COIN); + return(0); + } + if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,fundingtxid)) >= deposit ) // arbitrary cmpval + { + if ( AddNormalinputs(mtx,mypk,deposit+txfee,64) > 0 ) { - mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,rewardspk)); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,deposit,rewardspk)); return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('L',sbits,fundingtxid))); } else fprintf(stderr,"cant find enough inputs\n"); } From 87ccafbd6f87e6ee0ff12e3d4d0fb86e9b5bb28a Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 03:55:59 -1100 Subject: [PATCH 087/204] Test --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index fc8a61fd6..1dbe5b591 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -330,7 +330,7 @@ std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,u std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t deposit) { - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding,APR,minseconds,maxseconds,mindeposit; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; From 3ed54ff10c83392a82051c9d804041c64cd64620 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 04:19:15 -1100 Subject: [PATCH 088/204] +print --- src/cc/CCassetstx.cpp | 3 ++- src/cc/rewards.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 49e668526..efdf30299 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -218,8 +218,9 @@ std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 opret = EncodeAssetOpRet('s',assetid,zeroid,pricetotal,Mypubkey()); else opret = EncodeAssetOpRet('e',assetid,assetid2,pricetotal,Mypubkey()); return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); - } + } else fprintf(stderr,"need some assets to place ask\n"); } + fprintf(stderr,"need some native coins to place ask\n"); return(0); } diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 1dbe5b591..8093f57b4 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -40,7 +40,7 @@ uint64_t RewardsCalc(uint64_t claim,uint256 txid,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) { uint64_t reward = 0; - // get start time, get current time + // get txtime2, get pblock->nTime // if elapsed < mintime -> return 0 // if elapsed > maxtime, elapsed = maxtime // calc reward From c0198c14d7d217851aa87a3fb48fad34af62cb2e Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 06:04:46 -1100 Subject: [PATCH 089/204] tokenswapask --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index d1fef5092..210055d42 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5278,7 +5278,7 @@ UniValue tokenswapask(const UniValue& params, bool fHelp) static uint256 zeroid; UniValue result(UniValue::VOBJ); uint64_t askamount,numtokens; std::string hex; double price; uint256 tokenid,otherid; if ( fHelp || params.size() != 4 ) - throw runtime_error("tokenswap numtokens tokenid otherid price\n"); + throw runtime_error("tokenswapask numtokens tokenid otherid price\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"); numtokens = atoi(params[0].get_str().c_str()); From 352d59af121c77c91e4a6d22dd4db07346ea5d57 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 06:12:19 -1100 Subject: [PATCH 090/204] Test --- src/cc/CCassetsCore.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index d92db3c01..9a2b18418 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -167,6 +167,7 @@ bool SetSwapFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue uint64_t remaining_assetoshis; double dunitprice; if ( total_nValue == 0 ) { + fprintf(stderr,"total_nValue.0 origsatoshis.%llu paid_nValue.%llu\n",(long long)orig_satoshis,(long long)paid_nValue); received_assetoshis = remaining_nValue = paid_nValue = 0; return(false); } From 6d7133ae0cf8f741ecb8788eedd9e08a97893281 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 06:13:28 -1100 Subject: [PATCH 091/204] Test --- src/cc/CCassetsCore.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 9a2b18418..9293603ff 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -167,7 +167,7 @@ bool SetSwapFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue uint64_t remaining_assetoshis; double dunitprice; if ( total_nValue == 0 ) { - fprintf(stderr,"total_nValue.0 origsatoshis.%llu paid_nValue.%llu\n",(long long)orig_satoshis,(long long)paid_nValue); + fprintf(stderr,"total_nValue.0 origsatoshis.%llu paid_nValue.%llu\n",(long long)orig_assetoshis,(long long)paid_nValue); received_assetoshis = remaining_nValue = paid_nValue = 0; return(false); } From fff7c5d24989259c62ba7097128489a54029e607 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 06:42:17 -1100 Subject: [PATCH 092/204] Swap functions --- src/cc/CCassets.h | 3 ++- src/cc/CCassetstx.cpp | 35 +++++++++++++++++++++++++++++++++-- src/wallet/rpcwallet.cpp | 7 +++---- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/cc/CCassets.h b/src/cc/CCassets.h index 3a3df3680..187dc4219 100644 --- a/src/cc/CCassets.h +++ b/src/cc/CCassets.h @@ -54,7 +54,8 @@ std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector de std::string CreateBuyOffer(uint64_t txfee,uint64_t bidamount,uint256 assetid,uint64_t pricetotal); std::string CancelBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid); std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t fillamount); -std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 assetid2,uint64_t pricetotal); +std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint64_t pricetotal); +std::string CreateSwap(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 assetid2,uint64_t pricetotal); std::string CancelSell(uint64_t txfee,uint256 assetid,uint256 asktxid); std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,uint64_t fillamount); diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index efdf30299..dce1efd53 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -198,7 +198,7 @@ std::string CreateBuyOffer(uint64_t txfee,uint64_t bidamount,uint256 assetid,uin return(0); } -std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 assetid2,uint64_t pricetotal) +std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint64_t pricetotal) { CMutableTransaction mtx; CPubKey mypk; uint64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_ASSETS); @@ -209,6 +209,34 @@ std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 { if ( (inputs= AddAssetInputs(cp,mtx,mypk,assetid,askamount,60)) > 0 ) { + if ( inputs < askamount ) + askamount = inputs; + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount,GetUnspendable(cp,0))); + if ( inputs > askamount ) + CCchange = (inputs - askamount); + if ( CCchange != 0 ) + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); + opret = EncodeAssetOpRet('s',assetid,zeroid,pricetotal,Mypubkey()); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } else fprintf(stderr,"need some assets to place ask\n"); + } + fprintf(stderr,"need some native coins to place ask\n"); + return(0); +} + +std::string CreateSwap(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 assetid2,uint64_t pricetotal) +{ + CMutableTransaction mtx; CPubKey mypk; uint64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + if ( AddNormalinputs(mtx,mypk,txfee,1) > 0 ) + { + if ( (inputs= AddAssetInputs(cp,mtx,mypk,assetid,askamount,60)) > 0 ) + { + if ( inputs < askamount ) + askamount = inputs; mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount,GetUnspendable(cp,0))); if ( inputs > askamount ) CCchange = (inputs - askamount); @@ -216,7 +244,10 @@ std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); if ( assetid2 == zeroid ) opret = EncodeAssetOpRet('s',assetid,zeroid,pricetotal,Mypubkey()); - else opret = EncodeAssetOpRet('e',assetid,assetid2,pricetotal,Mypubkey()); + else + { + opret = EncodeAssetOpRet('e',assetid,assetid2,pricetotal,Mypubkey()); + } return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"need some assets to place ask\n"); } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 210055d42..f2811260b 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5254,7 +5254,6 @@ UniValue tokenfillbid(const UniValue& params, bool fHelp) UniValue tokenask(const UniValue& params, bool fHelp) { - static uint256 zeroid; UniValue result(UniValue::VOBJ); uint64_t askamount,numtokens; std::string hex; double price; uint256 tokenid; if ( fHelp || params.size() != 3 ) throw runtime_error("tokenask numtokens tokenid price\n"); @@ -5264,7 +5263,7 @@ UniValue tokenask(const UniValue& params, bool fHelp) tokenid = Parseuint256((char *)params[1].get_str().c_str()); price = atof(params[2].get_str().c_str()); askamount = (price * numtokens) * COIN + 0.0000000049999; - hex = CreateSell(0,numtokens,tokenid,zeroid,askamount); + hex = CreateSell(0,numtokens,tokenid,askamount); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -5285,8 +5284,8 @@ UniValue tokenswapask(const UniValue& params, bool fHelp) tokenid = Parseuint256((char *)params[1].get_str().c_str()); otherid = Parseuint256((char *)params[2].get_str().c_str()); price = atof(params[3].get_str().c_str()); - askamount = (price * numtokens) * COIN + 0.0000000049999; - hex = CreateSell(0,numtokens,tokenid,otherid,askamount); + askamount = (price * numtokens); + hex = CreateSwap(0,numtokens,tokenid,otherid,askamount); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); From 0f4a3ceaa92c74fd486881c96970734250ed111c Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Thu, 26 Jul 2018 16:06:01 -0300 Subject: [PATCH 093/204] ac_cc under 1000 is not fungible --- src/cc/import.cpp | 3 +++ src/crosschain.cpp | 2 +- src/rpccrosschain.cpp | 8 ++++---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/cc/import.cpp b/src/cc/import.cpp index 26f878369..33e6a5b60 100644 --- a/src/cc/import.cpp +++ b/src/cc/import.cpp @@ -54,6 +54,9 @@ bool Eval::ImportCoin(const std::vector params, const CTransaction &imp if (targetCcid != GetAssetchainsCC() || targetSymbol != GetAssetchainsSymbol()) return Invalid("importcoin-wrong-chain"); + if (targetCcid <= 1000) + return Invalid("chain-not-fungible"); + // check burn amount { uint64_t burnAmount = burnTx.vout[0].nValue; diff --git a/src/crosschain.cpp b/src/crosschain.cpp index ca7223abb..197390e59 100644 --- a/src/crosschain.cpp +++ b/src/crosschain.cpp @@ -42,7 +42,7 @@ uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int kmdHeigh * > scan backwards > */ - if (targetCCid <= 1) + if (targetCCid < 2) return uint256(); if (kmdHeight < 0 || kmdHeight > chainActive.Height()) diff --git a/src/rpccrosschain.cpp b/src/rpccrosschain.cpp index 35542613a..44f55b44a 100644 --- a/src/rpccrosschain.cpp +++ b/src/rpccrosschain.cpp @@ -153,8 +153,8 @@ UniValue migrate_converttoexport(const UniValue& params, bool fHelp) "import transaction.\n" ); - if (ASSETCHAINS_CC < 2) - throw runtime_error("-ac_cc < 2"); + if (ASSETCHAINS_CC <= 1000) + throw runtime_error("-ac_cc <= 1000"); if (ASSETCHAINS_SYMBOL[0] == 0) throw runtime_error("Must be called on assetchain"); @@ -208,8 +208,8 @@ UniValue migrate_createimporttransaction(const UniValue& params, bool fHelp) throw runtime_error("migrate_createimporttransaction burnTx payouts\n\n" "Create an importTx given a burnTx and the corresponding payouts, hex encoded"); - if (ASSETCHAINS_CC < 2) - throw runtime_error("-ac_cc < 2"); + if (ASSETCHAINS_CC <= 1000) + throw runtime_error("-ac_cc <= 1000"); if (ASSETCHAINS_SYMBOL[0] == 0) throw runtime_error("Must be called on assetchain"); From 43346db9d188a6cb01482b6c67b793b83152dd16 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 00:10:21 -1100 Subject: [PATCH 094/204] Allow to cancel invalid orders --- src/cc/assets.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index c13264c1e..86037235a 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -140,9 +140,9 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx if ( (funcid= DecodeAssetOpRet(tx.vout[numvouts-1].scriptPubKey,assetid,assetid2,remaining_price,origpubkey)) == 0 ) return eval->Invalid("Invalid opreturn payload"); fprintf(stderr,"AssetValidate (%c)\n",funcid); - if ( eval->GetTxUnconfirmed(assetid,createTx,hashBlock) == 0 ) + if ( cp->evalcode != 'o' && cp->evalcode != 'x' && eval->GetTxUnconfirmed(assetid,createTx,hashBlock) == 0 ) return eval->Invalid("cant find asset create txid"); - else if ( assetid2 != zero && eval->GetTxUnconfirmed(assetid2,createTx,hashBlock) == 0 ) + else if ( cp->evalcode != 'o' && cp->evalcode != 'x' && assetid2 != zero && eval->GetTxUnconfirmed(assetid2,createTx,hashBlock) == 0 ) return eval->Invalid("cant find asset2 create txid"); else if ( IsCCInput(tx.vin[0].scriptSig) != 0 ) return eval->Invalid("illegal asset vin0"); From c1861a13b84dd34027a59588dbb94406f6ecf094 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 00:19:58 -1100 Subject: [PATCH 095/204] Prevent negative numbers in assets tx --- src/cc/CCassets.h | 14 ++++++------- src/cc/CCassetstx.cpp | 47 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/cc/CCassets.h b/src/cc/CCassets.h index 187dc4219..3365c55a7 100644 --- a/src/cc/CCassets.h +++ b/src/cc/CCassets.h @@ -49,14 +49,14 @@ bool AssetExactAmounts(struct CCcontract_info *cp,uint64_t &inputs,int32_t start uint64_t GetAssetBalance(CPubKey pk,uint256 tokenid); uint64_t AddAssetInputs(CMutableTransaction &mtx,CPubKey pk,uint256 assetid,uint64_t total,int32_t maxinputs); UniValue AssetOrders(uint256 tokenid); -std::string CreateAsset(uint64_t txfee,uint64_t assetsupply,std::string name,std::string description); -std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector destpubkey,uint64_t total); -std::string CreateBuyOffer(uint64_t txfee,uint64_t bidamount,uint256 assetid,uint64_t pricetotal); +std::string CreateAsset(uint64_t txfee,int64_t assetsupply,std::string name,std::string description); +std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector destpubkey,int64_t total); +std::string CreateBuyOffer(uint64_t txfee,int64_t bidamount,uint256 assetid,int64_t pricetotal); std::string CancelBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid); -std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t fillamount); -std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint64_t pricetotal); -std::string CreateSwap(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 assetid2,uint64_t pricetotal); +std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,int64_t fillamount); +std::string CreateSell(uint64_t txfee,int64_t askamount,uint256 assetid,int64_t pricetotal); +std::string CreateSwap(uint64_t txfee,int64_t askamount,uint256 assetid,uint256 assetid2,int64_t pricetotal); std::string CancelSell(uint64_t txfee,uint256 assetid,uint256 asktxid); -std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,uint64_t fillamount); +std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,int64_t fillamount); #endif diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index dce1efd53..1e44cf2cd 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -133,9 +133,14 @@ UniValue AssetOrders(uint256 refassetid) return(result); } -std::string CreateAsset(uint64_t txfee,uint64_t assetsupply,std::string name,std::string description) +std::string CreateAsset(uint64_t txfee,int64_t assetsupply,std::string name,std::string description) { CMutableTransaction mtx; CPubKey mypk; struct CCcontract_info *cp,C; + if ( assetsupply < 0 ) + { + fprintf(stderr,"negative assetsupply %lld\n",(long long)assetsupply); + return(0); + } cp = CCinit(&C,EVAL_ASSETS); if ( name.size() > 32 || description.size() > 4096 ) { @@ -154,9 +159,14 @@ std::string CreateAsset(uint64_t txfee,uint64_t assetsupply,std::string name,std return(0); } -std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector destpubkey,uint64_t total) +std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector destpubkey,int64_t total) { CMutableTransaction mtx; CPubKey mypk; uint64_t CCchange=0,inputs=0; struct CCcontract_info *cp,C; + if ( total < 0 ) + { + fprintf(stderr,"negative total %lld\n",(long long)total); + return(0); + } cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; @@ -183,9 +193,14 @@ std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector de return(0); } -std::string CreateBuyOffer(uint64_t txfee,uint64_t bidamount,uint256 assetid,uint64_t pricetotal) +std::string CreateBuyOffer(uint64_t txfee,int64_t bidamount,uint256 assetid,int64_t pricetotal) { CMutableTransaction mtx; CPubKey mypk; struct CCcontract_info *cp,C; + if ( bidamount < 0 || pricetotal < 0 ) + { + fprintf(stderr,"negative bidamount %lld, pricetotal %lld\n",(long long)bidamount,(long long)pricetotal); + return(0); + } cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; @@ -198,9 +213,14 @@ std::string CreateBuyOffer(uint64_t txfee,uint64_t bidamount,uint256 assetid,uin return(0); } -std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint64_t pricetotal) +std::string CreateSell(uint64_t txfee,int64_t askamount,uint256 assetid,int64_t pricetotal) { CMutableTransaction mtx; CPubKey mypk; uint64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; + if ( askamount < 0 || pricetotal < 0 ) + { + fprintf(stderr,"negative askamount %lld, askamount %lld\n",(long long)pricetotal,(long long)askamount); + return(0); + } cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; @@ -224,9 +244,14 @@ std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint64_ return(0); } -std::string CreateSwap(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 assetid2,uint64_t pricetotal) +std::string CreateSwap(uint64_t txfee,int64_t askamount,uint256 assetid,uint256 assetid2,int64_t pricetotal) { CMutableTransaction mtx; CPubKey mypk; uint64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; + if ( askamount < 0 || pricetotal < 0 ) + { + fprintf(stderr,"negative askamount %lld, askamount %lld\n",(long long)pricetotal,(long long)askamount); + return(0); + } cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; @@ -295,9 +320,14 @@ std::string CancelSell(uint64_t txfee,uint256 assetid,uint256 asktxid) return(0); } -std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t fillamount) +std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,int64_t fillamount) { CTransaction vintx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t bidvout=0; uint64_t origprice,bidamount,paid_amount,remaining_required,inputs,CCchange=0; struct CCcontract_info *cp,C; + if ( fillamount < 0 ) + { + fprintf(stderr,"negative fillamount %lld\n",(long long)fillamount); + return(0); + } cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; @@ -332,6 +362,11 @@ std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,uint64_t fillunits) { CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; double dprice; int32_t askvout=0; uint64_t received_assetoshis,total_nValue,orig_assetoshis,paid_nValue,remaining_nValue,inputs,CCchange=0; struct CCcontract_info *cp,C; + if ( fillunits < 0 ) + { + fprintf(stderr,"negative fillunits %lld\n",(long long)fillunits); + return(0); + } cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; From 4b83ae999b32fae57943b7fdc1626abde869898a Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 00:27:34 -1100 Subject: [PATCH 096/204] Uint64 -> int64 --- src/cc/CCassets.h | 48 +++++++++++++++++++-------------------- src/cc/CCassetsCore.cpp | 50 ++++++++++++++++++++--------------------- src/cc/CCassetstx.cpp | 40 ++++++++++++++++----------------- src/cc/assets.cpp | 2 +- 4 files changed, 70 insertions(+), 70 deletions(-) diff --git a/src/cc/CCassets.h b/src/cc/CCassets.h index 3365c55a7..0b566bdd2 100644 --- a/src/cc/CCassets.h +++ b/src/cc/CCassets.h @@ -31,32 +31,32 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx // CCassetsCore //CTxOut MakeAssetsVout(CAmount nValue,CPubKey pk); CScript EncodeAssetCreateOpRet(uint8_t funcid,std::vector origpubkey,std::string name,std::string description); -CScript EncodeAssetOpRet(uint8_t funcid,uint256 assetid,uint256 assetid2,uint64_t price,std::vector origpubkey); -uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint256 &assetid,uint256 &assetid2,uint64_t &price,std::vector &origpubkey); -bool SetAssetOrigpubkey(std::vector &origpubkey,uint64_t &price,const CTransaction &tx); -uint64_t IsAssetvout(uint64_t &price,std::vector &origpubkey,const CTransaction& tx,int32_t v,uint256 refassetid); -bool ValidateBidRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidprice,uint64_t totalprice); -bool ValidateAskRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidprice,uint64_t totalprice); -bool ValidateSwapRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidprice,uint64_t totalprice); -bool SetBidFillamounts(uint64_t &paid,uint64_t &remaining_price,uint64_t orig_nValue,uint64_t &received,uint64_t totalprice); -bool SetAskFillamounts(uint64_t &paid,uint64_t &remaining_price,uint64_t orig_nValue,uint64_t &received,uint64_t totalprice); -bool SetSwapFillamounts(uint64_t &paid,uint64_t &remaining_price,uint64_t orig_nValue,uint64_t &received,uint64_t totalprice); -uint64_t AssetValidateBuyvin(struct CCcontract_info *cp,Eval* eval,uint64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 refassetid); -uint64_t AssetValidateSellvin(struct CCcontract_info *cp,Eval* eval,uint64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 assetid); -bool AssetExactAmounts(struct CCcontract_info *cp,uint64_t &inputs,int32_t starti,uint64_t &outputs,Eval* eval,const CTransaction &tx,uint256 assetid); +CScript EncodeAssetOpRet(uint8_t funcid,uint256 assetid,uint256 assetid2,int64_t price,std::vector origpubkey); +uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint256 &assetid,uint256 &assetid2,int64_t &price,std::vector &origpubkey); +bool SetAssetOrigpubkey(std::vector &origpubkey,int64_t &price,const CTransaction &tx); +int64_t IsAssetvout(int64_t &price,std::vector &origpubkey,const CTransaction& tx,int32_t v,uint256 refassetid); +bool ValidateBidRemainder(int64_t remaining_price,int64_t remaining_nValue,int64_t orig_nValue,int64_t received_nValue,int64_t paidprice,int64_t totalprice); +bool ValidateAskRemainder(int64_t remaining_price,int64_t remaining_nValue,int64_t orig_nValue,int64_t received_nValue,int64_t paidprice,int64_t totalprice); +bool ValidateSwapRemainder(int64_t remaining_price,int64_t remaining_nValue,int64_t orig_nValue,int64_t received_nValue,int64_t paidprice,int64_t totalprice); +bool SetBidFillamounts(int64_t &paid,int64_t &remaining_price,int64_t orig_nValue,int64_t &received,int64_t totalprice); +bool SetAskFillamounts(int64_t &paid,int64_t &remaining_price,int64_t orig_nValue,int64_t &received,int64_t totalprice); +bool SetSwapFillamounts(int64_t &paid,int64_t &remaining_price,int64_t orig_nValue,int64_t &received,int64_t totalprice); +int64_t AssetValidateBuyvin(struct CCcontract_info *cp,Eval* eval,int64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 refassetid); +int64_t AssetValidateSellvin(struct CCcontract_info *cp,Eval* eval,int64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 assetid); +bool AssetExactAmounts(struct CCcontract_info *cp,int64_t &inputs,int32_t starti,int64_t &outputs,Eval* eval,const CTransaction &tx,uint256 assetid); // CCassetstx -uint64_t GetAssetBalance(CPubKey pk,uint256 tokenid); -uint64_t AddAssetInputs(CMutableTransaction &mtx,CPubKey pk,uint256 assetid,uint64_t total,int32_t maxinputs); +int64_t GetAssetBalance(CPubKey pk,uint256 tokenid); +int64_t AddAssetInputs(CMutableTransaction &mtx,CPubKey pk,uint256 assetid,int64_t total,int32_t maxinputs); UniValue AssetOrders(uint256 tokenid); -std::string CreateAsset(uint64_t txfee,int64_t assetsupply,std::string name,std::string description); -std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector destpubkey,int64_t total); -std::string CreateBuyOffer(uint64_t txfee,int64_t bidamount,uint256 assetid,int64_t pricetotal); -std::string CancelBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid); -std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,int64_t fillamount); -std::string CreateSell(uint64_t txfee,int64_t askamount,uint256 assetid,int64_t pricetotal); -std::string CreateSwap(uint64_t txfee,int64_t askamount,uint256 assetid,uint256 assetid2,int64_t pricetotal); -std::string CancelSell(uint64_t txfee,uint256 assetid,uint256 asktxid); -std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,int64_t fillamount); +std::string CreateAsset(int64_t txfee,int64_t assetsupply,std::string name,std::string description); +std::string AssetTransfer(int64_t txfee,uint256 assetid,std::vector destpubkey,int64_t total); +std::string CreateBuyOffer(int64_t txfee,int64_t bidamount,uint256 assetid,int64_t pricetotal); +std::string CancelBuyOffer(int64_t txfee,uint256 assetid,uint256 bidtxid); +std::string FillBuyOffer(int64_t txfee,uint256 assetid,uint256 bidtxid,int64_t fillamount); +std::string CreateSell(int64_t txfee,int64_t askamount,uint256 assetid,int64_t pricetotal); +std::string CreateSwap(int64_t txfee,int64_t askamount,uint256 assetid,uint256 assetid2,int64_t pricetotal); +std::string CancelSell(int64_t txfee,uint256 assetid,uint256 asktxid); +std::string FillSell(int64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,int64_t fillamount); #endif diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 9293603ff..f57e50525 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -38,9 +38,9 @@ We assume that the effective unit cost in the orderbook is valid and that that amount was paid and also that any remainder will be close enough in effective unit cost to not matter. At the edge cases, this will probably be not true and maybe some orders wont be practically fillable when reduced to fractional state. However, the original pubkey that created the offer can always reclaim it. */ -bool ValidateBidRemainder(uint64_t remaining_units,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidunits,uint64_t totalunits) +bool ValidateBidRemainder(int64_t remaining_units,int64_t remaining_nValue,int64_t orig_nValue,int64_t received_nValue,int64_t paidunits,int64_t totalunits) { - uint64_t unitprice,recvunitprice,newunitprice=0; + int64_t unitprice,recvunitprice,newunitprice=0; if ( orig_nValue == 0 || received_nValue == 0 || paidunits == 0 || totalunits == 0 ) { fprintf(stderr,"ValidateAssetRemainder: orig_nValue == %llu || received_nValue == %llu || paidunits == %llu || totalunits == %llu\n",(long long)orig_nValue,(long long)received_nValue,(long long)paidunits,(long long)totalunits); @@ -72,9 +72,9 @@ bool ValidateBidRemainder(uint64_t remaining_units,uint64_t remaining_nValue,uin return(true); } -bool SetBidFillamounts(uint64_t &received_nValue,uint64_t &remaining_units,uint64_t orig_nValue,uint64_t &paidunits,uint64_t totalunits) +bool SetBidFillamounts(int64_t &received_nValue,int64_t &remaining_units,int64_t orig_nValue,int64_t &paidunits,int64_t totalunits) { - uint64_t remaining_nValue,unitprice; double dprice; + int64_t remaining_nValue,unitprice; double dprice; if ( totalunits == 0 ) { received_nValue = remaining_units = paidunits = 0; @@ -99,9 +99,9 @@ bool SetBidFillamounts(uint64_t &received_nValue,uint64_t &remaining_units,uint6 } else return(false); } -bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue,uint64_t orig_assetoshis,uint64_t &paid_nValue,uint64_t total_nValue) +bool SetAskFillamounts(int64_t &received_assetoshis,int64_t &remaining_nValue,int64_t orig_assetoshis,int64_t &paid_nValue,int64_t total_nValue) { - uint64_t remaining_assetoshis; double dunitprice; + int64_t remaining_assetoshis; double dunitprice; if ( total_nValue == 0 ) { received_assetoshis = remaining_nValue = paid_nValue = 0; @@ -127,9 +127,9 @@ bool SetAskFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue, } else return(false); } -bool ValidateAskRemainder(uint64_t remaining_nValue,uint64_t remaining_assetoshis,uint64_t orig_assetoshis,uint64_t received_assetoshis,uint64_t paid_nValue,uint64_t total_nValue) +bool ValidateAskRemainder(int64_t remaining_nValue,int64_t remaining_assetoshis,int64_t orig_assetoshis,int64_t received_assetoshis,int64_t paid_nValue,int64_t total_nValue) { - uint64_t unitprice,recvunitprice,newunitprice=0; + int64_t unitprice,recvunitprice,newunitprice=0; fprintf(stderr,"%llu %llu %llu %llu %llu %llu\n",(long long)remaining_nValue,(long long)remaining_assetoshis,(long long)orig_assetoshis,(long long)received_assetoshis,(long long)paid_nValue,(long long)total_nValue); if ( orig_assetoshis == 0 || received_assetoshis == 0 || paid_nValue == 0 || total_nValue == 0 ) { @@ -162,9 +162,9 @@ bool ValidateAskRemainder(uint64_t remaining_nValue,uint64_t remaining_assetoshi return(true); } -bool SetSwapFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue,uint64_t orig_assetoshis,uint64_t &paid_nValue,uint64_t total_nValue) +bool SetSwapFillamounts(int64_t &received_assetoshis,int64_t &remaining_nValue,int64_t orig_assetoshis,int64_t &paid_nValue,int64_t total_nValue) { - uint64_t remaining_assetoshis; double dunitprice; + int64_t remaining_assetoshis; double dunitprice; if ( total_nValue == 0 ) { fprintf(stderr,"total_nValue.0 origsatoshis.%llu paid_nValue.%llu\n",(long long)orig_assetoshis,(long long)paid_nValue); @@ -191,9 +191,9 @@ bool SetSwapFillamounts(uint64_t &received_assetoshis,uint64_t &remaining_nValue } else return(false); } -bool ValidateSwapRemainder(uint64_t remaining_price,uint64_t remaining_nValue,uint64_t orig_nValue,uint64_t received_nValue,uint64_t paidunits,uint64_t totalunits) +bool ValidateSwapRemainder(int64_t remaining_price,int64_t remaining_nValue,int64_t orig_nValue,int64_t received_nValue,int64_t paidunits,int64_t totalunits) { - uint64_t unitprice,recvunitprice,newunitprice=0; + int64_t unitprice,recvunitprice,newunitprice=0; if ( orig_nValue == 0 || received_nValue == 0 || paidunits == 0 || totalunits == 0 ) { fprintf(stderr,"ValidateAssetRemainder: orig_nValue == %llu || received_nValue == %llu || paidunits == %llu || totalunits == %llu\n",(long long)orig_nValue,(long long)received_nValue,(long long)paidunits,(long long)totalunits); @@ -232,7 +232,7 @@ CScript EncodeAssetCreateOpRet(uint8_t funcid,std::vector origpubkey,st return(opret); } -CScript EncodeAssetOpRet(uint8_t funcid,uint256 assetid,uint256 assetid2,uint64_t price,std::vector origpubkey) +CScript EncodeAssetOpRet(uint8_t funcid,uint256 assetid,uint256 assetid2,int64_t price,std::vector origpubkey) { CScript opret; uint8_t evalcode = EVAL_ASSETS; assetid = revuint256(assetid); @@ -256,7 +256,7 @@ CScript EncodeAssetOpRet(uint8_t funcid,uint256 assetid,uint256 assetid2,uint64_ return(opret); } -uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint256 &assetid,uint256 &assetid2,uint64_t &price,std::vector &origpubkey) +uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint256 &assetid,uint256 &assetid2,int64_t &price,std::vector &origpubkey) { std::vector vopret; uint8_t funcid=0,*script,e,f; GetOpReturnData(scriptPubKey, vopret); @@ -305,7 +305,7 @@ uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint256 &assetid,uint256 &a return(funcid); } -bool SetAssetOrigpubkey(std::vector &origpubkey,uint64_t &price,const CTransaction &tx) +bool SetAssetOrigpubkey(std::vector &origpubkey,int64_t &price,const CTransaction &tx) { uint256 assetid,assetid2; if ( tx.vout.size() > 0 && DecodeAssetOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,assetid,assetid2,price,origpubkey) != 0 ) @@ -315,7 +315,7 @@ bool SetAssetOrigpubkey(std::vector &origpubkey,uint64_t &price,const C bool GetAssetorigaddrs(struct CCcontract_info *cp,char *CCaddr,char *destaddr,const CTransaction& tx) { - uint256 assetid,assetid2; uint64_t price,nValue=0; int32_t n; uint8_t funcid; std::vector origpubkey; CScript script; + uint256 assetid,assetid2; int64_t price,nValue=0; int32_t n; uint8_t funcid; std::vector origpubkey; CScript script; n = tx.vout.size(); if ( n == 0 || (funcid= DecodeAssetOpRet(tx.vout[n-1].scriptPubKey,assetid,assetid2,price,origpubkey)) == 0 ) return(false); @@ -324,9 +324,9 @@ bool GetAssetorigaddrs(struct CCcontract_info *cp,char *CCaddr,char *destaddr,co else return(false); } -uint64_t IsAssetvout(uint64_t &price,std::vector &origpubkey,const CTransaction& tx,int32_t v,uint256 refassetid) +int64_t IsAssetvout(int64_t &price,std::vector &origpubkey,const CTransaction& tx,int32_t v,uint256 refassetid) { - uint256 assetid,assetid2; uint64_t nValue=0; int32_t n; uint8_t funcid; + uint256 assetid,assetid2; int64_t nValue=0; int32_t n; uint8_t funcid; if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 ) // maybe check address too? { n = tx.vout.size(); @@ -363,7 +363,7 @@ uint64_t IsAssetvout(uint64_t &price,std::vector &origpubkey,const CTra return(0); } -uint64_t AssetValidateCCvin(struct CCcontract_info *cp,Eval* eval,char *CCaddr,char *origaddr,const CTransaction &tx,int32_t vini,CTransaction &vinTx) +int64_t AssetValidateCCvin(struct CCcontract_info *cp,Eval* eval,char *CCaddr,char *origaddr,const CTransaction &tx,int32_t vini,CTransaction &vinTx) { uint256 hashBlock; char destaddr[64]; origaddr[0] = destaddr[0] = CCaddr[0] = 0; @@ -394,9 +394,9 @@ uint64_t AssetValidateCCvin(struct CCcontract_info *cp,Eval* eval,char *CCaddr,c return(vinTx.vout[0].nValue); } -uint64_t AssetValidateBuyvin(struct CCcontract_info *cp,Eval* eval,uint64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 refassetid) +int64_t AssetValidateBuyvin(struct CCcontract_info *cp,Eval* eval,int64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 refassetid) { - CTransaction vinTx; uint64_t nValue; uint256 assetid,assetid2; uint8_t funcid; + CTransaction vinTx; int64_t nValue; uint256 assetid,assetid2; uint8_t funcid; CCaddr[0] = origaddr[0] = 0; if ( (nValue= AssetValidateCCvin(cp,eval,CCaddr,origaddr,tx,1,vinTx)) == 0 ) return(0); @@ -416,9 +416,9 @@ uint64_t AssetValidateBuyvin(struct CCcontract_info *cp,Eval* eval,uint64_t &tmp return(nValue); } -uint64_t AssetValidateSellvin(struct CCcontract_info *cp,Eval* eval,uint64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 assetid) +int64_t AssetValidateSellvin(struct CCcontract_info *cp,Eval* eval,int64_t &tmpprice,std::vector &tmporigpubkey,char *CCaddr,char *origaddr,const CTransaction &tx,uint256 assetid) { - CTransaction vinTx; uint64_t nValue,assetoshis; + CTransaction vinTx; int64_t nValue,assetoshis; fprintf(stderr,"AssetValidateSellvin\n"); if ( (nValue= AssetValidateCCvin(cp,eval,CCaddr,origaddr,tx,1,vinTx)) == 0 ) return(0); @@ -427,9 +427,9 @@ uint64_t AssetValidateSellvin(struct CCcontract_info *cp,Eval* eval,uint64_t &tm else return(assetoshis); } -bool AssetExactAmounts(struct CCcontract_info *cp,uint64_t &inputs,int32_t starti,uint64_t &outputs,Eval* eval,const CTransaction &tx,uint256 assetid) +bool AssetExactAmounts(struct CCcontract_info *cp,int64_t &inputs,int32_t starti,int64_t &outputs,Eval* eval,const CTransaction &tx,uint256 assetid) { - CTransaction vinTx; uint256 hashBlock; int32_t i,numvins,numvouts; uint64_t assetoshis; std::vector tmporigpubkey; uint64_t tmpprice; + CTransaction vinTx; uint256 hashBlock; int32_t i,numvins,numvouts; int64_t assetoshis; std::vector tmporigpubkey; int64_t tmpprice; numvins = tx.vin.size(); numvouts = tx.vout.size(); inputs = outputs = 0; diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 1e44cf2cd..833d0bd1d 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -15,9 +15,9 @@ #include "CCassets.h" -uint64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint256 assetid,uint64_t total,int32_t maxinputs) +int64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint256 assetid,int64_t total,int32_t maxinputs) { - char coinaddr[64]; uint64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t j,vout,n = 0; + char coinaddr[64]; int64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t j,vout,n = 0; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); @@ -47,7 +47,7 @@ uint64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPub return(totalinputs); } -uint64_t GetAssetBalance(CPubKey pk,uint256 tokenid) +int64_t GetAssetBalance(CPubKey pk,uint256 tokenid) { CMutableTransaction mtx; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_ASSETS); @@ -57,7 +57,7 @@ uint64_t GetAssetBalance(CPubKey pk,uint256 tokenid) UniValue AssetOrders(uint256 refassetid) { static uint256 zero; - uint64_t price; uint256 txid,hashBlock,assetid,assetid2; std::vector origpubkey; CTransaction vintx; UniValue result(UniValue::VARR); std::vector > unspentOutputs; uint8_t funcid; char numstr[32],funcidstr[16],origaddr[64],assetidstr[65]; struct CCcontract_info *cp,C; + int64_t price; uint256 txid,hashBlock,assetid,assetid2; std::vector origpubkey; CTransaction vintx; UniValue result(UniValue::VARR); std::vector > unspentOutputs; uint8_t funcid; char numstr[32],funcidstr[16],origaddr[64],assetidstr[65]; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_ASSETS); SetCCunspents(unspentOutputs,(char *)cp->unspendableCCaddr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) @@ -133,7 +133,7 @@ UniValue AssetOrders(uint256 refassetid) return(result); } -std::string CreateAsset(uint64_t txfee,int64_t assetsupply,std::string name,std::string description) +std::string CreateAsset(int64_t txfee,int64_t assetsupply,std::string name,std::string description) { CMutableTransaction mtx; CPubKey mypk; struct CCcontract_info *cp,C; if ( assetsupply < 0 ) @@ -159,9 +159,9 @@ std::string CreateAsset(uint64_t txfee,int64_t assetsupply,std::string name,std: return(0); } -std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector destpubkey,int64_t total) +std::string AssetTransfer(int64_t txfee,uint256 assetid,std::vector destpubkey,int64_t total) { - CMutableTransaction mtx; CPubKey mypk; uint64_t CCchange=0,inputs=0; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk; int64_t CCchange=0,inputs=0; struct CCcontract_info *cp,C; if ( total < 0 ) { fprintf(stderr,"negative total %lld\n",(long long)total); @@ -193,7 +193,7 @@ std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector de return(0); } -std::string CreateBuyOffer(uint64_t txfee,int64_t bidamount,uint256 assetid,int64_t pricetotal) +std::string CreateBuyOffer(int64_t txfee,int64_t bidamount,uint256 assetid,int64_t pricetotal) { CMutableTransaction mtx; CPubKey mypk; struct CCcontract_info *cp,C; if ( bidamount < 0 || pricetotal < 0 ) @@ -213,9 +213,9 @@ std::string CreateBuyOffer(uint64_t txfee,int64_t bidamount,uint256 assetid,int6 return(0); } -std::string CreateSell(uint64_t txfee,int64_t askamount,uint256 assetid,int64_t pricetotal) +std::string CreateSell(int64_t txfee,int64_t askamount,uint256 assetid,int64_t pricetotal) { - CMutableTransaction mtx; CPubKey mypk; uint64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk; int64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; if ( askamount < 0 || pricetotal < 0 ) { fprintf(stderr,"negative askamount %lld, askamount %lld\n",(long long)pricetotal,(long long)askamount); @@ -244,9 +244,9 @@ std::string CreateSell(uint64_t txfee,int64_t askamount,uint256 assetid,int64_t return(0); } -std::string CreateSwap(uint64_t txfee,int64_t askamount,uint256 assetid,uint256 assetid2,int64_t pricetotal) +std::string CreateSwap(int64_t txfee,int64_t askamount,uint256 assetid,uint256 assetid2,int64_t pricetotal) { - CMutableTransaction mtx; CPubKey mypk; uint64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk; int64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; if ( askamount < 0 || pricetotal < 0 ) { fprintf(stderr,"negative askamount %lld, askamount %lld\n",(long long)pricetotal,(long long)askamount); @@ -280,9 +280,9 @@ std::string CreateSwap(uint64_t txfee,int64_t askamount,uint256 assetid,uint256 return(0); } -std::string CancelBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid) +std::string CancelBuyOffer(int64_t txfee,uint256 assetid,uint256 bidtxid) { - CMutableTransaction mtx; CTransaction vintx; uint256 hashBlock; uint64_t bidamount; CPubKey mypk; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CTransaction vintx; uint256 hashBlock; int64_t bidamount; CPubKey mypk; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; @@ -300,9 +300,9 @@ std::string CancelBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid) return(0); } -std::string CancelSell(uint64_t txfee,uint256 assetid,uint256 asktxid) +std::string CancelSell(int64_t txfee,uint256 assetid,uint256 asktxid) { - CMutableTransaction mtx; CTransaction vintx; uint256 hashBlock; uint64_t askamount; CPubKey mypk; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CTransaction vintx; uint256 hashBlock; int64_t askamount; CPubKey mypk; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; @@ -320,9 +320,9 @@ std::string CancelSell(uint64_t txfee,uint256 assetid,uint256 asktxid) return(0); } -std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,int64_t fillamount) +std::string FillBuyOffer(int64_t txfee,uint256 assetid,uint256 bidtxid,int64_t fillamount) { - CTransaction vintx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t bidvout=0; uint64_t origprice,bidamount,paid_amount,remaining_required,inputs,CCchange=0; struct CCcontract_info *cp,C; + CTransaction vintx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t bidvout=0; int64_t origprice,bidamount,paid_amount,remaining_required,inputs,CCchange=0; struct CCcontract_info *cp,C; if ( fillamount < 0 ) { fprintf(stderr,"negative fillamount %lld\n",(long long)fillamount); @@ -359,9 +359,9 @@ std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,int64_t return("no normal coins left"); } -std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,uint64_t fillunits) +std::string FillSell(int64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,int64_t fillunits) { - CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; double dprice; int32_t askvout=0; uint64_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 origpubkey; double dprice; 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 ) { fprintf(stderr,"negative fillunits %lld\n",(long long)fillunits); diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 86037235a..4a9804508 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -132,7 +132,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { static uint256 zero; - CTxDestination address; CTransaction vinTx,createTx; uint256 hashBlock,assetid,assetid2; int32_t i,starti,numvins,numvouts,preventCCvins,preventCCvouts; uint64_t remaining_price,nValue,assetoshis,outputs,inputs,tmpprice,totalunits,ignore; std::vector origpubkey,tmporigpubkey,ignorepubkey; uint8_t funcid; char destaddr[64],origaddr[64],CCaddr[64]; + CTxDestination address; CTransaction vinTx,createTx; uint256 hashBlock,assetid,assetid2; int32_t i,starti,numvins,numvouts,preventCCvins,preventCCvouts; int64_t remaining_price,nValue,assetoshis,outputs,inputs,tmpprice,totalunits,ignore; std::vector origpubkey,tmporigpubkey,ignorepubkey; uint8_t funcid; char destaddr[64],origaddr[64],CCaddr[64]; numvins = tx.vin.size(); numvouts = tx.vout.size(); outputs = inputs = 0; From 636517f866113491388e30fd0bf3e8ea24defc29 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 00:43:31 -1100 Subject: [PATCH 097/204] Fix oops --- src/cc/CCassetstx.cpp | 3 +++ src/cc/assets.cpp | 4 ++-- src/cc/rewards.cpp | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 833d0bd1d..34da82b70 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -15,6 +15,9 @@ #include "CCassets.h" +// need allassets +// find asset + int64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint256 assetid,int64_t total,int32_t maxinputs) { char coinaddr[64]; int64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t j,vout,n = 0; diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 4a9804508..8c8a3328a 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -140,9 +140,9 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx if ( (funcid= DecodeAssetOpRet(tx.vout[numvouts-1].scriptPubKey,assetid,assetid2,remaining_price,origpubkey)) == 0 ) return eval->Invalid("Invalid opreturn payload"); fprintf(stderr,"AssetValidate (%c)\n",funcid); - if ( cp->evalcode != 'o' && cp->evalcode != 'x' && eval->GetTxUnconfirmed(assetid,createTx,hashBlock) == 0 ) + if ( funcid != 'o' && funcid != 'x' && eval->GetTxUnconfirmed(assetid,createTx,hashBlock) == 0 ) return eval->Invalid("cant find asset create txid"); - else if ( cp->evalcode != 'o' && cp->evalcode != 'x' && assetid2 != zero && eval->GetTxUnconfirmed(assetid2,createTx,hashBlock) == 0 ) + else if ( funcid != 'o' && funcid != 'x' && assetid2 != zero && eval->GetTxUnconfirmed(assetid2,createTx,hashBlock) == 0 ) return eval->Invalid("cant find asset2 create txid"); else if ( IsCCInput(tx.vin[0].scriptSig) != 0 ) return eval->Invalid("illegal asset vin0"); diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 8093f57b4..006085ed3 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -271,7 +271,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 if ( (inputs= AddRewardsInputs(cp,mtx,mypk,reward+amount+txfee,30)) > 0 ) { if ( inputs >= (amount + reward + 2*txfee) ) - CCchange = (inputs - amount - reward - txfee); + CCchange = (inputs - (amount + reward + txfee)); if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk)); mtx.vout.push_back(CTxOut(amount+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); From e9c43e9e4030932ea974952bb5b62cd8ad499e50 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 00:46:54 -1100 Subject: [PATCH 098/204] Prevent bid for nonexistent assetid --- src/cc/CCassetstx.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 34da82b70..d3a887c06 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -198,12 +198,17 @@ std::string AssetTransfer(int64_t txfee,uint256 assetid,std::vector des std::string CreateBuyOffer(int64_t txfee,int64_t bidamount,uint256 assetid,int64_t pricetotal) { - CMutableTransaction mtx; CPubKey mypk; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk; struct CCcontract_info *cp,C; uint256 hashBlock; CTransaction vintx; if ( bidamount < 0 || pricetotal < 0 ) { fprintf(stderr,"negative bidamount %lld, pricetotal %lld\n",(long long)bidamount,(long long)pricetotal); return(0); } + if ( GetTransaction(assetid,vintx,hashBlock,false) == 0 ) + { + fprintf(stderr,"cant find assetid\n"); + return(0); + } cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; From 0de5ed27ed2edfd365ba1a560bec5605737e4830 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 00:55:55 -1100 Subject: [PATCH 099/204] Error check bidding for assetid that is txid but not asset creation --- src/cc/CCassets.h | 1 + src/cc/CCassetsCore.cpp | 13 +++++++++++++ src/cc/CCassetstx.cpp | 7 ++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/cc/CCassets.h b/src/cc/CCassets.h index 0b566bdd2..c072bde89 100644 --- a/src/cc/CCassets.h +++ b/src/cc/CCassets.h @@ -32,6 +32,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx //CTxOut MakeAssetsVout(CAmount nValue,CPubKey pk); CScript EncodeAssetCreateOpRet(uint8_t funcid,std::vector origpubkey,std::string name,std::string description); CScript EncodeAssetOpRet(uint8_t funcid,uint256 assetid,uint256 assetid2,int64_t price,std::vector origpubkey); +bool DecodeAssetCreateOpRet(std::vector &origpubkey,std::string &name,std::string &description); uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint256 &assetid,uint256 &assetid2,int64_t &price,std::vector &origpubkey); bool SetAssetOrigpubkey(std::vector &origpubkey,int64_t &price,const CTransaction &tx); int64_t IsAssetvout(int64_t &price,std::vector &origpubkey,const CTransaction& tx,int32_t v,uint256 refassetid); diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index f57e50525..53a75c58c 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -256,6 +256,19 @@ CScript EncodeAssetOpRet(uint8_t funcid,uint256 assetid,uint256 assetid2,int64_t return(opret); } +bool DecodeAssetCreateOpRet(const CScript &scriptPubKey,std::vector &origpubkey,std::string &name,std::string &description) +{ + uint8_t evalcode,funcid,*script; + GetOpReturnData(scriptPubKey, vopret); + script = (uint8_t *)vopret.data(); + if ( script != 0 && vopret.size() > 2 && script[0] == EVAL_ASSETS && script[1] == 'c' ) + { + if ( E_UNMARSHAL(vopret,ss >> evalcode; ss >> funcid; ss >> origpubkey; ss >> name; ss >> description) != 0 ) + return(true); + } + return(0); +} + uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint256 &assetid,uint256 &assetid2,int64_t &price,std::vector &origpubkey) { std::vector vopret; uint8_t funcid=0,*script,e,f; diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index d3a887c06..d5a0b4999 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -198,7 +198,7 @@ std::string AssetTransfer(int64_t txfee,uint256 assetid,std::vector des std::string CreateBuyOffer(int64_t txfee,int64_t bidamount,uint256 assetid,int64_t pricetotal) { - CMutableTransaction mtx; CPubKey mypk; struct CCcontract_info *cp,C; uint256 hashBlock; CTransaction vintx; + CMutableTransaction mtx; CPubKey mypk; struct CCcontract_info *cp,C; uint256 hashBlock; CTransaction vintx; std::vector origpubkey; std::string name,description; if ( bidamount < 0 || pricetotal < 0 ) { fprintf(stderr,"negative bidamount %lld, pricetotal %lld\n",(long long)bidamount,(long long)pricetotal); @@ -209,6 +209,11 @@ std::string CreateBuyOffer(int64_t txfee,int64_t bidamount,uint256 assetid,int64 fprintf(stderr,"cant find assetid\n"); return(0); } + if ( vintx.vout.size() > 0 && DecodeAssetCreateOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,origpubkey,name,description) == 0 ) + { + fprintf(stderr,"assetid isnt assetcreation txid\n"); + return(0); + } cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; From c5744a33c3cbdf1e43cbaf668c991f6047937dc9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 00:56:58 -1100 Subject: [PATCH 100/204] Fix --- src/cc/CCassetsCore.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index 53a75c58c..ae0cf2ece 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -258,7 +258,7 @@ CScript EncodeAssetOpRet(uint8_t funcid,uint256 assetid,uint256 assetid2,int64_t bool DecodeAssetCreateOpRet(const CScript &scriptPubKey,std::vector &origpubkey,std::string &name,std::string &description) { - uint8_t evalcode,funcid,*script; + std::vector vopret; uint8_t evalcode,funcid,*script; GetOpReturnData(scriptPubKey, vopret); script = (uint8_t *)vopret.data(); if ( script != 0 && vopret.size() > 2 && script[0] == EVAL_ASSETS && script[1] == 'c' ) From 09c32513d17a5959d832dabf099d3f998300164f Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 00:58:23 -1100 Subject: [PATCH 101/204] Fix --- src/cc/CCassets.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassets.h b/src/cc/CCassets.h index c072bde89..77e3e6313 100644 --- a/src/cc/CCassets.h +++ b/src/cc/CCassets.h @@ -32,7 +32,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx //CTxOut MakeAssetsVout(CAmount nValue,CPubKey pk); CScript EncodeAssetCreateOpRet(uint8_t funcid,std::vector origpubkey,std::string name,std::string description); CScript EncodeAssetOpRet(uint8_t funcid,uint256 assetid,uint256 assetid2,int64_t price,std::vector origpubkey); -bool DecodeAssetCreateOpRet(std::vector &origpubkey,std::string &name,std::string &description); +bool DecodeAssetCreateOpRet(const CScript &scriptPubKey,std::vector &origpubkey,std::string &name,std::string &description); uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint256 &assetid,uint256 &assetid2,int64_t &price,std::vector &origpubkey); bool SetAssetOrigpubkey(std::vector &origpubkey,int64_t &price,const CTransaction &tx); int64_t IsAssetvout(int64_t &price,std::vector &origpubkey,const CTransaction& tx,int32_t v,uint256 refassetid); From c66eb36d51a64b511b75d2eda2eaf4820595ca01 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 01:12:00 -1100 Subject: [PATCH 102/204] Add pause if scriptcheckqueue isn't idle --- src/cc/CCassets.h | 2 ++ src/cc/CCassetstx.cpp | 8 ++++++++ src/main.cpp | 8 ++++++++ src/rpcserver.cpp | 2 ++ src/rpcserver.h | 2 ++ src/wallet/rpcwallet.cpp | 21 +++++++++++++++++++++ 6 files changed, 43 insertions(+) diff --git a/src/cc/CCassets.h b/src/cc/CCassets.h index 77e3e6313..9a0f2a0ea 100644 --- a/src/cc/CCassets.h +++ b/src/cc/CCassets.h @@ -50,6 +50,8 @@ bool AssetExactAmounts(struct CCcontract_info *cp,int64_t &inputs,int32_t starti int64_t GetAssetBalance(CPubKey pk,uint256 tokenid); int64_t AddAssetInputs(CMutableTransaction &mtx,CPubKey pk,uint256 assetid,int64_t total,int32_t maxinputs); UniValue AssetOrders(uint256 tokenid); +UniValue AssetInfo(uint256 tokenid); +UniValue AssetList(); std::string CreateAsset(int64_t txfee,int64_t assetsupply,std::string name,std::string description); std::string AssetTransfer(int64_t txfee,uint256 assetid,std::vector destpubkey,int64_t total); std::string CreateBuyOffer(int64_t txfee,int64_t bidamount,uint256 assetid,int64_t pricetotal); diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index d5a0b4999..26a2682da 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -57,6 +57,14 @@ int64_t GetAssetBalance(CPubKey pk,uint256 tokenid) return(AddAssetInputs(cp,mtx,pk,tokenid,0,0)); } +UniValue AssetList(uint256 refassetid) +{ +} + +UniValue AssetInfo() +{ +} + UniValue AssetOrders(uint256 refassetid) { static uint256 zero; diff --git a/src/main.cpp b/src/main.cpp index ca64a9130..66d7f89fa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2825,6 +2825,14 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin CBlockUndo blockundo; + if ( ASSETCHAINS_CC != 0 ) + { + if ( pqueue->IsIdle() == 0 ) + { + fprintf(stderr,"scriptcheckqueue isnt idle\n"); + sleep(1); + } + } CCheckQueueControl control(fExpensiveChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL); int64_t nTimeStart = GetTimeMicros(); diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index c31204d29..1e665cea5 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -371,6 +371,8 @@ static const CRPCCommand vRPCCommands[] = { "dice", "diceaddress", &diceaddress, true }, /* tokens */ + { "tokens", "tokeninfo", &tokeninfo, true }, + { "tokens", "tokenlist", &tokenlist, true }, { "tokens", "tokenorders", &tokenorders, true }, { "tokens", "tokenaddress", &tokenaddress, true }, { "tokens", "tokenbalance", &tokenbalance, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index 17b6bf812..2b82e6e64 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -208,6 +208,8 @@ extern UniValue submitblock(const UniValue& params, bool fHelp); extern UniValue estimatefee(const UniValue& params, bool fHelp); extern UniValue estimatepriority(const UniValue& params, bool fHelp); extern UniValue coinsupply(const UniValue& params, bool fHelp); +extern UniValue tokeninfo(const UniValue& params, bool fHelp); +extern UniValue tokenlist(const UniValue& params, bool fHelp); extern UniValue tokenorders(const UniValue& params, bool fHelp); extern UniValue tokenbalance(const UniValue& params, bool fHelp); extern UniValue tokenaddress(const UniValue& params, bool fHelp); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index f2811260b..e06e4292e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5122,6 +5122,27 @@ UniValue dicebet(const UniValue& params, bool fHelp) return(result); } +UniValue tokenlist(const UniValue& params, bool fHelp) +{ + uint256 tokenid; + if ( fHelp || params.size() > 0 ) + throw runtime_error("tokenlist\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"); + return(AssetList()); +} + +UniValue tokeninfo(const UniValue& params, bool fHelp) +{ + uint256 tokenid; + if ( fHelp || params.size() != 1 ) + throw runtime_error("tokeninfo tokenid\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"); + tokenid = Parseuint256((char *)params[0].get_str().c_str()); + return(AssetInfo(tokenid)); +} + UniValue tokenorders(const UniValue& params, bool fHelp) { uint256 tokenid; From adb65e1c9657ac127c66c255271d9fdfad7ca357 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 01:13:27 -1100 Subject: [PATCH 103/204] Fix --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 66d7f89fa..e3441bb77 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2827,7 +2827,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin if ( ASSETCHAINS_CC != 0 ) { - if ( pqueue->IsIdle() == 0 ) + if ( scriptcheckqueue->IsIdle() == 0 ) { fprintf(stderr,"scriptcheckqueue isnt idle\n"); sleep(1); From 163b0aa78b23dea3234a6208a57348a479a77e61 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 01:14:11 -1100 Subject: [PATCH 104/204] -> -> . --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index e3441bb77..5371bed51 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2827,7 +2827,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin if ( ASSETCHAINS_CC != 0 ) { - if ( scriptcheckqueue->IsIdle() == 0 ) + if ( scriptcheckqueue.IsIdle() == 0 ) { fprintf(stderr,"scriptcheckqueue isnt idle\n"); sleep(1); From 5842e917f37446228cd11a076ca45af64f17963f Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 01:17:12 -1100 Subject: [PATCH 105/204] Fix --- src/cc/CCassetstx.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 26a2682da..646c8f66a 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -57,11 +57,11 @@ int64_t GetAssetBalance(CPubKey pk,uint256 tokenid) return(AddAssetInputs(cp,mtx,pk,tokenid,0,0)); } -UniValue AssetList(uint256 refassetid) +UniValue AssetInfo(uint256 refassetid) { } -UniValue AssetInfo() +UniValue AssetList() { } From 1cdfa0b54b1c9996a4e2c73899ef38c0bd1ef065 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 01:25:50 -1100 Subject: [PATCH 106/204] Test --- src/cc/CCassetstx.cpp | 25 ++++++++++++++++++++++++- src/cc/CCinclude.h | 1 + src/cc/CCutils.cpp | 14 +++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 646c8f66a..2dc9790b7 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -57,12 +57,35 @@ int64_t GetAssetBalance(CPubKey pk,uint256 tokenid) return(AddAssetInputs(cp,mtx,pk,tokenid,0,0)); } -UniValue AssetInfo(uint256 refassetid) +UniValue AssetInfo(uint256 assetid) { + UniValue result(UniValue::VARR); uint256 hashBlock; CTransaction vintx; std::vector origpubkey; std::string name,description; char str[67],numstr[65]; + if ( GetTransaction(assetid,vintx,hashBlock,false) == 0 ) + { + fprintf(stderr,"cant find assetid\n"); + result.push_back(Pair("error","cant find assetid")); + return(0); + } + if ( vintx.vout.size() > 0 && DecodeAssetCreateOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,origpubkey,name,description) == 0 ) + { + fprintf(stderr,"assetid isnt assetcreation txid\n"); + result.push_back(Pair("error","assetid isnt assetcreation txid")); + } + result.push_back(Pair("result","success")); + result.push_back(Pair("tokenid",uint256_str(str,assetid))); + result.push_back(Pair("owner",pubkey33_str(str,origpubkey.data()))); + result.push_back(Pair("name",name)); + sprintf(numstr,"%.8f",(double)vintx.vout[0].nValue/COIN); + result.push_back(Pair("supply",numstr)); + result.push_back(Pair("description",description)); + result.push_back(Pair("result","success")); + return(result); } UniValue AssetList() { + UniValue result(UniValue::VARR); + return(result); } UniValue AssetOrders(uint256 refassetid) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index da1cf252e..c0bd56c64 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -60,6 +60,7 @@ int32_t unstringbits(char *buf,uint64_t bits); uint64_t stringbits(char *str); uint256 revuint256(uint256 txid); char *uint256_str(char *dest,uint256 txid); +char *pubkey33_str(char *dest,uint8_t *pubkey33); uint256 Parseuint256(char *hexstr); CPubKey pubkey2pk(std::vector pubkey); bool GetCCaddress(struct CCcontract_info *cp,char *destaddr,CPubKey pk); diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index d9672c61d..039f5cbd9 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -92,7 +92,19 @@ char *uint256_str(char *dest,uint256 txid) int32_t i,j=0; for (i=31; i>=0; i--) sprintf(&dest[j++ * 2],"%02x",((uint8_t *)&txid)[i]); - return(dest); + dest[64] = 0; + return(dest); +} + +char *pubkey33_str(char *dest,uint8_t *pubkey33) +{ + int32_t i; + if ( pubkey33 != 0 ) + { + for (i=0; i<33; i++) + sprintf(&dest[i * 2],"%02x",pubkey33[i]); + } else dest[0] = 0; + return(dest); } uint256 Parseuint256(char *hexstr) From bc08ba76f72bbb7f5e11449b77ddaa7f7eacb5b6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 01:30:09 -1100 Subject: [PATCH 107/204] VOBJ --- src/cc/CCassetstx.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 2dc9790b7..8a90fb037 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -59,7 +59,7 @@ int64_t GetAssetBalance(CPubKey pk,uint256 tokenid) UniValue AssetInfo(uint256 assetid) { - UniValue result(UniValue::VARR); uint256 hashBlock; CTransaction vintx; std::vector origpubkey; std::string name,description; char str[67],numstr[65]; + UniValue result(UniValue::VOBJ); uint256 hashBlock; CTransaction vintx; std::vector origpubkey; std::string name,description; char str[67],numstr[65]; if ( GetTransaction(assetid,vintx,hashBlock,false) == 0 ) { fprintf(stderr,"cant find assetid\n"); @@ -84,7 +84,7 @@ UniValue AssetInfo(uint256 assetid) UniValue AssetList() { - UniValue result(UniValue::VARR); + UniValue result(UniValue::VOBJ); return(result); } From e7066211c6ed362aa659864ffbac62f8debbe19a Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 01:31:30 -1100 Subject: [PATCH 108/204] Tokeninfo rpc --- src/cc/CCassetstx.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 8a90fb037..3700f4db7 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -78,7 +78,6 @@ UniValue AssetInfo(uint256 assetid) sprintf(numstr,"%.8f",(double)vintx.vout[0].nValue/COIN); result.push_back(Pair("supply",numstr)); result.push_back(Pair("description",description)); - result.push_back(Pair("result","success")); return(result); } From 5de1d1aab193ca877c38e5ed64d8c8a2d01d07ca Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 01:41:30 -1100 Subject: [PATCH 109/204] Asset list --- src/cc/CCassetstx.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 3700f4db7..4f3682855 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -83,7 +83,20 @@ UniValue AssetInfo(uint256 assetid) UniValue AssetList() { - UniValue result(UniValue::VOBJ); + UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; uint256 txid; CTransaction vintx; std::vector origpubkey; std::string name,description; char str[65]; + cp = CCinit(&C,EVAL_ASSETS); + SetCCtxids(addressIndex,cp->normaladdr); + for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) + { + txid = it->first.txhash; + if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) + { + if ( vintx.vout.size() > 0 && DecodeAssetCreateOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,origpubkey,name,description) != 0 ) + { + result.push_back(uint256_str(str,txid)); + } + } + } return(result); } From 33d124c1d6da8428db3ff968a92666d1aaab9500 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 01:42:44 -1100 Subject: [PATCH 110/204] Test --- src/cc/CCassetstx.cpp | 2 +- src/cc/CCinclude.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 4f3682855..fe3d42eb9 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -83,7 +83,7 @@ UniValue AssetInfo(uint256 assetid) UniValue AssetList() { - UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; uint256 txid; CTransaction vintx; std::vector origpubkey; std::string name,description; char str[65]; + UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; uint256 txid,hashBlock; CTransaction vintx; std::vector origpubkey; std::string name,description; char str[65]; cp = CCinit(&C,EVAL_ASSETS); SetCCtxids(addressIndex,cp->normaladdr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index c0bd56c64..fe1ba7a6c 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -33,7 +33,7 @@ struct CCcontract_info { uint256 prevtxid; - char unspendableCCaddr[64],CChexstr[72]; + char unspendableCCaddr[64],CChexstr[72],normaladdr[64]; uint8_t CCpriv[32]; bool (*validate)(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); bool (*ismyvin)(CScript const& scriptSig); From 075a1114eb11d73b23c50fe7f85a2997aa38401e Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 01:48:25 -1100 Subject: [PATCH 111/204] strcpy(cp->normaladdr,AssetsNormaladdr); --- src/cc/CCassetstx.cpp | 2 +- src/cc/CCcustom.cpp | 28 +++++++++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index fe3d42eb9..2d66f1b1d 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -86,7 +86,7 @@ UniValue AssetList() UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; uint256 txid,hashBlock; CTransaction vintx; std::vector origpubkey; std::string name,description; char str[65]; cp = CCinit(&C,EVAL_ASSETS); SetCCtxids(addressIndex,cp->normaladdr); - for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) + for (std::vector >::const_iterator it=addressIndex.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index db891c583..3e6b91751 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -46,7 +46,8 @@ // Assets, aka Tokens #define FUNCNAME IsAssetsInput #define EVALCODE EVAL_ASSETS -const char *AssetsCCaddr = "RGKRjeTBw4LYFotSDLT6RWzMHbhXri6BG6"; //"RFYE2yL3KknWdHK6uNhvWacYsCUtwzjY3u"; +const char *AssetsCCaddr = "RGKRjeTBw4LYFotSDLT6RWzMHbhXri6BG6"; +const char *AssetsNormaladdr = "RFYE2yL3KknWdHK6uNhvWacYsCUtwzjY3u"; char AssetsCChexstr[67] = { "02adf84e0e075cf90868bd4e3d34a03420e034719649c41f371fc70d8e33aa2702" }; uint8_t AssetsCCpriv[32] = { 0x9b, 0x17, 0x66, 0xe5, 0x82, 0x66, 0xac, 0xb6, 0xba, 0x43, 0x83, 0x74, 0xf7, 0x63, 0x11, 0x3b, 0xf0, 0xf3, 0x50, 0x6f, 0xd9, 0x6b, 0x67, 0x85, 0xf9, 0x7a, 0xf0, 0x54, 0x4d, 0xb1, 0x30, 0x77 }; @@ -57,7 +58,8 @@ uint8_t AssetsCCpriv[32] = { 0x9b, 0x17, 0x66, 0xe5, 0x82, 0x66, 0xac, 0xb6, 0xb // Faucet #define FUNCNAME IsFaucetInput #define EVALCODE EVAL_FAUCET -const char *FaucetCCaddr = "R9zHrofhRbub7ER77B7NrVch3A63R39GuC"; //"RKQV4oYs4rvxAWx1J43VnT73rSTVtUeckk"; +const char *FaucetCCaddr = "R9zHrofhRbub7ER77B7NrVch3A63R39GuC"; +const char *FaucetNormaladdr = "RKQV4oYs4rvxAWx1J43VnT73rSTVtUeckk"; char FaucetCChexstr[67] = { "03682b255c40d0cde8faee381a1a50bbb89980ff24539cb8518e294d3a63cefe12" }; uint8_t FaucetCCpriv[32] = { 0xd4, 0x4f, 0xf2, 0x31, 0x71, 0x7d, 0x28, 0x02, 0x4b, 0xc7, 0xdd, 0x71, 0xa0, 0x39, 0xc4, 0xbe, 0x1a, 0xfe, 0xeb, 0xc2, 0x46, 0xda, 0x76, 0xf8, 0x07, 0x53, 0x3d, 0x96, 0xb4, 0xca, 0xa0, 0xe9 }; @@ -68,7 +70,8 @@ uint8_t FaucetCCpriv[32] = { 0xd4, 0x4f, 0xf2, 0x31, 0x71, 0x7d, 0x28, 0x02, 0x4 // Rewards #define FUNCNAME IsRewardsInput #define EVALCODE EVAL_REWARDS -const char *RewardsCCaddr = "RJCqA4jQTFEZ841dZgxko8aYgUU3FRNGNm"; //"RYRJGMAYEfLCZ6ZddbpxPiUZ1sens8vPYK"; +const char *RewardsCCaddr = "RJCqA4jQTFEZ841dZgxko8aYgUU3FRNGNm"; +const char *RewardsNormaladdr = "RYRJGMAYEfLCZ6ZddbpxPiUZ1sens8vPYK"; char RewardsCChexstr[67] = { "026f00fdc2f1ed0006d66e2ca1787633590581c2fc90e7cb7b01a6c1131b40e94d" }; uint8_t RewardsCCpriv[32] = { 0x9f, 0x0c, 0x57, 0xdc, 0x6f, 0x78, 0xae, 0xb0, 0xc7, 0x62, 0x9e, 0x7d, 0x2b, 0x90, 0x6b, 0xbd, 0x40, 0x78, 0x19, 0x5b, 0x3c, 0xb8, 0x82, 0x2d, 0x29, 0x84, 0x72, 0x7a, 0x59, 0x5a, 0x4b, 0x69 }; #include "CCcustom.inc" @@ -78,7 +81,8 @@ uint8_t RewardsCCpriv[32] = { 0x9f, 0x0c, 0x57, 0xdc, 0x6f, 0x78, 0xae, 0xb0, 0x // Dice #define FUNCNAME IsDiceInput #define EVALCODE EVAL_DICE -const char *DiceCCaddr = "REabWB7KjFN5C3LFMZ5odExHPenYzHLtVw"; //"RLEe8f7Eg3TDuXii9BmNiiiaVGraHUt25c"; +const char *DiceCCaddr = "REabWB7KjFN5C3LFMZ5odExHPenYzHLtVw"; +const char *DiceNormaladdr = "RLEe8f7Eg3TDuXii9BmNiiiaVGraHUt25c"; char DiceCChexstr[67] = { "039d966927cfdadab3ee6c56da63c21f17ea753dde4b3dfd41487103e24b27e94e" }; uint8_t DiceCCpriv[32] = { 0x0e, 0xe8, 0xf5, 0xb4, 0x3d, 0x25, 0xcc, 0x35, 0xd1, 0xf1, 0x2f, 0x04, 0x5f, 0x01, 0x26, 0xb8, 0xd1, 0xac, 0x3a, 0x5a, 0xea, 0xe0, 0x25, 0xa2, 0x8f, 0x2a, 0x8e, 0x0e, 0xf9, 0x34, 0xfa, 0x77 }; #include "CCcustom.inc" @@ -88,7 +92,8 @@ uint8_t DiceCCpriv[32] = { 0x0e, 0xe8, 0xf5, 0xb4, 0x3d, 0x25, 0xcc, 0x35, 0xd1, // Lotto #define FUNCNAME IsLottoInput #define EVALCODE EVAL_LOTTO -const char *LottoCCaddr = "RNXZxgyWSAE6XS3qGnTaf5dVNCxnYzhPrg"; //"RLW6hhRqBZZMBndnyPv29Yg3krh6iBYCyg"; +const char *LottoCCaddr = "RNXZxgyWSAE6XS3qGnTaf5dVNCxnYzhPrg"; +const char *LottoNormaladdr = "RLW6hhRqBZZMBndnyPv29Yg3krh6iBYCyg"; char LottoCChexstr[67] = { "03f72d2c4db440df1e706502b09ca5fec73ffe954ea1883e4049e98da68690d98f" }; uint8_t LottoCCpriv[32] = { 0xb4, 0xac, 0xc2, 0xd9, 0x67, 0x34, 0xd7, 0x58, 0x80, 0x4e, 0x25, 0x55, 0xc0, 0x50, 0x66, 0x84, 0xbb, 0xa2, 0xe7, 0xc0, 0x39, 0x17, 0xb4, 0xc5, 0x07, 0xb7, 0x3f, 0xca, 0x07, 0xb0, 0x9a, 0xeb }; #include "CCcustom.inc" @@ -98,7 +103,8 @@ uint8_t LottoCCpriv[32] = { 0xb4, 0xac, 0xc2, 0xd9, 0x67, 0x34, 0xd7, 0x58, 0x80 // Ponzi #define FUNCNAME IsPonziInput #define EVALCODE EVAL_PONZI -const char *PonziCCaddr = "RUKTbLBeKgHkm3Ss4hKZP3ikuLW1xx7B2x"; //"RWSHRbxnJYLvDjpcQ2i8MekgP6h2ctTKaj"; +const char *PonziCCaddr = "RUKTbLBeKgHkm3Ss4hKZP3ikuLW1xx7B2x"; +const char *PonziNormaladdr = "RWSHRbxnJYLvDjpcQ2i8MekgP6h2ctTKaj"; char PonziCChexstr[67] = { "039b52d294b413b07f3643c1a28c5467901a76562d8b39a785910ae0a0f3043810" }; uint8_t PonziCCpriv[32] = { 0x11, 0xe1, 0xea, 0x3e, 0xdb, 0x36, 0xf0, 0xa8, 0xc6, 0x34, 0xe1, 0x21, 0xb8, 0x02, 0xb9, 0x4b, 0x12, 0x37, 0x8f, 0xa0, 0x86, 0x23, 0x50, 0xb2, 0x5f, 0xe4, 0xe7, 0x36, 0x0f, 0xda, 0xae, 0xfc }; #include "CCcustom.inc" @@ -108,7 +114,8 @@ uint8_t PonziCCpriv[32] = { 0x11, 0xe1, 0xea, 0x3e, 0xdb, 0x36, 0xf0, 0xa8, 0xc6 // Auction #define FUNCNAME IsAuctionInput #define EVALCODE EVAL_AUCTION -const char *AuctionCCaddr = "RL4YPX7JYG3FnvoPqWF2pn3nQknH5NWEwx"; //"RFtVDNmdTZBTNZdmFRbfBgJ6LitgTghikL"; +const char *AuctionCCaddr = "RL4YPX7JYG3FnvoPqWF2pn3nQknH5NWEwx"; +const char *AuctionNormaladdr = "RFtVDNmdTZBTNZdmFRbfBgJ6LitgTghikL"; char AuctionCChexstr[67] = { "037eefe050c14cb60ae65d5b2f69eaa1c9006826d729bc0957bdc3024e3ca1dbe6" }; uint8_t AuctionCCpriv[32] = { 0x8c, 0x1b, 0xb7, 0x8c, 0x02, 0xa3, 0x9d, 0x21, 0x28, 0x59, 0xf5, 0xea, 0xda, 0xec, 0x0d, 0x11, 0xcd, 0x38, 0x47, 0xac, 0x0b, 0x6f, 0x19, 0xc0, 0x24, 0x36, 0xbf, 0x1c, 0x0a, 0x06, 0x31, 0xfb }; #include "CCcustom.inc" @@ -122,6 +129,7 @@ struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode) { case EVAL_ASSETS: strcpy(cp->unspendableCCaddr,AssetsCCaddr); + strcpy(cp->normaladdr,AssetsNormaladdr); strcpy(cp->CChexstr,AssetsCChexstr); memcpy(cp->CCpriv,AssetsCCpriv,32); cp->validate = AssetsValidate; @@ -129,6 +137,7 @@ struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode) break; case EVAL_FAUCET: strcpy(cp->unspendableCCaddr,FaucetCCaddr); + strcpy(cp->normaladdr,FaucetNormaladdr); strcpy(cp->CChexstr,FaucetCChexstr); memcpy(cp->CCpriv,FaucetCCpriv,32); cp->validate = FaucetValidate; @@ -136,6 +145,7 @@ struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode) break; case EVAL_REWARDS: strcpy(cp->unspendableCCaddr,RewardsCCaddr); + strcpy(cp->normaladdr,RewardsNormaladdr); strcpy(cp->CChexstr,RewardsCChexstr); memcpy(cp->CCpriv,RewardsCCpriv,32); cp->validate = RewardsValidate; @@ -143,6 +153,7 @@ struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode) break; case EVAL_DICE: strcpy(cp->unspendableCCaddr,DiceCCaddr); + strcpy(cp->normaladdr,DiceNormaladdr); strcpy(cp->CChexstr,DiceCChexstr); memcpy(cp->CCpriv,DiceCCpriv,32); cp->validate = DiceValidate; @@ -150,6 +161,7 @@ struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode) break; case EVAL_LOTTO: strcpy(cp->unspendableCCaddr,LottoCCaddr); + strcpy(cp->normaladdr,LottoNormaladdr); strcpy(cp->CChexstr,LottoCChexstr); memcpy(cp->CCpriv,LottoCCpriv,32); cp->validate = LottoValidate; @@ -157,6 +169,7 @@ struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode) break; case EVAL_PONZI: strcpy(cp->unspendableCCaddr,PonziCCaddr); + strcpy(cp->normaladdr,PonziNormaladdr); strcpy(cp->CChexstr,PonziCChexstr); memcpy(cp->CCpriv,PonziCCpriv,32); cp->validate = PonziValidate; @@ -164,6 +177,7 @@ struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode) break; case EVAL_AUCTION: strcpy(cp->unspendableCCaddr,AuctionCCaddr); + strcpy(cp->normaladdr,AuctionNormaladdr); strcpy(cp->CChexstr,AuctionCChexstr); memcpy(cp->CCpriv,AuctionCCpriv,32); cp->validate = AuctionValidate; From 5f3e6795dbe77a5681b980ad13ae3da5f3c8733e Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 01:49:25 -1100 Subject: [PATCH 112/204] Fix --- src/cc/CCassetstx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 2d66f1b1d..679c77ef5 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -86,7 +86,7 @@ UniValue AssetList() UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; uint256 txid,hashBlock; CTransaction vintx; std::vector origpubkey; std::string name,description; char str[65]; cp = CCinit(&C,EVAL_ASSETS); SetCCtxids(addressIndex,cp->normaladdr); - for (std::vector >::const_iterator it=addressIndex.begin(); it!=unspentOutputs.end(); it++) + for (std::vector >::const_iterator it=addressIndex.begin(); it!=addressIndex.end(); it++) { txid = it->first.txhash; if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) From fdd2281045613f57bfd83d595912c5dc7edaadd7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 02:07:40 -1100 Subject: [PATCH 113/204] Rewardslist rewardsinfo --- src/cc/CCrewards.h | 2 ++ src/cc/rewards.cpp | 47 ++++++++++++++++++++++++++++++++++++++++ src/rpcserver.cpp | 2 ++ src/rpcserver.h | 2 ++ src/wallet/rpcwallet.cpp | 21 ++++++++++++++++++ 5 files changed, 74 insertions(+) diff --git a/src/cc/CCrewards.h b/src/cc/CCrewards.h index fe9c33b07..b9d36054f 100644 --- a/src/cc/CCrewards.h +++ b/src/cc/CCrewards.h @@ -22,6 +22,8 @@ #define EVAL_REWARDS 0xe5 bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); +UniValue RewardsInfo(uint256 rewardid); +UniValue RewardsList(); std::string RewardsCreateFunding(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit); std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount); diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 006085ed3..d654c5f66 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -245,6 +245,53 @@ bool RewardsPlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey rewa return(false); } +UniValue RewardsInfo(uint256 rewardsid) +{ + UniValue result(UniValue::VOBJ); uint256 hashBlock; CTransaction vintx; int64_t APR,minseconds,maxseconds,mindeposit; char str[67],numstr[65]; + if ( GetTransaction(rewardsid,vintx,hashBlock,false) == 0 ) + { + fprintf(stderr,"cant find assetid\n"); + result.push_back(Pair("error","cant find assetid")); + return(0); + } + if ( vintx.vout.size() > 0 && (funcid= DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' ) + { + fprintf(stderr,"assetid isnt assetcreation txid\n"); + result.push_back(Pair("error","assetid isnt assetcreation txid")); + } + result.push_back(Pair("result","success")); + result.push_back(Pair("fundingtxid",uint256_str(str,rewardsid))); + result.push_back(Pair("name",name)); + sprintf(numstr,"%.8f",(double)APR/COIN); + result.push_back(Pair("APR",numstr)); + result.push_back(Pair("minseconds",minseconds)); + result.push_back(Pair("maxseconds",maxseconds)); + sprintf(numstr,"%.8f",(double)mindeposit/COIN); + result.push_back(Pair("mindeposit",numstr)); + sprintf(numstr,"%.8f",(double)vintx.vout[0].nValue/COIN); + result.push_back(Pair("funding",numstr)); + return(result); +} + +UniValue RewardsList() +{ + UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; uint256 txid,hashBlock; CTransaction vintx; std::vector origpubkey; std::string name,description; char str[65]; + cp = CCinit(&C,EVAL_REWARDS); + SetCCtxids(addressIndex,cp->normaladdr); + for (std::vector >::const_iterator it=addressIndex.begin(); it!=addressIndex.end(); it++) + { + txid = it->first.txhash; + if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) + { + if ( vintx.vout.size() > 0 && DecodeRewardsCreateOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,origpubkey,name,description) != 0 ) + { + result.push_back(uint256_str(str,txid)); + } + } + } + return(result); +} + std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid) { CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,sbits,reward,amount=0,inputs,CCchange=0,APR,minseconds,maxseconds,mindeposit; struct CCcontract_info *cp,C; diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 1e665cea5..a7e16652a 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -354,6 +354,8 @@ static const CRPCCommand vRPCCommands[] = { "ponzi", "ponziaddress", &ponziaddress, true }, /* rewards */ + { "rewards", "rewardslist", &rewardslist, true }, + { "rewards", "rewardsinfo", &rewardsinfo, true }, { "rewards", "rewardscreatefunding", &rewardscreatefunding, true }, { "rewards", "rewardsaddfunding", &rewardsaddfunding, true }, { "rewards", "rewardslock", &rewardslock, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index 2b82e6e64..9144b874a 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -226,6 +226,8 @@ extern UniValue tokenfillswap(const UniValue& params, bool fHelp); extern UniValue faucetfund(const UniValue& params, bool fHelp); extern UniValue faucetget(const UniValue& params, bool fHelp); extern UniValue faucetaddress(const UniValue& params, bool fHelp); +extern UniValue rewardsinfo(const UniValue& params, bool fHelp); +extern UniValue rewardslist(const UniValue& params, bool fHelp); extern UniValue rewardsaddress(const UniValue& params, bool fHelp); extern UniValue rewardscreatefunding(const UniValue& params, bool fHelp); extern UniValue rewardsaddfunding(const UniValue& params, bool fHelp); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index e06e4292e..3678b25e0 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5122,6 +5122,27 @@ UniValue dicebet(const UniValue& params, bool fHelp) return(result); } +UniValue rewardslist(const UniValue& params, bool fHelp) +{ + uint256 tokenid; + if ( fHelp || params.size() > 0 ) + throw runtime_error("rewardslist\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"); + return(RewardsList()); +} + +UniValue rewardsinfo(const UniValue& params, bool fHelp) +{ + uint256 fundingtxid; + if ( fHelp || params.size() != 1 ) + throw runtime_error("rewardsinfo fundingtxid\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"); + fundingtxid = Parseuint256((char *)params[0].get_str().c_str()); + return(RewardsInfo(fundingtxid)); +} + UniValue tokenlist(const UniValue& params, bool fHelp) { uint256 tokenid; From 34589c80c69501a6d71740698419d8c91cf0c2c4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 02:10:07 -1100 Subject: [PATCH 114/204] Fix --- src/cc/rewards.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index d654c5f66..ca7fa23b0 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -247,7 +247,7 @@ bool RewardsPlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey rewa UniValue RewardsInfo(uint256 rewardsid) { - UniValue result(UniValue::VOBJ); uint256 hashBlock; CTransaction vintx; int64_t APR,minseconds,maxseconds,mindeposit; char str[67],numstr[65]; + UniValue result(UniValue::VOBJ); uint256 hashBlock; CTransaction vintx; int64_t APR,minseconds,maxseconds,mindeposit,sbits; char str[67],numstr[65]; if ( GetTransaction(rewardsid,vintx,hashBlock,false) == 0 ) { fprintf(stderr,"cant find assetid\n"); @@ -261,7 +261,8 @@ UniValue RewardsInfo(uint256 rewardsid) } result.push_back(Pair("result","success")); result.push_back(Pair("fundingtxid",uint256_str(str,rewardsid))); - result.push_back(Pair("name",name)); + unstringbits(str,sbits); + result.push_back(Pair("name",str)); sprintf(numstr,"%.8f",(double)APR/COIN); result.push_back(Pair("APR",numstr)); result.push_back(Pair("minseconds",minseconds)); From 435e20c288ddb3555056396b1d59d090e3ca2b90 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 02:10:51 -1100 Subject: [PATCH 115/204] Fix --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index ca7fa23b0..aba38da51 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -284,7 +284,7 @@ UniValue RewardsList() txid = it->first.txhash; if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { - if ( vintx.vout.size() > 0 && DecodeRewardsCreateOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,origpubkey,name,description) != 0 ) + if ( vintx.vout.size() > 0 && DecodeRewardsFundingOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,origpubkey,name,description) != 0 ) { result.push_back(uint256_str(str,txid)); } From 9fa1bcd2b9ca48709ba398a10dc533748458f981 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 02:13:17 -1100 Subject: [PATCH 116/204] Fix --- src/cc/rewards.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index aba38da51..8cb683e26 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -254,7 +254,7 @@ UniValue RewardsInfo(uint256 rewardsid) result.push_back(Pair("error","cant find assetid")); return(0); } - if ( vintx.vout.size() > 0 && (funcid= DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' ) + if ( vintx.vout.size() > 0 && DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit) != 0 ) { fprintf(stderr,"assetid isnt assetcreation txid\n"); result.push_back(Pair("error","assetid isnt assetcreation txid")); @@ -276,7 +276,7 @@ UniValue RewardsInfo(uint256 rewardsid) UniValue RewardsList() { - UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; uint256 txid,hashBlock; CTransaction vintx; std::vector origpubkey; std::string name,description; char str[65]; + UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; uint256 txid,hashBlock; CTransaction vintx; uint64_t sbits,APR,minseconds,maxseconds,mindeposit; char str[65]; cp = CCinit(&C,EVAL_REWARDS); SetCCtxids(addressIndex,cp->normaladdr); for (std::vector >::const_iterator it=addressIndex.begin(); it!=addressIndex.end(); it++) @@ -284,7 +284,7 @@ UniValue RewardsList() txid = it->first.txhash; if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { - if ( vintx.vout.size() > 0 && DecodeRewardsFundingOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,origpubkey,name,description) != 0 ) + if ( vintx.vout.size() > 0 && DecodeRewardsFundingOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit) != 0 ) { result.push_back(uint256_str(str,txid)); } From 547ad8efd0fe4f7b6d5b14e84e885c437013225c Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 02:14:12 -1100 Subject: [PATCH 117/204] Vent --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 8cb683e26..f654c0291 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -254,7 +254,7 @@ UniValue RewardsInfo(uint256 rewardsid) result.push_back(Pair("error","cant find assetid")); return(0); } - if ( vintx.vout.size() > 0 && DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit) != 0 ) + if ( vintx.vout.size() > 0 && DecodeRewardsFundingOpRet(vintx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit) != 0 ) { fprintf(stderr,"assetid isnt assetcreation txid\n"); result.push_back(Pair("error","assetid isnt assetcreation txid")); From 1cc5b8b8b287ed6cd419934f74fce937239d1dd4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 02:14:49 -1100 Subject: [PATCH 118/204] fix --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index f654c0291..826de2e4a 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -254,7 +254,7 @@ UniValue RewardsInfo(uint256 rewardsid) result.push_back(Pair("error","cant find assetid")); return(0); } - if ( vintx.vout.size() > 0 && DecodeRewardsFundingOpRet(vintx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit) != 0 ) + if ( vintx.vout.size() > 0 && DecodeRewardsFundingOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit) != 0 ) { fprintf(stderr,"assetid isnt assetcreation txid\n"); result.push_back(Pair("error","assetid isnt assetcreation txid")); From 46bbc4334808d135cc9856e2d741c5c801e5852d Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 02:17:51 -1100 Subject: [PATCH 119/204] Int64 --- src/cc/CCrewards.h | 6 +++--- src/cc/rewards.cpp | 23 +++++++++++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/cc/CCrewards.h b/src/cc/CCrewards.h index b9d36054f..e7db9d4a9 100644 --- a/src/cc/CCrewards.h +++ b/src/cc/CCrewards.h @@ -25,9 +25,9 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t UniValue RewardsInfo(uint256 rewardid); UniValue RewardsList(); -std::string RewardsCreateFunding(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit); -std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount); -std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount); +std::string RewardsCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t APR,int64_t minseconds,int64_t maxseconds,int64_t mindeposit); +std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount); +std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount); std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid); #endif diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 826de2e4a..2bef71da2 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -247,7 +247,7 @@ bool RewardsPlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey rewa UniValue RewardsInfo(uint256 rewardsid) { - UniValue result(UniValue::VOBJ); uint256 hashBlock; CTransaction vintx; int64_t APR,minseconds,maxseconds,mindeposit,sbits; char str[67],numstr[65]; + UniValue result(UniValue::VOBJ); uint256 hashBlock; CTransaction vintx; uint64_t APR,minseconds,maxseconds,mindeposit,sbits; char str[67],numstr[65]; if ( GetTransaction(rewardsid,vintx,hashBlock,false) == 0 ) { fprintf(stderr,"cant find assetid\n"); @@ -331,9 +331,14 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 return(0); } -std::string RewardsCreateFunding(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) +std::string RewardsCreateFunding(uint64_t txfee,char *planstr,int64_t funds,uint64_t APR,int64_t minseconds,int64_t maxseconds,int64_t mindeposit) { CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,a,b,c,d; struct CCcontract_info *cp,C; + if ( funds < 0 || mindeposit < 0 || minseconds < 0 || maxseconds < 0 ) + { + fprintf(stderr,"negative parameter error\n"); + return(0); + } cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; @@ -354,9 +359,14 @@ std::string RewardsCreateFunding(uint64_t txfee,char *planstr,uint64_t funds,uin return(0); } -std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount) +std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount) { CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,a,b,c,d; struct CCcontract_info *cp,C; + if ( amount < 0 ) + { + fprintf(stderr,"negative parameter error\n"); + return(0); + } cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; @@ -376,9 +386,14 @@ std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,u return(0); } -std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t deposit) +std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t deposit) { CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding,APR,minseconds,maxseconds,mindeposit; struct CCcontract_info *cp,C; + if ( deposit < 0 ) + { + fprintf(stderr,"negative parameter error\n"); + return(0); + } cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; From 8eee6bd85694a760cc1807073139d6e9eec3697c Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 02:21:33 -1100 Subject: [PATCH 120/204] Int64 --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 2bef71da2..e3adadfe3 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -331,7 +331,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 return(0); } -std::string RewardsCreateFunding(uint64_t txfee,char *planstr,int64_t funds,uint64_t APR,int64_t minseconds,int64_t maxseconds,int64_t mindeposit) +std::string RewardsCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t APR,int64_t minseconds,int64_t maxseconds,int64_t mindeposit) { CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,a,b,c,d; struct CCcontract_info *cp,C; if ( funds < 0 || mindeposit < 0 || minseconds < 0 || maxseconds < 0 ) From d4e617631fa6086f4f247de03daaa9b378168f5b Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 02:24:22 -1100 Subject: [PATCH 121/204] Fix createfunding --- src/cc/rewards.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index e3adadfe3..375a697cd 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -263,6 +263,7 @@ UniValue RewardsInfo(uint256 rewardsid) result.push_back(Pair("fundingtxid",uint256_str(str,rewardsid))); unstringbits(str,sbits); result.push_back(Pair("name",str)); + result.push_back(Pair("sbits",sbits)); sprintf(numstr,"%.8f",(double)APR/COIN); result.push_back(Pair("APR",numstr)); result.push_back(Pair("minseconds",minseconds)); @@ -377,6 +378,7 @@ std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,i fprintf(stderr,"Rewards plan %s doesnt exist\n",planstr); return(0); } + sbits = stringbits(planstr); if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,rewardspk)); From bbf9b82f6217380d0630547542d699c5b72d3b86 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 02:29:21 -1100 Subject: [PATCH 122/204] Fix false positive error --- src/cc/rewards.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 375a697cd..9b8ecbb1a 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -250,14 +250,15 @@ UniValue RewardsInfo(uint256 rewardsid) UniValue result(UniValue::VOBJ); uint256 hashBlock; CTransaction vintx; uint64_t APR,minseconds,maxseconds,mindeposit,sbits; char str[67],numstr[65]; if ( GetTransaction(rewardsid,vintx,hashBlock,false) == 0 ) { - fprintf(stderr,"cant find assetid\n"); - result.push_back(Pair("error","cant find assetid")); - return(0); + fprintf(stderr,"cant find fundingtxid\n"); + result.push_back(Pair("error","cant find fundingtxid")); + return(result); } - if ( vintx.vout.size() > 0 && DecodeRewardsFundingOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit) != 0 ) + if ( vintx.vout.size() > 0 && DecodeRewardsFundingOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit) == 0 ) { - fprintf(stderr,"assetid isnt assetcreation txid\n"); - result.push_back(Pair("error","assetid isnt assetcreation txid")); + fprintf(stderr,"fundingtxid isnt rewards creation txid\n"); + result.push_back(Pair("error","fundingtxid isnt rewards creation txid")); + return(result); } result.push_back(Pair("result","success")); result.push_back(Pair("fundingtxid",uint256_str(str,rewardsid))); From d549130d79e3756e1e9989794d513c1b34b85a25 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 02:32:00 -1100 Subject: [PATCH 123/204] sbits = stringbits(planstr); --- src/cc/rewards.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 9b8ecbb1a..2c3b8ca6d 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -346,6 +346,7 @@ std::string RewardsCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int6 txfee = 10000; mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); + sbits = stringbits(planstr); if ( RewardsPlanExists(cp,sbits,rewardspk,a,b,c,d) != 0 ) { fprintf(stderr,"Rewards plan %s already exists\n",planstr); @@ -374,6 +375,7 @@ std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,i txfee = 10000; mypk = pubkey2pk(Mypubkey()); rewardspk = GetUnspendable(cp,0); + sbits = stringbits(planstr); if ( RewardsPlanExists(cp,sbits,rewardspk,a,b,c,d) == 0 ) { fprintf(stderr,"Rewards plan %s doesnt exist\n",planstr); From bdf2cf5fc301a1dff8c7f180f94e03b95eec2a73 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 02:33:45 -1100 Subject: [PATCH 124/204] Fix maxmoney --- src/komodo_utils.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_utils.h b/src/komodo_utils.h index 8188a6154..747a43b72 100644 --- a/src/komodo_utils.h +++ b/src/komodo_utils.h @@ -1605,8 +1605,8 @@ void komodo_args(char *argv0) if ( (baseid= komodo_baseid(ASSETCHAINS_SYMBOL)) >= 0 && baseid < 32 ) MAX_MONEY = komodo_maxallowed(baseid); else if ( ASSETCHAINS_REWARD == 0 ) - MAX_MONEY = (ASSETCHAINS_SUPPLY+1) * SATOSHIDEN; - else MAX_MONEY = (ASSETCHAINS_SUPPLY+1) * SATOSHIDEN + ASSETCHAINS_REWARD * (ASSETCHAINS_ENDSUBSIDY==0 ? 10000000 : ASSETCHAINS_ENDSUBSIDY); + MAX_MONEY = (ASSETCHAINS_SUPPLY+100) * SATOSHIDEN; + else MAX_MONEY = (ASSETCHAINS_SUPPLY+100) * SATOSHIDEN + ASSETCHAINS_REWARD * (ASSETCHAINS_ENDSUBSIDY==0 ? 10000000 : ASSETCHAINS_ENDSUBSIDY); MAX_MONEY += (MAX_MONEY * ASSETCHAINS_COMMISSION) / SATOSHIDEN; //printf("baseid.%d MAX_MONEY.%s %.8f\n",baseid,ASSETCHAINS_SYMBOL,(double)MAX_MONEY/SATOSHIDEN); ASSETCHAINS_P2PPORT = komodo_port(ASSETCHAINS_SYMBOL,ASSETCHAINS_SUPPLY,&ASSETCHAINS_MAGIC,extraptr,extralen); From 117874698cd260beaf31ae0187ade80a2d98f510 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 02:40:32 -1100 Subject: [PATCH 125/204] Fix funding name --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 3678b25e0..fee15e0f9 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4968,7 +4968,7 @@ UniValue rewardscreatefunding(const UniValue& params, bool fHelp) APR = 5 * COIN; minseconds = maxseconds = 60 * 3600 * 24; mindeposit = 100 * COIN; - name = (char *)params[1].get_str().c_str(); + name = (char *)params[0].get_str().c_str(); funds = atof(params[1].get_str().c_str()) * COIN; if ( params.size() > 2 ) { From 2205fd651e4bedf27fd823e301cdfb8fab3f59bf Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 02:55:00 -1100 Subject: [PATCH 126/204] Test --- src/cc/rewards.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 2c3b8ca6d..abd48104f 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -216,8 +216,9 @@ uint64_t RewardsPlanFunds(uint64_t refsbits,struct CCcontract_info *cp,CPubKey p { if ( refsbits == sbits && (nValue= IsRewardsvout(cp,tx,vout)) > 0 ) totalinputs += nValue; + else fprintf(stderr,"refsbits.%llx sbits.%llx nValue %.8f\n",(long long)refsbits,(long long)sbits,(double)nValue/COIN); } - } + } else fprintf(stderr,"funcid.%d skipped\n",funcid); } } return(totalinputs); @@ -349,7 +350,7 @@ std::string RewardsCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int6 sbits = stringbits(planstr); if ( RewardsPlanExists(cp,sbits,rewardspk,a,b,c,d) != 0 ) { - fprintf(stderr,"Rewards plan %s already exists\n",planstr); + fprintf(stderr,"Rewards plan (%s) already exists\n",planstr); return(0); } if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) From e90ab9d54dc1d8e6a0d5aa3acae7cbe86e4f4618 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 02:57:39 -1100 Subject: [PATCH 127/204] Print --- src/cc/rewards.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index abd48104f..fc1f1a036 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -215,9 +215,12 @@ uint64_t RewardsPlanFunds(uint64_t refsbits,struct CCcontract_info *cp,CPubKey p if ( (funcid == 'F' && reffundingtxid == txid) || reffundingtxid == fundingtxid ) { if ( refsbits == sbits && (nValue= IsRewardsvout(cp,tx,vout)) > 0 ) + { totalinputs += nValue; + fprintf(stderr,"got utxo\n"); + } else fprintf(stderr,"refsbits.%llx sbits.%llx nValue %.8f\n",(long long)refsbits,(long long)sbits,(double)nValue/COIN); - } + } else fprintf(stderr,"else case\n"); } else fprintf(stderr,"funcid.%d skipped\n",funcid); } } From 9d2841c3496f20adfd10685e618c50a782fedc9a Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 04:40:37 -1100 Subject: [PATCH 128/204] CCutoxvalue --- src/cc/CCinclude.h | 1 + src/cc/CCtx.cpp | 13 ++++++++ src/cc/CCutils.cpp | 2 -- src/cc/rewards.cpp | 82 ++++++++++++++++++++++++---------------------- 4 files changed, 57 insertions(+), 41 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index fe1ba7a6c..0f0348ebb 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -75,5 +75,6 @@ std::string FinalizeCCTx(struct CCcontract_info *cp,CMutableTransaction &mtx,CPu void SetCCunspents(std::vector > &unspentOutputs,char *coinaddr); void SetCCtxids(std::vector > &addressIndex,char *coinaddr); uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,int32_t maxinputs); +uint64_t CCutxovalue(char *coinaddr,uint256 utxotxid,int32_t utxovout); #endif diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index 7b8aaa866..1620773ee 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -182,6 +182,19 @@ void SetCCtxids(std::vector > &addressIndex } } +uint64_t CCutxovalue(char *coinaddr,uint256 utxotxid,int32_t utxovout) +{ + uint256 txid; std::vector > unspentOutputs; + SetCCunspents(unspentOutputs,coinaddr); + for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) + { + txid = it->first.txhash; + if ( txid == utxotxid && utxovout == it->first.index ) + return(it->second.satoshis); + } + return(0); +} + uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,int32_t maxinputs) { int32_t vout,j,n = 0; uint64_t nValue,totalinputs = 0; uint256 txid; std::vector vecOutputs; diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 039f5cbd9..b68d1061b 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -268,5 +268,3 @@ bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector param return(false); } - - diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index fc1f1a036..fd43e2667 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -299,44 +299,6 @@ UniValue RewardsList() return(result); } -std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid) -{ - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,sbits,reward,amount=0,inputs,CCchange=0,APR,minseconds,maxseconds,mindeposit; struct CCcontract_info *cp,C; - cp = CCinit(&C,EVAL_REWARDS); - if ( txfee == 0 ) - txfee = 10000; - rewardspk = GetUnspendable(cp,0); - mypk = pubkey2pk(Mypubkey()); - sbits = stringbits(planstr); - if ( RewardsPlanExists(cp,sbits,rewardspk,APR,minseconds,maxseconds,mindeposit) == 0 ) - { - fprintf(stderr,"Rewards plan %s doesnt exist\n",planstr); - return(0); - } - if ( locktxid == zeroid ) - amount = AddRewardsInputs(cp,mtx,rewardspk,(1LL << 30),1); - else - { - fprintf(stderr,"check if locktxid is unspent\n"); - return(0); - } - if ( amount > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash,APR,minseconds,maxseconds,mindeposit)) > txfee ) - { - if ( (inputs= AddRewardsInputs(cp,mtx,mypk,reward+amount+txfee,30)) > 0 ) - { - if ( inputs >= (amount + reward + 2*txfee) ) - CCchange = (inputs - (amount + reward + txfee)); - if ( CCchange != 0 ) - mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk)); - mtx.vout.push_back(CTxOut(amount+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid))); - } - fprintf(stderr,"cant find enough rewards inputs\n"); - } - fprintf(stderr,"cant find rewards inputs\n"); - return(0); -} - std::string RewardsCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t APR,int64_t minseconds,int64_t maxseconds,int64_t mindeposit) { CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,a,b,c,d; struct CCcontract_info *cp,C; @@ -425,10 +387,52 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t { mtx.vout.push_back(MakeCC1vout(cp->evalcode,deposit,rewardspk)); return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('L',sbits,fundingtxid))); - } else fprintf(stderr,"cant find enough inputs\n"); + } else fprintf(stderr,"cant find enough inputs %.8f note enough for %.8f\n",(double)funding/COIN,(double)deposit/COIN); } fprintf(stderr,"cant find rewards inputs\n"); return(0); } +std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid) +{ + CMutableTransaction mtx; char coinaddr[64]; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,sbits,reward,amount=0,inputs,CCchange=0,APR,minseconds,maxseconds,mindeposit; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_REWARDS); + if ( txfee == 0 ) + txfee = 10000; + rewardspk = GetUnspendable(cp,0); + mypk = pubkey2pk(Mypubkey()); + sbits = stringbits(planstr); + if ( RewardsPlanExists(cp,sbits,rewardspk,APR,minseconds,maxseconds,mindeposit) == 0 ) + { + fprintf(stderr,"Rewards plan %s doesnt exist\n",planstr); + return(0); + } + if ( locktxid == zeroid ) + amount = AddRewardsInputs(cp,mtx,rewardspk,(1LL << 30),1); + else + { + GetCCaddress(cp,coinaddr,mypk); + if ( (amount= CCutxovalue(coinaddr,locktxid,0)) == 0 ) + { + fprintf(stderr,"locktxid/v0 is spent\n"); + return(0); + } + mtx.vin.push_back(CTxIn(locktxid,0,CScript())); + } + if ( amount > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash,APR,minseconds,maxseconds,mindeposit)) > txfee ) + { + if ( (inputs= AddRewardsInputs(cp,mtx,mypk,reward+amount+txfee,30)) > 0 ) + { + if ( inputs >= (amount + reward + 2*txfee) ) + CCchange = (inputs - (amount + reward + txfee)); + if ( CCchange != 0 ) + mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk)); + mtx.vout.push_back(CTxOut(amount+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid))); + } + fprintf(stderr,"cant find enough rewards inputs\n"); + } + fprintf(stderr,"amount %.8f -> reward %.8f\n",(double)amount/COIN,(double)reward/COIN); + return(0); +} From feb9f83408d216722eda109b89f4643be1187af9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 04:47:41 -1100 Subject: [PATCH 129/204] Fix rewardslock utxo selection --- src/cc/rewards.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index fd43e2667..98e5d727a 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -167,9 +167,9 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t return(true); } -uint64_t AddRewardsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) +uint64_t AddRewardsInputs(int32_t cmpflag,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { - char coinaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,nValue,totalinputs = 0; uint256 txid,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; + char coinaddr[64],destaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,nValue,totalinputs = 0; uint256 txid,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; const CScript scriptPubKey; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); @@ -177,14 +177,21 @@ uint64_t AddRewardsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CP { txid = it->first.txhash; vout = (int32_t)it->first.index; + for (j=0; j 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (funcid= DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || DecodeRewardsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid) != 0 ) + scriptPubKey = tx.vout[tx.vout.size()-1].scriptPubKey; + Getscriptaddress(destaddr,scriptPubKey); + if ( cmpflag > 0 && strcmp(coinaddr,destaddr) != 0 ) + continue; + else if ( cmpflag < 0 && strcmp(coinaddr,destaddr) == 0 ) + continue; + if ( (funcid= DecodeRewardsFundingOpRet(scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || DecodeRewardsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid) != 0 ) { if ( total != 0 && maxinputs != 0 ) mtx.vin.push_back(CTxIn(txid,vout,CScript())); @@ -408,7 +415,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 return(0); } if ( locktxid == zeroid ) - amount = AddRewardsInputs(cp,mtx,rewardspk,(1LL << 30),1); + amount = AddRewardsInputs(-1,cp,mtx,rewardspk,(1LL << 30),1); else { GetCCaddress(cp,coinaddr,mypk); @@ -421,7 +428,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 } if ( amount > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash,APR,minseconds,maxseconds,mindeposit)) > txfee ) { - if ( (inputs= AddRewardsInputs(cp,mtx,mypk,reward+amount+txfee,30)) > 0 ) + if ( (inputs= AddRewardsInputs(1,cp,mtx,mypk,reward+amount+txfee,30)) > 0 ) { if ( inputs >= (amount + reward + 2*txfee) ) CCchange = (inputs - (amount + reward + txfee)); From dfb0fdfa32a4ab08f1578bd5c6d9849e0c155db6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 04:56:51 -1100 Subject: [PATCH 130/204] New PoW diff calc --- src/komodo_bitcoind.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 55e1ae2cf..5cc84abfb 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1357,9 +1357,12 @@ arith_uint256 komodo_PoWtarget(int32_t *percPoSp,arith_uint256 target,int32_t he if ( ave > target ) ave = target; } else ave = easydiff; //else return(target); + if ( percPoS == 0 ) + percPoS = 1; if ( percPoS < goalperc ) // increase PoW diff -> lower bnTarget { - bnTarget = (ave * arith_uint256(percPoS * percPoS)) / arith_uint256(goalperc * goalperc * goalperc); + //bnTarget = (ave * arith_uint256(percPoS * percPoS)) / arith_uint256(goalperc * goalperc * goalperc); + bnTarget = (ave / arith_uint256(goalperc * goalperc * goalperc)) * arith_uint256(percPoS * percPoS); if ( ASSETCHAINS_STAKED < 100 ) { for (i=31; i>=24; i--) @@ -1375,9 +1378,10 @@ arith_uint256 komodo_PoWtarget(int32_t *percPoSp,arith_uint256 target,int32_t he } else if ( percPoS > goalperc ) // decrease PoW diff -> raise bnTarget { - bnTarget = ((ave * arith_uint256(goalperc)) + (easydiff * arith_uint256(percPoS))) / arith_uint256(percPoS + goalperc); + //bnTarget = ((ave * arith_uint256(goalperc)) + (easydiff * arith_uint256(percPoS))) / arith_uint256(percPoS + goalperc); //bnTarget = (bnTarget * arith_uint256(percPoS * percPoS * percPoS)) / arith_uint256(goalperc * goalperc); bnTarget = (bnTarget / arith_uint256(goalperc * goalperc)) * arith_uint256(percPoS * percPoS * percPoS); + if ( bnTarget > easydiff ) bnTarget = easydiff; else if ( bnTarget < ave ) // overflow From e3a567574a5f3056a5ecaaea25ad2e2d7f619450 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 04:59:19 -1100 Subject: [PATCH 131/204] tst --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 98e5d727a..c7cae3f76 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -185,7 +185,7 @@ uint64_t AddRewardsInputs(int32_t cmpflag,struct CCcontract_info *cp,CMutableTra continue; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - scriptPubKey = tx.vout[tx.vout.size()-1].scriptPubKey; + scriptPubKey = (const CScript)tx.vout[tx.vout.size()-1].scriptPubKey; Getscriptaddress(destaddr,scriptPubKey); if ( cmpflag > 0 && strcmp(coinaddr,destaddr) != 0 ) continue; From 651f7284b9b92d16b8dfa188dc4649a297d45555 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 05:00:04 -1100 Subject: [PATCH 132/204] Test --- src/cc/rewards.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index c7cae3f76..9642921f4 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -169,7 +169,7 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t uint64_t AddRewardsInputs(int32_t cmpflag,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { - char coinaddr[64],destaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,nValue,totalinputs = 0; uint256 txid,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; const CScript scriptPubKey; + char coinaddr[64],destaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,nValue,totalinputs = 0; uint256 txid,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; CScript scriptPubKey; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); @@ -185,7 +185,7 @@ uint64_t AddRewardsInputs(int32_t cmpflag,struct CCcontract_info *cp,CMutableTra continue; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - scriptPubKey = (const CScript)tx.vout[tx.vout.size()-1].scriptPubKey; + scriptPubKey = (CScript)tx.vout[tx.vout.size()-1].scriptPubKey; Getscriptaddress(destaddr,scriptPubKey); if ( cmpflag > 0 && strcmp(coinaddr,destaddr) != 0 ) continue; From 23c192b98ad7709a962a27ba61027f60a3e22b52 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 05:03:47 -1100 Subject: [PATCH 133/204] Oldflag for PoW calc --- src/komodo_bitcoind.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 5cc84abfb..123c0fd5a 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1293,6 +1293,7 @@ uint32_t komodo_stake(int32_t validateflag,arith_uint256 bnTarget,int32_t nHeigh arith_uint256 komodo_PoWtarget(int32_t *percPoSp,arith_uint256 target,int32_t height,int32_t goalperc) { + int32_t oldflag = 0; CBlockIndex *pindex; arith_uint256 easydiff,bnTarget,hashval,sum,ave; bool fNegative,fOverflow; int32_t i,n,m,ht,percPoS,diff,val; *percPoSp = percPoS = 0; if ( height <= 10 || (ASSETCHAINS_STAKED == 100 && height <= 100) ) @@ -1361,8 +1362,9 @@ arith_uint256 komodo_PoWtarget(int32_t *percPoSp,arith_uint256 target,int32_t he percPoS = 1; if ( percPoS < goalperc ) // increase PoW diff -> lower bnTarget { - //bnTarget = (ave * arith_uint256(percPoS * percPoS)) / arith_uint256(goalperc * goalperc * goalperc); - bnTarget = (ave / arith_uint256(goalperc * goalperc * goalperc)) * arith_uint256(percPoS * percPoS); + if ( oldflag != 0 ) + bnTarget = (ave * arith_uint256(percPoS * percPoS)) / arith_uint256(goalperc * goalperc * goalperc); + else bnTarget = (ave / arith_uint256(goalperc * goalperc * goalperc)) * arith_uint256(percPoS * percPoS); if ( ASSETCHAINS_STAKED < 100 ) { for (i=31; i>=24; i--) @@ -1378,10 +1380,12 @@ arith_uint256 komodo_PoWtarget(int32_t *percPoSp,arith_uint256 target,int32_t he } else if ( percPoS > goalperc ) // decrease PoW diff -> raise bnTarget { - //bnTarget = ((ave * arith_uint256(goalperc)) + (easydiff * arith_uint256(percPoS))) / arith_uint256(percPoS + goalperc); - //bnTarget = (bnTarget * arith_uint256(percPoS * percPoS * percPoS)) / arith_uint256(goalperc * goalperc); - bnTarget = (bnTarget / arith_uint256(goalperc * goalperc)) * arith_uint256(percPoS * percPoS * percPoS); - + if ( oldflag != 0 ) + { + bnTarget = ((ave * arith_uint256(goalperc)) + (easydiff * arith_uint256(percPoS))) / arith_uint256(percPoS + goalperc); + //bnTarget = (bnTarget * arith_uint256(percPoS * percPoS * percPoS)) / arith_uint256(goalperc * goalperc); + bnTarget = (bnTarget / arith_uint256(goalperc * goalperc)) * arith_uint256(percPoS * percPoS * percPoS); + } else bnTarget = (bnTarget / arith_uint256(goalperc * goalperc)) * arith_uint256(percPoS * percPoS * percPoS); if ( bnTarget > easydiff ) bnTarget = easydiff; else if ( bnTarget < ave ) // overflow From 918a6e406dd9675d8d512075855fd448d9887813 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 05:06:24 -1100 Subject: [PATCH 134/204] Test --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 9642921f4..e40163c02 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -185,7 +185,7 @@ uint64_t AddRewardsInputs(int32_t cmpflag,struct CCcontract_info *cp,CMutableTra continue; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - scriptPubKey = (CScript)tx.vout[tx.vout.size()-1].scriptPubKey; + scriptPubKey = (CScript)tx.vout[vout].scriptPubKey; Getscriptaddress(destaddr,scriptPubKey); if ( cmpflag > 0 && strcmp(coinaddr,destaddr) != 0 ) continue; From 791cf78ca50eb980fe451bb3f5356bc719bbcab7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 05:18:27 -1100 Subject: [PATCH 135/204] Test --- src/cc/rewards.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index e40163c02..d5e79a694 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -167,9 +167,10 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t return(true); } -uint64_t AddRewardsInputs(int32_t cmpflag,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) +// 'L' vs 'F' and 'A' +uint64_t AddRewardsInputs(int32_t fundsflag,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { - char coinaddr[64],destaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,nValue,totalinputs = 0; uint256 txid,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; CScript scriptPubKey; + char coinaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,nValue,totalinputs = 0; uint256 txid,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); @@ -177,7 +178,6 @@ uint64_t AddRewardsInputs(int32_t cmpflag,struct CCcontract_info *cp,CMutableTra { txid = it->first.txhash; vout = (int32_t)it->first.index; - for (j=0; j 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - scriptPubKey = (CScript)tx.vout[vout].scriptPubKey; - Getscriptaddress(destaddr,scriptPubKey); - if ( cmpflag > 0 && strcmp(coinaddr,destaddr) != 0 ) - continue; - else if ( cmpflag < 0 && strcmp(coinaddr,destaddr) == 0 ) - continue; - if ( (funcid= DecodeRewardsFundingOpRet(scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || DecodeRewardsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid) != 0 ) + if ( (funcid= DecodeRewardsFundingOpRet(scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || (funcid= DecodeRewardsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid)) != 0 ) { + if ( fundsflag != 0 && funcid != 'F' && funcid != 'A' ) + continue; + else if ( fundsflag == 0 && funcid != 'L' ) + continue; if ( total != 0 && maxinputs != 0 ) mtx.vin.push_back(CTxIn(txid,vout,CScript())); totalinputs += it->second.satoshis; @@ -390,9 +388,10 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t } if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,fundingtxid)) >= deposit ) // arbitrary cmpval { - if ( AddNormalinputs(mtx,mypk,deposit+txfee,64) > 0 ) + if ( AddNormalinputs(mtx,mypk,deposit+2*txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,deposit,rewardspk)); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('L',sbits,fundingtxid))); } else fprintf(stderr,"cant find enough inputs %.8f note enough for %.8f\n",(double)funding/COIN,(double)deposit/COIN); } @@ -414,8 +413,9 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 fprintf(stderr,"Rewards plan %s doesnt exist\n",planstr); return(0); } + // need to deal with finding the right utxos if ( locktxid == zeroid ) - amount = AddRewardsInputs(-1,cp,mtx,rewardspk,(1LL << 30),1); + amount = AddRewardsInputs(0,cp,mtx,rewardspk,(1LL << 30),1); else { GetCCaddress(cp,coinaddr,mypk); From a89a76c457f0db7b776918ffe6db6f132da2330c Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 05:23:01 -1100 Subject: [PATCH 136/204] tst --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index d5e79a694..75ea380b9 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -185,7 +185,7 @@ uint64_t AddRewardsInputs(int32_t fundsflag,struct CCcontract_info *cp,CMutableT continue; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (funcid= DecodeRewardsFundingOpRet(scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || (funcid= DecodeRewardsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid)) != 0 ) + if ( (funcid= DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || (funcid= DecodeRewardsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid)) != 0 ) { if ( fundsflag != 0 && funcid != 'F' && funcid != 'A' ) continue; From b938be965030544224f98b06bcf4249c50f4cd45 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 05:25:10 -1100 Subject: [PATCH 137/204] Test --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 75ea380b9..8b51fc6cd 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -421,7 +421,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 GetCCaddress(cp,coinaddr,mypk); if ( (amount= CCutxovalue(coinaddr,locktxid,0)) == 0 ) { - fprintf(stderr,"locktxid/v0 is spent\n"); + fprintf(stderr,"%s locktxid/v0 is spent\n",coinaddr); return(0); } mtx.vin.push_back(CTxIn(locktxid,0,CScript())); From 1d0c776260a9cd9bdffa981e145db5537064fa60 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 05:26:30 -1100 Subject: [PATCH 138/204] Test --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 8b51fc6cd..b9cd074c6 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -418,7 +418,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 amount = AddRewardsInputs(0,cp,mtx,rewardspk,(1LL << 30),1); else { - GetCCaddress(cp,coinaddr,mypk); + GetCCaddress(cp,coinaddr,rewardspk); if ( (amount= CCutxovalue(coinaddr,locktxid,0)) == 0 ) { fprintf(stderr,"%s locktxid/v0 is spent\n",coinaddr); From 0efcb1996123b58b7329155d58c3d8afa8ddc473 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 05:45:41 -1100 Subject: [PATCH 139/204] Filter locked utxo --- src/cc/rewards.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index b9cd074c6..170a4a50a 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -189,7 +189,7 @@ uint64_t AddRewardsInputs(int32_t fundsflag,struct CCcontract_info *cp,CMutableT { if ( fundsflag != 0 && funcid != 'F' && funcid != 'A' ) continue; - else if ( fundsflag == 0 && funcid != 'L' ) + else if ( fundsflag == 0 && (funcid != 'L' || tx.vout.size() < 4 ) continue; if ( total != 0 && maxinputs != 0 ) mtx.vin.push_back(CTxIn(txid,vout,CScript())); @@ -215,7 +215,7 @@ uint64_t RewardsPlanFunds(uint64_t refsbits,struct CCcontract_info *cp,CPubKey p vout = (int32_t)it->first.index; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (funcid= DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || DecodeRewardsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid) != 0 ) + if ( (funcid= DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || (funcid= DecodeRewardsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid)) == 'A' ) { if ( (funcid == 'F' && reffundingtxid == txid) || reffundingtxid == fundingtxid ) { From 337c2501ff378825aa6821a09a2b84ee013e14ed Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 05:51:10 -1100 Subject: [PATCH 140/204] Test --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 170a4a50a..2e52b2861 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -189,7 +189,7 @@ uint64_t AddRewardsInputs(int32_t fundsflag,struct CCcontract_info *cp,CMutableT { if ( fundsflag != 0 && funcid != 'F' && funcid != 'A' ) continue; - else if ( fundsflag == 0 && (funcid != 'L' || tx.vout.size() < 4 ) + else if ( fundsflag == 0 && (funcid != 'L' || tx.vout.size() < 4) ) continue; if ( total != 0 && maxinputs != 0 ) mtx.vin.push_back(CTxIn(txid,vout,CScript())); From 64d610d41a92606eaf37ff492b360add65961e77 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 05:57:42 -1100 Subject: [PATCH 141/204] Fix PoW recalc --- src/cc/rewards.cpp | 3 +-- src/komodo_bitcoind.h | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 2e52b2861..9473d25e6 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -222,11 +222,10 @@ uint64_t RewardsPlanFunds(uint64_t refsbits,struct CCcontract_info *cp,CPubKey p if ( refsbits == sbits && (nValue= IsRewardsvout(cp,tx,vout)) > 0 ) { totalinputs += nValue; - fprintf(stderr,"got utxo\n"); } else fprintf(stderr,"refsbits.%llx sbits.%llx nValue %.8f\n",(long long)refsbits,(long long)sbits,(double)nValue/COIN); } else fprintf(stderr,"else case\n"); - } else fprintf(stderr,"funcid.%d skipped\n",funcid); + } else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); } } return(totalinputs); diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 123c0fd5a..3901c4ee7 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1385,7 +1385,7 @@ arith_uint256 komodo_PoWtarget(int32_t *percPoSp,arith_uint256 target,int32_t he bnTarget = ((ave * arith_uint256(goalperc)) + (easydiff * arith_uint256(percPoS))) / arith_uint256(percPoS + goalperc); //bnTarget = (bnTarget * arith_uint256(percPoS * percPoS * percPoS)) / arith_uint256(goalperc * goalperc); bnTarget = (bnTarget / arith_uint256(goalperc * goalperc)) * arith_uint256(percPoS * percPoS * percPoS); - } else bnTarget = (bnTarget / arith_uint256(goalperc * goalperc)) * arith_uint256(percPoS * percPoS * percPoS); + } else bnTarget = (ave / arith_uint256(goalperc * goalperc)) * arith_uint256(percPoS * percPoS * percPoS); if ( bnTarget > easydiff ) bnTarget = easydiff; else if ( bnTarget < ave ) // overflow From f66eff046a1c13e23ccb757cecbb6c12b7134a3b Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:08:52 -1100 Subject: [PATCH 142/204] Test min seconds --- src/cc/rewards.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 9473d25e6..6caec9e5d 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -37,13 +37,15 @@ Unlock does a CC spend to the vout1 address */ -uint64_t RewardsCalc(uint64_t claim,uint256 txid,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) +uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) { - uint64_t reward = 0; - // get txtime2, get pblock->nTime - // if elapsed < mintime -> return 0 - // if elapsed > maxtime, elapsed = maxtime - // calc reward + uint64_t duration,reward = 0; + if ( (duration= CCduration(txid)) < minseconds ) + return(0); + else if ( duration > maxseconds ) + maxseconds = duration; + reward = (amount * APR) / maxseconds; + fprintf(stderr,"amount %.8f -> duration.%llu reward %.8f\n",(double)amount/COIN,(long long)duration,(double)reward/COIN); return(reward); } From 2b011ff7fe8dfbdbd868be64b1b69cf9533b566a Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:12:13 -1100 Subject: [PATCH 143/204] int64_t CCduration(uint256 txid) --- src/cc/CCinclude.h | 1 + src/cc/CCutils.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 0f0348ebb..94d8d5b2c 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -69,6 +69,7 @@ bool PreventCC(Eval* eval,const CTransaction &tx,int32_t preventCCvins,int32_t n bool Getscriptaddress(char *destaddr,const CScript &scriptPubKey); std::vector Mypubkey(); bool Myprivkey(uint8_t myprivkey[]); +int64_t CCduration(uint256 txid); // CCtx std::string FinalizeCCTx(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret); diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index b68d1061b..af92b69a3 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -268,3 +268,14 @@ bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector param return(false); } +int64_t CCduration(uint256 txid) +{ + CTransaction tx; uint256 hashBlock; int64_t duration = 0; + if ( GetTransaction(txid,tx,hashBlock,false) == 0 ) + { + fprintf(stderr,"cant find duration txid\n"); + return(0); + } + return(duration); +} + From 626f89e2573e533bf0abd79f21cc423674787236 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:15:19 -1100 Subject: [PATCH 144/204] Test --- src/cc/rewards.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 6caec9e5d..765d4beba 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -41,7 +41,9 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon { uint64_t duration,reward = 0; if ( (duration= CCduration(txid)) < minseconds ) - return(0); + { + //return(0); + } else if ( duration > maxseconds ) maxseconds = duration; reward = (amount * APR) / maxseconds; From f400e642a2b9879e8141312058b627aa38fc8fd6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:18:41 -1100 Subject: [PATCH 145/204] Test --- src/cc/rewards.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 765d4beba..d35622235 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -43,10 +43,11 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon if ( (duration= CCduration(txid)) < minseconds ) { //return(0); + duration = 3600; } else if ( duration > maxseconds ) maxseconds = duration; - reward = (amount * APR) / maxseconds; + reward = (amount * duration) / APR; fprintf(stderr,"amount %.8f -> duration.%llu reward %.8f\n",(double)amount/COIN,(long long)duration,(double)reward/COIN); return(reward); } From ce21a0d9084f6afc08aa8ab382a5de6cdc75fc7f Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:23:56 -1100 Subject: [PATCH 146/204] Test one day --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index d35622235..b932124e4 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -43,7 +43,7 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon if ( (duration= CCduration(txid)) < minseconds ) { //return(0); - duration = 3600; + duration = 3600 * 24; } else if ( duration > maxseconds ) maxseconds = duration; From 018a87ddd09eaec131c12d5cf589690e7d991cec Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:26:43 -1100 Subject: [PATCH 147/204] Test --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index b932124e4..2f74f71b0 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -47,7 +47,7 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon } else if ( duration > maxseconds ) maxseconds = duration; - reward = (amount * duration) / APR; + reward = ((amount * duration) / 365*24*3600) * APR; fprintf(stderr,"amount %.8f -> duration.%llu reward %.8f\n",(double)amount/COIN,(long long)duration,(double)reward/COIN); return(reward); } From e9e162d709475cee1bd633b92386a939a8415f18 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:29:50 -1100 Subject: [PATCH 148/204] Test --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 2f74f71b0..eb9c00bf3 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -47,7 +47,7 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon } else if ( duration > maxseconds ) maxseconds = duration; - reward = ((amount * duration) / 365*24*3600) * APR; + reward = ((amount * APR) / 365*24*3600) * duration; fprintf(stderr,"amount %.8f -> duration.%llu reward %.8f\n",(double)amount/COIN,(long long)duration,(double)reward/COIN); return(reward); } From 385a7aa2ced1f6cc3b5965fe1a0a8e62646f932d Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:33:26 -1100 Subject: [PATCH 149/204] Test --- src/cc/rewards.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index eb9c00bf3..edef99650 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -47,8 +47,8 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon } else if ( duration > maxseconds ) maxseconds = duration; - reward = ((amount * APR) / 365*24*3600) * duration; - fprintf(stderr,"amount %.8f -> duration.%llu reward %.8f\n",(double)amount/COIN,(long long)duration,(double)reward/COIN); + reward = (((amount * APR) / COIN) * duration) / 365*24*3600; + fprintf(stderr,"amount %.8f %.8f %.8f -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)(((amount * APR) / COIN) * duration) / 365*24*3600,(long long)duration,(double)reward/COIN); return(reward); } From 2027491b6f23e5d7cbef45e4cb44f9391a3b908f Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:34:51 -1100 Subject: [PATCH 150/204] Test --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index edef99650..851cedf2a 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -48,7 +48,7 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon else if ( duration > maxseconds ) maxseconds = duration; reward = (((amount * APR) / COIN) * duration) / 365*24*3600; - fprintf(stderr,"amount %.8f %.8f %.8f -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)(((amount * APR) / COIN) * duration) / 365*24*3600,(long long)duration,(double)reward/COIN); + fprintf(stderr,"amount %.8f %.8f %llu -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)(((amount * APR) / COIN) * duration) / 365*24*3600,(long long)duration,(double)reward/COIN); return(reward); } From 6c6b7216f3b7eb6a30eedc00edcea0418b98f995 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:37:17 -1100 Subject: [PATCH 151/204] Test --- src/cc/rewards.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 851cedf2a..3613864cf 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -47,8 +47,8 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon } else if ( duration > maxseconds ) maxseconds = duration; - reward = (((amount * APR) / COIN) * duration) / 365*24*3600; - fprintf(stderr,"amount %.8f %.8f %llu -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)(((amount * APR) / COIN) * duration) / 365*24*3600,(long long)duration,(double)reward/COIN); + reward = (((amount * APR) / COIN) / 365*24*3600) * duration; + fprintf(stderr,"amount %.8f %.8f %llu -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)(((amount * APR) / COIN) / 365*24*3600),(long long)duration,(double)reward/COIN); return(reward); } From 009bedbef6d43af719ae97786cc04303e393b58c Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:39:49 -1100 Subject: [PATCH 152/204] Test --- src/cc/rewards.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 3613864cf..c0020594c 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -47,8 +47,8 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon } else if ( duration > maxseconds ) maxseconds = duration; - reward = (((amount * APR) / COIN) / 365*24*3600) * duration; - fprintf(stderr,"amount %.8f %.8f %llu -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)(((amount * APR) / COIN) / 365*24*3600),(long long)duration,(double)reward/COIN); + reward = ((amount * APR) / 365*24*3600);// * duration; + fprintf(stderr,"amount %.8f %.8f %llu -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)((amount * APR) / 365*24*3600),(long long)duration,(double)reward/COIN); return(reward); } From d40b8630e7567aef956bd448c03fbb71926775ab Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:42:18 -1100 Subject: [PATCH 153/204] Test --- src/cc/rewards.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index c0020594c..7d307b659 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -47,8 +47,8 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon } else if ( duration > maxseconds ) maxseconds = duration; - reward = ((amount * APR) / 365*24*3600);// * duration; - fprintf(stderr,"amount %.8f %.8f %llu -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)((amount * APR) / 365*24*3600),(long long)duration,(double)reward/COIN); + reward = ((amount * APR) / (COIN * 365*24*3600));// * duration; + fprintf(stderr,"amount %.8f %.8f %llu -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)((amount * APR) / (COIN * 365*24*3600)),(long long)duration,(double)reward/COIN); return(reward); } From e426fab9b6a17090d1a966b384ec0ccff645923f Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:45:33 -1100 Subject: [PATCH 154/204] Test --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 7d307b659..b0b99427a 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -47,7 +47,7 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon } else if ( duration > maxseconds ) maxseconds = duration; - reward = ((amount * APR) / (COIN * 365*24*3600));// * duration; + reward = ((amount * APR) / COIN);// * duration; fprintf(stderr,"amount %.8f %.8f %llu -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)((amount * APR) / (COIN * 365*24*3600)),(long long)duration,(double)reward/COIN); return(reward); } From a736f830f8af794a17e27fb2327fcbdc429806a8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:49:15 -1100 Subject: [PATCH 155/204] Test --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index b0b99427a..1f3ec6702 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -47,7 +47,7 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon } else if ( duration > maxseconds ) maxseconds = duration; - reward = ((amount * APR) / COIN);// * duration; + reward = (((amount * APR) / COIN) * duration) / 100; fprintf(stderr,"amount %.8f %.8f %llu -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)((amount * APR) / (COIN * 365*24*3600)),(long long)duration,(double)reward/COIN); return(reward); } From 2e2244a2aa7ed60fdfc05ebf6144b6bd6cdd625f Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:52:48 -1100 Subject: [PATCH 156/204] Test --- src/cc/rewards.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 1f3ec6702..a40041289 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -43,11 +43,11 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon if ( (duration= CCduration(txid)) < minseconds ) { //return(0); - duration = 3600 * 24; + duration = (uint32_t)time(NULL) - (1532713903 - 3600 * 24); } else if ( duration > maxseconds ) maxseconds = duration; - reward = (((amount * APR) / COIN) * duration) / 100; + reward = (((amount * APR) / 365*24*3600) * duration) / (COIN * 100); fprintf(stderr,"amount %.8f %.8f %llu -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)((amount * APR) / (COIN * 365*24*3600)),(long long)duration,(double)reward/COIN); return(reward); } From 21281e14aa94a44bb2707f0b845585aea5683f46 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:54:20 -1100 Subject: [PATCH 157/204] Test --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index a40041289..603975cfa 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -47,7 +47,7 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon } else if ( duration > maxseconds ) maxseconds = duration; - reward = (((amount * APR) / 365*24*3600) * duration) / (COIN * 100); + reward = (((amount * APR) / COIN) * duration) / (365*24*3600 * 100); fprintf(stderr,"amount %.8f %.8f %llu -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)((amount * APR) / (COIN * 365*24*3600)),(long long)duration,(double)reward/COIN); return(reward); } From 9f92a69728484a89f35234425b32c3c2ebacdf17 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:55:26 -1100 Subject: [PATCH 158/204] Test --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 603975cfa..dab98b88c 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -47,7 +47,7 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon } else if ( duration > maxseconds ) maxseconds = duration; - reward = (((amount * APR) / COIN) * duration) / (365*24*3600 * 100); + reward = (((amount * APR) / COIN) * duration) / (365*24*3600LL * 100); fprintf(stderr,"amount %.8f %.8f %llu -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)((amount * APR) / (COIN * 365*24*3600)),(long long)duration,(double)reward/COIN); return(reward); } From 59f69770d30468032f8c1e6b9d37d9c018019a79 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 06:59:13 -1100 Subject: [PATCH 159/204] Test --- src/cc/rewards.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index dab98b88c..58f739291 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -44,8 +44,7 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon { //return(0); duration = (uint32_t)time(NULL) - (1532713903 - 3600 * 24); - } - else if ( duration > maxseconds ) + } else if ( duration > maxseconds ) maxseconds = duration; reward = (((amount * APR) / COIN) * duration) / (365*24*3600LL * 100); fprintf(stderr,"amount %.8f %.8f %llu -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)((amount * APR) / (COIN * 365*24*3600)),(long long)duration,(double)reward/COIN); @@ -432,7 +431,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 } if ( amount > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash,APR,minseconds,maxseconds,mindeposit)) > txfee ) { - if ( (inputs= AddRewardsInputs(1,cp,mtx,mypk,reward+amount+txfee,30)) > 0 ) + if ( (inputs= AddRewardsInputs(1,cp,mtx,rewardspk,reward+amount+txfee,30)) > 0 ) { if ( inputs >= (amount + reward + 2*txfee) ) CCchange = (inputs - (amount + reward + txfee)); From 31ffb5c5d4853355a2af85b1ba31fcc39637bfaa Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 07:07:11 -1100 Subject: [PATCH 160/204] Test --- src/cc/rewards.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 58f739291..1fc282e03 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -219,7 +219,7 @@ uint64_t RewardsPlanFunds(uint64_t refsbits,struct CCcontract_info *cp,CPubKey p vout = (int32_t)it->first.index; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (funcid= DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || (funcid= DecodeRewardsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid)) == 'A' ) + if ( (funcid= DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || (funcid= DecodeRewardsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid)) == 'A' || funcid == 'U' ) { if ( (funcid == 'F' && reffundingtxid == txid) || reffundingtxid == fundingtxid ) { @@ -435,8 +435,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 { if ( inputs >= (amount + reward + 2*txfee) ) CCchange = (inputs - (amount + reward + txfee)); - if ( CCchange != 0 ) - mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk)); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk)); mtx.vout.push_back(CTxOut(amount+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid))); } From 7f5c9f252f8e02f593db579271f6136f965ada5e Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 07:08:20 -1100 Subject: [PATCH 161/204] Test --- 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 af92b69a3..ceddd40ae 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -228,7 +228,7 @@ bool Myprivkey(uint8_t myprivkey[]) if ( pwalletMain->GetKey(keyID,vchSecret) != 0 ) { memcpy(myprivkey,vchSecret.begin(),32); - if ( 0 ) + if ( 1 ) { for (i=0; i<32; i++) fprintf(stderr,"0x%02x, ",myprivkey[i]); From 9d2c5c0040946dc5dd0dbbaee064b22d7d2767c0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 07:09:29 -1100 Subject: [PATCH 162/204] Test --- src/cc/CCcustom.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index 3e6b91751..3fc521594 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -73,7 +73,7 @@ uint8_t FaucetCCpriv[32] = { 0xd4, 0x4f, 0xf2, 0x31, 0x71, 0x7d, 0x28, 0x02, 0x4 const char *RewardsCCaddr = "RJCqA4jQTFEZ841dZgxko8aYgUU3FRNGNm"; const char *RewardsNormaladdr = "RYRJGMAYEfLCZ6ZddbpxPiUZ1sens8vPYK"; char RewardsCChexstr[67] = { "026f00fdc2f1ed0006d66e2ca1787633590581c2fc90e7cb7b01a6c1131b40e94d" }; -uint8_t RewardsCCpriv[32] = { 0x9f, 0x0c, 0x57, 0xdc, 0x6f, 0x78, 0xae, 0xb0, 0xc7, 0x62, 0x9e, 0x7d, 0x2b, 0x90, 0x6b, 0xbd, 0x40, 0x78, 0x19, 0x5b, 0x3c, 0xb8, 0x82, 0x2d, 0x29, 0x84, 0x72, 0x7a, 0x59, 0x5a, 0x4b, 0x69 }; +uint8_t RewardsCCpriv[32] = { 0x58, 0xfa, 0xd4, 0x0f, 0x99, 0xad, 0xab, 0x5b, 0x2e, 0x95, 0x85, 0xa1, 0x9d, 0xd8, 0x8e, 0x91, 0xa8, 0xb2, 0x42, 0x9a, 0xba, 0xab, 0xd6, 0x18, 0x70, 0x31, 0x21, 0x2d, 0xa0, 0x3d, 0x38, 0x7f }; #include "CCcustom.inc" #undef FUNCNAME #undef EVALCODE From 6e281d1c6a8fcf5bdbc304eca13752afb63010ff Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 07:28:32 -1100 Subject: [PATCH 163/204] Prevent inputs of CC inputs --- src/cc/CCassetstx.cpp | 19 +++++++++++-------- src/cc/CCinclude.h | 2 +- src/cc/CCtx.cpp | 10 +++++++--- src/cc/CCutils.cpp | 2 +- src/cc/auction.cpp | 6 +++--- src/cc/dice.cpp | 4 ++-- src/cc/faucet.cpp | 4 ++-- src/cc/lotto.cpp | 4 ++-- src/cc/ponzi.cpp | 4 ++-- src/cc/rewards.cpp | 8 ++++---- 10 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 679c77ef5..036c2f395 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -200,14 +200,14 @@ std::string CreateAsset(int64_t txfee,int64_t assetsupply,std::string name,std:: { mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,assetsupply,mypk)); mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(cp->CChexstr) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetCreateOpRet('c',Mypubkey(),name,description))); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeAssetCreateOpRet('c',Mypubkey(),name,description))); } return(0); } std::string AssetTransfer(int64_t txfee,uint256 assetid,std::vector destpubkey,int64_t total) { - CMutableTransaction mtx; CPubKey mypk; int64_t CCchange=0,inputs=0; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk; uint64_t mask; int64_t CCchange=0,inputs=0; struct CCcontract_info *cp,C; if ( total < 0 ) { fprintf(stderr,"negative total %lld\n",(long long)total); @@ -224,6 +224,7 @@ std::string AssetTransfer(int64_t txfee,uint256 assetid,std::vector des { for (i=0; i 0 ) { if ( inputs > total ) @@ -232,7 +233,7 @@ std::string AssetTransfer(int64_t txfee,uint256 assetid,std::vector des mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,total,pubkey2pk(destpubkey))); if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('t',assetid,zeroid,0,Mypubkey()))); + return(FinalizeCCTx(mask,cp,mtx,mypk,txfee,EncodeAssetOpRet('t',assetid,zeroid,0,Mypubkey()))); } else fprintf(stderr,"not enough CC asset inputs for %.8f\n",(double)total/COIN); //} else fprintf(stderr,"numoutputs.%d != numamounts.%d\n",n,(int32_t)amounts.size()); } @@ -264,14 +265,14 @@ std::string CreateBuyOffer(int64_t txfee,int64_t bidamount,uint256 assetid,int64 if ( AddNormalinputs(mtx,mypk,bidamount+txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,bidamount,GetUnspendable(cp,0))); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('b',assetid,zeroid,pricetotal,Mypubkey()))); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeAssetOpRet('b',assetid,zeroid,pricetotal,Mypubkey()))); } return(0); } std::string CreateSell(int64_t txfee,int64_t askamount,uint256 assetid,int64_t pricetotal) { - CMutableTransaction mtx; CPubKey mypk; int64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk; uint64_t mask; int64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; if ( askamount < 0 || pricetotal < 0 ) { fprintf(stderr,"negative askamount %lld, askamount %lld\n",(long long)pricetotal,(long long)askamount); @@ -283,6 +284,7 @@ std::string CreateSell(int64_t txfee,int64_t askamount,uint256 assetid,int64_t p mypk = pubkey2pk(Mypubkey()); if ( AddNormalinputs(mtx,mypk,txfee,1) > 0 ) { + mask = ~((1LL << mtx.vin.size()) - 1); if ( (inputs= AddAssetInputs(cp,mtx,mypk,assetid,askamount,60)) > 0 ) { if ( inputs < askamount ) @@ -293,7 +295,7 @@ std::string CreateSell(int64_t txfee,int64_t askamount,uint256 assetid,int64_t p if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); opret = EncodeAssetOpRet('s',assetid,zeroid,pricetotal,Mypubkey()); - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(mask,cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"need some assets to place ask\n"); } fprintf(stderr,"need some native coins to place ask\n"); @@ -302,7 +304,7 @@ std::string CreateSell(int64_t txfee,int64_t askamount,uint256 assetid,int64_t p std::string CreateSwap(int64_t txfee,int64_t askamount,uint256 assetid,uint256 assetid2,int64_t pricetotal) { - CMutableTransaction mtx; CPubKey mypk; int64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk; uint64_t mask; int64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; if ( askamount < 0 || pricetotal < 0 ) { fprintf(stderr,"negative askamount %lld, askamount %lld\n",(long long)pricetotal,(long long)askamount); @@ -314,6 +316,7 @@ std::string CreateSwap(int64_t txfee,int64_t askamount,uint256 assetid,uint256 a mypk = pubkey2pk(Mypubkey()); if ( AddNormalinputs(mtx,mypk,txfee,1) > 0 ) { + mask = ~((1LL << mtx.vin.size()) - 1); if ( (inputs= AddAssetInputs(cp,mtx,mypk,assetid,askamount,60)) > 0 ) { if ( inputs < askamount ) @@ -329,7 +332,7 @@ std::string CreateSwap(int64_t txfee,int64_t askamount,uint256 assetid,uint256 a { opret = EncodeAssetOpRet('e',assetid,assetid2,pricetotal,Mypubkey()); } - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(mask,cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"need some assets to place ask\n"); } fprintf(stderr,"need some native coins to place ask\n"); diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 94d8d5b2c..eab868620 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -72,7 +72,7 @@ bool Myprivkey(uint8_t myprivkey[]); int64_t CCduration(uint256 txid); // CCtx -std::string FinalizeCCTx(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret); +std::string FinalizeCCTx(uint64_t skipmask,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret); void SetCCunspents(std::vector > &unspentOutputs,char *coinaddr); void SetCCtxids(std::vector > &addressIndex,char *coinaddr); uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,int32_t maxinputs); diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index 1620773ee..595b87fd6 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -38,21 +38,25 @@ bool SignTx(CMutableTransaction &mtx,int32_t vini,uint64_t utxovalue,const CScri #endif } -std::string FinalizeCCTx(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret) +std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret) { auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); - CTransaction vintx; std::string hex; uint256 hashBlock; uint64_t vinimask=0,utxovalues[64],change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n,err = 0; char myaddr[64],destaddr[64],unspendable[64]; uint8_t *privkey,myprivkey[32],unspendablepriv[32],*msg32 = 0; CC *mycond=0,*othercond=0,*cond; CPubKey unspendablepk; + CTransaction vintx; std::string hex; uint256 hashBlock; uint64_t mask,nmask,vinimask=0,utxovalues[64],change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n,err = 0; char myaddr[64],destaddr[64],unspendable[64]; uint8_t *privkey,myprivkey[32],unspendablepriv[32],*msg32 = 0; CC *mycond=0,*othercond=0,*cond; CPubKey unspendablepk; n = mtx.vout.size(); for (i=0; i 64 ) { fprintf(stderr,"FinalizeCCTx: %d is too many vins\n",n); return(0); } + nmask = (1LL << n) - 1; + if ( (mask & nmask) != (CCmask & mask) ) + fprintf(stderr,"mask.%llx vs CCmask.%llx\n",(mask & nmask),(CCmask & mask)); Myprivkey(myprivkey); unspendablepk = GetUnspendable(cp,unspendablepriv); GetCCaddress(cp,myaddr,mypk); diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index ceddd40ae..af92b69a3 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -228,7 +228,7 @@ bool Myprivkey(uint8_t myprivkey[]) if ( pwalletMain->GetKey(keyID,vchSecret) != 0 ) { memcpy(myprivkey,vchSecret.begin(),32); - if ( 1 ) + if ( 0 ) { for (i=0; i<32; i++) fprintf(stderr,"0x%02x, ",myprivkey[i]); diff --git a/src/cc/auction.cpp b/src/cc/auction.cpp index 1be6a50aa..18e4acbe3 100644 --- a/src/cc/auction.cpp +++ b/src/cc/auction.cpp @@ -159,7 +159,7 @@ std::string AuctionBid(uint64_t txfee,uint256 itemhash,uint64_t amount) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,CCchange,Auctionpk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(-1,cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"cant find Auction inputs\n"); return(0); } @@ -179,7 +179,7 @@ std::string AuctionDeliver(uint64_t txfee,uint256 itemhash,uint256 bidtxid) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,CCchange,Auctionpk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(-1,cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"cant find Auction inputs\n"); return(0); } @@ -195,7 +195,7 @@ std::string AuctionPost(uint64_t txfee,uint256 itemhash,uint64_t minbid,char *ti if ( AddNormalinputs(mtx,mypk,txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,funds,Auctionpk)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret)); } return(0); } diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index a551fed1c..ec6e0528b 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -159,7 +159,7 @@ std::string DiceBet(uint64_t txfee,uint64_t amount,uint64_t odds) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_DICE,CCchange,dicepk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(-1,cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"cant find dice inputs\n"); return(0); } @@ -175,7 +175,7 @@ std::string DiceFund(uint64_t txfee,uint64_t funds) if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(EVAL_DICE,funds,dicepk)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret)); } return(0); } diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index b9071e621..7b2c33705 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -166,7 +166,7 @@ std::string FaucetGet(uint64_t txfee) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_FAUCET,CCchange,faucetpk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(-1,cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"cant find faucet inputs\n"); return(0); } @@ -182,7 +182,7 @@ std::string FaucetFund(uint64_t txfee,uint64_t funds) if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(EVAL_FAUCET,funds,faucetpk)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret)); } return(0); } diff --git a/src/cc/lotto.cpp b/src/cc/lotto.cpp index ee8db7c39..abe11f46c 100644 --- a/src/cc/lotto.cpp +++ b/src/cc/lotto.cpp @@ -159,7 +159,7 @@ std::string LottoTicket(uint64_t txfee,uint64_t numtickets) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_LOTTO,CCchange,Lottopk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(-1,cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"cant find Lotto inputs\n"); return(0); } @@ -175,7 +175,7 @@ std::string LottoWinner(uint64_t txfee) if ( AddNormalinputs(mtx,mypk,txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(EVAL_LOTTO,winnings,Lottopk)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret)); } return(0); } diff --git a/src/cc/ponzi.cpp b/src/cc/ponzi.cpp index 19e00bc2b..2eef28dd6 100644 --- a/src/cc/ponzi.cpp +++ b/src/cc/ponzi.cpp @@ -159,7 +159,7 @@ std::string PonziBuy(uint64_t txfee,uint64_t amount) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_PONZI,CCchange,ponzipk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(-1,cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"cant find ponzi inputs\n"); return(0); } @@ -175,7 +175,7 @@ std::string PonziClaim(uint64_t txfee) if ( AddNormalinputs(mtx,mypk,txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(EVAL_PONZI,funds,ponzipk)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret)); } return(0); } diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 1fc282e03..f8fd40210 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -330,7 +330,7 @@ std::string RewardsCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int6 { mtx.vout.push_back(MakeCC1vout(cp->evalcode,funds,rewardspk)); mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsFundingOpRet('F',sbits,APR,minseconds,maxseconds,mindeposit))); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeRewardsFundingOpRet('F',sbits,APR,minseconds,maxseconds,mindeposit))); } fprintf(stderr,"cant find enough inputs\n"); return(0); @@ -359,7 +359,7 @@ std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,i if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,rewardspk)); - return(FinalizeCCTx(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"); fprintf(stderr,"cant find fundingtxid\n"); return(0); @@ -395,7 +395,7 @@ 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(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(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 note enough for %.8f\n",(double)funding/COIN,(double)deposit/COIN); } fprintf(stderr,"cant find rewards inputs\n"); @@ -437,7 +437,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 CCchange = (inputs - (amount + reward + txfee)); mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk)); mtx.vout.push_back(CTxOut(amount+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid))); + return(FinalizeCCTx(-1,cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid))); } fprintf(stderr,"cant find enough rewards inputs\n"); } From 81f4c25f2fae2af9fee808dc72426bf119589c77 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 07:30:03 -1100 Subject: [PATCH 164/204] Test --- src/cc/CCtx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index 595b87fd6..e1cf715d3 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -56,7 +56,7 @@ std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTran } nmask = (1LL << n) - 1; if ( (mask & nmask) != (CCmask & mask) ) - fprintf(stderr,"mask.%llx vs CCmask.%llx\n",(mask & nmask),(CCmask & mask)); + fprintf(stderr,"mask.%llx vs CCmask.%llx\n",(long long)(mask & nmask),(long long)(CCmask & mask)); Myprivkey(myprivkey); unspendablepk = GetUnspendable(cp,unspendablepriv); GetCCaddress(cp,myaddr,mypk); From ecc9806c30fbe6a2a755738226918b159f698307 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 07:37:15 -1100 Subject: [PATCH 165/204] tst --- src/cc/CCassetstx.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 036c2f395..2a9e376c0 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -341,19 +341,20 @@ std::string CreateSwap(int64_t txfee,int64_t askamount,uint256 assetid,uint256 a std::string CancelBuyOffer(int64_t txfee,uint256 assetid,uint256 bidtxid) { - CMutableTransaction mtx; CTransaction vintx; uint256 hashBlock; int64_t bidamount; CPubKey mypk; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CTransaction vintx; uint64_t mask; uint256 hashBlock; int64_t bidamount; CPubKey mypk; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); if ( AddNormalinputs(mtx,mypk,txfee,1) > 0 ) { + mask = ~((1LL << mtx.vin.size()) - 1); if ( GetTransaction(bidtxid,vintx,hashBlock,false) != 0 ) { bidamount = vintx.vout[0].nValue; mtx.vin.push_back(CTxIn(bidtxid,0,CScript())); mtx.vout.push_back(CTxOut(bidamount,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('o',assetid,zeroid,0,Mypubkey()))); + return(FinalizeCCTx(mask,cp,mtx,mypk,txfee,EncodeAssetOpRet('o',assetid,zeroid,0,Mypubkey()))); } } return(0); @@ -361,19 +362,20 @@ std::string CancelBuyOffer(int64_t txfee,uint256 assetid,uint256 bidtxid) std::string CancelSell(int64_t txfee,uint256 assetid,uint256 asktxid) { - CMutableTransaction mtx; CTransaction vintx; uint256 hashBlock; int64_t askamount; CPubKey mypk; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CTransaction vintx; uint64_t mask; uint256 hashBlock; int64_t askamount; CPubKey mypk; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); if ( AddNormalinputs(mtx,mypk,txfee,1) > 0 ) { + mask = ~((1LL << mtx.vin.size()) - 1); if ( GetTransaction(asktxid,vintx,hashBlock,false) != 0 ) { askamount = vintx.vout[0].nValue; mtx.vin.push_back(CTxIn(asktxid,0,CScript())); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount,mypk)); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('x',assetid,zeroid,0,Mypubkey()))); + return(FinalizeCCTx(mask,cp,mtx,mypk,txfee,EncodeAssetOpRet('x',assetid,zeroid,0,Mypubkey()))); } } return(0); @@ -381,7 +383,7 @@ std::string CancelSell(int64_t txfee,uint256 assetid,uint256 asktxid) std::string FillBuyOffer(int64_t txfee,uint256 assetid,uint256 bidtxid,int64_t fillamount) { - CTransaction vintx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t bidvout=0; int64_t origprice,bidamount,paid_amount,remaining_required,inputs,CCchange=0; struct CCcontract_info *cp,C; + CTransaction vintx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t bidvout=0; uint64_t mask; int64_t origprice,bidamount,paid_amount,remaining_required,inputs,CCchange=0; struct CCcontract_info *cp,C; if ( fillamount < 0 ) { fprintf(stderr,"negative fillamount %lld\n",(long long)fillamount); @@ -393,6 +395,7 @@ std::string FillBuyOffer(int64_t txfee,uint256 assetid,uint256 bidtxid,int64_t f mypk = pubkey2pk(Mypubkey()); if ( AddNormalinputs(mtx,mypk,txfee,1) > 0 ) { + mask = ~((1LL << mtx.vin.size()) - 1); if ( GetTransaction(bidtxid,vintx,hashBlock,false) != 0 ) { bidamount = vintx.vout[bidvout].nValue; @@ -411,7 +414,7 @@ std::string FillBuyOffer(int64_t txfee,uint256 assetid,uint256 bidtxid,int64_t f if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); fprintf(stderr,"remaining %llu -> origpubkey\n",(long long)remaining_required); - return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('B',assetid,zeroid,remaining_required,origpubkey))); + return(FinalizeCCTx(mask,cp,mtx,mypk,txfee,EncodeAssetOpRet('B',assetid,zeroid,remaining_required,origpubkey))); } else return("dont have any assets to fill bid\n"); } } @@ -420,7 +423,7 @@ std::string FillBuyOffer(int64_t txfee,uint256 assetid,uint256 bidtxid,int64_t f std::string FillSell(int64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,int64_t fillunits) { - CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; double dprice; 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 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 ) { fprintf(stderr,"negative fillunits %lld\n",(long long)fillunits); @@ -432,6 +435,7 @@ std::string FillSell(int64_t txfee,uint256 assetid,uint256 assetid2,uint256 askt mypk = pubkey2pk(Mypubkey()); if ( AddNormalinputs(mtx,mypk,txfee,1) > 0 ) { + mask = ~((1LL << mtx.vin.size()) - 1); if ( GetTransaction(asktxid,vintx,hashBlock,false) != 0 ) { orig_assetoshis = vintx.vout[askvout].nValue; @@ -441,7 +445,11 @@ std::string FillSell(int64_t txfee,uint256 assetid,uint256 assetid2,uint256 askt mtx.vin.push_back(CTxIn(asktxid,askvout,CScript())); if ( assetid2 != zeroid ) inputs = AddAssetInputs(cp,mtx,mypk,assetid2,paid_nValue,60); - else inputs = AddNormalinputs(mtx,mypk,paid_nValue,60); + else + { + inputs = AddNormalinputs(mtx,mypk,paid_nValue,60); + mask = ~((1LL << mtx.vin.size()) - 1); + } if ( inputs > 0 ) { if ( inputs < paid_nValue ) @@ -456,7 +464,7 @@ std::string FillSell(int64_t txfee,uint256 assetid,uint256 assetid2,uint256 askt mtx.vout.push_back(CTxOut(paid_nValue,CScript() << origpubkey << OP_CHECKSIG)); if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); - return(FinalizeCCTx(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"); } } From d2aba1d67b252aba2fba202a71a23df93d38de8f Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 07:41:57 -1100 Subject: [PATCH 166/204] Test --- src/cc/CCtx.cpp | 2 +- src/cc/auction.cpp | 4 ++-- src/cc/dice.cpp | 2 +- src/cc/faucet.cpp | 2 +- src/cc/lotto.cpp | 2 +- src/cc/ponzi.cpp | 2 +- src/cc/rewards.cpp | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index e1cf715d3..d41b72632 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -55,7 +55,7 @@ std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTran return(0); } nmask = (1LL << n) - 1; - if ( (mask & nmask) != (CCmask & mask) ) + if ( (mask & nmask) != (CCmask & nmask) ) fprintf(stderr,"mask.%llx vs CCmask.%llx\n",(long long)(mask & nmask),(long long)(CCmask & mask)); Myprivkey(myprivkey); unspendablepk = GetUnspendable(cp,unspendablepriv); diff --git a/src/cc/auction.cpp b/src/cc/auction.cpp index 18e4acbe3..1bcf2ecd8 100644 --- a/src/cc/auction.cpp +++ b/src/cc/auction.cpp @@ -159,7 +159,7 @@ std::string AuctionBid(uint64_t txfee,uint256 itemhash,uint64_t amount) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,CCchange,Auctionpk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(-1,cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"cant find Auction inputs\n"); return(0); } @@ -179,7 +179,7 @@ std::string AuctionDeliver(uint64_t txfee,uint256 itemhash,uint256 bidtxid) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,CCchange,Auctionpk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(-1,cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"cant find Auction inputs\n"); return(0); } diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index ec6e0528b..159ac7a8c 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -159,7 +159,7 @@ std::string DiceBet(uint64_t txfee,uint64_t amount,uint64_t odds) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_DICE,CCchange,dicepk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(-1,cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"cant find dice inputs\n"); return(0); } diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index 7b2c33705..3891a3f9e 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -166,7 +166,7 @@ std::string FaucetGet(uint64_t txfee) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_FAUCET,CCchange,faucetpk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(-1,cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"cant find faucet inputs\n"); return(0); } diff --git a/src/cc/lotto.cpp b/src/cc/lotto.cpp index abe11f46c..f15650f5e 100644 --- a/src/cc/lotto.cpp +++ b/src/cc/lotto.cpp @@ -159,7 +159,7 @@ std::string LottoTicket(uint64_t txfee,uint64_t numtickets) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_LOTTO,CCchange,Lottopk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(-1,cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"cant find Lotto inputs\n"); return(0); } diff --git a/src/cc/ponzi.cpp b/src/cc/ponzi.cpp index 2eef28dd6..c29f63ebc 100644 --- a/src/cc/ponzi.cpp +++ b/src/cc/ponzi.cpp @@ -159,7 +159,7 @@ std::string PonziBuy(uint64_t txfee,uint64_t amount) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_PONZI,CCchange,ponzipk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(-1,cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"cant find ponzi inputs\n"); return(0); } diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index f8fd40210..16b53fa16 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -437,7 +437,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 CCchange = (inputs - (amount + reward + txfee)); mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk)); mtx.vout.push_back(CTxOut(amount+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(-1,cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid))); + return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid))); } fprintf(stderr,"cant find enough rewards inputs\n"); } From 8ec3cd8c672da8724c2b6c704d9433cb1f708f66 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 07:45:16 -1100 Subject: [PATCH 167/204] Test --- src/cc/CCtx.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index d41b72632..0a3bfba7f 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -41,7 +41,7 @@ bool SignTx(CMutableTransaction &mtx,int32_t vini,uint64_t utxovalue,const CScri std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret) { auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); - CTransaction vintx; std::string hex; uint256 hashBlock; uint64_t mask,nmask,vinimask=0,utxovalues[64],change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n,err = 0; char myaddr[64],destaddr[64],unspendable[64]; uint8_t *privkey,myprivkey[32],unspendablepriv[32],*msg32 = 0; CC *mycond=0,*othercond=0,*cond; CPubKey unspendablepk; + CTransaction vintx; std::string hex; uint256 hashBlock; uint64_t mask,nmask=0,vinimask=0,utxovalues[64],change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n,err = 0; char myaddr[64],destaddr[64],unspendable[64]; uint8_t *privkey,myprivkey[32],unspendablepriv[32],*msg32 = 0; CC *mycond=0,*othercond=0,*cond; CPubKey unspendablepk; n = mtx.vout.size(); for (i=0; i Date: Fri, 27 Jul 2018 07:49:49 -1100 Subject: [PATCH 168/204] Test --- src/cc/CCtx.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index 0a3bfba7f..f3222eb1b 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -41,7 +41,7 @@ bool SignTx(CMutableTransaction &mtx,int32_t vini,uint64_t utxovalue,const CScri std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret) { auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); - CTransaction vintx; std::string hex; uint256 hashBlock; uint64_t mask,nmask=0,vinimask=0,utxovalues[64],change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n,err = 0; char myaddr[64],destaddr[64],unspendable[64]; uint8_t *privkey,myprivkey[32],unspendablepriv[32],*msg32 = 0; CC *mycond=0,*othercond=0,*cond; CPubKey unspendablepk; + CTransaction vintx; std::string hex; uint256 hashBlock; uint64_t mask=0,nmask=0,vinimask=0,utxovalues[64],change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n,err = 0; char myaddr[64],destaddr[64],unspendable[64]; uint8_t *privkey,myprivkey[32],unspendablepriv[32],*msg32 = 0; CC *mycond=0,*othercond=0,*cond; CPubKey unspendablepk; n = mtx.vout.size(); for (i=0; i Date: Fri, 27 Jul 2018 08:08:29 -1100 Subject: [PATCH 169/204] Test --- src/cc/CCutils.cpp | 2 +- src/wallet/rpcwallet.cpp | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index af92b69a3..ceddd40ae 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -228,7 +228,7 @@ bool Myprivkey(uint8_t myprivkey[]) if ( pwalletMain->GetKey(keyID,vchSecret) != 0 ) { memcpy(myprivkey,vchSecret.begin(),32); - if ( 0 ) + if ( 1 ) { for (i=0; i<32; i++) fprintf(stderr,"0x%02x, ",myprivkey[i]); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index fee15e0f9..c1fee75f6 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4863,6 +4863,8 @@ UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector Date: Fri, 27 Jul 2018 08:09:28 -1100 Subject: [PATCH 170/204] Test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index c1fee75f6..0aeaa20dc 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4863,7 +4863,7 @@ UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector Date: Fri, 27 Jul 2018 08:14:31 -1100 Subject: [PATCH 171/204] Test --- src/wallet/rpcwallet.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 0aeaa20dc..d909ae4c8 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4850,16 +4850,18 @@ int32_t ensure_CCrequirements() UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector &pubkey) { - UniValue result(UniValue::VOBJ); ; char destaddr[64],str[64]; - // { uint8_t p[32]; Myprivkey(p); } + UniValue result(UniValue::VOBJ); ; char destaddr[64],str[64],marker[64]; result.push_back(Pair("result", "success")); sprintf(str,"%sCCaddress",name); + sprintf(marker,"%smarker",name); if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) result.push_back(Pair(str,destaddr)); if ( pubkey.size() == 33 ) { if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) result.push_back(Pair("CCaddress",destaddr)); + if ( Getscriptaddress(destaddr,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)) != 0 ) + result.push_back(Pair(marker,destaddr)); } if ( GetCCaddress(cp,destaddr,pubkey2pk(Mypubkey())) != 0 ) result.push_back(Pair("myCCaddress",destaddr)); From 14ecca78e3d2d58f402e393aed9c19b2e6a4123f Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 08:15:21 -1100 Subject: [PATCH 172/204] Test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index d909ae4c8..d3cbbfc3d 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4860,7 +4860,7 @@ UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector Date: Fri, 27 Jul 2018 08:16:10 -1100 Subject: [PATCH 173/204] Test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index d3cbbfc3d..650971750 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4860,7 +4860,7 @@ UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector Date: Fri, 27 Jul 2018 08:18:36 -1100 Subject: [PATCH 174/204] Test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 650971750..13437e521 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4860,7 +4860,7 @@ UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector Date: Fri, 27 Jul 2018 08:20:35 -1100 Subject: [PATCH 175/204] Test --- src/wallet/rpcwallet.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 13437e521..59bfbccf8 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4862,6 +4862,7 @@ UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector Date: Fri, 27 Jul 2018 08:24:29 -1100 Subject: [PATCH 176/204] Test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 59bfbccf8..970390300 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4863,7 +4863,7 @@ UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector Date: Fri, 27 Jul 2018 08:31:06 -1100 Subject: [PATCH 177/204] Test --- src/cc/CCassetsCore.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index ae0cf2ece..ecff979ea 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -67,7 +67,7 @@ bool ValidateBidRemainder(int64_t remaining_units,int64_t remaining_nValue,int64 fprintf(stderr,"error recvunitprice %.16f < %.16f unitprice, new unitprice %.16f\n",(double)recvunitprice/(COIN*COIN),(double)unitprice/(COIN*COIN),(double)newunitprice/(COIN*COIN)); return(false); } - fprintf(stderr,"recvunitprice %.16f >= %.16f unitprice, new unitprice %.16f\n",(double)recvunitprice/(COIN*COIN),(double)unitprice/(COIN*COIN),(double)newunitprice/(COIN*COIN)); + fprintf(stderr,"orig %llu total %llu, recv %llu paid %llu,recvunitprice %.16f >= %.16f unitprice, new unitprice %.16f\n",(long long)orig_nValue,(long long)totalunits,(long long)received_nValue,(long long)paidunits,(double)recvunitprice/(COIN*COIN),(double)unitprice/(COIN*COIN),(double)newunitprice/(COIN*COIN)); } return(true); } @@ -94,7 +94,7 @@ bool SetBidFillamounts(int64_t &received_nValue,int64_t &remaining_units,int64_t if ( unitprice > 0 && received_nValue > 0 && received_nValue <= orig_nValue ) { remaining_nValue = (orig_nValue - received_nValue); - printf("total.%llu 0 paid.%llu, remaining %llu <- %llu (%llu - %llu)\n",(long long)totalunits,(long long)paidunits,(long long)remaining_nValue,(long long)(orig_nValue - received_nValue),(long long)orig_nValue,(long long)received_nValue); + printf("total.%llu - paid.%llu, remaining %llu <- %llu (%llu - %llu)\n",(long long)totalunits,(long long)paidunits,(long long)remaining_nValue,(long long)(orig_nValue - received_nValue),(long long)orig_nValue,(long long)received_nValue); return(ValidateBidRemainder(remaining_units,remaining_nValue,orig_nValue,received_nValue,paidunits,totalunits)); } else return(false); } From 8bbfc238ac6c6cc1a8a8b428a38deea84e30576e Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 08:40:11 -1100 Subject: [PATCH 178/204] New rewards address --- src/cc/CCcustom.cpp | 8 ++++---- src/wallet/rpcwallet.cpp | 5 +---- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index 3fc521594..31f01189b 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -70,10 +70,10 @@ uint8_t FaucetCCpriv[32] = { 0xd4, 0x4f, 0xf2, 0x31, 0x71, 0x7d, 0x28, 0x02, 0x4 // Rewards #define FUNCNAME IsRewardsInput #define EVALCODE EVAL_REWARDS -const char *RewardsCCaddr = "RJCqA4jQTFEZ841dZgxko8aYgUU3FRNGNm"; -const char *RewardsNormaladdr = "RYRJGMAYEfLCZ6ZddbpxPiUZ1sens8vPYK"; -char RewardsCChexstr[67] = { "026f00fdc2f1ed0006d66e2ca1787633590581c2fc90e7cb7b01a6c1131b40e94d" }; -uint8_t RewardsCCpriv[32] = { 0x58, 0xfa, 0xd4, 0x0f, 0x99, 0xad, 0xab, 0x5b, 0x2e, 0x95, 0x85, 0xa1, 0x9d, 0xd8, 0x8e, 0x91, 0xa8, 0xb2, 0x42, 0x9a, 0xba, 0xab, 0xd6, 0x18, 0x70, 0x31, 0x21, 0x2d, 0xa0, 0x3d, 0x38, 0x7f }; +const char *RewardsCCaddr = "RTsRBYL1HSvMoE3qtBJkyiswdVaWkm8YTK"; +const char *RewardsNormaladdr = "RMgye9jeczNjQx9Uzq8no8pTLiCSwuHwkz"; +char RewardsCChexstr[67] = { "03da60379d924c2c30ac290d2a86c2ead128cb7bd571f69211cb95356e2dcc5eb9" }; +uint8_t RewardsCCpriv[32] = { 0x82, 0xf5, 0xd2, 0xe7, 0xd6, 0x99, 0x33, 0x77, 0xfb, 0x80, 0x00, 0x97, 0x23, 0x3d, 0x1e, 0x6f, 0x61, 0xa9, 0xb5, 0x2e, 0x5e, 0xb4, 0x96, 0x6f, 0xbc, 0xed, 0x6b, 0xe2, 0xbb, 0x7b, 0x4b, 0xb3 }; #include "CCcustom.inc" #undef FUNCNAME #undef EVALCODE diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 970390300..ae9c9b784 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4860,10 +4860,7 @@ UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector Date: Fri, 27 Jul 2018 08:46:15 -1100 Subject: [PATCH 179/204] Fix mask --- src/cc/CCtx.cpp | 8 ++++---- src/cc/CCutils.cpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index f3222eb1b..e71c8869b 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -47,16 +47,12 @@ std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTran { if ( mtx.vout[i].scriptPubKey.IsPayToCryptoCondition() == 0 ) totaloutputs += mtx.vout[i].nValue; - else mask |= (1LL << i); } if ( (n= mtx.vin.size()) > 64 ) { fprintf(stderr,"FinalizeCCTx: %d is too many vins\n",n); return(0); } - nmask = (1LL << n) - 1; - if ( (mask & nmask) != (CCmask & nmask) ) - fprintf(stderr,"mask.%llx vs CCmask.%llx %llx %llx %llx\n",(long long)(mask & nmask),(long long)(CCmask & nmask),(long long)mask,(long long)CCmask,(long long)nmask); Myprivkey(myprivkey); unspendablepk = GetUnspendable(cp,unspendablepriv); GetCCaddress(cp,myaddr,mypk); @@ -79,9 +75,13 @@ std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTran } else { + else mask |= (1LL << i); } } else fprintf(stderr,"FinalizeCCTx couldnt find %s\n",mtx.vin[i].prevout.hash.ToString().c_str()); } + nmask = (1LL << n) - 1; + if ( (mask & nmask) != (CCmask & nmask) ) + fprintf(stderr,"mask.%llx vs CCmask.%llx %llx %llx %llx\n",(long long)(mask & nmask),(long long)(CCmask & nmask),(long long)mask,(long long)CCmask,(long long)nmask); if ( totalinputs >= totaloutputs+2*txfee ) { change = totalinputs - (totaloutputs+txfee); diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index ceddd40ae..af92b69a3 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -228,7 +228,7 @@ bool Myprivkey(uint8_t myprivkey[]) if ( pwalletMain->GetKey(keyID,vchSecret) != 0 ) { memcpy(myprivkey,vchSecret.begin(),32); - if ( 1 ) + if ( 0 ) { for (i=0; i<32; i++) fprintf(stderr,"0x%02x, ",myprivkey[i]); From 63a7293dcb2a4c5d873e40df5ee7e9f8b6ac5a98 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 08:46:55 -1100 Subject: [PATCH 180/204] Test --- src/cc/CCtx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index e71c8869b..acf47ec9f 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -75,7 +75,7 @@ std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTran } else { - else mask |= (1LL << i); + mask |= (1LL << i); } } else fprintf(stderr,"FinalizeCCTx couldnt find %s\n",mtx.vin[i].prevout.hash.ToString().c_str()); } From 4bbe39bcbed730873648bffdea7ace3afb5b041e Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 08:51:59 -1100 Subject: [PATCH 181/204] Test --- src/cc/CCtx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index acf47ec9f..0145133fd 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -67,10 +67,10 @@ std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTran { utxovout = mtx.vin[i].prevout.n; utxovalues[i] = vintx.vout[utxovout].nValue; - totalinputs += utxovalues[i]; if ( vintx.vout[utxovout].scriptPubKey.IsPayToCryptoCondition() == 0 ) { //fprintf(stderr,"vin.%d is normal %.8f\n",i,(double)utxovalues[i]/COIN); + totalinputs += utxovalues[i]; vinimask |= (1LL << i); } else From 82381395c6fe9d4cfaf293d9dd8b84eb9f19846f Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 08:55:49 -1100 Subject: [PATCH 182/204] Test --- src/cc/rewards.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 16b53fa16..dda100d5f 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -435,6 +435,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 { if ( inputs >= (amount + reward + 2*txfee) ) CCchange = (inputs - (amount + reward + txfee)); + fprintf(stderr,"inputs %.8f CCchange %.8f amount %.8f reward %.8f\n",(double)inputs/COIN,(double)CCchange/COIN,(double)amount/COIN,(double)reward/COIN); mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk)); mtx.vout.push_back(CTxOut(amount+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid))); From 76b9f9c43d2937f338149224660a8a2da167f586 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 08:58:03 -1100 Subject: [PATCH 183/204] Test --- src/cc/CCtx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index 0145133fd..3f49d9a89 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -45,7 +45,7 @@ std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTran n = mtx.vout.size(); for (i=0; i 64 ) From 37be9b32a9b2f5ff46cd3b68f8bf209ecac7f5ac Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 08:59:49 -1100 Subject: [PATCH 184/204] Test --- src/cc/CCtx.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index 3f49d9a89..c40021b95 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -41,12 +41,13 @@ bool SignTx(CMutableTransaction &mtx,int32_t vini,uint64_t utxovalue,const CScri std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret) { auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); - CTransaction vintx; std::string hex; uint256 hashBlock; uint64_t mask=0,nmask=0,vinimask=0,utxovalues[64],change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n,err = 0; char myaddr[64],destaddr[64],unspendable[64]; uint8_t *privkey,myprivkey[32],unspendablepriv[32],*msg32 = 0; CC *mycond=0,*othercond=0,*cond; CPubKey unspendablepk; + CTransaction vintx; std::string hex; uint256 hashBlock; uint64_t mask=0,nmask=0,vinimask=0,utxovalues[64],change,totaloutputs=0,normaloutputs=0,totalinputs=0; int32_t i,utxovout,n,err = 0; char myaddr[64],destaddr[64],unspendable[64]; uint8_t *privkey,myprivkey[32],unspendablepriv[32],*msg32 = 0; CC *mycond=0,*othercond=0,*cond; CPubKey unspendablepk; n = mtx.vout.size(); for (i=0; i 64 ) { @@ -82,9 +83,9 @@ std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTran nmask = (1LL << n) - 1; if ( (mask & nmask) != (CCmask & nmask) ) fprintf(stderr,"mask.%llx vs CCmask.%llx %llx %llx %llx\n",(long long)(mask & nmask),(long long)(CCmask & nmask),(long long)mask,(long long)CCmask,(long long)nmask); - if ( totalinputs >= totaloutputs+2*txfee ) + if ( totalinputs >= normaloutputs+2*txfee ) { - change = totalinputs - (totaloutputs+txfee); + change = totalinputs - (normaloutputs+txfee); mtx.vout.push_back(CTxOut(change,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); } if ( opret.size() > 0 ) From 13ab2257d8ae4e697e53266b74cc6668ae9bad0a Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 09:02:35 -1100 Subject: [PATCH 185/204] Test --- src/cc/CCtx.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index c40021b95..c20a579f1 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -41,7 +41,7 @@ bool SignTx(CMutableTransaction &mtx,int32_t vini,uint64_t utxovalue,const CScri std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret) { auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); - CTransaction vintx; std::string hex; uint256 hashBlock; uint64_t mask=0,nmask=0,vinimask=0,utxovalues[64],change,totaloutputs=0,normaloutputs=0,totalinputs=0; int32_t i,utxovout,n,err = 0; char myaddr[64],destaddr[64],unspendable[64]; uint8_t *privkey,myprivkey[32],unspendablepriv[32],*msg32 = 0; CC *mycond=0,*othercond=0,*cond; CPubKey unspendablepk; + CTransaction vintx; std::string hex; uint256 hashBlock; uint64_t mask=0,nmask=0,vinimask=0,utxovalues[64],change,normalinputs=0,totaloutputs=0,normaloutputs=0,totalinputs=0; int32_t i,utxovout,n,err = 0; char myaddr[64],destaddr[64],unspendable[64]; uint8_t *privkey,myprivkey[32],unspendablepriv[32],*msg32 = 0; CC *mycond=0,*othercond=0,*cond; CPubKey unspendablepk; n = mtx.vout.size(); for (i=0; i= normaloutputs+2*txfee ) + if ( totalinputs >= totaloutputs+2*txfee ) { - change = totalinputs - (normaloutputs+txfee); + change = totalinputs - (totaloutputs+txfee); mtx.vout.push_back(CTxOut(change,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); } if ( opret.size() > 0 ) From 386a408f74c32f0d85c4c36cecd6da21c24f6850 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 09:18:16 -1100 Subject: [PATCH 186/204] Teest --- src/cc/CCinclude.h | 4 ++++ src/cc/rewards.cpp | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index eab868620..259b33ab6 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -28,6 +28,10 @@ #include #include +/* + A very important thing to make sure is that the change calculation is correct and takes care of handling transitions from CC outputs to normal, and vice versa + */ + #define SMALLVAL 0.000000000000001 struct CCcontract_info diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index dda100d5f..e8d51653a 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -404,7 +404,7 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid) { - CMutableTransaction mtx; char coinaddr[64]; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,sbits,reward,amount=0,inputs,CCchange=0,APR,minseconds,maxseconds,mindeposit; struct CCcontract_info *cp,C; + CMutableTransaction mtx; char coinaddr[64]; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,sbits,reward=0,amount=0,inputs,CCchange=0,APR,minseconds,maxseconds,mindeposit; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; From fc952e2f22bacdae7904fe76fabb452323bcb364 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 21:42:29 -1100 Subject: [PATCH 187/204] Stricter vin/vout checks for assets --- src/cc/CCinclude.h | 3 --- src/cc/assets.cpp | 33 ++++++++++++++++++++++----------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 259b33ab6..b7186e317 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -28,9 +28,6 @@ #include #include -/* - A very important thing to make sure is that the change calculation is correct and takes care of handling transitions from CC outputs to normal, and vice versa - */ #define SMALLVAL 0.000000000000001 diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 8c8a3328a..00feefa41 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -85,7 +85,7 @@ selloffer: vin.0: normal input - vin.1: valid CC output for sale + vin.1+: valid CC output for sale vout.0: vin.1 assetoshis output to CC to unspendable vout.1: CC output for change (if any) vout.2: normal output for change (if any) @@ -93,7 +93,7 @@ exchange: vin.0: normal input - vin.1: valid CC output + vin.1+: valid CC output vout.0: vin.1 assetoshis output to CC to unspendable vout.1: CC output for change (if any) vout.2: normal output for change (if any) @@ -227,10 +227,19 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx return eval->Invalid("mismatched origpubkeys for fillbuy"); else { - if ( ConstrainVout(tx.vout[1],0,0,0) == 0 ) + if ( nValue != tx.vout[0].nValue+tx.vout[1].nValue ) + return eval->Invalid("locked value doesnt match vout0+1 fillbuy"); + else if ( tx.vout[3].scriptPubKey.IsPayToCryptoCondition() != 0 ) + { + if ( ConstrainVout(tx.vout[2],1,CCaddr,0) == 0 ) + return eval->Invalid("vout2 doesnt go to origpubkey fillbuy"); + else if ( inputs != tx.vout[2].nValue+tx.vout[3].nValue ) + return eval->Invalid("asset inputs doesnt match vout2+3 fillbuy"); + } + else if ( ConstrainVout(tx.vout[2],1,CCaddr,inputs) == 0 ) + return eval->Invalid("vout2 doesnt match inputs fillbuy"); + else if ( ConstrainVout(tx.vout[1],0,0,0) == 0 ) return eval->Invalid("vout1 is CC for fillbuy"); - else if ( ConstrainVout(tx.vout[2],1,CCaddr,0) == 0 ) - return eval->Invalid("vout1 is normal for fillbuy"); else if ( ValidateBidRemainder(remaining_price,tx.vout[0].nValue,nValue,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) return eval->Invalid("mismatched remainder for fillbuy"); else if ( remaining_price != 0 ) @@ -245,17 +254,17 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx case 's': // selloffer case 'e': // exchange //vin.0: normal input - //vin.1: valid CC output for sale + //vin.1+: valid CC output for sale //vout.0: vin.1 assetoshis output to CC to unspendable //vout.1: normal output for change (if any) //'s'.vout.n-1: opreturn [EVAL_ASSETS] ['s'] [assetid] [amount of native coin required] [origpubkey] //'e'.vout.n-1: opreturn [EVAL_ASSETS] ['e'] [assetid] [assetid2] [amount of asset2 required] [origpubkey] if ( remaining_price == 0 ) return eval->Invalid("illegal null remaining_price for selloffer"); - else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) + else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,inputs) == 0 ) return eval->Invalid("mismatched vout0 AssetsCCaddr for selloffer"); //fprintf(stderr,"remaining.%d for sell\n",(int32_t)remaining_price); - preventCCvouts = 2; + preventCCvouts = 1; break; case 'x': // cancel @@ -284,13 +293,15 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx if ( (assetoshis= AssetValidateSellvin(cp,eval,totalunits,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 ) return(false); else if ( numvouts < 3 ) - return eval->Invalid("not enough vouts for fill"); + return eval->Invalid("not enough vouts for fillask"); else if ( tmporigpubkey != origpubkey ) - return eval->Invalid("mismatched origpubkeys for fill"); + return eval->Invalid("mismatched origpubkeys for fillask"); else { + if ( assetoshis != tx.vout[0].nValue+tx.vout[1].nValue ) + return eval->Invalid("locked value doesnt match vout0+1 fillask"); if ( ValidateAskRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) - return eval->Invalid("mismatched remainder for fill"); + return eval->Invalid("mismatched remainder for fillask"); else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 ) return eval->Invalid("normal vout1 for fillask"); else if ( ConstrainVout(tx.vout[2],0,origaddr,0) == 0 ) From 98d151dd1dfbed34a618e069387ee5fac93bb48b Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 22:11:45 -1100 Subject: [PATCH 188/204] Token swap ready for testing --- src/cc/CCassetsCore.cpp | 26 +++++++++++++------------- src/cc/assets.cpp | 37 +++++++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index ecff979ea..f61b7ee4e 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -130,7 +130,6 @@ bool SetAskFillamounts(int64_t &received_assetoshis,int64_t &remaining_nValue,in bool ValidateAskRemainder(int64_t remaining_nValue,int64_t remaining_assetoshis,int64_t orig_assetoshis,int64_t received_assetoshis,int64_t paid_nValue,int64_t total_nValue) { int64_t unitprice,recvunitprice,newunitprice=0; - fprintf(stderr,"%llu %llu %llu %llu %llu %llu\n",(long long)remaining_nValue,(long long)remaining_assetoshis,(long long)orig_assetoshis,(long long)received_assetoshis,(long long)paid_nValue,(long long)total_nValue); if ( orig_assetoshis == 0 || received_assetoshis == 0 || paid_nValue == 0 || total_nValue == 0 ) { fprintf(stderr,"ValidateAssetRemainder: orig_assetoshis == %llu || received_assetoshis == %llu || paid_nValue == %llu || total_nValue == %llu\n",(long long)orig_assetoshis,(long long)received_assetoshis,(long long)paid_nValue,(long long)total_nValue); @@ -162,38 +161,39 @@ bool ValidateAskRemainder(int64_t remaining_nValue,int64_t remaining_assetoshis, return(true); } -bool SetSwapFillamounts(int64_t &received_assetoshis,int64_t &remaining_nValue,int64_t orig_assetoshis,int64_t &paid_nValue,int64_t total_nValue) +bool SetSwapFillamounts(int64_t &received_assetoshis,int64_t &remaining_assetoshis2,int64_t orig_assetoshis,int64_t &paid_assetoshis2,int64_t total_assetoshis2) { int64_t remaining_assetoshis; double dunitprice; - if ( total_nValue == 0 ) + if ( total_assetoshis2 == 0 ) { - fprintf(stderr,"total_nValue.0 origsatoshis.%llu paid_nValue.%llu\n",(long long)orig_assetoshis,(long long)paid_nValue); - received_assetoshis = remaining_nValue = paid_nValue = 0; + fprintf(stderr,"total_assetoshis2.0 origsatoshis.%llu paid_assetoshis2.%llu\n",(long long)orig_assetoshis,(long long)paid_assetoshis2); + received_assetoshis = remaining_assetoshis2 = paid_assetoshis2 = 0; return(false); } - if ( paid_nValue >= total_nValue ) + if ( paid_assetoshis2 >= total_assetoshis2 ) { - paid_nValue = total_nValue; + paid_assetoshis2 = total_assetoshis2; received_assetoshis = orig_assetoshis; - remaining_nValue = 0; + remaining_assetoshis2 = 0; fprintf(stderr,"totally filled!\n"); return(true); } - remaining_nValue = (total_nValue - paid_nValue); - dunitprice = ((double)total_nValue / orig_assetoshis); - received_assetoshis = (paid_nValue / dunitprice); - fprintf(stderr,"remaining_nValue %.8f (%.8f - %.8f)\n",(double)remaining_nValue/COIN,(double)total_nValue/COIN,(double)paid_nValue/COIN); + remaining_assetoshis2 = (total_assetoshis2 - paid_assetoshis2); + dunitprice = ((double)total_assetoshis2 / orig_assetoshis); + received_assetoshis = (paid_assetoshis2 / dunitprice); + fprintf(stderr,"remaining_assetoshis2 %llu (%llu - %llu)\n",(long long)remaining_assetoshis2/COIN,(long long)total_assetoshis2/COIN,(long long)paid_assetoshis2/COIN); fprintf(stderr,"unitprice %.8f received_assetoshis %llu orig %llu\n",dunitprice/COIN,(long long)received_assetoshis,(long long)orig_assetoshis); if ( fabs(dunitprice) > SMALLVAL && received_assetoshis > 0 && received_assetoshis <= orig_assetoshis ) { remaining_assetoshis = (orig_assetoshis - received_assetoshis); - return(ValidateAskRemainder(remaining_nValue,remaining_assetoshis,orig_assetoshis,received_assetoshis,paid_nValue,total_nValue)); + return(ValidateAskRemainder(remaining_assetoshis2,remaining_assetoshis,orig_assetoshis,received_assetoshis,paid_assetoshis2,total_assetoshis2)); } else return(false); } bool ValidateSwapRemainder(int64_t remaining_price,int64_t remaining_nValue,int64_t orig_nValue,int64_t received_nValue,int64_t paidunits,int64_t totalunits) { int64_t unitprice,recvunitprice,newunitprice=0; + fprintf(stderr,"%llu %llu %llu %llu %llu %llu\n",(long long)remaining_nValue,(long long)remaining_assetoshis,(long long)orig_assetoshis,(long long)received_assetoshis,(long long)paid_nValue,(long long)total_nValue); if ( orig_nValue == 0 || received_nValue == 0 || paidunits == 0 || totalunits == 0 ) { fprintf(stderr,"ValidateAssetRemainder: orig_nValue == %llu || received_nValue == %llu || paidunits == %llu || totalunits == %llu\n",(long long)orig_nValue,(long long)received_nValue,(long long)paidunits,(long long)totalunits); diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 00feefa41..1deb330f9 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -317,36 +317,45 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx case 'E': // fillexchange //vin.0: normal input //vin.1: unspendable.(vout.0 assetoshis from selloffer) sellTx.vout[0] - //'S'.vin.2+: normal output that satisfies selloffer (*tx.vin[2])->nValue - //'E'.vin.2+: valid CC assetid2 output that satisfies exchange (*tx.vin[2])->nValue + //vin.2+: valid CC assetid2 output that satisfies exchange (*tx.vin[2])->nValue //vout.0: remaining assetoshis -> unspendable //vout.1: vin.1 assetoshis to signer of vin.2 sellTx.vout[0].nValue -> any - //'S'.vout.2: vin.2 value to original pubkey [origpubkey] - //'E'.vout.2: vin.2 assetoshis2 to original pubkey [origpubkey] - //vout.3: normal output for change (if any) - //'S'.vout.n-1: opreturn [EVAL_ASSETS] ['S'] [assetid] [amount of coin still required] [origpubkey] - //'E'.vout.n-1: opreturn [EVAL_ASSETS] ['E'] [assetid vin0+1] [assetid vin2] [remaining asset2 required] [origpubkey] + //vout.2: vin.2+ assetoshis2 to original pubkey [origpubkey] + //vout.3: CC output for asset2 change (if any) + //vout.3/4: normal output for change (if any) + //vout.n-1: opreturn [EVAL_ASSETS] ['E'] [assetid vin0+1] [assetid vin2] [remaining asset2 required] [origpubkey] if ( AssetExactAmounts(cp,inputs,1,outputs,eval,tx,assetid2) == false ) eval->Invalid("asset2 inputs != outputs"); if ( (assetoshis= AssetValidateSellvin(cp,eval,totalunits,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 ) return(false); else if ( numvouts < 3 ) - return eval->Invalid("not enough vouts for fill"); + return eval->Invalid("not enough vouts for fillex"); else if ( tmporigpubkey != origpubkey ) - return eval->Invalid("mismatched origpubkeys for fill"); + return eval->Invalid("mismatched origpubkeys for fillex"); else { + if ( assetoshis != tx.vout[0].nValue+tx.vout[1].nValue ) + return eval->Invalid("locked value doesnt match vout0+1 fillex"); + else if ( tx.vout[3].scriptPubKey.IsPayToCryptoCondition() != 0 ) + { + if ( ConstrainVout(tx.vout[2],1,CCaddr,0) == 0 ) + return eval->Invalid("vout2 doesnt go to origpubkey fillex"); + else if ( inputs != tx.vout[2].nValue+tx.vout[3].nValue ) + return eval->Invalid("asset inputs doesnt match vout2+3 fillex"); + } + else if ( ConstrainVout(tx.vout[2],1,CCaddr,inputs) == 0 ) + return eval->Invalid("vout2 doesnt match inputs fillex"); + else if ( ConstrainVout(tx.vout[1],0,0,0) == 0 ) + return eval->Invalid("vout1 is CC for fillex"); fprintf(stderr,"assets vout0 %llu, vin1 %llu, vout2 %llu -> orig, vout1 %llu, total %llu\n",(long long)tx.vout[0].nValue,(long long)assetoshis,(long long)tx.vout[2].nValue,(long long)tx.vout[1].nValue,(long long)totalunits); if ( ValidateSwapRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false ) - return eval->Invalid("mismatched remainder for fill"); + return eval->Invalid("mismatched remainder for fillex"); else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 ) - return eval->Invalid("normal vout1 for fillask"); - else if ( ConstrainVout(tx.vout[2],1,CCaddr,0) == 0 ) - return eval->Invalid("normal vout2 for fillask"); + return eval->Invalid("normal vout1 for fillex"); else if ( remaining_price != 0 ) { if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) - return eval->Invalid("mismatched vout0 AssetsCCaddr for fill"); + return eval->Invalid("mismatched vout0 AssetsCCaddr for fillex"); } } fprintf(stderr,"fill validated\n"); From d5dae053ea469c926f39a7be564395cc60dc7d95 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 23:07:06 -1100 Subject: [PATCH 189/204] Fix rewards unlock --- src/cc/CCcustom.cpp | 6 +++++ src/cc/rewards.cpp | 66 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index 31f01189b..2b1c83b20 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -36,6 +36,12 @@ 4. make helper functions to create rawtx for RPC functions 5. add rpc calls to rpcserver.cpp and rpcserver.h and in one of the rpc.cpp files 6. add the new .cpp files to src/Makefile.am + + IMPORTANT: make sure that all CC inputs and CC outputs are properly accounted for and reconcile to the satoshi. The built in utxo management will enforce overall vin/vout constraints but it wont know anything about the CC constraints. That is what your Validate function needs to do. + + Generally speaking, there will be normal coins that change into CC outputs, CC outputs that go back to being normal coins, CC outputs that are spent to new CC outputs. + + Make sure both the CC coins and normal coins are preserved and follow the rules that make sense. It is a good idea to define specific roles for specific vins and vouts to reduce the complexity of validation. */ //BTCD Address: RAssetsAtGnvwgK9gVHBbAU4sVTah1hAm5 diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index e8d51653a..e6cc9e9a0 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -35,6 +35,35 @@ Locks wont have any CC vins, but will send to the RewardsCCaddress, with the plan stringbits in the opreturn. vout1 will have the unlock address and no other destination is valid. Unlock does a CC spend to the vout1 address + + + createfunding + vins.*: normal inputs + vout.0: CC vout for funding + vout.1: normal marker vout for easy searching + vout.2: normal change + vout.n-1: opreturn 'F' sbits APR minseconds maxseconds mindeposit + + addfunding + vins.*: normal inputs + vout.0: CC vout for funding + vout.1: normal change + vout.n-1: opreturn 'A' sbits fundingtxid + + lock + vins.*: normal inputs + vout.0: CC vout for locked funds + vout.1: normal output to unlock address + vout.2: change + vout.n-1: opreturn 'L' sbits fundingtxid + + unlock + vin.0: locked funds CC vout.0 from lock + vin.1+: funding CC vout.0 from 'F' and 'A' and 'U' + vout.0: funding CC change + vout.1: normal output to unlock address + vout.n-1: opreturn 'U' sbits fundingtxid + */ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) @@ -172,7 +201,7 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t } // 'L' vs 'F' and 'A' -uint64_t AddRewardsInputs(int32_t fundsflag,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) +uint64_t AddRewardsInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { char coinaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,nValue,totalinputs = 0; uint256 txid,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; std::vector > unspentOutputs; @@ -191,12 +220,16 @@ uint64_t AddRewardsInputs(int32_t fundsflag,struct CCcontract_info *cp,CMutableT { if ( (funcid= DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || (funcid= DecodeRewardsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid)) != 0 ) { - if ( fundsflag != 0 && funcid != 'F' && funcid != 'A' ) + if ( fundsflag != 0 && funcid != 'F' && funcid != 'A' && funcid != 'U' ) continue; else if ( fundsflag == 0 && (funcid != 'L' || tx.vout.size() < 4) ) continue; if ( total != 0 && maxinputs != 0 ) + { + if ( fundsflag == 0 ) + scriptPubKey = tx.vout[1].scriptPubKey; mtx.vin.push_back(CTxIn(txid,vout,CScript())); + } totalinputs += it->second.satoshis; n++; if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) ) @@ -404,7 +437,7 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid) { - CMutableTransaction mtx; char coinaddr[64]; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,sbits,reward=0,amount=0,inputs,CCchange=0,APR,minseconds,maxseconds,mindeposit; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CTransaction tx; char coinaddr[64]; CPubKey mypk,rewardspk; CScript opret,scriptPubKey,ignore; uint256 hashBlock; uint64_t funding,sbits,reward=0,amount=0,inputs,CCchange=0,APR,minseconds,maxseconds,mindeposit; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; @@ -418,7 +451,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 } // need to deal with finding the right utxos if ( locktxid == zeroid ) - amount = AddRewardsInputs(0,cp,mtx,rewardspk,(1LL << 30),1); + amount = AddRewardsInputs(scriptPubkey,0,cp,mtx,rewardspk,(1LL << 30),1); else { GetCCaddress(cp,coinaddr,rewardspk); @@ -427,17 +460,26 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 fprintf(stderr,"%s locktxid/v0 is spent\n",coinaddr); return(0); } - mtx.vin.push_back(CTxIn(locktxid,0,CScript())); - } - if ( amount > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash,APR,minseconds,maxseconds,mindeposit)) > txfee ) - { - if ( (inputs= AddRewardsInputs(1,cp,mtx,rewardspk,reward+amount+txfee,30)) > 0 ) + if ( GetTransaction(locktxid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[1].scriptPubKey.IsPayToCryptoCondition() == 0 ) { - if ( inputs >= (amount + reward + 2*txfee) ) - CCchange = (inputs - (amount + reward + txfee)); + scriptPubkey = tx.vout[1].scriptPubKey; + mtx.vin.push_back(CTxIn(locktxid,0,CScript())); + } + else + { + fprintf(stderr,"%s no normal vout.1 in locktxid\n",coinaddr); + return(0); + } + } + if ( amount > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash,APR,minseconds,maxseconds,mindeposit)) > txfee && scriptPubKey.size() > 0 ) + { + if ( (inputs= AddRewardsInputs(ignore,1,cp,mtx,rewardspk,reward+amount+txfee,30)) > 0 ) + { + if ( inputs >= (reward + 2*txfee) ) + CCchange = (inputs - (reward + txfee)); fprintf(stderr,"inputs %.8f CCchange %.8f amount %.8f reward %.8f\n",(double)inputs/COIN,(double)CCchange/COIN,(double)amount/COIN,(double)reward/COIN); mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk)); - mtx.vout.push_back(CTxOut(amount+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + mtx.vout.push_back(CTxOut(amount+reward,scriptPubKey)); return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid))); } fprintf(stderr,"cant find enough rewards inputs\n"); From 1fa85bb2c07414a2b2b5ed7fb29181adc2ddf2d3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 23:08:16 -1100 Subject: [PATCH 190/204] Fix --- src/cc/CCassetsCore.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index f61b7ee4e..da505db9c 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -193,7 +193,6 @@ bool SetSwapFillamounts(int64_t &received_assetoshis,int64_t &remaining_assetosh bool ValidateSwapRemainder(int64_t remaining_price,int64_t remaining_nValue,int64_t orig_nValue,int64_t received_nValue,int64_t paidunits,int64_t totalunits) { int64_t unitprice,recvunitprice,newunitprice=0; - fprintf(stderr,"%llu %llu %llu %llu %llu %llu\n",(long long)remaining_nValue,(long long)remaining_assetoshis,(long long)orig_assetoshis,(long long)received_assetoshis,(long long)paid_nValue,(long long)total_nValue); if ( orig_nValue == 0 || received_nValue == 0 || paidunits == 0 || totalunits == 0 ) { fprintf(stderr,"ValidateAssetRemainder: orig_nValue == %llu || received_nValue == %llu || paidunits == %llu || totalunits == %llu\n",(long long)orig_nValue,(long long)received_nValue,(long long)paidunits,(long long)totalunits); From 4f839b5c9cbafbb8afb7f79d8c26f7683b8f182e Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 27 Jul 2018 23:10:00 -1100 Subject: [PATCH 191/204] Test --- src/cc/rewards.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index e6cc9e9a0..7975bf6fe 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -451,7 +451,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 } // need to deal with finding the right utxos if ( locktxid == zeroid ) - amount = AddRewardsInputs(scriptPubkey,0,cp,mtx,rewardspk,(1LL << 30),1); + amount = AddRewardsInputs(scriptPubKey,0,cp,mtx,rewardspk,(1LL << 30),1); else { GetCCaddress(cp,coinaddr,rewardspk); @@ -462,7 +462,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 } if ( GetTransaction(locktxid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[1].scriptPubKey.IsPayToCryptoCondition() == 0 ) { - scriptPubkey = tx.vout[1].scriptPubKey; + scriptPubKey = tx.vout[1].scriptPubKey; mtx.vin.push_back(CTxIn(locktxid,0,CScript())); } else From 0af767e0983ab1d6b3693e4c1ffea3cbabc27761 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 28 Jul 2018 00:15:31 -1100 Subject: [PATCH 192/204] Validate rewards --- src/cc/rewards.cpp | 119 ++++++++++++++++++++++++++++++++------------- 1 file changed, 86 insertions(+), 33 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 7975bf6fe..e9856bcbd 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -92,7 +92,7 @@ uint8_t DecodeRewardsFundingOpRet(const CScript &scriptPubKey,uint64_t &sbits,ui std::vector vopret; uint8_t *script,e,f; GetOpReturnData(scriptPubKey, vopret); script = (uint8_t *)vopret.data(); - if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> APR; ss >> minseconds; ss >> maxseconds; ss >> mindeposit) != 0 ) + if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> APR; ss >> minseconds; ss >> maxseconds; ss >> mindeposit) != 0 ) { if ( e == EVAL_REWARDS && f == 'F' ) return(f); @@ -107,15 +107,26 @@ CScript EncodeRewardsOpRet(uint8_t funcid,uint64_t sbits,uint256 fundingtxid) return(opret); } -uint8_t DecodeRewardsOpRet(const CScript &scriptPubKey,uint64_t &sbits,uint256 &fundingtxid) +uint8_t DecodeRewardsOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits,uint256 &fundingtxid) { - std::vector vopret; uint8_t *script,e,f; + std::vector vopret; uint8_t *script,e,f; uint64_t APR,minseconds,maxseconds,mindeposit; GetOpReturnData(scriptPubKey, vopret); - script = (uint8_t *)vopret.data(); - if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> fundingtxid) != 0 ) + if ( vopret.size() > 2 ) { - if ( e == EVAL_REWARDS && (f == 'L' || f == 'U' || f == 'A') ) - return(f); + script = (uint8_t *)vopret.data(); + if ( script[0] == EVAL_REWARDS ) + { + if ( script[1] == 'F' ) + { + if ( (funcid= DecodeRewardsFundingOpRet(scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' ) + fundingtxid = txid; + } + else if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> fundingtxid) != 0 ) + { + if ( e == EVAL_REWARDS && (f == 'L' || f == 'U' || f == 'A') ) + return(f); + } + } } return(0); } @@ -131,10 +142,10 @@ uint64_t IsRewardsvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t return(0); } -bool RewardsExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee) +bool RewardsExactAmounts(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx,uint64_t txfee) { static uint256 zerohash; - CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; uint64_t inputs=0,outputs=0,assetoshis; + CTransaction vinTx; uint256 hashBlock; int32_t i,numvins,numvouts; uint64_t inputs=0,outputs=0,assetoshis; numvins = tx.vin.size(); numvouts = tx.vout.size(); for (i=0; iInvalid("mismatched inputs != outputs + COIN + txfee"); + return eval->Invalid("mismatched inputs != outputs + txfee"); } else return(true); } bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { - int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; + uint256 txid,fundingtxid; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,amount,reward; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; uint8_t funcid; CScript scriptPubKey; CTransaction fundingTx,vinTx; numvins = tx.vin.size(); numvouts = tx.vout.size(); preventCCvins = preventCCvouts = -1; @@ -176,26 +187,70 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t return eval->Invalid("no vouts"); else { - // follow rules - for (i=0; iInvalid("illegal normal vini"); - } - if ( RewardsExactAmounts(cp,eval,tx,1,10000) == false ) - return false; - else - { - preventCCvouts = 1; - if ( IsRewardsvout(cp,tx,0) != 0 ) + if ( eval->GetTxUnconfirmed(fundingtxid,fundingTx,hashBlock) == 0 ) + return eval->Invalid("cant find fundingtxid"); + else if ( fundingTx.vout.size() > 0 && DecodeRewardsFundingOpRet(fundingTx.vout[fundingTx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit) != 'F' ) + return eval->Invalid("fundingTx not valid"); + switch ( funcid ) { - preventCCvouts++; - i = 1; - } else i = 0; - if ( tx.vout[i].nValue != COIN ) - return eval->Invalid("invalid rewards output"); - return(PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts)); + case 'F': + //vins.*: normal inputs + //vout.0: CC vout for funding + //vout.1: normal marker vout for easy searching + //vout.2: normal change + //vout.n-1: opreturn 'F' sbits APR minseconds maxseconds mindeposit + return eval->Invalid("unexpected RewardsValidate for createfunding"); + break; + case 'A': + //vins.*: normal inputs + //vout.0: CC vout for funding + //vout.1: normal change + //vout.n-1: opreturn 'A' sbits fundingtxid + return eval->Invalid("unexpected RewardsValidate for addfunding"); + break; + case 'L': + //vins.*: normal inputs + //vout.0: CC vout for locked funds + //vout.1: normal output to unlock address + //vout.2: change + //vout.n-1: opreturn 'L' sbits fundingtxid + return eval->Invalid("unexpected RewardsValidate for lock"); + break; + case 'U': + //vin.0: locked funds CC vout.0 from lock + //vin.1+: funding CC vout.0 from 'F' and 'A' and 'U' + //vout.0: funding CC change + //vout.1: normal output to unlock address + //vout.n-1: opreturn 'U' sbits fundingtxid + for (i=0; iismyvin)(tx.vin[i].scriptSig) == 0 ) + return eval->Invalid("unexpected normal vin for unlock"); + } + if ( RewardsExactAmounts(cp,eval,tx,txfee+tx.vout[1].nValue) == 0 ) + return false; + else if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 ) + return eval->Invalid("always should find vin.0, but didnt"); + else if ( vinTx.vout[0].scriptPubKey.IsPayToCryptoCondition() == 0 ) + return eval->Invalid("lock tx vout.0 is normal output"); + else if ( tx.vout.size() < 3 ) + return eval->Invalid("unlock tx not enough vouts"); + else if ( tx.vout[0].scriptPubKey.IsPayToCryptoCondition() == 0 ) + return eval->Invalid("unlock tx vout.0 is normal output"); + else if ( tx.vout[1].scriptPubKey.IsPayToCryptoCondition() A= 0 ) + return eval->Invalid("unlock tx vout.1 is CC output"); + amount = vinTx.vout[0].nValue; + reward = RewardsCalc(amount,txid,APR,minseconds,maxseconds,mindeposit); + if ( tx.vout[1].nValue > amount+reward ) + return eval->Invalid("unlock tx vout.1 isnt amount+reward"); + preventCCvouts = 1; + break; + } } + return(PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts)); } return(true); } @@ -218,7 +273,7 @@ uint64_t AddRewardsInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontr continue; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (funcid= DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || (funcid= DecodeRewardsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid)) != 0 ) + if ( (funcid= DecodeRewardsOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid)) != 0 ) { if ( fundsflag != 0 && funcid != 'F' && funcid != 'A' && funcid != 'U' ) continue; @@ -252,14 +307,12 @@ uint64_t RewardsPlanFunds(uint64_t refsbits,struct CCcontract_info *cp,CPubKey p vout = (int32_t)it->first.index; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (funcid= DecodeRewardsFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' || (funcid= DecodeRewardsOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid)) == 'A' || funcid == 'U' ) + if ( (funcid= DecodeRewardsOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid)) != 0 ) { if ( (funcid == 'F' && reffundingtxid == txid) || reffundingtxid == fundingtxid ) { if ( refsbits == sbits && (nValue= IsRewardsvout(cp,tx,vout)) > 0 ) - { totalinputs += nValue; - } else fprintf(stderr,"refsbits.%llx sbits.%llx nValue %.8f\n",(long long)refsbits,(long long)sbits,(double)nValue/COIN); } else fprintf(stderr,"else case\n"); } else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); From 9f2d38b39c6a9d051ecd7ec637dc4f07206215aa Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 28 Jul 2018 00:17:41 -1100 Subject: [PATCH 193/204] Test --- src/cc/rewards.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index e9856bcbd..1d33dacbf 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -109,7 +109,7 @@ CScript EncodeRewardsOpRet(uint8_t funcid,uint64_t sbits,uint256 fundingtxid) uint8_t DecodeRewardsOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits,uint256 &fundingtxid) { - std::vector vopret; uint8_t *script,e,f; uint64_t APR,minseconds,maxseconds,mindeposit; + std::vector vopret; uint8_t *script,e,f,funcid; uint64_t APR,minseconds,maxseconds,mindeposit; GetOpReturnData(scriptPubKey, vopret); if ( vopret.size() > 2 ) { @@ -179,7 +179,7 @@ bool RewardsExactAmounts(struct CCcontract_info *cp,Eval *eval,const CTransactio bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { - uint256 txid,fundingtxid; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,amount,reward; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; uint8_t funcid; CScript scriptPubKey; CTransaction fundingTx,vinTx; + uint256 txid,fundingtxid,hashBlock; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,amount,reward,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; uint8_t funcid; CScript scriptPubKey; CTransaction fundingTx,vinTx; numvins = tx.vin.size(); numvouts = tx.vout.size(); preventCCvins = preventCCvouts = -1; @@ -240,7 +240,7 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t return eval->Invalid("unlock tx not enough vouts"); else if ( tx.vout[0].scriptPubKey.IsPayToCryptoCondition() == 0 ) return eval->Invalid("unlock tx vout.0 is normal output"); - else if ( tx.vout[1].scriptPubKey.IsPayToCryptoCondition() A= 0 ) + else if ( tx.vout[1].scriptPubKey.IsPayToCryptoCondition() != 0 ) return eval->Invalid("unlock tx vout.1 is CC output"); amount = vinTx.vout[0].nValue; reward = RewardsCalc(amount,txid,APR,minseconds,maxseconds,mindeposit); From 6e801a8243c7482aebcc1e96f44f21d84e7978e6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 28 Jul 2018 00:23:07 -1100 Subject: [PATCH 194/204] Trim funding input --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 1d33dacbf..64e50bd42 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -526,7 +526,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 } if ( amount > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash,APR,minseconds,maxseconds,mindeposit)) > txfee && scriptPubKey.size() > 0 ) { - if ( (inputs= AddRewardsInputs(ignore,1,cp,mtx,rewardspk,reward+amount+txfee,30)) > 0 ) + if ( (inputs= AddRewardsInputs(ignore,1,cp,mtx,rewardspk,reward+txfee,30)) > 0 ) { if ( inputs >= (reward + 2*txfee) ) CCchange = (inputs - (reward + txfee)); From 242ba739992e957a19df69bdaf447341b374adc0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 28 Jul 2018 00:27:50 -1100 Subject: [PATCH 195/204] Test --- src/cc/rewards.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 64e50bd42..ece58ca11 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -275,6 +275,7 @@ uint64_t AddRewardsInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontr { if ( (funcid= DecodeRewardsOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid)) != 0 ) { + fprintf(stderr,"fundsflag.%d (%c) %.8f %.8f\n",fundsflag,funcid,(double)tx.vout[vout].nValue/COIN,(double)it->second.satoshis/COIN); if ( fundsflag != 0 && funcid != 'F' && funcid != 'A' && funcid != 'U' ) continue; else if ( fundsflag == 0 && (funcid != 'L' || tx.vout.size() < 4) ) From 1cbb6e1390cd99fadbf3247fad735d88ef89979b Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 28 Jul 2018 00:30:27 -1100 Subject: [PATCH 196/204] Test --- src/cc/rewards.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index ece58ca11..a05ff915f 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -258,7 +258,7 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t // 'L' vs 'F' and 'A' uint64_t AddRewardsInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { - char coinaddr[64]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,nValue,totalinputs = 0; uint256 txid,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; + char coinaddr[64],str[65]; uint64_t sbits,APR,minseconds,maxseconds,mindeposit,nValue,totalinputs = 0; uint256 txid,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); @@ -266,6 +266,7 @@ uint64_t AddRewardsInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontr { txid = it->first.txhash; vout = (int32_t)it->first.index; + fprintf(stderr,"(%s) %s/v%d %.8f\n",coinaddr,uint256_str(str,txid),vout,(double)it->second.satoshis/COIN); for (j=0; j Date: Sat, 28 Jul 2018 00:34:09 -1100 Subject: [PATCH 197/204] Test --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index a05ff915f..70ae6fb75 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -291,7 +291,7 @@ uint64_t AddRewardsInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontr n++; if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) ) break; - } + } else fprintf(stderr,"null funcid\n"); } } return(totalinputs); From cb35ec92b837892249322af4a7761724184fee9c Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 28 Jul 2018 00:38:25 -1100 Subject: [PATCH 198/204] Test --- src/cc/rewards.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 70ae6fb75..19ef76d7b 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -118,16 +118,18 @@ uint8_t DecodeRewardsOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sb { if ( script[1] == 'F' ) { - if ( (funcid= DecodeRewardsFundingOpRet(scriptPubKey,sbits,APR,minseconds,maxseconds,mindeposit)) == 'F' ) + if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> APR; ss >> minseconds; ss >> maxseconds; ss >> mindeposit) != 0 ) fundingtxid = txid; + else fprintf(stderr,"unmarshal error for F\n"); } else if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> fundingtxid) != 0 ) { if ( e == EVAL_REWARDS && (f == 'L' || f == 'U' || f == 'A') ) return(f); + else fprintf(stderr,"mismatched e.%02x f.(%c)\n",e,f); } - } - } + } else fprintf(stderr,"script[0] %02x != EVAL_REWARDS\n",script[0]); + } else fprintf(stderr,"not enough opret.[%d]\n",vopret.size()); return(0); } From 5641892d0ba8c4ebd7ed645929c45f26e9077c0a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 28 Jul 2018 00:39:03 -1100 Subject: [PATCH 199/204] Test --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 19ef76d7b..68ad3cc6c 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -129,7 +129,7 @@ uint8_t DecodeRewardsOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sb else fprintf(stderr,"mismatched e.%02x f.(%c)\n",e,f); } } else fprintf(stderr,"script[0] %02x != EVAL_REWARDS\n",script[0]); - } else fprintf(stderr,"not enough opret.[%d]\n",vopret.size()); + } else fprintf(stderr,"not enough opret.[%d]\n",(int32_t)vopret.size()); return(0); } From 85afb4f0607ac92b41355d054efea0b3f87d1925 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 28 Jul 2018 00:40:42 -1100 Subject: [PATCH 200/204] Test --- src/cc/rewards.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 68ad3cc6c..b6a83c15e 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -119,8 +119,10 @@ uint8_t DecodeRewardsOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sb if ( script[1] == 'F' ) { if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> APR; ss >> minseconds; ss >> maxseconds; ss >> mindeposit) != 0 ) + { fundingtxid = txid; - else fprintf(stderr,"unmarshal error for F\n"); + return('F'); + } else fprintf(stderr,"unmarshal error for F\n"); } else if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> fundingtxid) != 0 ) { From 763ec3cae7f5235ae222426198f04f2eca8a4b57 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 28 Jul 2018 00:44:12 -1100 Subject: [PATCH 201/204] Test --- src/cc/rewards.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index b6a83c15e..1a94f34ef 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -246,6 +246,8 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t return eval->Invalid("unlock tx vout.0 is normal output"); else if ( tx.vout[1].scriptPubKey.IsPayToCryptoCondition() != 0 ) return eval->Invalid("unlock tx vout.1 is CC output"); + else if ( tx.vout[1].scriptPubKey != vinTx.vout[1].scriptPubKey ) + return eval->Invalid("unlock tx vout.1 mismatched scriptPubKey"); amount = vinTx.vout[0].nValue; reward = RewardsCalc(amount,txid,APR,minseconds,maxseconds,mindeposit); if ( tx.vout[1].nValue > amount+reward ) From 43b91cca463f0d0f070c3b0d4012e85ea7ca1723 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 28 Jul 2018 00:47:41 -1100 Subject: [PATCH 202/204] Test --- src/cc/CCutils.cpp | 5 +++-- src/cc/rewards.cpp | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index af92b69a3..7807e0e27 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -270,12 +270,13 @@ bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector param int64_t CCduration(uint256 txid) { - CTransaction tx; uint256 hashBlock; int64_t duration = 0; + CTransaction tx; uint256 hashBlock; char str[65]; int64_t duration = 0; if ( GetTransaction(txid,tx,hashBlock,false) == 0 ) { - fprintf(stderr,"cant find duration txid\n"); + fprintf(stderr,"cant find duration txid %s\n",uint256_str(str,txid)); return(0); } + // get current nTime return(duration); } diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 1a94f34ef..cc174804f 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -249,7 +249,7 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t else if ( tx.vout[1].scriptPubKey != vinTx.vout[1].scriptPubKey ) return eval->Invalid("unlock tx vout.1 mismatched scriptPubKey"); amount = vinTx.vout[0].nValue; - reward = RewardsCalc(amount,txid,APR,minseconds,maxseconds,mindeposit); + reward = RewardsCalc(amount,tx.vin[0].prevout.hash,APR,minseconds,maxseconds,mindeposit); if ( tx.vout[1].nValue > amount+reward ) return eval->Invalid("unlock tx vout.1 isnt amount+reward"); preventCCvouts = 1; From f1c75a6632ed74dd54f32c9c3433954ad73e513f Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 28 Jul 2018 00:59:46 -1100 Subject: [PATCH 203/204] Test --- src/cc/CCutils.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 7807e0e27..632b243bc 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -270,13 +270,29 @@ bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector param int64_t CCduration(uint256 txid) { - CTransaction tx; uint256 hashBlock; char str[65]; int64_t duration = 0; + CTransaction tx; uint256 hashBlock; uint32_t txtime=0; char str[65]; CBlockIndex *pindex; int64_t duration = 0; if ( GetTransaction(txid,tx,hashBlock,false) == 0 ) { - fprintf(stderr,"cant find duration txid %s\n",uint256_str(str,txid)); + fprintf(stderr,"CCduration cant find duration txid %s\n",uint256_str(str,txid)); return(0); } - // get current nTime + else if ( hashBlock == zeroid ) + { + fprintf(stderr,"CCduration no hashBlock for txid %s\n",uint256_str(str,txid)); + return(0); + } + else if ( (pindex= mapBlockIndex[hashBlock]) == 0 || (txtime= pindex->nTime) == 0 ) + { + fprintf(stderr,"CCduration no txtime %u %p for txid %s\n",txtime,pindex,uint256_str(str,txid)); + return(0); + } + else if ( (pindex= chainActive.LastTip()) == 0 || pindex->nTime < txtime ) + { + fprintf(stderr,"CCduration backwards timestamps %u %u for txid %s\n",(uint32_t)pindex->nTime,txtime,uint256_str(str,txid)); + return(0); + } + duration = (pindex->nTime - txtime); + fprintf(stderr,"duration %d (%u - %u)\n",(int32_t)duration,(uint32_t)pindex->nTime,txtime); return(duration); } From 572ce78169280a46c1c0e2e976d07ba37aad9079 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 28 Jul 2018 01:02:00 -1100 Subject: [PATCH 204/204] Activate CCduration constraint --- src/cc/rewards.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index cc174804f..2944c1bee 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -71,8 +71,8 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon uint64_t duration,reward = 0; if ( (duration= CCduration(txid)) < minseconds ) { - //return(0); - duration = (uint32_t)time(NULL) - (1532713903 - 3600 * 24); + return(0); + //duration = (uint32_t)time(NULL) - (1532713903 - 3600 * 24); } else if ( duration > maxseconds ) maxseconds = duration; reward = (((amount * APR) / COIN) * duration) / (365*24*3600LL * 100);