BlockChainHelper.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 BlockChainHelper.h
  15. * @author Dimitry Khokhlov <dimitry@ethdev.com>
  16. * @date 2015
  17. */
  18. #pragma once
  19. #include "JsonSpiritHeaders.h"
  20. #include <libethereum/BlockChain.h>
  21. #include <libethereum/TransactionQueue.h>
  22. #include <libdevcore/TransientDirectory.h>
  23. #include <libethashseal/GenesisInfo.h>
  24. #include <libethashseal/Ethash.h>
  25. using namespace std;
  26. using namespace json_spirit;
  27. using namespace dev;
  28. using namespace dev::eth;
  29. namespace dev { namespace test {
  30. struct BlockStateUndefined : virtual Exception {};
  31. class TestTransaction;
  32. class TestBlock;
  33. class TestBlockChain;
  34. class TestTransaction
  35. {
  36. public:
  37. TestTransaction(json_spirit::mObject const& _o);
  38. TestTransaction(Transaction const& _tr) : m_transaction(_tr) {}
  39. Transaction const& transaction() const { return m_transaction; }
  40. json_spirit::mObject& jsonObject() { return m_jsonTransaction; }
  41. static TestTransaction defaultTransaction(u256 const& _nonce = 1, u256 const& _gasPrice = 1, u256 const& _gasLimit = 50000, bytes const& _data = bytes());
  42. private:
  43. json_spirit::mObject m_jsonTransaction;
  44. Transaction m_transaction;
  45. };
  46. class TestBlock
  47. {
  48. public:
  49. TestBlock();
  50. TestBlock(std::string const& _blockRlp);
  51. TestBlock(mObject const& _blockObj, mObject const& _stateObj);
  52. TestBlock(TestBlock const& _original);
  53. TestBlock& operator=(TestBlock const& _original);
  54. void addTransaction(TestTransaction const& _tr);
  55. void addUncle(TestBlock const& _uncle);
  56. void setUncles(vector<TestBlock> const& _uncles);
  57. void setPremine(std::string const& _parameter) { m_premineUpdate[_parameter] = true; }
  58. void noteDirty() { m_dirty = true; }
  59. void mine(TestBlockChain const& _bc);
  60. void updateNonce(TestBlockChain const& _bc);
  61. void verify(TestBlockChain const& _bc) const;
  62. void setBlockHeader(BlockHeader const& _header);
  63. void setState(State const& _state);
  64. void clearState();
  65. BlockHeader const& premineHeader() { return m_premineHeader; } //should return fields according to m_premineUpdate. this is needed to check that premine chanes was not lost during mining .
  66. dev::bytes const& bytes() const { return m_bytes; }
  67. bytesConstRef receipts() const { return bytesConstRef(&m_receipts.out()[0], m_receipts.out().size()); }
  68. AccountMap const& accountMap() const { return m_accountMap; }
  69. State const& state() const { if (m_state.get() == 0) BOOST_THROW_EXCEPTION(BlockStateUndefined() << errinfo_comment("Block State is Nulled")); return *m_state.get(); }
  70. BlockHeader const& blockHeader() const { return m_blockHeader;}
  71. TransactionQueue const& transactionQueue() const { return m_transactionQueue; }
  72. TransactionQueue & transactionQueue() { return m_transactionQueue; }
  73. vector<TestTransaction> const& testTransactions() const { return m_testTransactions; }
  74. vector<TestBlock> const& uncles() const { return m_uncles; }
  75. Address const& beneficiary() const { return m_blockHeader.author(); }
  76. private:
  77. BlockHeader constructBlock(mObject const& _o, h256 const& _stateRoot);
  78. dev::bytes createBlockRLPFromFields(mObject const& _tObj, h256 const& _stateRoot = h256{});
  79. void recalcBlockHeaderBytes();
  80. void copyStateFrom(State const& _state);
  81. void populateFrom(TestBlock const& _original);
  82. void premineUpdate(BlockHeader& info);
  83. bool m_dirty;
  84. BlockHeader m_blockHeader;
  85. vector<TestBlock> m_uncles;
  86. std::unique_ptr<State> m_state;
  87. TransactionQueue m_transactionQueue;
  88. BlockQueue m_uncleQueue;
  89. dev::bytes m_bytes;
  90. std::unique_ptr<TransientDirectory> m_tempDirState;
  91. vector<TestTransaction> m_testTransactions;
  92. std::map<std::string, bool> m_premineUpdate; //Test Header alterate options. TODO: Do we really need this?
  93. BlockHeader m_premineHeader;
  94. AccountMap m_accountMap; //Needed for genesis state
  95. RLPStream m_receipts;
  96. };
  97. class TestBlockChain
  98. {
  99. public:
  100. TestBlockChain(bool _noProof = false): TestBlockChain(defaultGenesisBlock(), _noProof) {}
  101. TestBlockChain(TestBlock const& _genesisBlock, bool _noProof = false);
  102. void reset(TestBlock const& _genesisBlock, bool _noProof = false);
  103. bool addBlock(TestBlock const& _block);
  104. vector<TestBlock> syncUncles(vector<TestBlock> const& _uncles);
  105. TestBlock const& topBlock() { return m_lastBlock; }
  106. BlockChain const& interface() const { return *m_blockChain;}
  107. BlockChain& interfaceUnsafe() const { return *m_blockChain;}
  108. TestBlock const& testGenesis() const { return m_genesisBlock; }
  109. static TestBlock defaultGenesisBlock(u256 const& _gasLimit = DefaultBlockGasLimit);
  110. static AccountMap defaultAccountMap();
  111. static eth::Network s_sealEngineNetwork;
  112. private:
  113. std::unique_ptr<BlockChain> m_blockChain;
  114. TestBlock m_genesisBlock;
  115. TestBlock m_lastBlock;
  116. std::unique_ptr<TransientDirectory> m_tempDirBlockchain;
  117. };
  118. }}