Fabcoin Core  0.16.2
P2P Digital Currency
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 <string>
27 #include <functional>
28 #include <libdevcore/Common.h>
29 #include <libdevcore/FixedHash.h>
30 #include <libdevcrypto/Common.h>
31 
32 namespace dev
33 {
34 namespace eth
35 {
36 
38 extern const unsigned c_protocolVersion;
39 
41 extern const unsigned c_minorProtocolVersion;
42 
44 extern const unsigned c_databaseVersion;
45 
47 std::string formatBalance(bigint const& _b);
48 
49 DEV_SIMPLE_EXCEPTION(InvalidAddress);
50 
52 Address toAddress(std::string const& _s);
53 
55 std::vector<std::pair<u256, std::string>> const& units();
56 
58 using LogBloom = h2048;
59 
61 using LogBlooms = std::vector<LogBloom>;
62 
63 // The various denominations; here for ease of use where needed within code.
64 static const u256 ether = exp10<18>();
65 static const u256 finney = exp10<15>();
66 static const u256 szabo = exp10<12>();
67 static const u256 shannon = exp10<9>();
68 static const u256 wei = exp10<0>();
69 
70 using Nonce = h64;
71 
72 using BlockNumber = unsigned;
73 
74 static const BlockNumber LatestBlock = (BlockNumber)-2;
75 static const BlockNumber PendingBlock = (BlockNumber)-1;
76 static const h256 LatestBlockHash = h256(2);
77 static const h256 EarliestBlockHash = h256(1);
78 static const h256 PendingBlockHash = h256(0);
79 
80 static const u256 DefaultBlockGasLimit = 4712388;
81 
83 {
84  Latest = LatestBlock,
85  Pending = PendingBlock
86 };
87 
88 class Transaction;
89 
91 {
94  std::vector<Transaction> goodTranactions;
95 };
96 
97 enum class ImportResult
98 {
99  Success = 0,
104  AlreadyKnown,
105  Malformed,
107  BadChain
108 };
109 
111 {
112  using value = unsigned;
113  enum
114  {
115  ValidSeal = 1,
116  UncleBasic = 4,
117  TransactionBasic = 8,
118  UncleSeals = 16,
119  TransactionSignatures = 32,
120  Parent = 64,
121  UncleParent = 128,
122  PostGenesis = 256,
123  CheckUncles = UncleBasic | UncleSeals,
124  CheckTransactions = TransactionBasic | TransactionSignatures,
125  OutOfOrderChecks = ValidSeal | CheckUncles | CheckTransactions,
126  InOrderChecks = Parent | UncleParent,
127  Everything = ValidSeal | CheckUncles | CheckTransactions | Parent | UncleParent,
128  None = 0
129  };
130 };
131 
133 template<typename... Args> class Signal
134 {
135 public:
136  using Callback = std::function<void(Args...)>;
137 
139  {
140  friend class Signal;
141 
142  public:
143  ~HandlerAux() { if (m_s) m_s->m_fire.erase(m_i); }
144  void reset() { m_s = nullptr; }
145  void fire(Args const&... _args) { m_h(_args...); }
146 
147  private:
148  HandlerAux(unsigned _i, Signal* _s, Callback const& _h): m_i(_i), m_s(_s), m_h(_h) {}
149 
150  unsigned m_i = 0;
151  Signal* m_s = nullptr;
153  };
154 
156  {
157  for (auto const& h : m_fire)
158  if (auto l = h.second.lock())
159  l->reset();
160  }
161 
162  std::shared_ptr<HandlerAux> add(Callback const& _h)
163  {
164  auto n = m_fire.empty() ? 0 : (m_fire.rbegin()->first + 1);
165  auto h = std::shared_ptr<HandlerAux>(new HandlerAux(n, this, _h));
166  m_fire[n] = h;
167  return h;
168  }
169 
170  void operator()(Args const&... _args)
171  {
172  for (auto const& f: valuesOf(m_fire))
173  if (auto h = f.lock())
174  h->fire(_args...);
175  }
176 
177 private:
178  std::map<unsigned, std::weak_ptr<typename Signal::HandlerAux>> m_fire;
179 };
180 
181 template<class... Args> using Handler = std::shared_ptr<typename Signal<Args...>::HandlerAux>;
182 
184 {
185  bool creation = false;
190  u256 nonce = Invalid256;
192  u256 gasPrice = Invalid256;
193 
194  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;
195 };
196 
197 
198 void badBlock(bytesConstRef _header, std::string const& _err);
199 inline void badBlock(bytes const& _header, std::string const& _err) { badBlock(&_header, _err); }
200 
201 // TODO: move back into a mining subsystem and have it be accessible from Sealant only via a dynamic_cast.
206 {
207 // MiningProgress& operator+=(MiningProgress const& _mp) { hashes += _mp.hashes; ms = std::max(ms, _mp.ms); return *this; }
208  uint64_t hashes = 0;
209  uint64_t ms = 0;
210  u256 rate() const { return ms == 0 ? 0 : hashes * 1000 / ms; }
211 };
212 
214 enum class IfDropped
215 {
216  Ignore,
217  Retry
218 };
219 
220 }
221 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
Super-duper signal mechanism. TODO: replace with somthing a bit heavier weight.
Definition: Common.h:133
void fire(Args const &..._args)
Definition: Common.h:145
#define function(a, b, c, d, k, s)
const unsigned c_protocolVersion
Current protocol version.
Definition: Common.cpp:43
u256 exp10< 0 >()
Definition: Common.h:194
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>> bigint
Definition: Common.h:121
#define h(i)
Definition: sha.cpp:736
h160 Address
An Ethereum address: 20 bytes.
Definition: Common.h:62
FixedHash< 8 > h64
Definition: FixedHash.h:343
u256 rate() const
Definition: Common.h:210
unsigned BlockNumber
Definition: Common.h:72
std::vector< U > valuesOf(std::map< T, U > const &_m)
Definition: CommonData.h:359
Import transaction even if it was dropped before.
ImportResult
Definition: Common.h:97
void badBlock(bytesConstRef _block, string const &_err)
Definition: Common.cpp:147
IfDropped
Import transaction policy.
Definition: Common.h:214
std::shared_ptr< typename Signal< Args... >::HandlerAux > Handler
Definition: Common.h:181
std::vector< byte > bytes
Definition: Common.h:75
const u256 Invalid256
Definition: Common.cpp:38
Don&#39;t import transaction that was previously dropped.
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
std::shared_ptr< HandlerAux > add(Callback const &_h)
Definition: Common.h:162
FixedHash< 32 > h256
Definition: FixedHash.h:340
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
RelativeBlock
Definition: Common.h:82
Encodes a transaction, ready to be exported to or freshly imported from RLP.
Definition: Transaction.h:84
#define f(x)
Definition: gost.cpp:57
Describes the progress of a mining operation.
Definition: Common.h:205
const unsigned c_minorProtocolVersion
Current minor protocol version.
Definition: Common.cpp:49
FixedHash< 256 > h2048
Definition: FixedHash.h:336
const unsigned c_databaseVersion
Current database version.
Definition: Common.cpp:54
void operator()(Args const &..._args)
Definition: Common.h:170
HandlerAux(unsigned _i, Signal *_s, Callback const &_h)
Definition: Common.h:148
std::vector< Transaction > goodTranactions
Definition: Common.h:94
std::vector< h256 > h256s
Definition: FixedHash.h:345
DEV_SIMPLE_EXCEPTION(InvalidSealEngine)
std::vector< LogBloom > LogBlooms
Many log blooms.
Definition: Common.h:61
std::map< unsigned, std::weak_ptr< typename Signal::HandlerAux > > m_fire
Definition: Common.h:178
vector< pair< u256, string > > const & units()
Get information concerning the currency denominations.
Definition: Common.cpp:74