aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJustin Berman <justinberman@protonmail.com>2025-06-26 17:32:12 +0000
committernahuhh <50635951+nahuhh@users.noreply.github.com>2025-07-07 22:21:43 +0000
commite23d51bc1683e4aa369ba0f2889b96113eeb4e3a (patch)
treee5c6ae3a60d2fdfc03938a4dcaa6efb84ad234f5 /tests
parent678f5dab31aa27c9e6e1fb24a4c1f683ef242e1a (diff)
downloadmonzero-core-e23d51bc1683e4aa369ba0f2889b96113eeb4e3a.tar.gz
monzero-core-e23d51bc1683e4aa369ba0f2889b96113eeb4e3a.tar.xz
monzero-core-e23d51bc1683e4aa369ba0f2889b96113eeb4e3a.zip
wallet: improve lookahead logic & make rpc persistent
Diffstat (limited to 'tests')
-rw-r--r--tests/unit_tests/subaddress.cpp101
1 files changed, 93 insertions, 8 deletions
diff --git a/tests/unit_tests/subaddress.cpp b/tests/unit_tests/subaddress.cpp
index b03e58f5a..71cbdbee6 100644
--- a/tests/unit_tests/subaddress.cpp
+++ b/tests/unit_tests/subaddress.cpp
@@ -103,12 +103,36 @@ TEST_F(WalletSubaddress, OutOfBoundsIndexes)
}
}
-TEST_F(WalletSubaddress, ExpandPubkeyTable)
+// Helper function to check max subaddrs allocated
+static void check_expected_max(const tools::wallet2 &w1, const cryptonote::subaddress_index exp_max)
{
- // these test assume we are starting with the default setup state
+ for (uint32_t i = 0; i <= exp_max.minor; ++i)
+ {
+ auto subaddr = w1.get_subaddress({exp_max.major, i});
+ EXPECT_NE(boost::none, w1.get_subaddress_index(subaddr));
+ }
+ auto subaddr = w1.get_subaddress({exp_max.major, exp_max.minor + 1});
+ EXPECT_EQ(boost::none, w1.get_subaddress_index(subaddr));
+};
+
+static void expect_default_wallet_state(const tools::wallet2 &w1)
+{
+ // these tests assume we are starting with the default setup state
EXPECT_EQ(2, w1.get_num_subaddress_accounts());
EXPECT_EQ(50, w1.get_subaddress_lookahead().first);
EXPECT_EQ(200, w1.get_subaddress_lookahead().second);
+
+ // We assume we start with subaddrs for minor indexes 0 to 199
+ check_expected_max(w1, {0,199});
+ check_expected_max(w1, {1,199});
+ check_expected_max(w1, {49,199});
+ check_expected_max(w1, {50,199}); // 50 because the test starts with accounts 0 and 1 already allocated
+ EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({51,0})));
+}
+
+TEST_F(WalletSubaddress, SetLookahead)
+{
+ expect_default_wallet_state(w1);
// get_subaddress_index looks up keys in the private m_subaddresses dictionary so we will use it to test if a key is properly being scanned for
cryptonote::subaddress_index test_idx = {50, 199};
auto subaddr = w1.get_subaddress(test_idx);
@@ -117,14 +141,75 @@ TEST_F(WalletSubaddress, ExpandPubkeyTable)
w1.set_subaddress_lookahead(100, 200);
EXPECT_EQ(100, w1.get_subaddress_lookahead().first);
EXPECT_EQ(200, w1.get_subaddress_lookahead().second);
- test_idx = {100, 199};
- subaddr = w1.get_subaddress(test_idx);
- EXPECT_NE(boost::none, w1.get_subaddress_index(subaddr));
+ check_expected_max(w1, {100, 199});
// next test expanding the minor lookahead
w1.set_subaddress_lookahead(100, 300);
EXPECT_EQ(100, w1.get_subaddress_lookahead().first);
EXPECT_EQ(300, w1.get_subaddress_lookahead().second);
- test_idx = {100, 299};
- subaddr = w1.get_subaddress(test_idx);
- EXPECT_NE(boost::none, w1.get_subaddress_index(subaddr));
+ check_expected_max(w1, {100, 299});
+}
+
+TEST_F(WalletSubaddress, ExpandThenSetMinorIncreaseOnly)
+{
+ expect_default_wallet_state(w1);
+
+ // Mock receive to {0,150}, so expand from there
+ w1.expand_subaddresses({0,150});
+ // We should now have subaddresses for minor indexes 0 to 349
+ check_expected_max(w1, {0,349});
+ check_expected_max(w1, {1,199});
+ check_expected_max(w1, {49,199});
+ check_expected_max(w1, {50,199});
+ EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({51,0})));
+
+ // Now set the minor lookahead 100 higher
+ w1.set_subaddress_lookahead(50, 200+100);
+ // We should have subaddresses for minor indexes 0 to 449
+ check_expected_max(w1, {0,449});
+ check_expected_max(w1, {1,299});
+ check_expected_max(w1, {49,299});
+ check_expected_max(w1, {50,299});
+ EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({51,0})));
+}
+
+TEST_F(WalletSubaddress, ExpandThenSetMajorIncreaseOnly)
+{
+ expect_default_wallet_state(w1);
+
+ // Mock receive to {40,0}, so expand from there
+ w1.expand_subaddresses({40,0});
+ check_expected_max(w1, {0,199});
+ check_expected_max(w1, {1,199});
+ check_expected_max(w1, {40,199});
+ check_expected_max(w1, {89,199});
+ EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({90,0})));
+
+ // Now set the major lookahead 10 higher
+ w1.set_subaddress_lookahead(50+10, 200);
+ check_expected_max(w1, {0,199});
+ check_expected_max(w1, {1,199});
+ check_expected_max(w1, {40,199});
+ check_expected_max(w1, {99,199});
+ EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({100,0})));
+}
+
+TEST_F(WalletSubaddress, ExpandThenSetIncreaseBoth)
+{
+ expect_default_wallet_state(w1);
+
+ // Mock receive to {40,150}, so expand from there
+ w1.expand_subaddresses({40,150});
+ check_expected_max(w1, {0,199});
+ check_expected_max(w1, {1,199});
+ check_expected_max(w1, {40,349});
+ check_expected_max(w1, {89,199});
+ EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({90,0})));
+
+ // Now set the major lookahead 10 higher and minor 100 higher
+ w1.set_subaddress_lookahead(50+10, 200+100);
+ check_expected_max(w1, {0,299});
+ check_expected_max(w1, {1,299});
+ check_expected_max(w1, {40,449});
+ check_expected_max(w1, {99,299});
+ EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({100,0})));
}