LCOV - code coverage report
Current view: top level - src - timedata.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 26 26 100.0 %
Date: 2026-06-25 07:23:43 Functions: 15 15 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2014 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             : #ifndef BITCOIN_TIMEDATA_H
       6             : #define BITCOIN_TIMEDATA_H
       7             : 
       8             : #include <util/time.h>
       9             : 
      10             : #include <algorithm>
      11             : #include <cassert>
      12             : #include <chrono>
      13             : #include <cstdint>
      14             : #include <vector>
      15             : 
      16             : static const int64_t DEFAULT_MAX_TIME_ADJUSTMENT = 70 * 60;
      17             : 
      18             : class CNetAddr;
      19             : 
      20             : /**
      21             :  * Median filter over a stream of values.
      22             :  * Returns the median of the last N numbers
      23             :  */
      24             : template <typename T>
      25             : class CMedianFilter
      26             : {
      27             : private:
      28             :     std::vector<T> vValues;
      29             :     std::vector<T> vSorted;
      30             :     unsigned int nSize;
      31             : 
      32             : public:
      33        6624 :     CMedianFilter(unsigned int _size, T initial_value) : nSize(_size)
      34        3312 :     {
      35        3312 :         vValues.reserve(_size);
      36        3312 :         vValues.push_back(initial_value);
      37        3312 :         vSorted = vValues;
      38        6624 :     }
      39             : 
      40        1565 :     void input(T value)
      41             :     {
      42        1565 :         if (vValues.size() == nSize) {
      43           3 :             vValues.erase(vValues.begin());
      44           3 :         }
      45        1565 :         vValues.push_back(value);
      46             : 
      47        1565 :         vSorted.resize(vValues.size());
      48        1565 :         std::copy(vValues.begin(), vValues.end(), vSorted.begin());
      49        1565 :         std::sort(vSorted.begin(), vSorted.end());
      50        1565 :     }
      51             : 
      52         105 :     T median() const
      53             :     {
      54         105 :         int vSortedSize = vSorted.size();
      55         105 :         assert(vSortedSize > 0);
      56         105 :         if (vSortedSize & 1) // Odd number of elements
      57             :         {
      58         103 :             return vSorted[vSortedSize / 2];
      59             :         } else // Even number of elements
      60             :         {
      61           2 :             return (vSorted[vSortedSize / 2 - 1] + vSorted[vSortedSize / 2]) / 2;
      62             :         }
      63         105 :     }
      64             : 
      65        3315 :     int size() const
      66             :     {
      67        3315 :         return vValues.size();
      68             :     }
      69             : 
      70          98 :     std::vector<T> sorted() const
      71             :     {
      72          98 :         return vSorted;
      73             :     }
      74             : };
      75             : 
      76             : /** Functions to keep track of adjusted P2P time */
      77             : int64_t GetTimeOffset();
      78             : int64_t GetAdjustedTime();
      79             : void AddTimeData(const CNetAddr& ip, int64_t nTime);
      80             : 
      81             : /**
      82             :  * Reset the internal state of GetTimeOffset(), GetAdjustedTime() and AddTimeData().
      83             :  */
      84             : void TestOnlyResetTimeData();
      85             : 
      86             : #endif // BITCOIN_TIMEDATA_H

Generated by: LCOV version 1.16