|
| 1 | +// Copyright (c) 2022 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/BitcoinApp/chainmodel.h> |
| 6 | + |
| 7 | +#include <QDateTime> |
| 8 | +#include <QThread> |
| 9 | +#include <QTime> |
| 10 | +#include <interfaces/chain.h> |
| 11 | + |
| 12 | +ChainModel::ChainModel(interfaces::Chain& chain) |
| 13 | + : m_chain{chain} |
| 14 | +{ |
| 15 | + QTimer* timer = new QTimer(); |
| 16 | + connect(timer, &QTimer::timeout, this, &ChainModel::setCurrentTimeRatio); |
| 17 | + timer->start(1000); |
| 18 | + |
| 19 | + QThread* timer_thread = new QThread; |
| 20 | + timer->moveToThread(timer_thread); |
| 21 | + timer_thread->start(); |
| 22 | +} |
| 23 | + |
| 24 | +void ChainModel::setTimeRatioList(int new_time) |
| 25 | +{ |
| 26 | + if (m_time_ratio_list.isEmpty()) { |
| 27 | + setTimeRatioListInitial(); |
| 28 | + } |
| 29 | + int time_at_meridian = timestampAtMeridian(); |
| 30 | + |
| 31 | + if (new_time < time_at_meridian) { |
| 32 | + return; |
| 33 | + } |
| 34 | + m_time_ratio_list.push_back(double(new_time - time_at_meridian) / SECS_IN_12_HOURS); |
| 35 | + |
| 36 | + Q_EMIT timeRatioListChanged(); |
| 37 | +} |
| 38 | + |
| 39 | +int ChainModel::timestampAtMeridian() |
| 40 | +{ |
| 41 | + int secs_since_meridian = (QTime::currentTime().msecsSinceStartOfDay() / 1000) % SECS_IN_12_HOURS; |
| 42 | + int current_timestamp = QDateTime::currentSecsSinceEpoch(); |
| 43 | + |
| 44 | + return current_timestamp - secs_since_meridian; |
| 45 | +} |
| 46 | + |
| 47 | +void ChainModel::setTimeRatioListInitial() |
| 48 | +{ |
| 49 | + int time_at_meridian = timestampAtMeridian(); |
| 50 | + m_time_ratio_list.clear(); |
| 51 | + /* m_time_ratio_list[0] = current_time_ratio |
| 52 | + * m_time_ratio_list[1] = 0 |
| 53 | + * These two positions remain fixed for these |
| 54 | + * values in m_time_ratio_list */ |
| 55 | + m_time_ratio_list.push_back(double(QDateTime::currentSecsSinceEpoch() - time_at_meridian) / SECS_IN_12_HOURS); |
| 56 | + m_time_ratio_list.push_back(0); |
| 57 | + |
| 58 | + int first_block_height; |
| 59 | + int active_chain_height = m_chain.getHeight().value(); |
| 60 | + bool success = m_chain.findFirstBlockWithTimeAndHeight(/*min_time=*/time_at_meridian, /*min_height=*/0, interfaces::FoundBlock().height(first_block_height)); |
| 61 | + |
| 62 | + if (!success) { |
| 63 | + Q_EMIT timeRatioListChanged(); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + for (int height = first_block_height; height < active_chain_height + 1; height++) { |
| 68 | + m_time_ratio_list.push_back(double(m_chain.getBlockTime(height) - time_at_meridian) / SECS_IN_12_HOURS); |
| 69 | + } |
| 70 | + |
| 71 | + Q_EMIT timeRatioListChanged(); |
| 72 | +} |
| 73 | + |
| 74 | +void ChainModel::setCurrentTimeRatio() |
| 75 | +{ |
| 76 | + int secs_since_meridian = (QTime::currentTime().msecsSinceStartOfDay() / 1000) % SECS_IN_12_HOURS; |
| 77 | + double current_time_ratio = double(secs_since_meridian) / SECS_IN_12_HOURS; |
| 78 | + |
| 79 | + if (current_time_ratio < m_time_ratio_list[0].toDouble()) { // That means time has crossed a meridian |
| 80 | + m_time_ratio_list.clear(); |
| 81 | + } |
| 82 | + |
| 83 | + if (m_time_ratio_list.isEmpty()) { |
| 84 | + m_time_ratio_list.push_back(current_time_ratio); |
| 85 | + m_time_ratio_list.push_back(0); |
| 86 | + } else { |
| 87 | + m_time_ratio_list[0] = current_time_ratio; |
| 88 | + } |
| 89 | + |
| 90 | + Q_EMIT timeRatioListChanged(); |
| 91 | +} |
0 commit comments