diff --git a/build-aux/m4/bitcoin_find_bdb.m4 b/build-aux/m4/bitcoin_find_bdb.m4 index bd5c2af45..ada8ffdbe 100644 --- a/build-aux/m4/bitcoin_find_bdb.m4 +++ b/build-aux/m4/bitcoin_find_bdb.m4 @@ -38,7 +38,7 @@ AC_DEFUN([BITCOIN_FIND_BDB62],[ done if test "x$bdbpath" = "xX"; then AC_MSG_RESULT([no]) - AC_MSG_ERROR([libdb_cxx headers missing, Bitcoin Core requires this library for wallet functionality (--disable-wallet to disable wallet functionality)]) + AC_MSG_ERROR([libdb_cxx headers missing, Hush source code requires this library for wallet functionality (1) (--disable-wallet to disable wallet functionality)]) elif test "x$bdb62path" = "xX"; then BITCOIN_SUBDIR_TO_INCLUDE(BDB_CPPFLAGS,[${bdbpath}],db_cxx) AC_ARG_WITH([incompatible-bdb],[AS_HELP_STRING([--with-incompatible-bdb], [allow using a bdb version other than 6.2])],[ @@ -60,7 +60,7 @@ AC_DEFUN([BITCOIN_FIND_BDB62],[ ]) done if test "x$BDB_LIBS" = "x"; then - AC_MSG_ERROR([libdb_cxx missing, Bitcoin Core requires this library for wallet functionality (--disable-wallet to disable wallet functionality)]) + AC_MSG_ERROR([libdb_cxx missing, Hush source code requires this library for wallet functionality (2) (--disable-wallet to disable wallet functionality)]) fi AC_SUBST(BDB_LIBS) ]) diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index 69e5eebcc..80ece237c 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -60,7 +60,7 @@ extern int32_t ASSETCHAINS_BLOCKTIME; extern uint64_t ASSETCHAINS_CBOPRET; void hush_passport_iteration(); int32_t hush_longestchain(); -void komodo_cbopretupdate(int32_t forceflag); +void hush_cbopretupdate(int32_t forceflag); CBlockIndex *hush_chainactive(int32_t height); void WaitForShutdown(boost::thread_group* threadGroup) @@ -89,7 +89,7 @@ void WaitForShutdown(boost::thread_group* threadGroup) } else { //hush_longestchain(); if ( ASSETCHAINS_CBOPRET != 0 ) - komodo_cbopretupdate(0); + hush_cbopretupdate(0); for (i=0; i<=ASSETCHAINS_BLOCKTIME/5; i++) { fShutdown = ShutdownRequested(); diff --git a/src/cc/CCMarmara.h b/src/cc/CCMarmara.h index 85ef90fa5..a23a39930 100644 --- a/src/cc/CCMarmara.h +++ b/src/cc/CCMarmara.h @@ -32,7 +32,7 @@ #define EVAL_MARMARA 0xef extern uint8_t ASSETCHAINS_MARMARA; -uint64_t komodo_block_prg(uint32_t nHeight); +uint64_t hush_block_prg(uint32_t nHeight); int32_t MarmaraGetcreatetxid(uint256 &createtxid,uint256 txid); int32_t MarmaraGetbatontxid(std::vector &creditloop,uint256 &batontxid,uint256 txid); UniValue MarmaraCreditloop(uint256 txid); diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 2eafc975c..af72aafe2 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -19,9 +19,10 @@ #define CC_INCLUDE_H /*! \file CCinclude.h -\brief A Documented file. +\brief Cryptoconditions + +CCs for teh lulz -Details. */ /// \mainpage Brief introduction into cryptocondition contracts @@ -240,8 +241,6 @@ extern CWallet* pwalletMain; //!< global wallet object pointer to access wallet /// @private seems old-style bool GetAddressUnspent(uint160 addressHash, int type,std::vector > &unspentOutputs); -//CBlockIndex *komodo_getblockindex(uint256 hash); //moved to komodo_def.h -//int32_t hush_nextheight(); //moved to komodo_def.h /// CCgetspenttxid finds the txid of the transaction which spends a transaction output. The function does this without loading transactions from the chain, by using spent index /// @param[out] spenttxid transaction id of the spending transaction @@ -257,11 +256,6 @@ void CCclearvars(struct CCcontract_info *cp); UniValue CClib(struct CCcontract_info *cp,char *method,char *jsonstr); UniValue CClib_info(struct CCcontract_info *cp); -//CBlockIndex *hush_blockindex(uint256 hash); //moved to komodo_def.h -//CBlockIndex *hush_chainactive(int32_t height); //moved to komodo_def.h -//int32_t hush_blockheight(uint256 hash); //moved to komodo_def.h -//void StartShutdown(); - static const uint256 zeroid; //!< null uint256 constant /// \cond INTERNAL @@ -380,11 +374,8 @@ int64_t IsTokensvout(bool goDeeper, bool checkPubkeys, struct CCcontract_info *c /// returns true if success bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx); -//void komodo_sendmessage(int32_t minpeers,int32_t maxpeers,const char *message,std::vector payload); // moved to hush_defs.h - /// @private int32_t payments_parsehexdata(std::vector &hexdata,cJSON *item,int32_t len); -// int32_t komodo_blockload(CBlock& block,CBlockIndex *pindex); // this def in hush_defs.h /// Makes opreturn scriptPubKey for token creation transaction. Normally this function is called internally by the tokencreate rpc. You might need to call this function to create a customized token. /// The total opreturn length should not exceed 10001 byte @@ -701,7 +692,6 @@ 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(const char *hexstr); // located in komodo_defs /// \endcond /// converts public key as array of uint8_t to normal address @@ -795,8 +785,6 @@ int32_t CCCointxidExists(char const *logcategory,uint256 cointxid); /// @private uint256 BitcoinGetProofMerkleRoot(const std::vector &proofData, std::vector &txids); -// bool komodo_txnotarizedconfirmed(uint256 txid); //moved to hush_defs.h - /// @private CPubKey check_signing_pubkey(CScript scriptSig); diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index f6e244e1c..fef9cccc9 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -529,7 +529,7 @@ int64_t CCduration(int32_t &numblocks,uint256 txid) //fprintf(stderr,"CCduration no hashBlock for txid %s\n",uint256_str(str,txid)); return(0); } - else if ( (pindex= komodo_getblockindex(hashBlock)) == 0 || (txtime= pindex->nTime) == 0 || (txheight= pindex->GetHeight()) <= 0 ) + else if ( (pindex= hush_getblockindex(hashBlock)) == 0 || (txtime= pindex->nTime) == 0 || (txheight= pindex->GetHeight()) <= 0 ) { fprintf(stderr,"CCduration no txtime %u or txheight.%d %p for txid %s\n",txtime,txheight,pindex,uint256_str(str,txid)); return(0); @@ -666,7 +666,7 @@ uint256 BitcoinGetProofMerkleRoot(const std::vector &proofData, std::ve } extern struct NSPV_inforesp NSPV_inforesult; -int32_t komodo_get_current_height() +int32_t hush_get_current_height() { if ( HUSH_NSPV_SUPERLITE ) { diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index e5989ba43..a3ac74b10 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -939,7 +939,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx, if ( (iswin= DiceIsWinner(entropy,entropyvout,txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { // will only happen for fundingPubKey - if ( HUSH_INSYNC != 0 && KOMODO_DEALERNODE != 0 ) + if ( HUSH_INSYNC != 0 && HUSH_DEALERNODE != 0 ) DiceQueue(iswin,sbits,fundingtxid,txid,tx,entropyvout); } else @@ -1791,8 +1791,8 @@ double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettx { if ( myGetTransaction(betTx.vin[0].prevout.hash,entropyTx,hashBlock) != 0 ) { - flag = KOMODO_DEALERNODE != 0; - if ( KOMODO_DEALERNODE != 0 && scriptPubKey == fundingPubKey ) + flag = HUSH_DEALERNODE != 0; + if ( HUSH_DEALERNODE != 0 && scriptPubKey == fundingPubKey ) { bettorentropy = DiceGetEntropy(betTx,'B'); if ( (iswin= DiceIsWinner(hentropyproof,entropyvout,txid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) @@ -1821,7 +1821,7 @@ double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettx } } } - if ( didinit == 0 && KOMODO_DEALERNODE == 0 && scriptPubKey == fundingPubKey ) + if ( didinit == 0 && HUSH_DEALERNODE == 0 && scriptPubKey == fundingPubKey ) { strcpy(_planstr,planstr); dealer0_fundingtxid = fundingtxid; diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 0269bde65..940900834 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -71,7 +71,7 @@ int64_t prices_blockinfo(int32_t height,char *acaddr) std::vector vopret; CBlockIndex *pindex; CBlock block; CTransaction tx,vintx; uint64_t pricebits; char destaddr[64]; uint32_t aveprice=0,timestamp,uprice; uint256 hashBlock; int64_t dist,mindist=(1LL<<60),prizefund = 0; int32_t mini=-1,i,n,vini,numvouts,iter; struct prices_bar refbar; if ( (pindex= hush_chainactive(height)) != 0 ) { - if ( komodo_blockload(block,pindex) == 0 ) + if ( hush_blockload(block,pindex) == 0 ) { n = block.vtx.size(); vini = 0; diff --git a/src/cc/oracles.cpp b/src/cc/oracles.cpp index 21e7cbb82..b33751b03 100644 --- a/src/cc/oracles.cpp +++ b/src/cc/oracles.cpp @@ -95,7 +95,7 @@ vout.n-1: opreturn with oracletxid, prevbatontxid and data in proper format */ -extern int32_t komodo_get_current_height(); +extern int32_t hush_get_current_height(); #define PUBKEY_SPOOFING_FIX_ACTIVATION 1563148800 #define CC_MARKER_VALUE 10000 @@ -946,7 +946,7 @@ UniValue OracleRegister(const CPubKey& pk, int64_t txfee,uint256 oracletxid,int6 CCERR_RESULT("oraclescc",CCLOG_INFO, stream << "error adding inputs from your Oracles CC address, please fund it first with oraclesfund rpc!"); mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(markerpubkey)) << OP_CHECKSIG)); mtx.vout.push_back(MakeCC1vout(cp->evalcode,txfee,batonpk)); - if (GetLatestTimestamp(komodo_get_current_height())>PUBKEY_SPOOFING_FIX_ACTIVATION) mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + if (GetLatestTimestamp(hush_get_current_height())>PUBKEY_SPOOFING_FIX_ACTIVATION) mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); return(FinalizeCCTxExt(pk.IsValid(),0,cp,mtx,mypk,txfee,EncodeOraclesOpRet('R',oracletxid,mypk,datafee))); } CCERR_RESULT("oraclescc",CCLOG_INFO, stream << "error adding normal inputs"); diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index e449f207f..0f74b1a6f 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -15,70 +15,14 @@ * Removal or modification of this copyright notice is prohibited. * * * *****************************************************************************/ -//TODO: delete all this jl777 garbage - /* - CBOPRET creates trustless oracles, which can be used for making a synthetic cash settlement system based on real world prices; - - 0.5% fee based on betamount, NOT leveraged betamount!! - 0.1% collected by price basis determinant - 0.2% collected by rekt tx - - PricesBet -> +/-leverage, amount, synthetic -> opreturn includes current price - funds are locked into 1of2 global CC address - for first day, long basis is MAX(correlated,smoothed), short is MIN() - reference price is the smoothed of the previous block - if synthetic value + amount goes negative, then anybody can rekt it to collect a rektfee, proof of rekt must be included to show cost basis, rekt price - original creator can liquidate at anytime and collect (synthetic value + amount) from globalfund - 0.5% of bet -> globalfund - - PricesStatus -> bettxid maxsamples returns initial params, cost basis, amount left, rekt:true/false, rektheight, initial synthetic price, current synthetic price, net gain - - PricesRekt -> bettxid height -> 0.1% to miner, rest to global CC - - PricesClose -> bettxid returns (synthetic value + amount) - - PricesList -> all bettxid -> list [bettxid, netgain] - - */ - -/* -To create payments plan start a chain with the following ac_params: - -ac_snapshot=1440 (or for test chain something smaller, if you like.) - - this enables the payments airdrop cc to work. - -ac_earlytxidcontract=237 (Eval code for prices cc.) - - this allows to know what contract this chain is paying with the scriptpubkey in the earlytxid op_return. - -./komodod -ac_name=TESTPRC -ac_supply=100000000 -ac_reward=1000000000 -ac_nk=96,5 -ac_blocktime=20 -ac_cc=2 -ac_snapshot=50 -ac_sapling=1 -ac_earlytxidcontract=237 -testnode=1 -gen -genproclimit=1 - -Then in very early block < 10 or so, do paymentsairdrop eg. - `./komodo-cli -ac_name=TESTPRC paymentsairdrop '[10,10,0,3999,0,0]' -Once this tx is confirmed, do `paymentsfund` and decode the raw hex. You can edit the source to not send the tx if requried. -Get the full `hex` of the vout[0] that pays to CryptoCondition. then place it on chain with the following command: with the hex you got in place of the hex below. - './komodo-cli -ac_name=TESTPRC opreturn_burn 1 2ea22c8020292ba5c8fd9cc89b12b35bf8f5d00196990ecbb06102b84d9748d11d883ef01e81031210008203000401cc' -copy the hex, and sendrawtransaction, copy the txid returned. -this places the scriptpubkey that pays the plan into an op_return before block 100, allowing us to retreive it, and nobody to change it. -Restart the daemon with -earlytxid= eg: - -./komodod -ac_name=TESTPRC -ac_supply=100000000 -ac_reward=1000000000 -ac_nk=96,5 -ac_blocktime=20 -ac_cc=2 -ac_snapshot=50 -ac_sapling=1 -ac_earlytxidcontract=237 -earlytxid=cf89d17fb11037f65c160d0749dddd74dc44d9893b0bb67fe1f96c1f59786496 -testnode=1 -gen -genproclimit=1 - -mine the chain past block 100, preventing anyone else, creating another payments plan on chain before block 100. - -We call the following in Validation and RPC where the address is needed. -if ( ASSETCHAINS_EARLYTXIDCONTRACT == EVAL_PRICES && HUSH_EARLYTXID_SCRIPTPUB.size() == 0 ) - GetHushEarlytxidScriptPub(); - -This will fetch the op_return, calculate the scriptPubKey and save it to the global. -On daemon restart as soon as validation for BETTX happens the global will be filled, after this the transaction never needs to be looked up again. - */ +//TODO: Finish deleting all this jl777 garbage. This CC will never function #include "CCassets.h" #include "CCPrices.h" - #include #include #define IS_CHARINSTR(c, str) (std::string(str).find((char)(c)) != std::string::npos) - #define NVOUT_CCMARKER 1 #define NVOUT_NORMALMARKER 3 @@ -88,7 +32,7 @@ typedef struct OneBetData { int64_t costbasis; int64_t profits; - OneBetData() { positionsize = 0; firstheight = 0; costbasis = 0; profits = 0; } // it is important to clear costbasis as it will be calculated as minmax from inital value 0 + OneBetData() { positionsize = 0; firstheight = 0; costbasis = 0; profits = 0; } } onebetdata; typedef struct BetInfo { @@ -141,9 +85,7 @@ static bool prices_isacceptableamount(const std::vector &vecparsed, in // returns true if there are only digits and no alphas or slashes in 's' inline bool is_weight_str(std::string s) { - return - std::count_if(s.begin(), s.end(), [](unsigned char c) { return std::isdigit(c); } ) > 0 && - std::count_if(s.begin(), s.end(), [](unsigned char c) { return std::isalpha(c) || c == '/'; } ) == 0; + return false; } @@ -158,555 +100,92 @@ CScript prices_betopret(CPubKey mypk,int32_t height,int64_t amount,int16_t lever uint8_t prices_betopretdecode(CScript scriptPubKey,CPubKey &pk,int32_t &height,int64_t &amount,int16_t &leverage,int64_t &firstprice,std::vector &vec,uint256 &tokenid) { - std::vector vopret; uint8_t e,f; - - GetOpReturnData(scriptPubKey,vopret); - if (vopret.size() > 2 && E_UNMARSHAL(vopret, ss >> e; ss >> f; ss >> pk; ss >> height; ss >> amount; ss >> leverage; ss >> firstprice; ss >> vec; ss >> tokenid) != 0 && e == EVAL_PRICES && f == 'B') - { - return(f); - } return(0); } CScript prices_addopret(uint256 bettxid,CPubKey mypk,int64_t amount) { CScript opret; - opret << OP_RETURN << E_MARSHAL(ss << EVAL_PRICES << 'A' << bettxid << mypk << amount); return(opret); } uint8_t prices_addopretdecode(CScript scriptPubKey,uint256 &bettxid,CPubKey &pk,int64_t &amount) { - std::vector vopret; uint8_t e,f; - GetOpReturnData(scriptPubKey,vopret); - if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> bettxid; ss >> pk; ss >> amount) != 0 && e == EVAL_PRICES && f == 'A' ) - { - return(f); - } return(0); } CScript prices_costbasisopret(uint256 bettxid,CPubKey mypk,int32_t height,int64_t costbasis) { CScript opret; - opret << OP_RETURN << E_MARSHAL(ss << EVAL_PRICES << 'C' << bettxid << mypk << height << costbasis); return(opret); } uint8_t prices_costbasisopretdecode(CScript scriptPubKey,uint256 &bettxid,CPubKey &pk,int32_t &height,int64_t &costbasis) { - std::vector vopret; uint8_t e,f; - GetOpReturnData(scriptPubKey,vopret); - if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> bettxid; ss >> pk; ss >> height; ss >> costbasis) != 0 && e == EVAL_PRICES && f == 'C' ) - { - return(f); - } return(0); } CScript prices_finalopret(bool isRekt, uint256 bettxid, CPubKey pk, int32_t lastheight, int64_t costbasis, int64_t lastprice, int64_t liquidationprice, int64_t equity, int64_t exitfee, uint32_t nonce) { CScript opret; - opret << OP_RETURN << E_MARSHAL(ss << EVAL_PRICES << (isRekt ? 'R' : 'F') << bettxid << pk << lastheight << costbasis << lastprice << liquidationprice << equity << exitfee << nonce); return(opret); } uint8_t prices_finalopretdecode(CScript scriptPubKey, uint256 &bettxid, CPubKey &pk, int32_t &lastheight, int64_t &costbasis, int64_t &lastprice, int64_t &liquidationprice, int64_t &equity, int64_t &exitfee) { - std::vector vopret; uint8_t e,f; - uint32_t nonce; - - GetOpReturnData(scriptPubKey,vopret); - if (vopret.size() > 2 && E_UNMARSHAL(vopret, ss >> e; ss >> f; ss >> bettxid; ss >> pk; ss >> lastheight; ss >> costbasis; ss >> lastprice; ss >> liquidationprice; ss >> equity; ss >> exitfee; if (!ss.eof()) ss >> nonce; ) != 0 && e == EVAL_PRICES && (f == 'F' || f == 'R')) - { - return(f); - } return(0); } // price opret basic validation and retrieval static uint8_t PricesCheckOpret(const CTransaction & tx, vscript_t &opret) { - if (tx.vout.size() > 0 && GetOpReturnData(tx.vout.back().scriptPubKey, opret) && opret.size() > 2 && opret.begin()[0] == EVAL_PRICES && IS_CHARINSTR(opret.begin()[1], "BACFR")) - return opret.begin()[1]; - else - return (uint8_t)0; + return (uint8_t)0; } // validate bet tx helper static bool ValidateBetTx(struct CCcontract_info *cp, Eval *eval, const CTransaction & bettx) { - uint256 tokenid; - int64_t positionsize, firstprice; - int32_t firstheight; - int16_t leverage; - CPubKey pk, pricespk; - std::vector vec; - - // check payment cc config: - if ( ASSETCHAINS_EARLYTXIDCONTRACT == EVAL_PRICES && HUSH_EARLYTXID_SCRIPTPUB.size() == 0 ) - GetHushEarlytxidScriptPub(); - - if (bettx.vout.size() < 6 || bettx.vout.size() > 7) - return eval->Invalid("incorrect vout number for bet tx"); - - vscript_t opret; - if( prices_betopretdecode(bettx.vout.back().scriptPubKey, pk, firstheight, positionsize, leverage, firstprice, vec, tokenid) != 'B') - return eval->Invalid("cannot decode opreturn for bet tx"); - - pricespk = GetUnspendable(cp, 0); - - if (MakeCC1vout(cp->evalcode, bettx.vout[0].nValue, pk) != bettx.vout[0]) - return eval->Invalid("cannot validate vout0 in bet tx with pk from opreturn"); - if (MakeCC1vout(cp->evalcode, bettx.vout[1].nValue, pricespk) != bettx.vout[1]) - return eval->Invalid("cannot validate vout1 in bet tx with global pk"); - if (MakeCC1vout(cp->evalcode, bettx.vout[2].nValue, pricespk) != bettx.vout[2] ) - return eval->Invalid("cannot validate vout2 in bet tx with pk from opreturn"); - // This should be all you need to verify it, maybe also check amount? - if ( bettx.vout[4].scriptPubKey != HUSH_EARLYTXID_SCRIPTPUB ) - return eval->Invalid("the fee was paid to wrong address."); - - int64_t betamount = bettx.vout[2].nValue; - if (betamount != PRICES_SUBREVSHAREFEE(positionsize)) { - return eval->Invalid("invalid position size in the opreturn"); - } - - // validate if normal inputs are really signed by originator pubkey (someone not cheating with originator pubkey) - CAmount ccOutputs = 0; - for (auto vout : bettx.vout) - if (vout.scriptPubKey.IsPayToCryptoCondition()) - ccOutputs += vout.nValue; - CAmount normalInputs = TotalPubkeyNormalInputs(bettx, pk); - if (normalInputs < ccOutputs) { - return eval->Invalid("bettx normal inputs not signed with pubkey in opret"); - } - - if (leverage > PRICES_MAXLEVERAGE || leverage < -PRICES_MAXLEVERAGE) { - return eval->Invalid("invalid leverage"); - } - return true; } // validate add funding tx helper static bool ValidateAddFundingTx(struct CCcontract_info *cp, Eval *eval, const CTransaction & addfundingtx, const CTransaction & vintx) { - uint256 bettxid; - int64_t amount; - CPubKey pk, pricespk; - vscript_t vintxOpret; - - // check payment cc config: - if (ASSETCHAINS_EARLYTXIDCONTRACT == EVAL_PRICES && HUSH_EARLYTXID_SCRIPTPUB.size() == 0) - GetHushEarlytxidScriptPub(); - - if (addfundingtx.vout.size() < 4 || addfundingtx.vout.size() > 5) - return eval->Invalid("incorrect vout number for add funding tx"); - - vscript_t opret; - if (prices_addopretdecode(addfundingtx.vout.back().scriptPubKey, bettxid, pk, amount) != 'A') - return eval->Invalid("cannot decode opreturn for add funding tx"); - - pricespk = GetUnspendable(cp, 0); - uint8_t vintxFuncId = PricesCheckOpret(vintx, vintxOpret); - if (vintxFuncId != 'A' && vintxFuncId != 'B') { // if vintx is bettx - return eval->Invalid("incorrect vintx funcid"); - } - - if (vintxFuncId == 'B' && vintx.GetHash() != bettxid) {// if vintx is bettx - return eval->Invalid("incorrect bet txid in opreturn"); - } - - if (MakeCC1vout(cp->evalcode, addfundingtx.vout[0].nValue, pk) != addfundingtx.vout[0]) - return eval->Invalid("cannot validate vout0 in add funding tx with pk from opreturn"); - if (MakeCC1vout(cp->evalcode, addfundingtx.vout[1].nValue, pricespk) != addfundingtx.vout[1]) - return eval->Invalid("cannot validate vout1 in add funding tx with global pk"); - - // This should be all you need to verify it, maybe also check amount? - if (addfundingtx.vout[2].scriptPubKey != HUSH_EARLYTXID_SCRIPTPUB) - return eval->Invalid("the fee was paid to wrong address."); - - int64_t betamount = addfundingtx.vout[1].nValue; - if (betamount != PRICES_SUBREVSHAREFEE(amount)) { - return eval->Invalid("invalid bet position size in the opreturn"); - } - return true; } -// validate costbasis tx helper (deprecated) -/* -static bool ValidateCostbasisTx(struct CCcontract_info *cp, Eval *eval, const CTransaction & costbasistx, const CTransaction & bettx) -{ - uint256 bettxid; - int64_t costbasisInOpret; - CPubKey pk, pricespk; - int32_t height; - - return true; //deprecated - - // check basic structure: - if (costbasistx.vout.size() < 3 || costbasistx.vout.size() > 4) - return eval->Invalid("incorrect vout count for costbasis tx"); - - vscript_t opret; - if (prices_costbasisopretdecode(costbasistx.vout.back().scriptPubKey, bettxid, pk, height, costbasisInOpret) != 'C') - return eval->Invalid("cannot decode opreturn for costbasis tx"); - - pricespk = GetUnspendable(cp, 0); - if (CTxOut(costbasistx.vout[0].nValue, CScript() << ParseHex(HexStr(pk)) << OP_CHECKSIG) != costbasistx.vout[0]) //might go to any pk who calculated costbasis - return eval->Invalid("cannot validate vout0 in costbasis tx with pk from opreturn"); - if (MakeCC1vout(cp->evalcode, costbasistx.vout[1].nValue, pricespk) != costbasistx.vout[1]) - return eval->Invalid("cannot validate vout1 in costbasis tx with global pk"); - - if (bettx.GetHash() != bettxid) - return eval->Invalid("incorrect bettx id"); - - if (bettx.vout.size() < 1) // for safety and for check encapsulation - return eval->Invalid("incorrect bettx no vouts"); - - // check costbasis rules: - if (costbasistx.vout[0].nValue > bettx.vout[1].nValue / 10) { - return eval->Invalid("costbasis myfee too big"); - } - - uint256 tokenid; - int64_t positionsize, firstprice; - int32_t firstheight; - int16_t leverage; - CPubKey betpk; - std::vector vec; - if (prices_betopretdecode(bettx.vout.back().scriptPubKey, betpk, firstheight, positionsize, leverage, firstprice, vec, tokenid) != 'B') - return eval->Invalid("cannot decode opreturn for bet tx"); - - if (firstheight + PRICES_DAYWINDOW + PRICES_SMOOTHWIDTH > chainActive.Height()) { - return eval->Invalid("cannot calculate costbasis yet"); - } - - int64_t costbasis = 0, profits, lastprice; - int32_t retcode = prices_syntheticprofits(costbasis, firstheight, firstheight + PRICES_DAYWINDOW, leverage, vec, positionsize, profits, lastprice); - if (retcode < 0) - return eval->Invalid("cannot calculate costbasis yet"); - std::cerr << "ValidateCostbasisTx() costbasis=" << costbasis << " costbasisInOpret=" << costbasisInOpret << std::endl; - if (costbasis != costbasisInOpret) { - //std::cerr << "ValidateBetTx() " << "incorrect costbasis value" << std::endl; - return eval->Invalid("incorrect costbasis value"); - } - - return true; -} -*/ // validate final tx helper static bool ValidateFinalTx(struct CCcontract_info *cp, Eval *eval, const CTransaction & finaltx, const CTransaction & bettx) { - uint256 bettxid; - int64_t amount; - CPubKey pk, pricespk; - int64_t profits; - int32_t lastheight; - int64_t firstprice, costbasis, lastprice, liquidationprice, equity, fee; - int16_t leverage; - - if (finaltx.vout.size() < 3 || finaltx.vout.size() > 4) { - //std::cerr << "ValidateFinalTx()" << " incorrect vout number for final tx =" << finaltx.vout.size() << std::endl; - return eval->Invalid("incorrect vout number for final tx"); - } - - vscript_t opret; - uint8_t funcId; - if ((funcId = prices_finalopretdecode(finaltx.vout.back().scriptPubKey, bettxid, pk, lastheight, costbasis, lastprice, liquidationprice, equity, fee)) == 0) - return eval->Invalid("cannot decode opreturn for final tx"); - - // check rekt txid mining: -// if( funcId == 'R' && (finaltx.GetHash().begin()[0] != 0 || finaltx.GetHash().begin()[31] != 0) ) -// return eval->Invalid("incorrect rekt txid"); - - if (bettx.GetHash() != bettxid) - return eval->Invalid("incorrect bettx id"); - - pricespk = GetUnspendable(cp, 0); - - if (CTxOut(finaltx.vout[0].nValue, CScript() << ParseHex(HexStr(pk)) << OP_CHECKSIG) != finaltx.vout[0]) - return eval->Invalid("cannot validate vout0 in final tx with pk from opreturn"); - - if( finaltx.vout.size() == 3 && MakeCC1vout(cp->evalcode, finaltx.vout[1].nValue, pricespk) != finaltx.vout[1] ) - return eval->Invalid("cannot validate vout1 in final tx with global pk"); - - // TODO: validate exitfee for 'R' - // TODO: validate amount for 'F' - return true; } -// validate prices tx function -// performed checks: -// basic tx structure (vout num) -// basic tx opret structure -// reference to the bet tx vout -// referenced bet txid in tx opret -// referenced bet tx structure -// non-final tx has only 1 cc vin -// cc vouts to self with mypubkey from opret -// cc vouts to global pk with global pk -// for bet tx that normal inputs digned with my pubkey from the opret >= cc outputs - disable betting for other pubkeys (Do we need this rule?) -// TODO: -// opret params (firstprice,positionsize...) -// costbasis calculation -// cashout balance (PricesExactAmounts) -// use the special address for 50% fees bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn) { - vscript_t vopret; - - if (strcmp(SMART_CHAIN_SYMBOL, "REKT0") == 0 && chainActive.Height() < 5851) - return true; - // check basic opret rules: - if (PricesCheckOpret(tx, vopret) == 0) - return eval->Invalid("tx has no prices opreturn"); - - uint8_t funcId = vopret.begin()[1]; - - CTransaction firstVinTx; - vscript_t firstVinTxOpret; - bool foundFirst = false; - int32_t ccVinCount = 0; - uint32_t prevCCoutN = 0; - - // check basic rules: - - // find first cc vin and load vintx (might be either bet or add funding tx): - for (auto vin : tx.vin) { - if (cp->ismyvin(vin.scriptSig)) { - CTransaction vintx; - uint256 hashBlock; - vscript_t vintxOpret; - - if (!myGetTransaction(vin.prevout.hash, vintx, hashBlock)) - return eval->Invalid("cannot load vintx"); - - if (PricesCheckOpret(vintx, vintxOpret) == 0) { - //return eval->Invalid("cannot find prices opret in vintx"); - std::cerr << "PricesValidate() " << "cannot find prices opret in vintx" << std::endl; - } - - if (!IS_CHARINSTR(funcId, "FR") && vintxOpret.begin()[1] == 'B' && prevCCoutN == 1) { - //return eval->Invalid("cannot spend bet marker"); - std::cerr << "PricesValidate() " << " non-final tx cannot spend cc marker vout=" << prevCCoutN << std::endl; - } - - if (!foundFirst) { - prevCCoutN = vin.prevout.n; - firstVinTx = vintx; - firstVinTxOpret = vintxOpret; - foundFirst = true; - } - ccVinCount++; - } - } - if (!foundFirst) - return eval->Invalid("prices cc vin not found"); - - if (!IS_CHARINSTR(funcId, "FR") && ccVinCount > 1) {// for all prices tx except final tx only one cc vin is allowed - //return eval->Invalid("only one prices cc vin allowed for this tx"); - std::cerr << "PricesValidate() " << "only one prices cc vin allowed for this tx" << std::endl; - } - - switch (funcId) { - case 'B': // bet - return eval->Invalid("unexpected validate for bet funcid"); - - case 'A': // add funding - // check tx structure: - if (!ValidateAddFundingTx(cp, eval, tx, firstVinTx)) { - std::cerr << "PricesValidate() " << "ValidateAddFundingTx = false " << eval->state.GetRejectReason() << std::endl; - return false; // invalid state is already set in the func - } - - if (firstVinTxOpret.begin()[1] == 'B') { - if (!ValidateBetTx(cp, eval, firstVinTx)) {// check tx structure - std::cerr << "PricesValidate() " << "funcId=A ValidatebetTx = false " << eval->state.GetRejectReason() << std::endl; - return false; // invalid state is already set in the func - } - } - - if (prevCCoutN != 0) { // check spending rules - std::cerr << "PricesValidate() " << "addfunding tx incorrect vout to spend=" << prevCCoutN << std::endl; - return eval->Invalid("incorrect vintx vout to spend"); - } - break; - - /* not used: - case 'C': // set costbasis - if (!ValidateCostbasisTx(cp, eval, tx, firstVinTx)) { - //return false; - std::cerr << "PricesValidate() " << "ValidateCostbasisTx=false " << eval->state.GetRejectReason() << std::endl; - } - if (!ValidateBetTx(cp, eval, firstVinTx)) { - //return false; - std::cerr << "PricesValidate() " << "funcId=C ValidateBetTx=false " << eval->state.GetRejectReason() << std::endl; - } - if (prevoutN != 1) { // check spending rules - // return eval->Invalid("incorrect vout to spend"); - std::cerr << "PricesValidate() " << "costbasis tx incorrect vout to spend=" << prevoutN << std::endl; - } - //return eval->Invalid("test: costbasis is good"); - break; */ - - case 'F': // final tx - case 'R': - if (!ValidateFinalTx(cp, eval, tx, firstVinTx)) { - std::cerr << "PricesValidate() " << "ValidateFinalTx=false " << eval->state.GetRejectReason() << std::endl; - return false; - } - if (!ValidateBetTx(cp, eval, firstVinTx)) { - std::cerr << "PricesValidate() " << "ValidateBetTx=false " << eval->state.GetRejectReason() << std::endl; - return false; - } - if (prevCCoutN != 1) { // check spending rules - std::cerr << "PricesValidate() "<< "final tx incorrect vout to spend=" << prevCCoutN << std::endl; - return eval->Invalid("incorrect vout to spend"); - } - break; - - default: - return eval->Invalid("invalid funcid"); - } - - eval->state = CValidationState(); return true; } -// end of consensus code - // helper functions for rpc calls in rpcwallet.cpp int64_t AddPricesInputs(struct CCcontract_info *cp, CMutableTransaction &mtx, char *destaddr, int64_t total, int32_t maxinputs) { - int64_t nValue, price, totalinputs = 0; uint256 txid, hashBlock; std::vector origpubkey; CTransaction vintx; int32_t vout, n = 0; - std::vector > unspentOutputs; - - SetCCunspents(unspentOutputs, destaddr); - for (std::vector >::const_iterator it = unspentOutputs.begin(); it != unspentOutputs.end(); it++) - { - txid = it->first.txhash; - vout = (int32_t)it->first.index; - //if (vout == exclvout && txid == excltxid) // exclude vout which is added directly to vins outside this function - // continue; - if (myGetTransaction(txid, vintx, hashBlock) != 0 && vout < vintx.vout.size()) - { - vscript_t vopret; - uint8_t funcId = PricesCheckOpret(vintx, vopret); - if (funcId == 'B' && vout == 1) // skip cc marker - continue; - - if ((nValue = vintx.vout[vout].nValue) >= total / maxinputs && myIsutxo_spentinmempool(ignoretxid, ignorevin, txid, vout) == 0) - { - if (total != 0 && maxinputs != 0) - mtx.vin.push_back(CTxIn(txid, vout, CScript())); - nValue = it->second.satoshis; - totalinputs += nValue; - n++; - if ((total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs)) - break; - } - } - } + int64_t totalinputs = 0; return(totalinputs); } -// return min equity percentage depending on leverage value -// for lev=1 2% -// for lev>=100 10% double prices_minmarginpercent(int16_t leverage) { - int16_t absleverage = std::abs(leverage); - if (absleverage < 100) - return (absleverage * 0.080808 + 1.9191919) / 100.0; - else - return 0.1; + return 0.0; } UniValue prices_rawtxresult(UniValue &result, std::string rawtx, int32_t broadcastflag) { - CTransaction tx; - if (rawtx.size() > 0) - { - result.push_back(Pair("hex", rawtx)); - if (DecodeHexTx(tx, rawtx) != 0) - { - if (broadcastflag != 0 && myAddtomempool(tx) != 0) - RelayTransaction(tx); - result.push_back(Pair("txid", tx.GetHash().ToString())); - result.push_back(Pair("result", "success")); - } - else - result.push_back(Pair("error", "decode hex")); - } - else - result.push_back(Pair("error", "couldnt finalize CCtx")); return(result); } static std::string prices_getsourceexpression(const std::vector &vec) { - std::string expr; - - for (int32_t i = 0; i < vec.size(); i++) - { - char name[65]; - std::string operand; - uint16_t opcode = vec[i]; - int32_t value = (opcode & (HUSH_MAXPRICES - 1)); // index or weight - - switch (opcode & HUSH_PRICEMASK) - { - case 0: // indices - komodo_pricename(name, value); - operand = std::string(name); - break; - - case PRICES_WEIGHT: // multiply by weight and consume top of stack by updating price - operand = std::to_string(value); - break; - - case PRICES_MULT: // "*" - operand = std::string("*"); - break; - - case PRICES_DIV: // "/" - operand = std::string("/"); - break; - - case PRICES_INV: // "!" - operand = std::string("!"); - break; - - case PRICES_MDD: // "*//" - operand = std::string("*//"); - break; - - case PRICES_MMD: // "**/" - operand = std::string("**/"); - break; - - case PRICES_MMM: // "***" - operand = std::string("***"); - break; - - case PRICES_DDD: // "///" - operand = std::string("///"); - break; - - default: - return "invalid opcode"; - break; - } - - if (expr.size() > 0) - expr += std::string(", "); - expr += operand; - } return expr; } @@ -715,1002 +194,102 @@ static std::string prices_getsourceexpression(const std::vector &vec) // return s true and needed operand count if string is opcode static bool prices_isopcode(const std::string &s, int &need) { - if (s == "!") { - need = 1; - return true; - } - else if (s == "*" || s == "/") { - need = 2; - return true; - } - else if (s == "***" || s == "///" || s == "*//" || s == "**/") { - need = 3; - return true; - } - else - return false; + return false; } // split pair onto two quotes divided by "_" static void prices_splitpair(const std::string &pair, std::string &upperquote, std::string &bottomquote) { - size_t pos = pair.find('_'); // like BTC_USD - if (pos != std::string::npos) { - upperquote = pair.substr(0, pos); - bottomquote = pair.substr(pos + 1); - } - else { - upperquote = pair; - bottomquote = ""; - } - //std::cerr << "prices_splitpair: upperquote=" << upperquote << " bottomquote=" << bottomquote << std::endl; } // invert pair like BTS_USD -> USD_BTC static std::string prices_invertpair(const std::string &pair) { - std::string upperquote, bottomquote; - prices_splitpair(pair, upperquote, bottomquote); - return bottomquote + std::string("_") + upperquote; + std::string s; + return s; } // invert pairs in operation accordingly to "/" operator, convert operator to * or *** static void prices_invertoperation(const std::vector &vexpr, int p, std::vector &voperation) { - int need; - - voperation.clear(); - if (prices_isopcode(vexpr[p], need)) { - if (need > 1) { - if (need == 2) { - voperation.push_back(vexpr[p - 2]); - if (vexpr[p] == "/") - voperation.push_back(prices_invertpair(vexpr[p - 1])); - else - voperation.push_back(vexpr[p - 1]); - voperation.push_back("*"); - } - - if (need == 3) { - int i; - std::string::const_iterator c; - for (c = vexpr[p].begin(), i = -3; c != vexpr[p].end(); c++, i++) { - if (*c == '/') - voperation.push_back(prices_invertpair(vexpr[p + i])); - else - voperation.push_back(vexpr[p + i]); - } - voperation.push_back("***"); - } - } - else if (vexpr[p] == "!") { - voperation.push_back(prices_invertpair(vexpr[p - 1])); - // do not add operator - } - } - - //std::cerr << "prices_invert inverted="; - //for (auto v : voperation) std::cerr << v << " "; - //std::cerr << std::endl; } // reduce pairs in the operation, change or remove opcode if reduced static int prices_reduceoperands(std::vector &voperation) { - int opcount = voperation.size() - 1; - int need = opcount; - //std::cerr << "prices_reduceoperands begin need=" << need << std::endl; - - while (true) { - int i; - //std::cerr << "prices_reduceoperands opcount=" << opcount << std::endl; - for (i = 0; i < opcount; i++) { - std::string upperquote, bottomquote; - bool breaktostart = false; - - //std::cerr << "prices_reduceoperands voperation[i]=" << voperation[i] << " i=" << i << std::endl; - prices_splitpair(voperation[i], upperquote, bottomquote); - if (upperquote == bottomquote) { - std::cerr << "prices_reduceoperands erasing i=" << i << std::endl; - voperation.erase(voperation.begin() + i); - opcount--; - //std::cerr << "prices_reduceoperands erased, size=" << voperation.size() << std::endl; - - if (voperation.size() > 0 && voperation.back() == "*") - voperation.pop_back(); - breaktostart = true; - break; - } - - - int j; - for (j = i + 1; j < opcount; j++) { - - //std::cerr << "prices_reduceoperands voperation[j]=" << voperation[j] << " j=" << j << std::endl; - - std::string upperquotej, bottomquotej; - prices_splitpair(voperation[j], upperquotej, bottomquotej); - if (upperquote == bottomquotej || bottomquote == upperquotej) { - if (upperquote == bottomquotej) - voperation[i] = upperquotej + "_" + bottomquote; - else - voperation[i] = upperquote + "_" + bottomquotej; - //std::cerr << "prices_reduceoperands erasing j=" << j << std::endl; - voperation.erase(voperation.begin() + j); - opcount--; - //std::cerr << "prices_reduceoperands erased, size=" << voperation.size() << std::endl; - - need--; - if (voperation.back() == "***") { - voperation.pop_back(); - voperation.push_back("*"); // convert *** to * - } - else if (voperation.back() == "*") { - voperation.pop_back(); // convert * to nothing - } - breaktostart = true; - break; - } - } - if (breaktostart) - break; - } - if (i >= opcount) // all seen - break; - } - - //std::cerr << "prices_reduceoperands end need=" << need << std::endl; - return need; + return 0; } // substitute reduced operation in vectored expr static void prices_substitutereduced(std::vector &vexpr, int p, std::vector voperation) { - int need; - if (prices_isopcode(vexpr[p], need)) { - vexpr.erase(vexpr.begin() + p - need, vexpr.begin() + p + 1); - vexpr.insert(vexpr.begin() + p - need, voperation.begin(), voperation.end()); - } } // try to reduce synthetic expression by substituting "BTC_USD, BTC_EUR, 30, /" with "EUR_USD, 30" etc static std::string prices_getreducedexpr(const std::string &expr) { std::string reduced; - - std::vector vexpr; - SplitStr(expr, vexpr); - - for (size_t i = 0; i < vexpr.size(); i++) { - int need; - - if (prices_isopcode(vexpr[i], need)) { - std::vector voperation; - prices_invertoperation(vexpr, i, voperation); - if (voperation.size() > 0) { - int reducedneed = prices_reduceoperands(voperation); - if (reducedneed < need) { - prices_substitutereduced(vexpr, i, voperation); - } - } - } - } - - for (size_t i = 0; i < vexpr.size(); i++) { - if (reduced.size() > 0) - reduced += std::string(", "); - reduced += vexpr[i]; - } - - //std::cerr << "reduced=" << reduced << std::endl; return reduced; } // parse synthetic expression into vector of codes int32_t prices_syntheticvec(std::vector &vec, std::vector synthetic) { - int32_t i, need, ind, depth = 0; std::string opstr; uint16_t opcode, weight; - if (synthetic.size() == 0) { - std::cerr << "prices_syntheticvec() expression is empty" << std::endl; - return(-1); - } - for (i = 0; i < synthetic.size(); i++) - { - need = 0; - opstr = synthetic[i]; - if (opstr == "*") - opcode = PRICES_MULT, need = 2; - else if (opstr == "/") - opcode = PRICES_DIV, need = 2; - else if (opstr == "!") - opcode = PRICES_INV, need = 1; - else if (opstr == "**/") - opcode = PRICES_MMD, need = 3; - else if (opstr == "*//") - opcode = PRICES_MDD, need = 3; - else if (opstr == "***") - opcode = PRICES_MMM, need = 3; - else if (opstr == "///") - opcode = PRICES_DDD, need = 3; - else if (!is_weight_str(opstr) && (ind = komodo_priceind(opstr.c_str())) >= 0) - opcode = ind, need = 0; - else if ((weight = atoi(opstr.c_str())) > 0 && weight < HUSH_MAXPRICES) - { - opcode = PRICES_WEIGHT | weight; - need = 1; - } - else { - std::cerr << "prices_syntheticvec() incorrect opcode=" << opstr << std::endl; - return(-2); - } - if (depth < need) { - std::cerr << "prices_syntheticvec() incorrect not enough operands for opcode=" << opstr << std::endl; - return(-3); - } - depth -= need; - ///std::cerr << "prices_syntheticvec() opcode=" << opcode << " opstr=" << opstr << " need=" << need << " depth=" << depth << std::endl; - if ((opcode & HUSH_PRICEMASK) != PRICES_WEIGHT) { // skip weight - depth++; // increase operands count - ///std::cerr << "depth++=" << depth << std::endl; - } - if (depth > 3) { - std::cerr << "prices_syntheticvec() too many operands, last=" << opstr << std::endl; - return(-4); - } - vec.push_back(opcode); - } - if (depth != 0) - { - fprintf(stderr, "prices_syntheticvec() depth.%d not empty\n", depth); - return(-5); - } return(0); } // calculates price for synthetic expression int64_t prices_syntheticprice(std::vector vec, int32_t height, int32_t minmax, int16_t leverage) { - int32_t i, value, errcode, depth, retval = -1; - uint16_t opcode; - int64_t *pricedata, pricestack[4], a, b, c; - - mpz_t mpzTotalPrice, mpzPriceValue, mpzDen, mpzA, mpzB, mpzC, mpzResult; - - mpz_init(mpzTotalPrice); - mpz_init(mpzPriceValue); - mpz_init(mpzDen); - - mpz_init(mpzA); - mpz_init(mpzB); - mpz_init(mpzC); - mpz_init(mpzResult); - - pricedata = (int64_t *)calloc(sizeof(*pricedata) * 3, 1 + PRICES_DAYWINDOW * 2 + PRICES_SMOOTHWIDTH); - depth = errcode = 0; - mpz_set_si(mpzTotalPrice, 0); - mpz_set_si(mpzDen, 0); - - for (i = 0; i < vec.size(); i++) - { - opcode = vec[i]; - value = (opcode & (HUSH_MAXPRICES - 1)); // index or weight - - mpz_set_ui(mpzResult, 0); // clear result to test overflow (see below) - - //std::cerr << "prices_syntheticprice" << " i=" << i << " mpzTotalPrice=" << mpz_get_si(mpzTotalPrice) << " value=" << value << " depth=" << depth << " opcode&HUSH_PRICEMASK=" << (opcode & HUSH_PRICEMASK) <= 0) - { - //std::cerr << "prices_syntheticprice" << " pricedata[0]=" << pricedata[0] << " pricedata[1]=" << pricedata[1] << " pricedata[2]=" << pricedata[2] << std::endl; - // push price to the prices stack - /*if (!minmax) - pricestack[depth] = pricedata[2]; // use smoothed value if we are over 24h - else - { - // if we are within 24h use min or max price - if (leverage > 0) - pricestack[depth] = (pricedata[1] > pricedata[2]) ? pricedata[1] : pricedata[2]; // MAX - else - pricestack[depth] = (pricedata[1] < pricedata[2]) ? pricedata[1] : pricedata[2]; // MIN - }*/ - pricestack[depth] = pricedata[2]; - } - else - errcode = -1; - - if (pricestack[depth] == 0) - errcode = -14; - - depth++; - break; - - case PRICES_WEIGHT: // multiply by weight and consume top of stack by updating price - if (depth == 1) { - depth--; - // price += pricestack[0] * value; - mpz_set_si(mpzPriceValue, pricestack[0]); - mpz_mul_si(mpzPriceValue, mpzPriceValue, value); - mpz_add(mpzTotalPrice, mpzTotalPrice, mpzPriceValue); // accumulate weight's value - - // den += value; - mpz_add_ui(mpzDen, mpzDen, (uint64_t)value); // accumulate weight's value - } - else - errcode = -2; - break; - - case PRICES_MULT: // "*" - if (depth >= 2) { - b = pricestack[--depth]; - a = pricestack[--depth]; - // pricestack[depth++] = (a * b) / SATOSHIDEN; - mpz_set_si(mpzA, a); - mpz_set_si(mpzB, b); - mpz_mul(mpzResult, mpzA, mpzB); - mpz_tdiv_q_ui(mpzResult, mpzResult, SATOSHIDEN); - pricestack[depth++] = mpz_get_si(mpzResult); - - } - else - errcode = -3; - break; - - case PRICES_DIV: // "/" - if (depth >= 2) { - b = pricestack[--depth]; - a = pricestack[--depth]; - // pricestack[depth++] = (a * SATOSHIDEN) / b; - mpz_set_si(mpzA, a); - mpz_set_si(mpzB, b); - mpz_mul_ui(mpzResult, mpzA, SATOSHIDEN); - mpz_tdiv_q(mpzResult, mpzResult, mpzB); - pricestack[depth++] = mpz_get_si(mpzResult); - } - else - errcode = -4; - break; - - case PRICES_INV: // "!" - if (depth >= 1) { - a = pricestack[--depth]; - // pricestack[depth++] = (SATOSHIDEN * SATOSHIDEN) / a; - mpz_set_si(mpzA, a); - mpz_set_ui(mpzResult, SATOSHIDEN); - mpz_mul_ui(mpzResult, mpzResult, SATOSHIDEN); - mpz_tdiv_q(mpzResult, mpzResult, mpzA); - pricestack[depth++] = mpz_get_si(mpzResult); - } - else - errcode = -5; - break; - - case PRICES_MDD: // "*//" - if (depth >= 3) { - c = pricestack[--depth]; - b = pricestack[--depth]; - a = pricestack[--depth]; - // pricestack[depth++] = (((a * SATOSHIDEN) / b) * SATOSHIDEN) / c; - mpz_set_si(mpzA, a); - mpz_set_si(mpzB, b); - mpz_set_si(mpzC, c); - mpz_mul_ui(mpzResult, mpzA, SATOSHIDEN); - mpz_tdiv_q(mpzResult, mpzResult, mpzB); - mpz_mul_ui(mpzResult, mpzResult, SATOSHIDEN); - mpz_tdiv_q(mpzResult, mpzResult, mpzC); - pricestack[depth++] = mpz_get_si(mpzResult); - } - else - errcode = -6; - break; - - case PRICES_MMD: // "**/" - if (depth >= 3) { - c = pricestack[--depth]; - b = pricestack[--depth]; - a = pricestack[--depth]; - // pricestack[depth++] = (a * b) / c; - mpz_set_si(mpzA, a); - mpz_set_si(mpzB, b); - mpz_set_si(mpzC, c); - mpz_mul(mpzResult, mpzA, mpzB); - mpz_tdiv_q(mpzResult, mpzResult, mpzC); - pricestack[depth++] = mpz_get_si(mpzResult); - } - else - errcode = -7; - break; - - case PRICES_MMM: // "***" - if (depth >= 3) { - c = pricestack[--depth]; - b = pricestack[--depth]; - a = pricestack[--depth]; - // pricestack[depth++] = (((a * b) / SATOSHIDEN ) * c) / SATOSHIDEN; - mpz_set_si(mpzA, a); - mpz_set_si(mpzB, b); - mpz_set_si(mpzC, c); - mpz_mul(mpzResult, mpzA, mpzB); - mpz_tdiv_q_ui(mpzResult, mpzResult, SATOSHIDEN); - mpz_mul(mpzResult, mpzResult, mpzC); - mpz_tdiv_q_ui(mpzResult, mpzResult, SATOSHIDEN); - pricestack[depth++] = mpz_get_si(mpzResult); - } - else - errcode = -8; - break; - - case PRICES_DDD: // "///" - if (depth >= 3) { - c = pricestack[--depth]; - b = pricestack[--depth]; - a = pricestack[--depth]; - //pricestack[depth++] = (((((SATOSHIDEN * SATOSHIDEN) / a) * SATOSHIDEN) / b) * SATOSHIDEN) / c; - mpz_set_si(mpzA, a); - mpz_set_si(mpzB, b); - mpz_set_si(mpzC, c); - mpz_set_ui(mpzResult, SATOSHIDEN); - mpz_mul_ui(mpzResult, mpzResult, SATOSHIDEN); - mpz_tdiv_q(mpzResult, mpzResult, mpzA); - mpz_mul_ui(mpzResult, mpzResult, SATOSHIDEN); - mpz_tdiv_q(mpzResult, mpzResult, mpzB); - mpz_mul_ui(mpzResult, mpzResult, SATOSHIDEN); - mpz_tdiv_q(mpzResult, mpzResult, mpzC); - pricestack[depth++] = mpz_get_si(mpzResult); - } - else - errcode = -9; - break; - - default: - errcode = -10; - break; - } - - // std::cerr << "prices_syntheticprice test mpzResult=" << mpz_get_si(mpzResult) << std::endl; - // check overflow: - if (mpz_cmp_si(mpzResult, std::numeric_limits::max()) > 0) { - errcode = -13; - break; - } - - if (errcode != 0) - break; - - // if( depth > 0 ) - // std::cerr << "prices_syntheticprice top pricestack[depth-1=" << depth-1 << "]=" << pricestack[depth-1] << std::endl; - // else - // std::cerr << "prices_syntheticprice pricestack empty" << std::endl; - - } - free(pricedata); - mpz_clear(mpzResult); - mpz_clear(mpzA); - mpz_clear(mpzB); - mpz_clear(mpzC); - - if( mpz_get_si(mpzDen) != 0 ) - mpz_tdiv_q(mpzTotalPrice, mpzTotalPrice, mpzDen); // price / den - - int64_t den = mpz_get_si(mpzDen); - int64_t priceIndex = mpz_get_si(mpzTotalPrice); - - mpz_clear(mpzDen); - mpz_clear(mpzTotalPrice); - mpz_clear(mpzPriceValue); - - if (errcode != 0) - std::cerr << "prices_syntheticprice errcode in switch=" << errcode << std::endl; - - if( errcode == -1 ) { - std::cerr << "prices_syntheticprice error getting price (could be end of chain)" << std::endl; - return errcode; - } - - if (errcode == -13) { - std::cerr << "prices_syntheticprice overflow in price" << std::endl; - return errcode; - } - if (errcode == -14) { - std::cerr << "prices_syntheticprice price is zero, not enough historic data yet" << std::endl; - return errcode; - } - if (den == 0) { - std::cerr << "prices_syntheticprice den==0 return err=-11" << std::endl; - return(-11); - } - else if (depth != 0) { - std::cerr << "prices_syntheticprice depth!=0 err=-12" << std::endl; - return(-12); - } - else if (errcode != 0) { - std::cerr << "prices_syntheticprice err=" << errcode << std::endl; - return(errcode); - } -// std::cerr << "prices_syntheticprice priceIndex=totalprice/den=" << priceIndex << " den=" << den << std::endl; - - return priceIndex; + return 0; } // calculates costbasis and profit/loss for the bet int32_t prices_syntheticprofits(int64_t &costbasis, int32_t firstheight, int32_t height, int16_t leverage, std::vector vec, int64_t positionsize, int64_t &profits, int64_t &outprice) { - int64_t price; -#ifndef TESTMODE - const int32_t COSTBASIS_PERIOD = PRICES_DAYWINDOW; -#else - const int32_t COSTBASIS_PERIOD = 7; -#endif - - - if (height < firstheight) { - fprintf(stderr, "requested height is lower than bet firstheight.%d\n", height); - return -1; - } - - int32_t minmax = (height < firstheight + COSTBASIS_PERIOD); // if we are within 24h then use min or max value - - if ((price = prices_syntheticprice(vec, height, minmax, leverage)) < 0) - { - fprintf(stderr, "error getting synthetic price at height.%d\n", height); - return -1; - } - - // clear lowest positions: - //price /= PRICES_POINTFACTOR; - //price *= PRICES_POINTFACTOR; - outprice = price; - - if (minmax) { // if we are within day window, set temp costbasis to max (or min) price value - if (leverage > 0 && price > costbasis) { - costbasis = price; // set temp costbasis - //std::cerr << "prices_syntheticprofits() minmax costbasis=" << costbasis << std::endl; - } - else if (leverage < 0 && (costbasis == 0 || price < costbasis)) { - costbasis = price; - //std::cerr << "prices_syntheticprofits() minmax costbasis=" << costbasis << std::endl; - } - //else { //-> use the previous value - // std::cerr << "prices_syntheticprofits() unchanged costbasis=" << costbasis << " price=" << price << " leverage=" << leverage << std::endl; - //} - } - else { - //if (height == firstheight + COSTBASIS_PERIOD) { - // if costbasis not set, just set it - //costbasis = price; - - // use calculated minmax costbasis - //std::cerr << "prices_syntheticprofits() use permanent costbasis=" << costbasis << " at height=" << height << std::endl; - //} - } - - // normalize to 10,000,000 to prevent underflow - //profits = costbasis > 0 ? (((price / PRICES_POINTFACTOR * PRICES_NORMFACTOR) / costbasis) - PRICES_NORMFACTOR / PRICES_POINTFACTOR) * PRICES_POINTFACTOR : 0; - //double dprofits = costbasis > 0 ? ((double)price / (double)costbasis - 1) : 0; - - //std::cerr << "prices_syntheticprofits() test value1 (price/PRICES_POINTFACTOR * PRICES_NORMFACTOR)=" << (price / PRICES_POINTFACTOR * PRICES_NORMFACTOR) << std::endl; - //std::cerr << "prices_syntheticprofits() test value2 (price/PRICES_POINTFACTOR * PRICES_NORMFACTOR)/costbasis=" << (costbasis != 0 ? (price / PRICES_POINTFACTOR * PRICES_NORMFACTOR)/costbasis : 0) << std::endl; - - //std::cerr << "prices_syntheticprofits() fractional profits=" << profits << std::endl; - //std::cerr << "prices_syntheticprofits() profits double=" << (double)price / (double)costbasis -1.0 << std::endl; - //double dprofits = (double)price / (double)costbasis - 1.0; - - //profits *= ((int64_t)leverage * (int64_t)positionsize); - //profits /= (int64_t)PRICES_NORMFACTOR; // de-normalize - - //dprofits *= ((double)leverage * (double)positionsize); - - //dprofits *= leverage * positionsize; - // profits = dprofits; - //std::cerr << "prices_syntheticprofits() dprofits=" << dprofits << std::endl; - - if (costbasis > 0) { - mpz_t mpzProfits; - mpz_t mpzCostbasis; - mpz_t mpzPrice; - mpz_t mpzLeverage; - - mpz_init(mpzProfits); - mpz_init(mpzCostbasis); - mpz_init(mpzPrice); - mpz_init(mpzLeverage); - - mpz_set_si(mpzCostbasis, costbasis); - mpz_set_si(mpzPrice, price); - mpz_mul_ui(mpzPrice, mpzPrice, SATOSHIDEN); // (price*SATOSHIDEN) - - mpz_tdiv_q(mpzProfits, mpzPrice, mpzCostbasis); // profits = (price*SATOSHIDEN)/costbasis // normalization - mpz_sub_ui(mpzProfits, mpzProfits, SATOSHIDEN); // profits -= SATOSHIDEN - - mpz_set_si(mpzLeverage, leverage); - mpz_mul(mpzProfits, mpzProfits, mpzLeverage); // profits *= leverage - mpz_mul_ui(mpzProfits, mpzProfits, positionsize); // profits *= positionsize - mpz_tdiv_q_ui(mpzProfits, mpzProfits, SATOSHIDEN); // profits /= SATOSHIDEN // de-normalization - - profits = mpz_get_si(mpzProfits); - - mpz_clear(mpzLeverage); - mpz_clear(mpzProfits); - mpz_clear(mpzCostbasis); - mpz_clear(mpzPrice); - - } - else - profits = 0; - - //std::cerr << "prices_syntheticprofits() profits=" << profits << std::endl; - return 0; // (positionsize + addedbets + profits); + return 0; } // makes result json object void prices_betjson(UniValue &result, std::vector bets, int16_t leverage, int32_t endheight, int64_t lastprice) { - - UniValue resultbets(UniValue::VARR); - int64_t totalposition = 0; - int64_t totalprofits = 0; - - for (auto b : bets) { - UniValue entry(UniValue::VOBJ); - entry.push_back(Pair("positionsize", ValueFromAmount(b.positionsize))); - entry.push_back(Pair("profits", ValueFromAmount(b.profits))); - entry.push_back(Pair("costbasis", ValueFromAmount(b.costbasis))); - entry.push_back(Pair("firstheight", b.firstheight)); - resultbets.push_back(entry); - totalposition += b.positionsize; - totalprofits += b.profits; - } - int64_t equity = totalposition + totalprofits; - - result.push_back(Pair("bets", resultbets)); - result.push_back(Pair("leverage", (int64_t)leverage)); - result.push_back(Pair("TotalPositionSize", ValueFromAmount(totalposition))); - result.push_back(Pair("TotalProfits", ValueFromAmount(totalprofits))); - result.push_back(Pair("equity", ValueFromAmount(equity))); - result.push_back(Pair("LastPrice", ValueFromAmount(lastprice))); - result.push_back(Pair("LastHeight", endheight)); } // retrieves costbasis from a tx spending bettx vout1 (deprecated) int64_t prices_costbasis(CTransaction bettx, uint256 &txidCostbasis) { - int64_t costbasis = 0; - // if vout1 is spent, follow and extract costbasis from opreturn - //uint8_t prices_costbasisopretdecode(CScript scriptPubKey,uint256 &bettxid,CPubKey &pk,int32_t &height,int64_t &costbasis) - //uint256 txidCostbasis; - int32_t vini; - int32_t height; - txidCostbasis = zeroid; -/* - if (CCgetspenttxid(txidCostbasis, vini, height, bettx.GetHash(), 1) < 0) { - std::cerr << "prices_costbasis() no costbasis txid found" << std::endl; - return 0; - } - - CTransaction txCostbasis; - uint256 hashBlock; - uint256 bettxid; - CPubKey pk; - bool isLoaded = false; - uint8_t funcId = 0; - - if ((isLoaded = myGetTransaction(txidCostbasis, txCostbasis, hashBlock)) && - txCostbasis.vout.size() > 0 && - (funcId = prices_costbasisopretdecode(txCostbasis.vout.back().scriptPubKey, bettxid, pk, height, costbasis)) != 0) { - return costbasis; - } - - std::cerr << "prices_costbasis() cannot load costbasis tx or decode opret" << " isLoaded=" << isLoaded << " funcId=" << (int)funcId << std::endl; */ return 0; } // enumerates and retrieves added bets, returns the last baton txid int64_t prices_enumaddedbets(uint256 &batontxid, std::vector &bets, uint256 bettxid) { - int64_t addedBetsTotal = 0; - int32_t vini; - int32_t height; - int32_t retcode; - - batontxid = bettxid; // initially set to the source bet tx - uint256 sourcetxid = bettxid; - - // iterate through batons, adding up vout1 -> addedbets - while ((retcode = CCgetspenttxid(batontxid, vini, height, sourcetxid, 0)) == 0) { - - CTransaction txBaton; - CBlockIndex blockIdx; - uint256 bettxidInOpret; - CPubKey pk; - bool isLoaded = false; - uint8_t funcId = 0; - int64_t amount; - EvalRef eval; - - if ((isLoaded = eval->GetTxConfirmed(batontxid, txBaton, blockIdx)) && - blockIdx.IsValid() && - txBaton.vout.size() > 0 && - (funcId = prices_addopretdecode(txBaton.vout.back().scriptPubKey, bettxidInOpret, pk, amount)) != 0) - { - OneBetData added; - - addedBetsTotal += amount; - added.positionsize = amount; - added.firstheight = blockIdx.GetHeight(); //TODO: check if this is correct (to get height from the block not from the opret) - bets.push_back(added); - //std::cerr << "prices_batontxid() added amount=" << amount << std::endl; - } - else { - std::cerr << "prices_batontxid() cannot load or decode add bet tx, isLoaded=" << isLoaded << " funcId=" << (int)funcId << std::endl; - return -1; - } - sourcetxid = batontxid; - } - - return(addedBetsTotal); + return 0; } // pricesbet rpc impl: make betting tx UniValue PricesBet(int64_t txfee, int64_t amount, int16_t leverage, std::vector synthetic) { - int32_t nextheight = hush_nextheight(); - CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp, C; - CPubKey pricespk, mypk; - int64_t betamount, firstprice = 0; - std::vector vec; - //char myaddr[64]; - std::string rawtx; - - if (leverage > PRICES_MAXLEVERAGE || leverage < -PRICES_MAXLEVERAGE) - { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "leverage too big")); - return(result); - } - cp = CCinit(&C, EVAL_PRICES); - if (txfee == 0) - txfee = PRICES_TXFEE; - mypk = pubkey2pk(Mypubkey()); - pricespk = GetUnspendable(cp, 0); - //GetCCaddress(cp, myaddr, mypk); - if (prices_syntheticvec(vec, synthetic) < 0 || (firstprice = prices_syntheticprice(vec, nextheight - 1, 1, leverage)) < 0 || vec.size() == 0 || vec.size() > 4096) - { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "invalid synthetic")); - return(result); - } - - if (!prices_isacceptableamount(vec, amount, leverage)) { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "too big amount and leverage")); - return(result); - } - - if (AddNormalinputs(mtx, mypk, amount + 4 * txfee, 64) >= amount + 4 * txfee) - { - betamount = PRICES_SUBREVSHAREFEE(amount); - - /*if( amount - betamount < PRICES_REVSHAREDUST) { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "bet amount too small")); - return(result); - }*/ - - - mtx.vout.push_back(MakeCC1vout(cp->evalcode, txfee, mypk)); // vout0 baton for total funding - // mtx.vout.push_back(MakeCC1vout(cp->evalcode, (amount - betamount) + 2 * txfee, pricespk)); // vout1, when spent, costbasis is set - mtx.vout.push_back(MakeCC1vout(cp->evalcode, txfee, pricespk)); // vout1 cc marker (NVOUT_CCMARKER) - mtx.vout.push_back(MakeCC1vout(cp->evalcode, betamount, pricespk)); // vout2 betamount - mtx.vout.push_back(CTxOut(txfee, CScript() << ParseHex(HexStr(pricespk)) << OP_CHECKSIG)); // vout3 normal marker NVOUT_NORMALMARKER - TODO: remove it as we have cc marker now, when move to the new chain - if ( ASSETCHAINS_EARLYTXIDCONTRACT == EVAL_PRICES && HUSH_EARLYTXID_SCRIPTPUB.size() == 0 ) - { - // Lock here, as in validation we cannot call lock in the function itself. - // may not be needed as the validation call to update the global, is called in a LOCK already, and it can only update there and here. - LOCK(cs_main); - GetHushEarlytxidScriptPub(); - } - mtx.vout.push_back(CTxOut(amount-betamount, HUSH_EARLYTXID_SCRIPTPUB)); - //test: mtx.vout.push_back(CTxOut(amount - betamount, CScript() << ParseHex("037c803ec82d12da939ac04379bbc1130a9065c53d8244a61eece1db942cf0efa7") << OP_CHECKSIG)); // vout4 test revshare fee - - rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_betopret(mypk, nextheight - 1, amount, leverage, firstprice, vec, zeroid)); - return(prices_rawtxresult(result, rawtx, 0)); - } - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "not enough funds")); + UniValue result(UniValue::VOBJ); return(result); } // pricesaddfunding rpc impl: add yet another bet UniValue PricesAddFunding(int64_t txfee, uint256 bettxid, int64_t amount) { - int32_t nextheight = hush_nextheight(); - CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp, C; - CTransaction bettx; - CPubKey pricespk, mypk, pk; - int64_t positionsize, betamount, firstprice; - int32_t firstheight; - std::vector vecparsed; - uint256 batontxid, tokenid, hashBlock; - int16_t leverage = 0; - std::string rawtx; - //char myaddr[64]; - - cp = CCinit(&C, EVAL_PRICES); - if (txfee == 0) - txfee = PRICES_TXFEE; - mypk = pubkey2pk(Mypubkey()); - pricespk = GetUnspendable(cp, 0); - - if (!myGetTransaction(bettxid, bettx, hashBlock) || - bettx.vout.size() <= 3 || - hashBlock.IsNull() || - prices_betopretdecode(bettx.vout.back().scriptPubKey, pk, firstheight, positionsize, leverage, firstprice, vecparsed, tokenid) != 'B') { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "invalid bet tx")); - return(result); - } - - if (!prices_isacceptableamount(vecparsed, amount, leverage)) { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "too big amount and leverage")); - return(result); - } - - if (AddNormalinputs(mtx, mypk, amount + 2*txfee, 64) >= amount + 2*txfee) - { - betamount = PRICES_SUBREVSHAREFEE(amount); - /*if (amount - betamount < PRICES_REVSHAREDUST) { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "bet amount too small")); - return(result); - }*/ - - std::vector bets; - if (prices_enumaddedbets(batontxid, bets, bettxid) >= 0) - { - mtx.vin.push_back(CTxIn(batontxid, 0, CScript())); - mtx.vout.push_back(MakeCC1vout(cp->evalcode, txfee, mypk)); // vout0 baton for total funding - mtx.vout.push_back(MakeCC1vout(cp->evalcode, betamount, pricespk)); // vout1 added amount - - if (ASSETCHAINS_EARLYTXIDCONTRACT == EVAL_PRICES && HUSH_EARLYTXID_SCRIPTPUB.size() == 0) - { - // Lock here, as in validation we cannot call lock in the function itself. - // may not be needed as the validation call to update the global, is called in a LOCK already, and it can only update there and here. - LOCK(cs_main); - GetHushEarlytxidScriptPub(); - } - mtx.vout.push_back(CTxOut(amount - betamount, HUSH_EARLYTXID_SCRIPTPUB)); - // test: mtx.vout.push_back(CTxOut(amount - betamount, CScript() << ParseHex("037c803ec82d12da939ac04379bbc1130a9065c53d8244a61eece1db942cf0efa7") << OP_CHECKSIG)); //vout2 test revshare fee - - rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_addopret(bettxid, mypk, amount)); - return(prices_rawtxresult(result, rawtx, 0)); - } - else - { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "couldnt find batonttxid")); - return(result); - } - } - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "not enough funds")); + UniValue result(UniValue::VOBJ); return(result); } // scan chain from the initial bet's first position upto the chain tip and calculate bet's costbasises and profits, breaks if rekt detected int32_t prices_scanchain(std::vector &bets, int16_t leverage, std::vector vec, int64_t &lastprice, int32_t &endheight) { - - if (bets.size() == 0) - return -1; - - bool stop = false; - for (int32_t height = bets[0].firstheight+1; ; height++) // the last datum for 24h is the costbasis value - { - int64_t totalposition = 0; - int64_t totalprofits = 0; - - // scan upto the chain tip - for (int i = 0; i < bets.size(); i++) { - - if (height > bets[i].firstheight) { - - int32_t retcode = prices_syntheticprofits(bets[i].costbasis, bets[i].firstheight, height, leverage, vec, bets[i].positionsize, bets[i].profits, lastprice); - if (retcode < 0) { - std::cerr << "prices_scanchain() prices_syntheticprofits returned -1, finishing..." << std::endl; - stop = true; - break; - } - totalposition += bets[i].positionsize; - totalprofits += bets[i].profits; - } - } - - if (stop) - break; - - endheight = height; - int64_t equity = totalposition + totalprofits; - if (equity <= (int64_t)((double)totalposition * prices_minmarginpercent(leverage))) - { // we are in loss - break; - } - } - return 0; } // pricescostbasis rpc impl: set cost basis (open price) for the bet (deprecated) UniValue PricesSetcostbasis(int64_t txfee, uint256 bettxid) { - int32_t nextheight = hush_nextheight(); - CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp, C; CTransaction bettx; uint256 hashBlock, batontxid, tokenid; - int64_t myfee, positionsize = 0, addedbets, firstprice = 0, lastprice, profits = 0, costbasis = 0, equity; - int32_t i, firstheight = 0, height, numvouts; int16_t leverage = 0; - std::vector vec; - CPubKey pk, mypk, pricespk; std::string rawtx; -/* - cp = CCinit(&C, EVAL_PRICES); - if (txfee == 0) - txfee = PRICES_TXFEE; - - mypk = pubkey2pk(Mypubkey()); - pricespk = GetUnspendable(cp, 0); - if (myGetTransaction(bettxid, bettx, hashBlock) != 0 && (numvouts = bettx.vout.size()) > 3) - { - if (prices_betopretdecode(bettx.vout[numvouts - 1].scriptPubKey, pk, firstheight, positionsize, leverage, firstprice, vec, tokenid) == 'B') - { - if (nextheight <= firstheight + PRICES_DAYWINDOW + 1) { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "cannot calculate costbasis yet")); - return(result); - } - - addedbets = prices_enumaddedbets(batontxid, bettx, bettxid); - mtx.vin.push_back(CTxIn(bettxid, 1, CScript())); // spend vin1 with betamount - //for (i = 0; i < PRICES_DAYWINDOW + 1; i++) // the last datum for 24h is the actual costbasis value - //{ - int32_t retcode = prices_syntheticprofits(costbasis, firstheight, firstheight + PRICES_DAYWINDOW, leverage, vec, positionsize, addedbets, profits, lastprice); - if (retcode < 0) { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "cannot calculate costbasis error getting price")); - return(result); - } - equity = positionsize + addedbets + profits; - //if (equity < 0) - //{ // we are in loss - // result.push_back(Pair("rekt", (int64_t)1)); - // result.push_back(Pair("rektheight", (int64_t)firstheight + i)); - // break; - //} - //} - //if (i == PRICES_DAYWINDOW + 1) - // result.push_back(Pair("rekt", 0)); - - prices_betjson(result, profits, costbasis, positionsize, addedbets, leverage, firstheight, firstprice, lastprice, equity); - - if (AddNormalinputs(mtx, mypk, txfee, 4) >= txfee) - { - myfee = bettx.vout[1].nValue / 10; // fee for setting costbasis - result.push_back(Pair("myfee", myfee)); - - mtx.vout.push_back(CTxOut(myfee, CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - mtx.vout.push_back(MakeCC1vout(cp->evalcode, bettx.vout[1].nValue - myfee, pricespk)); - rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_costbasisopret(bettxid, mypk, firstheight + PRICES_DAYWINDOW , costbasis)); // -1 - return(prices_rawtxresult(result, rawtx, 0)); - } - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "not enough funds")); - return(result); - } - } */ - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "deprecated")); return(result); } @@ -1718,360 +297,28 @@ UniValue PricesSetcostbasis(int64_t txfee, uint256 bettxid) // pricesaddfunding rpc impl: add yet another bet UniValue PricesRefillFund(int64_t amount) { - int32_t nextheight = hush_nextheight(); - CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp, C; - CPubKey pricespk, mypk, pk; - std::string rawtx; - //char myaddr[64]; - - cp = CCinit(&C, EVAL_PRICES); - const int64_t txfee = PRICES_TXFEE; - mypk = pubkey2pk(Mypubkey()); - pricespk = GetUnspendable(cp, 0); - - if (AddNormalinputs(mtx, mypk, amount + txfee, 64) >= amount + txfee) - { - mtx.vout.push_back(MakeCC1vout(cp->evalcode, amount, pricespk)); // vout1 added amount - rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, CScript()); - return(prices_rawtxresult(result, rawtx, 0)); - - } - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "not enough funds")); + UniValue result(UniValue::VOBJ); return(result); } int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) { - CTransaction bettx; - uint256 hashBlock, batontxid, tokenid; - - if (myGetTransaction(bettxid, bettx, hashBlock) && bettx.vout.size() > 3) - { - if (hashBlock.IsNull()) - return -2; - - - // TODO: forget old tx - //CBlockIndex *bi = komodo_getblockindex(hashBlock); - //if (bi && bi->GetHeight() < 5342) - // return -5; - - OneBetData bet1; - if (prices_betopretdecode(bettx.vout.back().scriptPubKey, betinfo.pk, bet1.firstheight, bet1.positionsize, betinfo.leverage, betinfo.firstprice, betinfo.vecparsed, betinfo.tokenid) == 'B') - { - uint256 finaltxid; - int32_t vini; - int32_t finaltxheight; //, endheight; - //std::vector bets; - betinfo.txid = bettxid; - - if (CCgetspenttxid(finaltxid, vini, finaltxheight, bettxid, NVOUT_CCMARKER) == 0) - betinfo.isOpen = false; - else - betinfo.isOpen = true; - - // override with real amount (TODO: check this) - //bet1.positionsize = bettx.vout[2].nValue; - - betinfo.bets.push_back(bet1); - - prices_enumaddedbets(batontxid, betinfo.bets, bettxid); - - if (!betinfo.isOpen) { - CTransaction finaltx; - uint256 hashBlock; - vscript_t vopret; - if (myGetTransaction(finaltxid, finaltx, hashBlock) && finaltx.vout.size() > 0 && PricesCheckOpret(finaltx, vopret) != 0) { - uint8_t funcId = prices_finalopretdecode(finaltx.vout.back().scriptPubKey, betinfo.txid, betinfo.pk, betinfo.lastheight, betinfo.averageCostbasis, betinfo.lastprice, betinfo.liquidationprice, betinfo.equity, betinfo.exitfee); - if (funcId == 0) - return -3; - - betinfo.isRekt = (funcId == 'R'); - - // return 0; - } - else - return -6; - } - - - if (prices_scanchain(betinfo.bets, betinfo.leverage, betinfo.vecparsed, betinfo.lastprice, betinfo.lastheight) < 0) { - return -4; - } - - mpz_t mpzTotalPosition; - mpz_t mpzTotalprofits; - mpz_t mpzTotalcostbasis; - - mpz_init(mpzTotalPosition); - mpz_init(mpzTotalprofits); - mpz_init(mpzTotalcostbasis); - - int64_t totalposition = 0; - int64_t totalprofits = 0; - - for (auto b : betinfo.bets) { - mpz_t mpzProduct; - mpz_t mpzProfits; - - mpz_init(mpzProduct); - mpz_init(mpzProfits); - - //totalprofits += b.profits; - //dcostbasis += b.amount * (double)b.costbasis; - // costbasis += b.amount * (b.costbasis / PRICES_POINTFACTOR); // prevent int64 overflow (but we have underflow for 1/BTC) - // std::cerr << "PricesInfo() acc dcostbasis=" << dcostbasis << " b.amount=" << b.amount << " b.costbasis/PRICES_POINTFACTOR=" << (b.costbasis / PRICES_POINTFACTOR) << std::endl; - //std::cerr << "PricesInfo() acc dcostbasis=" << dcostbasis << " b.amount=" << b.amount << " b.costbasis/PRICES_POINTFACTOR=" << (b.costbasis / PRICES_POINTFACTOR) << std::endl; - mpz_set_ui(mpzProduct, b.costbasis); - mpz_mul_ui(mpzProduct, mpzProduct, (uint64_t)b.positionsize); // b.costbasis * b.amount - mpz_add(mpzTotalcostbasis, mpzTotalcostbasis, mpzProduct); //averageCostbasis += b.costbasis * b.amount; - - mpz_add_ui(mpzTotalPosition, mpzTotalPosition, (uint64_t)b.positionsize); //totalposition += b.amount; - mpz_add(mpzTotalprofits, mpzTotalprofits, mpzProfits); //totalprofits += b.profits; - - totalposition += b.positionsize; - totalprofits += b.profits; - - mpz_clear(mpzProduct); - mpz_clear(mpzProfits); - } - - betinfo.equity = totalposition + totalprofits; - //int64_t averageCostbasis = 0; - - if (mpz_get_ui(mpzTotalPosition) != 0) { //prevent zero div - mpz_t mpzAverageCostbasis; - mpz_init(mpzAverageCostbasis); - - //averageCostbasis = totalcostbasis / totalposition; - mpz_mul_ui(mpzTotalcostbasis, mpzTotalcostbasis, SATOSHIDEN); // profits *= SATOSHIDEN normalization to prevent loss of significance while division - mpz_tdiv_q(mpzAverageCostbasis, mpzTotalcostbasis, mpzTotalPosition); - - mpz_tdiv_q_ui(mpzAverageCostbasis, mpzAverageCostbasis, SATOSHIDEN); // profits /= SATOSHIDEN de-normalization - - betinfo.averageCostbasis = mpz_get_ui(mpzAverageCostbasis); - mpz_clear(mpzAverageCostbasis); - } - - betinfo.liquidationprice = 0; - if (betinfo.leverage != 0) {// prevent zero div - betinfo.liquidationprice = betinfo.averageCostbasis - (betinfo.averageCostbasis * (1 - prices_minmarginpercent(betinfo.leverage))) / betinfo.leverage; - } - - if (!betinfo.isRekt) { // not set by check for final tx - - if (betinfo.equity > (int64_t)((double)totalposition * prices_minmarginpercent(betinfo.leverage))) - betinfo.isRekt = false; - else - { - betinfo.isRekt = true; - betinfo.exitfee = (int64_t)(((double)totalposition * prices_minmarginpercent(betinfo.leverage)) / 10); // was: totalposition / 500 - } - } - - mpz_clear(mpzTotalPosition); - mpz_clear(mpzTotalprofits); - mpz_clear(mpzTotalcostbasis); - return 0; - } - return -3; - } - return (-1); + return (-420); } // pricesrekt rpc: anyone can rekt a bet at some block where losses reached limit, collecting fee UniValue PricesRekt(int64_t txfee, uint256 bettxid, int32_t rektheight) { - int32_t nextheight = hush_nextheight(); - CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp, C; - CTransaction bettx; - int64_t myfee = 0; - CPubKey pk, mypk, pricespk; - std::string rawtx; - char destaddr[64]; - - cp = CCinit(&C, EVAL_PRICES); - if (txfee == 0) // TODO: what did we want to do with txfee in prices? - txfee = PRICES_TXFEE; - mypk = pubkey2pk(Mypubkey()); - pricespk = GetUnspendable(cp, 0); - GetCCaddress(cp, destaddr, pricespk); - - BetInfo betinfo; - int32_t retcode = prices_getbetinfo(bettxid, betinfo); - if (retcode < 0) { - if (retcode == -1) { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "cant find bettxid or incorrect")); - } - else if (retcode == -2) { - throw std::runtime_error("tx still in mempool"); - } - else if (retcode == -3) - { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "cant decode opret")); - return(result); - } - else if (retcode == -4) { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "error scanning chain")); - } - return(result); - } - - int64_t totalposition = 0; - int64_t totalprofits = 0; - - for (auto b : betinfo.bets) { - totalposition += b.positionsize; - totalprofits += b.profits; - } - - - if (!betinfo.isOpen) { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "position closed")); - return result; - } - - prices_betjson(result, betinfo.bets, betinfo.leverage, betinfo.lastheight, betinfo.lastprice); // fill output - if (betinfo.isRekt) - { - myfee = betinfo.exitfee; // consolation fee for loss - } - if (myfee != 0) - { - int64_t CCchange = 0, inputsum; - - mtx.vin.push_back(CTxIn(bettxid, NVOUT_CCMARKER, CScript())); // spend cc marker - if ((inputsum = AddPricesInputs(cp, mtx, destaddr, myfee + txfee, 64)) > myfee + txfee) // TODO: why do we take txfee from global addr and not from user's addr? - CCchange = (inputsum - myfee); - mtx.vout.push_back(CTxOut(myfee, CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - if (CCchange >= txfee) - mtx.vout.push_back(MakeCC1vout(cp->evalcode, CCchange, pricespk)); - - /// mtx.vout.push_back(MakeCC1vout(cp->evalcode, bettx.vout[2].nValue - myfee - txfee, pricespk)); // change - - // make some PoW to get txid=0x00.....00 to 'faucet' rekts - fprintf(stderr, "start PoW at %u\n", (uint32_t)time(NULL)); - uint32_t nonce = rand() & 0xfffffff; - for (int i = 0; i<1000000; i++, nonce++) - { - CMutableTransaction tmpmtx = mtx; - //int32_t len; - //uint8_t txbuf[32768]; - - rawtx = FinalizeCCTx(0, cp, tmpmtx, mypk, txfee, prices_finalopret(true, bettxid, mypk, betinfo.lastheight, betinfo.averageCostbasis, betinfo.lastprice, betinfo.liquidationprice, betinfo.equity, myfee, nonce)); - //if ((len = (int32_t)rawtx.size()) > 0 && len < sizeof(txbuf) / sizeof(txbuf[0]) * 2) - //{ - // len >>= 1; // sizeof hex divide by 2 - //decode_hex(txbuf, len, (char *)rawtx.c_str()); - //bits256 hash = bits256_doublesha256(0, txbuf, len); - uint256 hash = tmpmtx.GetHash(); - //if ((hash.bytes[0] & 0xff) == 0 && (hash.bytes[31] & 0xff) == 0) - if ((hash.begin()[0] & 0xff) == 0 && (hash.begin()[31] & 0xff) == 0) - { - fprintf(stderr, "found valid txid after %d iterations %u\n", i, (uint32_t)time(NULL)); - return(prices_rawtxresult(result, rawtx, 0)); - } - //fprintf(stderr,"%02x%02x ",hash.bytes[0],hash.bytes[31]); - //} - } - fprintf(stderr, "couldnt generate valid txid %u\n", (uint32_t)time(NULL)); - - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "could not generate valid txid")); - return(result); - } - else - { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "position not rekt")); - return(result); - } + UniValue result(UniValue::VOBJ); + return(result); } // pricescashout rpc impl: bettor can cashout hit bet if it is not rekt UniValue PricesCashout(int64_t txfee, uint256 bettxid) { - int32_t nextheight = hush_nextheight(); - CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp, C; char destaddr[64]; - int64_t CCchange = 0, inputsum; - CPubKey pk, mypk, pricespk; - std::string rawtx; - - cp = CCinit(&C, EVAL_PRICES); - if (txfee == 0) - txfee = PRICES_TXFEE; - - mypk = pubkey2pk(Mypubkey()); - pricespk = GetUnspendable(cp, 0); - GetCCaddress(cp, destaddr, pricespk); - - BetInfo betinfo; - int32_t retcode = prices_getbetinfo(bettxid, betinfo); - if (retcode < 0) { - if (retcode == -1) { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "cant find bettxid or incorrect")); - } - else if (retcode == -2) { - throw std::runtime_error("tx still in mempool"); - } - else if (retcode == -3) - { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "cant decode opret")); - return(result); - } - else if (retcode == -4) { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "error scanning chain")); - } - return(result); - } - - int64_t totalposition = 0; - int64_t totalprofits = 0; - - for (auto b : betinfo.bets) { - totalposition += b.positionsize; - totalprofits += b.profits; - } - - - if (!betinfo.isOpen) { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "position closed")); - return result; - } - - prices_betjson(result, betinfo.bets, betinfo.leverage, betinfo.lastheight, betinfo.lastprice); // fill output json - - if (betinfo.isRekt) - { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "position rekt")); - return(result); - } - - mtx.vin.push_back(CTxIn(bettxid, NVOUT_CCMARKER, CScript())); // spend cc marker - if ((inputsum = AddPricesInputs(cp, mtx, destaddr, betinfo.equity + txfee, 64)) > betinfo.equity + txfee) // TODO: why txfee from the fund? - CCchange = (inputsum - betinfo.equity); - mtx.vout.push_back(CTxOut(betinfo.equity, CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - if (CCchange >= txfee) - mtx.vout.push_back(MakeCC1vout(cp->evalcode, CCchange, pricespk)); - // TODO: what should the opret param be: - rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_finalopret(false, bettxid, mypk, nextheight-1, betinfo.averageCostbasis, betinfo.lastprice, betinfo.liquidationprice, betinfo.equity, txfee, 0)); - return(prices_rawtxresult(result, rawtx, 0)); - + return result; } @@ -2079,66 +326,6 @@ UniValue PricesCashout(int64_t txfee, uint256 bettxid) UniValue PricesInfo(uint256 bettxid, int32_t refheight) { UniValue result(UniValue::VOBJ); -/* CTransaction bettx; - uint256 hashBlock, batontxid, tokenid; - int64_t positionsize = 0, firstprice = 0, lastprice = 0; - int32_t firstheight = 0, endheight; - int16_t leverage = 0; - std::vector vec; - CPubKey pk, mypk, pricespk; - std::string rawtx; */ - - BetInfo betinfo; - int32_t retcode = prices_getbetinfo(bettxid, betinfo); - if (retcode < 0) { - if( retcode == -1 ) { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "cant find bettxid or incorrect")); - } - else if (retcode == -2) { - throw std::runtime_error("tx still in mempool"); - } - else if (retcode == -3) - { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "cant decode opret")); - return(result); - } - else if (retcode == -4) { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "error scanning chain")); - } - else { - result.push_back(Pair("result", "error")); - result.push_back(Pair("error", retcode)); - } - return(result); - } - - if (!betinfo.isRekt) - result.push_back(Pair("rekt", 0)); - else - { - result.push_back(Pair("rekt", (int64_t)1)); - result.push_back(Pair("rektfee", betinfo.exitfee)); - result.push_back(Pair("rektheight", betinfo.lastheight)); - } - - result.push_back(Pair("open", betinfo.isOpen ? 1 : 0 )); - - std::string expr = prices_getsourceexpression(betinfo.vecparsed); - result.push_back(Pair("expression", expr)); - result.push_back(Pair("reduced", prices_getreducedexpr(expr))); -// result.push_back(Pair("batontxid", batontxid.GetHex())); - result.push_back(Pair("costbasis", ValueFromAmount(betinfo.averageCostbasis))); -#ifdef TESTMODE - result.push_back(Pair("costbasis_test_period", 7)); -#endif - - prices_betjson(result, betinfo.bets, betinfo.leverage, betinfo.lastheight, betinfo.lastprice); - - result.push_back(Pair("LiquidationPrice", ValueFromAmount(betinfo.liquidationprice))); - return(result); } @@ -2146,307 +333,35 @@ UniValue PricesInfo(uint256 bettxid, int32_t refheight) UniValue PricesList(uint32_t filter, CPubKey mypk) { UniValue result(UniValue::VARR); - std::vector > addressIndex, addressIndexCC; - struct CCcontract_info *cp, C; - - cp = CCinit(&C, EVAL_PRICES); - //pricespk = GetUnspendable(cp, 0); - - // filters and outputs prices bet txid - auto AddBetToList = [&](uint256 txid) - { - int64_t amount, firstprice; - int32_t height; - int16_t leverage; - uint256 hashBlock, tokenid; - CPubKey pk, pricespk; - std::vector vec; - CTransaction vintx; - - if (myGetTransaction(txid, vintx, hashBlock) != 0) - { - - // TODO: forget old tx - //CBlockIndex *bi = komodo_getblockindex(hashBlock); - //if (bi && bi->GetHeight() < 5342) - // return; - - bool bAppend = false; - if (vintx.vout.size() > 0 && prices_betopretdecode(vintx.vout.back().scriptPubKey, pk, height, amount, leverage, firstprice, vec, tokenid) == 'B' && - (mypk == CPubKey() || mypk == pk)) // if only mypubkey to list - { - if (filter == 0) - bAppend = true; - else { - int32_t vini; - int32_t height; - uint256 finaltxid; - - int32_t spent = CCgetspenttxid(finaltxid, vini, height, txid, NVOUT_CCMARKER); - if (filter == 1 && spent < 0 || // open positions - filter == 2 && spent == 0) // closed positions - bAppend = true; - } - if (bAppend) - result.push_back(txid.GetHex()); - } - std::cerr << "PricesList() " << " bettxid=" << txid.GetHex() << " mypk=" << HexStr(mypk) << " opretpk=" << HexStr(pk) << " filter=" << filter << " bAppend=" << bAppend << std::endl; - } - }; - - - SetCCtxids(addressIndex, cp->normaladdr, false); // old normal marker - for (std::vector >::const_iterator it = addressIndex.begin(); it != addressIndex.end(); it++) - { - if( it->first.index == NVOUT_NORMALMARKER ) - AddBetToList(it->first.txhash); - } - - /* for future when switch to cc marker only - SetCCtxids(addressIndexCC, cp->unspendableCCaddr, true); // cc marker - for (std::vector >::const_iterator it = addressIndexCC.begin(); it != addressIndexCC.end(); it++) - { - priceslist(it, 1); - } - */ return(result); } static bool prices_addbookentry(uint256 txid, std::vector &book) { - BetInfo betinfo; - if (prices_getbetinfo(txid, betinfo) == 0) { - book.push_back(betinfo); - return true; - } return false; } static bool prices_ispositionup(const std::vector &vecparsed, int16_t leverage) { - if (vecparsed.size() > 1 && vecparsed.size() <= 3) { - uint16_t opcode = vecparsed[0]; - - int32_t value = (opcode & (HUSH_MAXPRICES - 1)); // filter index or weight = opcode & (2048-1) - - if ((opcode & HUSH_PRICEMASK) == 0) { - char name[65]; - if (komodo_pricename(name, value)) { - std::string upperquote, bottomquote; - prices_splitpair(std::string(name), upperquote, bottomquote); - - uint16_t opcode1 = vecparsed[1]; - bool isInverted = ((opcode1 & HUSH_PRICEMASK) == PRICES_INV); - - //std::cerr << "prices_ispositionup upperquote=" << upperquote << " bottomquote=" << bottomquote << " opcode1=" << opcode1 << " (opcode1 & HUSH_PRICEMASK)=" << (opcode1 & HUSH_PRICEMASK) << std::endl; - - if (upperquote == "BTC" || bottomquote == "BTC") { // it is relatively btc - if (upperquote == "BTC" && (leverage > 0 && !isInverted || leverage < 0 && isInverted) || - bottomquote == "BTC" && (leverage < 0 && !isInverted || leverage > 0 && isInverted)) { - std::cerr << "prices_ispositionup returns true for BTC for expr=" << prices_getsourceexpression(vecparsed) << " lev=" << leverage << std::endl; - return true; - } - else { - std::cerr << "prices_ispositionup returns false for BTC for expr=" << prices_getsourceexpression(vecparsed) << " lev=" << leverage << std::endl; - return false; - } - } - - if (upperquote == "USD" || bottomquote == "USD") { // it is relatively usd - if (upperquote == "USD" && (leverage > 0 && !isInverted || leverage < 0 && isInverted) || - bottomquote == "USD" && (leverage < 0 && !isInverted || leverage > 0 && isInverted)) { - std::cerr << "prices_ispositionup returns true for USD for expr=" << prices_getsourceexpression(vecparsed) << " lev=" << leverage << std::endl; - return true; - } - else { - std::cerr << "prices_ispositionup returns false for USD for expr=" << prices_getsourceexpression(vecparsed) << " lev=" << leverage << std::endl; - return false; - } - } - } - } - } - std::cerr << "prices_ispositionup returns false for expr=" << prices_getsourceexpression(vecparsed) << " lev=" << leverage << std::endl; return false; } static bool prices_isopposite(BetInfo p1, BetInfo p2) { - if (p1.vecparsed.size() <= 3 && p2.vecparsed.size() <= 3) { // simple synthetic exp - - uint16_t opcode1 = p1.vecparsed[0]; - uint16_t opcode2 = p2.vecparsed[0]; - - int32_t value1 = (opcode1 & (HUSH_MAXPRICES - 1)); // index or weight - int32_t value2 = (opcode2 & (HUSH_MAXPRICES - 1)); // index or weight - - if ( (opcode1 & HUSH_PRICEMASK) == 0 && (opcode2 & HUSH_PRICEMASK) == 0 ) { - char name1[65]; - char name2[65]; - if (komodo_pricename(name1, value1) && komodo_pricename(name2, value2)) { - - uint16_t opcode1 = p1.vecparsed[1]; - uint16_t opcode2 = p2.vecparsed[1]; - - std::string upperquote1, bottomquote1, upperquote2, bottomquote2; - prices_splitpair(std::string(name1), upperquote1, bottomquote1); - prices_splitpair(std::string(name2), upperquote2, bottomquote2); - - bool isInverted1 = ((opcode1 & HUSH_PRICEMASK) != PRICES_INV); - bool isInverted2 = ((opcode2 & HUSH_PRICEMASK) != PRICES_INV); - - if (/*upperquote1 == bottomquote2 && bottomquote1 == upperquote2 && (p1.leverage > 0 == p2.leverage > 0 || (opcode1 & HUSH_PRICEMASK) == PRICES_INV == (opcode2 & HUSH_PRICEMASK) == PRICES_INV) ||*/ - upperquote1 == upperquote2 && bottomquote1 == bottomquote2 && ((p1.leverage > 0) != (p2.leverage > 0) || isInverted1 != isInverted2) ) - return true; - } - } - } return false; } static std::string findMatchedBook(const std::vector &vecparsed, const std::map > & bookmatched) { - - if (vecparsed.size() > 1 && vecparsed.size() <= 3) { - uint16_t opcode = vecparsed[0]; - - int32_t value = (opcode & (HUSH_MAXPRICES - 1)); // filter index or weight = opcode & (2048-1) - - if ((opcode & HUSH_PRICEMASK) == 0) { - char name[65]; - if (komodo_pricename(name, value)) { - auto it = bookmatched.find(std::string(name)); - if (it != bookmatched.end()) - return it->first; - } - } - } return std::string(""); } -void prices_getorderbook(std::map > & bookmatched, std::map &matchedTotals, TotalFund &fundTotals) -{ - std::vector book; - std::vector > addressIndex; - struct CCcontract_info *cp, C; - - cp = CCinit(&C, EVAL_PRICES); - - // add all bets: - SetCCtxids(addressIndex, cp->normaladdr, false); // old normal marker - for (std::vector >::const_iterator it = addressIndex.begin(); it != addressIndex.end(); it++) - { - if (it->first.index == NVOUT_NORMALMARKER) - prices_addbookentry(it->first.txhash, book); - } - - - // calc total fund amount - fundTotals.totalFund = 0; - fundTotals.totalRekt = 0; - fundTotals.totalEquity = 0; - fundTotals.totalActiveBets = 0; - - std::vector > addressCCunspents; - SetCCunspents(addressCCunspents, cp->unspendableCCaddr, true); // cc marker - for (std::vector >::const_iterator it = addressCCunspents.begin(); it != addressCCunspents.end(); it++) - { - //std::cerr << "totalfund calc txid=" << it->first.txhash.GetHex() << " nvout=" << it->first.index << " satoshis=" << it->second.satoshis << std::endl; - fundTotals.totalFund += it->second.satoshis; - } - - // extract out opposite bets: - while (book.size() > 0) { - - int64_t totalPos = 0; - for (auto bet : book[0].bets) totalPos += bet.positionsize; - - if (book[0].isOpen) { - - fundTotals.totalActiveBets += totalPos; - fundTotals.totalEquity += book[0].equity; - - if (book[0].vecparsed.size() <= 3) { // only short expr check for match: "BTC_USD,1" or "BTC_USD,!,1" - char name[65]; - komodo_pricename(name, (book[0].vecparsed[0] & (HUSH_MAXPRICES - 1))); - std::string sname = name; - bookmatched[sname].push_back(book[0]); - - for (int j = 1; j < book.size(); j++) { - if (book[0].isOpen && book[j].isOpen) { - if (prices_isopposite(book[0], book[j])) { - - bookmatched[sname].push_back(book[j]); - book.erase(book.begin() + j); - } - } - } - } - else { - // store as is - std::string sname = prices_getsourceexpression(book[0].vecparsed); - bookmatched[sname].push_back(book[0]); - } - } - else { - if( book[0].isRekt ) - fundTotals.totalRekt += (totalPos - book[0].exitfee); - else - fundTotals.totalCashout += (totalPos - book[0].exitfee); - - //TODO: store rekt - } - book.erase(book.begin()); - } - - // calculate cancelling amount - for (auto &m : bookmatched) { // note: use reference &m otherwise isUp will be changed only in a copy - int64_t totalLeveragedPositionUp = 0; - int64_t totalLeveragedPositionDown = 0; - - for (int i = 0; i < m.second.size(); i++) { - int64_t totalPos = 0; - for (auto bet : m.second[i].bets) totalPos += bet.positionsize; - m.second[i].isUp = prices_ispositionup(m.second[i].vecparsed, m.second[i].leverage); - if (m.second[i].isUp) - totalLeveragedPositionUp += totalPos * abs(m.second[i].leverage); - else - totalLeveragedPositionDown += totalPos * abs(m.second[i].leverage); - //std::cerr << "PricesGetOrderbook 0 m.second[i].isUp=" << m.second[i].isUp << " i=" << i << std::endl; - - } - matchedTotals[m.first].diffLeveragedPosition = totalLeveragedPositionUp - totalLeveragedPositionDown; - } +void prices_getorderbook(std::map > & bookmatched, std::map &matchedTotals, TotalFund &fundTotals) { } static bool prices_isacceptableamount(const std::vector &vecparsed, int64_t amount, int16_t leverage) { - - std::map > matchedBook; - std::map matchedTotals; - TotalFund fundTotals; - - prices_getorderbook(matchedBook, matchedTotals, fundTotals); - std::string pricename = findMatchedBook(vecparsed, matchedBook); - if (!pricename.empty()) { - std::cerr << "prices_isacceptableamount() found matched book=" << pricename << " diffLeveragedPosition=" << matchedTotals[pricename].diffLeveragedPosition << " expr=" << prices_getsourceexpression(vecparsed) << std::endl; - // could fit into leveraged amount - if (prices_ispositionup(vecparsed, leverage) && amount*abs(leverage) + matchedTotals[pricename].diffLeveragedPosition <= 0) { - std::cerr << "prices_isacceptableamount() could fit into opposite negative lev amount" << std::endl; - return true; - } - if (!prices_ispositionup(vecparsed, leverage) && -amount*abs(leverage) + matchedTotals[pricename].diffLeveragedPosition >= 0) { - std::cerr << "prices_isacceptableamount() could fit into opposite positive lev amount" << std::endl; - return true; - } - } - - std::cerr << "prices_isacceptableamount() amount=" << amount << " leverage=" << leverage << " fundTotals.totalFund=" << fundTotals.totalFund << " fundTotals.totalEquity=" << fundTotals.totalEquity << std::endl; - // if not fit to matched = allow to bet for leveraged amount no more than 10% from free fund - if (amount * leverage < (fundTotals.totalFund - fundTotals.totalEquity) * PRICES_MINAVAILFUNDFRACTION) - return true; - return false; } @@ -2459,64 +374,5 @@ static bool prices_isacceptableamount(const std::vector &vecparsed, in UniValue PricesGetOrderbook() { UniValue result(UniValue::VOBJ); - std::map > matchedBook; - std::map matchedTotals; - TotalFund fundTotals; - - prices_getorderbook(matchedBook, matchedTotals, fundTotals); - - /*UniValue resbook (UniValue::VARR); - for (int i = 0; i < book.size(); i++) { - UniValue entry(UniValue::VOBJ); - entry.push_back(Pair("expression", prices_getsourceexpression(book[i].vecparsed))); - entry.push_back(Pair("costbasis", book[i].averageCostbasis)); - entry.push_back(Pair("leverage", book[i].leverage)); - entry.push_back(Pair("equity", book[i].equity)); - resbook.push_back(entry); - } - result.push_back(Pair("unmatched", resbook)); */ - - // copy to rpc UniResult - for (auto &m : matchedBook) { - UniValue resheader(UniValue::VOBJ); - UniValue resbook(UniValue::VARR); - for (int i = 0; i < m.second.size(); i++) { - UniValue entry(UniValue::VOBJ); - - int64_t totalPos = 0; - for (auto bet : m.second[i].bets) totalPos += bet.positionsize; - entry.push_back(Pair("isOpen", (m.second[i].isOpen ? 1 : 0 ))); - entry.push_back(Pair("expression", prices_getsourceexpression(m.second[i].vecparsed))); - entry.push_back(Pair("positionsize", totalPos)); - entry.push_back(Pair("leverage", m.second[i].leverage)); - entry.push_back(Pair("costbasis", m.second[i].averageCostbasis)); - entry.push_back(Pair("lastprice", m.second[i].lastprice)); - entry.push_back(Pair("equity", m.second[i].equity)); - entry.push_back(Pair("isUpPosition", (m.second[i].isUp ? 1 : 0))); - resbook.push_back(entry); - } - resheader.push_back(Pair("positions", resbook)); - resheader.push_back(Pair("DiffLeveragedPosition", matchedTotals[m.first].diffLeveragedPosition)); - result.push_back(Pair(m.first, resheader)); - } - - //int64_t totalLiabilities = 0; - /* empty - for (int i = 0; i < book.size(); i++) { - if (book[i].isOpen) { - int64_t t = 0; - for (auto b : book[i].bets) t += b.positionsize; - std::cerr << "book[i].txid=" << book[i].txid.GetHex() << " exp=" << prices_getsourceexpression(book[i].vecparsed) << " totalpos=" << t << " equity=" << book[i].equity << std::endl; - totalLiabilities += book[i].equity; - } - } */ - - result.push_back(Pair("TotalFund", ValueFromAmount(fundTotals.totalFund))); - result.push_back(Pair("TotalEquity", ValueFromAmount(fundTotals.totalEquity))); - result.push_back(Pair("TotalRekt", ValueFromAmount(fundTotals.totalRekt))); - result.push_back(Pair("TotalBets", ValueFromAmount(fundTotals.totalActiveBets))); - result.push_back(Pair("TotalCashoutBets", ValueFromAmount(fundTotals.totalCashout))); - -// result.push_back(Pair("TotalLiabilities", ValueFromAmount(totalLiabilities))); return result; } diff --git a/src/crosschain.cpp b/src/crosschain.cpp index d015c210a..82199d2c7 100644 --- a/src/crosschain.cpp +++ b/src/crosschain.cpp @@ -42,7 +42,7 @@ // because it might be disconnecting blocks at the same time. // TODO: this assumes a blocktime of 75 seconds for HUSH and 60 seconds for other chains int NOTARISATION_SCAN_LIMIT_BLOCKS = strncmp(SMART_CHAIN_SYMBOL, "HUSH3",5) == 0 ? 1152 : 1440; -CBlockIndex *komodo_getblockindex(uint256 hash); +CBlockIndex *hush_getblockindex(uint256 hash); /* On HUSH */ uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int hushHeight, @@ -435,7 +435,7 @@ TxProof GetAssetchainProof(uint256 hash,CTransaction burnTx) if (blockHash.IsNull()) throw std::runtime_error("tx still in mempool"); - blockIndex = komodo_getblockindex(blockHash); + blockIndex = hush_getblockindex(blockHash); int h = blockIndex->GetHeight(); // The assumption here is that the first notarization for a height GTE than // the transaction block height will contain the corresponding MoM. If there diff --git a/src/hush.h b/src/hush.h index 225f46259..2c802f50b 100644 --- a/src/hush.h +++ b/src/hush.h @@ -515,7 +515,7 @@ int32_t komodo_validate_chain(uint256 srchash,int32_t notarized_height) static int32_t last_rewind; int32_t rewindtarget; CBlockIndex *pindex; struct hush_state *sp; char symbol[HUSH_SMART_CHAIN_MAXLEN],dest[HUSH_SMART_CHAIN_MAXLEN]; if ( (sp= hush_stateptr(symbol,dest)) == 0 ) return(0); - if ( IsInitialBlockDownload() == 0 && ((pindex= komodo_getblockindex(srchash)) == 0 || pindex->GetHeight() != notarized_height) ) + if ( IsInitialBlockDownload() == 0 && ((pindex= hush_getblockindex(srchash)) == 0 || pindex->GetHeight() != notarized_height) ) { if ( sp->NOTARIZED_HEIGHT > 0 && sp->NOTARIZED_HEIGHT < notarized_height ) rewindtarget = sp->NOTARIZED_HEIGHT - 1; diff --git a/src/hush_bitcoind.h b/src/hush_bitcoind.h index f3be69c98..256bd2e15 100644 --- a/src/hush_bitcoind.h +++ b/src/hush_bitcoind.h @@ -377,7 +377,7 @@ char *komodo_issuemethod(char *userpass,char *method,char *params,uint16_t port) { sprintf(url,(char *)"http://127.0.0.1:%u",port); sprintf(postdata,"{\"method\":\"%s\",\"params\":%s}",method,params); - //printf("[%s] (%s) postdata.(%s) params.(%s) USERPASS.(%s)\n",SMART_CHAIN_SYMBOL,url,postdata,params,KMDUSERPASS); + //printf("[%s] (%s) postdata.(%s) params.(%s) USERPASS.(%s)\n",SMART_CHAIN_SYMBOL,url,postdata,params,HUSHUSERPASS); retstr2 = bitcoind_RPC(&retstr,(char *)"debug",url,userpass,method,params); //retstr = curl_post(&cHandle,url,USERPASS,postdata,0,0,0,0); } @@ -389,17 +389,16 @@ int32_t notarizedtxid_height(char *dest,char *txidstr,int32_t *kmdnotarized_heig char *jsonstr,params[256],*userpass; uint16_t port; cJSON *json,*item; int32_t height = 0,txid_height = 0,txid_confirmations = 0; params[0] = 0; *kmdnotarized_heightp = 0; - if ( strcmp(dest,"KMD") == 0 ) - { - port = KMD_PORT; - userpass = KMDUSERPASS; - } - else if ( strcmp(dest,"BTC") == 0 ) + if ( strcmp(dest,"HUSH3") == 0 ) { + port = HUSH3_PORT; + userpass = HUSHUSERPASS; + } else if ( strcmp(dest,"BTC") == 0 ) { - port = 8332; + port = 8332; userpass = BTCUSERPASS; + } else { + return(0); } - else return(0); if ( userpass[0] != 0 ) { if ( (jsonstr= komodo_issuemethod(userpass,(char *)"getinfo",params,port)) != 0 ) @@ -482,12 +481,12 @@ int32_t komodo_verifynotarization(char *symbol,char *dest,int32_t height,int32_t printf("[%s] src.%s dest.%s params.[%s] ht.%d notarized.%d\n",SMART_CHAIN_SYMBOL,symbol,dest,params,height,NOTARIZED_HEIGHT); if ( strcmp(dest,"KMD") == 0 ) { - if ( KMDUSERPASS[0] != 0 ) + if ( HUSHUSERPASS[0] != 0 ) { if ( SMART_CHAIN_SYMBOL[0] != 0 ) { - jsonstr = komodo_issuemethod(KMDUSERPASS,(char *)"getrawtransaction",params,KMD_PORT); - //printf("userpass.(%s) got (%s)\n",KMDUSERPASS,jsonstr); + jsonstr = komodo_issuemethod(HUSHUSERPASS,(char *)"getrawtransaction",params,HUSH3_PORT); + //printf("userpass.(%s) got (%s)\n",HUSHUSERPASS,jsonstr); } }//else jsonstr = _dex_getrawtransaction(); else return(0); // need universal way to issue DEX* API, since notaries mine most blocks, this ok @@ -556,12 +555,12 @@ CScript komodo_makeopret(CBlock *pblock, bool fNew) return(opret); } -/*uint256 komodo_getblockhash(int32_t height) +/*uint256 hush_getblockhash(int32_t height) { uint256 hash; char params[128],*hexstr,*jsonstr; cJSON *result; int32_t i; uint8_t revbuf[32]; memset(&hash,0,sizeof(hash)); sprintf(params,"[%d]",height); - if ( (jsonstr= komodo_issuemethod(KMDUSERPASS,(char *)"getblockhash",params,BITCOIND_RPCPORT)) != 0 ) + if ( (jsonstr= komodo_issuemethod(HUSHUSERPASS,(char *)"getblockhash",params,BITCOIND_RPCPORT)) != 0 ) { if ( (result= cJSON_Parse(jsonstr)) != 0 ) { @@ -582,7 +581,7 @@ CScript komodo_makeopret(CBlock *pblock, bool fNew) return(hash); } - uint256 _komodo_getblockhash(int32_t height);*/ + uint256 _hush_getblockhash(int32_t height);*/ uint64_t komodo_seed(int32_t height) { @@ -595,9 +594,9 @@ uint64_t komodo_seed(int32_t height) if ( height > 10 ) height -= 10; if ( SMART_CHAIN_SYMBOL[0] == 0 ) - hash = _komodo_getblockhash(height); + hash = _hush_getblockhash(height); if ( memcmp(&hash,&zero,sizeof(hash)) == 0 ) - hash = komodo_getblockhash(height); + hash = hush_getblockhash(height); int32_t i; for (i=0; i<32; i++) printf("%02x",((uint8_t *)&hash)[i]); @@ -639,7 +638,7 @@ uint32_t komodo_txtime(CScript &opret,uint64_t *valuep,uint256 hash, int32_t n, return(tx.nLockTime); } -CBlockIndex *komodo_getblockindex(uint256 hash) +CBlockIndex *hush_getblockindex(uint256 hash) { BlockMap::const_iterator it = mapBlockIndex.find(hash); return((it != mapBlockIndex.end()) ? it->second : NULL); @@ -658,7 +657,7 @@ uint32_t komodo_txtime2(uint64_t *valuep,uint256 hash,int32_t n,char *destaddr) //fprintf(stderr,"ERROR: %s/v%d locktime.%u\n",hash.ToString().c_str(),n,(uint32_t)tx.nLockTime); return(0); } - if ( (pindex= komodo_getblockindex(hashBlock)) != 0 ) + if ( (pindex= hush_getblockindex(hashBlock)) != 0 ) txtime = pindex->nTime; else txtime = tx.nLockTime; //fprintf(stderr,"%s/v%d locktime.%u\n",hash.ToString().c_str(),n,(uint32_t)tx.nLockTime); @@ -671,7 +670,7 @@ uint32_t komodo_txtime2(uint64_t *valuep,uint256 hash,int32_t n,char *destaddr) return(txtime); } -bool komodo_checkopret(CBlock *pblock, CScript &merkleroot) +bool hush_checkopret(CBlock *pblock, CScript &merkleroot) { merkleroot = pblock->vtx.back().vout.back().scriptPubKey; return(merkleroot.IsOpReturn() && merkleroot == komodo_makeopret(pblock, false)); @@ -792,7 +791,7 @@ int32_t hush_block2pubkey33(uint8_t *pubkey33,CBlock *block) return(0); } -int32_t komodo_blockload(CBlock& block,CBlockIndex *pindex) +int32_t hush_blockload(CBlock& block,CBlockIndex *pindex) { block.SetNull(); // Open history file to read @@ -849,7 +848,7 @@ uint32_t hush_heightstamp(int32_t height) if ( HUSH_LOADINGBLOCKS == 0 ) memset(pindex->pubkey33,0xff,33); else memset(pindex->pubkey33,0,33); - if ( komodo_blockload(block,pindex) == 0 ) + if ( hush_blockload(block,pindex) == 0 ) { hush_block2pubkey33(pindex->pubkey33,&block); //for (i=0; i<33; i++) @@ -884,7 +883,7 @@ void komodo_index2pubkey33(uint8_t *pubkey33,CBlockIndex *pindex,int32_t height) memset(pubkey33,0,33); if ( pindex != 0 ) { - if ( komodo_blockload(block,pindex) == 0 ) + if ( hush_blockload(block,pindex) == 0 ) hush_block2pubkey33(pubkey33,&block); } } @@ -928,7 +927,7 @@ int32_t komodo_eligiblenotary(uint8_t pubkeys[66][33],int32_t *mids,uint32_t blo if ( (pindex= hush_chainactive(height-i)) != 0 ) { blocktimes[i] = pindex->nTime; - if ( komodo_blockload(block,pindex) == 0 ) + if ( hush_blockload(block,pindex) == 0 ) { hush_block2pubkey33(pubkeys[i],&block); for (j=0; j= 10000 sats */ @@ -1276,7 +1275,7 @@ int8_t hush_segid(int32_t nocache,int32_t height) { if ( nocache == 0 && pindex->segid >= -1 ) return(pindex->segid); - if ( komodo_blockload(block,pindex) == 0 ) + if ( hush_blockload(block,pindex) == 0 ) { txn_count = block.vtx.size(); if ( txn_count > 1 && block.vtx[txn_count-1].vin.size() == 1 && block.vtx[txn_count-1].vout.size() == 1 ) @@ -1484,7 +1483,7 @@ uint64_t komodo_notarypayamount(int32_t nHeight, int64_t notarycount) return(ret); } -int32_t komodo_getnotarizedheight(uint32_t timestamp,int32_t height, uint8_t *script, int32_t len) +int32_t hush_getnotarizedheight(uint32_t timestamp,int32_t height, uint8_t *script, int32_t len) { // Check the notarisation is valid, and extract notarised height. uint64_t voutmask; @@ -1515,7 +1514,7 @@ uint64_t komodo_notarypay(CMutableTransaction &txNew, std::vector &Notar return(0); // Check the notarisation is valid. - int32_t notarizedheight = komodo_getnotarizedheight(timestamp, height, script, len); + int32_t notarizedheight = hush_getnotarizedheight(timestamp, height, script, len); if ( notarizedheight == 0 ) return(0); @@ -1570,7 +1569,7 @@ bool GetNotarizationNotaries(uint8_t notarypubkeys[64][33], int8_t &numNN, const return true; } -uint64_t komodo_checknotarypay(CBlock *pblock,int32_t height) +uint64_t hush_checknotarypay(CBlock *pblock,int32_t height) { std::vector NotarizationNotaries; uint8_t *script; int32_t scriptlen; uint64_t timestamp = pblock->nTime; @@ -1722,7 +1721,7 @@ void GetHushEarlytxidScriptPub() StartShutdown(); } -int64_t komodo_checkcommission(CBlock *pblock,int32_t height) +int64_t hush_checkcommission(CBlock *pblock,int32_t height) { if(fDebug) fprintf(stderr,"%s at height=%d\n",__func__,height); @@ -1732,7 +1731,7 @@ int64_t komodo_checkcommission(CBlock *pblock,int32_t height) checktoshis = the_commission(pblock,height); if ( checktoshis >= 10000 && pblock->vtx[0].vout.size() < 2 ) { - fprintf(stderr,"ERROR: komodo_checkcommission vsize.%d height.%d commission %.8f has checktoshis=%lu <10000 or less than 2 vouts (vouts=%lu)\n",(int32_t)pblock->vtx[0].vout.size(),height,(double)checktoshis/COIN, checktoshis, pblock->vtx[0].vout.size() ); + fprintf(stderr,"ERROR: hush_checkcommission vsize.%d height.%d commission %.8f has checktoshis=%lu <10000 or less than 2 vouts (vouts=%lu)\n",(int32_t)pblock->vtx[0].vout.size(),height,(double)checktoshis/COIN, checktoshis, pblock->vtx[0].vout.size() ); return(-1); } else if ( checktoshis != 0 ) @@ -1791,14 +1790,14 @@ int64_t komodo_checkcommission(CBlock *pblock,int32_t height) bool HUSH_TEST_ASSETCHAIN_SKIP_POW = 0; -int32_t komodo_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) +int32_t hush_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) { uint256 hash,merkleroot; arith_uint256 bnTarget,bhash; bool fNegative,fOverflow; uint8_t *script,pubkey33[33],pubkeys[64][33]; int32_t i,scriptlen,possible,PoSperc,is_PoSblock=0,n,failed = 0,notaryid = -1; int64_t checktoshis,value; CBlockIndex *pprev; if ( HUSH_TEST_ASSETCHAIN_SKIP_POW == 0 && Params().NetworkIDString() == "regtest" ) HUSH_TEST_ASSETCHAIN_SKIP_POW = 1; if ( !CheckEquihashSolution(pblock, Params()) ) { - fprintf(stderr,"komodo_checkPOW slowflag.%d ht.%d CheckEquihashSolution failed\n",slowflag,height); + fprintf(stderr,"hush_checkPOW slowflag.%d ht.%d CheckEquihashSolution failed\n",slowflag,height); return(-1); } hash = pblock->GetHash(); @@ -1855,7 +1854,7 @@ int32_t komodo_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) return(-1); } } else { - if ( komodo_checkcommission(pblock,height) < 0 ) + if ( hush_checkcommission(pblock,height) < 0 ) return(-1); } } @@ -1886,7 +1885,7 @@ int32_t komodo_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) } if(fDebug) { - fprintf(stderr,"komodo_checkPOW possible.%d slowflag.%d ht.%d notaryid.%d failed.%d\n",possible,slowflag,height,notaryid,failed); + fprintf(stderr,"hush_checkPOW possible.%d slowflag.%d ht.%d notaryid.%d failed.%d\n",possible,slowflag,height,notaryid,failed); } if ( failed != 0 && possible == 0 && notaryid < 0 ) { return(-1); @@ -1895,10 +1894,10 @@ int32_t komodo_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) } } -int32_t komodo_acpublic(uint32_t tiptime) +int32_t hush_scpublic(uint32_t tiptime) { - int32_t acpublic = ASSETCHAINS_PUBLIC; CBlockIndex *pindex; - return(acpublic); + // HUSH does not support public blockchains, go use something else if you want no privacy + return 0; } int64_t hush_newcoins(int64_t *zfundsp,int64_t *sproutfundsp,int32_t nHeight,CBlock *pblock) @@ -1962,7 +1961,7 @@ int64_t hush_coinsupply(int64_t *zfundsp,int64_t *sproutfundsp,int32_t height) { if ( pindex->newcoins == 0 && pindex->zfunds == 0 ) { - if ( komodo_blockload(block,pindex) == 0 ) { + if ( hush_blockload(block,pindex) == 0 ) { pindex->newcoins = hush_newcoins(&pindex->zfunds,&pindex->sproutfunds,pindex->GetHeight(),&block); } else { fprintf(stderr,"error loading block.%d\n",pindex->GetHeight()); diff --git a/src/hush_defs.h b/src/hush_defs.h index 7bf98cff7..f1ea61524 100644 --- a/src/hush_defs.h +++ b/src/hush_defs.h @@ -26,7 +26,7 @@ #define KOMODO_ELECTION_GAP 2000 #define ROUNDROBIN_DELAY 61 #define HUSH_SMART_CHAIN_MAXLEN 65 -#define KOMODO_LIMITED_NETWORKSIZE 4 +#define HUSH_LIMITED_NETWORKSIZE 4 #define DRAGON_MAXSCRIPTSIZE 10001 #define HUSH_MAXMEMPOOLTIME 3600 // affects consensus #define CRYPTO555_PUBSECPSTR "038a1bd41a08f38edda51042988022933c5775dfce81f7bae0b32a9179650352ac" @@ -480,7 +480,7 @@ static const char *notaries_elected[NUM_HUSH_SEASONS][NUM_HUSH_NOTARIES][2] = #define CLEARBIT(bits,bitoffset) (((uint8_t *)bits)[(bitoffset) >> 3] &= ~(1 << ((bitoffset) & 7))) #define HUSH_MAXNVALUE (((uint64_t)1 << 63) - 1) -#define KOMODO_BIT63SET(x) ((x) & ((uint64_t)1 << 63)) +#define HUSH_BIT63SET(x) ((x) & ((uint64_t)1 << 63)) #define HUSH_VALUETOOBIG(x) ((x) > (uint64_t)10000000001*COIN) //#ifndef TESTMODE @@ -500,7 +500,7 @@ extern uint64_t ASSETCHAINS_SUPPLY, ASSETCHAINS_FOUNDERS_REWARD; extern int32_t ASSETCHAINS_LWMAPOS, ASSETCHAINS_SAPLING, ASSETCHAINS_OVERWINTER,ASSETCHAINS_BLOCKTIME; extern uint64_t ASSETCHAINS_TIMELOCKGTE; extern uint32_t ASSETCHAINS_ALGO,ASSETCHAINS_EQUIHASH,HUSH_INITDONE; -extern int32_t HUSH_MININGTHREADS,HUSH_LONGESTCHAIN,ASSETCHAINS_SEED,IS_HUSH_NOTARY,USE_EXTERNAL_PUBKEY,HUSH_CHOSEN_ONE,KOMODO_ON_DEMAND,HUSH_PASSPORT_INITDONE,ASSETCHAINS_STAKED,HUSH_NSPV; +extern int32_t HUSH_MININGTHREADS,HUSH_LONGESTCHAIN,ASSETCHAINS_SEED,IS_HUSH_NOTARY,USE_EXTERNAL_PUBKEY,HUSH_CHOSEN_ONE,HUSH_ON_DEMAND,HUSH_PASSPORT_INITDONE,ASSETCHAINS_STAKED,HUSH_NSPV; extern uint64_t ASSETCHAINS_COMMISSION, ASSETCHAINS_LASTERA,ASSETCHAINS_CBOPRET; extern uint64_t ASSETCHAINS_REWARD[ASSETCHAINS_MAX_ERAS+1], ASSETCHAINS_NOTARY_PAY[ASSETCHAINS_MAX_ERAS+1], ASSETCHAINS_TIMELOCKGTE, ASSETCHAINS_NONCEMASK[],ASSETCHAINS_NK[2]; extern const char *ASSETCHAINS_ALGORITHMS[]; @@ -509,7 +509,7 @@ extern std::string NOTARY_PUBKEY,ASSETCHAINS_OVERRIDE_PUBKEY,ASSETCHAINS_SCRIPTP extern uint8_t NOTARY_PUBKEY33[33],ASSETCHAINS_OVERRIDE_PUBKEY33[33],ASSETCHAINS_MARMARA; extern std::vector ASSETCHAINS_PRICES,ASSETCHAINS_STOCKS; extern uint256 HUSH_EARLYTXID; -extern int32_t HUSH_CONNECTING,HUSH_CCACTIVATE,KOMODO_DEALERNODE; +extern int32_t HUSH_CONNECTING,HUSH_CCACTIVATE,HUSH_DEALERNODE; extern uint32_t ASSETCHAINS_CC; extern std::string CCerror,ASSETCHAINS_CCLIB; extern uint8_t ASSETCHAINS_CCDISABLES[256]; @@ -562,13 +562,13 @@ bool hush_hardfork_active(uint32_t time); uint256 Parseuint256(const char *hexstr); void komodo_sendmessage(int32_t minpeers, int32_t maxpeers, const char *message, std::vector payload); -CBlockIndex *komodo_getblockindex(uint256 hash); +CBlockIndex *hush_getblockindex(uint256 hash); int32_t hush_nextheight(); CBlockIndex *hush_blockindex(uint256 hash); CBlockIndex *hush_chainactive(int32_t height); int32_t hush_blockheight(uint256 hash); bool komodo_txnotarizedconfirmed(uint256 txid); -int32_t komodo_blockload(CBlock& block, CBlockIndex *pindex); +int32_t hush_blockload(CBlock& block, CBlockIndex *pindex); uint32_t hush_chainactive_timestamp(); uint32_t GetLatestTimestamp(int32_t height); diff --git a/src/hush_gateway.h b/src/hush_gateway.h index 1db0cb454..0c0b56942 100644 --- a/src/hush_gateway.h +++ b/src/hush_gateway.h @@ -485,7 +485,7 @@ int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t to strcpy(symbol,base); if ( SMART_CHAIN_SYMBOL[0] != 0 && komodo_baseid(SMART_CHAIN_SYMBOL) < 0 ) return(0); - PENDING_KOMODO_TX = 0; + PENDING_HUSH_TX = 0; for (i=0; i<3; i++) { if ( komodo_isrealtime(&ht) != 0 ) @@ -589,13 +589,13 @@ int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t to data[len++] = ((uint8_t *)&pax->txid)[i]; data[len++] = pax->vout & 0xff; data[len++] = (pax->vout >> 8) & 0xff; - PENDING_KOMODO_TX += pax->fiatoshis; + PENDING_HUSH_TX += pax->fiatoshis; } else { len += komodo_rwapproval(1,&data[len],pax); - PENDING_KOMODO_TX += pax->komodoshis; - printf(" len.%d vout.%u DEPOSIT %.8f <- pax.%s pending ht %d %d %.8f | ",len,pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,pax->height,pax->otherheight,dstr(PENDING_KOMODO_TX)); + PENDING_HUSH_TX += pax->komodoshis; + printf(" len.%d vout.%u DEPOSIT %.8f <- pax.%s pending ht %d %d %.8f | ",len,pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,pax->height,pax->otherheight,dstr(PENDING_HUSH_TX)); } if ( numvouts++ >= 64 || sum > COIN ) break; @@ -618,7 +618,7 @@ int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t to memcpy(script,opret,opretlen); for (i=0; i<8; i++) printf("%02x",opret[i]); - printf(" <- opret, MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,SMART_CHAIN_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen); + printf(" <- opret, MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,SMART_CHAIN_SYMBOL,numvouts,dstr(PENDING_HUSH_TX),opretlen); return(1); } return(0); @@ -656,7 +656,7 @@ const char *banned_txids[] = //"ce567928b5490a17244167af161b1d8dd6ff753fef222fe6855d95b2278a35b3", // missed }; -int32_t komodo_checkvout(int32_t vout,int32_t k,int32_t indallvouts) +int32_t hush_checkvout(int32_t vout,int32_t k,int32_t indallvouts) { if ( k < indallvouts ) return(vout == 1); @@ -681,7 +681,7 @@ int32_t komodo_bannedset(int32_t *indallvoutsp,uint256 *array,int32_t max) void hush_passport_iteration(); -int32_t komodo_check_deposit(int32_t height,const CBlock& block,uint32_t prevtime) // verify above block is valid pax pricing +int32_t hush_check_deposit(int32_t height,const CBlock& block,uint32_t prevtime) // verify above block is valid pax pricing { static uint256 array[64]; static int32_t numbanned,indallvouts; int32_t i,j,k,n,ht,baseid,txn_count,activation,num,opretlen,offset=1,errs=0,notmatched=0,matched=0,kmdheights[256],otherheights[256]; uint256 hash,txids[256]; char symbol[HUSH_SMART_CHAIN_MAXLEN],base[HUSH_SMART_CHAIN_MAXLEN]; uint16_t vouts[256]; int8_t baseids[256]; uint8_t *script,opcode,rmd160s[256*20]; uint64_t total,subsidy,available,deposited,issued,withdrawn,approved,redeemed,seed; int64_t checktoshis,values[256],srcvalues[256]; struct pax_transaction *pax; struct hush_state *sp; CTransaction tx; @@ -730,7 +730,7 @@ int32_t komodo_check_deposit(int32_t height,const CBlock& block,uint32_t prevtim { for (k=0; k= indallvouts) ) + if ( block.vtx[i].vin[j].prevout.hash == array[k] && hush_checkvout(block.vtx[i].vin[j].prevout.n,k,indallvouts) != 0 ) //(block.vtx[i].vin[j].prevout.n == 1 || k >= indallvouts) ) { printf("banned tx.%d being used at ht.%d txi.%d vini.%d\n",k,height,i,j); return(-1); @@ -804,7 +804,7 @@ int32_t komodo_check_deposit(int32_t height,const CBlock& block,uint32_t prevtim checktoshis = 0; if ( (ASSETCHAINS_COMMISSION != 0 || ASSETCHAINS_FOUNDERS_REWARD) && height > 1 ) { - if ( (checktoshis= komodo_checkcommission((CBlock *)&block,height)) < 0 ) + if ( (checktoshis= hush_checkcommission((CBlock *)&block,height)) < 0 ) { fprintf(stderr,"ht.%d checktoshis %.8f overflow.%d total %.8f strangeout.%d\n",height,dstr(checktoshis),overflow,dstr(total),strangeout); return(-1); @@ -1568,7 +1568,7 @@ void hush_passport_iteration() extern std::vector Mineropret; // opreturn data set by the data gathering code #define PRICES_ERRORRATE (COIN / 100) // maximum acceptable change, set at 1% #define PRICES_SIZEBIT0 (sizeof(uint32_t) * 4) // 4 uint32_t unixtimestamp, BTCUSD, BTCGBP and BTCEUR -#define KOMODO_LOCALPRICE_CACHESIZE 13 +#define HUSH_LOCALPRICE_CACHESIZE 13 #define HUSH_MAXPRICES 2048 #define PRICES_SMOOTHWIDTH 1 @@ -1594,14 +1594,14 @@ struct komodo_priceinfo char symbol[64]; } PRICES[HUSH_MAXPRICES]; -uint32_t PriceCache[KOMODO_LOCALPRICE_CACHESIZE][HUSH_MAXPRICES];//4+sizeof(Cryptos)/sizeof(*Cryptos)+sizeof(Forex)/sizeof(*Forex)]; +uint32_t PriceCache[HUSH_LOCALPRICE_CACHESIZE][HUSH_MAXPRICES];//4+sizeof(Cryptos)/sizeof(*Cryptos)+sizeof(Forex)/sizeof(*Forex)]; int64_t PriceMult[HUSH_MAXPRICES]; -int32_t komodo_cbopretsize(uint64_t flags); +int32_t hush_cbopretsize(uint64_t flags); void komodo_PriceCache_shift() { int32_t i; - for (i=KOMODO_LOCALPRICE_CACHESIZE-1; i>0; i--) + for (i=HUSH_LOCALPRICE_CACHESIZE-1; i>0; i--) memcpy(PriceCache[i],PriceCache[i-1],sizeof(PriceCache[i])); memcpy(PriceCache[0],Mineropret.data(),Mineropret.size()); } @@ -1630,7 +1630,7 @@ int32_t hush_heightpricebits(uint64_t *seedp,uint32_t *heightbits,int32_t nHeigh *seedp = 0; if ( (pindex= hush_chainactive(nHeight)) != 0 ) { - if ( komodo_blockload(block,pindex) == 0 ) + if ( hush_blockload(block,pindex) == 0 ) { return(_hush_heightpricebits(seedp,heightbits,&block)); } @@ -1727,7 +1727,7 @@ CScript komodo_mineropret(int32_t nHeight) if ( numzero != 0 ) { fprintf(stderr," komodo_mineropret numzero.%d vs n.%d\n",numzero,n); - komodo_cbopretupdate(1); + hush_cbopretupdate(1); sleep(61); } } @@ -1856,13 +1856,13 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i if ( iter == 0 ) break; // second iteration checks recent prices to see if within local volatility - for (j=0; j= prevbits[i] ) { fprintf(stderr,"i.%d within recent localprices[%d] %u >= %u\n",i,j,PriceCache[j][i],prevbits[i]); break; } - if ( j == KOMODO_LOCALPRICE_CACHESIZE ) + if ( j == HUSH_LOCALPRICE_CACHESIZE ) { komodo_queuelocalprice(1,nHeight,block->nTime,bhash,i,prevbits[i]); break; @@ -1872,13 +1872,13 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i { if ( iter == 0 ) break; - for (j=0; jnTime,bhash,i,prevbits[i]); break; @@ -1891,7 +1891,7 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i if ( iter == 0 ) { fprintf(stderr,"force update prices\n"); - komodo_cbopretupdate(1); + hush_cbopretupdate(1); memcpy(localbits,Mineropret.data(),Mineropret.size()); } else return(-1); } @@ -2199,10 +2199,10 @@ int32_t get_btcusd(uint32_t pricebits[4]) return(-1); } -// komodo_cbopretupdate() obtains the external price data and encodes it into Mineropret, which will then be used by the miner and validation +// hush_cbopretupdate() obtains the external price data and encodes it into Mineropret, which will then be used by the miner and validation // save history, use new data to approve past rejection, where is the auto-reconsiderblock? -int32_t komodo_cbopretsize(uint64_t flags) +int32_t hush_cbopretsize(uint64_t flags) { int32_t size = 0; if ( (ASSETCHAINS_CBOPRET & 1) != 0 ) @@ -2220,7 +2220,7 @@ int32_t komodo_cbopretsize(uint64_t flags) extern uint256 Queued_reconsiderblock; -void komodo_cbopretupdate(int32_t forceflag) +void hush_cbopretupdate(int32_t forceflag) { static uint32_t lasttime,lastbtc,pending; static uint32_t pricebits[4],pricebuf[HUSH_MAXPRICES],forexprices[sizeof(Forex)/sizeof(*Forex)]; @@ -2243,7 +2243,7 @@ void komodo_cbopretupdate(int32_t forceflag) { //if ( hush_nextheight() > 333 ) // for debug only! // ASSETCHAINS_CBOPRET = 7; - size = komodo_cbopretsize(ASSETCHAINS_CBOPRET); + size = hush_cbopretsize(ASSETCHAINS_CBOPRET); if ( Mineropret.size() < size ) Mineropret.resize(size); size = PRICES_SIZEBIT0; @@ -2418,7 +2418,7 @@ char *komodo_pricename(char *name,int32_t ind) // finds index for its symbol name int32_t komodo_priceind(const char *symbol) { - char name[65]; int32_t i,n = (int32_t)(komodo_cbopretsize(ASSETCHAINS_CBOPRET) / sizeof(uint32_t)); + char name[65]; int32_t i,n = (int32_t)(hush_cbopretsize(ASSETCHAINS_CBOPRET) / sizeof(uint32_t)); for (i=1; iheight = height; - if ( (pindex= hush_chainactive(height)) != 0 && komodo_blockload(block,pindex) == 0 ) + if ( (pindex= hush_chainactive(height)) != 0 && hush_blockload(block,pindex) == 0 ) { BOOST_FOREACH(const CTransaction&tx, block.vtx) { diff --git a/src/hush_pax.h b/src/hush_pax.h index 06aec7fbe..d2984e18c 100644 --- a/src/hush_pax.h +++ b/src/hush_pax.h @@ -742,7 +742,7 @@ uint64_t PAX_fiatdest(uint64_t *seedp,int32_t tokomodo,char *destaddr,uint8_t pu if ( bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr) == 20 ) { PAX_pubkey(1,pubkey33,&addrtype,rmd160,base,&shortflag,tokomodo != 0 ? &komodoshis : &fiatoshis); - bitcoin_address(destaddr,KOMODO_PUBTYPE,pubkey33,33); + bitcoin_address(destaddr,HUSH_PUBTYPE,pubkey33,33); } return(komodoshis); } diff --git a/src/hush_utils.h b/src/hush_utils.h index edc55a47c..23a7d39f2 100644 --- a/src/hush_utils.h +++ b/src/hush_utils.h @@ -49,7 +49,7 @@ typedef struct queue #define CRYPTO555_HUSHADDR "RFetqf8WUfWnwNeXdknkm8ojk7EXnYFzrv" #define CRYPTO555_RMD160STR "deadbeefbadcaca748c4986b240ff7d7bc3fffb0" -#define KOMODO_PUBTYPE 60 +#define HUSH_PUBTYPE 60 struct sha256_vstate { uint64_t length; uint32_t state[8],curlen; uint8_t buf[64]; }; struct rmd160_vstate { uint64_t length; uint8_t buf[64]; uint32_t curlen, state[5]; }; @@ -1025,7 +1025,7 @@ int32_t komodo_opreturnscript(uint8_t *script,uint8_t type,uint8_t *opret,int32_ // get a pseudo random number that is the same for each block individually at all times and different // from all other blocks. the sequence is extremely likely, but not guaranteed to be unique for each block chain -uint64_t komodo_block_prg(uint32_t nHeight) +uint64_t hush_block_prg(uint32_t nHeight) { int i; uint8_t hashSrc[8]; @@ -1050,7 +1050,7 @@ uint64_t komodo_block_prg(uint32_t nHeight) // given a block height, this returns the unlock time for that block height, derived from // the ASSETCHAINS_MAGIC number as well as the block height, providing different random numbers // for corresponding blocks across chains, but the same sequence in each chain -int64_t komodo_block_unlocktime(uint32_t nHeight) +int64_t hush_block_unlocktime(uint32_t nHeight) { uint64_t fromTime, toTime, unlocktime; @@ -1058,7 +1058,7 @@ int64_t komodo_block_unlocktime(uint32_t nHeight) unlocktime = ASSETCHAINS_TIMEUNLOCKTO; else { - unlocktime = komodo_block_prg(nHeight) / (0xffffffffffffffff / ((ASSETCHAINS_TIMEUNLOCKTO - ASSETCHAINS_TIMEUNLOCKFROM) + 1)); + unlocktime = hush_block_prg(nHeight) / (0xffffffffffffffff / ((ASSETCHAINS_TIMEUNLOCKTO - ASSETCHAINS_TIMEUNLOCKFROM) + 1)); // boundary and power of 2 can make it exceed to time by 1 unlocktime = unlocktime + ASSETCHAINS_TIMEUNLOCKFROM; if (unlocktime > ASSETCHAINS_TIMEUNLOCKTO) @@ -1320,7 +1320,7 @@ uint16_t _komodo_userpass(char *username,char *password,FILE *fp) strcpy(username,rpcuser); strcpy(password,rpcpassword); } - //printf("rpcuser.(%s) rpcpassword.(%s) KMDUSERPASS.(%s) %u\n",rpcuser,rpcpassword,KMDUSERPASS,port); + //printf("rpcuser.(%s) rpcpassword.(%s) HUSHUSERPASS.(%s) %u\n",rpcuser,rpcpassword,HUSHUSERPASS,port); if ( rpcuser != 0 ) free(rpcuser); if ( rpcpassword != 0 ) @@ -1403,9 +1403,7 @@ void komodo_configfile(char *symbol,uint16_t rpcport) printf("Created (%s)\n",fname); } else printf("Couldnt create (%s)\n",fname); #endif - } - else - { + } else { _komodo_userpass(myusername,mypassword,fp); mapArgs["-rpcpassword"] = mypassword; mapArgs["-rpcusername"] = myusername; @@ -1430,10 +1428,10 @@ void komodo_configfile(char *symbol,uint16_t rpcport) if ( (fp= fopen(fname,"rb")) != 0 ) { if ( (kmdport= _komodo_userpass(username,password,fp)) != 0 ) - KMD_PORT = kmdport; - sprintf(KMDUSERPASS,"%s:%s",username,password); + HUSH3_PORT = kmdport; + sprintf(HUSHUSERPASS,"%s:%s",username,password); fclose(fp); -//printf("KOMODO.(%s) -> userpass.(%s)\n",fname,KMDUSERPASS); + //printf("HUSH.(%s) -> userpass.(%s)\n",fname,HUSHUSERPASS); } //else printf("couldnt open.(%s)\n",fname); } @@ -1492,9 +1490,6 @@ uint16_t hush_smartport(uint32_t magic,int32_t extralen) else return(16000 + (magic % 49500)); } -// DUKE: extralen is sometimes wrong which causes wrong diffbits? -// This function is only called on startup, blocktime changing code does not modify magic -// Hush 3.5.x and Hush 3.6.x have same history and p2p ports, but different magic??? Seems not. uint16_t hush_port(char *symbol,uint64_t supply,uint32_t *magicp,uint8_t *extraptr,int32_t extralen) { if(fDebug) @@ -1629,7 +1624,7 @@ uint64_t hush_block_subsidy(int height) } // wrapper for more general supply curves of Hush Smart Chains -uint64_t komodo_ac_block_subsidy(int nHeight) +uint64_t hush_sc_block_subsidy(int nHeight) { //fprintf(stderr,"%s: ht.%d\n", __func__, nHeight); // Find current era, start from beginning reward, and determine current subsidy @@ -1730,7 +1725,7 @@ uint64_t komodo_ac_block_subsidy(int nHeight) } extern int64_t MAX_MONEY; -void komodo_cbopretupdate(int32_t forceflag); +void hush_cbopretupdate(int32_t forceflag); void SplitStr(const std::string& strVal, std::vector &outVals); int8_t equihash_params_possible(uint64_t n, uint64_t k) @@ -1780,7 +1775,7 @@ void hush_args(char *argv0) } DONATION_PUBKEY = GetArg("-donation", ""); NOTARY_PUBKEY = GetArg("-pubkey", ""); - KOMODO_DEALERNODE = GetArg("-dealer",0); + HUSH_DEALERNODE = GetArg("-dealer",0); HUSH_TESTNODE = GetArg("-testnode",0); if ( strlen(NOTARY_PUBKEY.c_str()) == 66 ) @@ -1827,12 +1822,12 @@ void hush_args(char *argv0) ASSETCHAINS_CC = GetArg("-ac_cc",0); HUSH_CCACTIVATE = GetArg("-ac_ccactivate",0); ASSETCHAINS_BLOCKTIME = GetArg("-ac_blocktime",60); - ASSETCHAINS_PUBLIC = GetArg("-ac_public",0); + ASSETCHAINS_PUBLIC = 0; ASSETCHAINS_PRIVATE = GetArg("-ac_private",0); - HUSH_SNAPSHOT_INTERVAL = GetArg("-ac_snapshot",0); + HUSH_SNAPSHOT_INTERVAL = GetArg("-ac_snapshot",0); Split(GetArg("-ac_nk",""), sizeof(ASSETCHAINS_NK)/sizeof(*ASSETCHAINS_NK), ASSETCHAINS_NK, 0); - fprintf(stderr,".oO Starting HUSH Full Node with genproc=%d notary=%d\n",HUSH_MININGTHREADS, IS_HUSH_NOTARY); + fprintf(stderr,".oO Starting HUSH Full Node (Extreme Privacy!) with genproc=%d notary=%d\n",HUSH_MININGTHREADS, IS_HUSH_NOTARY); // -ac_ccactivateht=evalcode,height,evalcode,height,evalcode,height.... Split(GetArg("-ac_ccactivateht",""), sizeof(ccEnablesHeight)/sizeof(*ccEnablesHeight), ccEnablesHeight, 0); @@ -2242,7 +2237,7 @@ void hush_args(char *argv0) } fprintf(stderr," <- CCLIB name\n"); } - // is this not being run correctly? + if ( ASSETCHAINS_BLOCKTIME != 60 ) { extralen += dragon_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_BLOCKTIME),(void *)&ASSETCHAINS_BLOCKTIME); fprintf(stderr,"%s: ASSETCHAINS_BLOCKTIME=%d, extralen=%d\n", __func__, ASSETCHAINS_BLOCKTIME, extralen); @@ -2275,7 +2270,7 @@ void hush_args(char *argv0) } } //komodo_pricesinit(); - komodo_cbopretupdate(1); // will set Mineropret + hush_cbopretupdate(1); // will set Mineropret fprintf(stderr,"This blockchain uses data produced from CoinDesk Bitcoin Price Index\n"); } if ( ASSETCHAINS_NK[0] != 0 && ASSETCHAINS_NK[1] != 0 ) @@ -2316,7 +2311,7 @@ void hush_args(char *argv0) if ( ASSETCHAINS_CC >= HUSH_FIRSTFUNGIBLEID && MAX_MONEY < 1000000LL*SATOSHIDEN ) MAX_MONEY = 1000000LL*SATOSHIDEN; - if ( KOMODO_BIT63SET(MAX_MONEY) != 0 ) + if ( HUSH_BIT63SET(MAX_MONEY) != 0 ) MAX_MONEY = HUSH_MAXNVALUE; if(fDebug) fprintf(stderr,"MAX_MONEY %llu %.8f\n",(long long)MAX_MONEY,(double)MAX_MONEY/SATOSHIDEN); @@ -2425,9 +2420,9 @@ void hush_args(char *argv0) if ( (fp= fopen(fname,"rb")) != 0 ) { _komodo_userpass(username,password,fp); - sprintf(iter == 0 ? KMDUSERPASS : BTCUSERPASS,"%s:%s",username,password); + sprintf(iter == 0 ? HUSHUSERPASS : BTCUSERPASS,"%s:%s",username,password); fclose(fp); - //printf("KOMODO.(%s) -> userpass.(%s)\n",fname,KMDUSERPASS); + //printf("HUSH.(%s) -> userpass.(%s)\n",fname,HUSHUSERPASS); } //else printf("couldnt open.(%s)\n",fname); if ( IS_HUSH_NOTARY == 0 ) break; diff --git a/src/init.cpp b/src/init.cpp index ab3b5e778..40d1738f3 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -88,7 +88,7 @@ using namespace std; #include "hush_defs.h" extern void ThreadSendAlert(); -extern bool komodo_dailysnapshot(int32_t height); +extern bool hush_dailysnapshot(int32_t height); extern int32_t HUSH_LOADINGBLOCKS; extern char SMART_CHAIN_SYMBOL[]; extern int32_t HUSH_SNAPSHOT_INTERVAL; @@ -431,15 +431,14 @@ std::string HelpMessage(HelpMessageMode mode) strUsage += HelpMessageOpt("-peerbloomfilters", strprintf(_("Support filtering of blocks and transaction with Bloom filters (default: %u)"), 1)); if (showDebug) strUsage += HelpMessageOpt("-enforcenodebloom", strprintf("Enforce minimum protocol version to limit use of Bloom filters (default: %u)", 0)); - strUsage += HelpMessageOpt("-port=", strprintf(_("Listen for connections on (default: %u or testnet: %u)"), 7770, 17770)); + strUsage += HelpMessageOpt("-port=", strprintf(_("Listen for connections on (default: %u or testnet: %u)"), 55555, 55420)); strUsage += HelpMessageOpt("-proxy=", _("Connect through SOCKS5 proxy")); strUsage += HelpMessageOpt("-proxyrandomize", strprintf(_("Randomize credentials for every proxy connection. This enables Tor stream isolation (default: %u)"), 1)); strUsage += HelpMessageOpt("-seednode=", _("Connect to a node to retrieve peer addresses, and disconnect")); strUsage += HelpMessageOpt("-timeout=", strprintf(_("Specify connection timeout in milliseconds (minimum: 1, default: %d)"), DEFAULT_CONNECT_TIMEOUT)); strUsage += HelpMessageOpt("-torcontrol=:", strprintf(_("Tor control port to use if onion listening enabled (default: %s)"), DEFAULT_TOR_CONTROL)); strUsage += HelpMessageOpt("-torpassword=", _("Tor control port password (default: empty)")); - strUsage += HelpMessageOpt("-tls=