| 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 | // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 30 | |
| 31 | #include "gtest/gtest.h" |
| 32 | |
| 33 | #include "cryptonote_basic/cryptonote_basic_impl.h" |
| 34 | |
| 35 | using namespace cryptonote; |
| 36 | |
| 37 | namespace |
| 38 | { |
| 39 | //-------------------------------------------------------------------------------------------------------------------- |
| 40 | class block_reward_and_already_generated_coins : public ::testing::Test |
| 41 | { |
| 42 | protected: |
| 43 | static const size_t current_block_weight = CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1 / 2; |
| 44 | |
| 45 | bool m_block_not_too_big; |
| 46 | uint64_t m_block_reward; |
| 47 | }; |
| 48 | |
| 49 | #define TEST_ALREADY_GENERATED_COINS(already_generated_coins, expected_reward) \ |
| 50 | m_block_not_too_big = get_block_reward(0, current_block_weight, already_generated_coins, m_block_reward,1); \ |
| 51 | ASSERT_TRUE(m_block_not_too_big); \ |
| 52 | ASSERT_EQ(m_block_reward, expected_reward); |
| 53 | |
| 54 | TEST_F(block_reward_and_already_generated_coins, handles_first_values) |
| 55 | { |
| 56 | // 17592186044415 from neozaru, confirmed by fluffypony |
| 57 | TEST_ALREADY_GENERATED_COINS(0, UINT64_C(17592186044415)); |
| 58 | TEST_ALREADY_GENERATED_COINS(m_block_reward, UINT64_C(17592169267200)); |
| 59 | TEST_ALREADY_GENERATED_COINS(UINT64_C(2756434948434199641), UINT64_C(14963444829249)); |
| 60 | } |
| 61 | |
| 62 | TEST_F(block_reward_and_already_generated_coins, correctly_steps_from_2_to_1) |
| 63 | { |
| 64 | TEST_ALREADY_GENERATED_COINS(MONEY_SUPPLY - ((2 << 20) + 1), FINAL_SUBSIDY_PER_MINUTE); |
| 65 | TEST_ALREADY_GENERATED_COINS(MONEY_SUPPLY - (2 << 20) , FINAL_SUBSIDY_PER_MINUTE); |
| 66 | TEST_ALREADY_GENERATED_COINS(MONEY_SUPPLY - ((2 << 20) - 1), FINAL_SUBSIDY_PER_MINUTE); |
| 67 | } |
| 68 | |
| 69 | TEST_F(block_reward_and_already_generated_coins, handles_max) |
| 70 | { |
| 71 | TEST_ALREADY_GENERATED_COINS(MONEY_SUPPLY - ((1 << 20) + 1), FINAL_SUBSIDY_PER_MINUTE); |
| 72 | TEST_ALREADY_GENERATED_COINS(MONEY_SUPPLY - (1 << 20) , FINAL_SUBSIDY_PER_MINUTE); |
| 73 | TEST_ALREADY_GENERATED_COINS(MONEY_SUPPLY - ((1 << 20) - 1), FINAL_SUBSIDY_PER_MINUTE); |
| 74 | } |
| 75 | |
| 76 | //-------------------------------------------------------------------------------------------------------------------- |
| 77 | class block_reward_and_current_block_weight : public ::testing::Test |
| 78 | { |
| 79 | protected: |
| 80 | virtual void SetUp() |
| 81 | { |
| 82 | m_block_not_too_big = get_block_reward(0, 0, already_generated_coins, m_standard_block_reward, 1); |
| 83 | ASSERT_TRUE(m_block_not_too_big); |
| 84 | ASSERT_LT(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1, m_standard_block_reward); |
| 85 | } |
| 86 | |
| 87 | void do_test(size_t median_block_weight, size_t current_block_weight) |
| 88 | { |
| 89 | m_block_not_too_big = get_block_reward(median_block_weight, current_block_weight, already_generated_coins, m_block_reward, 1); |
| 90 | } |
| 91 | |
| 92 | static const uint64_t already_generated_coins = 0; |
| 93 | |
| 94 | bool m_block_not_too_big; |
| 95 | uint64_t m_block_reward; |
| 96 | uint64_t m_standard_block_reward; |
| 97 | }; |
| 98 | |
| 99 | TEST_F(block_reward_and_current_block_weight, handles_block_weight_less_relevance_level) |
| 100 | { |
| 101 | do_test(0, CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1 - 1); |
| 102 | ASSERT_TRUE(m_block_not_too_big); |
| 103 | ASSERT_EQ(m_block_reward, m_standard_block_reward); |
| 104 | } |
| 105 | |
| 106 | TEST_F(block_reward_and_current_block_weight, handles_block_weight_eq_relevance_level) |
| 107 | { |
| 108 | do_test(0, CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1); |
| 109 | ASSERT_TRUE(m_block_not_too_big); |
| 110 | ASSERT_EQ(m_block_reward, m_standard_block_reward); |
| 111 | } |
| 112 | |
| 113 | TEST_F(block_reward_and_current_block_weight, handles_block_weight_gt_relevance_level) |
| 114 | { |
| 115 | do_test(0, CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1 + 1); |
| 116 | ASSERT_TRUE(m_block_not_too_big); |
| 117 | ASSERT_LT(m_block_reward, m_standard_block_reward); |
| 118 | } |
| 119 | |
| 120 | TEST_F(block_reward_and_current_block_weight, handles_block_weight_less_2_relevance_level) |
| 121 | { |
| 122 | do_test(0, 2 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1 - 1); |
| 123 | ASSERT_TRUE(m_block_not_too_big); |
| 124 | ASSERT_LT(m_block_reward, m_standard_block_reward); |
| 125 | ASSERT_LT(0, m_block_reward); |
| 126 | } |
| 127 | |
| 128 | TEST_F(block_reward_and_current_block_weight, handles_block_weight_eq_2_relevance_level) |
| 129 | { |
| 130 | do_test(0, 2 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1); |
| 131 | ASSERT_TRUE(m_block_not_too_big); |
| 132 | ASSERT_EQ(0, m_block_reward); |
| 133 | } |
| 134 | |
| 135 | TEST_F(block_reward_and_current_block_weight, handles_block_weight_gt_2_relevance_level) |
| 136 | { |
| 137 | do_test(0, 2 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1 + 1); |
| 138 | ASSERT_FALSE(m_block_not_too_big); |
| 139 | } |
| 140 | |
| 141 | //-------------------------------------------------------------------------------------------------------------------- |
| 142 | class block_reward_and_last_block_weights : public ::testing::Test |
| 143 | { |
| 144 | protected: |
| 145 | virtual void SetUp() |
| 146 | { |
| 147 | m_last_block_weights.push_back(3 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1); |
| 148 | m_last_block_weights.push_back(5 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1); |
| 149 | m_last_block_weights.push_back(7 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1); |
| 150 | m_last_block_weights.push_back(11 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1); |
| 151 | m_last_block_weights.push_back(13 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1); |
| 152 | |
| 153 | m_last_block_weights_median = 7 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1; |
| 154 | |
| 155 | m_block_not_too_big = get_block_reward(epee::misc_utils::median(m_last_block_weights), 0, already_generated_coins, m_standard_block_reward, 1); |
| 156 | ASSERT_TRUE(m_block_not_too_big); |
| 157 | ASSERT_LT(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1, m_standard_block_reward); |
| 158 | } |
| 159 | |
| 160 | void do_test(size_t current_block_weight) |
| 161 | { |
| 162 | m_block_not_too_big = get_block_reward(epee::misc_utils::median(m_last_block_weights), current_block_weight, already_generated_coins, m_block_reward, 1); |
| 163 | } |
| 164 | |
| 165 | static const uint64_t already_generated_coins = 0; |
| 166 | |
| 167 | std::vector<size_t> m_last_block_weights; |
| 168 | uint64_t m_last_block_weights_median; |
| 169 | bool m_block_not_too_big; |
| 170 | uint64_t m_block_reward; |
| 171 | uint64_t m_standard_block_reward; |
| 172 | }; |
| 173 | |
| 174 | TEST_F(block_reward_and_last_block_weights, handles_block_weight_less_median) |
| 175 | { |
| 176 | do_test(m_last_block_weights_median - 1); |
| 177 | ASSERT_TRUE(m_block_not_too_big); |
| 178 | ASSERT_EQ(m_block_reward, m_standard_block_reward); |
| 179 | } |
| 180 | |
| 181 | TEST_F(block_reward_and_last_block_weights, handles_block_weight_eq_median) |
| 182 | { |
| 183 | do_test(m_last_block_weights_median); |
| 184 | ASSERT_TRUE(m_block_not_too_big); |
| 185 | ASSERT_EQ(m_block_reward, m_standard_block_reward); |
| 186 | } |
| 187 | |
| 188 | TEST_F(block_reward_and_last_block_weights, handles_block_weight_gt_median) |
| 189 | { |
| 190 | do_test(m_last_block_weights_median + 1); |
| 191 | ASSERT_TRUE(m_block_not_too_big); |
| 192 | ASSERT_LT(m_block_reward, m_standard_block_reward); |
| 193 | } |
| 194 | |
| 195 | TEST_F(block_reward_and_last_block_weights, handles_block_weight_less_2_medians) |
| 196 | { |
| 197 | do_test(2 * m_last_block_weights_median - 1); |
| 198 | ASSERT_TRUE(m_block_not_too_big); |
| 199 | ASSERT_LT(m_block_reward, m_standard_block_reward); |
| 200 | ASSERT_LT(0, m_block_reward); |
| 201 | } |
| 202 | |
| 203 | TEST_F(block_reward_and_last_block_weights, handles_block_weight_eq_2_medians) |
| 204 | { |
| 205 | do_test(2 * m_last_block_weights_median); |
| 206 | ASSERT_TRUE(m_block_not_too_big); |
| 207 | ASSERT_EQ(0, m_block_reward); |
| 208 | } |
| 209 | |
| 210 | TEST_F(block_reward_and_last_block_weights, handles_block_weight_gt_2_medians) |
| 211 | { |
| 212 | do_test(2 * m_last_block_weights_median + 1); |
| 213 | ASSERT_FALSE(m_block_not_too_big); |
| 214 | } |
| 215 | |
| 216 | TEST_F(block_reward_and_last_block_weights, calculates_correctly) |
| 217 | { |
| 218 | ASSERT_EQ(0, m_last_block_weights_median % 8); |
| 219 | |
| 220 | do_test(m_last_block_weights_median * 9 / 8); |
| 221 | ASSERT_TRUE(m_block_not_too_big); |
| 222 | ASSERT_EQ(m_block_reward, m_standard_block_reward * 63 / 64); |
| 223 | |
| 224 | // 3/2 = 12/8 |
| 225 | do_test(m_last_block_weights_median * 3 / 2); |
| 226 | ASSERT_TRUE(m_block_not_too_big); |
| 227 | ASSERT_EQ(m_block_reward, m_standard_block_reward * 3 / 4); |
| 228 | |
| 229 | do_test(m_last_block_weights_median * 15 / 8); |
| 230 | ASSERT_TRUE(m_block_not_too_big); |
| 231 | ASSERT_EQ(m_block_reward, m_standard_block_reward * 15 / 64); |
| 232 | } |
| 233 | } |