rippled
TimeKeeper.cpp
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 #include <ripple/basics/Log.h>
21 #include <ripple/core/TimeKeeper.h>
22 #include <ripple/core/impl/SNTPClock.h>
23 #include <memory>
24 #include <mutex>
25 
26 namespace ripple {
27 
28 class TimeKeeperImpl : public TimeKeeper
29 {
30 private:
32  std::mutex mutable mutex_;
35 
36  // Adjust system_clock::time_point for NetClock epoch
37  static 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() - days(10957)));
42  }
43 
44 public:
47  {
48  }
49 
50  void
51  run(std::vector<std::string> const& servers) override
52  {
53  clock_->run(servers);
54  }
55 
57  now() const override
58  {
59  std::lock_guard lock(mutex_);
60  return adjust(clock_->now());
61  }
62 
64  closeTime() const override
65  {
66  std::lock_guard lock(mutex_);
67  return adjust(clock_->now()) + closeOffset_;
68  }
69 
70  void
72  {
73  using namespace std::chrono;
74  auto const s = amount.count();
75  std::lock_guard lock(mutex_);
76  // Take large offsets, ignore small offsets,
77  // push the close time towards our wall time.
78  if (s > 1)
79  closeOffset_ += seconds((s + 3) / 4);
80  else if (s < -1)
81  closeOffset_ += seconds((s - 3) / 4);
82  else
83  closeOffset_ = (closeOffset_ * 3) / 4;
84  if (closeOffset_.count() != 0)
85  {
86  if (std::abs(closeOffset_.count()) < 60)
87  {
88  JLOG(j_.info()) << "TimeKeeper: Close time offset now "
89  << closeOffset_.count();
90  }
91  else
92  {
93  JLOG(j_.warn()) << "TimeKeeper: Large close time offset = "
94  << closeOffset_.count();
95  }
96  }
97  }
98 
100  nowOffset() const override
101  {
102  using namespace std::chrono;
103  using namespace std;
105  return duration_cast<chrono::duration<int32_t>>(clock_->offset());
106  }
107 
109  closeOffset() const override
110  {
112  return closeOffset_;
113  }
114 };
115 
116 //------------------------------------------------------------------------------
117 
120 {
121  return std::make_unique<TimeKeeperImpl>(j);
122 }
123 
124 } // namespace ripple
std::lock
T lock(T... args)
ripple::TimeKeeperImpl::adjust
static time_point adjust(std::chrono::system_clock::time_point when)
Definition: TimeKeeper.cpp:38
std::vector< std::string >
ripple::TimeKeeperImpl::closeOffset_
std::chrono::duration< std::int32_t > closeOffset_
Definition: TimeKeeper.cpp:33
std::chrono::duration< std::int32_t >
ripple::TimeKeeperImpl::closeTime
time_point closeTime() const override
Returns the close time, in network time.
Definition: TimeKeeper.cpp:64
beast::Journal::warn
Stream warn() const
Definition: Journal.h:327
std::lock_guard
STL class.
ripple::TimeKeeperImpl::mutex_
std::mutex mutex_
Definition: TimeKeeper.cpp:32
ripple::TimeKeeperImpl::j_
const beast::Journal j_
Definition: TimeKeeper.cpp:31
ripple::TimeKeeperImpl::nowOffset
std::chrono::duration< std::int32_t > nowOffset() const override
Definition: TimeKeeper.cpp:100
ripple::make_SNTPClock
std::unique_ptr< SNTPClock > make_SNTPClock(beast::Journal j)
Definition: SNTPClock.cpp:486
beast::Journal::info
Stream info() const
Definition: Journal.h:321
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::TimeKeeper
Manages various times used by the server.
Definition: TimeKeeper.h:32
memory
ripple::TimeKeeperImpl::adjustCloseTime
void adjustCloseTime(std::chrono::duration< std::int32_t > amount) override
Adjust the close time.
Definition: TimeKeeper.cpp:71
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::TimeKeeperImpl::run
void run(std::vector< std::string > const &servers) override
Launch the internal thread.
Definition: TimeKeeper.cpp:51
std
STL namespace.
ripple::TimeKeeperImpl::closeOffset
std::chrono::duration< std::int32_t > closeOffset() const override
Definition: TimeKeeper.cpp:109
ripple::make_TimeKeeper
std::unique_ptr< TimeKeeper > make_TimeKeeper(beast::Journal j)
Definition: TimeKeeper.cpp:119
ripple::TimeKeeperImpl::clock_
std::unique_ptr< SNTPClock > clock_
Definition: TimeKeeper.cpp:34
std::chrono::duration::count
T count(T... args)
ripple::TimeKeeperImpl
Definition: TimeKeeper.cpp:28
mutex
ripple::TimeKeeperImpl::now
time_point now() const override
Returns the estimate of wall time, in network time.
Definition: TimeKeeper.cpp:57
std::unique_ptr
STL class.
beast::abstract_clock< NetClock >::time_point
typename NetClock ::time_point time_point
Definition: abstract_clock.h:63
ripple::TimeKeeperImpl::TimeKeeperImpl
TimeKeeperImpl(beast::Journal j)
Definition: TimeKeeper.cpp:45
std::chrono