|
| 1 | +// Copyright (c) 2026 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <qml/models/chainmodel.h> |
| 6 | + |
| 7 | +#include <uint256.h> |
| 8 | + |
| 9 | +#include <QtTest/QtTest> |
| 10 | + |
| 11 | +#include <cmath> |
| 12 | +#include <optional> |
| 13 | + |
| 14 | +class FakeChainDataSource final : public ChainModel::DataSource |
| 15 | +{ |
| 16 | +public: |
| 17 | + std::optional<int> m_height{}; |
| 18 | + bool m_find_first_block_result{false}; |
| 19 | + int m_first_block_height{0}; |
| 20 | + bool m_find_block_time_result{true}; |
| 21 | + int64_t m_last_min_time{0}; |
| 22 | + |
| 23 | + std::optional<int> getHeight() override |
| 24 | + { |
| 25 | + return m_height; |
| 26 | + } |
| 27 | + |
| 28 | + bool findFirstBlockWithTimeAndHeight(int64_t min_time, int /*min_height*/, int& first_block_height) override |
| 29 | + { |
| 30 | + m_last_min_time = min_time; |
| 31 | + if (!m_find_first_block_result) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + first_block_height = m_first_block_height; |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + uint256 getBlockHash(int height) override |
| 39 | + { |
| 40 | + return uint256(static_cast<uint8_t>(height)); |
| 41 | + } |
| 42 | + |
| 43 | + bool findBlockTime(const uint256& hash, int64_t& block_time) override |
| 44 | + { |
| 45 | + if (!m_find_block_time_result) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + const int height = static_cast<int>(hash.GetUint64(0)); |
| 50 | + block_time = m_last_min_time + (height * 60); |
| 51 | + return true; |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +class ChainModelTests : public QObject |
| 56 | +{ |
| 57 | + Q_OBJECT |
| 58 | + |
| 59 | +private Q_SLOTS: |
| 60 | + void setCurrentNetworkName_uppercases_and_emits_signal(); |
| 61 | + void setTimeRatioListInitial_without_chain_height_keeps_sentinel_entries(); |
| 62 | + void setTimeRatioListInitial_appends_recent_block_ratios(); |
| 63 | + void setTimeRatioList_rejects_times_before_meridian(); |
| 64 | + void setCurrentTimeRatio_updates_head_ratio_without_dropping_existing_entries(); |
| 65 | +}; |
| 66 | + |
| 67 | +void ChainModelTests::setCurrentNetworkName_uppercases_and_emits_signal() |
| 68 | +{ |
| 69 | + auto source = std::make_unique<FakeChainDataSource>(); |
| 70 | + ChainModel model(std::move(source), /*start_time_ratio_timer=*/false); |
| 71 | + |
| 72 | + QSignalSpy signal_spy(&model, &ChainModel::currentNetworkNameChanged); |
| 73 | + |
| 74 | + model.setCurrentNetworkName(QStringLiteral("testnet4")); |
| 75 | + |
| 76 | + QCOMPARE(model.currentNetworkName(), QStringLiteral("TESTNET4")); |
| 77 | + QCOMPARE(signal_spy.count(), 1); |
| 78 | +} |
| 79 | + |
| 80 | +void ChainModelTests::setTimeRatioListInitial_without_chain_height_keeps_sentinel_entries() |
| 81 | +{ |
| 82 | + auto source = std::make_unique<FakeChainDataSource>(); |
| 83 | + source->m_height = std::nullopt; |
| 84 | + |
| 85 | + ChainModel model(std::move(source), /*start_time_ratio_timer=*/false); |
| 86 | + QSignalSpy signal_spy(&model, &ChainModel::timeRatioListChanged); |
| 87 | + |
| 88 | + model.setTimeRatioListInitial(); |
| 89 | + |
| 90 | + QCOMPARE(signal_spy.count(), 1); |
| 91 | + |
| 92 | + const QVariantList ratios = model.timeRatioList(); |
| 93 | + QCOMPARE(ratios.size(), 2); |
| 94 | + QVERIFY(ratios[0].toDouble() >= 0.0); |
| 95 | + QVERIFY(ratios[0].toDouble() < 1.0); |
| 96 | + QCOMPARE(ratios[1].toDouble(), 0.0); |
| 97 | +} |
| 98 | + |
| 99 | +void ChainModelTests::setTimeRatioListInitial_appends_recent_block_ratios() |
| 100 | +{ |
| 101 | + auto source = std::make_unique<FakeChainDataSource>(); |
| 102 | + source->m_height = 3; |
| 103 | + source->m_find_first_block_result = true; |
| 104 | + source->m_first_block_height = 2; |
| 105 | + FakeChainDataSource* fake_source = source.get(); |
| 106 | + |
| 107 | + ChainModel model(std::move(source), /*start_time_ratio_timer=*/false); |
| 108 | + |
| 109 | + model.setTimeRatioListInitial(); |
| 110 | + |
| 111 | + const QVariantList ratios = model.timeRatioList(); |
| 112 | + QCOMPARE(ratios.size(), 4); |
| 113 | + |
| 114 | + const double expected_height_2_ratio = 120.0 / SECS_IN_12_HOURS; |
| 115 | + const double expected_height_3_ratio = 180.0 / SECS_IN_12_HOURS; |
| 116 | + |
| 117 | + QVERIFY(std::abs(ratios[2].toDouble() - expected_height_2_ratio) < 1e-12); |
| 118 | + QVERIFY(std::abs(ratios[3].toDouble() - expected_height_3_ratio) < 1e-12); |
| 119 | + QVERIFY(fake_source->m_last_min_time > 0); |
| 120 | +} |
| 121 | + |
| 122 | +void ChainModelTests::setTimeRatioList_rejects_times_before_meridian() |
| 123 | +{ |
| 124 | + auto source = std::make_unique<FakeChainDataSource>(); |
| 125 | + source->m_height = std::nullopt; |
| 126 | + |
| 127 | + ChainModel model(std::move(source), /*start_time_ratio_timer=*/false); |
| 128 | + model.setTimeRatioListInitial(); |
| 129 | + |
| 130 | + const int baseline_size = model.timeRatioList().size(); |
| 131 | + QSignalSpy signal_spy(&model, &ChainModel::timeRatioListChanged); |
| 132 | + |
| 133 | + model.setTimeRatioList(model.timestampAtMeridian() - 1); |
| 134 | + |
| 135 | + QCOMPARE(signal_spy.count(), 0); |
| 136 | + QCOMPARE(model.timeRatioList().size(), baseline_size); |
| 137 | +} |
| 138 | + |
| 139 | +void ChainModelTests::setCurrentTimeRatio_updates_head_ratio_without_dropping_existing_entries() |
| 140 | +{ |
| 141 | + auto source = std::make_unique<FakeChainDataSource>(); |
| 142 | + source->m_height = std::nullopt; |
| 143 | + |
| 144 | + ChainModel model(std::move(source), /*start_time_ratio_timer=*/false); |
| 145 | + model.setTimeRatioListInitial(); |
| 146 | + model.setTimeRatioList(model.timestampAtMeridian() + SECS_IN_12_HOURS + 5); |
| 147 | + |
| 148 | + const QVariantList before_update = model.timeRatioList(); |
| 149 | + QVERIFY(before_update.size() >= 3); |
| 150 | + |
| 151 | + model.setCurrentTimeRatio(); |
| 152 | + |
| 153 | + const QVariantList ratios = model.timeRatioList(); |
| 154 | + QCOMPARE(ratios.size(), before_update.size()); |
| 155 | + QVERIFY(ratios[0].toDouble() >= 0.0); |
| 156 | + QVERIFY(ratios[0].toDouble() < 1.0); |
| 157 | + QCOMPARE(ratios[1].toDouble(), 0.0); |
| 158 | + QCOMPARE(ratios.back().toDouble(), before_update.back().toDouble()); |
| 159 | +} |
| 160 | + |
| 161 | +int RunChainModelTests(int argc, char* argv[]) |
| 162 | +{ |
| 163 | + ChainModelTests tests; |
| 164 | + return QTest::qExec(&tests, argc, argv); |
| 165 | +} |
| 166 | + |
| 167 | +#ifndef BITCOINQML_NO_TEST_MAIN |
| 168 | +QTEST_MAIN(ChainModelTests) |
| 169 | +#endif |
| 170 | +#include "test_chainmodel.moc" |
0 commit comments