rippled
TimeKeeper.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_CORE_TIMEKEEPER_H_INCLUDED
21 #define RIPPLE_CORE_TIMEKEEPER_H_INCLUDED
22 
23 #include <ripple/basics/chrono.h>
24 #include <ripple/beast/clock/abstract_clock.h>
25 
26 #include <atomic>
27 
28 namespace ripple {
29 
31 class TimeKeeper : public beast::abstract_clock<NetClock>
32 {
33 private:
35 
36  // Adjust system_clock::time_point for NetClock epoch
37  static constexpr time_point
38  adjust(std::chrono::system_clock::time_point when)
39  {
40  return time_point(std::chrono::duration_cast<duration>(
41  when.time_since_epoch() - epoch_offset));
42  }
43 
44 public:
45  virtual ~TimeKeeper() = default;
46 
63  [[nodiscard]] time_point
64  now() const override
65  {
67  }
68 
75  [[nodiscard]] time_point
76  closeTime() const
77  {
78  return now() + closeOffset_.load();
79  }
80 
81  // This may return a negative value
82  [[nodiscard]] std::chrono::seconds
83  closeOffset() const
84  {
85  return closeOffset_.load();
86  }
87 
91  {
92  using namespace std::chrono_literals;
93 
94  auto offset = closeOffset_.load();
95 
96  if (by == 0s && offset == 0s)
97  return offset;
98 
99  // The close time adjustment is serialized externally to this
100  // code. The compare/exchange only serves as a weak check and
101  // should not fail. Even if it does, it's safe to simply just
102  // skip the adjustment.
103  closeOffset_.compare_exchange_strong(offset, [by, offset]() {
104  // Ignore small offsets and push the close time
105  // towards our wall time.
106  if (by > 1s)
107  return offset + ((by + 3s) / 4);
108 
109  if (by < -1s)
110  return offset + ((by - 3s) / 4);
111 
112  return (offset * 3) / 4;
113  }());
114 
115  return closeOffset_.load();
116  }
117 };
118 
119 } // namespace ripple
120 
121 #endif
std::chrono::seconds
ripple::TimeKeeper::closeOffset
std::chrono::seconds closeOffset() const
Definition: TimeKeeper.h:83
std::atomic::compare_exchange_strong
T compare_exchange_strong(T... args)
ripple::TimeKeeper::closeTime
time_point closeTime() const
Returns the predicted close time, in network time.
Definition: TimeKeeper.h:76
ripple::epoch_offset
constexpr static std::chrono::seconds epoch_offset
Clock for measuring the network time.
Definition: chrono.h:56
ripple::TimeKeeper::adjustCloseTime
std::chrono::seconds adjustCloseTime(std::chrono::seconds by)
Adjust the close time, based on the network's view of time.
Definition: TimeKeeper.h:90
std::atomic::load
T load(T... args)
atomic
ripple::TimeKeeper
Manages various times used by the server.
Definition: TimeKeeper.h:31
ripple::TimeKeeper::now
time_point now() const override
Returns the current time, using the server's clock.
Definition: TimeKeeper.h:64
beast::abstract_clock
Abstract interface to a clock.
Definition: abstract_clock.h:57
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::TimeKeeper::~TimeKeeper
virtual ~TimeKeeper()=default
ripple::TimeKeeper::closeOffset_
std::atomic< std::chrono::seconds > closeOffset_
Definition: TimeKeeper.h:34
ripple::TimeKeeper::adjust
static constexpr time_point adjust(std::chrono::system_clock::time_point when)
Definition: TimeKeeper.h:38
beast::abstract_clock< NetClock >::time_point
typename NetClock ::time_point time_point
Definition: abstract_clock.h:63
std::chrono::system_clock::now
T now(T... args)