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 <boost/algorithm/string/case_conv.hpp>
24 #include <libdevcore/Base64.h>
25 #include <libdevcore/Terminal.h>
26 #include <libdevcore/CommonData.h>
27 #include <libdevcore/CommonIO.h>
28 #include <libdevcore/Log.h>
29 #include <libdevcore/SHA3.h>
30 #include "ICAP.h"
31 #include "Exceptions.h"
32 #include "BlockHeader.h"
33 
34 using namespace std;
35 using namespace dev;
36 using namespace dev::eth;
37 
38 namespace dev
39 {
40 namespace eth
41 {
42 
43 const unsigned c_protocolVersion = 63;
44 #if ETH_FATDB
45 const unsigned c_minorProtocolVersion = 3;
46 const unsigned c_databaseBaseVersion = 9;
47 const unsigned c_databaseVersionModifier = 1;
48 #else
49 const unsigned c_minorProtocolVersion = 2;
50 const unsigned c_databaseBaseVersion = 9;
51 const unsigned c_databaseVersionModifier = 0;
52 #endif
53 
54 const unsigned c_databaseVersion = c_databaseBaseVersion + (c_databaseVersionModifier << 8) + (23 << 9);
55 
56 Address toAddress(std::string const& _s)
57 {
58  try
59  {
60  eth::ICAP i = eth::ICAP::decoded(_s);
61  return i.direct();
62  }
63  catch (eth::InvalidICAP&) {}
64  try
65  {
66  auto b = fromHex(_s.substr(0, 2) == "0x" ? _s.substr(2) : _s, WhenError::Throw);
67  if (b.size() == 20)
68  return Address(b);
69  }
70  catch (BadHexCharacter&) {}
71  BOOST_THROW_EXCEPTION(InvalidAddress());
72 }
73 
74 vector<pair<u256, string>> const& units()
75 {
76  static const vector<pair<u256, string>> s_units =
77  {
78  {exp10<54>(), "Uether"},
79  {exp10<51>(), "Vether"},
80  {exp10<48>(), "Dether"},
81  {exp10<45>(), "Nether"},
82  {exp10<42>(), "Yether"},
83  {exp10<39>(), "Zether"},
84  {exp10<36>(), "Eether"},
85  {exp10<33>(), "Pether"},
86  {exp10<30>(), "Tether"},
87  {exp10<27>(), "Gether"},
88  {exp10<24>(), "Mether"},
89  {exp10<21>(), "grand"},
90  {exp10<18>(), "ether"},
91  {exp10<15>(), "finney"},
92  {exp10<12>(), "szabo"},
93  {exp10<9>(), "Gwei"},
94  {exp10<6>(), "Mwei"},
95  {exp10<3>(), "Kwei"},
96  {exp10<0>(), "wei"}
97  };
98 
99  return s_units;
100 }
101 
102 std::string formatBalance(bigint const& _b)
103 {
104  ostringstream ret;
105  u256 b;
106  if (_b < 0)
107  {
108  ret << "-";
109  b = (u256)-_b;
110  }
111  else
112  b = (u256)_b;
113 
114  if (b > units()[0].first * 1000)
115  {
116  ret << (b / units()[0].first) << " " << units()[0].second;
117  return ret.str();
118  }
119  ret << setprecision(5);
120  for (auto const& i: units())
121  if (i.first != 1 && b >= i.first)
122  {
123  ret << (double(b / (i.first / 1000)) / 1000.0) << " " << i.second;
124  return ret.str();
125  }
126  ret << b << " wei";
127  return ret.str();
128 }
129 
130 static void badBlockInfo(BlockHeader const& _bi, string const& _err)
131 {
132  string const c_line = EthReset EthOnMaroon + string(80, ' ') + EthReset;
133  string const c_border = EthReset EthOnMaroon + string(2, ' ') + EthReset EthMaroonBold;
134  string const c_space = c_border + string(76, ' ') + c_border + EthReset;
135  stringstream ss;
136  ss << c_line << endl;
137  ss << c_space << endl;
138  ss << c_border + " Import Failure " + _err + string(max<int>(0, 53 - _err.size()), ' ') + " " + c_border << endl;
139  ss << c_space << endl;
140  string bin = toString(_bi.number());
141  ss << c_border + (" Guru Meditation #" + string(max<int>(0, 8 - bin.size()), '0') + bin + "." + _bi.hash().abridged() + " ") + c_border << endl;
142  ss << c_space << endl;
143  ss << c_line;
144  cwarn << "\n" + ss.str();
145 }
146 
147 void badBlock(bytesConstRef _block, string const& _err)
148 {
149  BlockHeader bi;
150  DEV_IGNORE_EXCEPTIONS(bi = BlockHeader(_block));
151  badBlockInfo(bi, _err);
152 }
153 
154 string TransactionSkeleton::userReadable(bool _toProxy, function<pair<bool, string>(TransactionSkeleton const&)> const& _getNatSpec, function<string(Address const&)> const& _formatAddress) const
155 {
156  if (creation)
157  {
158  // show notice concerning the creation code. TODO: this needs entering into natspec.
159  return string("ÐApp is attempting to create a contract; ") + (_toProxy ? "(this transaction is not executed directly, but forwarded to another ÐApp) " : "") + "to be endowed with " + formatBalance(value) + ", with additional network fees of up to " + formatBalance(gas * gasPrice) + ".\n\nMaximum total cost is " + formatBalance(value + gas * gasPrice) + ".";
160  }
161 
162  bool isContract;
163  std::string natSpec;
164  tie(isContract, natSpec) = _getNatSpec(*this);
165  if (!isContract)
166  {
167  // recipient has no code - nothing special about this transaction, show basic value transfer info
168  return "ÐApp is attempting to send " + formatBalance(value) + " to a recipient " + _formatAddress(to) + (_toProxy ? " (this transaction is not executed directly, but forwarded to another ÐApp)" : "") + ", with additional network fees of up to " + formatBalance(gas * gasPrice) + ".\n\nMaximum total cost is " + formatBalance(value + gas * gasPrice) + ".";
169  }
170 
171  if (natSpec.empty())
172  return "ÐApp is attempting to call into an unknown contract at address " +
173  _formatAddress(to) + ".\n\n" +
174  (_toProxy ? "This transaction is not executed directly, but forwarded to another ÐApp.\n\n" : "") +
175  "Call involves sending " +
176  formatBalance(value) + " to the recipient, with additional network fees of up to " +
177  formatBalance(gas * gasPrice) +
178  "However, this also does other stuff which we don't understand, and does so in your name.\n\n" +
179  "WARNING: This is probably going to cost you at least " +
180  formatBalance(value + gas * gasPrice) +
181  ", however this doesn't include any side-effects, which could be of far greater importance.\n\n" +
182  "REJECT UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!";
183 
184  return "ÐApp attempting to conduct contract interaction with " +
185  _formatAddress(to) +
186  ": <b>" + natSpec + "</b>.\n\n" +
187  (_toProxy ? "This transaction is not executed directly, but forwarded to another ÐApp.\n\n" : "") +
188  (value > 0 ?
189  "In addition, ÐApp is attempting to send " +
190  formatBalance(value) + " to said recipient, with additional network fees of up to " +
191  formatBalance(gas * gasPrice) + " = " +
192  formatBalance(value + gas * gasPrice) + "."
193  :
194  "Additional network fees are at most" +
195  formatBalance(gas * gasPrice) + ".");
196 }
197 
198 }
199 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
u256 const & number() const
Definition: BlockHeader.h:162
const unsigned c_protocolVersion
Current protocol version.
Definition: Common.cpp:43
const unsigned c_databaseVersionModifier
Definition: Common.cpp:51
h256 hash(IncludeSeal _i=WithSeal) const
Definition: BlockHeader.cpp:64
u256 exp10< 0 >()
Definition: Common.h:194
Encapsulation of a block header.
Definition: BlockHeader.h:95
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>> bigint
Definition: Common.h:121
h160 Address
An Ethereum address: 20 bytes.
Definition: Common.h:62
#define EthOnMaroon
Definition: Terminal.h:171
std::hash for asio::adress
Definition: Common.h:323
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
Definition: CommonData.cpp:141
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
Definition: CommonData.cpp:99
void badBlock(bytesConstRef _block, string const &_err)
Definition: Common.cpp:147
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
#define cwarn
Definition: Log.h:304
Address toAddress(std::string const &_s)
Convert the given string into an address.
Definition: Common.cpp:56
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 formatBalance(bigint const &_b)
User-friendly string representation of the amount _b in wei.
Definition: Common.cpp:102
#define b(i, j)
Encapsulation of an ICAP address.
Definition: ICAP.h:45
Address const & direct() const
Definition: ICAP.h:81
const unsigned c_databaseBaseVersion
Definition: Common.cpp:50
const unsigned c_minorProtocolVersion
Current minor protocol version.
Definition: Common.cpp:49
std::string abridged() const
Definition: FixedHash.h:124
const unsigned c_databaseVersion
Current database version.
Definition: Common.cpp:54
#define DEV_IGNORE_EXCEPTIONS(X)
Definition: Common.h:63
#define EthReset
Definition: Terminal.h:80
vector< pair< u256, string > > const & units()
Get information concerning the currency denominations.
Definition: Common.cpp:74
#define EthMaroonBold
Definition: Terminal.h:137