Skip to content

Commit

Permalink
Merge pull request #235 from VanGrx/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
VanGrx authored Sep 22, 2020
2 parents 74f907e + eb243b8 commit 91894c8
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 207 deletions.
2 changes: 1 addition & 1 deletion external/unbound
Submodule unbound updated 1 files
+4 −0 config.h.cmake.in
6 changes: 5 additions & 1 deletion src/blockchain_db/lmdb/db_lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,11 @@ uint64_t BlockchainLMDB::add_advanced_output(const tx_out& tx_output, const uint
okadv.unlock_time = unlock_time;
okadv.output_id = output_id;
okadv.pubkey = txout.key; //todo if there are multiple keys, rest will go to data
okadv.data = blobdata(txout.data.begin(),txout.data.end()); //no need to serialize vector to blob. Just copy it.

if(out_type == tx_out_type::out_staked_token)
okadv.data = std::to_string(tx_output.token_amount);
else
okadv.data = blobdata(txout.data.begin(),txout.data.end()); //no need to serialize vector to blob. Just copy it.

MDB_val_copy<cryptonote::output_advanced_data_t> adv_value(okadv);

Expand Down
177 changes: 149 additions & 28 deletions src/safex/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,28 @@ namespace safex
return execution_status::error_unstake_token_offset_not_one;

uint64_t staked_token_index = txin.key_offsets[0];
const cryptonote::output_advanced_data_t od = blokchainDB.get_output_advanced_data(cryptonote::tx_out_type::out_staked_token, staked_token_index);

try
{
const cryptonote::output_advanced_data_t od = blokchainDB.get_output_advanced_data(cryptonote::tx_out_type::out_staked_token, staked_token_index);
uint64_t token_amount = 0;
epee::string_tools::get_xtype_from_string(token_amount, od.data);

auto toi = blokchainDB.get_output_tx_and_index_from_global(od.output_id);
auto tx = blokchainDB.get_tx(toi.first);
bool output_found = false;
for(auto out: tx.vout)
if(out.target.type() == typeid(cryptonote::txout_to_script) && get_tx_out_type(out.target) == cryptonote::tx_out_type::out_staked_token)
if(out.token_amount == txin.token_amount)
output_found = true;
if(token_amount != txin.token_amount)
return execution_status::error_unstake_token_output_not_found;

uint64_t expected_interest = blokchainDB.calculate_staked_token_interest_for_output(txin, blokchainDB.height());
uint64_t expected_interest = blokchainDB.calculate_staked_token_interest_for_output(txin, blokchainDB.height());

if(txin.amount > expected_interest)
return execution_status::error_unstake_token_network_fee_not_matching;
if(txin.amount > expected_interest)
return execution_status::error_unstake_token_network_fee_not_matching;

if(!output_found)
return execution_status::error_unstake_token_output_not_found;
if(od.height + get_safex_minumum_token_lock_period(blokchainDB.get_net_type()) > blokchainDB.height())
return execution_status::error_unstake_token_minimum_period;
if(od.height + get_safex_minumum_token_lock_period(blokchainDB.get_net_type()) > blokchainDB.height())
return execution_status::error_unstake_token_minimum_period;
}
catch (...)
{
return execution_status::error_unstake_token_output_not_found;
}

return execution_status::ok;
}
Expand Down Expand Up @@ -287,17 +289,39 @@ namespace safex
std::unique_ptr<safex::edit_account> cmd = safex::safex_command_serializer::parse_safex_command<safex::edit_account>(txin.script);


for (auto ch: cmd->get_username()) {
if (!(std::islower(ch) || std::isdigit(ch)) && ch!='_' && ch!='-') {
return execution_status::error_invalid_account_name;
}
}
if(txin.key_offsets.size() != 1)
return execution_status::error_account_offset_not_one;

uint64_t safex_account_index = txin.key_offsets[0];

std::vector<uint8_t> dummy{};
if (!blokchainDB.get_account_data(cmd->get_username(), dummy)) {
return execution_status::error_account_non_existant;
}

try
{
const cryptonote::output_advanced_data_t od = blokchainDB.get_output_advanced_data(cryptonote::tx_out_type::out_safex_account, safex_account_index);

safex::create_account_data account;
const cryptonote::blobdata accblob(std::begin(od.data), std::end(od.data));
cryptonote::parse_and_validate_from_blob(accblob, account);
std::string accusername(begin(account.username), end(account.username));

if(accusername != cmd->get_username())
return execution_status::error_invalid_account_name;
}
catch (...)
{
return execution_status::error_account_non_existant;
}

for (auto ch: cmd->get_username()) {
if (!(std::islower(ch) || std::isdigit(ch)) && ch!='_' && ch!='-') {
return execution_status::error_invalid_account_name;
}
}

if (cmd->get_username().length() > SAFEX_ACCOUNT_USERNAME_MAX_SIZE)
{
return execution_status::error_account_data_too_big;
Expand Down Expand Up @@ -330,20 +354,38 @@ namespace safex

std::unique_ptr<safex::create_offer> cmd = safex::safex_command_serializer::parse_safex_command<safex::create_offer>(txin.script);

if(txin.key_offsets.size() != 1)
return execution_status::error_offer_offset_not_one;

uint64_t safex_account_index = txin.key_offsets[0];

std::vector<uint8_t> dummy{};
if (!blokchainDB.get_account_data(cmd->get_seller(), dummy)) {
return execution_status::error_account_non_existant;
}

try
{
const cryptonote::output_advanced_data_t od = blokchainDB.get_output_advanced_data(cryptonote::tx_out_type::out_safex_account, safex_account_index);

safex::create_account_data account;
const cryptonote::blobdata accblob(std::begin(od.data), std::end(od.data));
cryptonote::parse_and_validate_from_blob(accblob, account);

if(account.username != cmd->get_seller())
return execution_status::error_invalid_account_name;
}
catch (...)
{
return execution_status::error_account_non_existant;
}


safex::safex_offer sfx_offer{};
if (blokchainDB.get_offer(cmd->get_offerid(),sfx_offer)) {
return execution_status::error_offer_already_exists;
}

if(cmd->get_active() == false || cmd->get_quantity() == 0) {
return execution_status::error_wrong_input_params;
}

if(cmd->get_min_sfx_price() < SAFEX_OFFER_MINIMUM_PRICE){
return execution_status::error_offer_price_too_small;
}
Expand Down Expand Up @@ -393,11 +435,37 @@ namespace safex

std::unique_ptr<safex::edit_offer> cmd = safex::safex_command_serializer::parse_safex_command<safex::edit_offer>(txin.script);

if(txin.key_offsets.size() != 1)
return execution_status::error_offer_offset_not_one;

uint64_t safex_offer_index = txin.key_offsets[0];

std::vector<uint8_t> dummy{};
if (!blokchainDB.get_account_data(cmd->get_seller(), dummy)) {
return execution_status::error_account_non_existant;
}

safex::safex_offer sfx_dummy{};
if (!blokchainDB.get_offer(cmd->get_offerid(), sfx_dummy)) {
return execution_status::error_offer_non_existant;
}

try
{
const cryptonote::output_advanced_data_t od = blokchainDB.get_output_advanced_data(cryptonote::tx_out_type::out_safex_offer, safex_offer_index);

safex::create_offer_data offer;
const cryptonote::blobdata offerblob(std::begin(od.data), std::end(od.data));
cryptonote::parse_and_validate_from_blob(offerblob, offer);

if(offer.offer_id != cmd->get_offerid())
return execution_status::error_offer_invalid_offer_id;
}
catch (...)
{
return execution_status::error_account_non_existant;
}

if(cmd->get_min_sfx_price() < SAFEX_OFFER_MINIMUM_PRICE){
return execution_status::error_offer_price_too_small;
}
Expand Down Expand Up @@ -427,10 +495,6 @@ namespace safex
return execution_status::error_offer_price_peg_not_existant;
}

safex::safex_offer sfx_dummy{};
if (!blokchainDB.get_offer(cmd->get_offerid(), sfx_dummy)) {
return execution_status::error_offer_non_existant;
}
return execution_status::ok;
};

Expand All @@ -451,6 +515,22 @@ namespace safex

std::unique_ptr<safex::create_feedback> cmd = safex::safex_command_serializer::parse_safex_command<safex::create_feedback>(txin.script);

if(txin.key_offsets.size() != 1)
return execution_status::error_feedback_offset_not_one;

uint64_t safex_feedback_token_index = txin.key_offsets[0];

try
{
const cryptonote::output_advanced_data_t od = blokchainDB.get_output_advanced_data(cryptonote::tx_out_type::out_safex_feedback_token, safex_feedback_token_index);

}
catch (...)
{
return execution_status::error_feedback_token_non_existant;
}


safex::safex_offer sfx_dummy{};
if (!blokchainDB.get_offer(cmd->get_offerid(), sfx_dummy)) {
return execution_status::error_offer_non_existant;
Expand Down Expand Up @@ -490,6 +570,27 @@ namespace safex
return execution_status::error_account_non_existant;
}

if(txin.key_offsets.size() != 1)
return execution_status::error_price_peg_offset_not_one;

uint64_t safex_account_index = txin.key_offsets[0];

try
{
const cryptonote::output_advanced_data_t od = blokchainDB.get_output_advanced_data(cryptonote::tx_out_type::out_safex_account, safex_account_index);

safex::create_account_data account;
const cryptonote::blobdata accblob(std::begin(od.data), std::end(od.data));
cryptonote::parse_and_validate_from_blob(accblob, account);

if(account.username != cmd->get_creator())
return execution_status::error_invalid_account_name;
}
catch (...)
{
return execution_status::error_account_non_existant;
}

safex::safex_price_peg dummy_price_peg{};
if(blokchainDB.get_safex_price_peg(cmd->get_price_peg_id(),dummy_price_peg))
{
Expand Down Expand Up @@ -544,6 +645,26 @@ namespace safex

std::unique_ptr<safex::update_price_peg> cmd = safex::safex_command_serializer::parse_safex_command<safex::update_price_peg>(txin.script);

if(txin.key_offsets.size() != 1)
return execution_status::error_price_peg_offset_not_one;

uint64_t safex_price_peg_index = txin.key_offsets[0];

try
{
const cryptonote::output_advanced_data_t od = blokchainDB.get_output_advanced_data(cryptonote::tx_out_type::out_safex_price_peg, safex_price_peg_index);

safex::create_price_peg_data price_peg;
const cryptonote::blobdata price_pegblob(std::begin(od.data), std::end(od.data));
cryptonote::parse_and_validate_from_blob(price_pegblob, price_peg);

if(price_peg.price_peg_id != cmd->get_price_peg_id())
return execution_status::error_price_peg_invalid_price_peg_id;
}
catch (...)
{
return execution_status::error_account_non_existant;
}

if(cmd->get_rate() == 0)
{
Expand Down
7 changes: 7 additions & 0 deletions src/safex/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace safex
error_invalid_account_name = 12,
error_account_non_existant = 13,
error_account_no_tokens = 14,
error_account_offset_not_one = 15,
// Safex purchase
error_offer_non_existant = 20,
error_purchase_out_of_stock = 21,
Expand All @@ -62,15 +63,21 @@ namespace safex
error_offer_price_peg_not_existant = 33,
error_offer_price_mismatch = 34,
error_offer_already_exists = 35,
error_offer_invalid_offer_id = 36,
error_offer_offset_not_one = 37,
// Safex feedback
error_feedback_invalid_rating = 40,
error_feedback_data_too_big = 41,
error_feedback_offset_not_one = 42,
error_feedback_token_non_existant = 43,
// Safex price peg
error_price_peg_bad_currency_format = 51,
error_price_peg_data_too_big = 52,
error_price_peg_not_existant = 53,
error_price_peg_rate_zero = 54,
error_price_peg_already_exists = 55,
error_price_peg_offset_not_one = 56,
error_price_peg_invalid_price_peg_id = 57,
// Safex unstake token
error_unstake_token_output_not_found = 60,
error_unstake_token_minimum_period = 61,
Expand Down
3 changes: 3 additions & 0 deletions tests/unit_tests/safex_db/safex_account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ namespace
cryptonote::txin_to_script txinput = AUTO_VAL_INIT(txinput);
txinput.command_type = safex::command_t::edit_account;
txinput.token_amount = 100*SAFEX_TOKEN;
txinput.key_offsets.push_back(0);
std::string username = this->m_safex_account1.username;
std::string description = "Some test data inserted";
safex::edit_account command1{SAFEX_COMMAND_PROTOCOL_VERSION, username, description};
Expand Down Expand Up @@ -861,6 +862,7 @@ namespace
cryptonote::txin_to_script txinput = AUTO_VAL_INIT(txinput);
txinput.command_type = safex::command_t::edit_account;
txinput.token_amount = 100*SAFEX_TOKEN;
txinput.key_offsets.push_back(0);
std::string username = "not_here";
std::string description = "Some test data inserted";
safex::edit_account command1{SAFEX_COMMAND_PROTOCOL_VERSION, username, description};
Expand Down Expand Up @@ -894,6 +896,7 @@ namespace
cryptonote::txin_to_script txinput = AUTO_VAL_INIT(txinput);
txinput.command_type = safex::command_t::edit_account;
txinput.token_amount = 100*SAFEX_TOKEN;
txinput.key_offsets.push_back(0);
std::string username = this->m_safex_account1.username;
std::string description = "";
for(int i=0; i < SAFEX_ACCOUNT_DATA_MAX_SIZE + 1; i++)
Expand Down
Loading

0 comments on commit 91894c8

Please sign in to comment.