Fabcoin Core  0.16.2
P2P Digital Currency
CommonJS.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 */
24 #include "CommonJS.h"
25 
26 namespace dev
27 {
28 
29 std::string prettyU256(u256 _n, bool _abridged)
30 {
31  std::string raw;
32  std::ostringstream s;
33  if (!(_n >> 64))
34  s << " " << (uint64_t)_n << " (0x" << std::hex << (uint64_t)_n << ")";
35  else if (!~(_n >> 64))
36  s << " " << (int64_t)_n << " (0x" << std::hex << (int64_t)_n << ")";
37  else if ((_n >> 160) == 0)
38  {
39  Address a = right160(_n);
40 
41  std::string n;
42  if (_abridged)
43  n = a.abridged();
44  else
45  n = toHex(a.ref());
46 
47  if (n.empty())
48  s << "0";
49  else
50  s << _n << "(0x" << n << ")";
51  }
52  else if (!(raw = fromRaw((h256)_n)).empty())
53  return "\"" + raw + "\"";
54  else
55  s << "" << (h256)_n;
56  return s.str();
57 }
58 
59 namespace eth
60 {
61 
62 BlockNumber jsToBlockNumber(std::string const& _js)
63 {
64  if (_js == "latest")
65  return LatestBlock;
66  else if (_js == "earliest")
67  return 0;
68  else if (_js == "pending")
69  return PendingBlock;
70  else
71  return (unsigned)jsToInt(_js);
72 }
73 
74 }
75 
76 }
77 
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
Definition: CommonData.h:54
h160 right160(h256 const &_t)
Convert the given value into h160 (160-bit unsigned integer) using the right 20 bytes.
Definition: FixedHash.h:353
unsigned BlockNumber
Definition: Common.h:72
BlockNumber jsToBlockNumber(std::string const &_js)
Convert to a block number, a bit like jsToInt, except that it correctly recognises "pending" and "lat...
Definition: CommonJS.cpp:62
std::string prettyU256(u256 _n, bool _abridged)
Convert u256 into user-readable string. Returns int/hex value of 64 bits int, hex of 160 bits FixedHa...
Definition: CommonJS.cpp:29
#define a(i)
bytesRef ref()
Definition: FixedHash.h:133
string fromRaw(h256 _n)
Convert h256 into user-readable string (by directly using std::string constructor). If it can&#39;t be interpreted as an ASCII string, empty string is returned.
Definition: CommonJS.cpp:81
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
FixedHash< 32 > h256
Definition: FixedHash.h:340
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
std::string abridged() const
Definition: FixedHash.h:124
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< N *8, N *8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > jsToInt(std::string const &_s)
Convert a string representation of a number to an int String can be a normal decimal number...
Definition: CommonJS.h:97