Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #782: Change hard-coded limitations in API's to configurable #1513

Merged
merged 26 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cb5250f
Merge pull request #1 from bitshares/develop
manikey123 Dec 20, 2018
a4afb7c
Merge pull request #2 from bitshares/develop
manikey123 Jan 4, 2019
a22e850
added test case for 782 part1
manikey123 Jan 4, 2019
b4195aa
Update history_api_tests.cpp
manikey123 Jan 8, 2019
1829a9a
updated based on review comment received
manikey123 Jan 9, 2019
d66f15b
updated based on review comment received1
manikey123 Jan 9, 2019
ab7b44a
Update history_api_tests.cpp
manikey123 Jan 9, 2019
c3db2db
Update application.cpp
manikey123 Jan 9, 2019
43a743b
updating uint64_t
manikey123 Jan 9, 2019
9a9a8f8
782 pt 1 and 2
manikey123 Jan 13, 2019
3aecf1f
782 pt 1 and 2 edit
manikey123 Jan 13, 2019
a724f9d
Merge pull request #3 from bitshares/develop
manikey123 Jan 15, 2019
3066629
782 chnages based on review comments
manikey123 Jan 15, 2019
9d6b06f
782 changes based on travis build and review comments
manikey123 Jan 15, 2019
884a6b4
782 changes based on travis build and review comments
manikey123 Jan 15, 2019
f6f6cde
Update group_order_api_tests.cpp
manikey123 Jan 15, 2019
f85b46d
Update api.hpp
manikey123 Jan 15, 2019
8d59ea3
782 changes based on get_key_references
manikey123 Jan 15, 2019
f928f90
Update application.hpp
manikey123 Jan 15, 2019
097a8e6
changes for 782 to correct spacing issue
manikey123 Jan 15, 2019
782bbba
Merge branch '782_test_p1' of https://github.com/manikey123/bitshares…
manikey123 Jan 15, 2019
ba3a895
Update database_api_tests.cpp
manikey123 Jan 15, 2019
a4108e0
Update as per the review comments 1513
manikey123 Jan 15, 2019
3014c38
Merge branch 'develop' into 782_test_p1
manikey123 Mar 18, 2019
df514ac
Update asset_api_tests.cpp
manikey123 Mar 18, 2019
78cb2c7
updated api.cpp for build failures
manikey123 Mar 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions libraries/app/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ namespace graphene { namespace app {
}
else if( api_name == "asset_api" )
{
_asset_api = std::make_shared< asset_api >( std::ref( *_app.chain_database() ) );
_asset_api = std::make_shared< asset_api >( std::ref( _app ) );
}
else if( api_name == "orders_api" )
{
Expand Down Expand Up @@ -316,7 +316,8 @@ namespace graphene { namespace app {
{
FC_ASSERT( _app.chain_database() );
const auto& db = *_app.chain_database();
FC_ASSERT( limit <= 100 );
uint64_t api_limit_get_account_history=_app.get_options().api_limit_get_account_history;
FC_ASSERT( limit <= api_limit_get_account_history );
vector<operation_history_object> result;
account_id_type account;
try {
Expand Down Expand Up @@ -352,7 +353,8 @@ namespace graphene { namespace app {
{
FC_ASSERT( _app.chain_database() );
const auto& db = *_app.chain_database();
FC_ASSERT( limit <= 100 );
uint64_t api_limit_get_account_history_operations=_app.get_options().api_limit_get_account_history_operations;
FC_ASSERT(limit <= api_limit_get_account_history_operations);
vector<operation_history_object> result;
account_id_type account;
try {
Expand Down Expand Up @@ -391,7 +393,8 @@ namespace graphene { namespace app {
{
FC_ASSERT( _app.chain_database() );
const auto& db = *_app.chain_database();
FC_ASSERT(limit <= 100);
uint64_t api_limit_get_relative_account_history=_app.get_options().api_limit_get_relative_account_history;
FC_ASSERT(limit <= api_limit_get_relative_account_history);
vector<operation_history_object> result;
account_id_type account;
try {
Expand Down Expand Up @@ -430,7 +433,8 @@ namespace graphene { namespace app {

history_operation_detail history_api::get_account_history_by_operations(const std::string account_id_or_name, vector<uint16_t> operation_types, uint32_t start, unsigned limit)
{
FC_ASSERT(limit <= 100);
uint64_t api_limit_get_account_history_by_operations=_app.get_options().api_limit_get_account_history_by_operations;
FC_ASSERT(limit <= api_limit_get_account_history_by_operations);
history_operation_detail result;
vector<operation_history_object> objs = get_relative_account_history(account_id_or_name, start, limit, limit + start - 1);
std::for_each(objs.begin(), objs.end(), [&](const operation_history_object &o) {
Expand Down Expand Up @@ -526,13 +530,14 @@ namespace graphene { namespace app {
}

// asset_api
asset_api::asset_api(graphene::chain::database& db) : _db(db) { }
asset_api::~asset_api() { }

vector<account_asset_balance> asset_api::get_asset_holders( asset_id_type asset_id, uint32_t start, uint32_t limit ) const {
FC_ASSERT(limit <= 100);

const auto& bal_idx = _db.get_index_type< account_balance_index >().indices().get< by_asset_balance >();
FC_ASSERT( _app.chain_database() );
const auto& db = *_app.chain_database();
uint64_t api_limit_get_asset_holders=_app.get_options().api_limit_get_asset_holders;

FC_ASSERT(limit <= api_limit_get_asset_holders);
const auto& bal_idx = db.get_index_type< account_balance_index >().indices().get< by_asset_balance >();
auto range = bal_idx.equal_range( boost::make_tuple( asset_id ) );

vector<account_asset_balance> result;
Expand All @@ -549,7 +554,7 @@ namespace graphene { namespace app {
if( index++ < start )
continue;

const auto account = _db.find(bal.owner);
const auto account = db.find(bal.owner);

account_asset_balance aab;
aab.name = account->name;
Expand All @@ -563,8 +568,9 @@ namespace graphene { namespace app {
}
// get number of asset holders.
int asset_api::get_asset_holders_count( asset_id_type asset_id ) const {

const auto& bal_idx = _db.get_index_type< account_balance_index >().indices().get< by_asset_balance >();
FC_ASSERT( _app.chain_database() );
const auto& db = *_app.chain_database();
const auto& bal_idx = db.get_index_type< account_balance_index >().indices().get< by_asset_balance >();
auto range = bal_idx.equal_range( boost::make_tuple( asset_id ) );

int count = boost::distance(range) - 1;
Expand All @@ -573,18 +579,19 @@ namespace graphene { namespace app {
}
// function to get vector of system assets with holders count.
vector<asset_holders> asset_api::get_all_asset_holders() const {

FC_ASSERT( _app.chain_database() );
const auto& db = *_app.chain_database();
vector<asset_holders> result;

vector<asset_id_type> total_assets;
for( const asset_object& asset_obj : _db.get_index_type<asset_index>().indices() )
for( const asset_object& asset_obj : db.get_index_type<asset_index>().indices() )
{
const auto& dasset_obj = asset_obj.dynamic_asset_data_id(_db);
const auto& dasset_obj = asset_obj.dynamic_asset_data_id(db);

asset_id_type asset_id;
asset_id = dasset_obj.id;

const auto& bal_idx = _db.get_index_type< account_balance_index >().indices().get< by_asset_balance >();
const auto& bal_idx = db.get_index_type< account_balance_index >().indices().get< by_asset_balance >();
auto range = bal_idx.equal_range( boost::make_tuple( asset_id ) );

int count = boost::distance(range) - 1;
Expand Down Expand Up @@ -613,7 +620,8 @@ namespace graphene { namespace app {
optional<price> start,
uint32_t limit )const
{
FC_ASSERT( limit <= 101 );
uint64_t api_limit_get_grouped_limit_orders=_app.get_options().api_limit_get_grouped_limit_orders;
FC_ASSERT( limit <= api_limit_get_grouped_limit_orders );
auto plugin = _app.get_plugin<grouped_orders_plugin>( "grouped_orders" );
FC_ASSERT( plugin );
const auto& limit_groups = plugin->limit_order_groups();
Expand Down
49 changes: 49 additions & 0 deletions libraries/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,29 @@ void application_impl::set_dbg_init_key( graphene::chain::genesis_state_type& ge
genesis.initial_witness_candidates[i].block_signing_key = init_pubkey;
}


void application_impl::set_dgb_api_limit_api() {
if (_options->count("api-limit-get-account-history-operations")) {
_app_options.api_limit_get_account_history_operations = _options->at("api-limit-get-account-history-operations").as<uint64_t>();
}
if(_options->count("api-limit-get-account-history")){
_app_options.api_limit_get_account_history = _options->at("api-limit-get-account-history").as<uint64_t>();
}
if(_options->count("api-limit-get-grouped-limit-orders")){
_app_options.api_limit_get_grouped_limit_orders = _options->at("api-limit-get-grouped-limit-orders").as<uint64_t>();
}
if(_options->count("api-limit-get-relative-account-history")){
_app_options.api_limit_get_relative_account_history = _options->at("api-limit-get-relative-account-history").as<uint64_t>();
}
if(_options->count("api-limit-get-account-history-by-operations")){
_app_options.api_limit_get_account_history_by_operations = _options->at("api-limit-get-account-history-by-operations").as<uint64_t>();
}
if(_options->count("api-limit-get-asset-holders")){
_app_options.api_limit_get_asset_holders = _options->at("api-limit-get-asset-holders").as<uint64_t>();
}

}

void application_impl::startup()
{ try {
fc::create_directories(_data_dir / "blockchain");
Expand Down Expand Up @@ -439,6 +462,8 @@ void application_impl::startup()
if ( _options->count("enable-subscribe-to-all") )
_app_options.enable_subscribe_to_all = _options->at( "enable-subscribe-to-all" ).as<bool>();

set_dgb_api_limit_api();

if( _active_plugins.find( "market_history" ) != _active_plugins.end() )
_app_options.has_market_history_plugin = true;

Expand Down Expand Up @@ -982,6 +1007,18 @@ void application::set_program_options(boost::program_options::options_descriptio
("enable-standby-votes-tracking", bpo::value<bool>()->implicit_value(true),
"Whether to enable tracking of votes of standby witnesses and committee members. "
"Set it to true to provide accurate data to API clients, set to false for slightly better performance.")
("api-limit-get-account-history-operations",boost::program_options::value<uint64_t>()->default_value(100),
"For history_api::get_account_history_operations to set its default limit value as 100")
("api-limit-get-account-history",boost::program_options::value<uint64_t>()->default_value(100),
"For history_api::get_account_history to set its default limit value as 100")
("api-limit-get-grouped-limit-orders",boost::program_options::value<uint64_t>()->default_value(101),
"For orders_api::get_grouped_limit_orders to set its default limit value as 101")
("api-limit-get-relative-account-history",boost::program_options::value<uint64_t>()->default_value(100),
"For history_api::get_relative_account_history to set its default limit value as 100")
("api-limit-get-account-history-by-operations",boost::program_options::value<uint64_t>()->default_value(100),
"For history_api::get_account_history_by_operations to set its default limit value as 100")
("api-limit-get-asset-holders",boost::program_options::value<uint64_t>()->default_value(100),
"For asset_api::get_asset_holders to set its default limit value as 100")
;
command_line_options.add(configuration_file_options);
command_line_options.add_options()
Expand Down Expand Up @@ -1076,6 +1113,18 @@ void application::startup()
throw;
}
}
void application::set_dgb_api_limit_api()
{
try {
my->set_dgb_api_limit_api();
} catch ( const fc::exception& e ) {
elog( "${e}", ("e",e.to_detail_string()) );
throw;
} catch ( ... ) {
elog( "unexpected exception" );
throw;
}
}

std::shared_ptr<abstract_plugin> application::get_plugin(const string& name) const
{
Expand Down
1 change: 1 addition & 0 deletions libraries/app/application_impl.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class application_impl : public net::node_delegate
}

void set_dbg_init_key( graphene::chain::genesis_state_type& genesis, const std::string& init_key );
void set_dgb_api_limit_api();
pmconrad marked this conversation as resolved.
Show resolved Hide resolved

void startup();

Expand Down
7 changes: 4 additions & 3 deletions libraries/app/include/graphene/app/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ namespace graphene { namespace app {
class asset_api
{
public:
asset_api(graphene::chain::database& db);
~asset_api();
asset_api(application& app)
:_app(app), database_api( std::ref(*app.chain_database()), &(app.get_options())) {}

/**
* @brief Get asset holders for a specific asset
Expand All @@ -464,7 +464,8 @@ namespace graphene { namespace app {
vector<asset_holders> get_all_asset_holders() const;

private:
graphene::chain::database& _db;
pmconrad marked this conversation as resolved.
Show resolved Hide resolved
application& _app;
graphene::app::database_api database_api;
};

/**
Expand Down
8 changes: 7 additions & 1 deletion libraries/app/include/graphene/app/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ namespace graphene { namespace app {
public:
bool enable_subscribe_to_all = false;
bool has_market_history_plugin = false;
uint64_t api_limit_get_account_history_operations=100;
uint64_t api_limit_get_account_history=100;
uint64_t api_limit_get_grouped_limit_orders=101;
uint64_t api_limit_get_relative_account_history=100;
uint64_t api_limit_get_account_history_by_operations=100;
uint64_t api_limit_get_asset_holders=100;
};

class application
Expand Down Expand Up @@ -87,7 +93,7 @@ namespace graphene { namespace app {

net::node_ptr p2p_node();
std::shared_ptr<chain::database> chain_database()const;

void set_dgb_api_limit_api();
void set_block_production(bool producing_blocks);
fc::optional< api_access_info > get_api_access_info( const string& username )const;
void set_api_access_info(const string& username, api_access_info&& permissions);
Expand Down
41 changes: 41 additions & 0 deletions tests/common/database_fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,40 @@ database_fixture::database_fixture()
{
options.insert(std::make_pair("max-ops-per-account", boost::program_options::variable_value((uint64_t)75, false)));
}
if (current_test_name == "api_limit_get_account_history_operations")
{
options.insert(std::make_pair("max-ops-per-account", boost::program_options::variable_value((uint64_t)125, false)));
options.insert(std::make_pair("api-limit-get-account-history-operations", boost::program_options::variable_value((uint64_t)300, false)));
options.insert(std::make_pair("plugins", boost::program_options::variable_value(string("account_history"), false)));
}
if(current_test_name =="api_limit_get_account_history")
{
options.insert(std::make_pair("max-ops-per-account", boost::program_options::variable_value((uint64_t)125, false)));
options.insert(std::make_pair("api-limit-get-account-history", boost::program_options::variable_value((uint64_t)250, false)));
options.insert(std::make_pair("plugins", boost::program_options::variable_value(string("account_history"), false)));
}
if(current_test_name =="api_limit_get_grouped_limit_orders")
{
options.insert(std::make_pair("api-limit-get-grouped-limit-orders", boost::program_options::variable_value((uint64_t)250, false)));
options.insert(std::make_pair("plugins", boost::program_options::variable_value(string("grouped_orders"), false)));
}
if(current_test_name =="api_limit_get_relative_account_history")
{
options.insert(std::make_pair("max-ops-per-account", boost::program_options::variable_value((uint64_t)125, false)));
options.insert(std::make_pair("api-limit-get-relative-account-history", boost::program_options::variable_value((uint64_t)250, false)));
options.insert(std::make_pair("plugins", boost::program_options::variable_value(string("account_history"), false)));
}
if(current_test_name =="api_limit_get_account_history_by_operations")
{
options.insert(std::make_pair("api-limit-get-account-history-by-operations", boost::program_options::variable_value((uint64_t)250, false)));
options.insert(std::make_pair("plugins", boost::program_options::variable_value(string("account_history"), false)));
}
if(current_test_name =="api_limit_get_asset_holders")
{
options.insert(std::make_pair("api-limit-get-asset-holders", boost::program_options::variable_value((uint64_t)250, false)));
options.insert(std::make_pair("plugins", boost::program_options::variable_value(string("account_history"), false)));
}

pmconrad marked this conversation as resolved.
Show resolved Hide resolved
// add account tracking for ahplugin for special test case with track-account enabled
if( !options.count("track-account") && current_test_name == "track_account") {
std::vector<std::string> track_account;
Expand Down Expand Up @@ -161,6 +195,13 @@ database_fixture::database_fixture()
ahplugin->plugin_set_app(&app);
ahplugin->plugin_initialize(options);
ahplugin->plugin_startup();
if (current_test_name == "api_limit_get_account_history_operations" || current_test_name == "api_limit_get_account_history"
|| current_test_name == "api_limit_get_grouped_limit_orders" || current_test_name == "api_limit_get_relative_account_history"
|| current_test_name == "api_limit_get_account_history_by_operations" || current_test_name =="api_limit_get_asset_holders")
{
app.initialize(graphene::utilities::temp_directory_path(), options);
app.set_dgb_api_limit_api();
}
}

if(current_test_name == "elasticsearch_objects" || current_test_name == "elasticsearch_suite") {
Expand Down
22 changes: 21 additions & 1 deletion tests/tests/asset_api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ BOOST_FIXTURE_TEST_SUITE(asset_api_tests, database_fixture)

BOOST_AUTO_TEST_CASE( asset_holders )
{
graphene::app::asset_api asset_api(db);
graphene::app::asset_api asset_api(app);

// create an asset and some accounts
create_bitasset("USD", account_id_type());
Expand All @@ -60,5 +60,25 @@ BOOST_AUTO_TEST_CASE( asset_holders )
BOOST_CHECK(holders[2].name == "alice");
BOOST_CHECK(holders[3].name == "dan");
}
BOOST_AUTO_TEST_CASE( api_limit_get_asset_holders )
{
graphene::app::asset_api asset_api(app);

// create an asset and some accounts
create_bitasset("USD", account_id_type());
auto dan = create_account("dan");
auto bob = create_account("bob");
auto alice = create_account("alice");

// send them some bts
transfer(account_id_type()(db), dan, asset(100));
transfer(account_id_type()(db), alice, asset(200));
transfer(account_id_type()(db), bob, asset(300));

// make call
GRAPHENE_CHECK_THROW(asset_api.get_asset_holders(asset_id_type(), 0, 260), fc::exception)
vector<account_asset_balance> holders = asset_api.get_asset_holders(asset_id_type(), 0, 210);;
BOOST_REQUIRE_EQUAL( holders.size(), 4 );
}

BOOST_AUTO_TEST_SUITE_END()
Loading