Common.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file Common.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #include "Common.h"
  19. #include <boost/algorithm/string/case_conv.hpp>
  20. #include <libdevcore/Base64.h>
  21. #include <libdevcore/Terminal.h>
  22. #include <libdevcore/CommonData.h>
  23. #include <libdevcore/CommonIO.h>
  24. #include <libdevcore/Log.h>
  25. #include <libdevcore/SHA3.h>
  26. #include "ICAP.h"
  27. #include "Exceptions.h"
  28. #include "BlockHeader.h"
  29. using namespace std;
  30. using namespace dev;
  31. using namespace dev::eth;
  32. namespace dev
  33. {
  34. namespace eth
  35. {
  36. const unsigned c_protocolVersion = 63;
  37. #if ETH_FATDB
  38. const unsigned c_minorProtocolVersion = 3;
  39. const unsigned c_databaseBaseVersion = 9;
  40. const unsigned c_databaseVersionModifier = 1;
  41. #else
  42. const unsigned c_minorProtocolVersion = 2;
  43. const unsigned c_databaseBaseVersion = 9;
  44. const unsigned c_databaseVersionModifier = 0;
  45. #endif
  46. const unsigned c_databaseVersion = c_databaseBaseVersion + (c_databaseVersionModifier << 8) + (23 << 9);
  47. Address toAddress(std::string const& _s)
  48. {
  49. try
  50. {
  51. eth::ICAP i = eth::ICAP::decoded(_s);
  52. return i.direct();
  53. }
  54. catch (eth::InvalidICAP&) {}
  55. try
  56. {
  57. auto b = fromHex(_s.substr(0, 2) == "0x" ? _s.substr(2) : _s, WhenError::Throw);
  58. if (b.size() == 20)
  59. return Address(b);
  60. }
  61. catch (BadHexCharacter&) {}
  62. BOOST_THROW_EXCEPTION(InvalidAddress());
  63. }
  64. vector<pair<u256, string>> const& units()
  65. {
  66. static const vector<pair<u256, string>> s_units =
  67. {
  68. {exp10<54>(), "Uether"},
  69. {exp10<51>(), "Vether"},
  70. {exp10<48>(), "Dether"},
  71. {exp10<45>(), "Nether"},
  72. {exp10<42>(), "Yether"},
  73. {exp10<39>(), "Zether"},
  74. {exp10<36>(), "Eether"},
  75. {exp10<33>(), "Pether"},
  76. {exp10<30>(), "Tether"},
  77. {exp10<27>(), "Gether"},
  78. {exp10<24>(), "Mether"},
  79. {exp10<21>(), "grand"},
  80. {exp10<18>(), "ether"},
  81. {exp10<15>(), "finney"},
  82. {exp10<12>(), "szabo"},
  83. {exp10<9>(), "Gwei"},
  84. {exp10<6>(), "Mwei"},
  85. {exp10<3>(), "Kwei"},
  86. {exp10<0>(), "wei"}
  87. };
  88. return s_units;
  89. }
  90. std::string formatBalance(bigint const& _b)
  91. {
  92. ostringstream ret;
  93. u256 b;
  94. if (_b < 0)
  95. {
  96. ret << "-";
  97. b = (u256)-_b;
  98. }
  99. else
  100. b = (u256)_b;
  101. if (b > units()[0].first * 1000)
  102. {
  103. ret << (b / units()[0].first) << " " << units()[0].second;
  104. return ret.str();
  105. }
  106. ret << setprecision(5);
  107. for (auto const& i: units())
  108. if (i.first != 1 && b >= i.first)
  109. {
  110. ret << (double(b / (i.first / 1000)) / 1000.0) << " " << i.second;
  111. return ret.str();
  112. }
  113. ret << b << " wei";
  114. return ret.str();
  115. }
  116. static void badBlockInfo(BlockHeader const& _bi, string const& _err)
  117. {
  118. string const c_line = EthReset EthOnMaroon + string(80, ' ') + EthReset;
  119. string const c_border = EthReset EthOnMaroon + string(2, ' ') + EthReset EthMaroonBold;
  120. string const c_space = c_border + string(76, ' ') + c_border + EthReset;
  121. stringstream ss;
  122. ss << c_line << endl;
  123. ss << c_space << endl;
  124. ss << c_border + " Import Failure " + _err + string(max<int>(0, 53 - _err.size()), ' ') + " " + c_border << endl;
  125. ss << c_space << endl;
  126. string bin = toString(_bi.number());
  127. ss << c_border + (" Guru Meditation #" + string(max<int>(0, 8 - bin.size()), '0') + bin + "." + _bi.hash().abridged() + " ") + c_border << endl;
  128. ss << c_space << endl;
  129. ss << c_line;
  130. cwarn << "\n" + ss.str();
  131. }
  132. void badBlock(bytesConstRef _block, string const& _err)
  133. {
  134. BlockHeader bi;
  135. DEV_IGNORE_EXCEPTIONS(bi = BlockHeader(_block));
  136. badBlockInfo(bi, _err);
  137. }
  138. string TransactionSkeleton::userReadable(bool _toProxy, function<pair<bool, string>(TransactionSkeleton const&)> const& _getNatSpec, function<string(Address const&)> const& _formatAddress) const
  139. {
  140. if (creation)
  141. {
  142. // show notice concerning the creation code. TODO: this needs entering into natspec.
  143. 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) + ".";
  144. }
  145. bool isContract;
  146. std::string natSpec;
  147. tie(isContract, natSpec) = _getNatSpec(*this);
  148. if (!isContract)
  149. {
  150. // recipient has no code - nothing special about this transaction, show basic value transfer info
  151. 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) + ".";
  152. }
  153. if (natSpec.empty())
  154. return "ÐApp is attempting to call into an unknown contract at address " +
  155. _formatAddress(to) + ".\n\n" +
  156. (_toProxy ? "This transaction is not executed directly, but forwarded to another ÐApp.\n\n" : "") +
  157. "Call involves sending " +
  158. formatBalance(value) + " to the recipient, with additional network fees of up to " +
  159. formatBalance(gas * gasPrice) +
  160. "However, this also does other stuff which we don't understand, and does so in your name.\n\n" +
  161. "WARNING: This is probably going to cost you at least " +
  162. formatBalance(value + gas * gasPrice) +
  163. ", however this doesn't include any side-effects, which could be of far greater importance.\n\n" +
  164. "REJECT UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!";
  165. return "ÐApp attempting to conduct contract interaction with " +
  166. _formatAddress(to) +
  167. ": <b>" + natSpec + "</b>.\n\n" +
  168. (_toProxy ? "This transaction is not executed directly, but forwarded to another ÐApp.\n\n" : "") +
  169. (value > 0 ?
  170. "In addition, ÐApp is attempting to send " +
  171. formatBalance(value) + " to said recipient, with additional network fees of up to " +
  172. formatBalance(gas * gasPrice) + " = " +
  173. formatBalance(value + gas * gasPrice) + "."
  174. :
  175. "Additional network fees are at most" +
  176. formatBalance(gas * gasPrice) + ".");
  177. }
  178. }
  179. }