ClientTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file ClientTest.cpp
  15. * @author Dimitry Khokhlov <dimitry@ethdev.com>
  16. * @date 2016
  17. */
  18. #include <libethereum/EthereumHost.h>
  19. #include <libethereum/ClientTest.h>
  20. using namespace std;
  21. using namespace dev;
  22. using namespace dev::eth;
  23. using namespace p2p;
  24. ClientTest& dev::eth::asClientTest(Interface& _c)
  25. {
  26. return dynamic_cast<ClientTest&>(_c);
  27. }
  28. ClientTest* dev::eth::asClientTest(Interface* _c)
  29. {
  30. return &dynamic_cast<ClientTest&>(*_c);
  31. }
  32. ClientTest::ClientTest(
  33. ChainParams const& _params,
  34. int _networkID,
  35. p2p::Host* _host,
  36. std::shared_ptr<GasPricer> _gpForAdoption,
  37. std::string const& _dbPath,
  38. WithExisting _forceAction,
  39. TransactionQueue::Limits const& _limits
  40. ):
  41. Client(_params, _networkID, _host, _gpForAdoption, _dbPath, _forceAction, _limits)
  42. {}
  43. void ClientTest::setChainParams(string const& _genesis)
  44. {
  45. ChainParams params;
  46. try
  47. {
  48. params = params.loadConfig(_genesis);
  49. if (params.sealEngineName != "NoProof")
  50. BOOST_THROW_EXCEPTION(ChainParamsNotNoProof() << errinfo_comment("Provided configuration is not well formatted."));
  51. reopenChain(params, WithExisting::Kill);
  52. setAuthor(params.author); //for some reason author is not being set
  53. }
  54. catch (...)
  55. {
  56. BOOST_THROW_EXCEPTION(ChainParamsInvalid() << errinfo_comment("Provided configuration is not well formatted."));
  57. }
  58. }
  59. bool ClientTest::addBlock(string const& _rlp)
  60. {
  61. if (auto h = m_host.lock())
  62. h->noteNewBlocks();
  63. bytes rlpBytes = fromHex(_rlp, WhenError::Throw);
  64. RLP blockRLP(rlpBytes);
  65. return (m_bq.import(blockRLP.data(), true) == ImportResult::Success);
  66. }
  67. void ClientTest::modifyTimestamp(u256 const& _timestamp)
  68. {
  69. Block block(chainParams().accountStartNonce);
  70. DEV_READ_GUARDED(x_preSeal)
  71. block = m_preSeal;
  72. Transactions transactions;
  73. DEV_READ_GUARDED(x_postSeal)
  74. transactions = m_postSeal.pending();
  75. block.resetCurrent(_timestamp);
  76. DEV_WRITE_GUARDED(x_preSeal)
  77. m_preSeal = block;
  78. auto lastHashes = bc().lastHashes();
  79. for (auto const& t: transactions)
  80. block.execute(lastHashes, t);
  81. DEV_WRITE_GUARDED(x_working)
  82. m_working = block;
  83. DEV_READ_GUARDED(x_postSeal)
  84. m_postSeal = block;
  85. onPostStateChanged();
  86. }
  87. void ClientTest::mineBlocks(unsigned _count)
  88. {
  89. m_blocksToMine = _count;
  90. startSealing();
  91. }
  92. void ClientTest::onNewBlocks(h256s const& _blocks, h256Hash& io_changed)
  93. {
  94. Client::onNewBlocks(_blocks, io_changed);
  95. if(--m_blocksToMine <= 0)
  96. stopSealing();
  97. }