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 #include <atomic>
26 
27 namespace ripple {
28 
30 class TimeKeeper : public beast::abstract_clock<NetClock>
31 {
32 private:
34 
35  // Adjust system_clock::time_point for NetClock epoch
36  static constexpr time_point
37  adjust(std::chrono::system_clock::time_point when)
38  {
39  return time_point(std::chrono::duration_cast<duration>(
40  when.time_since_epoch() - days(10957)));
41  }
42 
43 public:
44  virtual ~TimeKeeper() = default;
45 
62  [[nodiscard]] time_point
63  now() const override
64  {
66  }
67 
74  [[nodiscard]] time_point
75  closeTime() const
76  {
77  return now() + closeOffset_.load();
78  }
79 
80  // This may return a negative value
81  [[nodiscard]] std::chrono::seconds
82  closeOffset() const
83  {
84  return closeOffset_.load();
85  }
86 
90  {
91  using namespace std::chrono_literals;
92 
93  auto offset = closeOffset_.load();
94 
95  if (by == 0s && offset == 0s)
96  return offset;
97 
98  // The close time adjustment is serialized externally to this
99  // code. The compare/exchange only serves as a weak check and
100  // should not fail. Even if it does, it's safe to simply just
101  // skip the adjustment.
102  closeOffset_.compare_exchange_strong(offset, [by, offset]() {
103  // Ignore small offsets and push the close time
104  // towards our wall time.
105  if (by > 1s)
106  return offset + ((by + 3s) / 4);
107 
108  if (by < -1s)
109  return offset + ((by - 3s) / 4);
110 
111  return (offset * 3) / 4;
112  }());
113 
114  return closeOffset_.load();
115  }
116 };
117 
118 } // namespace ripple
119 
120 #endif
std::chrono::duration
ripple::TimeKeeper::closeOffset
std::chrono::seconds closeOffset() const
Definition: TimeKeeper.h:82
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:75
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:89
std::atomic::load
T load(T... args)
atomic
ripple::TimeKeeper
Manages various times used by the server.
Definition: TimeKeeper.h:30
ripple::TimeKeeper::now
time_point now() const override
Returns the current time, using the server's clock.
Definition: TimeKeeper.h:63
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:33
ripple::TimeKeeper::adjust
static constexpr time_point adjust(std::chrono::system_clock::time_point when)
Definition: TimeKeeper.h:37
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)