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

add list_htlcs database api call #1849

Merged
merged 1 commit into from Jul 24, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -364,6 +364,9 @@ void application_impl::set_api_limit() {
if(_options->count("api-limit-get-order-book")){
_app_options.api_limit_get_order_book = _options->at("api-limit-get-order-book").as<uint64_t>();
}
if(_options->count("api-limit-list-htlcs")){
_app_options.api_limit_list_htlcs = _options->at("api-limit-list-htlcs").as<uint64_t>();
}
}

void application_impl::startup()
@@ -183,6 +183,7 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
optional<htlc_object> get_htlc(htlc_id_type id) const;
vector<htlc_object> get_htlc_by_from(const std::string account_id_or_name, htlc_id_type start, uint32_t limit) const;
vector<htlc_object> get_htlc_by_to(const std::string account_id_or_name, htlc_id_type start, uint32_t limit) const;
vector<htlc_object> list_htlcs(const htlc_id_type lower_bound_id, uint32_t limit) const;

//private:
static string price_to_string( const price& _price, const asset_object& _base, const asset_object& _quote );
@@ -2642,6 +2643,26 @@ vector<htlc_object> database_api_impl::get_htlc_by_to(const std::string account_
return result;
}

vector<htlc_object> database_api::list_htlcs(const htlc_id_type start, uint32_t limit)const
{
return my->list_htlcs(start, limit);
}

vector<htlc_object> database_api_impl::list_htlcs(const htlc_id_type start, uint32_t limit) const
{
FC_ASSERT( limit <= _app_options->api_limit_list_htlcs );

vector<htlc_object> result;
const auto& htlc_idx = _db.get_index_type<htlc_index>().indices().get<by_id>();
auto itr = htlc_idx.lower_bound(start);
while(itr != htlc_idx.end() && result.size() < limit)
{
result.push_back(*itr);
++itr;
}
return result;
}

//////////////////////////////////////////////////////////////////////
// //
// Private methods //
@@ -55,6 +55,7 @@ namespace graphene { namespace app {
uint64_t api_limit_get_assets = 101;
uint64_t api_limit_get_limit_orders = 300;
uint64_t api_limit_get_order_book = 50;
uint64_t api_limit_list_htlcs = 100;
};

class application
@@ -821,6 +821,15 @@ class database_api
*/
vector<htlc_object> get_htlc_by_to(const std::string account_id_or_name, htlc_id_type start, uint32_t limit) const;

/**
* @brief Get all HTLCs
* @param start Lower bound of htlc id to start getting results
* @param limit Maximum number of htlc objects to fetch
* @return The htlc object list
*/
vector<htlc_object> list_htlcs(const htlc_id_type start, uint32_t limit) const;


private:
std::shared_ptr< database_api_impl > my;
};
@@ -953,4 +962,5 @@ FC_API(graphene::app::database_api,
(get_htlc)
(get_htlc_by_from)
(get_htlc_by_to)
(list_htlcs)
)
@@ -978,6 +978,37 @@ try {
full = db_api.get_full_accounts({bob.name}, false);
BOOST_CHECK_EQUAL( full[bob.name].htlcs_to.size(), 1 );

auto list = db_api.list_htlcs(graphene::chain::htlc_id_type(0), 1);
BOOST_CHECK_EQUAL( list.size(), 1 );
BOOST_CHECK_EQUAL( list[0].id.instance(), 0 );

list = db_api.list_htlcs(graphene::chain::htlc_id_type(1), 1);
BOOST_CHECK_EQUAL( list.size(), 1 );
BOOST_CHECK_EQUAL( list[0].id.instance(), 1 );

list = db_api.list_htlcs(graphene::chain::htlc_id_type(2), 1);
BOOST_CHECK_EQUAL( list.size(), 1 );
BOOST_CHECK_EQUAL( list[0].id.instance(), 2 );

list = db_api.list_htlcs(graphene::chain::htlc_id_type(1), 2);
BOOST_CHECK_EQUAL( list.size(), 2 );
BOOST_CHECK_EQUAL( list[0].id.instance(), 1 );
BOOST_CHECK_EQUAL( list[1].id.instance(), 2 );

list = db_api.list_htlcs(graphene::chain::htlc_id_type(1), 3);
BOOST_CHECK_EQUAL( list.size(), 2 );
BOOST_CHECK_EQUAL( list[0].id.instance(), 1 );
BOOST_CHECK_EQUAL( list[1].id.instance(), 2 );

list = db_api.list_htlcs(graphene::chain::htlc_id_type(0), 100);
BOOST_CHECK_EQUAL( list.size(), 3 );
BOOST_CHECK_EQUAL( list[0].id.instance(), 0 );
BOOST_CHECK_EQUAL( list[1].id.instance(), 1 );
BOOST_CHECK_EQUAL( list[2].id.instance(), 2 );

list = db_api.list_htlcs(graphene::chain::htlc_id_type(10), 100);
BOOST_CHECK_EQUAL( list.size(), 0 );

} catch (fc::exception &e) {
edump((e.to_detail_string()));
throw;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.