| 1 | // Copyright (c) 2018-2022, The Monero Project |
| 2 | |
| 3 | // |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without modification, are |
| 7 | // permitted provided that the following conditions are met: |
| 8 | // |
| 9 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | // conditions and the following disclaimer. |
| 11 | // |
| 12 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 13 | // of conditions and the following disclaimer in the documentation and/or other |
| 14 | // materials provided with the distribution. |
| 15 | // |
| 16 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 17 | // used to endorse or promote products derived from this software without specific |
| 18 | // prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 21 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 28 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include "gtest/gtest.h" |
| 31 | #include "misc_log_ex.h" |
| 32 | #include "rpc/rpc_handler.h" |
| 33 | #include "blockchain_db/blockchain_db.h" |
| 34 | #include "cryptonote_core/cryptonote_core.h" |
| 35 | #include "cryptonote_core/tx_pool.h" |
| 36 | #include "cryptonote_core/blockchain.h" |
| 37 | #include "blockchain_db/testdb.h" |
| 38 | |
| 39 | static const uint64_t test_distribution[32] = { |
| 40 | 0, 0, 0, 0, 0, 1, 5, 1, 4, 0, 0, 1, 0, 1, 2, 3, 1, 0, 2, 0, 1, 3, 8, 1, 3, 5, 7, 1, 5, 0, 2, 3 |
| 41 | }; |
| 42 | static const size_t test_distribution_size = sizeof(test_distribution) / sizeof(test_distribution[0]); |
| 43 | |
| 44 | namespace |
| 45 | { |
| 46 | |
| 47 | class TestDB: public cryptonote::BaseTestDB |
| 48 | { |
| 49 | public: |
| 50 | TestDB(size_t bc_height = test_distribution_size): blockchain_height(bc_height) { m_open = true; } |
| 51 | virtual uint64_t height() const override { return blockchain_height; } |
| 52 | |
| 53 | std::vector<uint64_t> get_block_cumulative_rct_outputs(const std::vector<uint64_t> &heights) const override |
| 54 | { |
| 55 | std::vector<uint64_t> d; |
| 56 | for (uint64_t h: heights) |
| 57 | { |
| 58 | uint64_t c = 0; |
| 59 | for (uint64_t i = 0; i <= h; ++i) |
| 60 | c += test_distribution[i]; |
| 61 | d.push_back(c); |
| 62 | } |
| 63 | return d; |
| 64 | } |
| 65 | |
| 66 | std::vector<uint64_t> get_block_weights(uint64_t start_offset, size_t count) const override |
| 67 | { |
| 68 | std::vector<uint64_t> weights; |
| 69 | while (count--) weights.push_back(1); |
| 70 | return weights; |
| 71 | } |
| 72 | |
| 73 | uint64_t blockchain_height; |
| 74 | }; |
| 75 | |
| 76 | } |
| 77 | |
| 78 | bool get_output_distribution(uint64_t amount, uint64_t from, uint64_t to, uint64_t &start_height, std::vector<uint64_t> &distribution, uint64_t &base) |
| 79 | { |
| 80 | std::unique_ptr<cryptonote::Blockchain> bc; |
| 81 | cryptonote::tx_memory_pool txpool(*bc); |
| 82 | bc.reset(new cryptonote::Blockchain(txpool)); |
| 83 | struct get_test_options { |
| 84 | const std::pair<uint8_t, uint64_t> hard_forks[2]; |
| 85 | const cryptonote::test_options test_options = { |
| 86 | hard_forks |
| 87 | }; |
| 88 | get_test_options():hard_forks{std::make_pair((uint8_t)1, (uint64_t)0), std::make_pair((uint8_t)0, (uint64_t)0)}{} |
| 89 | } opts; |
| 90 | cryptonote::Blockchain *blockchain = bc.get(); |
| 91 | bool r = blockchain->init(new TestDB(test_distribution_size), cryptonote::FAKECHAIN, true, &opts.test_options, 0, NULL); |
| 92 | return r && bc->get_output_distribution(amount, from, to, start_height, distribution, base); |
| 93 | } |
| 94 | |
| 95 | crypto::hash get_block_hash(uint64_t height) |
| 96 | { |
| 97 | crypto::hash hash = crypto::null_hash; |
| 98 | *((uint64_t*)&hash) = height; |
| 99 | return hash; |
| 100 | } |
| 101 | |
| 102 | TEST(output_distribution, extend) |
| 103 | { |
| 104 | boost::optional<cryptonote::rpc::output_distribution_data> res; |
| 105 | |
| 106 | res = cryptonote::rpc::RpcHandler::get_output_distribution(::get_output_distribution, 0, 28, 29, ::get_block_hash, false, test_distribution_size); |
| 107 | ASSERT_TRUE(res != boost::none); |
| 108 | ASSERT_EQ(res->distribution.size(), 2); |
| 109 | ASSERT_EQ(res->distribution, std::vector<uint64_t>({5, 0})); |
| 110 | |
| 111 | res = cryptonote::rpc::RpcHandler::get_output_distribution(::get_output_distribution, 0, 28, 29, ::get_block_hash, true, test_distribution_size); |
| 112 | ASSERT_TRUE(res != boost::none); |
| 113 | ASSERT_EQ(res->distribution.size(), 2); |
| 114 | ASSERT_EQ(res->distribution, std::vector<uint64_t>({55, 55})); |
| 115 | |
| 116 | res = cryptonote::rpc::RpcHandler::get_output_distribution(::get_output_distribution, 0, 28, 30, ::get_block_hash, false, test_distribution_size); |
| 117 | ASSERT_TRUE(res != boost::none); |
| 118 | ASSERT_EQ(res->distribution.size(), 3); |
| 119 | ASSERT_EQ(res->distribution, std::vector<uint64_t>({5, 0, 2})); |
| 120 | |
| 121 | res = cryptonote::rpc::RpcHandler::get_output_distribution(::get_output_distribution, 0, 28, 30, ::get_block_hash, true, test_distribution_size); |
| 122 | ASSERT_TRUE(res != boost::none); |
| 123 | ASSERT_EQ(res->distribution.size(), 3); |
| 124 | ASSERT_EQ(res->distribution, std::vector<uint64_t>({55, 55, 57})); |
| 125 | |
| 126 | res = cryptonote::rpc::RpcHandler::get_output_distribution(::get_output_distribution, 0, 28, 31, ::get_block_hash, false, test_distribution_size); |
| 127 | ASSERT_TRUE(res != boost::none); |
| 128 | ASSERT_EQ(res->distribution.size(), 4); |
| 129 | ASSERT_EQ(res->distribution, std::vector<uint64_t>({5, 0, 2, 3})); |
| 130 | |
| 131 | res = cryptonote::rpc::RpcHandler::get_output_distribution(::get_output_distribution, 0, 28, 31, ::get_block_hash, true, test_distribution_size); |
| 132 | ASSERT_TRUE(res != boost::none); |
| 133 | ASSERT_EQ(res->distribution.size(), 4); |
| 134 | ASSERT_EQ(res->distribution, std::vector<uint64_t>({55, 55, 57, 60})); |
| 135 | } |
| 136 | |
| 137 | TEST(output_distribution, one) |
| 138 | { |
| 139 | boost::optional<cryptonote::rpc::output_distribution_data> res; |
| 140 | |
| 141 | res = cryptonote::rpc::RpcHandler::get_output_distribution(::get_output_distribution, 0, 0, 0, ::get_block_hash, false, test_distribution_size); |
| 142 | ASSERT_TRUE(res != boost::none); |
| 143 | ASSERT_EQ(res->distribution.size(), 1); |
| 144 | ASSERT_EQ(res->distribution.back(), 0); |
| 145 | } |
| 146 | |
| 147 | TEST(output_distribution, full_cumulative) |
| 148 | { |
| 149 | boost::optional<cryptonote::rpc::output_distribution_data> res; |
| 150 | |
| 151 | res = cryptonote::rpc::RpcHandler::get_output_distribution(::get_output_distribution, 0, 0, 31, ::get_block_hash, true, test_distribution_size); |
| 152 | ASSERT_TRUE(res != boost::none); |
| 153 | ASSERT_EQ(res->distribution.size(), 32); |
| 154 | ASSERT_EQ(res->distribution.back(), 60); |
| 155 | } |
| 156 | |
| 157 | TEST(output_distribution, full_noncumulative) |
| 158 | { |
| 159 | boost::optional<cryptonote::rpc::output_distribution_data> res; |
| 160 | |
| 161 | res = cryptonote::rpc::RpcHandler::get_output_distribution(::get_output_distribution, 0, 0, 31, ::get_block_hash, false, test_distribution_size); |
| 162 | ASSERT_TRUE(res != boost::none); |
| 163 | ASSERT_EQ(res->distribution.size(), 32); |
| 164 | for (size_t i = 0; i < 32; ++i) |
| 165 | ASSERT_EQ(res->distribution[i], test_distribution[i]); |
| 166 | } |
| 167 | |
| 168 | TEST(output_distribution, part_cumulative) |
| 169 | { |
| 170 | boost::optional<cryptonote::rpc::output_distribution_data> res; |
| 171 | |
| 172 | res = cryptonote::rpc::RpcHandler::get_output_distribution(::get_output_distribution, 0, 4, 8, ::get_block_hash, true, test_distribution_size); |
| 173 | ASSERT_TRUE(res != boost::none); |
| 174 | ASSERT_EQ(res->distribution.size(), 5); |
| 175 | ASSERT_EQ(res->distribution, std::vector<uint64_t>({0, 1, 6, 7, 11})); |
| 176 | } |
| 177 | |
| 178 | TEST(output_distribution, part_noncumulative) |
| 179 | { |
| 180 | boost::optional<cryptonote::rpc::output_distribution_data> res; |
| 181 | |
| 182 | res = cryptonote::rpc::RpcHandler::get_output_distribution(::get_output_distribution, 0, 4, 8, ::get_block_hash, false, test_distribution_size); |
| 183 | ASSERT_TRUE(res != boost::none); |
| 184 | ASSERT_EQ(res->distribution.size(), 5); |
| 185 | ASSERT_EQ(res->distribution, std::vector<uint64_t>({0, 1, 5, 1, 4})); |
| 186 | } |