Skip to content

Commit 2a011b4

Browse files
authored
Cache representative keys to optimize wallets::foreach_representative (...) hot path (nanocurrency#5060)
* Cache representative keys for voting Store unlocked representative keys in a cache so `foreach_representative` can iterate preloaded keys instead of walking wallet state and fetching them on every call. Refresh the cache when representative membership or wallet lock state changes, use `wallet::lock()` in the RPC handler, and remove the recursive representative iteration test since recursion is no longer supported. * Rename compute_reps to refresh_reps * Wallet rep key cache tests * Refresh rep cache after wallet write txns Move representative cache rebuilds out of active write transaction scopes for unlock, rekey, seed, and deterministic wallet updates.
1 parent f511bf9 commit 2a011b4

File tree

7 files changed

+603
-121
lines changed

7 files changed

+603
-121
lines changed

nano/core_test/vote_generator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ TEST (vote_generator, multiple_representatives)
681681
wallet.insert_adhoc (rep1.prv);
682682
wallet.insert_adhoc (rep2.prv);
683683
wallet.insert_adhoc (rep3.prv);
684-
node.wallets.compute_reps ();
684+
node.wallets.refresh_reps ();
685685
ASSERT_EQ (4, node.wallets.reps ().voting);
686686

687687
nano::shared_locked<std::vector<std::shared_ptr<nano::vote>>> votes;

nano/core_test/vote_replier.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ TEST (vote_replier, single_hash_reply)
117117
config.enable_voting = true;
118118
auto & node = *system.add_node (config);
119119
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
120-
node.wallets.compute_reps ();
120+
node.wallets.refresh_reps ();
121121

122122
auto blocks = nano::test::setup_chain (system, node, 1);
123123

@@ -145,7 +145,7 @@ TEST (vote_replier, multiple_hashes_reply)
145145
config.enable_voting = true;
146146
auto & node = *system.add_node (config);
147147
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
148-
node.wallets.compute_reps ();
148+
node.wallets.refresh_reps ();
149149

150150
auto blocks = nano::test::setup_chain (system, node, 3);
151151

@@ -182,7 +182,7 @@ TEST (vote_replier, mixed_known_unknown)
182182
config.enable_voting = true;
183183
auto & node = *system.add_node (config);
184184
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
185-
node.wallets.compute_reps ();
185+
node.wallets.refresh_reps ();
186186

187187
auto blocks = nano::test::setup_chain (system, node, 1);
188188

@@ -248,7 +248,7 @@ TEST (vote_replier, per_channel_fairness)
248248
config.vote_replier.channel_limit = 1;
249249
auto & node = *system.add_node (config);
250250
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
251-
node.wallets.compute_reps ();
251+
node.wallets.refresh_reps ();
252252

253253
auto blocks = nano::test::setup_chain (system, node, 1);
254254

@@ -281,7 +281,7 @@ TEST (vote_replier, integration_confirm_req)
281281
config.optimistic_scheduler.enable = false;
282282
auto & node = *system.add_node (config);
283283
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
284-
node.wallets.compute_reps ();
284+
node.wallets.refresh_reps ();
285285

286286
// Create and confirm a block
287287
nano::keypair key;
@@ -528,7 +528,7 @@ TEST (vote_replier, multiple_representatives)
528528
ASSERT_TIMELY (5s, node.balance (rep2.pub) == amount);
529529
wallet.change_sync (rep2.pub, rep2.pub);
530530
ASSERT_EQ (node.weight (rep2.pub), amount);
531-
node.wallets.compute_reps ();
531+
node.wallets.refresh_reps ();
532532
ASSERT_EQ (2, node.wallets.reps ().voting);
533533

534534
auto blocks = nano::test::setup_chain (system, node, 1);

nano/core_test/wallet.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -995,28 +995,6 @@ TEST (wallet, epoch_2_receive_unopened)
995995
ASSERT_LT (tries, max_tries);
996996
}
997997

998-
/**
999-
* This test checks that wallets::foreach_representative can be used recursively
1000-
*/
1001-
TEST (wallet, foreach_representative_deadlock)
1002-
{
1003-
nano::test::system system (1);
1004-
auto & node (*system.nodes[0]);
1005-
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
1006-
node.wallets.compute_reps ();
1007-
ASSERT_EQ (1, node.wallets.reps ().voting);
1008-
1009-
bool set = false;
1010-
node.wallets.foreach_representative ([&node, &set, &system] (nano::public_key const & pub, nano::raw_key const & prv) {
1011-
node.wallets.foreach_representative ([&node, &set, &system] (nano::public_key const & pub, nano::raw_key const & prv) {
1012-
ASSERT_TIMELY (5s, node.wallets.mutex.try_lock () == 1);
1013-
node.wallets.mutex.unlock ();
1014-
set = true;
1015-
});
1016-
});
1017-
ASSERT_TRUE (set);
1018-
}
1019-
1020998
TEST (wallet, search_receivable)
1021999
{
10221000
nano::test::system system;

0 commit comments

Comments
 (0)