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

Delay wallet work caching to allow using lower difficulty on demand #2680

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions nano/core_test/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,41 @@ TEST (wallet, work_generate)
}
}

TEST (wallet, work_cache_delayed)
{
nano::system system (1);
auto & node1 (*system.nodes[0]);
auto wallet (system.wallet (0));
nano::uint128_t amount1 (node1.balance (nano::test_genesis_key.pub));
uint64_t work1;
wallet->insert_adhoc (nano::test_genesis_key.prv);
nano::account account1;
{
auto transaction (node1.wallets.tx_begin_read ());
account1 = system.account (transaction, 0);
}
nano::keypair key;
auto block1 (wallet->send_action (nano::test_genesis_key.pub, key.pub, 100));
ASSERT_EQ (block1->hash (), node1.latest (nano::test_genesis_key.pub));
auto block2 (wallet->send_action (nano::test_genesis_key.pub, key.pub, 100));
ASSERT_EQ (block2->hash (), node1.latest (nano::test_genesis_key.pub));
ASSERT_EQ (block2->hash (), node1.wallets.delayed_work->operator[] (nano::test_genesis_key.pub));
auto threshold (node1.network_params.network.publish_thresholds.base);
auto again (true);
system.deadline_set (10s);
while (again)
{
ASSERT_NO_ERROR (system.poll ());
if (!wallet->store.work_get (node1.wallets.tx_begin_read (), account1, work1))
{
ASSERT_LT (nano::work_difficulty (nano::work_version::work_1, nano::test_genesis_key.pub, work1), threshold);
ASSERT_LT (nano::work_difficulty (nano::work_version::work_1, block1->hash (), work1), threshold);
again = nano::work_difficulty (nano::work_version::work_1, block2->hash (), work1) < threshold;
}
}
ASSERT_GE (nano::work_difficulty (nano::work_version::work_1, block2->hash (), work1), threshold);
}

TEST (wallet, insert_locked)
{
nano::system system (1);
Expand Down
28 changes: 26 additions & 2 deletions nano/node/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,8 @@ std::shared_ptr<nano::block> nano::wallet::send_action (nano::account const & so
bool nano::wallet::action_complete (std::shared_ptr<nano::block> const & block_a, nano::account const & account_a, bool const generate_work_a)
{
bool error{ false };
// Unschedule any work caching for this account
wallets.delayed_work->erase (account_a);
if (block_a != nullptr)
{
if (nano::work_validate (*block_a))
Expand Down Expand Up @@ -1235,8 +1237,30 @@ void nano::wallet::work_update (nano::transaction const & transaction_a, nano::a

void nano::wallet::work_ensure (nano::account const & account_a, nano::root const & root_a)
{
wallets.node.wallets.queue_wallet_action (nano::wallets::generate_priority, shared_from_this (), [account_a, root_a](nano::wallet & wallet_a) {
wallet_a.work_cache_blocking (account_a, root_a);
using namespace std::chrono_literals;
std::chrono::seconds const precache_delay = wallets.node.network_params.network.is_test_network () ? 1s : 10s;

wallets.delayed_work->operator[] (account_a) = root_a;

wallets.node.alarm.add (std::chrono::steady_clock::now () + precache_delay, [this_l = shared_from_this (), account_a, root_a] {
auto delayed_work = this_l->wallets.delayed_work.lock ();
auto existing (delayed_work->find (account_a));
if (existing != delayed_work->end () && existing->second == root_a)
{
delayed_work->erase (existing);
this_l->wallets.queue_wallet_action (nano::wallets::generate_priority, this_l, [account_a, root_a](nano::wallet & wallet_a) {
wallet_a.work_cache_blocking (account_a, root_a);
});
}
else if (existing == delayed_work->end ())
{
this_l->wallets.node.logger.always_log ("Error caching work for account ", account_a.to_account ());
debug_assert (false);
}
else
{
// Scheduled work was replaced by another root, no action to be done
}
});
}

Expand Down
3 changes: 3 additions & 0 deletions nano/node/wallet.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <nano/lib/lmdbconfig.hpp>
#include <nano/lib/locks.hpp>
#include <nano/lib/work.hpp>
#include <nano/node/lmdb/lmdb.hpp>
#include <nano/node/lmdb/wallet_value.hpp>
Expand Down Expand Up @@ -146,6 +147,7 @@ class wallet final : public std::enable_shared_from_this<nano::wallet>
void send_async (nano::account const &, nano::account const &, nano::uint128_t const &, std::function<void(std::shared_ptr<nano::block>)> const &, uint64_t = 0, bool = true, boost::optional<std::string> = {});
void work_cache_blocking (nano::account const &, nano::root const &);
void work_update (nano::transaction const &, nano::account const &, nano::root const &, uint64_t);
// Schedule work generation after a few seconds
void work_ensure (nano::account const &, nano::root const &);
bool search_pending ();
void init_free_accounts (nano::transaction const &);
Expand Down Expand Up @@ -223,6 +225,7 @@ class wallets final
std::function<void(bool)> observer;
std::unordered_map<nano::wallet_id, std::shared_ptr<nano::wallet>> items;
std::multimap<nano::uint128_t, std::pair<std::shared_ptr<nano::wallet>, std::function<void(nano::wallet &)>>, std::greater<nano::uint128_t>> actions;
nano::locked<std::unordered_map<nano::account, nano::root>> delayed_work;
std::mutex mutex;
std::mutex action_mutex;
nano::condition_variable condition;
Expand Down