Merge pull request #4711 from clemahieu/revert_receivable_iterator

Partially revert receivable iterator
This commit is contained in:
clemahieu 2024-08-22 10:30:27 +01:00 committed by GitHub
commit d84ffb7219
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 125 additions and 113 deletions

View file

@ -1054,34 +1054,35 @@ void nano::json_handler::accounts_receivable ()
if (!ec) if (!ec)
{ {
boost::property_tree::ptree peers_l; 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; nano::pending_key const & key (i->first);
if (include_only_confirmed && !node.ledger.confirmed.block_exists_or_pruned (transaction, key.hash)) if (block_confirmed (node, transaction, key.hash, include_active, include_only_confirmed))
{ {
continue;
}
if (simple) if (simple)
{ {
boost::property_tree::ptree entry; boost::property_tree::ptree entry;
entry.put ("", key.hash.to_string ()); entry.put ("", key.hash.to_string ());
peers_l.push_back (std::make_pair ("", entry)); peers_l.push_back (std::make_pair ("", entry));
continue;
} }
if (info.amount.number () < threshold.number ()) else
{
nano::pending_info const & info (i->second);
if (info.amount.number () >= threshold.number ())
{ {
continue;
}
if (source) if (source)
{ {
boost::property_tree::ptree pending_tree; boost::property_tree::ptree pending_tree;
pending_tree.put ("amount", info.amount.number ().template convert_to<std::string> ()); pending_tree.put ("amount", info.amount.number ().convert_to<std::string> ());
pending_tree.put ("source", info.source.to_account ()); pending_tree.put ("source", info.source.to_account ());
peers_l.add_child (key.hash.to_string (), pending_tree); peers_l.add_child (key.hash.to_string (), pending_tree);
} }
else else
{ {
peers_l.put (key.hash.to_string (), info.amount.number ().template convert_to<std::string> ()); peers_l.put (key.hash.to_string (), info.amount.number ().convert_to<std::string> ());
}
}
}
} }
} }
if (sorting && !simple) if (sorting && !simple)
@ -3109,13 +3110,11 @@ 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. // 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, boost::property_tree::ptree>> hash_ptree_pairs;
std::vector<std::pair<std::string, nano::uint128_t>> hash_amount_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; nano::pending_key const & key (i->first);
if (include_only_confirmed && !node.ledger.confirmed.block_exists_or_pruned (transaction, key.hash)) if (block_confirmed (node, transaction, key.hash, include_active, include_only_confirmed))
{ {
continue;
}
if (!should_sort && offset_counter > 0) if (!should_sort && offset_counter > 0)
{ {
--offset_counter; --offset_counter;
@ -3127,16 +3126,16 @@ void nano::json_handler::receivable ()
boost::property_tree::ptree entry; boost::property_tree::ptree entry;
entry.put ("", key.hash.to_string ()); entry.put ("", key.hash.to_string ());
peers_l.push_back (std::make_pair ("", entry)); peers_l.push_back (std::make_pair ("", entry));
continue;
} }
if (info.amount.number () < threshold.number ()) else
{
nano::pending_info const & info (i->second);
if (info.amount.number () >= threshold.number ())
{ {
continue;
}
if (source || min_version) if (source || min_version)
{ {
boost::property_tree::ptree pending_tree; boost::property_tree::ptree pending_tree;
pending_tree.put ("amount", info.amount.number ().template convert_to<std::string> ()); pending_tree.put ("amount", info.amount.number ().convert_to<std::string> ());
if (source) if (source)
{ {
pending_tree.put ("source", info.source.to_account ()); pending_tree.put ("source", info.source.to_account ());
@ -3163,7 +3162,10 @@ void nano::json_handler::receivable ()
} }
else else
{ {
peers_l.put (key.hash.to_string (), info.amount.number ().template convert_to<std::string> ()); peers_l.put (key.hash.to_string (), info.amount.number ().convert_to<std::string> ());
}
}
}
} }
} }
} }
@ -4837,28 +4839,26 @@ void nano::json_handler::wallet_receivable ()
{ {
nano::account const & account (i->first); nano::account const & account (i->first);
boost::property_tree::ptree peers_l; 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; nano::pending_key key (ii->first);
if (include_only_confirmed && !node.ledger.confirmed.block_exists_or_pruned (block_transaction, key.hash)) if (block_confirmed (node, block_transaction, key.hash, include_active, include_only_confirmed))
{ {
continue;
}
if (threshold.is_zero () && !source) if (threshold.is_zero () && !source)
{ {
boost::property_tree::ptree entry; boost::property_tree::ptree entry;
entry.put ("", key.hash.to_string ()); entry.put ("", key.hash.to_string ());
peers_l.push_back (std::make_pair ("", entry)); peers_l.push_back (std::make_pair ("", entry));
continue;
} }
if (info.amount.number () < threshold.number ()) else
{
nano::pending_info info (ii->second);
if (info.amount.number () >= threshold.number ())
{ {
continue;
}
if (source || min_version) if (source || min_version)
{ {
boost::property_tree::ptree pending_tree; boost::property_tree::ptree pending_tree;
pending_tree.put ("amount", info.amount.number ().template convert_to<std::string> ()); pending_tree.put ("amount", info.amount.number ().convert_to<std::string> ());
if (source) if (source)
{ {
pending_tree.put ("source", info.source.to_account ()); pending_tree.put ("source", info.source.to_account ());
@ -4871,7 +4871,10 @@ void nano::json_handler::wallet_receivable ()
} }
else else
{ {
peers_l.put (key.hash.to_string (), info.amount.number ().template convert_to<std::string> ()); peers_l.put (key.hash.to_string (), info.amount.number ().convert_to<std::string> ());
}
}
}
} }
} }
if (!peers_l.empty ()) if (!peers_l.empty ())

View file

@ -1185,14 +1185,15 @@ bool nano::wallet::search_receivable (store::transaction const & wallet_transact
// Don't search pending for watch-only accounts // Don't search pending for watch-only accounts
if (!nano::wallet_value (i->second).key.is_zero ()) 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; nano::pending_key key (j->first);
auto hash = key.hash; auto hash (key.hash);
auto amount = info.amount.number (); nano::pending_info pending (j->second);
auto amount (pending.amount.number ());
if (wallets.node.config.receive_minimum.number () <= amount) 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)) if (wallets.node.ledger.confirmed.block_exists_or_pruned (block_transaction, hash))
{ {

View file

@ -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 nano::ledger::account_receivable (secure::transaction const & transaction_a, nano::account const & account_a, bool only_confirmed_a)
{ {
nano::uint128_t result{ 0 }; 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::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; nano::pending_info const & info (i->second);
if (!only_confirmed_a || confirmed.block_exists_or_pruned (transaction_a, key.hash)) if (only_confirmed_a)
{
if (confirmed.block_exists_or_pruned (transaction_a, i->first.hash))
{
result += info.amount.number ();
}
}
else
{ {
result += info.amount.number (); result += info.amount.number ();
} }