| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | // FIXME: move this into a full wallet2 unit test suite, if possible |
| 30 | |
| 31 | #include "gtest/gtest.h" |
| 32 | |
| 33 | #include "wallet/wallet2.h" |
| 34 | #include <string> |
| 35 | |
| 36 | static tools::wallet2::transfer_container make_transfers_container(size_t N) |
| 37 | { |
| 38 | tools::wallet2::transfer_container transfers; |
| 39 | for (size_t n = 0; n < N; ++n) |
| 40 | { |
| 41 | transfers.push_back(AUTO_VAL_INIT(tools::wallet2::transfer_details())); |
| 42 | tools::wallet2::transfer_details &td = transfers.back(); |
| 43 | td.m_block_height = 1000; |
| 44 | td.m_spent = false; |
| 45 | td.m_txid = crypto::null_hash; |
| 46 | td.m_txid.data[0] = n & 0xff; |
| 47 | td.m_txid.data[1] = (n >> 8) & 0xff; |
| 48 | td.m_txid.data[2] = (n >> 16) & 0xff; |
| 49 | td.m_txid.data[3] = (n >> 24) & 0xff; |
| 50 | } |
| 51 | return transfers; |
| 52 | } |
| 53 | |
| 54 | #define SELECT(idx) \ |
| 55 | do { \ |
| 56 | auto i = std::find(unused_indices.begin(), unused_indices.end(), idx); \ |
| 57 | ASSERT_TRUE(i != unused_indices.end()); \ |
| 58 | unused_indices.erase(i); \ |
| 59 | selected.push_back(idx); \ |
| 60 | } while(0) |
| 61 | |
| 62 | #define PICK(expected) \ |
| 63 | do { \ |
| 64 | size_t idx = w.pop_best_value_from(transfers, unused_indices, selected); \ |
| 65 | ASSERT_EQ(expected, idx); \ |
| 66 | selected.push_back(idx); \ |
| 67 | } while(0) |
| 68 | |
| 69 | TEST(select_outputs, one_out_of_N) |
| 70 | { |
| 71 | tools::wallet2 w; |
| 72 | |
| 73 | // check that if there are N-1 outputs of the same height, one of them |
| 74 | // already selected, the next one selected is the one that's from a |
| 75 | // different height |
| 76 | tools::wallet2::transfer_container transfers = make_transfers_container(10); |
| 77 | transfers[6].m_block_height = 700; |
| 78 | std::vector<size_t> unused_indices({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); |
| 79 | std::vector<size_t> selected; |
| 80 | SELECT(2); |
| 81 | PICK(6); |
| 82 | } |
| 83 | |
| 84 | TEST(select_outputs, order) |
| 85 | { |
| 86 | tools::wallet2 w; |
| 87 | |
| 88 | // check that most unrelated heights are picked in order |
| 89 | tools::wallet2::transfer_container transfers = make_transfers_container(5); |
| 90 | transfers[0].m_block_height = 700; |
| 91 | transfers[1].m_block_height = 700; |
| 92 | transfers[2].m_block_height = 704; |
| 93 | transfers[3].m_block_height = 716; |
| 94 | transfers[4].m_block_height = 701; |
| 95 | std::vector<size_t> unused_indices({0, 1, 2, 3, 4}); |
| 96 | std::vector<size_t> selected; |
| 97 | SELECT(0); |
| 98 | PICK(3); // first the one that's far away |
| 99 | PICK(2); // then the one that's close |
| 100 | PICK(4); // then the one that's adjacent |
| 101 | PICK(1); // then the one that's on the same height |
| 102 | } |
| 103 | |
| 104 | #define MKOFFSETS(N, n) \ |
| 105 | offsets.resize(N); \ |
| 106 | size_t n_outs = 0; \ |
| 107 | for (auto &offset: offsets) \ |
| 108 | { \ |
| 109 | offset = n_outs += (n); \ |
| 110 | } |
| 111 | |
| 112 | TEST(select_outputs, gamma) |
| 113 | { |
| 114 | std::vector<uint64_t> offsets; |
| 115 | |
| 116 | MKOFFSETS(300000, 1); |
| 117 | tools::gamma_picker picker(offsets); |
| 118 | std::vector<double> ages(100000); |
| 119 | double age_scale = 120. * (offsets.size() / (double)n_outs); |
| 120 | for (size_t i = 0; i < ages.size(); ) |
| 121 | { |
| 122 | uint64_t o = picker.pick(); |
| 123 | if (o >= n_outs) |
| 124 | continue; |
| 125 | ages[i] = (n_outs - 1 - o) * age_scale; |
| 126 | ASSERT_GE(ages[i], 0); |
| 127 | ASSERT_LE(ages[i], offsets.size() * 120); |
| 128 | ++i; |
| 129 | } |
| 130 | double median = epee::misc_utils::median(ages); |
| 131 | MDEBUG("median age: " << median / 86400. << " days"); |
| 132 | ASSERT_GE(median, 1.3 * 86400); |
| 133 | ASSERT_LE(median, 1.4 * 86400); |
| 134 | } |
| 135 | |
| 136 | TEST(select_outputs, density) |
| 137 | { |
| 138 | static const size_t NPICKS = 1000000; |
| 139 | std::vector<uint64_t> offsets; |
| 140 | |
| 141 | MKOFFSETS(300000, 1 + (crypto::rand<size_t>() & 0x1f)); |
| 142 | tools::gamma_picker picker(offsets); |
| 143 | |
| 144 | std::vector<int> picks(/*n_outs*/offsets.size(), 0); |
| 145 | for (int i = 0; i < NPICKS; ) |
| 146 | { |
| 147 | uint64_t o = picker.pick(); |
| 148 | if (o >= n_outs) |
| 149 | continue; |
| 150 | auto it = std::lower_bound(offsets.begin(), offsets.end(), o); |
| 151 | auto idx = std::distance(offsets.begin(), it); |
| 152 | ASSERT_LT(idx, picks.size()); |
| 153 | ++picks[idx]; |
| 154 | ++i; |
| 155 | } |
| 156 | |
| 157 | for (int d = 1; d < 0x20; ++d) |
| 158 | { |
| 159 | // count the number of times an output in a block of d outputs was selected |
| 160 | // count how many outputs are in a block of d outputs |
| 161 | size_t count_selected = 0, count_chain = 0; |
| 162 | for (size_t i = 0; i < offsets.size(); ++i) |
| 163 | { |
| 164 | size_t n_outputs = offsets[i] - (i == 0 ? 0 : offsets[i - 1]); |
| 165 | if (n_outputs == d) |
| 166 | { |
| 167 | count_selected += picks[i]; |
| 168 | count_chain += d; |
| 169 | } |
| 170 | } |
| 171 | float selected_ratio = count_selected / (float)NPICKS; |
| 172 | float chain_ratio = count_chain / (float)n_outs; |
| 173 | MDEBUG(count_selected << "/" << NPICKS << " outputs selected in blocks of density " << d << ", " << 100.0f * selected_ratio << "%"); |
| 174 | MDEBUG(count_chain << "/" << offsets.size() << " outputs in blocks of density " << d << ", " << 100.0f * chain_ratio << "%"); |
| 175 | ASSERT_LT(fabsf(selected_ratio - chain_ratio), 0.025f); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | TEST(select_outputs, same_distribution) |
| 180 | { |
| 181 | static const size_t NPICKS = 1000000; |
| 182 | std::vector<uint64_t> offsets; |
| 183 | |
| 184 | MKOFFSETS(300000, 1 + (crypto::rand<size_t>() & 0x1f)); |
| 185 | tools::gamma_picker picker(offsets); |
| 186 | |
| 187 | std::vector<int> chain_picks(offsets.size(), 0); |
| 188 | std::vector<int> output_picks(n_outs, 0); |
| 189 | for (int i = 0; i < NPICKS; ) |
| 190 | { |
| 191 | uint64_t o = picker.pick(); |
| 192 | if (o >= n_outs) |
| 193 | continue; |
| 194 | auto it = std::lower_bound(offsets.begin(), offsets.end(), o); |
| 195 | auto idx = std::distance(offsets.begin(), it); |
| 196 | ASSERT_LT(idx, chain_picks.size()); |
| 197 | ++chain_picks[idx]; |
| 198 | ++output_picks[o]; |
| 199 | ++i; |
| 200 | } |
| 201 | |
| 202 | // scale them both to 0-100 |
| 203 | std::vector<int> chain_norm(100, 0), output_norm(100, 0); |
| 204 | for (size_t i = 0; i < output_picks.size(); ++i) |
| 205 | output_norm[i * 100 / output_picks.size()] += output_picks[i]; |
| 206 | for (size_t i = 0; i < chain_picks.size(); ++i) |
| 207 | chain_norm[i * 100 / chain_picks.size()] += chain_picks[i]; |
| 208 | |
| 209 | double avg_dev = 0.0; |
| 210 | for (size_t i = 0; i < 100; ++i) |
| 211 | { |
| 212 | const double diff = (double)output_norm[i] - (double)chain_norm[i]; |
| 213 | double dev = fabs(2.0 * diff / (output_norm[i] + chain_norm[i])); |
| 214 | ASSERT_LT(dev, 0.15); |
| 215 | avg_dev += dev; |
| 216 | } |
| 217 | avg_dev /= 100; |
| 218 | MDEBUG("avg_dev: " << avg_dev); |
| 219 | ASSERT_LT(avg_dev, 0.02); |
| 220 | } |