| 1 | // Copyright (c) 2019-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 | // References: |
| 30 | // - https://github.com/ArticMine/Monero-Documents/blob/master/MoneroScaling2021.pdf |
| 31 | // - https://github.com/monero-project/research-lab/issues/70 |
| 32 | |
| 33 | #define IN_UNIT_TESTS |
| 34 | |
| 35 | #include "gtest/gtest.h" |
| 36 | #include "cryptonote_core/blockchain.h" |
| 37 | #include "cryptonote_core/tx_pool.h" |
| 38 | #include "cryptonote_core/cryptonote_core.h" |
| 39 | #include "blockchain_db/testdb.h" |
| 40 | |
| 41 | namespace |
| 42 | { |
| 43 | |
| 44 | class TestDB: public cryptonote::BaseTestDB |
| 45 | { |
| 46 | public: |
| 47 | TestDB() { m_open = true; } |
| 48 | }; |
| 49 | |
| 50 | } |
| 51 | |
| 52 | #define PREFIX_WINDOW(hf_version,window) \ |
| 53 | std::unique_ptr<cryptonote::Blockchain> bc; \ |
| 54 | cryptonote::tx_memory_pool txpool(*bc); \ |
| 55 | bc.reset(new cryptonote::Blockchain(txpool)); \ |
| 56 | struct get_test_options { \ |
| 57 | const std::pair<uint8_t, uint64_t> hard_forks[3]; \ |
| 58 | const cryptonote::test_options test_options = { \ |
| 59 | hard_forks, \ |
| 60 | window, \ |
| 61 | }; \ |
| 62 | get_test_options(): hard_forks{std::make_pair(1, (uint64_t)0), std::make_pair((uint8_t)hf_version, (uint64_t)1), std::make_pair((uint8_t)0, (uint64_t)0)} {} \ |
| 63 | } opts; \ |
| 64 | cryptonote::Blockchain *blockchain = bc.get(); \ |
| 65 | bool r = blockchain->init(new TestDB(), cryptonote::FAKECHAIN, true, &opts.test_options, 0, NULL); \ |
| 66 | ASSERT_TRUE(r) |
| 67 | |
| 68 | #define PREFIX(hf_version) PREFIX_WINDOW(hf_version, TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW) |
| 69 | |
| 70 | TEST(fee_2021_scaling, relay_fee_cases_from_pdf) |
| 71 | { |
| 72 | PREFIX_WINDOW(HF_VERSION_2021_SCALING, CRYPTONOTE_LONG_TERM_BLOCK_WEIGHT_WINDOW_SIZE); |
| 73 | |
| 74 | ASSERT_EQ(bc->get_dynamic_base_fee(1200000000000, 300000, HF_VERSION_2021_SCALING-1), 8000); |
| 75 | ASSERT_EQ(bc->get_dynamic_base_fee(1200000000000, 300000, HF_VERSION_2021_SCALING), 38000); |
| 76 | ASSERT_EQ(bc->get_dynamic_base_fee(1200000000000, 1425000, HF_VERSION_2021_SCALING-1), 1684 /*1680*/); |
| 77 | ASSERT_EQ(bc->get_dynamic_base_fee(1200000000000, 1425000, HF_VERSION_2021_SCALING), 1684 /*1680*/); |
| 78 | ASSERT_EQ(bc->get_dynamic_base_fee(1200000000000, 1500000, HF_VERSION_2021_SCALING-1), 1600); |
| 79 | ASSERT_EQ(bc->get_dynamic_base_fee(1200000000000, 1500000, HF_VERSION_2021_SCALING), 1520); |
| 80 | |
| 81 | ASSERT_EQ(bc->get_dynamic_base_fee(600000000000, 300000, HF_VERSION_2021_SCALING-1), 4000); |
| 82 | ASSERT_EQ(bc->get_dynamic_base_fee(600000000000, 300000, HF_VERSION_2021_SCALING), 19000); |
| 83 | ASSERT_EQ(bc->get_dynamic_base_fee(600000000000, 1425000, HF_VERSION_2021_SCALING-1), 842 /*840*/); |
| 84 | ASSERT_EQ(bc->get_dynamic_base_fee(600000000000, 1425000, HF_VERSION_2021_SCALING), 842 /*840*/); |
| 85 | ASSERT_EQ(bc->get_dynamic_base_fee(600000000000, 1500000, HF_VERSION_2021_SCALING-1), 800); |
| 86 | ASSERT_EQ(bc->get_dynamic_base_fee(600000000000, 1500000, HF_VERSION_2021_SCALING), 760); |
| 87 | } |
| 88 | |
| 89 | TEST(fee_2021_scaling, wallet_fee_cases_from_pdf) |
| 90 | { |
| 91 | PREFIX_WINDOW(HF_VERSION_2021_SCALING, CRYPTONOTE_LONG_TERM_BLOCK_WEIGHT_WINDOW_SIZE); |
| 92 | std::vector<uint64_t> fees; |
| 93 | |
| 94 | fees.clear(); |
| 95 | bc->get_dynamic_base_fee_estimate_2021_scaling(10, 600000000000, 300000, 300000, fees); |
| 96 | ASSERT_EQ(fees.size(), 4); |
| 97 | ASSERT_EQ(fees[0], 20000); |
| 98 | ASSERT_EQ(fees[1], 80000); |
| 99 | ASSERT_EQ(fees[2], 320000); |
| 100 | ASSERT_EQ(fees[3], 4000000); |
| 101 | |
| 102 | fees.clear(); |
| 103 | bc->get_dynamic_base_fee_estimate_2021_scaling(10, 600000000000, 15000000, 300000, fees); |
| 104 | ASSERT_EQ(fees.size(), 4); |
| 105 | ASSERT_EQ(fees[0], 20000); |
| 106 | ASSERT_EQ(fees[1], 80000); |
| 107 | ASSERT_EQ(fees[2], 320000); |
| 108 | ASSERT_EQ(fees[3], 1300000); |
| 109 | |
| 110 | fees.clear(); |
| 111 | bc->get_dynamic_base_fee_estimate_2021_scaling(10, 600000000000, 1425000, 1425000, fees); |
| 112 | ASSERT_EQ(fees.size(), 4); |
| 113 | ASSERT_EQ(fees[0], 890); |
| 114 | ASSERT_EQ(fees[1], 3600); |
| 115 | ASSERT_EQ(fees[2], 68000); |
| 116 | ASSERT_EQ(fees[3], 850000 /* 842000 */); |
| 117 | |
| 118 | fees.clear(); |
| 119 | bc->get_dynamic_base_fee_estimate_2021_scaling(10, 600000000000, 1500000, 1500000, fees); |
| 120 | ASSERT_EQ(fees.size(), 4); |
| 121 | ASSERT_EQ(fees[0], 800); |
| 122 | ASSERT_EQ(fees[1], 3200); |
| 123 | ASSERT_EQ(fees[2], 64000); |
| 124 | ASSERT_EQ(fees[3], 800000); |
| 125 | |
| 126 | fees.clear(); |
| 127 | bc->get_dynamic_base_fee_estimate_2021_scaling(10, 600000000000, 75000000, 1500000, fees); |
| 128 | ASSERT_EQ(fees.size(), 4); |
| 129 | ASSERT_EQ(fees[0], 800); |
| 130 | ASSERT_EQ(fees[1], 3200); |
| 131 | ASSERT_EQ(fees[2], 64000); |
| 132 | ASSERT_EQ(fees[3], 260000); |
| 133 | } |
| 134 | |
| 135 | TEST(fee_2021_scaling, rounding) |
| 136 | { |
| 137 | ASSERT_EQ(cryptonote::round_money_up("27810", 3), "27900.000000000000"); |
| 138 | ASSERT_EQ(cryptonote::round_money_up("37.94", 3), "38.000000000000"); |
| 139 | ASSERT_EQ(cryptonote::round_money_up("0.5555", 3), "0.556000000000"); |
| 140 | ASSERT_EQ(cryptonote::round_money_up("0.002342", 3), "0.002350000000"); |
| 141 | |
| 142 | ASSERT_EQ(cryptonote::round_money_up("27810", 2), "28000.000000000000"); |
| 143 | ASSERT_EQ(cryptonote::round_money_up("37.94", 2), "38.000000000000"); |
| 144 | ASSERT_EQ(cryptonote::round_money_up("0.5555", 2), "0.560000000000"); |
| 145 | ASSERT_EQ(cryptonote::round_money_up("0.002342", 2), "0.002400000000"); |
| 146 | |
| 147 | ASSERT_EQ(cryptonote::round_money_up("0", 8), "0.000000000000"); |
| 148 | ASSERT_EQ(cryptonote::round_money_up("0.0", 8), "0.000000000000"); |
| 149 | ASSERT_EQ(cryptonote::round_money_up("50.0", 8), "50.000000000000"); |
| 150 | ASSERT_EQ(cryptonote::round_money_up("0.002342", 8), "0.002342000000"); |
| 151 | ASSERT_EQ(cryptonote::round_money_up("0.002342", 1), "0.003000000000"); |
| 152 | ASSERT_EQ(cryptonote::round_money_up("12345", 8), "12345.000000000000"); |
| 153 | ASSERT_EQ(cryptonote::round_money_up("45678", 1), "50000.000000000000"); |
| 154 | ASSERT_EQ(cryptonote::round_money_up("1.234", 1), "2.000000000000"); |
| 155 | ASSERT_EQ(cryptonote::round_money_up("1.0000001", 4), "1.001000000000"); |
| 156 | ASSERT_EQ(cryptonote::round_money_up("1.0020001", 4), "1.003000000000"); |
| 157 | |
| 158 | ASSERT_EQ(cryptonote::round_money_up("1.999999", 1), "2.000000000000"); |
| 159 | ASSERT_EQ(cryptonote::round_money_up("1.999999", 2), "2.000000000000"); |
| 160 | ASSERT_EQ(cryptonote::round_money_up("1.999999", 3), "2.000000000000"); |
| 161 | ASSERT_EQ(cryptonote::round_money_up("1.999999", 4), "2.000000000000"); |
| 162 | ASSERT_EQ(cryptonote::round_money_up("1.999999", 5), "2.000000000000"); |
| 163 | ASSERT_EQ(cryptonote::round_money_up("1.999999", 6), "2.000000000000"); |
| 164 | ASSERT_EQ(cryptonote::round_money_up("1.999999", 7), "1.999999000000"); |
| 165 | ASSERT_EQ(cryptonote::round_money_up("1.999999", 8), "1.999999000000"); |
| 166 | ASSERT_EQ(cryptonote::round_money_up("1.999999", 9), "1.999999000000"); |
| 167 | |
| 168 | ASSERT_EQ(cryptonote::round_money_up("2.000001", 1), "3.000000000000"); |
| 169 | ASSERT_EQ(cryptonote::round_money_up("2.000001", 2), "2.100000000000"); |
| 170 | ASSERT_EQ(cryptonote::round_money_up("2.000001", 3), "2.010000000000"); |
| 171 | ASSERT_EQ(cryptonote::round_money_up("2.000001", 4), "2.001000000000"); |
| 172 | ASSERT_EQ(cryptonote::round_money_up("2.000001", 5), "2.000100000000"); |
| 173 | ASSERT_EQ(cryptonote::round_money_up("2.000001", 6), "2.000010000000"); |
| 174 | ASSERT_EQ(cryptonote::round_money_up("2.000001", 7), "2.000001000000"); |
| 175 | ASSERT_EQ(cryptonote::round_money_up("2.000001", 8), "2.000001000000"); |
| 176 | ASSERT_EQ(cryptonote::round_money_up("2.000001", 9), "2.000001000000"); |
| 177 | ASSERT_EQ(cryptonote::round_money_up("2.000001", 4000), "2.000001000000"); |
| 178 | |
| 179 | ASSERT_EQ(cryptonote::round_money_up("999", 2), "1000.000000000000"); |
| 180 | |
| 181 | ASSERT_THROW(cryptonote::round_money_up("1.23", 0), std::runtime_error); |
| 182 | ASSERT_THROW(cryptonote::round_money_up("18446744.073709551615", 1), std::runtime_error); |
| 183 | ASSERT_THROW(cryptonote::round_money_up("18446744.073709551615", 2), std::runtime_error); |
| 184 | ASSERT_THROW(cryptonote::round_money_up("18446744.073709551615", 12), std::runtime_error); |
| 185 | ASSERT_THROW(cryptonote::round_money_up("18446744.073709551615", 19), std::runtime_error); |
| 186 | ASSERT_EQ(cryptonote::round_money_up("18446744.073709551615", 20), "18446744.073709551615"); |
| 187 | } |