diff --git a/nano/node/json_handler.cpp b/nano/node/json_handler.cpp index a75c63a4dd..e426f5fc66 100644 --- a/nano/node/json_handler.cpp +++ b/nano/node/json_handler.cpp @@ -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 ()); - 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 ()); + 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 ()); + 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 ()); + } + } + } } } if (sorting && !simple) @@ -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> hash_ptree_pairs; std::vector> 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 ()); - 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 ()); + 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 ()); + 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 ()); + } + } + } } } } @@ -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 ()); - 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 ()); + 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 ()); + } + } } - peers_l.add_child (key.hash.to_string (), pending_tree); - } - else - { - peers_l.put (key.hash.to_string (), info.amount.number ().template convert_to ()); } } if (!peers_l.empty ()) diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index e8fc2473c8..764e3fea15 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -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)) { diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index 67a3e8d705..90b17c57c7 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -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 ()); @@ -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 ()); @@ -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 ()); diff --git a/nano/secure/ledger.cpp b/nano/secure/ledger.cpp index 52f819924d..e513bfb05c 100644 --- a/nano/secure/ledger.cpp +++ b/nano/secure/ledger.cpp @@ -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 (); }