Skip to content

Commit

Permalink
Add missing "cancel" checks, "cancel-everything" ecosystem limitation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dexX7 committed Dec 9, 2014
1 parent 2f2f946 commit bea28a1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
10 changes: 7 additions & 3 deletions src/mastercore_dex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -953,9 +953,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__);

Expand All @@ -967,6 +967,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;

Expand All @@ -981,7 +985,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;
Expand Down
2 changes: 1 addition & 1 deletion src/mastercore_dex.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ int DEx_payment(uint256 txid, unsigned int vout, string seller, string buyer, ui
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_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);

Expand Down
70 changes: 51 additions & 19 deletions src/mastercore_tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,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);
Expand All @@ -513,46 +511,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<int64_t>(nNewValue)) return (PKT_ERROR_METADEX -11);
if (0 >= static_cast<int64_t>(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<int64_t>(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<int64_t>(nNewValue)) return (PKT_ERROR_METADEX -11);
if (0 >= static_cast<int64_t>(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);
Expand Down

0 comments on commit bea28a1

Please sign in to comment.