Fabcoin Core  0.16.2
P2P Digital Currency
TestUtils.cpp
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  */
20 #include <thread>
21 #include <boost/test/unit_test.hpp>
22 #include <boost/filesystem.hpp>
23 #include <libdevcrypto/Common.h>
28 
29 using namespace std;
30 using namespace dev;
31 using namespace dev::eth;
32 using namespace dev::test;
33 
34 namespace dev
35 {
36 namespace test
37 {
38 
39 bool getCommandLineOption(std::string const& _name);
40 std::string getCommandLineArgument(std::string const& _name, bool _require = false);
41 
42 }
43 }
44 
45 bool dev::test::getCommandLineOption(string const& _name)
46 {
47  auto argc = boost::unit_test::framework::master_test_suite().argc;
48  auto argv = boost::unit_test::framework::master_test_suite().argv;
49  bool result = false;
50  for (auto i = 0; !result && i < argc; ++i)
51  result = _name == argv[i];
52  return result;
53 }
54 
55 std::string dev::test::getCommandLineArgument(string const& _name, bool _require)
56 {
57  auto argc = boost::unit_test::framework::master_test_suite().argc;
58  auto argv = boost::unit_test::framework::master_test_suite().argv;
59  for (auto i = 1; i < argc; ++i)
60  {
61  string str = argv[i];
62  if (_name == str.substr(0, _name.size()))
63  return str.substr(str.find("=") + 1);
64  }
65  if (_require)
66  BOOST_ERROR("Failed getting command line argument: " << _name << " from: " << argv);
67  return "";
68 }
69 
70 LoadTestFileFixture::LoadTestFileFixture()
71 {
72  m_json = loadJsonFromFile(toTestFilePath(getCommandLineArgument("--eth_testfile")));
73 }
74 
75 void ParallelFixture::enumerateThreads(std::function<void()> callback) const
76 {
77  size_t threadsCount = std::stoul(getCommandLineArgument("--eth_threads"), nullptr, 10);
78 
79  vector<thread> workers;
80  for (size_t i = 0; i < threadsCount; i++)
81  workers.emplace_back(callback);
82 
83  for_each(workers.begin(), workers.end(), [](thread &t)
84  {
85  t.join();
86  });
87 }
88 
89 void BlockChainFixture::enumerateBlockchains(std::function<void(Json::Value const&, dev::eth::BlockChain const&, State state)> callback) const
90 {
91  for (string const& name: m_json.getMemberNames())
92  {
93  BlockChainLoader bcl(m_json[name]);
94  callback(m_json[name], bcl.bc(), bcl.state());
95  }
96 }
97 
98 void ClientBaseFixture::enumerateClients(std::function<void(Json::Value const&, dev::eth::ClientBase&)> callback) const
99 {
100  enumerateBlockchains([&callback](Json::Value const& _json, BlockChain const& _bc, State _state) -> void
101  {
102  cerr << "void ClientBaseFixture::enumerateClients. FixedClient now accepts block not sate!" << endl;
103  _state.commit(State::CommitBehaviour::KeepEmptyAccounts); //unused variable. remove this line
104  eth::Block b(Block::Null);
105  b.noteChain(_bc);
106  FixedClient client(_bc, b);
107  callback(_json, client);
108  });
109 }
110 
111 void ParallelClientBaseFixture::enumerateClients(std::function<void(Json::Value const&, dev::eth::ClientBase&)> callback) const
112 {
113  ClientBaseFixture::enumerateClients([this, &callback](Json::Value const& _json, dev::eth::ClientBase& _client) -> void
114  {
115  // json is being copied here
116  enumerateThreads([callback, _json, &_client]() -> void
117  {
118  callback(_json, _client);
119  });
120  });
121 }
122 
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)
Json::Value loadJsonFromFile(std::string const &_path)
Definition: Common.cpp:58
void commit(CommitBehaviour _commitBehaviour)
Commit all changes waiting in the address cache to the DB.
Definition: State.cpp:210
Implements the blockchain database.
Definition: BlockChain.h:105
eth::BlockChain const & bc() const
void noteChain(BlockChain const &_bc)
Note the fact that this block is being used with a particular chain.
Definition: Block.cpp:140
std::hash for asio::adress
Definition: Common.h:323
Model of an Ethereum state, essentially a facade for the trie.
Definition: State.h:161
Active model of a block within the block chain.
Definition: Block.h:73
Config::Value_type Value
const char * name
Definition: rest.cpp:36
Should be used to load test blockchain from json file Loads the blockchain from json, creates temporary directory to store it, removes the directory on dealloc.
std::string getCommandLineArgument(std::string const &_name, bool _require=false)
eth::State const & state() const
#define b(i, j)
std::string toTestFilePath(std::string const &_filename)
Definition: Common.cpp:72
mvp implementation of ClientBase Doesn&#39;t support mining interface
Definition: FixedClient.h:37
bool getCommandLineOption(std::string const &_name)