aboutsummaryrefslogtreecommitdiff
path: root/tests/unit_tests/subaddress.cpp
diff options
context:
space:
mode:
authortobtoht <tob@featherwallet.org>2025-07-10 12:19:42 +0000
committertobtoht <tob@featherwallet.org>2025-07-10 12:19:42 +0000
commit8b4f0a6258af26b978b85a1fa1bd0ee8244de3de (patch)
tree6d4fa72191edfdd028228d04e5f2688648757b03 /tests/unit_tests/subaddress.cpp
parentf1ffcc5c497c8b5475eab4a9ff53b21e21f58d50 (diff)
parent1da19dac545bb5c0521949a212af42a7f351776e (diff)
downloadmonzero-core-8b4f0a6258af26b978b85a1fa1bd0ee8244de3de.tar.gz
monzero-core-8b4f0a6258af26b978b85a1fa1bd0ee8244de3de.tar.xz
monzero-core-8b4f0a6258af26b978b85a1fa1bd0ee8244de3de.zip
Merge pull request #9954
1da19da wallet: refactor subaddress expansion & add to transfer test (jeffro256) e23d51b wallet: improve lookahead logic & make rpc persistent (Justin Berman) 678f5da wallet: create set_subaddress_lookahead wallet rpc endpoint (benevanoff) 8f5a7b0 wallet: ensure subaddress keys table is at least size of requested lookahead (benevanoff)
Diffstat (limited to 'tests/unit_tests/subaddress.cpp')
-rw-r--r--tests/unit_tests/subaddress.cpp112
1 files changed, 112 insertions, 0 deletions
diff --git a/tests/unit_tests/subaddress.cpp b/tests/unit_tests/subaddress.cpp
index 17fc0fe78..71cbdbee6 100644
--- a/tests/unit_tests/subaddress.cpp
+++ b/tests/unit_tests/subaddress.cpp
@@ -28,6 +28,7 @@
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#include <boost/filesystem.hpp>
+#include <boost/optional/optional_io.hpp>
#include "gtest/gtest.h"
#include "include_base_utils.h"
@@ -101,3 +102,114 @@ TEST_F(WalletSubaddress, OutOfBoundsIndexes)
EXPECT_STREQ("index.minor is out of bound", e.what());
}
}
+
+// Helper function to check max subaddrs allocated
+static void check_expected_max(const tools::wallet2 &w1, const cryptonote::subaddress_index exp_max)
+{
+ 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);
+ EXPECT_NE(boost::none, w1.get_subaddress_index(subaddr));
+ // fist test expanding the major lookahead
+ w1.set_subaddress_lookahead(100, 200);
+ EXPECT_EQ(100, w1.get_subaddress_lookahead().first);
+ EXPECT_EQ(200, w1.get_subaddress_lookahead().second);
+ 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);
+ 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})));
+}