Skip to content

Commit

Permalink
Merge pull request #4711 from clemahieu/revert_receivable_iterator
Browse files Browse the repository at this point in the history
Partially revert receivable iterator
  • Loading branch information
clemahieu authored Aug 22, 2024
2 parents a2f9e18 + fb077a0 commit d84ffb7
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 113 deletions.
205 changes: 104 additions & 101 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1054,34 +1054,35 @@ void nano::json_handler::accounts_receivable ()
if (!ec)
{
boost::property_tree::ptree peers_l;
for (auto current = node.ledger.any.receivable_upper_bound (transaction, account, 0), end = node.ledger.any.receivable_end (); current != end && peers_l.size () < count; ++current)
for (auto i (node.store.pending.begin (transaction, nano::pending_key (account, 0))), n (node.store.pending.end ()); i != n && nano::pending_key (i->first).account == account && peers_l.size () < count; ++i)
{
auto const & [key, info] = *current;
if (include_only_confirmed && !node.ledger.confirmed.block_exists_or_pruned (transaction, key.hash))
nano::pending_key const & key (i->first);
if (block_confirmed (node, transaction, key.hash, include_active, include_only_confirmed))
{
continue;
}
if (simple)
{
boost::property_tree::ptree entry;
entry.put ("", key.hash.to_string ());
peers_l.push_back (std::make_pair ("", entry));
continue;
}
if (info.amount.number () < threshold.number ())
{
continue;
}
if (source)
{
boost::property_tree::ptree pending_tree;
pending_tree.put ("amount", info.amount.number ().template convert_to<std::string> ());
pending_tree.put ("source", info.source.to_account ());
peers_l.add_child (key.hash.to_string (), pending_tree);
}
else
{
peers_l.put (key.hash.to_string (), info.amount.number ().template convert_to<std::string> ());
if (simple)
{
boost::property_tree::ptree entry;
entry.put ("", key.hash.to_string ());
peers_l.push_back (std::make_pair ("", entry));
}
else
{
nano::pending_info const & info (i->second);
if (info.amount.number () >= threshold.number ())
{
if (source)
{
boost::property_tree::ptree pending_tree;
pending_tree.put ("amount", info.amount.number ().convert_to<std::string> ());
pending_tree.put ("source", info.source.to_account ());
peers_l.add_child (key.hash.to_string (), pending_tree);
}
else
{
peers_l.put (key.hash.to_string (), info.amount.number ().convert_to<std::string> ());
}
}
}
}
}
if (sorting && !simple)
Expand Down Expand Up @@ -3109,61 +3110,62 @@ void nano::json_handler::receivable ()
// The ptree container is used if there are any children nodes (e.g source/min_version) otherwise the amount container is used.
std::vector<std::pair<std::string, boost::property_tree::ptree>> hash_ptree_pairs;
std::vector<std::pair<std::string, nano::uint128_t>> hash_amount_pairs;
for (auto current = node.ledger.any.receivable_upper_bound (transaction, account, 0), end = node.ledger.any.receivable_end (); current != end && (should_sort || peers_l.size () < count); ++current)
for (auto i (node.store.pending.begin (transaction, nano::pending_key (account, 0))), n (node.store.pending.end ()); i != n && nano::pending_key (i->first).account == account && (should_sort || peers_l.size () < count); ++i)
{
auto const & [key, info] = *current;
if (include_only_confirmed && !node.ledger.confirmed.block_exists_or_pruned (transaction, key.hash))
{
continue;
}
if (!should_sort && offset_counter > 0)
{
--offset_counter;
continue;
}

if (simple)
{
boost::property_tree::ptree entry;
entry.put ("", key.hash.to_string ());
peers_l.push_back (std::make_pair ("", entry));
continue;
}
if (info.amount.number () < threshold.number ())
{
continue;
}
if (source || min_version)
nano::pending_key const & key (i->first);
if (block_confirmed (node, transaction, key.hash, include_active, include_only_confirmed))
{
boost::property_tree::ptree pending_tree;
pending_tree.put ("amount", info.amount.number ().template convert_to<std::string> ());
if (source)
if (!should_sort && offset_counter > 0)
{
pending_tree.put ("source", info.source.to_account ());
}
if (min_version)
{
pending_tree.put ("min_version", epoch_as_string (info.epoch));
--offset_counter;
continue;
}

if (should_sort)
{
hash_ptree_pairs.emplace_back (key.hash.to_string (), pending_tree);
}
else
{
peers_l.add_child (key.hash.to_string (), pending_tree);
}
}
else
{
if (should_sort)
if (simple)
{
hash_amount_pairs.emplace_back (key.hash.to_string (), info.amount.number ());
boost::property_tree::ptree entry;
entry.put ("", key.hash.to_string ());
peers_l.push_back (std::make_pair ("", entry));
}
else
{
peers_l.put (key.hash.to_string (), info.amount.number ().template convert_to<std::string> ());
nano::pending_info const & info (i->second);
if (info.amount.number () >= threshold.number ())
{
if (source || min_version)
{
boost::property_tree::ptree pending_tree;
pending_tree.put ("amount", info.amount.number ().convert_to<std::string> ());
if (source)
{
pending_tree.put ("source", info.source.to_account ());
}
if (min_version)
{
pending_tree.put ("min_version", epoch_as_string (info.epoch));
}

if (should_sort)
{
hash_ptree_pairs.emplace_back (key.hash.to_string (), pending_tree);
}
else
{
peers_l.add_child (key.hash.to_string (), pending_tree);
}
}
else
{
if (should_sort)
{
hash_amount_pairs.emplace_back (key.hash.to_string (), info.amount.number ());
}
else
{
peers_l.put (key.hash.to_string (), info.amount.number ().convert_to<std::string> ());
}
}
}
}
}
}
Expand Down Expand Up @@ -4837,41 +4839,42 @@ void nano::json_handler::wallet_receivable ()
{
nano::account const & account (i->first);
boost::property_tree::ptree peers_l;
for (auto current = node.ledger.any.receivable_upper_bound (block_transaction, account, 0), end = node.ledger.any.receivable_end (); current != end && (peers_l.size () < count); ++current)
for (auto ii (node.store.pending.begin (block_transaction, nano::pending_key (account, 0))), nn (node.store.pending.end ()); ii != nn && nano::pending_key (ii->first).account == account && peers_l.size () < count; ++ii)
{
auto const & [key, info] = *current;
if (include_only_confirmed && !node.ledger.confirmed.block_exists_or_pruned (block_transaction, key.hash))
{
continue;
}
if (threshold.is_zero () && !source)
{
boost::property_tree::ptree entry;
entry.put ("", key.hash.to_string ());
peers_l.push_back (std::make_pair ("", entry));
continue;
}
if (info.amount.number () < threshold.number ())
{
continue;
}
if (source || min_version)
nano::pending_key key (ii->first);
if (block_confirmed (node, block_transaction, key.hash, include_active, include_only_confirmed))
{
boost::property_tree::ptree pending_tree;
pending_tree.put ("amount", info.amount.number ().template convert_to<std::string> ());
if (source)
if (threshold.is_zero () && !source)
{
pending_tree.put ("source", info.source.to_account ());
boost::property_tree::ptree entry;
entry.put ("", key.hash.to_string ());
peers_l.push_back (std::make_pair ("", entry));
}
if (min_version)
else
{
pending_tree.put ("min_version", epoch_as_string (info.epoch));
nano::pending_info info (ii->second);
if (info.amount.number () >= threshold.number ())
{
if (source || min_version)
{
boost::property_tree::ptree pending_tree;
pending_tree.put ("amount", info.amount.number ().convert_to<std::string> ());
if (source)
{
pending_tree.put ("source", info.source.to_account ());
}
if (min_version)
{
pending_tree.put ("min_version", epoch_as_string (info.epoch));
}
peers_l.add_child (key.hash.to_string (), pending_tree);
}
else
{
peers_l.put (key.hash.to_string (), info.amount.number ().convert_to<std::string> ());
}
}
}
peers_l.add_child (key.hash.to_string (), pending_tree);
}
else
{
peers_l.put (key.hash.to_string (), info.amount.number ().template convert_to<std::string> ());
}
}
if (!peers_l.empty ())
Expand Down
11 changes: 6 additions & 5 deletions nano/node/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1185,14 +1185,15 @@ bool nano::wallet::search_receivable (store::transaction const & wallet_transact
// Don't search pending for watch-only accounts
if (!nano::wallet_value (i->second).key.is_zero ())
{
for (auto i = wallets.node.ledger.any.receivable_upper_bound (block_transaction, account, 0), n = wallets.node.ledger.any.receivable_end (); i != n; ++i)
for (auto j (wallets.node.store.pending.begin (block_transaction, nano::pending_key (account, 0))), k (wallets.node.store.pending.end ()); j != k && nano::pending_key (j->first).account == account; ++j)
{
auto const & [key, info] = *i;
auto hash = key.hash;
auto amount = info.amount.number ();
nano::pending_key key (j->first);
auto hash (key.hash);
nano::pending_info pending (j->second);
auto amount (pending.amount.number ());
if (wallets.node.config.receive_minimum.number () <= amount)
{
wallets.node.logger.info (nano::log::type::wallet, "Found a receivable block {} for account {}", hash.to_string (), info.source.to_account ());
wallets.node.logger.info (nano::log::type::wallet, "Found a receivable block {} for account {}", hash.to_string (), pending.source.to_account ());

if (wallets.node.ledger.confirmed.block_exists_or_pruned (block_transaction, hash))
{
Expand Down
6 changes: 3 additions & 3 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5580,7 +5580,7 @@ TEST (rpc, unopened)
boost::property_tree::ptree request;
request.put ("action", "unopened");
request.put ("count", "1");
request.put ("account", account1.to_account());
request.put ("account", account1.to_account ());
auto response (wait_response (system, rpc_ctx, request));
auto & accounts (response.get_child ("accounts"));
ASSERT_EQ (1, accounts.size ());
Expand All @@ -5591,7 +5591,7 @@ TEST (rpc, unopened)
boost::property_tree::ptree request;
request.put ("action", "unopened");
request.put ("count", "1");
request.put ("account", account2.to_account());
request.put ("account", account2.to_account ());
auto response (wait_response (system, rpc_ctx, request));
auto & accounts (response.get_child ("accounts"));
ASSERT_EQ (1, accounts.size ());
Expand Down Expand Up @@ -5626,7 +5626,7 @@ TEST (rpc, unopened_seek)
boost::property_tree::ptree request;
request.put ("action", "unopened");
request.put ("count", "1");
request.put ("account", nano::dev::genesis_key.pub.to_account());
request.put ("account", nano::dev::genesis_key.pub.to_account ());
auto response (wait_response (system, rpc_ctx, request));
auto & accounts (response.get_child ("accounts"));
ASSERT_EQ (1, accounts.size ());
Expand Down
16 changes: 12 additions & 4 deletions nano/secure/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,11 +792,19 @@ void nano::ledger::initialize (nano::generate_cache_flags const & generate_cache

nano::uint128_t nano::ledger::account_receivable (secure::transaction const & transaction_a, nano::account const & account_a, bool only_confirmed_a)
{
nano::uint128_t result{ 0 };
for (auto i = any.receivable_upper_bound (transaction_a, account_a, 0), n = any.receivable_end (); i != n; ++i)
nano::uint128_t result (0);
nano::account end (account_a.number () + 1);
for (auto i (store.pending.begin (transaction_a, nano::pending_key (account_a, 0))), n (store.pending.begin (transaction_a, nano::pending_key (end, 0))); i != n; ++i)
{
auto const & [key, info] = *i;
if (!only_confirmed_a || confirmed.block_exists_or_pruned (transaction_a, key.hash))
nano::pending_info const & info (i->second);
if (only_confirmed_a)
{
if (confirmed.block_exists_or_pruned (transaction_a, i->first.hash))
{
result += info.amount.number ();
}
}
else
{
result += info.amount.number ();
}
Expand Down

0 comments on commit d84ffb7

Please sign in to comment.