LCOV - code coverage report
Current view: top level - src - timedata.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 5 26 19.2 %
Date: 2023-11-10 23:46:46 Functions: 2 7 28.6 %
Branches: 3 12 25.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2014-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                 :            : #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                 :          0 : class CMedianFilter
      26                 :            : {
      27                 :            : private:
      28                 :            :     std::vector<T> vValues;
      29                 :            :     std::vector<T> vSorted;
      30                 :            :     unsigned int nSize;
      31                 :            : 
      32                 :            : public:
      33                 :          2 :     CMedianFilter(unsigned int _size, T initial_value) : nSize(_size)
      34                 :            :     {
      35         [ +  - ]:          2 :         vValues.reserve(_size);
      36         [ +  - ]:          2 :         vValues.push_back(initial_value);
      37         [ +  - ]:          2 :         vSorted = vValues;
      38                 :          2 :     }
      39                 :            : 
      40                 :          0 :     void input(T value)
      41                 :            :     {
      42         [ #  # ]:          0 :         if (vValues.size() == nSize) {
      43                 :          0 :             vValues.erase(vValues.begin());
      44                 :          0 :         }
      45                 :          0 :         vValues.push_back(value);
      46                 :            : 
      47                 :          0 :         vSorted.resize(vValues.size());
      48                 :          0 :         std::copy(vValues.begin(), vValues.end(), vSorted.begin());
      49                 :          0 :         std::sort(vSorted.begin(), vSorted.end());
      50                 :          0 :     }
      51                 :            : 
      52                 :          0 :     T median() const
      53                 :            :     {
      54                 :          0 :         int vSortedSize = vSorted.size();
      55         [ #  # ]:          0 :         assert(vSortedSize > 0);
      56         [ #  # ]:          0 :         if (vSortedSize & 1) // Odd number of elements
      57                 :            :         {
      58                 :          0 :             return vSorted[vSortedSize / 2];
      59                 :            :         } else // Even number of elements
      60                 :            :         {
      61                 :          0 :             return (vSorted[vSortedSize / 2 - 1] + vSorted[vSortedSize / 2]) / 2;
      62                 :            :         }
      63                 :          0 :     }
      64                 :            : 
      65                 :          0 :     int size() const
      66                 :            :     {
      67                 :          0 :         return vValues.size();
      68                 :            :     }
      69                 :            : 
      70                 :          0 :     std::vector<T> sorted() const
      71                 :            :     {
      72                 :          0 :         return vSorted;
      73                 :            :     }
      74                 :            : };
      75                 :            : 
      76                 :            : /** Functions to keep track of adjusted P2P time */
      77                 :            : int64_t GetTimeOffset();
      78                 :            : NodeClock::time_point 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.14