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