Fabcoin Core  0.16.2
P2P Digital Currency
Common.h
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 */
24 #pragma once
25 
26 // way to many unsigned to size_t warnings in 32 bit build
27 #ifdef _M_IX86
28 #pragma warning(disable:4244)
29 #endif
30 
31 #if _MSC_VER && _MSC_VER < 1900
32 #define _ALLOW_KEYWORD_MACROS
33 #define noexcept throw()
34 #endif
35 
36 #ifdef __INTEL_COMPILER
37 #pragma warning(disable:3682) //call through incomplete class
38 #endif
39 
40 #include <map>
41 #include <unordered_map>
42 #include <vector>
43 #include <set>
44 #include <unordered_set>
45 #include <functional>
46 #include <string>
47 #include <chrono>
48 #pragma warning(push)
49 #pragma GCC diagnostic push
50 #pragma GCC diagnostic ignored "-Wunused-parameter"
51 #include <boost/multiprecision/cpp_int.hpp>
52 #pragma warning(pop)
53 #pragma GCC diagnostic pop
54 #include "vector_ref.h"
55 
56 // CryptoPP defines byte in the global namespace, so must we.
57 using byte = uint8_t;
58 
59 // Quote a given token stream to turn it into a string.
60 #define DEV_QUOTED_HELPER(s) #s
61 #define DEV_QUOTED(s) DEV_QUOTED_HELPER(s)
62 
63 #define DEV_IGNORE_EXCEPTIONS(X) try { X; } catch (...) {}
64 
65 #define DEV_IF_THROWS(X) try{X;}catch(...)
66 
67 namespace dev
68 {
69 
70 extern char const* Version;
71 
72 static const std::string EmptyString;
73 
74 // Binary data types.
75 using bytes = std::vector<byte>;
78 
79 template <class T>
81 {
82 public:
84  secure_vector(secure_vector<T> const& /*_c*/) = default; // See https://github.com/ethereum/libweb3core/pull/44
85  explicit secure_vector(size_t _size): m_data(_size) {}
86  explicit secure_vector(size_t _size, T _item): m_data(_size, _item) {}
87  explicit secure_vector(std::vector<T> const& _c): m_data(_c) {}
88  explicit secure_vector(vector_ref<T> _c): m_data(_c.data(), _c.data() + _c.size()) {}
89  explicit secure_vector(vector_ref<const T> _c): m_data(_c.data(), _c.data() + _c.size()) {}
90  ~secure_vector() { ref().cleanse(); }
91 
93  {
94  if (&_c == this)
95  return *this;
96 
97  ref().cleanse();
98  m_data = _c.m_data;
99  return *this;
100  }
101  std::vector<T>& writable() { clear(); return m_data; }
102  std::vector<T> const& makeInsecure() const { return m_data; }
103 
104  void clear() { ref().cleanse(); }
105 
108 
109  size_t size() const { return m_data.size(); }
110  bool empty() const { return m_data.empty(); }
111 
112  void swap(secure_vector<T>& io_other) { m_data.swap(io_other.m_data); }
113 
114 private:
115  std::vector<T> m_data;
116 };
117 
119 
120 // Numeric types.
121 using bigint = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<>>;
122 using u8 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<8, 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
123 using u64 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<64, 64, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
124 using u128 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<128, 128, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
125 using u256 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
126 using s256 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<256, 256, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void>>;
127 using u160 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<160, 160, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
128 using s160 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<160, 160, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void>>;
129 using u512 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<512, 512, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
130 using s512 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<512, 512, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void>>;
131 using u256s = std::vector<u256>;
132 using u160s = std::vector<u160>;
133 using u256Set = std::set<u256>;
134 using u160Set = std::set<u160>;
135 
136 // Map types.
137 using StringMap = std::map<std::string, std::string>;
138 using BytesMap = std::map<bytes, bytes>;
139 using u256Map = std::map<u256, u256>;
140 using HexMap = std::map<bytes, bytes>;
141 
142 // Hash types.
143 using StringHashMap = std::unordered_map<std::string, std::string>;
144 using u256HashMap = std::unordered_map<u256, u256>;
145 
146 // String types.
147 using strings = std::vector<std::string>;
148 
149 // Fixed-length string types.
150 using string32 = std::array<char, 32>;
151 static const string32 ZeroString32 = {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }};
152 
153 // Null/Invalid values for convenience.
154 static const bytes NullBytes;
155 static const std::map<u256, u256> EmptyMapU256U256;
156 extern const u256 Invalid256;
157 
159 inline s256 u2s(u256 _u)
160 {
161  static const bigint c_end = bigint(1) << 256;
162  if (boost::multiprecision::bit_test(_u, 255))
163  return s256(-(c_end - _u));
164  else
165  return s256(_u);
166 }
167 
169 inline u256 s2u(s256 _u)
170 {
171  static const bigint c_end = bigint(1) << 256;
172  if (_u >= 0)
173  return u256(_u);
174  else
175  return u256(c_end + _u);
176 }
177 
179 std::string inUnits(bigint const& _b, strings const& _units);
180 
182 inline unsigned int toLog2(u256 _x)
183 {
184  unsigned ret;
185  for (ret = 0; _x >>= 1; ++ret) {}
186  return ret;
187 }
188 
189 template <size_t n> inline u256 exp10()
190 {
191  return exp10<n - 1>() * u256(10);
192 }
193 
194 template <> inline u256 exp10<0>()
195 {
196  return u256(1);
197 }
198 
200 template <typename T> uint64_t toUint64(T _u)
201 {
202  return static_cast<uint64_t>(u64(_u));
203 }
204 
205 template <typename T> uint8_t toUint8(T _u)
206 {
207  return static_cast<uint8_t>(u8(_u));
208 }
209 
211 template <class N>
212 inline N diff(N const& _a, N const& _b)
213 {
214  return std::max(_a, _b) - std::min(_a, _b);
215 }
216 
219 {
220 public:
221  ScopeGuard(std::function<void(void)> _f): m_f(_f) {}
222  ~ScopeGuard() { m_f(); }
223 
224 private:
225  std::function<void(void)> m_f;
226 };
227 
230 {
231 public:
233  virtual bool invariants() const = 0;
234 };
235 
238 {
239 public:
240  InvariantChecker(HasInvariants* _this, char const* _fn, char const* _file, int _line): m_this(_this), m_function(_fn), m_file(_file), m_line(_line) { checkInvariants(_this, _fn , _file, _line, true); }
241  ~InvariantChecker() { checkInvariants(m_this, m_function, m_file, m_line, false); }
243  static void checkInvariants(HasInvariants const* _this, char const* _fn, char const* _file, int line, bool _pre);
244 
245 private:
247  char const* m_function;
248  char const* m_file;
249  int m_line;
250 };
251 
253 #if ETH_DEBUG
254 #define DEV_INVARIANT_CHECK ::dev::InvariantChecker __dev_invariantCheck(this, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__)
255 #define DEV_INVARIANT_CHECK_HERE ::dev::InvariantChecker::checkInvariants(this, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, true)
256 #else
257 #define DEV_INVARIANT_CHECK (void)0;
258 #define DEV_INVARIANT_CHECK_HERE (void)0;
259 #endif
260 
263 {
264 public:
265  TimerHelper(std::string const& _id, unsigned _msReportWhenGreater = 0): m_t(std::chrono::high_resolution_clock::now()), m_id(_id), m_ms(_msReportWhenGreater) {}
266  ~TimerHelper();
267 
268 private:
270  std::string m_id;
271  unsigned m_ms;
272 };
273 
274 class Timer
275 {
276 public:
277  Timer() { restart(); }
278 
279  std::chrono::high_resolution_clock::duration duration() const { return std::chrono::high_resolution_clock::now() - m_t; }
280  double elapsed() const { return std::chrono::duration_cast<std::chrono::microseconds>(duration()).count() / 1000000.0; }
281  void restart() { m_t = std::chrono::high_resolution_clock::now(); }
282 
283 private:
285 };
286 
287 #define DEV_TIMED(S) for (::std::pair<::dev::TimerHelper, bool> __eth_t(S, true); __eth_t.second; __eth_t.second = false)
288 #define DEV_TIMED_SCOPE(S) ::dev::TimerHelper __eth_t(S)
289 #if defined(_MSC_VER)
290 #define DEV_TIMED_FUNCTION DEV_TIMED_SCOPE(__FUNCSIG__)
291 #else
292 #define DEV_TIMED_FUNCTION DEV_TIMED_SCOPE(__PRETTY_FUNCTION__)
293 #endif
294 
295 #define DEV_TIMED_ABOVE(S, MS) for (::std::pair<::dev::TimerHelper, bool> __eth_t(::dev::TimerHelper(S, MS), true); __eth_t.second; __eth_t.second = false)
296 #define DEV_TIMED_SCOPE_ABOVE(S, MS) ::dev::TimerHelper __eth_t(S, MS)
297 #if defined(_MSC_VER)
298 #define DEV_TIMED_FUNCTION_ABOVE(MS) DEV_TIMED_SCOPE_ABOVE(__FUNCSIG__, MS)
299 #else
300 #define DEV_TIMED_FUNCTION_ABOVE(MS) DEV_TIMED_SCOPE_ABOVE(__PRETTY_FUNCTION__, MS)
301 #endif
302 
303 #ifdef _MSC_VER
304 // TODO.
305 #define DEV_UNUSED
306 #else
307 #define DEV_UNUSED __attribute__((unused))
308 #endif
309 
310 enum class WithExisting: int
311 {
312  Trust = 0,
313  Verify,
314  Rescue,
315  Kill
316 };
317 
319 uint64_t utcTime();
320 
321 }
322 
323 namespace std
324 {
325 
327 {
328  return static_cast<dev::WithExisting>(max(static_cast<int>(_a), static_cast<int>(_b)));
329 }
330 
331 template <> struct hash<dev::u256>
332 {
333  size_t operator()(dev::u256 const& _a) const
334  {
335  unsigned size = _a.backend().size();
336  auto limbs = _a.backend().limbs();
337  return boost::hash_range(limbs, limbs + size);
338  }
339 };
340 
341 }
uint64_t toUint64(T _u)
Converts given multiprecision number to standard number type.
Definition: Common.h:200
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
#define function(a, b, c, d, k, s)
unsigned m_ms
Definition: Common.h:271
A modifiable reference to an existing object or vector in memory.
Definition: vector_ref.h:20
vector_ref< T > ref()
Definition: Common.h:106
uint8_t byte
Definition: Common.h:57
std::map< bytes, bytes > BytesMap
Definition: Common.h:138
secure_vector< T > & operator=(secure_vector< T > const &_c)
Definition: Common.h:92
std::chrono::high_resolution_clock::time_point m_t
Definition: Common.h:284
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void >> s256
Definition: Common.h:126
RAII utility class whose destructor calls a given function.
Definition: Common.h:218
uint64_t utcTime()
Get the current time in seconds since the epoch in UTC.
Definition: Common.cpp:64
u256 exp10< 0 >()
Definition: Common.h:194
#define T(i, x)
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>> bigint
Definition: Common.h:121
RAII checker for invariant assertions.
Definition: Common.h:237
size_t count
Definition: ExecStats.cpp:37
unsigned int toLog2(u256 _x)
Definition: Common.h:182
char const * m_file
Definition: Common.h:248
std::function< void(void)> m_f
Definition: Common.h:225
std::vector< std::string > strings
Definition: Common.h:147
std::hash for asio::adress
Definition: Common.h:323
std::set< u160 > u160Set
Definition: Common.h:134
std::vector< T > const & makeInsecure() const
Definition: Common.h:102
ExecStats::duration min
Definition: ExecStats.cpp:35
u256 exp10()
Definition: Common.h:189
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
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 160, 160, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u160
Definition: Common.h:127
std::chrono::high_resolution_clock::duration duration() const
Definition: Common.h:279
WithExisting
Definition: Common.h:310
std::map< u256, u256 > u256Map
Definition: Common.h:139
size_t operator()(dev::u256 const &_a) const
Definition: Common.h:333
double elapsed() const
Definition: Common.h:280
std::vector< u256 > u256s
Definition: Common.h:131
char const * m_function
Definition: Common.h:247
clock::duration duration
Definition: bench.h:50
HasInvariants const * m_this
Definition: Common.h:246
std::vector< u160 > u160s
Definition: Common.h:132
std::vector< byte > bytes
Definition: Common.h:75
secure_vector(vector_ref< T > _c)
Definition: Common.h:88
const u256 Invalid256
Definition: Common.cpp:38
ScopeGuard(std::function< void(void)> _f)
Definition: Common.h:221
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 512, 512, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u512
Definition: Common.h:129
secure_vector(size_t _size)
Definition: Common.h:85
void restart()
Definition: Common.h:281
Simple scope-based timer helper.
Definition: Common.h:262
secure_vector(std::vector< T > const &_c)
Definition: Common.h:87
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
std::unordered_map< std::string, std::string > StringHashMap
Definition: Common.h:143
std::vector< T > & writable()
Definition: Common.h:101
void swap(secure_vector< T > &io_other)
Definition: Common.h:112
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 64, 64, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u64
Definition: Common.h:123
std::string m_id
Definition: Common.h:270
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 128, 128, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u128
Definition: Common.h:124
std::array< char, 32 > string32
Definition: Common.h:150
InvariantChecker(HasInvariants *_this, char const *_fn, char const *_file, int _line)
Definition: Common.h:240
vector_ref< T const > ref() const
Definition: Common.h:107
s256 u2s(u256 _u)
Interprets _u as a two&#39;s complement signed number and returns the resulting s256. ...
Definition: Common.h:159
std::map< bytes, bytes > HexMap
Definition: Common.h:140
std::vector< T > m_data
Definition: Common.h:115
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 8, 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u8
Definition: Common.h:122
u256 s2u(s256 _u)
Definition: Common.h:169
std::set< u256 > u256Set
Definition: Common.h:133
N diff(N const &_a, N const &_b)
Definition: Common.h:212
clock::time_point time_point
Definition: bench.h:49
char const * Version
Definition: Common.cpp:36
TimerHelper(std::string const &_id, unsigned _msReportWhenGreater=0)
Definition: Common.h:265
size_t size() const
Definition: Common.h:109
secure_vector(vector_ref< const T > _c)
Definition: Common.h:89
uint8_t toUint8(T _u)
Definition: Common.h:205
dev::WithExisting max(dev::WithExisting _a, dev::WithExisting _b)
Definition: Common.h:326
std::map< std::string, std::string > StringMap
Definition: Common.h:137
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 160, 160, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void >> s160
Definition: Common.h:128
std::unordered_map< u256, u256 > u256HashMap
Definition: Common.h:144
std::chrono::high_resolution_clock::time_point m_t
Definition: Common.h:269
uint8_t const * data
Definition: sha3.h:19
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 512, 512, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void >> s512
Definition: Common.h:130
secure_vector(size_t _size, T _item)
Definition: Common.h:86
bool empty() const
Definition: Common.h:110