Fabcoin Core  0.16.2
P2P Digital Currency
TestHelper.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 */
21 #pragma once
22 
23 #include <thread>
24 #include <future>
25 #include <functional>
26 #include <boost/test/unit_test.hpp>
27 #include <boost/filesystem.hpp>
28 #include <boost/progress.hpp>
29 
30 #include <libethashseal/Ethash.h>
31 #include <libethereum/State.h>
33 #include <libevm/ExtVMFace.h>
35 
40 
41 namespace dev
42 {
43 
44 namespace eth
45 {
46 
47 class Client;
48 class State;
49 
50 void mine(Client& c, int numBlocks);
51 void connectClients(Client& c1, Client& c2);
52 void mine(Block& _s, BlockChain const& _bc, SealEngineFace* _sealer);
53 void mine(BlockHeader& _bi, SealEngineFace* _sealer, bool _verify = true);
54 }
55 
56 namespace test
57 {
58 
59 struct ValueTooLarge: virtual Exception {};
60 struct MissingFields : virtual Exception {};
61 bigint const c_max256plus1 = bigint(1) << 256;
62 
67 #define ETH_TEST_REQUIRE_NO_THROW(_statement, _message) \
68  do \
69  { \
70  try \
71  { \
72  BOOST_TEST_PASSPOINT(); \
73  _statement; \
74  } \
75  catch (boost::exception const& _e) \
76  { \
77  auto msg = std::string(_message " due to an exception thrown by " \
78  BOOST_STRINGIZE(_statement) "\n") + boost::diagnostic_information(_e); \
79  BOOST_CHECK_IMPL(false, msg, REQUIRE, CHECK_MSG); \
80  } \
81  catch (...) \
82  { \
83  BOOST_CHECK_IMPL(false, "Unknown exception thrown by " \
84  BOOST_STRINGIZE(_statement), REQUIRE, CHECK_MSG); \
85  } \
86  } \
87  while (0)
88 
93 #define ETH_TEST_CHECK_NO_THROW(_statement, _message) \
94  do \
95  { \
96  try \
97  { \
98  BOOST_TEST_PASSPOINT(); \
99  _statement; \
100  } \
101  catch (boost::exception const& _e) \
102  { \
103  auto msg = std::string(_message " due to an exception thrown by " \
104  BOOST_STRINGIZE(_statement) "\n") + boost::diagnostic_information(_e); \
105  BOOST_CHECK_IMPL(false, msg, CHECK, CHECK_MSG); \
106  } \
107  catch (...) \
108  { \
109  BOOST_CHECK_IMPL(false, "Unknown exception thrown by " \
110  BOOST_STRINGIZE(_statement), CHECK, CHECK_MSG ); \
111  } \
112  } \
113  while (0)
114 
115 
117 {
118 protected:
119  u256 ask(eth::Block const&) const override { return 0; }
121 };
122 
123 // helping functions
124 std::string netIdToString(eth::Network _netId);
125 eth::Network stringToNetId(std::string const& _netname);
126 u256 toInt(json_spirit::mValue const& _v);
127 byte toByte(json_spirit::mValue const& _v);
129 std::string compileLLL(std::string const& _code);
132 bytes importByteArray(std::string const& _str);
133 void copyFile(std::string const& _source, std::string const& _destination);
137 void checkStorage(std::map<u256, u256> _expectedStore, std::map<u256, u256> _resultStore, Address _expectedAddr);
138 void checkLog(eth::LogEntries _resultLogs, eth::LogEntries _expectedLogs);
139 void checkCallCreates(eth::Transactions _resultCallCreates, eth::Transactions _expectedCallCreates);
141  h256 const& _parentHash,
142  h256 const& _sha3Uncles,
143  Address const& _author,
144  h256 const& _stateRoot,
145  h256 const& _transactionsRoot,
146  h256 const& _receiptsRoot,
147  dev::eth::LogBloom const& _logBloom,
148  u256 const& _difficulty,
149  u256 const& _number,
150  u256 const& _gasLimit,
151  u256 const& _gasUsed,
152  u256 const& _timestamp,
153  bytes const& _extraData);
154 void updateEthashSeal(dev::eth::BlockHeader& _header, h256 const& _mixHash, dev::eth::Nonce const& _nonce);
155 void executeTests(const std::string& _name, const std::string& _testPathAppendix, const std::string& _fillerPathAppendix, std::function<void(json_spirit::mValue&, bool)> doTests, bool _addFillerSuffix = true);
156 void userDefinedTest(std::function<void(json_spirit::mValue&, bool)> doTests);
158 eth::LastHashes lastHashes(u256 _currentBlockNumber);
162 
163 //Fill Test Functions
164 int createRandomTest(std::vector<char*> const& _parameters);
165 void doTransactionTests(json_spirit::mValue& _v, bool _fillin);
166 void doStateTests(json_spirit::mValue& v, bool _fillin);
167 void doVMTests(json_spirit::mValue& v, bool _fillin);
168 void doBlockchainTests(json_spirit::mValue& _v, bool _fillin);
169 void doRlpTests(json_spirit::mValue& v, bool _fillin);
170 
171 
174 class Listener
175 {
176 public:
177  virtual ~Listener() = default;
178 
179  virtual void suiteStarted(std::string const&) {}
180  virtual void testStarted(std::string const& _name) = 0;
181  virtual void testFinished(int64_t _gasUsed) = 0;
182 
183  static void registerListener(Listener& _listener);
184  static void notifySuiteStarted(std::string const& _name);
185  static void notifyTestStarted(std::string const& _name);
186  static void notifyTestFinished(int64_t _gasUsed);
187 
190  {
191  int64_t m_gasUsed = -1;
192  public:
193  ExecTimeGuard(std::string const& _testName) { notifyTestStarted(_testName); }
194  ~ExecTimeGuard() { notifyTestFinished(m_gasUsed); }
195  ExecTimeGuard(ExecTimeGuard const&) = delete;
196  ExecTimeGuard& operator=(ExecTimeGuard) = delete;
197  void setGasUsed(int64_t _gas) { m_gasUsed = _gas; }
198  };
199 };
200 
201 }
202 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
#define function(a, b, c, d, k, s)
Helper class for managing data when running state tests.
string netIdToString(eth::Network _netId)
Definition: TestHelper.cpp:93
uint8_t byte
Definition: Common.h:57
bytes importData(json_spirit::mObject const &_o)
Definition: TestHelper.cpp:226
ExecTimeGuard(std::string const &_testName)
Definition: TestHelper.h:193
Allows observing test execution process.
Definition: TestHelper.h:174
void doRlpTests(json_spirit::mValue &v, bool _fillin)
Definition: rlp.cpp:52
Fixture class for boost output when running testeth.
Network
The network id.
Definition: GenesisInfo.h:34
void checkOutput(bytesConstRef _output, json_spirit::mObject &_o)
Definition: TestHelper.cpp:318
Encapsulation of a block header.
Definition: BlockHeader.h:95
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>> bigint
Definition: Common.h:121
std::vector< Transaction > Transactions
Nice name for vector of Transaction.
Definition: Transaction.h:121
#define c(i)
void connectClients(Client &c1, Client &c2)
Definition: TestHelper.cpp:49
void createRandomTest()
Definition: boostTest.cpp:60
u256 bid(eth::TransactionPriority=eth::TransactionPriority::Medium) const override
Definition: TestHelper.h:120
Access a block of memory.
Definition: misc.h:2233
Model of an Ethereum state, essentially a facade for the trie.
Definition: State.h:161
Class for handling testeth custom options.
json_spirit::mObject fillJsonWithTransaction(Transaction const &_txn)
Definition: TestHelper.cpp:124
TransactionPriority
Definition: GasPricer.h:34
void updateEthashSeal(dev::eth::BlockHeader &_header, h256 const &_mixHash, h64 const &_nonce)
Definition: TestHelper.cpp:576
void checkLog(LogEntries _resultLogs, LogEntries _expectedLogs)
Definition: TestHelper.cpp:361
void doStateTests(json_spirit::mValue &_v, bool _fillin)
Definition: StateTests.cpp:42
Active model of a block within the block chain.
Definition: Block.h:73
std::vector< h256 > LastHashes
Definition: ExtVMFace.h:191
Base class for all exceptions.
Definition: Exceptions.h:39
void userDefinedTest(std::function< void(json_spirit::mValue &, bool)> doTests)
Definition: TestHelper.cpp:386
mConfig::Array_type mArray
dev::eth::BlockHeader constructHeader(h256 const &_parentHash, h256 const &_sha3Uncles, Address const &_author, h256 const &_stateRoot, h256 const &_transactionsRoot, h256 const &_receiptsRoot, dev::eth::LogBloom const &_logBloom, u256 const &_difficulty, u256 const &_number, u256 const &_gasLimit, u256 const &_gasUsed, u256 const &_timestamp, bytes const &_extraData)
Definition: TestHelper.cpp:552
void doTransactionTests(json_spirit::mValue &_v, bool _fillin)
byte toByte(json_spirit::mValue const &_v)
Definition: TestHelper.cpp:208
RLPStream createRLPStreamFromTransactionFields(json_spirit::mObject const &_tObj)
Definition: TestHelper.cpp:499
Test started/finished notification RAII helper.
Definition: TestHelper.h:189
json_spirit::mArray exportLog(eth::LogEntries _logs)
Definition: TestHelper.cpp:176
std::vector< byte > bytes
Definition: Common.h:75
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
json_spirit::mObject fillJsonWithState(State const &_state)
Definition: TestHelper.cpp:139
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
bytes importCode(json_spirit::mObject &_o)
Definition: TestHelper.cpp:281
void copyFile(std::string const &_source, std::string const &_destination)
Definition: TestHelper.cpp:492
Encodes a transaction, ready to be exported to or freshly imported from RLP.
Definition: Transaction.h:84
std::unordered_map< Address, AccountMask > AccountMaskMap
Definition: Account.h:240
mConfig::Object_type mObject
eth::Network stringToNetId(std::string const &_netname)
std::string compileLLL(std::string const &_code)
void doVMTests(json_spirit::mValue &_v, bool _fillin)
Definition: vm.cpp:286
void replaceLLLinState(json_spirit::mObject &_o)
Definition: TestHelper.cpp:237
u256 ask(eth::Block const &) const override
Definition: TestHelper.h:119
LastHashes lastHashes(u256 _currentBlockNumber)
Definition: TestHelper.cpp:544
void doBlockchainTests(json_spirit::mValue &_v, bool _fillin)
bytes importByteArray(std::string const &_str)
Definition: TestHelper.cpp:221
void checkCallCreates(eth::Transactions _resultCallCreates, eth::Transactions _expectedCallCreates)
Definition: TestHelper.cpp:373
virtual void suiteStarted(std::string const &)
Definition: TestHelper.h:179
void checkStorage(std::map< u256, u256 > _expectedStore, std::map< u256, u256 > _resultStore, Address _expectedAddr)
void executeTests(const std::string &_name, const std::string &_testPathAppendix, const std::string &_fillerPathAppendix, std::function< void(json_spirit::mValue &, bool)> doTests, bool _addFillerSuffix=true)
Class for writing to an RLP bytestream.
Definition: RLP.h:383
void mine(Client &c, int numBlocks)
Definition: TestHelper.cpp:39
LogEntries importLog(json_spirit::mArray &_a)
Definition: TestHelper.cpp:298
u256 toInt(json_spirit::mValue const &_v)
Definition: TestHelper.cpp:195
bigint const c_max256plus1
Definition: TestHelper.h:61
std::vector< LogEntry > LogEntries
Definition: ExtVMFace.h:110