Skip to content

Commit 6bcaea9

Browse files
committed
Use extended ledger index for receivable RPC when available
Use the account_receivables_by_amount extended ledger index to serve receivable RPC when the extended ledger is available.
1 parent 2ac909e commit 6bcaea9

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

nano/node/json_handler.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <nano/secure/ledger_set_confirmed.hpp>
2323
#include <nano/secure/transaction.hpp>
2424
#include <nano/store/ext_ledger/account_delegators_by_weight.hpp>
25+
#include <nano/store/ext_ledger/account_receivables_by_amount.hpp>
2526
#include <nano/store/ext_ledger_store.hpp>
2627
#include <nano/store/ledger/account.hpp>
2728
#include <nano/store/ledger/confirmation_height.hpp>
@@ -3269,6 +3270,86 @@ void nano::json_handler::receivable ()
32693270
response_errors ();
32703271
}
32713272

3273+
void nano::json_handler::receivable_ext ()
3274+
{
3275+
if (!node.store.ext.is_initialized ())
3276+
{
3277+
ec = nano::error_rpc::requires_ext_ledger;
3278+
}
3279+
3280+
auto account (account_impl ());
3281+
auto count (count_optional_impl ());
3282+
auto offset (offset_optional_impl (0));
3283+
auto threshold (threshold_optional_impl ());
3284+
bool const source = request.get<bool> ("source", false);
3285+
bool const min_version = request.get<bool> ("min_version", false);
3286+
bool const include_active = request.get<bool> ("include_active", false);
3287+
bool const include_only_confirmed = request.get<bool> ("include_only_confirmed", true);
3288+
auto simple (threshold.is_zero () && !source && !min_version); // if simple, response is a list of hashes
3289+
3290+
if (!ec)
3291+
{
3292+
auto transaction = node.ledger.tx_begin_read ();
3293+
boost::property_tree::ptree peers_l;
3294+
3295+
for (auto i = node.store.ext.account_receivables_by_amount.rupper_bound (transaction, account), n = node.store.ext.account_receivables_by_amount.rend (transaction); i != n && peers_l.size () < count; ++i)
3296+
{
3297+
nano::account_receivable_by_amount_key const & key = i->first;
3298+
nano::account_receivable_by_amount_info const & info = i->second;
3299+
3300+
if (key.account != account)
3301+
{
3302+
break;
3303+
}
3304+
3305+
if (key.amount.number () < threshold.number ())
3306+
{
3307+
break;
3308+
}
3309+
3310+
if (!block_confirmed (node, transaction, key.send_block_hash, include_active, include_only_confirmed))
3311+
{
3312+
continue;
3313+
}
3314+
3315+
if (offset > 0)
3316+
{
3317+
--offset;
3318+
continue;
3319+
}
3320+
3321+
if (simple)
3322+
{
3323+
boost::property_tree::ptree entry;
3324+
entry.put ("", key.send_block_hash.to_string ());
3325+
peers_l.push_back (std::make_pair ("", entry));
3326+
}
3327+
else if (source || min_version)
3328+
{
3329+
boost::property_tree::ptree entry;
3330+
entry.put ("amount", key.amount.to_string_dec ());
3331+
if (source)
3332+
{
3333+
entry.put ("source", info.source.to_account ());
3334+
}
3335+
if (min_version)
3336+
{
3337+
entry.put ("min_version", epoch_as_string (info.epoch));
3338+
}
3339+
peers_l.add_child (key.send_block_hash.to_string (), entry);
3340+
}
3341+
else
3342+
{
3343+
peers_l.put (key.send_block_hash.to_string (), key.amount.to_string_dec ());
3344+
}
3345+
}
3346+
3347+
response_l.add_child ("blocks", peers_l);
3348+
}
3349+
3350+
response_errors ();
3351+
}
3352+
32723353
void nano::json_handler::pending_exists ()
32733354
{
32743355
response_l.put ("deprecated", "1");
@@ -5468,6 +5549,7 @@ ipc_json_handler_no_arg_func_map create_ipc_json_handler_no_arg_func_map ()
54685549
no_arg_funcs.emplace ("pending", &nano::json_handler::pending);
54695550
no_arg_funcs.emplace ("pending_exists", &nano::json_handler::pending_exists);
54705551
no_arg_funcs.emplace ("receivable", &nano::json_handler::receivable);
5552+
no_arg_funcs.emplace ("receivable_ext", &nano::json_handler::receivable_ext);
54715553
no_arg_funcs.emplace ("receivable_exists", &nano::json_handler::receivable_exists);
54725554
no_arg_funcs.emplace ("process", &nano::json_handler::process);
54735555
no_arg_funcs.emplace ("pruned_exists", &nano::json_handler::pruned_exists);

nano/node/json_handler.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class json_handler : public std::enable_shared_from_this<nano::json_handler>
9595
void pending ();
9696
void pending_exists ();
9797
void receivable ();
98+
void receivable_ext ();
9899
void receivable_exists ();
99100
void populate_backlog ();
100101
void process ();

0 commit comments

Comments
 (0)