Ethereum  PoC-8
The C++ Implementation of Ethereum
ClientTest.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 */
22 #include <libdevcore/CommonJS.h>
23 #include <libethashseal/Ethash.h>
24 #include <libethereum/ClientTest.h>
26 #include <boost/filesystem/path.hpp>
27 #include <future>
28 
29 using namespace std;
30 using namespace dev;
31 using namespace dev::eth;
32 using namespace p2p;
33 namespace fs = boost::filesystem;
34 
36 {
37  return dynamic_cast<ClientTest&>(_c);
38 }
39 
41 {
42  return &dynamic_cast<ClientTest&>(*_c);
43 }
44 
45 ClientTest::ClientTest(ChainParams const& _params, int _networkID, p2p::Host& _host,
46  std::shared_ptr<GasPricer> _gpForAdoption, fs::path const& _dbPath, WithExisting _forceAction,
47  TransactionQueue::Limits const& _limits)
48  : Client(
49  _params, _networkID, _host, _gpForAdoption, _dbPath, std::string(), _forceAction, _limits)
50 {}
51 
53 {
54  m_signalled.notify_all(); // to wake up the thread from Client::doWork()
55  terminate();
56 }
57 
58 void ClientTest::setChainParams(string const& _genesis)
59 {
60  ChainParams params;
61  try
62  {
63  params = params.loadConfig(_genesis);
64  if (params.sealEngineName != NoProof::name() && params.sealEngineName != Ethash::name())
65  BOOST_THROW_EXCEPTION(
66  ChainParamsInvalid() << errinfo_comment("Seal engine is not supported!"));
67 
69  }
70  catch (std::exception const& ex)
71  {
72  BOOST_THROW_EXCEPTION(ChainParamsInvalid() << errinfo_comment(ex.what()));
73  }
74 }
75 
76 void ClientTest::modifyTimestamp(int64_t _timestamp)
77 {
78  Block block(chainParams().accountStartNonce);
80  block = m_preSeal;
81 
85  block.resetCurrent(_timestamp);
86 
88  m_preSeal = block;
89 
90  auto& lastHashes = bc().lastBlockHashes();
91  assert(bc().currentHash() == block.info().parentHash());
92  for (auto const& t: transactions)
93  block.execute(lastHashes, t);
94 
96  m_working = block;
98  m_postSeal = block;
99 
101 }
102 
103 bool ClientTest::mineBlocks(unsigned _count) noexcept
104 {
105  if (wouldSeal())
106  return false;
107  try
108  {
109  unsigned sealedBlocks = 0;
110  auto sealHandler = setOnBlockSealed([this, _count, &sealedBlocks](bytes const&) {
111  if (++sealedBlocks == _count)
112  stopSealing();
113  });
114 
115  std::promise<void> allBlocksImported;
116  unsigned importedBlocks = 0;
117  auto chainChangedHandler = setOnChainChanged(
118  [_count, &importedBlocks, &allBlocksImported](h256s const&, h256s const& _newBlocks) {
119  importedBlocks += _newBlocks.size();
120  if (importedBlocks == _count)
121  allBlocksImported.set_value();
122  });
123 
124  startSealing();
125  future_status ret = allBlocksImported.get_future().wait_for(
126  std::chrono::seconds(m_singleBlockMaxMiningTimeInSeconds * _count));
127  return (ret == future_status::ready);
128  }
129  catch (std::exception const&)
130  {
131  LOG(m_logger) << boost::current_exception_diagnostic_information();
132  return false;
133  }
134 }
135 
137 {
138  auto h = m_host.lock();
139  if (!h)
140  return false;
141 
142  h->completeSync();
143  return true;
144 }
145 
146 h256 ClientTest::importRawBlock(const string& _blockRLP)
147 {
148  bytes blockBytes = jsToBytes(_blockRLP, OnFailed::Throw);
149  h256 blockHash = BlockHeader::headerHashFromBlock(blockBytes);
150  ImportResult result = queueBlock(blockBytes, true);
151  if (result != ImportResult::Success)
152  BOOST_THROW_EXCEPTION(ImportBlockFailed() << errinfo_importResult(result));
153 
154  if (auto h = m_host.lock())
155  h->noteNewBlocks();
156 
157  bool moreToImport = true;
158  while (moreToImport)
159  {
160  tie(ignore, moreToImport, ignore) = syncQueue(100000);
161  this_thread::sleep_for(chrono::milliseconds(100));
162  }
163  return blockHash;
164 }
dev::eth::Client::m_preSeal
Block m_preSeal
The present state of the client.
Definition: Client.h:343
dev::eth::Client::reopenChain
void reopenChain(ChainParams const &_p, WithExisting _we=WithExisting::Trust)
Reloads the blockchain. Just for debug use.
Definition: Client.cpp:269
dev::eth::ClientTest::setChainParams
void setChainParams(std::string const &_genesis)
Definition: ClientTest.cpp:58
dev::eth::Client::syncQueue
std::tuple< ImportRoute, bool, unsigned > syncQueue(unsigned _max=1)
Freeze worker thread and sync some of the block queue.
Definition: Client.cpp:164
dev::eth::Client::bc
BlockChain & bc() override
InterfaceStub methods.
Definition: Client.h:241
dev::eth::ImportResult
ImportResult
Definition: Common.h:97
dev::eth::ClientTest::modifyTimestamp
void modifyTimestamp(int64_t _timestamp)
Definition: ClientTest.cpp:76
dev::eth::Client::x_working
SharedMutex x_working
Lock on m_working.
Definition: Client.h:346
dev::eth::Transactions
std::vector< Transaction > Transactions
Nice name for vector of Transaction.
Definition: Transaction.h:122
dev::eth::ClientTest::importRawBlock
h256 importRawBlock(std::string const &_blockRLP)
Definition: ClientTest.cpp:146
dev::eth::ChainParams
Definition: ChainParams.h:38
dev::eth::Client::m_host
std::weak_ptr< EthereumCapability > m_host
Definition: Client.h:353
dev::eth::ClientTest::completeSync
bool completeSync()
Definition: ClientTest.cpp:136
dev::eth::ClientBase::transactions
Transactions transactions(h256 _blockHash) const override
Definition: ClientBase.cpp:376
dev::eth
Definition: BasicAuthority.h:32
dev::eth::Client::chainParams
ChainParams const & chainParams() const
Get information on this chain.
Definition: Client.h:89
CommonJS.h
dev::FixedHash< 32 >
dev::WithExisting::Kill
@ Kill
dev::WithExisting
WithExisting
Definition: Common.h:292
LOG
#define LOG
Definition: Log.h:63
dev::eth::ChainOperationParams::sealEngineName
std::string sealEngineName
The chain sealer name: e.g. Ethash, NoProof, BasicAuthority.
Definition: ChainOperationParams.h:75
dev::eth::Client::x_preSeal
SharedMutex x_preSeal
Lock on m_preSeal.
Definition: Client.h:342
EthereumCapability.h
dev::h256s
std::vector< h256 > h256s
Definition: FixedHash.h:361
dev::eth::BlockHeader::parentHash
h256 const & parentHash() const
Definition: BlockHeader.h:157
dev::eth::Client::block
dev::eth::Block block(h256 const &_blockHash, PopulationStatistics *o_stats) const
Get the block.
Definition: Client.cpp:774
dev::eth::TransactionQueue::Limits
Definition: TransactionQueue.h:47
dev::OnFailed::Throw
@ Throw
dev::eth::Client
Main API hub for interfacing with Ethereum.
Definition: Client.h:77
dev::eth::ClientTest
Definition: ClientTest.h:37
dev::eth::Block::execute
ExecutionResult execute(LastBlockHashesFace const &_lh, Transaction const &_t, Permanence _p=Permanence::Committed, OnOpFunc const &_onOp=OnOpFunc())
Definition: Block.cpp:657
DEV_WRITE_GUARDED
#define DEV_WRITE_GUARDED(MUTEX)
Definition: Guards.h:136
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::eth::Block::pending
Transactions const & pending() const
Get the list of pending transactions.
Definition: Block.h:183
dev::jsToBytes
bytes jsToBytes(string const &_s, OnFailed _f)
Definition: CommonJS.cpp:31
dev::eth::BlockChain::lastBlockHashes
LastBlockHashesFace const & lastBlockHashes() const
Definition: BlockChain.h:186
name
const char * name
Definition: VMFactory.cpp:49
dev::eth::Client::queueBlock
ImportResult queueBlock(bytes const &_block, bool _isSafe=false)
Queues a block for import.
Definition: Client.cpp:157
dev::eth::Interface
Main API hub for interfacing with Ethereum.
Definition: Interface.h:68
dev::eth::ChainParams::loadConfig
ChainParams loadConfig(std::string const &_json, h256 const &_stateRoot={}, const boost::filesystem::path &_configPath={}) const
load config
Definition: ChainParams.cpp:57
DEV_READ_GUARDED
#define DEV_READ_GUARDED(MUTEX)
Definition: Guards.h:134
dev::eth::NoProof::name
static std::string name()
Definition: SealEngine.h:144
dev::eth::Block::resetCurrent
void resetCurrent(int64_t _timestamp=utcTime())
Definition: Block.cpp:118
dev::eth::Client::m_signalled
std::condition_variable m_signalled
Definition: Client.h:356
std
Definition: FixedHash.h:393
dev::eth::errinfo_importResult
boost::error_info< struct tag_importResult, ImportResult > errinfo_importResult
Definition: Exceptions.h:42
dev::eth::Success
@ Success
Definition: Common.h:223
dev::eth::ClientTest::~ClientTest
~ClientTest()
Definition: ClientTest.cpp:52
ClientTest.h
dev::eth::ClientTest::mineBlocks
bool mineBlocks(unsigned _count) noexcept
Definition: ClientTest.cpp:103
dev
Definition: Address.cpp:21
dev::eth::Client::m_working
Block m_working
The state of the client which we're sealing (i.e. it'll have all the rewards added),...
Definition: Client.h:347
dev::eth::Block::info
BlockHeader const & info() const
Get the header information on the present block.
Definition: Block.h:273
dev::Worker::terminate
void terminate()
Blocks caller into worker thread has finished.
Definition: Worker.cpp:116
dev::eth::Client::onPostStateChanged
void onPostStateChanged()
Definition: Client.cpp:576
dev::eth::Client::m_postSeal
Block m_postSeal
The state of the client which we're sealing (i.e. it'll have all the rewards added).
Definition: Client.h:345
dev::eth::BlockHeader::headerHashFromBlock
static h256 headerHashFromBlock(bytes const &_block)
Definition: BlockHeader.h:108
dev::eth::asClientTest
ClientTest & asClientTest(Interface &_c)
Definition: ClientTest.cpp:35
dev::eth::Block
Active model of a block within the block chain. Keeps track of all transactions, receipts and state f...
Definition: Block.h:69
dev::eth::Client::x_postSeal
SharedMutex x_postSeal
Lock on m_postSeal.
Definition: Client.h:344
dev::errinfo_comment
boost::error_info< struct tag_comment, std::string > errinfo_comment
Definition: Assertions.h:69