From 774229c4b3cab2d443fb1bf3f4bff670b7329820 Mon Sep 17 00:00:00 2001 From: dexX7 Date: Fri, 27 Mar 2015 11:35:09 +0100 Subject: [PATCH 1/3] [Meta DEx] enable, but hide RPC calls Should have no functional effect, as parsing transactions is guarded by the `isTransactionTypeAllowed` checks. --- src/mastercore.h | 2 -- src/mastercore_rpc.cpp | 2 -- src/rpcclient.cpp | 2 -- src/rpcserver.cpp | 14 ++++++-------- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/mastercore.h b/src/mastercore.h index 08441e5376579..7545621640c3e 100644 --- a/src/mastercore.h +++ b/src/mastercore.h @@ -32,8 +32,6 @@ using json_spirit::Array; using std::string; -#define DISABLE_METADEX - #define LOG_FILENAME "mastercore.log" #define INFO_FILENAME "mastercore_crowdsales.log" #define OWNERS_FILENAME "mastercore_owners.log" diff --git a/src/mastercore_rpc.cpp b/src/mastercore_rpc.cpp index fd76103b76666..dcb40d949da45 100644 --- a/src/mastercore_rpc.cpp +++ b/src/mastercore_rpc.cpp @@ -940,7 +940,6 @@ int check_prop_valid(int64_t tmpPropId, string error, string exist_error ) { return tmpPropId; } -#ifndef DISABLE_METADEX Value trade_MP(const Array& params, bool fHelp) { if (fHelp || params.size() < 6) @@ -1212,7 +1211,6 @@ Value gettradehistory_MP(const Array& params, bool fHelp) return response; } -#endif Value getactivedexsells_MP(const Array& params, bool fHelp) { diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp index 17fd7f2880ffa..1d2aab6081739 100644 --- a/src/rpcclient.cpp +++ b/src/rpcclient.cpp @@ -103,7 +103,6 @@ static const CRPCConvertParam vRPCConvertParams[] = { "listtransactions_MP", 4 }, { "getallbalancesforid_MP", 0 }, { "listblocktransactions_MP", 0 }, -#if 0 // disabled for pre meta DEx release { "trade_MP", 2 }, { "trade_MP", 4 }, { "trade_MP", 5 }, @@ -114,7 +113,6 @@ static const CRPCConvertParam vRPCConvertParams[] = { "gettradessince_MP", 2 }, { "gettradehistory_MP", 1 }, { "gettradehistory_MP", 2 }, -#endif }; class CRPCConvertTable diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 052c526532dbb..1eebcd05e1605 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -364,14 +364,12 @@ static const CRPCCommand vRPCCommands[] = { "omni layer", "getgrants_MP", &getgrants_MP, false, false, true }, { "omni layer", "getactivedexsells_MP", &getactivedexsells_MP, false, false, true }, { "omni layer", "getactivecrowdsales_MP", &getactivecrowdsales_MP, false, false, true }, -#if 0 // disabled for pre meta DEx release - { "omni layer", "trade_MP", &trade_MP, false, false, true }, - { "omni layer", "getorderbook_MP", &getorderbook_MP, false, false, true }, - { "omni layer", "gettradessince_MP", &gettradessince_MP, false, false, true }, - { "omni layer", "getopenorders_MP", &getopenorders_MP, false, false, true }, - { "omni layer", "gettradehistory_MP", &gettradehistory_MP, false, false, true }, - { "omni layer", "gettrade_MP", &gettrade_MP, false, false, true }, -#endif + { "hidden", "trade_MP", &trade_MP, false, false, true }, + { "hidden", "getorderbook_MP", &getorderbook_MP, false, false, true }, + { "hidden", "gettradessince_MP", &gettradessince_MP, false, false, true }, + { "hidden", "getopenorders_MP", &getopenorders_MP, false, false, true }, + { "hidden", "gettradehistory_MP", &gettradehistory_MP, false, false, true }, + { "hidden", "gettrade_MP", &gettrade_MP, false, false, true }, { "omni layer", "sendtoowners_MP", &sendtoowners_MP, false, false, true }, { "omni layer", "sendrawtx_MP", &sendrawtx_MP, false, false, true }, { "omni layer", "getsto_MP", &getsto_MP, false, false, true }, From b720738900df346d94681a3c1b94211159a41fdd Mon Sep 17 00:00:00 2001 From: dexX7 Date: Tue, 9 Dec 2014 15:21:15 +0100 Subject: [PATCH 2/3] [Meta DEx] Add missing "cancel" checks + "cancel-everything" logic This commit adds all missing checks before executing "cancel-at-price", "cancel-all-for-pair" and "cancel-everything". Furthermore the behavior of "cancel-everything" was enhanced, such that it cancels all open orders, if offered and desired properties are within the same ecosystem, and otherwise it cancels all open orders for all properties of both ecosystems. --- src/mastercore_dex.cpp | 16 ++++++---- src/mastercore_dex.h | 8 ++--- src/mastercore_tx.cpp | 70 ++++++++++++++++++++++++++++++------------ 3 files changed, 65 insertions(+), 29 deletions(-) diff --git a/src/mastercore_dex.cpp b/src/mastercore_dex.cpp index b9dc2b110d663..65a481ff188c9 100644 --- a/src/mastercore_dex.cpp +++ b/src/mastercore_dex.cpp @@ -721,7 +721,7 @@ AcceptMap::iterator my_it = my_accepts.begin(); } // pretty much directly linked to the ADD TX21 command off the wire -int mastercore::MetaDEx_ADD(const string &sender_addr, unsigned int prop, uint64_t amount, int block, unsigned int property_desired, uint64_t amount_desired, const uint256 &txid, unsigned int idx) +int mastercore::MetaDEx_ADD(const std::string& sender_addr, unsigned int prop, uint64_t amount, int block, unsigned int property_desired, uint64_t amount_desired, const uint256& txid, unsigned int idx) { int rc = METADEX_ERROR -1; @@ -816,7 +816,7 @@ int rc = METADEX_ERROR -1; return rc; } -int mastercore::MetaDEx_CANCEL_AT_PRICE(const uint256 txid, unsigned int block, const string &sender_addr, unsigned int prop, uint64_t amount, unsigned int property_desired, uint64_t amount_desired) +int mastercore::MetaDEx_CANCEL_AT_PRICE(const uint256& txid, unsigned int block, const std::string& sender_addr, unsigned int prop, uint64_t amount, unsigned int property_desired, uint64_t amount_desired) { int rc = METADEX_ERROR -20; CMPMetaDEx mdex(sender_addr, 0, prop, amount, property_desired, amount_desired, 0, 0, CMPTransaction::CANCEL_AT_PRICE); @@ -874,7 +874,7 @@ const CMPMetaDEx *p_mdex = NULL; return rc; } -int mastercore::MetaDEx_CANCEL_ALL_FOR_PAIR(const uint256 txid, unsigned int block, const string &sender_addr, unsigned int prop, unsigned int property_desired) +int mastercore::MetaDEx_CANCEL_ALL_FOR_PAIR(const uint256& txid, unsigned int block, const std::string& sender_addr, unsigned int prop, unsigned int property_desired) { int rc = METADEX_ERROR -30; md_PricesMap *prices = get_Prices(prop); @@ -928,9 +928,9 @@ const CMPMetaDEx *p_mdex = NULL; } // scan the orderbook and remove everything for an address -int mastercore::MetaDEx_CANCEL_EVERYTHING(const uint256 txid, unsigned int block, const string &sender_addr) +int mastercore::MetaDEx_CANCEL_EVERYTHING(const uint256& txid, unsigned int block, const std::string& sender_addr, unsigned char ecosystem) { -int rc = METADEX_ERROR -40; + int rc = METADEX_ERROR -40; file_log("%s()\n", __FUNCTION__); @@ -942,6 +942,10 @@ int rc = METADEX_ERROR -40; { unsigned int prop = my_it->first; + // skip property, if it is not in the expected ecosystem + if (isMainEcosystemProperty(ecosystem) && !isMainEcosystemProperty(prop)) continue; + if (isTestEcosystemProperty(ecosystem) && !isTestEcosystemProperty(prop)) continue; + file_log(" ## property: %u\n", prop); md_PricesMap & prices = my_it->second; @@ -956,7 +960,7 @@ int rc = METADEX_ERROR -40; { file_log("%s= %s\n", price.str(DISPLAY_PRECISION_LEN, std::ios_base::fixed) , it->ToString()); - if ((it->getAddr() != sender_addr)) + if (it->getAddr() != sender_addr) { ++it; continue; diff --git a/src/mastercore_dex.h b/src/mastercore_dex.h index 54340c05667c9..9f194543c6f30 100644 --- a/src/mastercore_dex.h +++ b/src/mastercore_dex.h @@ -311,10 +311,10 @@ int DEx_acceptCreate(const string &buyer, const string &seller, int, uint64_t nV int DEx_acceptDestroy(const string &buyer, const string &seller, int, bool bForceErase = false); int DEx_payment(uint256 txid, unsigned int vout, string seller, string buyer, uint64_t BTC_paid, int blockNow, uint64_t *nAmended = NULL); -int MetaDEx_ADD(const string &sender_addr, unsigned int, uint64_t, int block, unsigned int property_desired, uint64_t amount_desired, const uint256 &txid, unsigned int idx); -int MetaDEx_CANCEL_AT_PRICE(const uint256, unsigned int, const string &, unsigned int, uint64_t, unsigned int, uint64_t); -int MetaDEx_CANCEL_ALL_FOR_PAIR(const uint256, unsigned int, const string &, unsigned int, unsigned int); -int MetaDEx_CANCEL_EVERYTHING(const uint256, unsigned int, const string &); +int MetaDEx_ADD(const std::string& sender_addr, unsigned int, uint64_t, int block, unsigned int property_desired, uint64_t amount_desired, const uint256& txid, unsigned int idx); +int MetaDEx_CANCEL_AT_PRICE(const uint256&, unsigned int, const std::string&, unsigned int, uint64_t, unsigned int, uint64_t); +int MetaDEx_CANCEL_ALL_FOR_PAIR(const uint256&, unsigned int, const std::string&, unsigned int, unsigned int); +int MetaDEx_CANCEL_EVERYTHING(const uint256& txid, unsigned int block, const std::string& sender_addr, unsigned char ecosystem); md_PricesMap *get_Prices(unsigned int prop); md_Set *get_Indexes(md_PricesMap *p, XDOUBLE price); diff --git a/src/mastercore_tx.cpp b/src/mastercore_tx.cpp index 82dd0056c3314..9f9d4e06a45f6 100644 --- a/src/mastercore_tx.cpp +++ b/src/mastercore_tx.cpp @@ -467,10 +467,8 @@ int rc = DEX_ERROR_ACCEPT; int CMPTransaction::logicMath_MetaDEx(CMPMetaDEx *mdex_o) { -int rc = PKT_ERROR_METADEX -100; -unsigned char action = 0; - - if (!isTransactionTypeAllowed(block, property, type, version)) return (PKT_ERROR_METADEX -888); + int rc = PKT_ERROR_METADEX -100; + unsigned char action = 0; memcpy(&desired_property, &pkt[16], 4); swapByteOrder32(desired_property); @@ -494,46 +492,80 @@ unsigned char action = 0; switch (action) { case ADD: - if (!isTransactionTypeAllowed(block, desired_property, type, version)) return (PKT_ERROR_METADEX -889); + { + if (!isTransactionTypeAllowed(block, property, type, version)) return (PKT_ERROR_METADEX -889); // ensure we are not trading same property for itself if (property == desired_property) return (PKT_ERROR_METADEX -5); - // ensure no cross-over of currencies from Test Eco to normal + // ensure no cross-over of properties from test to main ecosystem if (isTestEcosystemProperty(property) != isTestEcosystemProperty(desired_property)) return (PKT_ERROR_METADEX -4); // ensure the desired property exists in our universe if (!_my_sps->hasSP(desired_property)) return (PKT_ERROR_METADEX -30); - if (!nValue) return (PKT_ERROR_METADEX -11); - if (!desired_value) return (PKT_ERROR_METADEX -12); + // ensure offered and desired values are positive + if (0 >= static_cast(nNewValue)) return (PKT_ERROR_METADEX -11); + if (0 >= static_cast(desired_value)) return (PKT_ERROR_METADEX -12); // ensure sufficient balance is available to offer - if (getMPbalance(sender, property, BALANCE) < (int64_t)nValue) return (PKT_ERROR_METADEX -567); - - // Does the sender have any tokens? - if (0 >= nNewValue) return (PKT_ERROR_METADEX -3); + if (getMPbalance(sender, property, BALANCE) < static_cast(nNewValue)) return (PKT_ERROR_METADEX -567); rc = MetaDEx_ADD(sender, property, nNewValue, block, desired_property, desired_value, txid, tx_idx); break; + } case CANCEL_AT_PRICE: - // ensure the 4 necessary parameters for this command are provided - // TODO - // ... + { + if (!isTransactionTypeAllowed(block, property, type, version)) return (PKT_ERROR_METADEX -890); + + // ensure we are not trading same property for itself + if (property == desired_property) return (PKT_ERROR_METADEX -5); + + // ensure no cross-over of properties from test to main ecosystem + if (isTestEcosystemProperty(property) != isTestEcosystemProperty(desired_property)) return (PKT_ERROR_METADEX -4); + + // ensure the desired property exists in our universe + if (!_my_sps->hasSP(desired_property)) return (PKT_ERROR_METADEX -30); + + // ensure offered and desired values are positive + if (0 >= static_cast(nNewValue)) return (PKT_ERROR_METADEX -11); + if (0 >= static_cast(desired_value)) return (PKT_ERROR_METADEX -12); + rc = MetaDEx_CANCEL_AT_PRICE(txid, block, sender, property, nNewValue, desired_property, desired_value); break; + } case CANCEL_ALL_FOR_PAIR: - // ensure the 2 necessary parameters for this command are provided - // TODO - // ... + { + if (!isTransactionTypeAllowed(block, property, type, version)) return (PKT_ERROR_METADEX -891); + + // ensure we are not trading same property for itself + if (property == desired_property) return (PKT_ERROR_METADEX -5); + + // ensure no cross-over of properties from test to main ecosystem + if (isTestEcosystemProperty(property) != isTestEcosystemProperty(desired_property)) return (PKT_ERROR_METADEX -4); + + // ensure the desired property exists in our universe + if (!_my_sps->hasSP(desired_property)) return (PKT_ERROR_METADEX -30); + rc = MetaDEx_CANCEL_ALL_FOR_PAIR(txid, block, sender, property, desired_property); break; + } case CANCEL_EVERYTHING: - rc = MetaDEx_CANCEL_EVERYTHING(txid, block, sender); + { + // cancel all open orders, if offered and desired properties are within the same ecosystem, + // otherwise cancel all open orders for all properties of both ecosystems + unsigned char ecosystem = 0; + if (isMainEcosystemProperty(property) && isMainEcosystemProperty(desired_property)) ecosystem = OMNI_PROPERTY_MSC; + if (isTestEcosystemProperty(property) && isTestEcosystemProperty(desired_property)) ecosystem = OMNI_PROPERTY_TMSC; + + if (!isTransactionTypeAllowed(block, ecosystem, type, version, true)) return (PKT_ERROR_METADEX -892); + + rc = MetaDEx_CANCEL_EVERYTHING(txid, block, sender, ecosystem); break; + } default: return (PKT_ERROR_METADEX -999); From 5e5be38efd1fcbfd34ad0af1d5ae1bafd739eb08 Mon Sep 17 00:00:00 2001 From: dexX7 Date: Thu, 9 Apr 2015 10:52:11 +0200 Subject: [PATCH 3/3] [Meta DEx] fix initializer list of CMPMetaDEx, CMPOffer CMPOffer: - subaction was undefined CMPMetaDex: - still_left_for_sale is already in initializer list - addr.empty() should be addr.clear() (addr.empty() is faulty here) - std::string has default constructor - therefore addr.empty() was removed - other constructor should use initializer list --- src/mastercore_dex.cpp | 6 ------ src/mastercore_dex.h | 28 +++++++++++++++++----------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/mastercore_dex.cpp b/src/mastercore_dex.cpp index 65a481ff188c9..be659db2d13f7 100644 --- a/src/mastercore_dex.cpp +++ b/src/mastercore_dex.cpp @@ -346,12 +346,6 @@ void CMPMetaDEx::Set(const string &sa, int b, unsigned int c, uint64_t nValue, u subaction = suba; } -CMPMetaDEx::CMPMetaDEx(const string &addr, int b, unsigned int c, uint64_t nValue, unsigned int cd, uint64_t ad, const uint256 &tx, unsigned int i, unsigned char suba, uint64_t lfors) -{ - still_left_forsale = lfors; - Set(addr, b,c,nValue,cd,ad,tx,i,suba); -} - std::string CMPMetaDEx::ToString() const { return strprintf("%s:%34s in %d/%03u, txid: %s , trade #%u %s for #%u %s", diff --git a/src/mastercore_dex.h b/src/mastercore_dex.h index 9f194543c6f30..33ef99f567a59 100644 --- a/src/mastercore_dex.h +++ b/src/mastercore_dex.h @@ -61,12 +61,15 @@ class CMPOffer uint64_t getOfferAmountOriginal() const { return offer_amount_original; } uint64_t getBTCDesiredOriginal() const { return BTC_desired_original; } - CMPOffer():offerBlock(0),offer_amount_original(0),property(0),BTC_desired_original(0),min_fee(0),blocktimelimit(0),txid(0) + CMPOffer() + : offerBlock(0), offer_amount_original(0), property(0), BTC_desired_original(0), min_fee(0), blocktimelimit(0), + txid(0), subaction(0) { } - CMPOffer(int b, uint64_t a, unsigned int cu, uint64_t d, uint64_t fee, unsigned char btl, const uint256 &tx) - :offerBlock(b),offer_amount_original(a),property(cu),BTC_desired_original(d),min_fee(fee),blocktimelimit(btl),txid(tx) + CMPOffer(int b, uint64_t a, unsigned int cu, uint64_t d, uint64_t fee, unsigned char btl, const uint256& tx) + : offerBlock(b), offer_amount_original(a), property(cu), BTC_desired_original(d), min_fee(fee), blocktimelimit(btl), + txid(tx), subaction(0) { if (msc_debug_dex) file_log("%s(%lu): %s , line %d, file: %s\n", __FUNCTION__, a, txid.GetHex(), __LINE__, __FILE__); } @@ -203,12 +206,9 @@ class CMPMetaDEx uint64_t amount_forsale; // the amount for sale specified when the offer was placed unsigned int desired_property; int64_t amount_desired; - uint64_t still_left_forsale; - unsigned char subaction; - - string addr; + std::string addr; public: uint256 getHash() const { return txid; } @@ -246,13 +246,19 @@ class CMPMetaDEx } // needed only by the RPC functions - CMPMetaDEx():block(0),txid(0),idx(0),property(0),amount_forsale(0),desired_property(0),amount_desired(0),still_left_forsale(0),subaction(0) + // needed only by the RPC functions + CMPMetaDEx() + : block(0), txid(0), idx(0), property(0), amount_forsale(0), desired_property(0), amount_desired(0), + still_left_forsale(0), subaction(0) { - still_left_forsale = 0; - addr.empty(); } - CMPMetaDEx(const string &, int, unsigned int, uint64_t, unsigned int, uint64_t, const uint256 &, unsigned int, unsigned char, uint64_t lfors = 0); + CMPMetaDEx(const std::string& addr, int b, unsigned int c, uint64_t nValue, unsigned int cd, uint64_t ad, + const uint256& tx, unsigned int i, unsigned char suba, uint64_t lfors = 0) + : block(b), txid(tx), idx(i), property(c), amount_forsale(nValue), desired_property(cd), amount_desired(ad), + still_left_forsale(lfors), subaction(suba), addr(addr) + { + } void Set(const string &, int, unsigned int, uint64_t, unsigned int, uint64_t, const uint256 &, unsigned int, unsigned char);