Skip to content

Commit

Permalink
leave only general storage code
Browse files Browse the repository at this point in the history
  • Loading branch information
oxarbitrage committed Sep 10, 2019
1 parent 8423376 commit e4f0d76
Show file tree
Hide file tree
Showing 14 changed files with 325 additions and 1,481 deletions.
51 changes: 0 additions & 51 deletions libraries/app/api.cpp
Expand Up @@ -687,57 +687,6 @@ namespace graphene { namespace app {
}

// custom operations api
optional<account_contact_object> custom_operations_api::get_contact_info(std::string account_id_or_name)const
{
const auto account_id = database_api.get_account_id_from_string(account_id_or_name);
auto &index = _app.chain_database()->get_index_type<account_contact_index>().indices().get<by_custom_account>();
auto itr = index.find(account_id);
if(itr != index.end())
return *itr;
return optional<account_contact_object>();
}

vector<htlc_order_object> custom_operations_api::get_account_htlc_offers(std::string account_id_or_name,
htlc_order_id_type start, uint32_t limit)const
{
FC_ASSERT(limit <= 101);

const auto account_id = database_api.get_account_id_from_string(account_id_or_name);
vector<htlc_order_object> results;
auto &index = _app.chain_database()->get_index_type<htlc_orderbook_index>().indices().get<by_bitshares_account>();

auto itr = index.lower_bound(boost::make_tuple(account_id, start));
while(itr != index.end() && itr->bitshares_account == account_id && results.size() < limit)
{
results.push_back(*itr);
++itr;
}
return results;
}
vector<htlc_order_object> custom_operations_api::get_active_htlc_offers(htlc_order_id_type start, uint32_t limit)const
{
FC_ASSERT(limit <= 101);

vector<htlc_order_object> results;
auto db = _app.chain_database();
auto &index = db->get_index_type<htlc_orderbook_index>().indices().get<by_active>();
auto itr = index.lower_bound(make_tuple(true, db->head_block_time(), start));
while(itr != index.end() && itr->active && itr->expiration > db->head_block_time() && results.size() < limit)
{
results.push_back(*itr);
++itr;
}
return results;
}
optional<htlc_order_object> custom_operations_api::get_htlc_offer(htlc_order_id_type id)const
{
auto &index = _app.chain_database()->get_index_type<htlc_orderbook_index>().indices().get<by_custom_id>();
auto itr = index.find(id);
if(itr != index.end())
return *itr;
return optional<htlc_order_object>();
}

vector<account_storage_object> custom_operations_api::get_storage_info(std::string account_id_or_name,
std::string catalog)const
{
Expand Down
46 changes: 1 addition & 45 deletions libraries/app/include/graphene/app/api.hpp
Expand Up @@ -531,51 +531,11 @@ namespace graphene { namespace app {
custom_operations_api(application& app):_app(app), database_api( std::ref(*app.chain_database()),
&(app.get_options()) ){}

/**
* @breif Get contact information of an account
*
* @param account Account name to get info from
*
* @return The contact information of the account or empty
*/
optional<account_contact_object> get_contact_info(std::string account)const;

/**
* @breif Get htlc offers from an account
*
* @param account Account name to get htlc offers from
* @param start ID of the most recent htlc offer to retrieve
* @param limit Maximum number of order objects to retrieve
*
* @return A vector of htlc offer objects from the account
*/
vector<htlc_order_object> get_account_htlc_offers(std::string account, htlc_order_id_type start,
uint32_t limit)const;

/**
* @breif Get all active and non expired htlc offers
*
* @param start ID of the most recent htlc offer to retrieve
* @param limit Maximum number of order objects to retrieve
*
* @return A vector of active and non expired htlc offers
*/
vector<htlc_order_object> get_active_htlc_offers(htlc_order_id_type start, uint32_t limit)const;

/**
* @breif Get htlc order offer by id
*
* @param id ID of the htlc order offer to retrieve
*
* @return A vector of active and non expired htlc offers
*/
optional<htlc_order_object> get_htlc_offer(htlc_order_id_type id)const;

/**
* @breif Get all stored objects of an account in a particular catalog
*
* @param account Account name to get info from
* @param catalog Category classification
* @param catalog Category classification. Each account can store multiple catalogs.
*
* @return The vector of objects of the account or empty
*/
Expand Down Expand Up @@ -722,10 +682,6 @@ FC_API(graphene::app::orders_api,
(get_grouped_limit_orders)
)
FC_API(graphene::app::custom_operations_api,
(get_contact_info)
(get_account_htlc_offers)
(get_active_htlc_offers)
(get_htlc_offer)
(get_storage_info)
)
FC_API(graphene::app::login_api,
Expand Down
150 changes: 52 additions & 98 deletions libraries/plugins/custom_operations/custom_evaluators.cpp
Expand Up @@ -35,144 +35,98 @@ custom_generic_evaluator::custom_generic_evaluator(database& db, const account_i
_account = account;
}

void fill_contact_object(account_contact_object& aco, account_id_type account, const account_contact_operation& op)
{
aco.account = account;
if(op.extensions.value.name.valid()) aco.name = *op.extensions.value.name;
if(op.extensions.value.email.valid()) aco.email = *op.extensions.value.email;
if(op.extensions.value.phone.valid()) aco.phone = *op.extensions.value.phone;
if(op.extensions.value.address.valid()) aco.address = *op.extensions.value.address;
if(op.extensions.value.company.valid()) aco.company = *op.extensions.value.company;
if(op.extensions.value.url.valid()) aco.url = *op.extensions.value.url;
}

object_id_type custom_generic_evaluator::do_apply(const account_contact_operation& op)
{
auto &index = _db->get_index_type<account_contact_index>().indices().get<by_custom_account>();

auto itr = index.find(_account);
if( itr != index.end() )
{
_db->modify( *itr, [&op, this]( account_contact_object& aco ){
fill_contact_object(aco, _account, op);
});
return itr->id;
}
else
{
auto created = _db->create<account_contact_object>( [&op, this]( account_contact_object& aco ) {
fill_contact_object(aco, _account, op);
});
return created.id;
}
}

object_id_type custom_generic_evaluator::do_apply(const create_htlc_order_operation& op)
{
FC_ASSERT(*op.extensions.value.expiration > _db->head_block_time() + fc::seconds(3600));

auto order_time = _db->head_block_time();
auto created = _db->create<htlc_order_object>( [&op, &order_time, this]( htlc_order_object& hoo ) {
hoo.bitshares_account = _account;
if(op.extensions.value.bitshares_amount.valid()) hoo.bitshares_amount = *op.extensions.value.bitshares_amount;
if(op.extensions.value.blockchain.valid()) hoo.blockchain = *op.extensions.value.blockchain;
if(op.extensions.value.blockchain_account.valid()) hoo.blockchain_account = *op.extensions.value.blockchain_account;
if(op.extensions.value.blockchain_asset.valid()) hoo.blockchain_asset = *op.extensions.value.blockchain_asset;
if(op.extensions.value.blockchain_asset_precision.valid()) hoo.blockchain_asset_precision =
*op.extensions.value.blockchain_asset_precision;
if(op.extensions.value.blockchain_amount.valid()) hoo.blockchain_amount = *op.extensions.value.blockchain_amount;
if(op.extensions.value.expiration.valid()) hoo.expiration = *op.extensions.value.expiration;
if(op.extensions.value.token_contract.valid()) hoo.token_contract = *op.extensions.value.token_contract;
if(op.extensions.value.tag.valid()) hoo.tag = *op.extensions.value.tag;

hoo.order_time = order_time;
hoo.active = true;
});
return created.id;
}

object_id_type custom_generic_evaluator::do_apply(const take_htlc_order_operation& op)
{
auto &index = _db->get_index_type<htlc_orderbook_index>().indices().get<by_custom_id>();
htlc_order_id_type htlc_order_id;

if(op.extensions.value.htlc_order_id.valid()) {
htlc_order_id = *op.extensions.value.htlc_order_id;
auto itr = index.find(htlc_order_id);
if (itr != index.end()) {
auto close_time = _db->head_block_time();
_db->modify(*itr, [&op, &close_time, this](htlc_order_object &hoo) {
hoo.active = false;
hoo.taker_bitshares_account = _account;
if (op.extensions.value.blockchain_account.valid())
hoo.taker_blockchain_account = op.extensions.value.blockchain_account;
hoo.close_time = close_time;
});
}
}
return htlc_order_id;
}

object_id_type custom_generic_evaluator::do_apply(const account_storage_map& op)
vector<object_id_type> custom_generic_evaluator::do_apply(const account_storage_map& op)
{
auto &index = _db->get_index_type<account_storage_index>().indices().get<by_account_catalog_key>();
vector<object_id_type> results;

if (op.extensions.value.remove.valid() && *op.extensions.value.remove)
{
for(auto const& row: *op.extensions.value.key_values) {
auto itr = index.find(make_tuple(_account, *op.extensions.value.catalog, row.first));
if(itr != index.end())
if(itr != index.end()) {
results.push_back(itr->id);
_db->remove(*itr);
}
}
}
else {
for(auto const& row: *op.extensions.value.key_values) {
if(row.first.length() > CUSTOM_OPERATIONS_MAX_KEY_SIZE)
{
wlog("Key can't be bigger than ${max} characters", ("max", CUSTOM_OPERATIONS_MAX_KEY_SIZE));
continue;
}
auto itr = index.find(make_tuple(_account, *op.extensions.value.catalog, row.first));
if(itr == index.end())
{
auto created = _db->create<account_storage_object>( [&op, this, &row]( account_storage_object& aso ) {
aso.catalog = *op.extensions.value.catalog;
aso.account = _account;
aso.key = row.first;
aso.value = row.second;
});
try {
auto created = _db->create<account_storage_object>( [&op, this, &row]( account_storage_object& aso ) {
aso.catalog = *op.extensions.value.catalog;
aso.account = _account;
aso.key = row.first;
aso.value = fc::json::from_string(row.second);
});
results.push_back(created.id);
}
catch(const fc::parse_error_exception& e) { wdump((e.to_detail_string())); }
catch(const fc::assert_exception& e) { wdump((e.to_detail_string())); }
}
else
{
_db->modify(*itr, [&op, this, &row](account_storage_object &aso) {
aso.value = row.second;
});
try {
_db->modify(*itr, [&op, this, &row](account_storage_object &aso) {
aso.value = fc::json::from_string(row.second);
});
results.push_back(itr->id);
}
catch(const fc::parse_error_exception& e) { wdump((e.to_detail_string())); }
catch(const fc::assert_exception& e) { wdump((e.to_detail_string())); }
}
}
}
return results;
}
object_id_type custom_generic_evaluator::do_apply(const account_storage_list& op)
vector<object_id_type> custom_generic_evaluator::do_apply(const account_storage_list& op)
{
auto &index = _db->get_index_type<account_storage_index>().indices().get<by_account_catalog_value>();
auto &index = _db->get_index_type<account_storage_index>().indices().get<by_account_catalog_subkey>();
vector<object_id_type> results;

if (op.extensions.value.remove.valid() && *op.extensions.value.remove)
{
for(auto const& list_value: *op.extensions.value.values) {

auto itr = index.find(make_tuple(_account, *op.extensions.value.catalog, list_value));
if(itr != index.end())
if(itr != index.end()) {
results.push_back(itr->id);
_db->remove(*itr);
}
}
}
else {

for(auto const& list_value: *op.extensions.value.values) {
if(list_value.length() > 200)
{
wlog("List value can't be bigger than ${max} characters", ("max", CUSTOM_OPERATIONS_MAX_KEY_SIZE));
continue;
}
auto itr = index.find(make_tuple(_account, *op.extensions.value.catalog, list_value));
if(itr == index.end())
{
auto created = _db->create<account_storage_object>( [&op, this, &list_value]( account_storage_object& aso ) {
aso.catalog = *op.extensions.value.catalog;
aso.account = _account;
aso.value = list_value;
});
try {
auto created = _db->create<account_storage_object>(
[&op, this, &list_value](account_storage_object &aso) {
aso.catalog = *op.extensions.value.catalog;
aso.account = _account;
aso.subkey = list_value;
});
results.push_back(itr->id);
}
catch(const fc::assert_exception& e) { wdump((e.to_detail_string())); }
}
}
}
return results;
}

} }
26 changes: 2 additions & 24 deletions libraries/plugins/custom_operations/custom_operations.cpp
Expand Up @@ -25,44 +25,22 @@

namespace graphene { namespace custom_operations {

void account_contact_operation::validate()const
{
}

void create_htlc_order_operation::validate()const
{
FC_ASSERT(extensions.value.bitshares_amount.valid());
FC_ASSERT(extensions.value.bitshares_amount->amount.value > 0);
FC_ASSERT(extensions.value.blockchain.valid());
FC_ASSERT(extensions.value.blockchain_account.valid());
FC_ASSERT(extensions.value.blockchain_asset.valid());
FC_ASSERT(extensions.value.blockchain_amount.valid());
FC_ASSERT(extensions.value.expiration.valid());
}

void take_htlc_order_operation::validate()const
{
FC_ASSERT(extensions.value.blockchain_account.valid());
FC_ASSERT(extensions.value.htlc_order_id.valid());
}

void account_storage_map::validate()const
{
FC_ASSERT(extensions.value.catalog.valid());
FC_ASSERT(extensions.value.key_values.valid());
FC_ASSERT(extensions.value.key_values->size() <= 10);
FC_ASSERT(extensions.value.catalog->length() <= CUSTOM_OPERATIONS_MAX_KEY_SIZE);
}
void account_storage_list::validate()const
{
FC_ASSERT(extensions.value.catalog.valid());
FC_ASSERT(extensions.value.values.valid());
FC_ASSERT(extensions.value.values->size() <= 10);
FC_ASSERT(extensions.value.catalog->length() <= CUSTOM_OPERATIONS_MAX_KEY_SIZE);
}

} } //graphene::custom_operations

GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::custom_operations::account_contact_operation )
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::custom_operations::create_htlc_order_operation )
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::custom_operations::take_htlc_order_operation )
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::custom_operations::account_storage_map )
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::custom_operations::account_storage_list )
Expand Up @@ -113,8 +113,6 @@ void custom_operations_plugin::plugin_set_program_options(

void custom_operations_plugin::plugin_initialize(const boost::program_options::variables_map& options)
{
database().add_index< primary_index< account_contact_index > >();
database().add_index< primary_index< htlc_orderbook_index > >();
database().add_index< primary_index< account_storage_index > >();

database().applied_block.connect( [this]( const signed_block& b) {
Expand Down
Expand Up @@ -34,11 +34,8 @@ class custom_generic_evaluator
account_id_type _account;
custom_generic_evaluator(database& db, const account_id_type account);

object_id_type do_apply(const account_contact_operation& o);
object_id_type do_apply(const create_htlc_order_operation& o);
object_id_type do_apply(const take_htlc_order_operation& o);
object_id_type do_apply(const account_storage_map& o);
object_id_type do_apply(const account_storage_list& o);
vector<object_id_type> do_apply(const account_storage_map& o);
vector<object_id_type> do_apply(const account_storage_list& o);
};

} }
Expand Down

0 comments on commit e4f0d76

Please sign in to comment.