Common.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. *
  18. * Ethereum-specific data structures & algorithms.
  19. */
  20. #pragma once
  21. #include <string>
  22. #include <functional>
  23. #include <libdevcore/Common.h>
  24. #include <libdevcore/FixedHash.h>
  25. #include <libdevcrypto/Common.h>
  26. namespace dev
  27. {
  28. namespace eth
  29. {
  30. /// Current protocol version.
  31. extern const unsigned c_protocolVersion;
  32. /// Current minor protocol version.
  33. extern const unsigned c_minorProtocolVersion;
  34. /// Current database version.
  35. extern const unsigned c_databaseVersion;
  36. /// User-friendly string representation of the amount _b in wei.
  37. std::string formatBalance(bigint const& _b);
  38. DEV_SIMPLE_EXCEPTION(InvalidAddress);
  39. /// Convert the given string into an address.
  40. Address toAddress(std::string const& _s);
  41. /// Get information concerning the currency denominations.
  42. std::vector<std::pair<u256, std::string>> const& units();
  43. /// The log bloom's size (2048-bit).
  44. using LogBloom = h2048;
  45. /// Many log blooms.
  46. using LogBlooms = std::vector<LogBloom>;
  47. // The various denominations; here for ease of use where needed within code.
  48. static const u256 ether = exp10<18>();
  49. static const u256 finney = exp10<15>();
  50. static const u256 szabo = exp10<12>();
  51. static const u256 shannon = exp10<9>();
  52. static const u256 wei = exp10<0>();
  53. using Nonce = h64;
  54. using BlockNumber = unsigned;
  55. static const BlockNumber LatestBlock = (BlockNumber)-2;
  56. static const BlockNumber PendingBlock = (BlockNumber)-1;
  57. static const h256 LatestBlockHash = h256(2);
  58. static const h256 EarliestBlockHash = h256(1);
  59. static const h256 PendingBlockHash = h256(0);
  60. static const u256 DefaultBlockGasLimit = 4712388;
  61. enum class RelativeBlock: BlockNumber
  62. {
  63. Latest = LatestBlock,
  64. Pending = PendingBlock
  65. };
  66. class Transaction;
  67. struct ImportRoute
  68. {
  69. h256s deadBlocks;
  70. h256s liveBlocks;
  71. std::vector<Transaction> goodTranactions;
  72. };
  73. enum class ImportResult
  74. {
  75. Success = 0,
  76. UnknownParent,
  77. FutureTimeKnown,
  78. FutureTimeUnknown,
  79. AlreadyInChain,
  80. AlreadyKnown,
  81. Malformed,
  82. OverbidGasPrice,
  83. BadChain
  84. };
  85. struct ImportRequirements
  86. {
  87. using value = unsigned;
  88. enum
  89. {
  90. ValidSeal = 1, ///< Validate seal
  91. UncleBasic = 4, ///< Check the basic structure of the uncles.
  92. TransactionBasic = 8, ///< Check the basic structure of the transactions.
  93. UncleSeals = 16, ///< Check the basic structure of the uncles.
  94. TransactionSignatures = 32, ///< Check the basic structure of the transactions.
  95. Parent = 64, ///< Check parent block header.
  96. UncleParent = 128, ///< Check uncle parent block header.
  97. PostGenesis = 256, ///< Require block to be non-genesis.
  98. CheckUncles = UncleBasic | UncleSeals, ///< Check uncle seals.
  99. CheckTransactions = TransactionBasic | TransactionSignatures, ///< Check transaction signatures.
  100. OutOfOrderChecks = ValidSeal | CheckUncles | CheckTransactions, ///< Do all checks that can be done independently of prior blocks having been imported.
  101. InOrderChecks = Parent | UncleParent, ///< Do all checks that cannot be done independently of prior blocks having been imported.
  102. Everything = ValidSeal | CheckUncles | CheckTransactions | Parent | UncleParent,
  103. None = 0
  104. };
  105. };
  106. /// Super-duper signal mechanism. TODO: replace with somthing a bit heavier weight.
  107. template<typename... Args> class Signal
  108. {
  109. public:
  110. using Callback = std::function<void(Args...)>;
  111. class HandlerAux
  112. {
  113. friend class Signal;
  114. public:
  115. ~HandlerAux() { if (m_s) m_s->m_fire.erase(m_i); }
  116. void reset() { m_s = nullptr; }
  117. void fire(Args const&... _args) { m_h(_args...); }
  118. private:
  119. HandlerAux(unsigned _i, Signal* _s, Callback const& _h): m_i(_i), m_s(_s), m_h(_h) {}
  120. unsigned m_i = 0;
  121. Signal* m_s = nullptr;
  122. Callback m_h;
  123. };
  124. ~Signal()
  125. {
  126. for (auto const& h : m_fire)
  127. if (auto l = h.second.lock())
  128. l->reset();
  129. }
  130. std::shared_ptr<HandlerAux> add(Callback const& _h)
  131. {
  132. auto n = m_fire.empty() ? 0 : (m_fire.rbegin()->first + 1);
  133. auto h = std::shared_ptr<HandlerAux>(new HandlerAux(n, this, _h));
  134. m_fire[n] = h;
  135. return h;
  136. }
  137. void operator()(Args const&... _args)
  138. {
  139. for (auto const& f: valuesOf(m_fire))
  140. if (auto h = f.lock())
  141. h->fire(_args...);
  142. }
  143. private:
  144. std::map<unsigned, std::weak_ptr<typename Signal::HandlerAux>> m_fire;
  145. };
  146. template<class... Args> using Handler = std::shared_ptr<typename Signal<Args...>::HandlerAux>;
  147. struct TransactionSkeleton
  148. {
  149. bool creation = false;
  150. Address from;
  151. Address to;
  152. u256 value;
  153. bytes data;
  154. u256 nonce = Invalid256;
  155. u256 gas = Invalid256;
  156. u256 gasPrice = Invalid256;
  157. std::string userReadable(bool _toProxy, std::function<std::pair<bool, std::string>(TransactionSkeleton const&)> const& _getNatSpec, std::function<std::string(Address const&)> const& _formatAddress) const;
  158. };
  159. void badBlock(bytesConstRef _header, std::string const& _err);
  160. inline void badBlock(bytes const& _header, std::string const& _err) { badBlock(&_header, _err); }
  161. // TODO: move back into a mining subsystem and have it be accessible from Sealant only via a dynamic_cast.
  162. /**
  163. * @brief Describes the progress of a mining operation.
  164. */
  165. struct WorkingProgress
  166. {
  167. // MiningProgress& operator+=(MiningProgress const& _mp) { hashes += _mp.hashes; ms = std::max(ms, _mp.ms); return *this; }
  168. uint64_t hashes = 0; ///< Total number of hashes computed.
  169. uint64_t ms = 0; ///< Total number of milliseconds of mining thus far.
  170. u256 rate() const { return ms == 0 ? 0 : hashes * 1000 / ms; }
  171. };
  172. /// Import transaction policy
  173. enum class IfDropped
  174. {
  175. Ignore, ///< Don't import transaction that was previously dropped.
  176. Retry ///< Import transaction even if it was dropped before.
  177. };
  178. }
  179. }