Ethereum  PoC-8
The C++ Implementation of Ethereum
Common.h
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 #pragma once
25 
26 #include <libdevcore/Address.h>
27 #include <libdevcore/Common.h>
28 #include <libdevcore/Exceptions.h>
29 #include <libdevcore/FixedHash.h>
30 
31 #include <functional>
32 #include <string>
33 
34 namespace dev
35 {
36 
37 class RLP;
38 class RLPStream;
39 
40 namespace eth
41 {
42 
44 extern const unsigned c_protocolVersion;
45 
47 extern const unsigned c_minorProtocolVersion;
48 
50 extern const unsigned c_databaseVersion;
51 
55 extern const bytes c_blockhashContractCode;
56 
58 std::string formatBalance(bigint const& _b);
59 
60 DEV_SIMPLE_EXCEPTION(InvalidAddress);
61 
63 Address toAddress(std::string const& _s);
64 
66 std::vector<std::pair<u256, std::string>> const& units();
67 
69 using LogBloom = h2048;
70 
72 using LogBlooms = std::vector<LogBloom>;
73 
74 // The various denominations; here for ease of use where needed within code.
75 static const u256 ether = exp10<18>();
76 static const u256 finney = exp10<15>();
77 static const u256 szabo = exp10<12>();
78 static const u256 shannon = exp10<9>();
79 static const u256 wei = exp10<0>();
80 
81 using Nonce = h64;
82 
83 using BlockNumber = unsigned;
84 
85 static const BlockNumber LatestBlock = (BlockNumber)-2;
86 static const BlockNumber PendingBlock = (BlockNumber)-1;
87 static const h256 LatestBlockHash = h256(2);
88 static const h256 EarliestBlockHash = h256(1);
89 static const h256 PendingBlockHash = h256(0);
90 
91 static const u256 DefaultBlockGasLimit = 4712388;
92 
94 {
95  Latest = LatestBlock,
96  Pending = PendingBlock
97 };
98 
99 enum class BlockPolarity
100 {
101  Unknown,
102  Dead,
103  Live
104 };
105 
106 class Transaction;
107 
109 {
112  std::vector<Transaction> goodTranactions;
113 };
114 
115 enum class ImportResult
116 {
117  Success = 0,
122  AlreadyKnown,
123  Malformed,
125  BadChain,
127 };
128 
130 {
131  using value = unsigned;
132  enum
133  {
134  ValidSeal = 1,
137  UncleSeals = 16,
139  Parent = 64,
140  UncleParent = 128,
141  PostGenesis = 256,
147  None = 0
148  };
149 };
150 
152 template<typename... Args> class Signal
153 {
154 public:
155  using Callback = std::function<void(Args...)>;
156 
158  {
159  friend class Signal;
160 
161  public:
162  ~HandlerAux() { if (m_s) m_s->m_fire.erase(m_i); }
163  void reset() { m_s = nullptr; }
164  void fire(Args const&... _args) { m_h(_args...); }
165 
166  private:
167  HandlerAux(unsigned _i, Signal* _s, Callback const& _h): m_i(_i), m_s(_s), m_h(_h) {}
168 
169  unsigned m_i = 0;
170  Signal* m_s = nullptr;
171  Callback m_h;
172  };
173 
175  {
176  for (auto const& h : m_fire)
177  if (auto l = h.second.lock())
178  l->reset();
179  }
180 
181  std::shared_ptr<HandlerAux> add(Callback const& _h)
182  {
183  auto n = m_fire.empty() ? 0 : (m_fire.rbegin()->first + 1);
184  auto h = std::shared_ptr<HandlerAux>(new HandlerAux(n, this, _h));
185  m_fire[n] = h;
186  return h;
187  }
188 
189  void operator()(Args const&... _args)
190  {
191  for (auto const& f: valuesOf(m_fire))
192  if (auto h = f.lock())
193  h->fire(_args...);
194  }
195 
196 private:
197  std::map<unsigned, std::weak_ptr<typename Signal::HandlerAux>> m_fire;
198 };
199 
200 template<class... Args> using Handler = std::shared_ptr<typename Signal<Args...>::HandlerAux>;
201 
203 {
204  bool creation = false;
212 
213  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;
214 };
215 
216 
217 void badBlock(bytesConstRef _header, std::string const& _err);
218 inline void badBlock(bytes const& _header, std::string const& _err) { badBlock(&_header, _err); }
219 
220 // TODO: move back into a mining subsystem and have it be accessible from Sealant only via a dynamic_cast.
225 {
226 // MiningProgress& operator+=(MiningProgress const& _mp) { hashes += _mp.hashes; ms = std::max(ms, _mp.ms); return *this; }
227  uint64_t hashes = 0;
228  uint64_t ms = 0;
229  u256 rate() const { return ms == 0 ? 0 : hashes * 1000 / ms; }
230 };
231 
233 enum class IfDropped
234 {
235  Ignore,
236  Retry
237 };
238 
239 }
240 }
Definition: Address.cpp:20
std::string formatBalance(bigint const &_b)
User-friendly string representation of the amount _b in wei.
Definition: Common.cpp:96
Super-duper signal mechanism. TODO: replace with somthing a bit heavier weight.
Definition: Common.h:152
const unsigned c_protocolVersion
Current protocol version.
Definition: Common.cpp:40
void operator()(Args const &... _args)
Definition: Common.h:189
u256 exp10< 0 >()
Definition: Common.h:186
Do all checks that cannot be done independently of prior blocks having been imported.
Definition: Common.h:145
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
Definition: Common.cpp:148
Check the basic structure of the uncles.
Definition: Common.h:137
FixedHash< 8 > h64
Definition: FixedHash.h:357
Address toAddress(std::string const &_s)
Convert the given string into an address.
Definition: Common.cpp:56
const Address c_blockhashContractAddress(0xf0)
Address of the special contract for block hash storage defined in EIP96.
Definition: Common.h:53
unsigned BlockNumber
Definition: Common.h:83
std::vector< U > valuesOf(std::map< T, U > const &_m)
Definition: CommonData.h:304
Import transaction even if it was dropped before.
ImportResult
Definition: Common.h:115
Check the basic structure of the transactions.
Definition: Common.h:136
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<> > bigint
Definition: Common.h:118
void badBlock(bytesConstRef _block, string const &_err)
Definition: Common.cpp:141
IfDropped
Import transaction policy.
Definition: Common.h:233
std::shared_ptr< typename Signal< Args... >::HandlerAux > Handler
Definition: Common.h:200
BlockPolarity
Definition: Common.h:99
Require block to be non-genesis.
Definition: Common.h:141
Check the basic structure of the transactions.
Definition: Common.h:138
u256 constexpr Invalid256
Definition: Common.h:147
Check parent block header.
Definition: Common.h:139
std::vector< byte > bytes
Definition: Common.h:72
const bytes c_blockhashContractCode
Code of the special contract for block hash storage defined in EIP96.
uint64_t ms
Total number of milliseconds of mining thus far.
Definition: Common.h:228
Don&#39;t import transaction that was previously dropped.
std::shared_ptr< HandlerAux > add(Callback const &_h)
Definition: Common.h:181
FixedHash< 32 > h256
Definition: FixedHash.h:354
Do all checks that can be done independently of prior blocks having been imported.
Definition: Common.h:144
RelativeBlock
Definition: Common.h:93
Check uncle parent block header.
Definition: Common.h:140
uint64_t hashes
Total number of hashes computed.
Definition: Common.h:227
Describes the progress of a mining operation.
Definition: Common.h:224
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:121
const unsigned c_minorProtocolVersion
Current minor protocol version.
Definition: Common.cpp:46
FixedHash< 256 > h2048
Definition: FixedHash.h:350
DEV_SIMPLE_EXCEPTION(NoHashRecorded)
Check transaction signatures.
Definition: Common.h:143
std::function< void(Args...)> Callback
Definition: Common.h:155
const unsigned c_databaseVersion
Current database version.
Definition: Common.cpp:51
u256 rate() const
Definition: Common.h:229
Check the basic structure of the uncles.
Definition: Common.h:135
std::vector< Transaction > goodTranactions
Definition: Common.h:112
std::vector< h256 > h256s
Definition: FixedHash.h:359
std::vector< LogBloom > LogBlooms
Many log blooms.
Definition: Common.h:72
void fire(Args const &... _args)
Definition: Common.h:164
vector< pair< u256, string > > const & units()
Get information concerning the currency denominations.
Definition: Common.cpp:68