Fabcoin Core  0.16.2
P2P Digital Currency
Common.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include "Common.h"
23 #include "Exceptions.h"
24 #include "Log.h"
25 #ifndef FASC_BUILD
26 #include "BuildInfo.h"
27 #endif
28 using namespace std;
29 using namespace dev;
30 
31 namespace dev
32 {
33 #ifdef FASC_BUILD
34 #define ETH_PROJECT_VERSION "1.3.0"
35 #endif
36 char const* Version = ETH_PROJECT_VERSION;
37 
38 const u256 Invalid256 = ~(u256)0;
39 
40 void InvariantChecker::checkInvariants(HasInvariants const* _this, char const* _fn, char const* _file, int _line, bool _pre)
41 {
42  if (!_this->invariants())
43  {
44  cwarn << (_pre ? "Pre" : "Post") << "invariant failed in" << _fn << "at" << _file << ":" << _line;
45  ::boost::exception_detail::throw_exception_(FailedInvariant(), _fn, _file, _line);
46  }
47 }
48 
49 struct TimerChannel: public LogChannel { static const char* name(); static const int verbosity = 0; };
50 
51 #if defined(_WIN32)
52 const char* TimerChannel::name() { return EthRed " ! "; }
53 #else
54 const char* TimerChannel::name() { return EthRed " ⚡ "; }
55 #endif
56 
57 TimerHelper::~TimerHelper()
58 {
59  auto e = std::chrono::high_resolution_clock::now() - m_t;
60  if (!m_ms || e > chrono::milliseconds(m_ms))
61  clog(TimerChannel) << m_id << chrono::duration_cast<chrono::milliseconds>(e).count() << "ms";
62 }
63 
64 uint64_t utcTime()
65 {
66  // TODO: Fix if possible to not use time(0) and merge only after testing in all platforms
67  // time_t t = time(0);
68  // return mktime(gmtime(&t));
69  return time(0);
70 }
71 
72 string inUnits(bigint const& _b, strings const& _units)
73 {
74  ostringstream ret;
75  u256 b;
76  if (_b < 0)
77  {
78  ret << "-";
79  b = (u256)-_b;
80  }
81  else
82  b = (u256)_b;
83 
84  u256 biggest = 1;
85  for (unsigned i = _units.size() - 1; !!i; --i)
86  biggest *= 1000;
87 
88  if (b > biggest * 1000)
89  {
90  ret << (b / biggest) << " " << _units.back();
91  return ret.str();
92  }
93  ret << setprecision(3);
94 
95  u256 unit = biggest;
96  for (auto it = _units.rbegin(); it != _units.rend(); ++it)
97  {
98  auto i = *it;
99  if (i != _units.front() && b >= unit)
100  {
101  ret << (double(b / (unit / 1000)) / 1000.0) << " " << i;
102  return ret.str();
103  }
104  else
105  unit /= 1000;
106  }
107  ret << b << " " << _units.front();
108  return ret.str();
109 }
110 
111 }
Inheritable for classes that have invariants.
Definition: Common.h:229
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
uint64_t utcTime()
Get the current time in seconds since the epoch in UTC.
Definition: Common.cpp:64
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>> bigint
Definition: Common.h:121
size_t count
Definition: ExecStats.cpp:37
std::vector< std::string > strings
Definition: Common.h:147
std::hash for asio::adress
Definition: Common.h:323
string inUnits(bigint const &_b, strings const &_units)
Converts given int to a string and appends one of a series of units according to its size...
Definition: Common.cpp:72
const char * name
Definition: rest.cpp:36
const u256 Invalid256
Definition: Common.cpp:38
virtual bool invariants() const =0
Reimplement to specify the invariants.
#define cwarn
Definition: Log.h:304
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
#define b(i, j)
#define clog(X)
Definition: Log.h:295
#define e(i)
Definition: sha.cpp:733
The default logging channels.
Definition: Log.h:130
#define EthRed
Definition: Terminal.h:121