rippled
Loading...
Searching...
No Matches
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 <xrpl/basics/chrono.h>
24#include <xrpl/beast/clock/abstract_clock.h>
25
26#include <atomic>
27
28namespace ripple {
29
31class TimeKeeper : public beast::abstract_clock<NetClock>
32{
33private:
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
44public:
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
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
Abstract interface to a clock.
typename Clock::time_point time_point
Manages various times used by the server.
Definition TimeKeeper.h:32
std::atomic< std::chrono::seconds > closeOffset_
Definition TimeKeeper.h:34
static constexpr time_point adjust(std::chrono::system_clock::time_point when)
Definition TimeKeeper.h:38
time_point now() const override
Returns the current time, using the server's clock.
Definition TimeKeeper.h:64
std::chrono::seconds closeOffset() const
Definition TimeKeeper.h:83
virtual ~TimeKeeper()=default
std::chrono::seconds adjustCloseTime(std::chrono::seconds by)
Adjust the close time, based on the network's view of time.
Definition TimeKeeper.h:90
time_point closeTime() const
Returns the predicted close time, in network time.
Definition TimeKeeper.h:76
T compare_exchange_strong(T... args)
T load(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
static constexpr std::chrono::seconds epoch_offset
Clock for measuring the network time.
Definition chrono.h:55